diff options
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 31 |
1 files changed, 23 insertions, 8 deletions
@@ -1,8 +1,6 @@ | |||
1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
2 | * UDP object | 2 | * UDP object |
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | * | ||
5 | * RCS ID: $Id: udp.c,v 1.30 2009/05/27 09:31:35 diego Exp $ | ||
6 | \*=========================================================================*/ | 4 | \*=========================================================================*/ |
7 | #include <string.h> | 5 | #include <string.h> |
8 | 6 | ||
@@ -32,6 +30,7 @@ static int meth_send(lua_State *L); | |||
32 | static int meth_sendto(lua_State *L); | 30 | static int meth_sendto(lua_State *L); |
33 | static int meth_receive(lua_State *L); | 31 | static int meth_receive(lua_State *L); |
34 | static int meth_receivefrom(lua_State *L); | 32 | static int meth_receivefrom(lua_State *L); |
33 | static int meth_getfamily(lua_State *L); | ||
35 | static int meth_getsockname(lua_State *L); | 34 | static int meth_getsockname(lua_State *L); |
36 | static int meth_getpeername(lua_State *L); | 35 | static int meth_getpeername(lua_State *L); |
37 | static int meth_setsockname(lua_State *L); | 36 | static int meth_setsockname(lua_State *L); |
@@ -50,6 +49,7 @@ static luaL_Reg udp_methods[] = { | |||
50 | {"__tostring", auxiliar_tostring}, | 49 | {"__tostring", auxiliar_tostring}, |
51 | {"close", meth_close}, | 50 | {"close", meth_close}, |
52 | {"dirty", meth_dirty}, | 51 | {"dirty", meth_dirty}, |
52 | {"getfamily", meth_getfamily}, | ||
53 | {"getfd", meth_getfd}, | 53 | {"getfd", meth_getfd}, |
54 | {"getpeername", meth_getpeername}, | 54 | {"getpeername", meth_getpeername}, |
55 | {"getsockname", meth_getsockname}, | 55 | {"getsockname", meth_getsockname}, |
@@ -227,6 +227,21 @@ static int meth_receivefrom(lua_State *L) { | |||
227 | } | 227 | } |
228 | 228 | ||
229 | /*-------------------------------------------------------------------------*\ | 229 | /*-------------------------------------------------------------------------*\ |
230 | * Returns family as string | ||
231 | \*-------------------------------------------------------------------------*/ | ||
232 | static int meth_getfamily(lua_State *L) | ||
233 | { | ||
234 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); | ||
235 | if (udp->family == PF_INET6) { | ||
236 | lua_pushliteral(L, "inet6"); | ||
237 | return 1; | ||
238 | } else { | ||
239 | lua_pushliteral(L, "inet4"); | ||
240 | return 1; | ||
241 | } | ||
242 | } | ||
243 | |||
244 | /*-------------------------------------------------------------------------*\ | ||
230 | * Select support methods | 245 | * Select support methods |
231 | \*-------------------------------------------------------------------------*/ | 246 | \*-------------------------------------------------------------------------*/ |
232 | static int meth_getfd(lua_State *L) { | 247 | static int meth_getfd(lua_State *L) { |
@@ -302,7 +317,7 @@ static int meth_setpeername(lua_State *L) { | |||
302 | memset(&connecthints, 0, sizeof(connecthints)); | 317 | memset(&connecthints, 0, sizeof(connecthints)); |
303 | connecthints.ai_socktype = SOCK_DGRAM; | 318 | connecthints.ai_socktype = SOCK_DGRAM; |
304 | /* make sure we try to connect only to the same family */ | 319 | /* make sure we try to connect only to the same family */ |
305 | connecthints.ai_family = udp->domain; | 320 | connecthints.ai_family = udp->family; |
306 | err = inet_tryconnect(&udp->sock, address, port, | 321 | err = inet_tryconnect(&udp->sock, address, port, |
307 | tm, &connecthints); | 322 | tm, &connecthints); |
308 | if (err) { | 323 | if (err) { |
@@ -338,7 +353,7 @@ static int meth_setsockname(lua_State *L) { | |||
338 | struct addrinfo bindhints; | 353 | struct addrinfo bindhints; |
339 | memset(&bindhints, 0, sizeof(bindhints)); | 354 | memset(&bindhints, 0, sizeof(bindhints)); |
340 | bindhints.ai_socktype = SOCK_DGRAM; | 355 | bindhints.ai_socktype = SOCK_DGRAM; |
341 | bindhints.ai_family = udp->domain; | 356 | bindhints.ai_family = udp->family; |
342 | bindhints.ai_flags = AI_PASSIVE; | 357 | bindhints.ai_flags = AI_PASSIVE; |
343 | err = inet_trybind(&udp->sock, address, port, &bindhints); | 358 | err = inet_trybind(&udp->sock, address, port, &bindhints); |
344 | if (err) { | 359 | if (err) { |
@@ -356,9 +371,9 @@ static int meth_setsockname(lua_State *L) { | |||
356 | /*-------------------------------------------------------------------------*\ | 371 | /*-------------------------------------------------------------------------*\ |
357 | * Creates a master udp object | 372 | * Creates a master udp object |
358 | \*-------------------------------------------------------------------------*/ | 373 | \*-------------------------------------------------------------------------*/ |
359 | static int udp_create(lua_State *L, int domain) { | 374 | static int udp_create(lua_State *L, int family) { |
360 | t_socket sock; | 375 | t_socket sock; |
361 | const char *err = inet_trycreate(&sock, domain, SOCK_DGRAM); | 376 | const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); |
362 | /* try to allocate a system socket */ | 377 | /* try to allocate a system socket */ |
363 | if (!err) { | 378 | if (!err) { |
364 | /* allocate udp object */ | 379 | /* allocate udp object */ |
@@ -366,14 +381,14 @@ static int udp_create(lua_State *L, int domain) { | |||
366 | auxiliar_setclass(L, "udp{unconnected}", -1); | 381 | auxiliar_setclass(L, "udp{unconnected}", -1); |
367 | /* initialize remaining structure fields */ | 382 | /* initialize remaining structure fields */ |
368 | socket_setnonblocking(&sock); | 383 | socket_setnonblocking(&sock); |
369 | if (domain == PF_INET6) { | 384 | if (family == PF_INET6) { |
370 | int yes = 1; | 385 | int yes = 1; |
371 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | 386 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
372 | (void *)&yes, sizeof(yes)); | 387 | (void *)&yes, sizeof(yes)); |
373 | } | 388 | } |
374 | udp->sock = sock; | 389 | udp->sock = sock; |
375 | timeout_init(&udp->tm, -1, -1); | 390 | timeout_init(&udp->tm, -1, -1); |
376 | udp->domain = domain; | 391 | udp->family = family; |
377 | return 1; | 392 | return 1; |
378 | } else { | 393 | } else { |
379 | lua_pushnil(L); | 394 | lua_pushnil(L); |