Browse Source

Refine ipv6only handling

pull/2316/head
Max Lv 5 years ago
parent
commit
3fa9e57248
6 changed files with 27 additions and 15 deletions
  1. 8
      src/local.c
  2. 14
      src/netutils.c
  3. 2
      src/netutils.h
  4. 8
      src/redir.c
  5. 2
      src/server.c
  6. 8
      src/tunnel.c

8
src/local.c

@ -1708,7 +1708,7 @@ main(int argc, char **argv)
FATAL("failed to find a free port"); FATAL("failed to find a free port");
} }
snprintf(tmp_port, 8, "%d", port); snprintf(tmp_port, 8, "%d", port);
if (is_ipv6only(remote_addr, remote_num)) {
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
plugin_host = "::1"; plugin_host = "::1";
} else { } else {
plugin_host = "127.0.0.1"; plugin_host = "127.0.0.1";
@ -1748,7 +1748,11 @@ main(int argc, char **argv)
#endif #endif
if (local_addr == NULL) { if (local_addr == NULL) {
local_addr = "127.0.0.1";
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
local_addr = "::1";
} else {
local_addr = "127.0.0.1";
}
} }
USE_SYSLOG(argv[0], pid_flags); USE_SYSLOG(argv[0], pid_flags);

14
src/netutils.c

@ -285,17 +285,17 @@ validate_hostname(const char *hostname, const int hostname_len)
} }
int int
is_ipv6only(ss_addr_t *servers, size_t server_num)
is_ipv6only(ss_addr_t *servers, size_t server_num, int ipv6first)
{ {
struct cork_ip ip;
int i; int i;
for (i = 0; i < server_num; i++) for (i = 0; i < server_num; i++)
{ {
if (cork_ip_init(&ip, servers[i].host) != -1) {
if (ip.version != 6) {
return 0;
}
} else {
struct sockaddr_storage storage;
memset(&storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(servers[i].host, servers[i].port, &storage, 1, ipv6first) == -1) {
FATAL("failed to resolve the provided hostname");
}
if (storage.ss_family != AF_INET6) {
return 0; return 0;
} }
} }

2
src/netutils.h

@ -108,6 +108,6 @@ int sockaddr_cmp_addr(struct sockaddr_storage *addr1,
int validate_hostname(const char *hostname, const int hostname_len); int validate_hostname(const char *hostname, const int hostname_len);
int is_ipv6only(ss_addr_t *servers, size_t server_num);
int is_ipv6only(ss_addr_t *servers, size_t server_num, int ipv6first);
#endif #endif

8
src/redir.c

@ -1067,7 +1067,7 @@ main(int argc, char **argv)
FATAL("failed to find a free port"); FATAL("failed to find a free port");
} }
snprintf(tmp_port, 8, "%d", port); snprintf(tmp_port, 8, "%d", port);
if (is_ipv6only(remote_addr, remote_num)) {
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
plugin_host = "::1"; plugin_host = "::1";
} else { } else {
plugin_host = "127.0.0.1"; plugin_host = "127.0.0.1";
@ -1099,7 +1099,11 @@ main(int argc, char **argv)
#endif #endif
if (local_addr == NULL) { if (local_addr == NULL) {
local_addr = "127.0.0.1";
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
local_addr = "::1";
} else {
local_addr = "127.0.0.1";
}
} }
if (fast_open == 1) { if (fast_open == 1) {

2
src/server.c

@ -1835,7 +1835,7 @@ main(int argc, char **argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (is_ipv6only(server_addr, server_num)) {
if (is_ipv6only(server_addr, server_num, ipv6first)) {
plugin_host = "::1"; plugin_host = "::1";
} else { } else {
plugin_host = "127.0.0.1"; plugin_host = "127.0.0.1";

8
src/tunnel.c

@ -1116,7 +1116,7 @@ main(int argc, char **argv)
FATAL("failed to find a free port"); FATAL("failed to find a free port");
} }
snprintf(tmp_port, 8, "%d", port); snprintf(tmp_port, 8, "%d", port);
if (is_ipv6only(remote_addr, remote_num)) {
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
plugin_host = "::1"; plugin_host = "::1";
} else { } else {
plugin_host = "127.0.0.1"; plugin_host = "127.0.0.1";
@ -1156,7 +1156,11 @@ main(int argc, char **argv)
#endif #endif
if (local_addr == NULL) { if (local_addr == NULL) {
local_addr = "127.0.0.1";
if (is_ipv6only(remote_addr, remote_num, ipv6first)) {
local_addr = "::1";
} else {
local_addr = "127.0.0.1";
}
} }
if (fast_open == 1) { if (fast_open == 1) {

Loading…
Cancel
Save