aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcp.c')
-rw-r--r--src/tcp.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/tcp.c b/src/tcp.c
index 6734dc0..60c1e8a 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -222,7 +222,6 @@ static int meth_bind(lua_State *L)
222 bindhints.ai_socktype = SOCK_STREAM; 222 bindhints.ai_socktype = SOCK_STREAM;
223 bindhints.ai_family = tcp->family; 223 bindhints.ai_family = tcp->family;
224 bindhints.ai_flags = AI_PASSIVE; 224 bindhints.ai_flags = AI_PASSIVE;
225 address = strcmp(address, "*")? address: NULL;
226 err = inet_trybind(&tcp->sock, address, port, &bindhints); 225 err = inet_trybind(&tcp->sock, address, port, &bindhints);
227 if (err) { 226 if (err) {
228 lua_pushnil(L); 227 lua_pushnil(L);
@@ -248,7 +247,8 @@ static int meth_connect(lua_State *L)
248 /* make sure we try to connect only to the same family */ 247 /* make sure we try to connect only to the same family */
249 connecthints.ai_family = tcp->family; 248 connecthints.ai_family = tcp->family;
250 timeout_markstart(&tcp->tm); 249 timeout_markstart(&tcp->tm);
251 err = inet_tryconnect(&tcp->sock, address, port, &tcp->tm, &connecthints); 250 err = inet_tryconnect(&tcp->sock, &tcp->family, address, port,
251 &tcp->tm, &connecthints);
252 /* have to set the class even if it failed due to non-blocking connects */ 252 /* have to set the class even if it failed due to non-blocking connects */
253 auxiliar_setclass(L, "tcp{client}", 1); 253 auxiliar_setclass(L, "tcp{client}", 1);
254 if (err) { 254 if (err) {
@@ -388,6 +388,7 @@ static int global_create6(lua_State *L) {
388 return tcp_create(L, AF_INET6); 388 return tcp_create(L, AF_INET6);
389} 389}
390 390
391#if 0
391static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, 392static const char *tryconnect6(const char *remoteaddr, const char *remoteserv,
392 struct addrinfo *connecthints, p_tcp tcp) { 393 struct addrinfo *connecthints, p_tcp tcp) {
393 struct addrinfo *iterator = NULL, *resolved = NULL; 394 struct addrinfo *iterator = NULL, *resolved = NULL;
@@ -402,8 +403,13 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv,
402 /* iterate over all returned addresses trying to connect */ 403 /* iterate over all returned addresses trying to connect */
403 for (iterator = resolved; iterator; iterator = iterator->ai_next) { 404 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
404 p_timeout tm = timeout_markstart(&tcp->tm); 405 p_timeout tm = timeout_markstart(&tcp->tm);
405 /* create new socket if one wasn't created by the bind stage */ 406 /* create new socket if necessary. if there was no
406 if (tcp->sock == SOCKET_INVALID) { 407 * bind, we need to create one for every new family
408 * that shows up while iterating. if there was a
409 * bind, all families will be the same and we will
410 * not enter this branch. */
411 if (tcp->family != iterator->ai_family) {
412 socket_destroy(&tcp->sock);
407 err = socket_strerror(socket_create(&tcp->sock, 413 err = socket_strerror(socket_create(&tcp->sock,
408 iterator->ai_family, iterator->ai_socktype, 414 iterator->ai_family, iterator->ai_socktype,
409 iterator->ai_protocol)); 415 iterator->ai_protocol));
@@ -427,6 +433,7 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv,
427 /* here, if err is set, we failed */ 433 /* here, if err is set, we failed */
428 return err; 434 return err;
429} 435}
436#endif
430 437
431static int global_connect(lua_State *L) { 438static int global_connect(lua_State *L) {
432 const char *remoteaddr = luaL_checkstring(L, 1); 439 const char *remoteaddr = luaL_checkstring(L, 1);
@@ -444,6 +451,7 @@ static int global_connect(lua_State *L) {
444 timeout_init(&tcp->tm, -1, -1); 451 timeout_init(&tcp->tm, -1, -1);
445 buffer_init(&tcp->buf, &tcp->io, &tcp->tm); 452 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
446 tcp->sock = SOCKET_INVALID; 453 tcp->sock = SOCKET_INVALID;
454 tcp->family = PF_UNSPEC;
447 /* allow user to pick local address and port */ 455 /* allow user to pick local address and port */
448 memset(&bindhints, 0, sizeof(bindhints)); 456 memset(&bindhints, 0, sizeof(bindhints));
449 bindhints.ai_socktype = SOCK_STREAM; 457 bindhints.ai_socktype = SOCK_STREAM;
@@ -463,7 +471,8 @@ static int global_connect(lua_State *L) {
463 connecthints.ai_socktype = SOCK_STREAM; 471 connecthints.ai_socktype = SOCK_STREAM;
464 /* make sure we try to connect only to the same family */ 472 /* make sure we try to connect only to the same family */
465 connecthints.ai_family = bindhints.ai_family; 473 connecthints.ai_family = bindhints.ai_family;
466 err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp); 474 err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv,
475 &tcp->tm, &connecthints);
467 if (err) { 476 if (err) {
468 socket_destroy(&tcp->sock); 477 socket_destroy(&tcp->sock);
469 lua_pushnil(L); 478 lua_pushnil(L);