diff options
| author | Diego Nehab <diego@impa.br> | 2013-05-26 21:26:26 +0800 |
|---|---|---|
| committer | Diego Nehab <diego@impa.br> | 2013-05-26 21:26:26 +0800 |
| commit | 427220c7b177cdae2379ac419d975c69764ba8ee (patch) | |
| tree | b2690db3cb9cde4505ad51eaac83d3fe4a854ff5 | |
| parent | 6d93fd7c8f04fecbcdc28994da8b8357f712463f (diff) | |
| download | luasocket-427220c7b177cdae2379ac419d975c69764ba8ee.tar.gz luasocket-427220c7b177cdae2379ac419d975c69764ba8ee.tar.bz2 luasocket-427220c7b177cdae2379ac419d975c69764ba8ee.zip | |
Merge tryconnect6 into inet_tryconnect.
| -rw-r--r-- | src/inet.c | 22 | ||||
| -rw-r--r-- | src/inet.h | 2 | ||||
| -rw-r--r-- | src/tcp.c | 18 | ||||
| -rw-r--r-- | src/udp.c | 3 |
4 files changed, 29 insertions, 16 deletions
| @@ -396,7 +396,7 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) | |||
| 396 | /*-------------------------------------------------------------------------*\ | 396 | /*-------------------------------------------------------------------------*\ |
| 397 | * Tries to connect to remote address (address, port) | 397 | * Tries to connect to remote address (address, port) |
| 398 | \*-------------------------------------------------------------------------*/ | 398 | \*-------------------------------------------------------------------------*/ |
| 399 | const char *inet_tryconnect(p_socket ps, const char *address, | 399 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, |
| 400 | const char *serv, p_timeout tm, struct addrinfo *connecthints) | 400 | const char *serv, p_timeout tm, struct addrinfo *connecthints) |
| 401 | { | 401 | { |
| 402 | struct addrinfo *iterator = NULL, *resolved = NULL; | 402 | struct addrinfo *iterator = NULL, *resolved = NULL; |
| @@ -410,6 +410,23 @@ const char *inet_tryconnect(p_socket ps, const char *address, | |||
| 410 | } | 410 | } |
| 411 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 411 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
| 412 | timeout_markstart(tm); | 412 | timeout_markstart(tm); |
| 413 | /* create new socket if necessary. if there was no | ||
| 414 | * bind, we need to create one for every new family | ||
| 415 | * that shows up while iterating. if there was a | ||
| 416 | * bind, all families will be the same and we will | ||
| 417 | * not enter this branch. */ | ||
| 418 | if (*family != iterator->ai_family) { | ||
| 419 | socket_destroy(ps); | ||
| 420 | err = socket_strerror(socket_create(ps, iterator->ai_family, | ||
| 421 | iterator->ai_socktype, iterator->ai_protocol)); | ||
| 422 | if (err != NULL) { | ||
| 423 | freeaddrinfo(resolved); | ||
| 424 | return err; | ||
| 425 | } | ||
| 426 | *family = iterator->ai_family; | ||
| 427 | /* all sockets initially non-blocking */ | ||
| 428 | socket_setnonblocking(ps); | ||
| 429 | } | ||
| 413 | /* try connecting to remote address */ | 430 | /* try connecting to remote address */ |
| 414 | err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, | 431 | err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, |
| 415 | (socklen_t) iterator->ai_addrlen, tm)); | 432 | (socklen_t) iterator->ai_addrlen, tm)); |
| @@ -424,7 +441,8 @@ const char *inet_tryconnect(p_socket ps, const char *address, | |||
| 424 | /*-------------------------------------------------------------------------*\ | 441 | /*-------------------------------------------------------------------------*\ |
| 425 | * Tries to accept a socket | 442 | * Tries to accept a socket |
| 426 | \*-------------------------------------------------------------------------*/ | 443 | \*-------------------------------------------------------------------------*/ |
| 427 | const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) | 444 | const char *inet_tryaccept(p_socket server, int family, p_socket client, |
| 445 | p_timeout tm) | ||
| 428 | { | 446 | { |
| 429 | socklen_t len; | 447 | socklen_t len; |
| 430 | t_sockaddr_storage addr; | 448 | t_sockaddr_storage addr; |
| @@ -26,7 +26,7 @@ | |||
| 26 | int inet_open(lua_State *L); | 26 | int inet_open(lua_State *L); |
| 27 | 27 | ||
| 28 | const char *inet_trycreate(p_socket ps, int family, int type); | 28 | const char *inet_trycreate(p_socket ps, int family, int type); |
| 29 | const char *inet_tryconnect(p_socket ps, const char *address, | 29 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, |
| 30 | const char *serv, p_timeout tm, struct addrinfo *connecthints); | 30 | const char *serv, p_timeout tm, struct addrinfo *connecthints); |
| 31 | const char *inet_trybind(p_socket ps, const char *address, const char *serv, | 31 | const char *inet_trybind(p_socket ps, const char *address, const char *serv, |
| 32 | struct addrinfo *bindhints); | 32 | struct addrinfo *bindhints); |
| @@ -248,7 +248,8 @@ static int meth_connect(lua_State *L) | |||
| 248 | /* make sure we try to connect only to the same family */ | 248 | /* make sure we try to connect only to the same family */ |
| 249 | connecthints.ai_family = tcp->family; | 249 | connecthints.ai_family = tcp->family; |
| 250 | timeout_markstart(&tcp->tm); | 250 | timeout_markstart(&tcp->tm); |
| 251 | err = inet_tryconnect(&tcp->sock, address, port, &tcp->tm, &connecthints); | 251 | err = inet_tryconnect(&tcp->sock, &tcp->family, address, port, |
| 252 | &tcp->tm, &connecthints); | ||
| 252 | /* have to set the class even if it failed due to non-blocking connects */ | 253 | /* have to set the class even if it failed due to non-blocking connects */ |
| 253 | auxiliar_setclass(L, "tcp{client}", 1); | 254 | auxiliar_setclass(L, "tcp{client}", 1); |
| 254 | if (err) { | 255 | if (err) { |
| @@ -388,20 +389,11 @@ static int global_create6(lua_State *L) { | |||
| 388 | return tcp_create(L, AF_INET6); | 389 | return tcp_create(L, AF_INET6); |
| 389 | } | 390 | } |
| 390 | 391 | ||
| 391 | const char *strfamily(int family) { | 392 | #if 0 |
| 392 | switch (family) { | ||
| 393 | case PF_UNSPEC: return "unspec"; | ||
| 394 | case PF_INET: return "inet"; | ||
| 395 | case PF_INET6: return "inet6"; | ||
| 396 | default: return "invalid"; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | 393 | static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, |
| 401 | struct addrinfo *connecthints, p_tcp tcp) { | 394 | struct addrinfo *connecthints, p_tcp tcp) { |
| 402 | struct addrinfo *iterator = NULL, *resolved = NULL; | 395 | struct addrinfo *iterator = NULL, *resolved = NULL; |
| 403 | const char *err = NULL; | 396 | const char *err = NULL; |
| 404 | int i = 0; | ||
| 405 | /* try resolving */ | 397 | /* try resolving */ |
| 406 | err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv, | 398 | err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv, |
| 407 | connecthints, &resolved)); | 399 | connecthints, &resolved)); |
| @@ -442,6 +434,7 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | |||
| 442 | /* here, if err is set, we failed */ | 434 | /* here, if err is set, we failed */ |
| 443 | return err; | 435 | return err; |
| 444 | } | 436 | } |
| 437 | #endif | ||
| 445 | 438 | ||
| 446 | static int global_connect(lua_State *L) { | 439 | static int global_connect(lua_State *L) { |
| 447 | const char *remoteaddr = luaL_checkstring(L, 1); | 440 | const char *remoteaddr = luaL_checkstring(L, 1); |
| @@ -479,7 +472,8 @@ static int global_connect(lua_State *L) { | |||
| 479 | connecthints.ai_socktype = SOCK_STREAM; | 472 | connecthints.ai_socktype = SOCK_STREAM; |
| 480 | /* make sure we try to connect only to the same family */ | 473 | /* make sure we try to connect only to the same family */ |
| 481 | connecthints.ai_family = bindhints.ai_family; | 474 | connecthints.ai_family = bindhints.ai_family; |
| 482 | err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp); | 475 | err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, |
| 476 | &tcp->tm, &connecthints); | ||
| 483 | if (err) { | 477 | if (err) { |
| 484 | socket_destroy(&tcp->sock); | 478 | socket_destroy(&tcp->sock); |
| 485 | lua_pushnil(L); | 479 | lua_pushnil(L); |
| @@ -376,7 +376,8 @@ static int meth_setpeername(lua_State *L) { | |||
| 376 | /* make sure we try to connect only to the same family */ | 376 | /* make sure we try to connect only to the same family */ |
| 377 | connecthints.ai_family = udp->family; | 377 | connecthints.ai_family = udp->family; |
| 378 | if (connecting) { | 378 | if (connecting) { |
| 379 | err = inet_tryconnect(&udp->sock, address, port, tm, &connecthints); | 379 | err = inet_tryconnect(&udp->sock, &udp->family, address, |
| 380 | port, tm, &connecthints); | ||
| 380 | if (err) { | 381 | if (err) { |
| 381 | lua_pushnil(L); | 382 | lua_pushnil(L); |
| 382 | lua_pushstring(L, err); | 383 | lua_pushstring(L, err); |
