/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
result->append(addr.str());
}
+bool IPAddressV6::validate(StringPiece ip) {
+ if (ip.size() > 0 && ip.front() == '[' && ip.back() == ']') {
+ ip = ip.subpiece(1, ip.size() - 2);
+ }
+
+ constexpr size_t kStrMaxLen = INET6_ADDRSTRLEN;
+ std::array<char, kStrMaxLen + 1> ip_cstr;
+ const size_t len = std::min(ip.size(), kStrMaxLen);
+ std::memcpy(ip_cstr.data(), ip.data(), len);
+ ip_cstr[len] = 0;
+ struct in6_addr addr;
+ return 1 == inet_pton(AF_INET6, ip_cstr.data(), &addr);
+}
+
// public default constructor
IPAddressV6::IPAddressV6() {
}
buffer, INET6_ADDRSTRLEN,
nullptr, 0, NI_NUMERICHOST)) {
string ip(buffer);
- return std::move(ip);
+ return ip;
} else {
throw IPAddressFormatException("Invalid address with hex ",
"'", detail::Bytes::toHex(bytes(), 16), "'");
// protected
bool IPAddressV6::inBinarySubnet(const std::array<uint8_t, 2> addr,
size_t numBits) const {
- const unsigned char* subbytes = mask(numBits).bytes();
- return (std::memcmp(addr.data(), subbytes, 2) == 0);
+ auto masked = mask(numBits);
+ return (std::memcmp(addr.data(), masked.bytes(), 2) == 0);
}
// static private