Add utility function for loading certificates from a buffer
[folly.git] / folly / IPAddressV6.cpp
index e77ea540b8e298fa941c0ef4b5f932861d4024f6..be909315f147b2c35ecb970062f4d4bfd179e25e 100644 (file)
@@ -31,8 +31,8 @@
 // Because of the massive pain that is libnl, this can't go into the socket
 // portability header as you can't include <linux/if.h> and <net/if.h> in
 // the same translation unit without getting errors -_-...
-#include <iphlpapi.h>
-#include <ntddndis.h>
+#include <iphlpapi.h> // @manual
+#include <ntddndis.h> // @manual
 
 // Alias the max size of an interface name to what posix expects.
 #define IFNAMSIZ IF_NAMESIZE
@@ -77,8 +77,7 @@ bool IPAddressV6::validate(StringPiece ip) {
 }
 
 // public default constructor
-IPAddressV6::IPAddressV6() {
-}
+IPAddressV6::IPAddressV6() {}
 
 // public string constructor
 IPAddressV6::IPAddressV6(StringPiece addr) {
@@ -110,28 +109,17 @@ IPAddressV6::IPAddressV6(StringPiece addr) {
 }
 
 // in6_addr constructor
-IPAddressV6::IPAddressV6(const in6_addr& src)
-  : addr_(src)
-{
-}
+IPAddressV6::IPAddressV6(const in6_addr& src) : addr_(src) {}
 
 // sockaddr_in6 constructor
 IPAddressV6::IPAddressV6(const sockaddr_in6& src)
-  : addr_(src.sin6_addr)
-  , scope_(uint16_t(src.sin6_scope_id))
-{
-}
+    : addr_(src.sin6_addr), scope_(uint16_t(src.sin6_scope_id)) {}
 
 // ByteArray16 constructor
-IPAddressV6::IPAddressV6(const ByteArray16& src)
-  : addr_(src)
-{
-}
+IPAddressV6::IPAddressV6(const ByteArray16& src) : addr_(src) {}
 
 // link-local constructor
-IPAddressV6::IPAddressV6(LinkLocalTag, MacAddress mac)
-  : addr_(mac) {
-}
+IPAddressV6::IPAddressV6(LinkLocalTag, MacAddress mac) : addr_(mac) {}
 
 IPAddressV6::AddressStorage::AddressStorage(MacAddress mac) {
   // The link-local address uses modified EUI-64 format,
@@ -150,20 +138,23 @@ IPAddressV6::AddressStorage::AddressStorage(MacAddress mac) {
 
 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)) {
+  if (!isLinkLocal()) {
     return folly::none;
   }
-  // The link-local address uses modified EUI-64 format,
+  return getMacAddressFromEUI64();
+}
+
+Optional<MacAddress> IPAddressV6::getMacAddressFromEUI64() const {
+  if (!(addr_.bytes_[11] == 0xff && addr_.bytes_[12] == 0xfe)) {
+    return folly::none;
+  }
+  // The auto configured 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 1: first 8 bytes are network prefix, 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
+  // Step 3: copy these bytes as they are
   bytes[1] = addr_.bytes_[9];
   bytes[2] = addr_.bytes_[10];
   // Step 4: strip bytes (0xfffe), which are bytes_[11] and bytes_[12]
@@ -228,9 +219,8 @@ static inline uint16_t unpack(uint8_t lobyte, uint8_t hibyte) {
 
 // given a src string, unpack count*2 bytes into dest
 // dest must have as much storage as count
-static inline void unpackInto(const unsigned char* src,
-                              uint16_t* dest,
-                              size_t count) {
+static inline void
+unpackInto(const unsigned char* src, uint16_t* dest, size_t count) {
   for (size_t i = 0, hi = 1, lo = 0; i < count; i++) {
     dest[i] = unpack(src[hi], src[lo]);
     hi += 2;
@@ -245,7 +235,7 @@ IPAddressV4 IPAddressV6::getIPv4For6To4() const {
         sformat("Invalid IP '{}': not a 6to4 address", str()));
   }
   // convert 16x8 bytes into first 4x16 bytes
-  uint16_t ints[4] = {0,0,0,0};
+  uint16_t ints[4] = {0, 0, 0, 0};
   unpackInto(bytes(), ints, 4);
   // repack into 4x8
   union {
@@ -281,7 +271,7 @@ bool IPAddressV6::isIPv4Mapped() const {
 // public
 IPAddressV6::Type IPAddressV6::type() const {
   // convert 16x8 bytes into first 2x16 bytes
-  uint16_t ints[2] = {0,0};
+  uint16_t ints[2] = {0, 0};
   unpackInto(bytes(), ints, 2);
 
   if ((((uint32_t)ints[0] << 16) | ints[1]) == IPAddressV6::PREFIX_TEREDO) {
@@ -327,11 +317,11 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const {
 }
 
 // public
-bool IPAddressV6::inSubnetWithMask(const IPAddressV6& subnet,
-                                   const ByteArray16& cidrMask) const {
-  const ByteArray16 mask = detail::Bytes::mask(toByteArray(), cidrMask);
-  const ByteArray16 subMask = detail::Bytes::mask(subnet.toByteArray(),
-                                                  cidrMask);
+bool IPAddressV6::inSubnetWithMask(
+    const IPAddressV6& subnet,
+    const ByteArray16& cidrMask) const {
+  const auto mask = detail::Bytes::mask(toByteArray(), cidrMask);
+  const auto subMask = detail::Bytes::mask(subnet.toByteArray(), cidrMask);
   return (mask == subMask);
 }
 
@@ -347,11 +337,11 @@ bool IPAddressV6::isLoopback() const {
 
 bool IPAddressV6::isRoutable() const {
   return
-    // 2000::/3 is the only assigned global unicast block
-    inBinarySubnet({{0x20, 0x00}}, 3) ||
-    // ffxe::/16 are global scope multicast addresses,
-    // which are eligible to be routed over the internet
-    (isMulticast() && getMulticastScope() == 0xe);
+      // 2000::/3 is the only assigned global unicast block
+      inBinarySubnet({{0x20, 0x00}}, 3) ||
+      // ffxe::/16 are global scope multicast addresses,
+      // which are eligible to be routed over the internet
+      (isMulticast() && getMulticastScope() == 0xe);
 }
 
 bool IPAddressV6::isLinkLocalBroadcast() const {
@@ -392,11 +382,24 @@ IPAddressV6 IPAddressV6::getSolicitedNodeAddress() const {
   // addresses
   DCHECK(!isMulticast());
 
-  uint8_t bytes[16] = { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                        0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00 };
-  bytes[13] = addr_.bytes_[13];
-  bytes[14] = addr_.bytes_[14];
-  bytes[15] = addr_.bytes_[15];
+  uint8_t bytes[16] = {
+      0xff,
+      0x02,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x00,
+      0x01,
+      0xff,
+      addr_.bytes_[13],
+      addr_.bytes_[14],
+      addr_.bytes_[15],
+  };
   return IPAddressV6::fromBinary(ByteRange(bytes, 16));
 }
 
@@ -504,8 +507,9 @@ CIDRNetworkV6 IPAddressV6::longestCommonPrefix(
 }
 
 // protected
-bool IPAddressV6::inBinarySubnet(const std::array<uint8_t, 2> addr,
-                                 size_t numBits) const {
+bool IPAddressV6::inBinarySubnet(
+    const std::array<uint8_t, 2> addr,
+    size_t numBits) const {
   auto masked = mask(numBits);
   return (std::memcmp(addr.data(), masked.bytes(), 2) == 0);
 }