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/tcp.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/tcp.c')
-rw-r--r-- | src/tcp.c | 52 |
1 files changed, 21 insertions, 31 deletions
@@ -355,39 +355,29 @@ static int meth_settimeout(lua_State *L) | |||
355 | * Creates a master tcp object | 355 | * Creates a master tcp object |
356 | \*-------------------------------------------------------------------------*/ | 356 | \*-------------------------------------------------------------------------*/ |
357 | static int tcp_create(lua_State *L, int family) { | 357 | static int tcp_create(lua_State *L, int family) { |
358 | t_socket sock; | 358 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); |
359 | /* if family is AF_UNSPEC, we create an AF_INET socket | 359 | memset(tcp, 0, sizeof(t_tcp)); |
360 | * but store AF_UNSPEC into tcp-family. This will allow it | 360 | /* set its type as master object */ |
361 | * later be replaced with an AF_INET6 socket if | 361 | auxiliar_setclass(L, "tcp{master}", -1); |
362 | * trybind or tryconnect prefer it instead. */ | 362 | /* if family is AF_UNSPEC, we leave the socket invalid and |
363 | const char *err = inet_trycreate(&sock, family == AF_UNSPEC? | 363 | * store AF_UNSPEC into family. This will allow it to later be |
364 | AF_INET: family, SOCK_STREAM); | 364 | * replaced with an AF_INET6 or AF_INET socket upon first use. */ |
365 | /* try to allocate a system socket */ | 365 | tcp->sock = SOCKET_INVALID; |
366 | if (!err) { | 366 | tcp->family = family; |
367 | /* allocate tcp object */ | 367 | io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, |
368 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); | 368 | (p_error) socket_ioerror, &tcp->sock); |
369 | memset(tcp, 0, sizeof(t_tcp)); | 369 | timeout_init(&tcp->tm, -1, -1); |
370 | /* set its type as master object */ | 370 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); |
371 | auxiliar_setclass(L, "tcp{master}", -1); | 371 | if (family != AF_UNSPEC) { |
372 | /* initialize remaining structure fields */ | 372 | const char *err = inet_trycreate(&tcp->sock, family, SOCK_STREAM, 0); |
373 | socket_setnonblocking(&sock); | 373 | if (err != NULL) { |
374 | if (family == AF_INET6) { | 374 | lua_pushnil(L); |
375 | int yes = 1; | 375 | lua_pushstring(L, err); |
376 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | 376 | return 2; |
377 | (void *)&yes, sizeof(yes)); | ||
378 | } | 377 | } |
379 | tcp->sock = sock; | 378 | socket_setnonblocking(&tcp->sock); |
380 | io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, | ||
381 | (p_error) socket_ioerror, &tcp->sock); | ||
382 | timeout_init(&tcp->tm, -1, -1); | ||
383 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); | ||
384 | tcp->family = family; | ||
385 | return 1; | ||
386 | } else { | ||
387 | lua_pushnil(L); | ||
388 | lua_pushstring(L, err); | ||
389 | return 2; | ||
390 | } | 379 | } |
380 | return 1; | ||
391 | } | 381 | } |
392 | 382 | ||
393 | static int global_create(lua_State *L) { | 383 | static int global_create(lua_State *L) { |