add missing include to ThreadId.h
[folly.git] / folly / IPAddressV6.cpp
index 9a7758c0e095bbbd66723fd7c0e72df83185ec79..6f1eb5958996c5f12bb16204a6c2a3398ddf26de 100644 (file)
@@ -136,6 +136,32 @@ 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(to<std::string>(
@@ -147,6 +173,34 @@ void IPAddressV6::setFromBinary(ByteRange bytes) {
   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()) {
@@ -371,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;