Refine port binding

This commit is contained in:
Max Lv
2017-03-16 13:06:10 +08:00
parent b25c071822
commit 1af0272702
5 changed files with 36 additions and 23 deletions
+8 -5
View File
@@ -156,13 +156,20 @@ create_and_bind(const char *addr, const char *port)
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 */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s));
return -1;
}
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) {
@@ -190,11 +197,7 @@ create_and_bind(const char *addr, const char *port)
}
close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
}
freeaddrinfo(result);
+8 -5
View File
@@ -140,12 +140,19 @@ create_and_bind(const char *addr, const char *port)
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s));
return -1;
}
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) {
@@ -173,11 +180,7 @@ create_and_bind(const char *addr, const char *port)
}
close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
}
freeaddrinfo(result);
+8 -5
View File
@@ -328,6 +328,8 @@ create_and_bind(const char *host, const char *port, int mptcp)
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_TCP;
result = NULL;
for (int i = 1; i < 8; i++) {
s = getaddrinfo(host, port, &hints, &result);
if (s == 0) {
@@ -343,6 +345,11 @@ create_and_bind(const char *host, const char *port, int mptcp)
return -1;
}
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
rp = result;
/*
@@ -410,11 +417,7 @@ create_and_bind(const char *host, const char *port, int mptcp)
}
close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
}
freeaddrinfo(result);
+8 -5
View File
@@ -121,12 +121,19 @@ create_and_bind(const char *addr, const char *port)
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s));
return -1;
}
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) {
@@ -154,11 +161,7 @@ create_and_bind(const char *addr, const char *port)
}
close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
}
freeaddrinfo(result);
+4 -3
View File
@@ -389,13 +389,13 @@ create_server_socket(const char *host, const char *port)
return -1;
}
rp = result;
if (rp == NULL) {
if (result == NULL) {
LOGE("[udp] cannot bind");
return -1;
}
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
@@ -462,6 +462,7 @@ create_server_socket(const char *host, const char *port)
}
close(server_sock);
server_sock = -1;
}
freeaddrinfo(result);