Debian Codegraph @

texk/web2c/luatexdir/luasocket/src/udp.c

raw
1 /*=========================================================================*\
2 * UDP object
3 * LuaSocket toolkit
4 \*=========================================================================*/
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "lua.h"
9 #include "lauxlib.h"
10 #include "compat.h"
11
12 #include "auxiliar.h"
13 #include "socket.h"
14 #include "inet.h"
15 #include "options.h"
16 #include "udp.h"
17
18 /* min and max macros */
19 #ifndef MIN
20 #define MIN(x, y) ((x) < (y) ? x : y)
21 #endif
22 #ifndef MAX
23 #define MAX(x, y) ((x) > (y) ? x : y)
24 #endif
25
26 /*=========================================================================*\
27 * Internal function prototypes
28 \*=========================================================================*/
29 static int global_create(lua_State *L);
30 static int global_create4(lua_State *L);
31 static int global_create6(lua_State *L);
32 static int meth_send(lua_State *L);
33 static int meth_sendto(lua_State *L);
34 static int meth_receive(lua_State *L);
35 static int meth_receivefrom(lua_State *L);
36 static int meth_getfamily(lua_State *L);
37 static int meth_getsockname(lua_State *L);
38 static int meth_getpeername(lua_State *L);
39 static int meth_gettimeout(lua_State *L);
40 static int meth_setsockname(lua_State *L);
41 static int meth_setpeername(lua_State *L);
42 static int meth_close(lua_State *L);
43 static int meth_setoption(lua_State *L);
44 static int meth_getoption(lua_State *L);
45 static int meth_settimeout(lua_State *L);
46 static int meth_getfd(lua_State *L);
47 static int meth_setfd(lua_State *L);
48 static int meth_dirty(lua_State *L);
49
50 /* udp object methods */
51 static luaL_Reg udp_methods[] = {
52 {"__gc", meth_close},
53 {"__tostring", auxiliar_tostring},
54 {"close", meth_close},
55 {"dirty", meth_dirty},
56 {"getfamily", meth_getfamily},
57 {"getfd", meth_getfd},
58 {"getpeername", meth_getpeername},
59 {"getsockname", meth_getsockname},
60 {"receive", meth_receive},
61 {"receivefrom", meth_receivefrom},
62 {"send", meth_send},
63 {"sendto", meth_sendto},
64 {"setfd", meth_setfd},
65 {"setoption", meth_setoption},
66 {"getoption", meth_getoption},
67 {"setpeername", meth_setpeername},
68 {"setsockname", meth_setsockname},
69 {"settimeout", meth_settimeout},
70 {"gettimeout", meth_gettimeout},
71 {NULL, NULL}
72 };
73
74 /* socket options for setoption */
75 static t_opt optset[] = {
76 {"dontroute", opt_set_dontroute},
77 {"broadcast", opt_set_broadcast},
78 {"reuseaddr", opt_set_reuseaddr},
79 {"reuseport", opt_set_reuseport},
80 {"ip-multicast-if", opt_set_ip_multicast_if},
81 {"ip-multicast-ttl", opt_set_ip_multicast_ttl},
82 {"ip-multicast-loop", opt_set_ip_multicast_loop},
83 {"ip-add-membership", opt_set_ip_add_membership},
84 {"ip-drop-membership", opt_set_ip_drop_membersip},
85 {"ipv6-unicast-hops", opt_set_ip6_unicast_hops},
86 {"ipv6-multicast-hops", opt_set_ip6_unicast_hops},
87 {"ipv6-multicast-loop", opt_set_ip6_multicast_loop},
88 {"ipv6-add-membership", opt_set_ip6_add_membership},
89 {"ipv6-drop-membership", opt_set_ip6_drop_membersip},
90 {"ipv6-v6only", opt_set_ip6_v6only},
91 {NULL, NULL}
92 };
93
94 /* socket options for getoption */
95 static t_opt optget[] = {
96 {"dontroute", opt_get_dontroute},
97 {"broadcast", opt_get_broadcast},
98 {"reuseaddr", opt_get_reuseaddr},
99 {"reuseport", opt_get_reuseport},
100 {"ip-multicast-if", opt_get_ip_multicast_if},
101 {"ip-multicast-loop", opt_get_ip_multicast_loop},
102 {"error", opt_get_error},
103 {"ipv6-unicast-hops", opt_get_ip6_unicast_hops},
104 {"ipv6-multicast-hops", opt_get_ip6_unicast_hops},
105 {"ipv6-multicast-loop", opt_get_ip6_multicast_loop},
106 {"ipv6-v6only", opt_get_ip6_v6only},
107 {NULL, NULL}
108 };
109
110 /* functions in library namespace */
111 static luaL_Reg func[] = {
112 {"udp", global_create},
113 {"udp4", global_create4},
114 {"udp6", global_create6},
115 {NULL, NULL}
116 };
117
118 /*-------------------------------------------------------------------------*\
119 * Initializes module
120 \*-------------------------------------------------------------------------*/
121 int udp_open(lua_State *L) {
122 /* create classes */
123 auxiliar_newclass(L, "udp{connected}", udp_methods);
124 auxiliar_newclass(L, "udp{unconnected}", udp_methods);
125 /* create class groups */
126 auxiliar_add2group(L, "udp{connected}", "udp{any}");
127 auxiliar_add2group(L, "udp{unconnected}", "udp{any}");
128 auxiliar_add2group(L, "udp{connected}", "select{able}");
129 auxiliar_add2group(L, "udp{unconnected}", "select{able}");
130 /* define library functions */
131 luaL_setfuncs(L, func, 0);
132 /* export default UDP size */
133 lua_pushliteral(L, "_DATAGRAMSIZE");
134 lua_pushinteger(L, UDP_DATAGRAMSIZE);
135 lua_rawset(L, -3);
136 return 0;
137 }
138
139 /*=========================================================================*\
140 * Lua methods
141 \*=========================================================================*/
142 static const char *udp_strerror(int err) {
143 /* a 'closed' error on an unconnected means the target address was not
144 * accepted by the transport layer */
145 if (err == IO_CLOSED) return "refused";
146 else return socket_strerror(err);
147 }
148
149 /*-------------------------------------------------------------------------*\
150 * Send data through connected udp socket
151 \*-------------------------------------------------------------------------*/
152 static int meth_send(lua_State *L) {
153 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
154 p_timeout tm = &udp->tm;
155 size_t count, sent = 0;
156 int err;
157 const char *data = luaL_checklstring(L, 2, &count);
158 timeout_markstart(tm);
159 err = socket_send(&udp->sock, data, count, &sent, tm);
160 if (err != IO_DONE) {
161 lua_pushnil(L);
162 lua_pushstring(L, udp_strerror(err));
163 return 2;
164 }
165 lua_pushnumber(L, (lua_Number) sent);
166 return 1;
167 }
168
169 /*-------------------------------------------------------------------------*\
170 * Send data through unconnected udp socket
171 \*-------------------------------------------------------------------------*/
172 static int meth_sendto(lua_State *L) {
173 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
174 size_t count, sent = 0;
175 const char *data = luaL_checklstring(L, 2, &count);
176 const char *ip = luaL_checkstring(L, 3);
177 const char *port = luaL_checkstring(L, 4);
178 p_timeout tm = &udp->tm;
179 int err;
180 struct addrinfo aihint;
181 struct addrinfo *ai;
182 memset(&aihint, 0, sizeof(aihint));
183 aihint.ai_family = udp->family;
184 aihint.ai_socktype = SOCK_DGRAM;
185 aihint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
186 err = getaddrinfo(ip, port, &aihint, &ai);
187 if (err) {
188 lua_pushnil(L);
189 lua_pushstring(L, gai_strerror(err));
190 return 2;
191 }
192
193 /* create socket if on first sendto if AF_UNSPEC was set */
194 if (udp->family == AF_UNSPEC && udp->sock == SOCKET_INVALID) {
195 struct addrinfo *ap;
196 const char *errstr = NULL;
197 for (ap = ai; ap != NULL; ap = ap->ai_next) {
198 errstr = inet_trycreate(&udp->sock, ap->ai_family, SOCK_DGRAM, 0);
199 if (errstr == NULL) {
200 socket_setnonblocking(&udp->sock);
201 udp->family = ap->ai_family;
202 break;
203 }
204 }
205 if (errstr != NULL) {
206 lua_pushnil(L);
207 lua_pushstring(L, errstr);
208 freeaddrinfo(ai);
209 return 2;
210 }
211 }
212
213 timeout_markstart(tm);
214 err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
215 (socklen_t) ai->ai_addrlen, tm);
216 freeaddrinfo(ai);
217 if (err != IO_DONE) {
218 lua_pushnil(L);
219 lua_pushstring(L, udp_strerror(err));
220 return 2;
221 }
222 lua_pushnumber(L, (lua_Number) sent);
223 return 1;
224 }
225
226 /*-------------------------------------------------------------------------*\
227 * Receives data from a UDP socket
228 \*-------------------------------------------------------------------------*/
229 static int meth_receive(lua_State *L) {
230 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
231 char buf[UDP_DATAGRAMSIZE];
232 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
233 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
234 int err;
235 p_timeout tm = &udp->tm;
236 timeout_markstart(tm);
237 if (!dgram) {
238 lua_pushnil(L);
239 lua_pushliteral(L, "out of memory");
240 return 2;
241 }
242 err = socket_recv(&udp->sock, dgram, wanted, &got, tm);
243 /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
244 if (err != IO_DONE && err != IO_CLOSED) {
245 lua_pushnil(L);
246 lua_pushstring(L, udp_strerror(err));
247 if (wanted > sizeof(buf)) free(dgram);
248 return 2;
249 }
250 lua_pushlstring(L, dgram, got);
251 if (wanted > sizeof(buf)) free(dgram);
252 return 1;
253 }
254
255 /*-------------------------------------------------------------------------*\
256 * Receives data and sender from a UDP socket
257 \*-------------------------------------------------------------------------*/
258 static int meth_receivefrom(lua_State *L) {
259 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
260 char buf[UDP_DATAGRAMSIZE];
261 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
262 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
263 struct sockaddr_storage addr;
264 socklen_t addr_len = sizeof(addr);
265 char addrstr[INET6_ADDRSTRLEN];
266 char portstr[6];
267 int err;
268 p_timeout tm = &udp->tm;
269 timeout_markstart(tm);
270 if (!dgram) {
271 lua_pushnil(L);
272 lua_pushliteral(L, "out of memory");
273 return 2;
274 }
275 err = socket_recvfrom(&udp->sock, dgram, wanted, &got, (SA *) &addr,
276 &addr_len, tm);
277 /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */
278 if (err != IO_DONE && err != IO_CLOSED) {
279 lua_pushnil(L);
280 lua_pushstring(L, udp_strerror(err));
281 if (wanted > sizeof(buf)) free(dgram);
282 return 2;
283 }
284 err = getnameinfo((struct sockaddr *)&addr, addr_len, addrstr,
285 INET6_ADDRSTRLEN, portstr, 6, NI_NUMERICHOST | NI_NUMERICSERV);
286 if (err) {
287 lua_pushnil(L);
288 lua_pushstring(L, gai_strerror(err));
289 if (wanted > sizeof(buf)) free(dgram);
290 return 2;
291 }
292 lua_pushlstring(L, dgram, got);
293 lua_pushstring(L, addrstr);
294 lua_pushinteger(L, (int) strtol(portstr, (char **) NULL, 10));
295 if (wanted > sizeof(buf)) free(dgram);
296 return 3;
297 }
298
299 /*-------------------------------------------------------------------------*\
300 * Returns family as string
301 \*-------------------------------------------------------------------------*/
302 static int meth_getfamily(lua_State *L) {
303 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
304 if (udp->family == AF_INET6) {
305 lua_pushliteral(L, "inet6");
306 return 1;
307 } else {
308 lua_pushliteral(L, "inet4");
309 return 1;
310 }
311 }
312
313 /*-------------------------------------------------------------------------*\
314 * Select support methods
315 \*-------------------------------------------------------------------------*/
316 static int meth_getfd(lua_State *L) {
317 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
318 lua_pushnumber(L, (int) udp->sock);
319 return 1;
320 }
321
322 /* this is very dangerous, but can be handy for those that are brave enough */
323 static int meth_setfd(lua_State *L) {
324 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
325 udp->sock = (t_socket) luaL_checknumber(L, 2);
326 return 0;
327 }
328
329 static int meth_dirty(lua_State *L) {
330 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
331 (void) udp;
332 lua_pushboolean(L, 0);
333 return 1;
334 }
335
336 /*-------------------------------------------------------------------------*\
337 * Just call inet methods
338 \*-------------------------------------------------------------------------*/
339 static int meth_getpeername(lua_State *L) {
340 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
341 return inet_meth_getpeername(L, &udp->sock, udp->family);
342 }
343
344 static int meth_getsockname(lua_State *L) {
345 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
346 return inet_meth_getsockname(L, &udp->sock, udp->family);
347 }
348
349 /*-------------------------------------------------------------------------*\
350 * Just call option handler
351 \*-------------------------------------------------------------------------*/
352 static int meth_setoption(lua_State *L) {
353 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
354 return opt_meth_setoption(L, optset, &udp->sock);
355 }
356
357 /*-------------------------------------------------------------------------*\
358 * Just call option handler
359 \*-------------------------------------------------------------------------*/
360 static int meth_getoption(lua_State *L) {
361 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
362 return opt_meth_getoption(L, optget, &udp->sock);
363 }
364
365 /*-------------------------------------------------------------------------*\
366 * Just call tm methods
367 \*-------------------------------------------------------------------------*/
368 static int meth_settimeout(lua_State *L) {
369 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
370 return timeout_meth_settimeout(L, &udp->tm);
371 }
372
373 static int meth_gettimeout(lua_State *L) {
374 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
375 return timeout_meth_gettimeout(L, &udp->tm);
376 }
377
378 /*-------------------------------------------------------------------------*\
379 * Turns a master udp object into a client object.
380 \*-------------------------------------------------------------------------*/
381 static int meth_setpeername(lua_State *L) {
382 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
383 p_timeout tm = &udp->tm;
384 const char *address = luaL_checkstring(L, 2);
385 int connecting = strcmp(address, "*");
386 const char *port = connecting? luaL_checkstring(L, 3): "0";
387 struct addrinfo connecthints;
388 const char *err;
389 memset(&connecthints, 0, sizeof(connecthints));
390 connecthints.ai_socktype = SOCK_DGRAM;
391 /* make sure we try to connect only to the same family */
392 connecthints.ai_family = udp->family;
393 if (connecting) {
394 err = inet_tryconnect(&udp->sock, &udp->family, address,
395 port, tm, &connecthints);
396 if (err) {
397 lua_pushnil(L);
398 lua_pushstring(L, err);
399 return 2;
400 }
401 auxiliar_setclass(L, "udp{connected}", 1);
402 } else {
403 /* we ignore possible errors because Mac OS X always
404 * returns EAFNOSUPPORT */
405 inet_trydisconnect(&udp->sock, udp->family, tm);
406 auxiliar_setclass(L, "udp{unconnected}", 1);
407 }
408 lua_pushnumber(L, 1);
409 return 1;
410 }
411
412 /*-------------------------------------------------------------------------*\
413 * Closes socket used by object
414 \*-------------------------------------------------------------------------*/
415 static int meth_close(lua_State *L) {
416 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
417 socket_destroy(&udp->sock);
418 lua_pushnumber(L, 1);
419 return 1;
420 }
421
422 /*-------------------------------------------------------------------------*\
423 * Turns a master object into a server object
424 \*-------------------------------------------------------------------------*/
425 static int meth_setsockname(lua_State *L) {
426 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
427 const char *address = luaL_checkstring(L, 2);
428 const char *port = luaL_checkstring(L, 3);
429 const char *err;
430 struct addrinfo bindhints;
431 memset(&bindhints, 0, sizeof(bindhints));
432 bindhints.ai_socktype = SOCK_DGRAM;
433 bindhints.ai_family = udp->family;
434 bindhints.ai_flags = AI_PASSIVE;
435 err = inet_trybind(&udp->sock, &udp->family, address, port, &bindhints);
436 if (err) {
437 lua_pushnil(L);
438 lua_pushstring(L, err);
439 return 2;
440 }
441 lua_pushnumber(L, 1);
442 return 1;
443 }
444
445 /*=========================================================================*\
446 * Library functions
447 \*=========================================================================*/
448 /*-------------------------------------------------------------------------*\
449 * Creates a master udp object
450 \*-------------------------------------------------------------------------*/
451 static int udp_create(lua_State *L, int family) {
452 /* allocate udp object */
453 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
454 auxiliar_setclass(L, "udp{unconnected}", -1);
455 /* if family is AF_UNSPEC, we leave the socket invalid and
456 * store AF_UNSPEC into family. This will allow it to later be
457 * replaced with an AF_INET6 or AF_INET socket upon first use. */
458 udp->sock = SOCKET_INVALID;
459 timeout_init(&udp->tm, -1, -1);
460 udp->family = family;
461 if (family != AF_UNSPEC) {
462 const char *err = inet_trycreate(&udp->sock, family, SOCK_DGRAM, 0);
463 if (err != NULL) {
464 lua_pushnil(L);
465 lua_pushstring(L, err);
466 return 2;
467 }
468 socket_setnonblocking(&udp->sock);
469 }
470 return 1;
471 }
472
473 static int global_create(lua_State *L) {
474 return udp_create(L, AF_UNSPEC);
475 }
476
477 static int global_create4(lua_State *L) {
478 return udp_create(L, AF_INET);
479 }
480
481 static int global_create6(lua_State *L) {
482 return udp_create(L, AF_INET6);
483 }