libudt4_sys/
lib.rs

1extern crate libc;
2
3use libc::{c_int, c_char, c_void, c_uchar};
4
5#[cfg(windows)]
6extern crate winapi;
7
8#[cfg(windows)]
9mod plat_imports {
10    pub use winapi::SOCKADDR as sockaddr;
11}
12#[cfg(not(windows))]
13mod plat_imports {
14    pub use libc::sockaddr;
15}
16
17use plat_imports::*;
18
19
20pub type UDTSOCKET = c_int;
21pub type SYSSOCKET = c_int;
22
23/// success operation.
24pub const SUCCESS : c_int = 0;
25/// connection setup failure.
26pub const ECONNSETUP  : c_int = 1000;
27/// server does not exist.
28pub const ENOSERVER   : c_int = 1001;
29/// connection request was rejected by server.
30pub const ECONNREJ    : c_int = 1002;
31/// could not create/configure UDP socket.
32pub const ESOCKFAIL   : c_int = 1003;
33/// connection request was aborted due to security reasons.
34pub const ESECFAIL    : c_int = 1004;
35/// connection failure.
36pub const ECONNFAIL   : c_int = 2000;
37/// connection was broken.
38pub const ECONNLOST   : c_int = 2001;
39/// connection does not exist.
40pub const ENOCONN : c_int = 2002;
41/// system resource failure.
42pub const ERESOURCE   : c_int = 3000;
43/// could not create new thread.
44pub const ETHREAD : c_int = 3001;
45/// no memory space.
46pub const ENOBUF  : c_int = 3002;
47/// file access error.
48pub const EFILE   : c_int = 4000;
49/// invalid read offset.
50pub const EINVRDOFF   : c_int = 4001;
51/// no read permission.
52pub const ERDPERM : c_int = 4002;
53/// invalid write offset.
54pub const EINVWROFF   : c_int = 4003;
55/// no write permission.
56pub const EWRPERM : c_int = 4004;
57/// operation not supported.
58pub const EINVOP  : c_int = 5000;
59/// cannot execute the operation on a bound socket.
60pub const EBOUNDSOCK  : c_int = 5001;
61/// cannot execute the operation on a connected socket.
62pub const ECONNSOCK   : c_int = 5002;
63/// bad parameters.
64pub const EINVPARAM   : c_int = 5003;
65/// invalid UDT socket.
66pub const EINVSOCK    : c_int = 5004;
67/// cannot listen on unbound socket.
68pub const EUNBOUNDSOCK    : c_int = 5005;
69/// (accept) socket is not in listening state.
70pub const ENOLISTEN   : c_int = 5006;
71/// rendezvous connection process does not allow listen and accept call.
72pub const ERDVNOSERV  : c_int = 5007;
73/// rendezvous connection setup is enabled but bind has not been called before connect.
74pub const ERDVUNBOUND : c_int = 5008;
75/// operation not supported in SOCK_STREAM mode.
76pub const ESTREAMILL  : c_int = 5009;
77/// operation not supported in SOCK_DGRAM mode.
78pub const EDGRAMILL   : c_int = 5010;
79/// another socket is already listening on the same UDP port.
80pub const EDUPLISTEN  : c_int = 5011;
81/// message is too large to be hold in the sending buffer.
82pub const ELARGEMSG   : c_int = 5012;
83/// non-blocking call failure.
84pub const EASYNCFAIL  : c_int = 6000;
85/// no buffer available for sending.
86pub const EASYNCSND   : c_int = 6001;
87/// no data available for read.
88pub const EASYNCRCV   : c_int = 6002;
89/// timeout before operation completes.
90pub const ETIMEOUT    : c_int = 6003;
91/// Error has happened at the peer side.
92pub const EPEERERR    : c_int = 7000;
93
94
95pub const INVALID_SOCK: c_int = -1;
96pub const UDT_ERROR: c_int = -1;
97
98#[repr(C)]
99pub enum EPOLLOpt {
100    UDT_EPOLL_IN = 0x1,
101    UDT_EPOLL_OUT = 0x4,
102    UDT_EPOLL_ERR = 0x8
103}
104
105#[repr(C)]
106pub enum UDTOpt {
107#[allow(non_camel_case_types)]
108   UDT_MSS,             // the Maximum Transfer Unit
109   UDT_SNDSYN,          // if sending is blocking
110   UDT_RCVSYN,          // if receiving is blocking
111   UDT_CC,              // custom congestion control algorithm
112   UDT_FC,		// Flight flag size (window size)
113   UDT_SNDBUF,          // maximum buffer in sending queue
114   UDT_RCVBUF,          // UDT receiving buffer size
115   UDT_LINGER,          // waiting for unsent data when closing
116   UDP_SNDBUF,          // UDP sending buffer size
117   UDP_RCVBUF,          // UDP receiving buffer size
118   UDT_MAXMSG,          // maximum datagram message size
119   UDT_MSGTTL,          // time-to-live of a datagram message
120   UDT_RENDEZVOUS,      // rendezvous connection mode
121   UDT_SNDTIMEO,        // send() timeout
122   UDT_RCVTIMEO,        // recv() timeout
123   UDT_REUSEADDR,	// reuse an existing port or create a new one
124   UDT_MAXBW,		// maximum bandwidth (bytes per second) that the connection can use
125   UDT_STATE,		// current socket state, see UDTSTATUS, read only
126   UDT_EVENT,		// current avalable events associated with the socket
127   UDT_SNDDATA,		// size of data in the sending buffer
128   UDT_RCVDATA		// size of data available for recv
129}
130
131#[derive(Debug, Clone, Copy, PartialEq)]
132#[repr(C)]
133pub enum UdtStatus {
134    INIT = 1,
135    OPENED,
136    LISTENING,
137    CONNECTING,
138    CONNECTED,
139    BROKEN,
140    CLOSING,
141    CLOSED,
142    NONEXIST
143}
144
145
146pub type SOCKOPT = UDTOpt;
147
148#[cfg(windows)]
149pub type SYS_UDPSOCKET = std::os::windows::io::RawSocket;
150#[cfg(not(windows))]
151pub type SYS_UDPSOCKET = std::os::unix::io::RawFd;
152
153
154extern {
155
156    pub fn udt_startup();
157    pub fn udt_cleanup();
158    pub fn udt_socket(af: c_int, ty: c_int, protocol: c_int) -> UDTSOCKET;
159    pub fn udt_bind(u: UDTSOCKET, name: *const sockaddr, namelen: c_int) -> c_int;
160    pub fn udt_bind2(u: UDTSOCKET, other: SYS_UDPSOCKET) -> c_int;
161    pub fn udt_listen(u: UDTSOCKET, backlog: c_int) -> c_int;
162    pub fn udt_accept(u: UDTSOCKET, addr: *mut sockaddr, addrlen: *mut c_int) -> UDTSOCKET;
163    pub fn udt_connect(u: UDTSOCKET, name: *const sockaddr, namelen: c_int) -> c_int;
164    pub fn udt_close(u: UDTSOCKET) -> c_int;
165    pub fn udt_getpeername(u: UDTSOCKET, name: *mut sockaddr, namelen: *mut c_int) -> c_int;
166    pub fn udt_getsockname(u: UDTSOCKET, name: *mut sockaddr, namelen: *mut c_int) -> c_int;
167    pub fn udt_getsockopt(u: UDTSOCKET, level: c_int, optname: SOCKOPT, optval: *mut c_void, optlen: *mut c_int) -> c_int;
168    pub fn udt_setsockopt(u: UDTSOCKET, level: c_int, optname: SOCKOPT, optval: *const c_void, openlen: c_int) -> c_int;
169
170    pub fn udt_send(u: UDTSOCKET, buf: *const c_uchar, len: c_int, flags: c_int) -> c_int;
171    pub fn udt_sendmsg(U: UDTSOCKET, buf: *const c_uchar, len: c_int, ttl: c_int, inorder: c_int) -> c_int;
172
173    pub fn udt_recv(u: UDTSOCKET, buf: *mut c_uchar, len: c_int, flags: c_int) -> c_int;
174    pub fn udt_recvmsg(u: UDTSOCKET, but: *mut c_uchar, len: c_int) -> c_int;
175
176    pub fn udt_epoll_create() -> c_int;
177    pub fn udt_epoll_add_usock(eid: c_int, usock: UDTSOCKET, events: *const c_int) -> c_int;
178    pub fn udt_epoll_add_ssock(eid: c_int, ssock: SYSSOCKET, events: *const c_int) -> c_int;
179
180    pub fn udt_epoll_remove_usock(eid: c_int, usock: UDTSOCKET) -> c_int;
181    pub fn udt_epoll_remove_ssock(eid: c_int, ssock: SYSSOCKET) -> c_int;
182
183    pub fn udt_epoll_wait2(eid: c_int, readfs: *mut UDTSOCKET, rnum: *mut c_int, writefs: *mut UDTSOCKET, wnum: *mut c_int, msTimeOut: i64,
184                        lrfds: *mut SYSSOCKET, lrnum: *mut c_int, lwfds: *mut SYSSOCKET, lwnum: *mut c_int) -> c_int;
185    pub fn udt_epoll_release(eid: c_int) -> c_int;
186
187    pub fn udt_getsockstate(u: UDTSOCKET) -> UdtStatus;
188
189
190    pub fn udt_getlasterror_code() -> c_int;
191    pub fn udt_getlasterror_desc() -> *const c_char;
192
193
194}
195
196
197#[test]
198fn smoke() {
199    unsafe {
200        udt_startup();
201        udt_cleanup();
202    }
203}