From 4055fb6d77f239f9f14513a8bb3b6d5b7fde4b14 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 2 Apr 2016 14:06:43 -0400 Subject: [PATCH] Always try to resolve a hostname to IPv4 first --- src/Connection.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Connection.c b/src/Connection.c index 9e0ad1f..2140d8d 100644 --- a/src/Connection.c +++ b/src/Connection.c @@ -151,23 +151,34 @@ static void ClInternalConnectionTerminated(long errorCode) PltCloseThread(&terminationCallbackThread); } -static int resolveHostName(const char*host) +static int resolveHostName(const char* host) { struct addrinfo hints, *res; int err; + // We must first try IPv4-only because GFE doesn't listen on IPv6, + // so we'll only want to use an IPv6 address if it's the only address we have. + // For NAT64 networks, the IPv4 address resolution will fail but the IPv6 address + // will give us working connectivity to the host. All other networks will use IPv4 + // addresses. memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = AF_INET; hints.ai_flags = AI_ADDRCONFIG; err = getaddrinfo(host, NULL, &hints, &res); - if (err != 0) { - Limelog("getaddrinfo() failed: %d\n", err); - return err; - } - - if (res == NULL) { - Limelog("getaddrinfo() returned success without addresses\n"); - return -1; + if (err != 0 || res == NULL) { + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_ADDRCONFIG; + err = getaddrinfo(host, NULL, &hints, &res); + if (err != 0) { + Limelog("getaddrinfo() failed: %d\n", err); + return err; + } + + if (res == NULL) { + Limelog("getaddrinfo() returned success without addresses\n"); + return -1; + } } // Use the first address in the list