forked from squeek502/luv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.c
More file actions
350 lines (318 loc) · 10.1 KB
/
Copy pathudp.c
File metadata and controls
350 lines (318 loc) · 10.1 KB
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
/*
* Copyright 2014 The Luvit Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "luv.h"
static uv_udp_t* luv_check_udp(lua_State* L, int index) {
uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, index, "uv_udp");
luaL_argcheck(L, handle->type == UV_UDP && handle->data, index, "Expected uv_udp_t");
return handle;
}
static int luv_new_udp(lua_State* L) {
uv_udp_t* handle = (uv_udp_t*)luv_newuserdata(L, sizeof(*handle));
int ret;
if (lua_isnoneornil(L, 1)) {
ret = uv_udp_init(luv_loop(L), handle);
}
else {
ret = uv_udp_init_ex(luv_loop(L), handle, lua_tointeger(L, 1));
}
if (ret < 0) {
lua_pop(L, 1);
return luv_error(L, ret);
}
handle->data = luv_setup_handle(L);
return 1;
}
static int luv_udp_get_send_queue_size(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
lua_pushinteger(L, handle->send_queue_size);
return 1;
}
static int luv_udp_get_send_queue_count(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
lua_pushinteger(L, handle->send_queue_count);
return 1;
}
static int luv_udp_open(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
uv_os_sock_t sock = luaL_checkinteger(L, 2);
int ret = uv_udp_open(handle, sock);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_bind(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
const char* host = luaL_checkstring(L, 2);
int port = luaL_checkinteger(L, 3);
unsigned int flags = 0;
struct sockaddr_storage addr;
int ret;
if (uv_ip4_addr(host, port, (struct sockaddr_in*)&addr) &&
uv_ip6_addr(host, port, (struct sockaddr_in6*)&addr)) {
return luaL_error(L, "Invalid IP address or port [%s:%d]", host, port);
}
if (lua_type(L, 4) == LUA_TTABLE) {
luaL_checktype(L, 4, LUA_TTABLE);
lua_getfield(L, 4, "reuseaddr");
if (lua_toboolean(L, -1)) flags |= UV_UDP_REUSEADDR;
lua_pop(L, 1);
lua_getfield(L, 4, "ipv6only");
if (lua_toboolean(L, -1)) flags |= UV_UDP_IPV6ONLY;
lua_pop(L, 1);
}
ret = uv_udp_bind(handle, (struct sockaddr*)&addr, flags);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_getsockname(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
struct sockaddr_storage address;
int addrlen = sizeof(address);
int ret = uv_udp_getsockname(handle, (struct sockaddr*)&address, &addrlen);
if (ret < 0) return luv_error(L, ret);
parse_sockaddr(L, &address);
return 1;
}
// These are the same order as uv_membership which also starts at 0
static const char *const luv_membership_opts[] = {
"leave", "join", NULL
};
static int luv_udp_set_membership(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
const char* multicast_addr = luaL_checkstring(L, 2);
const char* interface_addr = luaL_checkstring(L, 3);
uv_membership membership = (uv_membership)luaL_checkoption(L, 4, NULL, luv_membership_opts);
int ret = uv_udp_set_membership(handle, multicast_addr, interface_addr, membership);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_set_multicast_loop(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int on, ret;
luaL_checktype(L, 2, LUA_TBOOLEAN);
on = lua_toboolean(L, 2);
ret = uv_udp_set_multicast_loop(handle, on);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_set_multicast_ttl(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int ttl, ret;
ttl = luaL_checkinteger(L, 2);
ret = uv_udp_set_multicast_ttl(handle, ttl);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_set_multicast_interface(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
const char* interface_addr = luaL_checkstring(L, 2);
int ret = uv_udp_set_multicast_interface(handle, interface_addr);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_set_broadcast(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int on, ret;
luaL_checktype(L, 2, LUA_TBOOLEAN);
on = lua_toboolean(L, 2);
ret =uv_udp_set_broadcast(handle, on);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_set_ttl(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int ttl, ret;
ttl = luaL_checknumber(L, 2);
ret = uv_udp_set_ttl(handle, ttl);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static void luv_udp_send_cb(uv_udp_send_t* req, int status) {
luv_req_t* data = (luv_req_t*)req->data;
lua_State* L = data->L;
luv_status(L, status);
luv_fulfill_req(L, (luv_req_t*)req->data, 1);
luv_cleanup_req(L, (luv_req_t*)req->data);
req->data = NULL;
}
static struct sockaddr* luv_check_addr(lua_State *L, struct sockaddr_storage* addr, int hostidx, int portidx) {
const char* host;
int port;
#if LUV_UV_VERSION_GEQ(1, 27, 0)
int host_type, port_type;
host_type = lua_type(L, hostidx);
port_type = lua_type(L, portidx);
if (host_type == LUA_TNIL && port_type == LUA_TNIL) {
return NULL;
}
host = lua_tostring(L, hostidx);
port = lua_tointeger(L, portidx);
if (host_type == LUA_TSTRING && port_type == LUA_TNUMBER) {
if (uv_ip4_addr(host, port, (struct sockaddr_in*)addr) &&
uv_ip6_addr(host, port, (struct sockaddr_in6*)addr)) {
luaL_error(L, "Invalid IP address or port [%s:%d]", host, port);
return NULL;
}
return (struct sockaddr*)addr;
}
else {
if (host_type == LUA_TNIL || port_type == LUA_TNIL) {
luaL_argerror(L, host_type == LUA_TNIL ? portidx : hostidx,
"Both host and port must be nil if one is nil");
}
luaL_argcheck(L, host_type == LUA_TNIL || host_type == LUA_TSTRING, hostidx,
"Host must be string or nil");
luaL_argcheck(L, port_type == LUA_TNIL || port_type == LUA_TNUMBER, portidx,
"Port must be number or nil");
return NULL;
}
#else
host = luaL_checkstring(L, hostidx);
port = luaL_checkinteger(L, portidx);
if (uv_ip4_addr(host, port, (struct sockaddr_in*)addr) &&
uv_ip6_addr(host, port, (struct sockaddr_in6*)addr)) {
luaL_error(L, "Invalid IP address or port [%s:%d]", host, port);
return NULL;
}
return (struct sockaddr*)addr;
#endif
}
static int luv_udp_send(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
uv_udp_send_t* req;
uv_buf_t buf;
int ret, ref;
struct sockaddr_storage addr;
struct sockaddr* addr_ptr;
luv_check_buf(L, 2, &buf);
addr_ptr = luv_check_addr(L, &addr, 3, 4);
ref = luv_check_continuation(L, 5);
req = (uv_udp_send_t*)lua_newuserdata(L, sizeof(*req));
req->data = luv_setup_req(L, ref);
ret = uv_udp_send(req, handle, &buf, 1, addr_ptr, luv_udp_send_cb);
if (ret < 0) {
luv_cleanup_req(L, (luv_req_t*)req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
lua_pushvalue(L, 2);
((luv_req_t*)req->data)->data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_try_send(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
uv_buf_t buf;
int ret;
struct sockaddr_storage addr;
struct sockaddr* addr_ptr;
luv_check_buf(L, 2, &buf);
addr_ptr = luv_check_addr(L, &addr, 3, 4);
ret = uv_udp_try_send(handle, &buf, 1, addr_ptr);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static void luv_udp_recv_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
luv_handle_t* data = (luv_handle_t*)handle->data;
lua_State* L = data->L;
// err
if (nread < 0) {
luv_status(L, nread);
}
else {
lua_pushnil(L);
}
// data
if (nread == 0) {
if (addr) {
lua_pushstring(L, "");
}
else {
lua_pushnil(L);
}
}
else if (nread > 0) {
lua_pushlstring(L, buf->base, nread);
}
else {
lua_pushnil(L);
}
if (buf) free(buf->base);
// address
if (addr) {
parse_sockaddr(L, (struct sockaddr_storage*)addr);
}
else {
lua_pushnil(L);
}
// flags
lua_newtable(L);
if (flags & UV_UDP_PARTIAL) {
lua_pushboolean(L, 1);
lua_setfield(L, -2, "partial");
}
luv_call_callback(L, (luv_handle_t*)handle->data, LUV_RECV, 4);
}
static int luv_udp_recv_start(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int ret;
luv_check_callback(L, (luv_handle_t*)handle->data, LUV_RECV, 2);
ret = uv_udp_recv_start(handle, luv_alloc_cb, luv_udp_recv_cb);
#if LUV_UV_VERSION_LEQ(1, 23, 0)
// in Libuv <= 1.23.0, uv_udp_recv_start will return untranslated error codes on Windows
ret = uv_translate_sys_error(ret);
#endif
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_recv_stop(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
int ret = uv_udp_recv_stop(handle);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
#if LUV_UV_VERSION_GEQ(1, 27, 0)
static int luv_udp_connect(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
struct sockaddr_storage addr;
struct sockaddr* addr_ptr = luv_check_addr(L, &addr, 2, 3);
int ret = uv_udp_connect(handle, addr_ptr);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_udp_getpeername(lua_State* L) {
uv_udp_t* handle = luv_check_udp(L, 1);
struct sockaddr_storage address;
int addrlen = sizeof(address);
int ret = uv_udp_getpeername(handle, (struct sockaddr*)&address, &addrlen);
if (ret < 0) return luv_error(L, ret);
parse_sockaddr(L, &address);
return 1;
}
#endif