diff --git a/lib/network.cpp b/lib/network.cpp index 10849fde57..0f2696b68d 100644 --- a/lib/network.cpp +++ b/lib/network.cpp @@ -146,8 +146,12 @@ int resolve_hostname(const char* hostname, sockaddr_storage &ip_addr) { if (!hep) { return ERR_GETHOSTBYNAME; } - ip_addr.sin_family = AF_INET; - ip_addr.sin_addr.s_addr = *(int*)hep->h_addr_list[0]; + for (int i=0; ; i++) { + if (!hep->h_addr_list[i]) break; + ip_addr.sin_family = AF_INET; + ip_addr.sin_addr.s_addr = *(int*)hep->h_addr_list[i]; + if ((ip_addr.sin_addr.s_addr&0xff) != 0x7f) return 0; // look for non-loopback addr + } return 0; #else @@ -162,7 +166,11 @@ int resolve_hostname(const char* hostname, sockaddr_storage &ip_addr) { perror("getaddrinfo"); return retval; } - memcpy(&ip_addr, res->ai_addr, res->ai_addrlen); + while (1) { + memcpy(&ip_addr, res->ai_addr, res->ai_addrlen); + if ((ip_addr.sin_addr.s_addr&0xff) != 0x7f) break; + res = res->ai_next; + } freeaddrinfo(res); return 0; #endif