-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib_socket.lua
355 lines (323 loc) · 8.96 KB
/
lib_socket.lua
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
-- lib_socket.lua
module(..., package.seeall)
local ffi = require("ffi")
local C = ffi.C
local util = require("lib_util")
local bit = require("bit")
local lshift = bit.lshift
local rshift = bit.rshift
local band = bit.band
local bor = bit.bor
local bnot = bit.bnot
local bswap = bit.bswap
local s
if util.isWin then
--require "win_socket"
ffi.cdef[[
int WSAPoll(LPWSAPOLLFD fdArray, ULONG fds, INT timeout);
]]
s = ffi.load("ws2_32")
else
-- unix
s = C
end
local err_prefix = " SOCKET ERROR: "
function error_prefix_text_set(errTxt)
err_prefix = errTxt
end
if util.isWin then
INVALID_SOCKET = ffi.new("SOCKET", -1)
else
INVALID_SOCKET = -1
end
SOCKET_ERROR = -1 -- 0xffffffff
local function MAKEWORD(low,high)
return bor(low , lshift(high , 8))
end
local function LOWBYTE(word)
return band(word, 0xff)
end
local function HIGHBYTE(word)
return band(rshift(word,8), 0xff)
end
if util.isWin then
function errortext(err)
return util.win_errortext(err)
end
function initialize()
local wsadata
--if is64bit then
-- wsadata = ffi.new("WSADATA64[1]")
--else
wsadata = ffi.new("WSADATA[1]")
--end
local wVersionRequested = ffi.cast("WORD", MAKEWORD(2, 2))
local err = s.WSAStartup(wVersionRequested, wsadata)
if err ~= 0 then
print(err_prefix.."WSAStartup failed with error code: "..err)
elseif s.WSAGetLastError() ~= 0 then
print(err_prefix.."WSAStartup failed with error code: "..s.WSAGetLastError())
end
--print(err_prefix.."WSAStartup: ".. err)
return err -- err,wsadata[0]
end
function poll(fdArray, fds, timeout)
return s.WSAPoll(fdArray, fds, timeout)
end
function close(socket)
local socket_c = ffi.cast("int", socket)
return s.closesocket(socket_c)
end
function cleanup(socket, errnum, errtext)
-- get WSAGetLastError() before close and WSACleanup
local wsa_err_num = s.WSAGetLastError()
local wsa_err_text = errortext(wsa_err_num)
if errnum and errnum ~= -1 and errnum ~= wsa_err_num then
wsa_err_text = errortext(wsa_err_num)..", WSAGetLastError: "..tonumber(wsa_err_num)..". "..wsa_err_text
end
if socket then
close(socket)
end
s.WSACleanup()
if errtext and #errtext > 0 then
error(err_prefix..errtext.."("..tonumber(errnum)..") "..wsa_err_text)
end
end
function inet_ntop(family, pAddr, strptr)
-- win XP: http://memset.wordpress.com/2010/10/09/inet_ntop-for-win32/
local srcaddr = ffi.new("struct sockaddr_in") --ffi.cast("struct sockaddr_in *", pAddr)
ffi.copy(srcaddr.sin_addr, pAddr, ffi.sizeof(srcaddr.sin_addr))
srcaddr.sin_family = family
local len = ffi.new("unsigned long[1]", ffi.sizeof(strptr))
local ret = s.WSAAddressToStringA(ffi.cast("struct sockaddr *", srcaddr), ffi.sizeof("struct sockaddr"), nil, strptr, len)
if ret ~= 0 then
print(err_prefix.."WSAAddressToString failed with error: "..tonumber(ret))
return nil
end
return strptr
end
function set_nonblock(socket, arg)
local arg_c = ffi.new("int[1]")
arg_c[0] = arg
return s.ioctlsocket(socket, FIONBIO, arg_c) -- FIONBIO in win_socket.lua
end
else
-- unix
function errortext(err)
return ffi.string(C.gai_strerror(err))
end
function initialize()
return 0 -- for win compatibilty
end
function poll(fds, nfds, timeout)
return s.poll(fds, nfds, timeout)
end
function close(socket)
return s.close(socket)
end
function cleanup(socket, errnum, errtext)
if socket then
close(socket)
end
--s.WSACleanup()
if errtext and #errtext > 0 then
error(err_prefix..errtext.."("..tonumber(errnum)..") "..errortext(errnum))
end
end
function inet_ntop(family, pAddr, strptr)
return s.inet_ntop(family, pAddr, strptr, ffi.sizeof(strptr))
end
function set_nonblock(socket, arg)
local flags = s.fcntl(socket, s.F_GETFL, 0);
if flags < 0 then return flags end
if arg ~= 0 then
flags = bit.bor(flags, s.O_NONBLOCK)
else
flags = bit.band(flags, bit.bnot(s.O_NONBLOCK))
end
return s.fcntl(socket, s.F_SETFL, ffi.new("int", flags))
end
end
function shutdown(socket, how)
return s.shutdown(socket, how)
end
function htons(num)
return s.htons(num)
end
function socket(domain, type_, protocol)
return s.socket(domain, type_, protocol)
end
function bind(socket, sockaddr ,addrlen)
return s.bind(socket, sockaddr ,addrlen)
end
function listen(socket, backlog)
return s.listen(socket, backlog)
end
function connect(socket, sockaddr ,address_len)
return s.connect(socket, sockaddr ,address_len)
end
function accept(socket, sockaddr ,addrlen)
return s.accept(socket, sockaddr ,addrlen)
end
function recv(socket, buffer, length, flags)
return tonumber(s.recv(socket, buffer, length, flags))
end
function send(socket, buffer, length, flags)
return tonumber(s.send(socket, buffer, length, flags))
end
function getsockopt(socket, level, option_name, option_value, option_len)
return s.getsockopt(socket, level, option_name, option_value, option_len)
end
function setsockopt(socket, level, option_name, option_value)
--local arg_c = ffi.new("uint32_t[1]", option_value)
local arg_c = ffi.new("int[1]", option_value)
local option_len = ffi.sizeof(arg_c)
--return s.setsockopt(socket, level, option_name, ffi.cast("void *", arg_c), option_len)
return s.setsockopt(socket, level, option_name, arg_c, option_len)
end
function getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
return s.getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
end
function getaddrinfo(hostname, servname, hints, res)
return s.getaddrinfo(hostname, servname, hints, res)
end
function getpeername(socket, name, namelen)
return s.getpeername(socket, name, namelen)
end
function ntohs(netshort)
return s.ntohs(netshort)
end
--[[
int l_socket_set_nonblock(LSocketFD sock, bool val) {
#ifdef __WINDOWS__
unsigned long flag = val;
return ioctlsocket(sock, FIONBIO, &flag);
#else
int flags;
flags = fcntl(sock, F_GETFL);
if(flags < 0) return flags;
if(val) {
flags |= O_NONBLOCK;
} else {
flags &= ~(O_NONBLOCK);
}
return fcntl(sock, F_SETFL, flags);
#endif
}
int l_socket_set_close_on_exec(LSocketFD sock, bool val) {
#ifdef __WINDOWS__
return 0;
#else
int flags;
flags = fcntl(sock, F_GETFD);
if(flags < 0) return flags;
if(val) {
flags |= FD_CLOEXEC;
} else {
flags &= ~(FD_CLOEXEC);
}
return fcntl(sock, F_SETFD, flags);
#endif
}
int l_socket_get_option(LSocketFD sock, int level, int opt, void *val, socklen_t *len) {
return getsockopt(sock, level, opt, val, len);
}
int l_socket_set_option(LSocketFD sock, int level, int opt, const void *val, socklen_t len) {
return setsockopt(sock, level, opt, val, len);
}
int l_socket_get_int_option(LSocketFD sock, int level, int opt, int *val) {
socklen_t len = sizeof(*val);
return l_socket_get_option(sock, level, opt, val, &len);
}
int l_socket_set_int_option(LSocketFD sock, int level, int opt, int val) {
return l_socket_set_option(sock, level, opt, &val, sizeof(val));
}
int l_socket_pair(int type, int flags, LSocketFD sv[2]) {
type |= flags;
#ifdef __WINDOWS__
/* TODO: use TCP sockets. */
errno = EAFNOSUPPORT;
return -1;
#else
return socketpair(AF_UNIX, type, 0, sv);
#endif
}
LSocketFD l_socket_open(int domain, int type, int protocol, int flags) {
type |= flags;
return socket(domain, type, protocol);
}
LSocketFD l_socket_close_internal(LSocketFD sock) {
#ifdef __WINDOWS__
return closesocket(sock);
#else
return close(sock);
#endif
}
int l_socket_shutdown(LSocketFD sock, int how) {
return shutdown(sock, how);
}
int l_socket_connect(LSocketFD sock, LSockAddr *addr) {
return connect(sock, L_SOCKADDR_TO_CONST_ADDR_AND_LEN(addr));
}
int l_socket_bind(LSocketFD sock, LSockAddr *addr) {
return bind(sock, L_SOCKADDR_TO_CONST_ADDR_AND_LEN(addr));
}
int l_socket_listen(LSocketFD sock, int backlog) {
return listen(sock, backlog);
}
int l_socket_get_sockname(LSocketFD sock, LSockAddr *addr) {
MAKE_TEMP_ADDR(tmp1);
int rc;
rc = getsockname(sock, GET_TEMP_ADDR_AND_PTR_LEN(tmp1));
if(rc == 0) {
L_SOCKADDR_FILL_FROM_TEMP(addr, tmp1);
}
return rc;
}
int l_socket_get_peername(LSocketFD sock, LSockAddr *addr) {
MAKE_TEMP_ADDR(tmp1);
int rc;
rc = getpeername(sock, GET_TEMP_ADDR_AND_PTR_LEN(tmp1));
if(rc == 0) {
L_SOCKADDR_FILL_FROM_TEMP(addr, tmp1);
}
return rc;
}
LSocketFD l_socket_accept(LSocketFD sock, LSockAddr *peer, int flags) {
MAKE_TEMP_ADDR(tmp1);
#ifdef __WINDOWS__
LSocketFD rc;
if(peer != NULL) {
rc = accept(sock, GET_TEMP_ADDR_AND_PTR_LEN(tmp1));
if(rc == 0) {
L_SOCKADDR_FILL_FROM_TEMP(peer, tmp1);
}
} else {
rc = accept(sock, NULL, NULL);
}
if(rc == INVALID_SOCKET) {
return rc;
}
if((flags & SOCK_NONBLOCK) == SOCK_NONBLOCK) {
l_socket_set_nonblock(rc, 1);
}
return rc;
#else
if(peer != NULL) {
LSocketFD rc;
rc = accept4(sock, GET_TEMP_ADDR_AND_PTR_LEN(tmp1), flags);
if(rc == 0) {
L_SOCKADDR_FILL_FROM_TEMP(peer, tmp1);
}
return rc;
}
return accept4(sock, NULL, NULL, flags);
#endif
}
int l_socket_send(LSocketFD sock, const void *buf, size_t len, int flags) {
return send(sock, buf, len, flags);
}
int l_socket_recv(LSocketFD sock, void *buf, size_t len, int flags) {
return recv(sock, buf, len, flags);
}
--]]