From 1ba753adaad5454eabf2315fc3a40b82ea732bfa Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sat, 9 Nov 2019 07:08:59 +0800 Subject: [PATCH] Fix #2536 --- config.json | 12 ------------ src/netutils.c | 6 +++--- src/server.c | 28 ++++++++++++++++------------ src/udprelay.c | 22 +++++++++++++--------- 4 files changed, 32 insertions(+), 36 deletions(-) delete mode 100644 config.json diff --git a/config.json b/config.json deleted file mode 100644 index b30ad929..00000000 --- a/config.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "server":"la.maxlv.net", - "server_port":1080, - "local_address":"0.0.0.0", - "local_port":1080, - "password":"CCCCCCCC", - "method":"aes-256-gcm", - "timeout":60, - "mode":"tcp_and_udp", - "plugin":"obfs-local", - "plugin_opts":"obfs=http;obfs-host=www.CCCCCCC.com" -} diff --git a/src/netutils.c b/src/netutils.c index 3fc0b229..8c7329a9 100644 --- a/src/netutils.c +++ b/src/netutils.c @@ -99,18 +99,18 @@ parse_local_addr(struct sockaddr_storage *storage_v4, inet_pton(AF_INET, host, &addr->sin_addr); addr->sin_family = AF_INET; LOGI("binding to outbound IPv4 addr: %s", host); - return 0; + return AF_INET; } else if (ip.version == 6) { memset(storage_v6, 0, sizeof(struct sockaddr_storage)); struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage_v6; inet_pton(AF_INET6, host, &addr->sin6_addr); addr->sin6_family = AF_INET6; LOGI("binding to outbound IPv6 addr: %s", host); - return 0; + return AF_INET6; } } } - return -1; + return 0; } int diff --git a/src/server.c b/src/server.c index a6867434..faea13e3 100644 --- a/src/server.c +++ b/src/server.c @@ -478,10 +478,12 @@ connect_to_remote(EV_P_ struct addrinfo *res, if (is_bind_local_addr) { struct sockaddr_storage *local_addr = res->ai_family == AF_INET ? &local_addr_v4 : &local_addr_v6; - if (bind_to_addr(local_addr, sockfd) == -1) { - ERROR("bind_to_addr"); - close(sockfd); - return NULL; + if (res->ai_family == local_addr->ss_family) { + if (bind_to_addr(local_addr, sockfd) == -1) { + ERROR("bind_to_addr"); + FATAL("cannot bind socket"); + return NULL; + } } } @@ -1568,6 +1570,8 @@ main(int argc, char **argv) int server_num = 0; ss_addr_t server_addr[MAX_REMOTE_NUM]; memset(server_addr, 0, sizeof(ss_addr_t) * MAX_REMOTE_NUM); + memset(&local_addr_v4, 0, sizeof(struct sockaddr_storage)); + memset(&local_addr_v6, 0, sizeof(struct sockaddr_storage)); static struct option long_options[] = { { "fast-open", no_argument, NULL, GETOPT_VAL_FAST_OPEN }, @@ -1635,8 +1639,7 @@ main(int argc, char **argv) } break; case 'b': - if (parse_local_addr(&local_addr_v4, &local_addr_v6, optarg) == 0) - is_bind_local_addr = 1; + is_bind_local_addr += parse_local_addr(&local_addr_v4, &local_addr_v6, optarg); break; case 'p': server_port = optarg; @@ -1760,12 +1763,13 @@ main(int argc, char **argv) fast_open = conf->fast_open; } if (is_bind_local_addr == 0) { - if (parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr) == 0) - is_bind_local_addr = 1; - if (parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr_v4) == 0) - is_bind_local_addr = 1; - if (parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr_v6) == 0) - is_bind_local_addr = 1; + is_bind_local_addr += parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr); + } + if (is_bind_local_addr == 0) { + is_bind_local_addr += parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr_v4); + } + if (is_bind_local_addr == 0) { + is_bind_local_addr += parse_local_addr(&local_addr_v4, &local_addr_v6, conf->local_addr_v6); } #ifdef HAVE_SETRLIMIT if (nofile == 0) { diff --git a/src/udprelay.c b/src/udprelay.c index cf3cb9d5..4eec87f6 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -377,15 +377,17 @@ create_remote_socket(int ipv6) } #ifdef MODULE_REMOTE if (is_bind_local_addr) { - if (bind_to_addr(&local_addr_v6, remote_sock) == -1) { - ERROR("bind_to_addr"); - FATAL("[udp] cannot bind remote"); - return -1; + if (local_addr_v6.ss_family == AF_INET6) { + if (bind_to_addr(&local_addr_v6, remote_sock) == -1) { + ERROR("bind_to_addr"); + FATAL("[udp] cannot bind socket"); + return -1; + } } } else { #endif if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) { - FATAL("[udp] cannot bind remote"); + FATAL("[udp] cannot bind socket"); return -1; } #ifdef MODULE_REMOTE @@ -405,10 +407,12 @@ create_remote_socket(int ipv6) } #ifdef MODULE_REMOTE if (is_bind_local_addr) { - if (bind_to_addr(&local_addr_v4, remote_sock) == -1) { - ERROR("bind_to_addr"); - FATAL("[udp] cannot bind remote"); - return -1; + if (local_addr_v4.ss_family == AF_INET) { + if (bind_to_addr(&local_addr_v4, remote_sock) == -1) { + ERROR("bind_to_addr"); + FATAL("[udp] cannot bind socket"); + return -1; + } } } else { #endif