This commit is contained in:
Max Lv
2015-01-22 09:12:19 +08:00
parent 37f9b9e75f
commit 726a1b7dd7
6 changed files with 31 additions and 28 deletions
+21 -19
View File
@@ -151,9 +151,9 @@ int create_and_bind(const char *host, const char *port)
int s, listen_sock;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_flags = AI_ALL|AI_V4MAPPED|AI_PASSIVE|AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_flags = AI_ALL | AI_V4MAPPED | AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_TCP;
s = getaddrinfo(host, port, &hints, &result);
@@ -165,24 +165,24 @@ int create_and_bind(const char *host, const char *port)
rp = result;
/*
On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with
AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to
return a list of addresses to listen on, but it is impossible to listen on
0.0.0.0 and :: at the same time, if :: implies dualstack mode.
*/
On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with
AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to
return a list of addresses to listen on, but it is impossible to listen on
0.0.0.0 and :: at the same time, if :: implies dualstack mode.
*/
if (!host) {
ipv4v6bindall = result;
/* Loop over all address infos found until a IPV6 address is found. */
while (ipv4v6bindall) {
if (ipv4v6bindall->ai_family == AF_INET6) {
rp = ipv4v6bindall; /* Take first IPV6 address available */
break;
rp = ipv4v6bindall; /* Take first IPV6 address available */
break;
}
ipv4v6bindall= ipv4v6bindall->ai_next; /* Get next address info, if any */
}
}
ipv4v6bindall = ipv4v6bindall->ai_next; /* Get next address info, if any */
}
}
for (/*rp = result*/; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
@@ -194,10 +194,12 @@ int create_and_bind(const char *host, const char *port)
if (rp->ai_family == AF_INET6) {
int ipv6only = 0;
if (host) ipv6only = 1;
if (host) {
ipv6only = 1;
}
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only));
}
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
@@ -1132,9 +1134,9 @@ int main(int argc, char **argv)
}
}
if (server_num == 0) {
server_host[server_num++]=NULL;
}
if (server_num == 0) {
server_host[server_num++] = NULL;
}
if (server_num == 0 || server_port == NULL || password == NULL) {
usage();