X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FIPAddressV4.h;h=6603828a93b8e9bc44c5679946658e2fca7b42ff;hb=8a56900338f3fccf82a35f5e4de9e9680abb05ad;hp=9c66afb64f18d522c69e09456cf2e45145cab3ec;hpb=e401a93c31c351a48a21dae97fcd7c3b07d7790c;p=folly.git diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 9c66afb6..6603828a 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 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,10 +16,11 @@ #pragma once -#include -#include +#include -#include +#include +#include +#include #include #include @@ -51,8 +52,11 @@ typedef std::array ByteArray4; * * @see IPAddress */ -class IPAddressV4 : boost::totally_ordered { +class IPAddressV4 { public: + // returns true iff the input string can be parsed as an ipv4-address + static bool validate(StringPiece ip); + // create an IPAddressV4 instance from a uint32_t (network byte order) static IPAddressV4 fromLong(uint32_t src); // same as above but host byte order @@ -68,6 +72,13 @@ class IPAddressV4 : boost::totally_ordered { return addr; } + /** + * Returns the address as a Range. + */ + ByteRange toBinary() const { + return ByteRange((const unsigned char *) &addr_.inAddr_.s_addr, 4); + } + /** * Convert a IPv4 address string to a long in network byte order. * @param [in] ip the address to convert @@ -98,6 +109,11 @@ class IPAddressV4 : boost::totally_ordered { // Return the V6 mapped representation of the address. IPAddressV6 createIPv6() const; + /** + * Return a V6 address in the format of an 6To4 address. + */ + IPAddressV6 getIPv6For6To4() const; + // Return the long (network byte order) representation of the address. uint32_t toLong() const { return toAddr().s_addr; @@ -139,6 +155,9 @@ class IPAddressV4 : boost::totally_ordered { // @see IPAddress#isLoopback bool isLoopback() const; + // @see IPAddress#isLinkLocal + bool isLinkLocal() const; + // @see IPAddress#isNonroutable bool isNonroutable() const; @@ -150,7 +169,8 @@ class IPAddressV4 : boost::totally_ordered { // @see IPAddress#isZero bool isZero() const { - return detail::Bytes::isZero(bytes(), 4); + constexpr auto zero = ByteArray4{{}}; + return 0 == std::memcmp(bytes(), zero.data(), zero.size()); } bool isLinkLocalBroadcast() const { @@ -163,6 +183,8 @@ class IPAddressV4 : boost::totally_ordered { // @see IPAddress#str std::string str() const; + std::string toInverseArpaName() const; + // return underlying in_addr structure in_addr toAddr() const { return addr_.inAddr_; } @@ -177,14 +199,14 @@ class IPAddressV4 : boost::totally_ordered { ByteArray4 toByteArray() const { ByteArray4 ba{{0}}; std::memcpy(ba.data(), bytes(), 4); - return std::move(ba); + return ba; } // @see IPAddress#toFullyQualified std::string toFullyQualified() const { return str(); } // @see IPAddress#version - size_t version() const { return 4; } + uint8_t version() const { return 4; } /** * Return the mask associated with the given number of bits. @@ -196,15 +218,11 @@ class IPAddressV4 : boost::totally_ordered { */ static const ByteArray4 fetchMask(size_t numBits); - // Given 2 IPAddressV4,mask pairs extract the longest common IPAddress, + // Given 2 IPAddressV4, mask pairs extract the longest common IPAddress, // mask pair static CIDRNetworkV4 longestCommonPrefix( - const CIDRNetworkV4& one, const CIDRNetworkV4& two) { - auto prefix = - detail::Bytes::longestCommonPrefix(one.first.addr_.bytes_, one.second, - two.first.addr_.bytes_, two.second); - return {IPAddressV4(prefix.first), prefix.second}; - } + const CIDRNetworkV4& one, + const CIDRNetworkV4& two); // Number of bytes in the address representation. static size_t byteCount() { return 4; } //get nth most significant bit - 0 indexed @@ -264,6 +282,19 @@ inline bool operator==(const IPAddressV4& addr1, const IPAddressV4& addr2) { inline bool operator<(const IPAddressV4& addr1, const IPAddressV4& addr2) { return (addr1.toLongHBO() < addr2.toLongHBO()); } +// Derived operators +inline bool operator!=(const IPAddressV4& a, const IPAddressV4& b) { + return !(a == b); +} +inline bool operator>(const IPAddressV4& a, const IPAddressV4& b) { + return b < a; +} +inline bool operator<=(const IPAddressV4& a, const IPAddressV4& b) { + return !(a > b); +} +inline bool operator>=(const IPAddressV4& a, const IPAddressV4& b) { + return !(a < b); +} } // folly