aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/udp.c')
-rw-r--r--src/udp.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/udp.c b/src/udp.c
index 0f9b7cc..94bf125 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -27,6 +27,7 @@
27* Internal function prototypes 27* Internal function prototypes
28\*=========================================================================*/ 28\*=========================================================================*/
29static int global_create(lua_State *L); 29static int global_create(lua_State *L);
30static int global_create6(lua_State *L);
30static int meth_send(lua_State *L); 31static int meth_send(lua_State *L);
31static int meth_sendto(lua_State *L); 32static int meth_sendto(lua_State *L);
32static int meth_receive(lua_State *L); 33static int meth_receive(lua_State *L);
@@ -89,6 +90,7 @@ static t_opt optget[] = {
89/* functions in library namespace */ 90/* functions in library namespace */
90static luaL_reg func[] = { 91static luaL_reg func[] = {
91 {"udp", global_create}, 92 {"udp", global_create},
93 {"udp6", global_create6},
92 {NULL, NULL} 94 {NULL, NULL}
93}; 95};
94 96
@@ -317,8 +319,14 @@ static int meth_close(lua_State *L) {
317static int meth_setsockname(lua_State *L) { 319static int meth_setsockname(lua_State *L) {
318 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); 320 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
319 const char *address = luaL_checkstring(L, 2); 321 const char *address = luaL_checkstring(L, 2);
320 unsigned short port = (unsigned short) luaL_checknumber(L, 3); 322 const char *port = luaL_checkstring(L, 3);
321 const char *err = inet_trybind(&udp->sock, address, port); 323 const char *err;
324 struct addrinfo bindhints;
325 memset(&bindhints, 0, sizeof(bindhints));
326 bindhints.ai_socktype = SOCK_DGRAM;
327 bindhints.ai_family = udp->domain;
328 bindhints.ai_flags = AI_PASSIVE;
329 err = inet_trybind(&udp->sock, address, port, &bindhints);
322 if (err) { 330 if (err) {
323 lua_pushnil(L); 331 lua_pushnil(L);
324 lua_pushstring(L, err); 332 lua_pushstring(L, err);
@@ -334,18 +342,19 @@ static int meth_setsockname(lua_State *L) {
334/*-------------------------------------------------------------------------*\ 342/*-------------------------------------------------------------------------*\
335* Creates a master udp object 343* Creates a master udp object
336\*-------------------------------------------------------------------------*/ 344\*-------------------------------------------------------------------------*/
337static int global_create(lua_State *L) { 345static int udp_create(lua_State *L, int domain) {
338 t_socket sock; 346 t_socket sock;
339 const char *err = inet_trycreate(&sock, SOCK_DGRAM); 347 const char *err = inet_trycreate(&sock, domain, SOCK_DGRAM);
340 /* try to allocate a system socket */ 348 /* try to allocate a system socket */
341 if (!err) { 349 if (!err) {
342 /* allocate tcp object */ 350 /* allocate udp object */
343 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); 351 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
344 auxiliar_setclass(L, "udp{unconnected}", -1); 352 auxiliar_setclass(L, "udp{unconnected}", -1);
345 /* initialize remaining structure fields */ 353 /* initialize remaining structure fields */
346 socket_setnonblocking(&sock); 354 socket_setnonblocking(&sock);
347 udp->sock = sock; 355 udp->sock = sock;
348 timeout_init(&udp->tm, -1, -1); 356 timeout_init(&udp->tm, -1, -1);
357 udp->domain = domain;
349 return 1; 358 return 1;
350 } else { 359 } else {
351 lua_pushnil(L); 360 lua_pushnil(L);
@@ -353,3 +362,11 @@ static int global_create(lua_State *L) {
353 return 2; 362 return 2;
354 } 363 }
355} 364}
365
366static int global_create(lua_State *L) {
367 return udp_create(L, AF_INET);
368}
369
370static int global_create6(lua_State *L) {
371 return udp_create(L, AF_INET6);
372}