diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2015-08-25 15:41:40 -0300 |
---|---|---|
committer | Diego Nehab <diego.nehab@gmail.com> | 2015-08-25 15:41:40 -0300 |
commit | 77bba625d7aaa0f9e118879163687fcbcb0b5a7b (patch) | |
tree | bbb719610c306b0af3225784164b44e2c509eb7c /src/udp.c | |
parent | 96965b179c7311f850f72a8629b9ba6d3a31d117 (diff) | |
download | luasocket-77bba625d7aaa0f9e118879163687fcbcb0b5a7b.tar.gz luasocket-77bba625d7aaa0f9e118879163687fcbcb0b5a7b.tar.bz2 luasocket-77bba625d7aaa0f9e118879163687fcbcb0b5a7b.zip |
Fixes suggested by @Florob in #147.
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 52 |
1 files changed, 21 insertions, 31 deletions
@@ -185,7 +185,7 @@ static int meth_sendto(lua_State *L) { | |||
185 | return 2; | 185 | return 2; |
186 | } | 186 | } |
187 | timeout_markstart(tm); | 187 | timeout_markstart(tm); |
188 | err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr, | 188 | err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr, |
189 | (socklen_t) ai->ai_addrlen, tm); | 189 | (socklen_t) ai->ai_addrlen, tm); |
190 | freeaddrinfo(ai); | 190 | freeaddrinfo(ai); |
191 | if (err != IO_DONE) { | 191 | if (err != IO_DONE) { |
@@ -237,7 +237,7 @@ static int meth_receivefrom(lua_State *L) | |||
237 | char portstr[6]; | 237 | char portstr[6]; |
238 | timeout_markstart(tm); | 238 | timeout_markstart(tm); |
239 | count = MIN(count, sizeof(buffer)); | 239 | count = MIN(count, sizeof(buffer)); |
240 | err = socket_recvfrom(&udp->sock, buffer, count, &got, (SA *) &addr, | 240 | err = socket_recvfrom(&udp->sock, buffer, count, &got, (SA *) &addr, |
241 | &addr_len, tm); | 241 | &addr_len, tm); |
242 | /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ | 242 | /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ |
243 | if (err == IO_CLOSED) | 243 | if (err == IO_CLOSED) |
@@ -247,7 +247,7 @@ static int meth_receivefrom(lua_State *L) | |||
247 | lua_pushstring(L, udp_strerror(err)); | 247 | lua_pushstring(L, udp_strerror(err)); |
248 | return 2; | 248 | return 2; |
249 | } | 249 | } |
250 | err = getnameinfo((struct sockaddr *)&addr, addr_len, addrstr, | 250 | err = getnameinfo((struct sockaddr *)&addr, addr_len, addrstr, |
251 | INET6_ADDRSTRLEN, portstr, 6, NI_NUMERICHOST | NI_NUMERICSERV); | 251 | INET6_ADDRSTRLEN, portstr, 6, NI_NUMERICHOST | NI_NUMERICSERV); |
252 | if (err) { | 252 | if (err) { |
253 | lua_pushnil(L); | 253 | lua_pushnil(L); |
@@ -351,7 +351,7 @@ static int meth_setpeername(lua_State *L) { | |||
351 | /* make sure we try to connect only to the same family */ | 351 | /* make sure we try to connect only to the same family */ |
352 | connecthints.ai_family = udp->family; | 352 | connecthints.ai_family = udp->family; |
353 | if (connecting) { | 353 | if (connecting) { |
354 | err = inet_tryconnect(&udp->sock, &udp->family, address, | 354 | err = inet_tryconnect(&udp->sock, &udp->family, address, |
355 | port, tm, &connecthints); | 355 | port, tm, &connecthints); |
356 | if (err) { | 356 | if (err) { |
357 | lua_pushnil(L); | 357 | lua_pushnil(L); |
@@ -365,7 +365,6 @@ static int meth_setpeername(lua_State *L) { | |||
365 | inet_trydisconnect(&udp->sock, udp->family, tm); | 365 | inet_trydisconnect(&udp->sock, udp->family, tm); |
366 | auxiliar_setclass(L, "udp{unconnected}", 1); | 366 | auxiliar_setclass(L, "udp{unconnected}", 1); |
367 | } | 367 | } |
368 | /* change class to connected or unconnected depending on address */ | ||
369 | lua_pushnumber(L, 1); | 368 | lua_pushnumber(L, 1); |
370 | return 1; | 369 | return 1; |
371 | } | 370 | } |
@@ -410,34 +409,25 @@ static int meth_setsockname(lua_State *L) { | |||
410 | * Creates a master udp object | 409 | * Creates a master udp object |
411 | \*-------------------------------------------------------------------------*/ | 410 | \*-------------------------------------------------------------------------*/ |
412 | static int udp_create(lua_State *L, int family) { | 411 | static int udp_create(lua_State *L, int family) { |
413 | t_socket sock; | 412 | /* allocate udp object */ |
414 | /* if family is AF_UNSPEC, we create an AF_INET socket | 413 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |
415 | * but store AF_UNSPEC into tcp-family. This will allow it | 414 | auxiliar_setclass(L, "udp{unconnected}", -1); |
416 | * later be replaced with an AF_INET6 socket if | 415 | /* if family is AF_UNSPEC, we leave the socket invalid and |
417 | * trybind or tryconnect prefer it instead. */ | 416 | * store AF_UNSPEC into family. This will allow it to later be |
418 | const char *err = inet_trycreate(&sock, family == AF_UNSPEC? | 417 | * replaced with an AF_INET6 or AF_INET socket upon first use. */ |
419 | AF_INET: family, SOCK_DGRAM); | 418 | udp->sock = SOCKET_INVALID; |
420 | /* try to allocate a system socket */ | 419 | timeout_init(&udp->tm, -1, -1); |
421 | if (!err) { | 420 | udp->family = family; |
422 | /* allocate udp object */ | 421 | if (family != AF_UNSPEC) { |
423 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | 422 | const char *err = inet_trycreate(&udp->sock, family, SOCK_DGRAM, 0); |
424 | auxiliar_setclass(L, "udp{unconnected}", -1); | 423 | if (err != NULL) { |
425 | /* initialize remaining structure fields */ | 424 | lua_pushnil(L); |
426 | socket_setnonblocking(&sock); | 425 | lua_pushstring(L, err); |
427 | if (family == AF_INET6) { | 426 | return 2; |
428 | int yes = 1; | ||
429 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | ||
430 | (void *)&yes, sizeof(yes)); | ||
431 | } | 427 | } |
432 | udp->sock = sock; | 428 | socket_setnonblocking(&udp->sock); |
433 | timeout_init(&udp->tm, -1, -1); | ||
434 | udp->family = family; | ||
435 | return 1; | ||
436 | } else { | ||
437 | lua_pushnil(L); | ||
438 | lua_pushstring(L, err); | ||
439 | return 2; | ||
440 | } | 429 | } |
430 | return 1; | ||
441 | } | 431 | } |
442 | 432 | ||
443 | static int global_create(lua_State *L) { | 433 | static int global_create(lua_State *L) { |