X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FIPAddressV6.h;h=21e892a42aac4f986c2c4a0d89585cb0e4b1431e;hp=703f9da71e5ffeca96047bf5a8ab80bcc1fab66f;hb=5da6f8de1779620e07722fea4d6e0bbc31e2e226;hpb=01dbb37f94558e8793137943b5fd97fe9d64d594 diff --git a/folly/IPAddressV6.h b/folly/IPAddressV6.h index 703f9da7..21e892a4 100644 --- a/folly/IPAddressV6.h +++ b/folly/IPAddressV6.h @@ -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. @@ -16,14 +16,16 @@ #pragma once +#include + +#include #include -#include +#include #include #include -#include - #include +#include #include #include @@ -63,7 +65,7 @@ typedef std::array ByteArray16; * Serializing / Deserializing IPAddressB6's on different hosts * that use link-local scoping probably won't work. */ -class IPAddressV6 : boost::totally_ordered { +class IPAddressV6 { public: // V6 Address Type enum Type { @@ -99,6 +101,13 @@ class IPAddressV6 : boost::totally_ordered { 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. */ @@ -208,6 +217,16 @@ class IPAddressV6 : boost::totally_ordered { */ bool isLinkLocal() const; + /** + * Return the mac address if this is a link-local IPv6 address. + * + * @return an Optional 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 getMacAddressFromLinkLocal() const; + /** * Return true if this is a multicast address. */ @@ -227,7 +246,8 @@ class IPAddressV6 : boost::totally_ordered { // @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; @@ -261,11 +281,13 @@ class IPAddressV6 : boost::totally_ordered { // @see IPAddress#toFullyQualified std::string toFullyQualified() 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. @@ -283,13 +305,9 @@ class IPAddressV6 : boost::totally_ordered { 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; } @@ -367,6 +385,19 @@ inline bool operator<(const IPAddressV6& addr1, const IPAddressV6& addr2) { return cmp; } } +// Derived operators +inline bool operator!=(const IPAddressV6& a, const IPAddressV6& b) { + return !(a == b); +} +inline bool operator>(const IPAddressV6& a, const IPAddressV6& b) { + return b < a; +} +inline bool operator<=(const IPAddressV6& a, const IPAddressV6& b) { + return !(a > b); +} +inline bool operator>=(const IPAddressV6& a, const IPAddressV6& b) { + return !(a < b); +} } // folly