/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "IPAddress.h"
+#include <folly/IPAddress.h>
#include <limits>
#include <ostream>
// 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) {
}
case AF_INET6: {
const sockaddr_in6 *v6addr = reinterpret_cast<const sockaddr_in6*>(addr);
- addr_.ipV6Addr = IPAddressV6(v6addr->sin6_addr);
+ addr_.ipV6Addr = IPAddressV6(*v6addr);
break;
}
default:
}
}
// addr1 is v4 mapped v6 address, addr2 is v4
- if (addr1.isIPv4Mapped()) {
+ if (addr1.isIPv4Mapped() && addr2.isV4()) {
if (IPAddress::createIPv4(addr1) == addr2.asV4()) {
return true;
}
}
// addr2 is v4 mapped v6 address, addr1 is v4
- if (addr2.isIPv4Mapped()) {
+ if (addr2.isIPv4Mapped() && addr1.isV4()) {
if (IPAddress::createIPv4(addr2) == addr1.asV4()) {
return true;
}