rust_rsm/rsm/socket/
mod.rs1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4#![allow(dead_code)]
5
6use crate::net_ext::{self,mac_addr::mac_addr_t};
7use std::net::SocketAddr;
8use crate::common::errcode;
9use crate::rsm;
10use serde::{Deserialize, Serialize};
11#[cfg(windows)]
12use windows_sys::Win32::Networking::WinSock;
13
14#[cfg(unix)]
15use libc;
16
17pub mod socket;
18pub mod poll;
19pub mod socketpool;
20pub mod udpsocket;
21pub mod tcplistener;
22pub mod tcpsocket;
23
24
25
26pub type socket_event_t= rsm::rsm_socket_event_t;
27
28#[derive(Clone,Debug,PartialEq,Copy)]
29pub enum SOCKET_ADDRESS_FAMILY {
30 SOCKET_INET = 1,
31 SOCKET_INET6 = 2,
32 SOCKET_RAW_PACKET=4,
33}
34
35#[derive(Clone,Debug,PartialEq,Copy,Serialize,Deserialize)]
36pub enum SOCKET_TYPE {
37 PROTO_RAW = 0,
38 PROTO_DGRAM = 1,
39 PROTO_STREAM = 2,
40}
41
42#[derive(Clone,Debug,PartialEq,Copy)]
43pub enum SOCKET_STATE {
44 SOCK_INIT = 0,
45 SOCK_BIND = 1,
46 SOCK_LISTENING = 2,
47 SOCK_CONNECTING = 3,
48 SOCK_CONNECTED = 4,
49}
50
51#[derive(Clone,Debug,PartialEq,Copy)]
54pub enum SOCKET_LB_POLICY {
55 SOCK_LB_ALL_INSTANCE=0,
57 SOCK_LB_CALLER_INSTANCE=1,
59 SOCK_LB_EXCLUDE_CALLER_INSTANCE=2,
61}
62
63#[derive(Clone,Debug)]
64pub struct Socket {
65 sock_id:i32,
66 os_fd:net_ext::RawFdType,
67 sock_af:SOCKET_ADDRESS_FAMILY,
68 sock_type:SOCKET_TYPE,
69 proto:u8,
70 state:SOCKET_STATE,
71 tcp_server:bool,
72 lb_policy:SOCKET_LB_POLICY,
73 local_addr:SocketAddr,
74 peer_addr:SocketAddr,
75}
76pub trait AsSocket {
77 fn as_socket(&mut self)->Socket;
78}
79
80#[derive(Clone,Debug)]
81pub struct UdpSocket {
82 sck_idx:i32,
83}
84
85#[derive(Clone,Debug)]
86pub struct TcpListener {
87 sck_idx:i32,
88}
89
90#[derive(Clone,Debug)]
91pub struct TcpSocket {
92 sck_idx:i32,
93}
94
95#[derive(Clone,Debug)]
96pub struct RawSocket {
97 sck_idx:i32,
98}
99
100#[derive(Clone,Debug)]
101pub struct PacketSocket {
102 sck_idx:i32,
103 if_idx:u32,
104 mac:mac_addr_t
105}
106
107#[cfg(windows)]
108fn socket(sock_af:SOCKET_ADDRESS_FAMILY,sock_type:SOCKET_TYPE,proto:u8)->Result<net_ext::RawFdType,errcode::RESULT> {
109 let af = match sock_af {
110 SOCKET_ADDRESS_FAMILY::SOCKET_INET=>WinSock::AF_INET,
111 SOCKET_ADDRESS_FAMILY::SOCKET_INET6=>WinSock::AF_INET6,
112 _=>WinSock::AF_UNSPEC,
113 };
114
115 let stype = match sock_type {
116 SOCKET_TYPE::PROTO_RAW=>WinSock::SOCK_RAW,
117 SOCKET_TYPE::PROTO_STREAM=>WinSock::SOCK_STREAM,
118 SOCKET_TYPE::PROTO_DGRAM=>WinSock::SOCK_DGRAM,
119 };
120
121 let s = unsafe { WinSock::WSASocketA(af as i32, stype as i32, proto as i32,
122 std::ptr::null(),0,WinSock::WSA_FLAG_OVERLAPPED) };
123 return Ok(s as net_ext::RawFdType)
124}
125
126#[cfg(unix)]
127fn socket(sock_af:SOCKET_ADDRESS_FAMILY,sock_type:SOCKET_TYPE,proto:u8)->Result<net_ext::RawFdType,errcode::RESULT> {
128 let af = match sock_af {
129 SOCKET_ADDRESS_FAMILY::SOCKET_INET=>libc::AF_INET,
130 SOCKET_ADDRESS_FAMILY::SOCKET_INET6=>libc::AF_INET6,
131 SOCKET_ADDRESS_FAMILY::SOCKET_RAW_PACKET=>libc::AF_PACKET,
132 };
133
134 let stype = match sock_type {
135 SOCKET_TYPE::PROTO_RAW=>libc::SOCK_RAW,
136 SOCKET_TYPE::PROTO_STREAM=>libc::SOCK_STREAM,
137 SOCKET_TYPE::PROTO_DGRAM=>libc::SOCK_DGRAM,
138 };
139
140 let s = unsafe { libc::socket(af as i32, stype as i32, proto as i32) };
141 return Ok(s as net_ext::RawFdType)
142}