Browse Source

Refine port binding

pull/1379/head
Max Lv 7 years ago
parent
commit
1af0272702
5 changed files with 36 additions and 23 deletions
  1. 13
      src/local.c
  2. 13
      src/redir.c
  3. 13
      src/server.c
  4. 13
      src/tunnel.c
  5. 7
      src/udprelay.c

13
src/local.c

@ -156,13 +156,20 @@ create_and_bind(const char *addr, const char *port)
memset(&hints, 0, sizeof(struct addrinfo)); memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */ hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result); s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) { if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s)); LOGI("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
} }
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) { for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) { if (listen_sock == -1) {
@ -190,11 +197,7 @@ create_and_bind(const char *addr, const char *port)
} }
close(listen_sock); close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
} }
freeaddrinfo(result); freeaddrinfo(result);

13
src/redir.c

@ -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_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result); s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) { if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s)); LOGI("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
} }
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) { for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) { if (listen_sock == -1) {
@ -173,11 +180,7 @@ create_and_bind(const char *addr, const char *port)
} }
close(listen_sock); close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
} }
freeaddrinfo(result); freeaddrinfo(result);

13
src/server.c

@ -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_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
result = NULL;
for (int i = 1; i < 8; i++) { for (int i = 1; i < 8; i++) {
s = getaddrinfo(host, port, &hints, &result); s = getaddrinfo(host, port, &hints, &result);
if (s == 0) { if (s == 0) {
@ -343,6 +345,11 @@ create_and_bind(const char *host, const char *port, int mptcp)
return -1; return -1;
} }
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
rp = result; rp = result;
/* /*
@ -410,11 +417,7 @@ create_and_bind(const char *host, const char *port, int mptcp)
} }
close(listen_sock); close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
} }
freeaddrinfo(result); freeaddrinfo(result);

13
src/tunnel.c

@ -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_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */ hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
result = NULL;
s = getaddrinfo(addr, port, &hints, &result); s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) { if (s != 0) {
LOGI("getaddrinfo: %s", gai_strerror(s)); LOGI("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
} }
if (result == NULL) {
LOGE("Could not bind");
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) { for (rp = result; rp != NULL; rp = rp->ai_next) {
listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (listen_sock == -1) { if (listen_sock == -1) {
@ -154,11 +161,7 @@ create_and_bind(const char *addr, const char *port)
} }
close(listen_sock); close(listen_sock);
}
if (rp == NULL) {
LOGE("Could not bind");
return -1;
listen_sock = -1;
} }
freeaddrinfo(result); freeaddrinfo(result);

7
src/udprelay.c

@ -389,13 +389,13 @@ create_server_socket(const char *host, const char *port)
return -1; return -1;
} }
rp = result;
if (rp == NULL) {
if (result == NULL) {
LOGE("[udp] cannot bind"); LOGE("[udp] cannot bind");
return -1; return -1;
} }
rp = result;
/* /*
* On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with * 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 * 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); close(server_sock);
server_sock = -1;
} }
freeaddrinfo(result); freeaddrinfo(result);

Loading…
Cancel
Save