Fix for building with icc.
[folly.git] / folly / IPAddressV6.h
index be32b4aaaedcb67137593ad45dac018a4c0b719c..c40032028edf8dcd8f6d34877af395e705eaf402 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 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.
 
 #pragma once
 
+#include <cstring>
+
+#include <array>
 #include <functional>
-#include <iostream>
+#include <iosfwd>
 #include <map>
 #include <stdexcept>
 
-#include <boost/operators.hpp>
-
 #include <folly/Hash.h>
+#include <folly/Optional.h>
 #include <folly/Range.h>
 #include <folly/detail/IPAddress.h>
 
@@ -63,7 +65,7 @@ typedef std::array<uint8_t, 16> ByteArray16;
  * Serializing / Deserializing IPAddressB6's on different hosts
  * that use link-local scoping probably won't work.
  */
-class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
+class IPAddressV6 {
  public:
   // V6 Address Type
   enum Type {
@@ -86,6 +88,7 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   static constexpr size_t kToFullyQualifiedSize =
     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);
 
   /**
@@ -98,6 +101,13 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
     return addr;
   }
 
+  /**
+   * Create a new IPAddress instance from the ip6.arpa representation.
+   * @throws IPAddressFormatException if the input is not a valid ip6.arpa
+   * representation
+   */
+  static IPAddressV6 fromInverseArpaName(const std::string& arpaname);
+
   /**
    * Returns the address as a Range.
    */
@@ -207,6 +217,16 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
    */
   bool isLinkLocal() const;
 
+  /**
+   * Return the mac address if this is a link-local IPv6 address.
+   *
+   * @return an Optional<MacAddress> union representing the mac address.
+   *
+   * If the address is not a link-local one it will return an empty Optional.
+   * You can use Optional::value() to check whether the mac address is not null.
+   */
+  Optional<MacAddress> getMacAddressFromLinkLocal() const;
+
   /**
    * Return true if this is a multicast address.
    */
@@ -226,7 +246,8 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
 
   // @see IPAddress#isZero
   bool isZero() const {
-    return detail::Bytes::isZero(bytes(), 16);
+    constexpr auto zero = ByteArray16{{}};
+    return 0 == std::memcmp(bytes(), zero.data(), zero.size());
   }
 
   bool isLinkLocalBroadcast() const;
@@ -260,11 +281,16 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   // @see IPAddress#toFullyQualified
   std::string toFullyQualified() const;
 
+  // @see IPAddress#toFullyQualifiedAppend
+  void toFullyQualifiedAppend(std::string& out) const;
+
+  std::string toInverseArpaName() const;
+
   // @see IPAddress#str
   std::string str() const;
 
   // @see IPAddress#version
-  size_t version() const { return 6; }
+  uint8_t version() const { return 6; }
 
   /**
    * Return the solicited-node multicast address for this address.
@@ -282,13 +308,9 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   static const ByteArray16 fetchMask(size_t numBits);
   // Given 2 IPAddressV6,mask pairs extract the longest common IPAddress,
   // mask pair
-  static CIDRNetworkV6 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};
-  }
+  static CIDRNetworkV6 longestCommonPrefix(
+      const CIDRNetworkV6& one,
+      const CIDRNetworkV6& two);
   // Number of bytes in the address representation.
   static constexpr size_t byteCount() { return 16; }
 
@@ -308,7 +330,8 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   }
 
   const unsigned char* bytes() const { return addr_.in6Addr_.s6_addr; }
-  protected:
+
+ protected:
   /**
    * Helper that returns true if the address is in the binary subnet specified
    * by addr.
@@ -316,6 +339,31 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   bool inBinarySubnet(const std::array<uint8_t, 2> addr,
                       size_t numBits) const;
 
+ private:
+  auto tie() const {
+    return std::tie(addr_.bytes_, scope_);
+  }
+
+ public:
+  friend inline bool operator==(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() == b.tie();
+  }
+  friend inline bool operator!=(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() != b.tie();
+  }
+  friend inline bool operator<(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() < b.tie();
+  }
+  friend inline bool operator>(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() > b.tie();
+  }
+  friend inline bool operator<=(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() <= b.tie();
+  }
+  friend inline bool operator>=(const IPAddressV6& a, const IPAddressV6& b) {
+    return a.tie() >= b.tie();
+  }
+
  private:
   union AddressStorage {
     in6_addr in6Addr_;
@@ -332,8 +380,6 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
   // are *not* link-local.
   uint16_t scope_{0};
 
-  static const std::array<ByteArray16, 129> masks_;
-
   /**
    * Set the current IPAddressV6 object to have the address specified by bytes.
    * @throws IPAddressFormatException if bytes.size() is not 16.
@@ -349,31 +395,13 @@ std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr);
 void toAppend(IPAddressV6 addr, std::string* result);
 void toAppend(IPAddressV6 addr, fbstring* result);
 
-/**
- * Return true if two addresses are equal.
- */
-inline bool operator==(const IPAddressV6& addr1, const IPAddressV6& addr2) {
-  return (std::memcmp(addr1.toAddr().s6_addr, addr2.toAddr().s6_addr, 16) == 0)
-    && addr1.getScopeId() == addr2.getScopeId();
-}
-// Return true if addr1 < addr2
-inline bool operator<(const IPAddressV6& addr1, const IPAddressV6& addr2) {
-  auto cmp = std::memcmp(addr1.toAddr().s6_addr,
-                         addr2.toAddr().s6_addr, 16) < 0;
-  if (!cmp) {
-    return addr1.getScopeId() < addr2.getScopeId();
-  } else {
-    return cmp;
-  }
-}
-
-}  // folly
+} // namespace folly
 
 namespace std {
-template<>
+template <>
 struct hash<folly::IPAddressV6> {
   size_t operator()(const folly::IPAddressV6& addr) const {
     return addr.hash();
   }
 };
-}  // std
+} // namespace std