anet.c: Allow creation of TCP listening sockets bound to N addresses.

This commit is contained in:
antirez 2013-07-04 18:48:46 +02:00
parent 585b0a61ce
commit bd234d62bb
2 changed files with 16 additions and 9 deletions

View File

@ -331,9 +331,9 @@ static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) {
return ANET_OK;
}
int anetTcpServer(char *err, int port, char *bindaddr)
int anetTcpServer(char *err, int port, char **bindaddr, int bindaddr_count)
{
int s;
int s, j;
struct sockaddr_in sa;
if ((s = anetCreateSocket(err,AF_INET)) == ANET_ERR)
@ -343,13 +343,20 @@ int anetTcpServer(char *err, int port, char *bindaddr)
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if (bindaddr && inet_aton(bindaddr, &sa.sin_addr) == 0) {
anetSetError(err, "invalid bind address");
close(s);
return ANET_ERR;
if (bindaddr_count) {
for (j = 0; j < bindaddr_count; j++) {
if (inet_aton(bindaddr[j], &sa.sin_addr) == 0) {
anetSetError(err, "invalid bind address");
close(s);
return ANET_ERR;
}
if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR)
return ANET_ERR;
}
} else {
if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR)
return ANET_ERR;
}
if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR)
return ANET_ERR;
return s;
}

View File

@ -45,7 +45,7 @@ int anetUnixConnect(char *err, char *path);
int anetUnixNonBlockConnect(char *err, char *path);
int anetRead(int fd, char *buf, int count);
int anetResolve(char *err, char *host, char *ipbuf);
int anetTcpServer(char *err, int port, char *bindaddr);
int anetTcpServer(char *err, int port, char **bindaddr, int bindaddr_count);
int anetUnixServer(char *err, char *path, mode_t perm);
int anetTcpAccept(char *err, int serversock, char *ip, int *port);
int anetUnixAccept(char *err, int serversock);