1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
extern crate libc;
use libc::{c_int, c_char, c_void, c_uchar};
#[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
mod plat_imports {
pub use winapi::SOCKADDR as sockaddr;
}
#[cfg(not(windows))]
mod plat_imports {
pub use libc::sockaddr;
}
use plat_imports::*;
pub type UDTSOCKET = c_int;
pub type SYSSOCKET = c_int;
pub const SUCCESS : c_int = 0;
pub const ECONNSETUP : c_int = 1000;
pub const ENOSERVER : c_int = 1001;
pub const ECONNREJ : c_int = 1002;
pub const ESOCKFAIL : c_int = 1003;
pub const ESECFAIL : c_int = 1004;
pub const ECONNFAIL : c_int = 2000;
pub const ECONNLOST : c_int = 2001;
pub const ENOCONN : c_int = 2002;
pub const ERESOURCE : c_int = 3000;
pub const ETHREAD : c_int = 3001;
pub const ENOBUF : c_int = 3002;
pub const EFILE : c_int = 4000;
pub const EINVRDOFF : c_int = 4001;
pub const ERDPERM : c_int = 4002;
pub const EINVWROFF : c_int = 4003;
pub const EWRPERM : c_int = 4004;
pub const EINVOP : c_int = 5000;
pub const EBOUNDSOCK : c_int = 5001;
pub const ECONNSOCK : c_int = 5002;
pub const EINVPARAM : c_int = 5003;
pub const EINVSOCK : c_int = 5004;
pub const EUNBOUNDSOCK : c_int = 5005;
pub const ENOLISTEN : c_int = 5006;
pub const ERDVNOSERV : c_int = 5007;
pub const ERDVUNBOUND : c_int = 5008;
pub const ESTREAMILL : c_int = 5009;
pub const EDGRAMILL : c_int = 5010;
pub const EDUPLISTEN : c_int = 5011;
pub const ELARGEMSG : c_int = 5012;
pub const EASYNCFAIL : c_int = 6000;
pub const EASYNCSND : c_int = 6001;
pub const EASYNCRCV : c_int = 6002;
pub const ETIMEOUT : c_int = 6003;
pub const EPEERERR : c_int = 7000;
pub const INVALID_SOCK: c_int = -1;
pub const UDT_ERROR: c_int = -1;
#[repr(C)]
pub enum EPOLLOpt {
UDT_EPOLL_IN = 0x1,
UDT_EPOLL_OUT = 0x4,
UDT_EPOLL_ERR = 0x8
}
#[repr(C)]
pub enum UDTOpt {
#[allow(non_camel_case_types)]
UDT_MSS,
UDT_SNDSYN,
UDT_RCVSYN,
UDT_CC,
UDT_FC,
UDT_SNDBUF,
UDT_RCVBUF,
UDT_LINGER,
UDP_SNDBUF,
UDP_RCVBUF,
UDT_MAXMSG,
UDT_MSGTTL,
UDT_RENDEZVOUS,
UDT_SNDTIMEO,
UDT_RCVTIMEO,
UDT_REUSEADDR,
UDT_MAXBW,
UDT_STATE,
UDT_EVENT,
UDT_SNDDATA,
UDT_RCVDATA
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(C)]
pub enum UdtStatus {
INIT = 1,
OPENED,
LISTENING,
CONNECTING,
CONNECTED,
BROKEN,
CLOSING,
CLOSED,
NONEXIST
}
pub type SOCKOPT = UDTOpt;
#[cfg(windows)]
pub type SYS_UDPSOCKET = std::os::windows::io::RawSocket;
#[cfg(not(windows))]
pub type SYS_UDPSOCKET = std::os::unix::io::RawFd;
extern {
pub fn udt_startup();
pub fn udt_cleanup();
pub fn udt_socket(af: c_int, ty: c_int, protocol: c_int) -> UDTSOCKET;
pub fn udt_bind(u: UDTSOCKET, name: *const sockaddr, namelen: c_int) -> c_int;
pub fn udt_bind2(u: UDTSOCKET, other: SYS_UDPSOCKET) -> c_int;
pub fn udt_listen(u: UDTSOCKET, backlog: c_int) -> c_int;
pub fn udt_accept(u: UDTSOCKET, addr: *mut sockaddr, addrlen: *mut c_int) -> UDTSOCKET;
pub fn udt_connect(u: UDTSOCKET, name: *const sockaddr, namelen: c_int) -> c_int;
pub fn udt_close(u: UDTSOCKET) -> c_int;
pub fn udt_getpeername(u: UDTSOCKET, name: *mut sockaddr, namelen: *mut c_int) -> c_int;
pub fn udt_getsockname(u: UDTSOCKET, name: *mut sockaddr, namelen: *mut c_int) -> c_int;
pub fn udt_getsockopt(u: UDTSOCKET, level: c_int, optname: SOCKOPT, optval: *mut c_void, optlen: *mut c_int) -> c_int;
pub fn udt_setsockopt(u: UDTSOCKET, level: c_int, optname: SOCKOPT, optval: *const c_void, openlen: c_int) -> c_int;
pub fn udt_send(u: UDTSOCKET, buf: *const c_uchar, len: c_int, flags: c_int) -> c_int;
pub fn udt_sendmsg(U: UDTSOCKET, buf: *const c_uchar, len: c_int, ttl: c_int, inorder: c_int) -> c_int;
pub fn udt_recv(u: UDTSOCKET, buf: *mut c_uchar, len: c_int, flags: c_int) -> c_int;
pub fn udt_recvmsg(u: UDTSOCKET, but: *mut c_uchar, len: c_int) -> c_int;
pub fn udt_epoll_create() -> c_int;
pub fn udt_epoll_add_usock(eid: c_int, usock: UDTSOCKET, events: *const c_int) -> c_int;
pub fn udt_epoll_add_ssock(eid: c_int, ssock: SYSSOCKET, events: *const c_int) -> c_int;
pub fn udt_epoll_remove_usock(eid: c_int, usock: UDTSOCKET) -> c_int;
pub fn udt_epoll_remove_ssock(eid: c_int, ssock: SYSSOCKET) -> c_int;
pub fn udt_epoll_wait2(eid: c_int, readfs: *mut UDTSOCKET, rnum: *mut c_int, writefs: *mut UDTSOCKET, wnum: *mut c_int, msTimeOut: i64,
lrfds: *mut SYSSOCKET, lrnum: *mut c_int, lwfds: *mut SYSSOCKET, lwnum: *mut c_int) -> c_int;
pub fn udt_epoll_release(eid: c_int) -> c_int;
pub fn udt_getsockstate(u: UDTSOCKET) -> UdtStatus;
pub fn udt_getlasterror_code() -> c_int;
pub fn udt_getlasterror_desc() -> *const c_char;
}
#[test]
fn smoke() {
unsafe {
udt_startup();
udt_cleanup();
}
}