From 92038286e8c047966f779e8d7eefa6d2786bfa43 Mon Sep 17 00:00:00 2001 From: oranagra Date: Sun, 21 Aug 2016 20:39:15 +0300 Subject: [PATCH] fix tcp binding when IPv6 is unsupported --- src/anet.c | 2 +- src/server.c | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/anet.c b/src/anet.c index 1728f3eb..ef1711d0 100644 --- a/src/anet.c +++ b/src/anet.c @@ -486,7 +486,7 @@ static int _anetTcpServer(char *err, int port, char *bindaddr, int af, int backl goto end; } if (p == NULL) { - anetSetError(err, "unable to bind socket"); + anetSetError(err, "unable to bind socket, errno: %d", errno); goto error; } diff --git a/src/server.c b/src/server.c index a7758259..fe8364d0 100644 --- a/src/server.c +++ b/src/server.c @@ -1636,6 +1636,7 @@ int listenToPort(int port, int *fds, int *count) { if (server.bindaddr_count == 0) server.bindaddr[0] = NULL; for (j = 0; j < server.bindaddr_count || j == 0; j++) { if (server.bindaddr[j] == NULL) { + int unsupported = 0; /* Bind * for both IPv6 and IPv4, we enter here only if * server.bindaddr_count == 0. */ fds[*count] = anetTcp6Server(server.neterr,port,NULL, @@ -1643,19 +1644,22 @@ int listenToPort(int port, int *fds, int *count) { if (fds[*count] != ANET_ERR) { anetNonBlock(NULL,fds[*count]); (*count)++; + } else if (errno == EAFNOSUPPORT) + unsupported++; - /* Bind the IPv4 address as well. */ - fds[*count] = anetTcpServer(server.neterr,port,NULL, - server.tcp_backlog); - if (fds[*count] != ANET_ERR) { - anetNonBlock(NULL,fds[*count]); - (*count)++; - } - } + /* Bind the IPv4 address as well. */ + fds[*count] = anetTcpServer(server.neterr,port,NULL, + server.tcp_backlog); + if (fds[*count] != ANET_ERR) { + anetNonBlock(NULL,fds[*count]); + (*count)++; + } else if (errno == EAFNOSUPPORT) + unsupported++; /* Exit the loop if we were able to bind * on IPv4 and IPv6, + * or if one is unsupported by th OS. * otherwise fds[*count] will be ANET_ERR and we'll print an * error and return to the caller with an error. */ - if (*count == 2) break; + if (*count + unsupported == 2) break; } else if (strchr(server.bindaddr[j],':')) { /* Bind IPv6 address. */ fds[*count] = anetTcp6Server(server.neterr,port,server.bindaddr[j],