From 5e13d396eee26082c21cff20fe821da4461a6154 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Tue, 7 Mar 2017 12:05:07 +0800 Subject: [PATCH] Refine port binding --- src/local.c | 13 ++++++++----- src/redir.c | 13 ++++++++----- src/server.c | 13 ++++++++----- src/tunnel.c | 13 ++++++++----- src/udprelay.c | 7 ++++--- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/local.c b/src/local.c index 8fbca925..0553e710 100644 --- a/src/local.c +++ b/src/local.c @@ -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); diff --git a/src/redir.c b/src/redir.c index 35b29116..2a97d324 100644 --- a/src/redir.c +++ b/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_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); diff --git a/src/server.c b/src/server.c index 59659a84..23c5bb9d 100644 --- a/src/server.c +++ b/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_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); diff --git a/src/tunnel.c b/src/tunnel.c index 9c792f84..40d8047b 100644 --- a/src/tunnel.c +++ b/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_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); diff --git a/src/udprelay.c b/src/udprelay.c index 781fbc14..3c26eda4 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -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);