Added folly::hint_emplace_iterator to folly/Iterator.h
[folly.git] / folly / IPAddressV6.cpp
index 849c6d63be6fe57eda3922dbd9ce2c6dd977aadc..6f1eb5958996c5f12bb16204a6c2a3398ddf26de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "IPAddressV6.h"
+#include <folly/IPAddressV6.h>
 
 #include <ostream>
 #include <string>
@@ -23,6 +23,7 @@
 #include <folly/IPAddress.h>
 #include <folly/IPAddressV4.h>
 #include <folly/MacAddress.h>
+#include <folly/detail/IPAddressSource.h>
 
 using std::ostream;
 using std::string;
@@ -48,6 +49,20 @@ void toAppend(IPAddressV6 addr, fbstring* result) {
   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() {
 }
@@ -58,8 +73,8 @@ IPAddressV6::IPAddressV6(StringPiece addr) {
 
   // Allow addresses surrounded in brackets
   if (ip.size() < 2) {
-    throw IPAddressFormatException("Invalid IPv6 address '", ip,
-                                   "': address too short");
+    throw IPAddressFormatException(
+        to<std::string>("Invalid IPv6 address '", ip, "': address too short"));
   }
   if (ip.front() == '[' && ip.back() == ']') {
     ip = ip.substr(1, ip.size() - 2);
@@ -74,10 +89,11 @@ IPAddressV6::IPAddressV6(StringPiece addr) {
   if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) {
     struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr;
     addr_.in6Addr_ = ipAddr->sin6_addr;
-    scope_ = ipAddr->sin6_scope_id;
+    scope_ = uint16_t(ipAddr->sin6_scope_id);
     freeaddrinfo(result);
   } else {
-    throw IPAddressFormatException("Invalid IPv6 address '", ip, "'");
+    throw IPAddressFormatException(
+        to<std::string>("Invalid IPv6 address '", ip, "'"));
   }
 }
 
@@ -90,7 +106,7 @@ IPAddressV6::IPAddressV6(const in6_addr& src)
 // sockaddr_in6 constructor
 IPAddressV6::IPAddressV6(const sockaddr_in6& src)
   : addr_(src.sin6_addr)
-  , scope_(src.sin6_scope_id)
+  , scope_(uint16_t(src.sin6_scope_id))
 {
 }
 
@@ -110,7 +126,7 @@ IPAddressV6::AddressStorage::AddressStorage(MacAddress mac) {
   // See RFC 4291 sections 2.5.1, 2.5.6, and Appendix A
   const auto* macBytes = mac.bytes();
   memcpy(&bytes_.front(), "\xfe\x80\x00\x00\x00\x00\x00\x00", 8);
-  bytes_[8] = macBytes[0] ^ 0x02;
+  bytes_[8] = uint8_t(macBytes[0] ^ 0x02);
   bytes_[9] = macBytes[1];
   bytes_[10] = macBytes[2];
   bytes_[11] = 0xff;
@@ -120,15 +136,71 @@ IPAddressV6::AddressStorage::AddressStorage(MacAddress mac) {
   bytes_[15] = macBytes[5];
 }
 
+Optional<MacAddress> IPAddressV6::getMacAddressFromLinkLocal() const {
+  // Returned MacAddress must be constructed from a link-local IPv6 address.
+  if (!(addr_.bytes_[0] == 0xfe && addr_.bytes_[1] == 0x80 &&
+        addr_.bytes_[2] == 0x00 && addr_.bytes_[3] == 0x00 &&
+        addr_.bytes_[4] == 0x00 && addr_.bytes_[5] == 0x00 &&
+        addr_.bytes_[6] == 0x00 && addr_.bytes_[7] == 0x00 &&
+        addr_.bytes_[11] == 0xff && addr_.bytes_[12] == 0xfe)) {
+    return folly::none;
+  }
+  // The link-local address uses modified EUI-64 format,
+  // See RFC 4291 sections 2.5.1, 2.5.6, and Appendix A
+  std::array<uint8_t, MacAddress::SIZE> bytes;
+  // Step 1: first 8 bytes are fe:80:00:00:00:00:00:00, and can be stripped
+  // Step 2: invert the universal/local (U/L) flag (bit 7)
+  bytes[0] = addr_.bytes_[8] ^ 0x02;
+  // Step 3: copy thhese bytes are they are
+  bytes[1] = addr_.bytes_[9];
+  bytes[2] = addr_.bytes_[10];
+  // Step 4: strip bytes (0xfffe), which are bytes_[11] and bytes_[12]
+  // Step 5: copy the rest.
+  bytes[3] = addr_.bytes_[13];
+  bytes[4] = addr_.bytes_[14];
+  bytes[5] = addr_.bytes_[15];
+  return Optional<MacAddress>(MacAddress::fromBinary(range(bytes)));
+}
+
 void IPAddressV6::setFromBinary(ByteRange bytes) {
   if (bytes.size() != 16) {
-    throw IPAddressFormatException("Invalid IPv6 binary data: length must "
-                                   "be 16 bytes, got ", bytes.size());
+    throw IPAddressFormatException(to<std::string>(
+        "Invalid IPv6 binary data: length must ",
+        "be 16 bytes, got ",
+        bytes.size()));
   }
   memcpy(&addr_.in6Addr_.s6_addr, bytes.data(), sizeof(in6_addr));
   scope_ = 0;
 }
 
+// static
+IPAddressV6 IPAddressV6::fromInverseArpaName(const std::string& arpaname) {
+  auto piece = StringPiece(arpaname);
+  if (!piece.removeSuffix(".ip6.arpa")) {
+    throw IPAddressFormatException(sformat(
+        "Invalid input. Should end with 'ip6.arpa'. Got '{}'", arpaname));
+  }
+  std::vector<StringPiece> pieces;
+  split(".", piece, pieces);
+  if (pieces.size() != 32) {
+    throw IPAddressFormatException(sformat("Invalid input. Got '{}'", piece));
+  }
+  std::array<char, IPAddressV6::kToFullyQualifiedSize> ip;
+  size_t pos = 0;
+  int count = 0;
+  for (size_t i = 1; i <= pieces.size(); i++) {
+    ip[pos] = pieces[pieces.size() - i][0];
+    pos++;
+    count++;
+    // add ':' every 4 chars
+    if (count == 4 && pos < ip.size()) {
+      ip[pos++] = ':';
+      count = 0;
+    }
+  }
+  return IPAddressV6(folly::range(ip));
+}
+
 // public
 IPAddressV4 IPAddressV6::createIPv4() const {
   if (!isIPv4Mapped()) {
@@ -140,7 +212,7 @@ IPAddressV4 IPAddressV6::createIPv4() const {
 
 // convert two uint8_t bytes into a uint16_t as hibyte.lobyte
 static inline uint16_t unpack(uint8_t lobyte, uint8_t hibyte) {
-  return ((uint16_t)hibyte << 8) | (uint16_t)lobyte;
+  return uint16_t((uint16_t(hibyte) << 8) | lobyte);
 }
 
 // given a src string, unpack count*2 bytes into dest
@@ -148,7 +220,7 @@ static inline uint16_t unpack(uint8_t lobyte, uint8_t hibyte) {
 static inline void unpackInto(const unsigned char* src,
                               uint16_t* dest,
                               size_t count) {
-  for (int i = 0, hi = 1, lo = 0; i < count; i++) {
+  for (size_t i = 0, hi = 1, lo = 0; i < count; i++) {
     dest[i] = unpack(src[hi], src[lo]);
     hi += 2;
     lo += 2;
@@ -238,8 +310,8 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const {
   auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
   auto addr = subnetInfo.first;
   if (!addr.isV6()) {
-    throw IPAddressFormatException("Address '", addr.toJson(), "' ",
-                                   "is not a V6 address");
+    throw IPAddressFormatException(to<std::string>(
+        "Address '", addr.toJson(), "' ", "is not a V6 address"));
   }
   return inSubnetWithMask(addr.asV6(), fetchMask(subnetInfo.second));
 }
@@ -255,13 +327,12 @@ bool IPAddressV6::inSubnetWithMask(const IPAddressV6& subnet,
 
 // public
 bool IPAddressV6::isLoopback() const {
-  const unsigned char* by = bytes();
-  for (int i = 0; i < 15; i++) {
-    if (by[i] != 0x00) {
-      return false;
-    }
+  // Check if v4 mapped is loopback
+  if (isIPv4Mapped() && createIPv4().isLoopback()) {
+    return true;
   }
-  return (by[15] == 0x01);
+  auto socka = toSockAddr();
+  return IN6_IS_ADDR_LOOPBACK(&socka.sin6_addr);
 }
 
 bool IPAddressV6::isRoutable() const {
@@ -280,9 +351,14 @@ bool IPAddressV6::isLinkLocalBroadcast() const {
 
 // public
 bool IPAddressV6::isPrivate() const {
+  // Check if mapped is private
+  if (isIPv4Mapped() && createIPv4().isPrivate()) {
+    return true;
+  }
   return isLoopback() || inBinarySubnet({{0xfc, 0x00}}, 7);
 }
 
+// public
 bool IPAddressV6::isLinkLocal() const {
   return inBinarySubnet({{0xfe, 0x80}}, 10);
 }
@@ -293,12 +369,12 @@ bool IPAddressV6::isMulticast() const {
 
 uint8_t IPAddressV6::getMulticastFlags() const {
   DCHECK(isMulticast());
-  return ((addr_.bytes_[1] >> 4) & 0xf);
+  return uint8_t((addr_.bytes_[1] >> 4) & 0xf);
 }
 
 uint8_t IPAddressV6::getMulticastScope() const {
   DCHECK(isMulticast());
-  return (addr_.bytes_[1] & 0xf);
+  return uint8_t(addr_.bytes_[1] & 0xf);
 }
 
 IPAddressV6 IPAddressV6::getSolicitedNodeAddress() const {
@@ -318,8 +394,8 @@ IPAddressV6 IPAddressV6::getSolicitedNodeAddress() const {
 IPAddressV6 IPAddressV6::mask(size_t numBits) const {
   static const auto bits = bitCount();
   if (numBits > bits) {
-    throw IPAddressFormatException("numBits(", numBits, ") > bitCount(",
-                                   bits, ")");
+    throw IPAddressFormatException(
+        to<std::string>("numBits(", numBits, ") > bitCount(", bits, ")"));
   }
   ByteArray16 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
   return IPAddressV6(ba);
@@ -334,10 +410,13 @@ string IPAddressV6::str() const {
         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), "'");
+    throw IPAddressFormatException(to<std::string>(
+        "Invalid address with hex ",
+        "'",
+        detail::Bytes::toHex(bytes(), 16),
+        "'"));
   }
 }
 
@@ -346,6 +425,19 @@ string IPAddressV6::toFullyQualified() const {
   return detail::fastIpv6ToString(addr_.in6Addr_);
 }
 
+// public
+string IPAddressV6::toInverseArpaName() const {
+  constexpr folly::StringPiece lut = "0123456789abcdef";
+  std::array<char, 32> a;
+  int j = 0;
+  for (int i = 15; i >= 0; i--) {
+    a[j] = (lut[bytes()[i] & 0xf]);
+    a[j + 1] = (lut[bytes()[i] >> 4]);
+    j += 2;
+  }
+  return sformat("{}.ip6.arpa", join(".", a));
+}
+
 // public
 uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const {
   const auto highestIndex = byteCount() - 1;
@@ -359,7 +451,7 @@ uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const {
 
 // protected
 const ByteArray16 IPAddressV6::fetchMask(size_t numBits) {
-  static const uint8_t bits = bitCount();
+  static const size_t bits = bitCount();
   if (numBits > bits) {
     throw IPAddressFormatException("IPv6 addresses are 128 bits.");
   }
@@ -367,11 +459,20 @@ const ByteArray16 IPAddressV6::fetchMask(size_t numBits) {
   return masks_[numBits];
 }
 
+// public static
+CIDRNetworkV6 IPAddressV6::longestCommonPrefix(
+    const CIDRNetworkV6& one,
+    const CIDRNetworkV6& two) {
+  auto prefix = detail::Bytes::longestCommonPrefix(
+      one.first.addr_.bytes_, one.second, two.first.addr_.bytes_, two.second);
+  return {IPAddressV6(prefix.first), prefix.second};
+}
+
 // 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