aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@impa.br>2013-05-26 21:26:26 +0800
committerDiego Nehab <diego@impa.br>2013-05-26 21:26:26 +0800
commit427220c7b177cdae2379ac419d975c69764ba8ee (patch)
treeb2690db3cb9cde4505ad51eaac83d3fe4a854ff5
parent6d93fd7c8f04fecbcdc28994da8b8357f712463f (diff)
downloadluasocket-427220c7b177cdae2379ac419d975c69764ba8ee.tar.gz
luasocket-427220c7b177cdae2379ac419d975c69764ba8ee.tar.bz2
luasocket-427220c7b177cdae2379ac419d975c69764ba8ee.zip
Merge tryconnect6 into inet_tryconnect.
-rw-r--r--src/inet.c22
-rw-r--r--src/inet.h2
-rw-r--r--src/tcp.c18
-rw-r--r--src/udp.c3
4 files changed, 29 insertions, 16 deletions
diff --git a/src/inet.c b/src/inet.c
index 1530fef..5bc6364 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -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\*-------------------------------------------------------------------------*/
399const char *inet_tryconnect(p_socket ps, const char *address, 399const 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\*-------------------------------------------------------------------------*/
427const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) 444const 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;
diff --git a/src/inet.h b/src/inet.h
index 2f72533..252e293 100644
--- a/src/inet.h
+++ b/src/inet.h
@@ -26,7 +26,7 @@
26int inet_open(lua_State *L); 26int inet_open(lua_State *L);
27 27
28const char *inet_trycreate(p_socket ps, int family, int type); 28const char *inet_trycreate(p_socket ps, int family, int type);
29const char *inet_tryconnect(p_socket ps, const char *address, 29const 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);
31const char *inet_trybind(p_socket ps, const char *address, const char *serv, 31const char *inet_trybind(p_socket ps, const char *address, const char *serv,
32 struct addrinfo *bindhints); 32 struct addrinfo *bindhints);
diff --git a/src/tcp.c b/src/tcp.c
index 4b0451f..ca8eec2 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -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
391const 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
400static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, 393static 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
446static int global_connect(lua_State *L) { 439static 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);
diff --git a/src/udp.c b/src/udp.c
index 6e0de9f..2a51d1c 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -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);