X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FIPAddress.cpp;h=0a92d3cbaa6260e5bc7caa2647f49de69608e6ec;hb=e5e3fde0cc5e35b87bec8714b0a7e6987a3d3975;hp=b49b0dc86f370eb08c0845d3366b602202181b10;hpb=7ccfefc766cc09064823ae19e886e17188d44a46;p=folly.git diff --git a/folly/IPAddress.cpp b/folly/IPAddress.cpp index b49b0dc8..0a92d3cb 100644 --- a/folly/IPAddress.cpp +++ b/folly/IPAddress.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "IPAddress.h" +#include #include #include @@ -146,12 +146,20 @@ IPAddress::IPAddress(StringPiece addr) // need to check for V4 address second, since IPv4-mapped IPv6 addresses may // contain a period if (ip.find(':') != string::npos) { - in6_addr ipAddr; - if (inet_pton(AF_INET6, ip.c_str(), &ipAddr) != 1) { - throwFormatException("inet_pton failed for V6 address"); + struct addrinfo* result; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_NUMERICHOST; + if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) { + struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr; + addr_ = IPAddressV46(IPAddressV6(*ipAddr)); + family_ = AF_INET6; + freeaddrinfo(result); + } else { + throwFormatException("getsockaddr failed for V6 address"); } - addr_ = IPAddressV46(IPAddressV6(ipAddr)); - family_ = AF_INET6; } else if (ip.find('.') != string::npos) { in_addr ipAddr; if (inet_pton(AF_INET, ip.c_str(), &ipAddr) != 1) { @@ -181,7 +189,7 @@ IPAddress::IPAddress(const sockaddr* addr) } case AF_INET6: { const sockaddr_in6 *v6addr = reinterpret_cast(addr); - addr_.ipV6Addr = IPAddressV6(v6addr->sin6_addr); + addr_.ipV6Addr = IPAddressV6(*v6addr); break; } default: