Make Range.h and FBString.h mutually independent
[folly.git] / folly / IPAddressV6.h
index d2fd02a91b891be5b500fce4dfd767a8927f5e24..f641bdcd85e988fa8562a0f986cb0aa467597f2a 100644 (file)
@@ -24,6 +24,7 @@
 #include <map>
 #include <stdexcept>
 
+#include <folly/FBString.h>
 #include <folly/Hash.h>
 #include <folly/Optional.h>
 #include <folly/Range.h>
@@ -69,7 +70,9 @@ class IPAddressV6 {
  public:
   // V6 Address Type
   enum Type {
-    TEREDO, T6TO4, NORMAL,
+    TEREDO,
+    T6TO4,
+    NORMAL,
   };
   // A constructor parameter to indicate that we should create a link-local
   // IPAddressV6.
@@ -86,7 +89,7 @@ class IPAddressV6 {
 
   // Size of std::string returned by toFullyQualified.
   static constexpr size_t kToFullyQualifiedSize =
-    8 /*words*/ * 4 /*hex chars per word*/ + 7 /*separators*/;
+      8 /*words*/ * 4 /*hex chars per word*/ + 7 /*separators*/;
 
   // returns true iff the input string can be parsed as an ipv6-address
   static bool validate(StringPiece ip);
@@ -112,7 +115,7 @@ class IPAddressV6 {
    * Returns the address as a Range.
    */
   ByteRange toBinary() const {
-    return ByteRange((const unsigned char *) &addr_.in6Addr_.s6_addr, 16);
+    return ByteRange((const unsigned char*)&addr_.in6Addr_.s6_addr, 16);
   }
 
   /**
@@ -171,7 +174,9 @@ class IPAddressV6 {
    * @see IPAddress#bitCount
    * @returns 128
    */
-  static size_t bitCount() { return 128; }
+  static constexpr size_t bitCount() {
+    return 128;
+  }
 
   /**
    * @see IPAddress#toJson
@@ -188,8 +193,8 @@ class IPAddressV6 {
   bool inSubnet(const IPAddressV6& subnet, uint8_t cidr) const {
     return inSubnetWithMask(subnet, fetchMask(cidr));
   }
-  bool inSubnetWithMask(const IPAddressV6& subnet,
-                        const ByteArray16& mask) const;
+  bool inSubnetWithMask(const IPAddressV6& subnet, const ByteArray16& mask)
+      const;
 
   // @see IPAddress#isLoopback
   bool isLoopback() const;
@@ -227,6 +232,16 @@ class IPAddressV6 {
    */
   Optional<MacAddress> getMacAddressFromLinkLocal() const;
 
+  /**
+   * Return the mac address if this is an auto-configured IPv6 address based on
+   * EUI-64
+   *
+   * @return an Optional<MacAddress> union representing the mac address.
+   * If the address is not based on EUI-64 it will return an empty Optional.
+   * You can use Optional::value() to check whether the mac address is not null.
+   */
+  Optional<MacAddress> getMacAddressFromEUI64() const;
+
   /**
    * Return true if this is a multicast address.
    */
@@ -256,9 +271,13 @@ class IPAddressV6 {
   IPAddressV6 mask(size_t numBits) const;
 
   // return underlying in6_addr structure
-  in6_addr toAddr() const { return addr_.in6Addr_; }
+  in6_addr toAddr() const {
+    return addr_.in6Addr_;
+  }
 
-  uint16_t getScopeId() const { return scope_; }
+  uint16_t getScopeId() const {
+    return scope_;
+  }
   void setScopeId(uint16_t scope) {
     scope_ = scope;
   }
@@ -290,7 +309,9 @@ class IPAddressV6 {
   std::string str() const;
 
   // @see IPAddress#version
-  uint8_t version() const { return 6; }
+  uint8_t version() const {
+    return 6;
+  }
 
   /**
    * Return the solicited-node multicast address for this address.
@@ -312,32 +333,35 @@ class IPAddressV6 {
       const CIDRNetworkV6& one,
       const CIDRNetworkV6& two);
   // Number of bytes in the address representation.
-  static constexpr size_t byteCount() { return 16; }
+  static constexpr size_t byteCount() {
+    return 16;
+  }
 
-  //get nth most significant bit - 0 indexed
+  // get nth most significant bit - 0 indexed
   bool getNthMSBit(size_t bitIndex) const {
     return detail::getNthMSBitImpl(*this, bitIndex, AF_INET6);
   }
-  //get nth most significant byte - 0 indexed
+  // get nth most significant byte - 0 indexed
   uint8_t getNthMSByte(size_t byteIndex) const;
-  //get nth bit - 0 indexed
+  // get nth bit - 0 indexed
   bool getNthLSBit(size_t bitIndex) const {
     return getNthMSBit(bitCount() - bitIndex - 1);
   }
-  //get nth byte - 0 indexed
+  // get nth byte - 0 indexed
   uint8_t getNthLSByte(size_t byteIndex) const {
     return getNthMSByte(byteCount() - byteIndex - 1);
   }
 
-  const unsigned char* bytes() const { return addr_.in6Addr_.s6_addr; }
+  const unsigned char* bytes() const {
+    return addr_.in6Addr_.s6_addr;
+  }
 
  protected:
   /**
    * Helper that returns true if the address is in the binary subnet specified
    * by addr.
    */
-  bool inBinarySubnet(const std::array<uint8_t, 2> addr,
-                      size_t numBits) const;
+  bool inBinarySubnet(const std::array<uint8_t, 2> addr, size_t numBits) const;
 
  private:
   auto tie() const {
@@ -371,8 +395,8 @@ class IPAddressV6 {
     AddressStorage() {
       std::memset(this, 0, sizeof(AddressStorage));
     }
-    explicit AddressStorage(const ByteArray16& bytes): bytes_(bytes) {}
-    explicit AddressStorage(const in6_addr& addr): in6Addr_(addr) {}
+    explicit AddressStorage(const ByteArray16& bytes) : bytes_(bytes) {}
+    explicit AddressStorage(const in6_addr& addr) : in6Addr_(addr) {}
     explicit AddressStorage(MacAddress mac);
   } addr_;
 
@@ -395,7 +419,7 @@ std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr);
 void toAppend(IPAddressV6 addr, std::string* result);
 void toAppend(IPAddressV6 addr, fbstring* result);
 
-}  // folly
+} // namespace folly
 
 namespace std {
 template <>
@@ -404,4 +428,4 @@ struct hash<folly::IPAddressV6> {
     return addr.hash();
   }
 };
-}  // std
+} // namespace std