From c77b95fd01cb6959005121b91e21037cd3da1c88 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2016 10:02:42 +0200 Subject: [PATCH] Bind both IPv4 and IPv6 or exit with an error by default. Thanks to @tushar2708 for the PR. I applied a slightly different fix. Thanks to @cespare for reporting. Close #3024 Close #3020 --- src/server.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/server.c b/src/server.c index bab8bb67..bd56ceeb 100644 --- a/src/server.c +++ b/src/server.c @@ -1789,17 +1789,19 @@ int listenToPort(int port, int *fds, int *count) { 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)++; + } } - fds[*count] = anetTcpServer(server.neterr,port,NULL, - server.tcp_backlog); - if (fds[*count] != ANET_ERR) { - anetNonBlock(NULL,fds[*count]); - (*count)++; - } - /* Exit the loop if we were able to bind * on IPv4 or IPv6, + /* Exit the loop if we were able to bind * on IPv4 and IPv6, * otherwise fds[*count] will be ANET_ERR and we'll print an * error and return to the caller with an error. */ - if (*count) break; + if (*count == 2) break; } else if (strchr(server.bindaddr[j],':')) { /* Bind IPv6 address. */ fds[*count] = anetTcp6Server(server.neterr,port,server.bindaddr[j],