proxygen
folly::IPAddressV4 Class Reference

#include <IPAddressV4.h>

Classes

union  AddressStorage
 

Public Member Functions

ByteRange toBinary () const
 
 IPAddressV4 ()
 
 IPAddressV4 (StringPiece ip)
 
 IPAddressV4 (const ByteArray4 &src) noexcept
 
 IPAddressV4 (const in_addr src) noexcept
 
IPAddressV6 createIPv6 () const
 
IPAddressV6 getIPv6For6To4 () const
 
uint32_t toLong () const
 
uint32_t toLongHBO () const
 
std::string toJson () const
 
size_t hash () const
 
bool inSubnet (StringPiece cidrNetwork) const
 
bool inSubnet (const IPAddressV4 &subnet, uint8_t cidr) const
 
bool inSubnetWithMask (const IPAddressV4 &subnet, const ByteArray4 mask) const
 
bool isLoopback () const
 
bool isLinkLocal () const
 
bool isNonroutable () const
 
bool isPrivate () const
 
bool isMulticast () const
 
bool isZero () const
 
bool isLinkLocalBroadcast () const
 
IPAddressV4 mask (size_t numBits) const
 
std::string str () const
 
std::string toInverseArpaName () const
 
in_addr toAddr () const
 
sockaddr_in toSockAddr () const
 
ByteArray4 toByteArray () const
 
std::string toFullyQualified () const
 
void toFullyQualifiedAppend (std::string &out) const
 
uint8_t version () const
 
bool getNthMSBit (size_t bitIndex) const
 
uint8_t getNthMSByte (size_t byteIndex) const
 
bool getNthLSBit (size_t bitIndex) const
 
uint8_t getNthLSByte (size_t byteIndex) const
 
const unsigned char * bytes () const
 

Static Public Member Functions

static bool validate (StringPiece ip) noexcept
 
static IPAddressV4 fromLong (uint32_t src)
 
static IPAddressV4 fromLongHBO (uint32_t src)
 
static IPAddressV4 fromBinary (ByteRange bytes)
 
static Expected< IPAddressV4, IPAddressFormatErrortryFromBinary (ByteRange bytes) noexcept
 
static Expected< IPAddressV4, IPAddressFormatErrortryFromString (StringPiece str) noexcept
 
static IPAddressV4 fromInverseArpaName (const std::string &arpaname)
 
static uint32_t toLong (StringPiece ip)
 
static uint32_t toLongHBO (StringPiece ip)
 
static constexpr size_t bitCount ()
 
static const ByteArray4 fetchMask (size_t numBits)
 
static CIDRNetworkV4 longestCommonPrefix (const CIDRNetworkV4 &one, const CIDRNetworkV4 &two)
 
static size_t byteCount ()
 

Static Public Attributes

static constexpr size_t kMaxToFullyQualifiedSize
 

Private Member Functions

Expected< Unit, IPAddressFormatErrortrySetFromBinary (ByteRange bytes) noexcept
 

Private Attributes

union folly::IPAddressV4::AddressStorage addr_
 

Detailed Description

IPv4 variation of IPAddress.

Added methods: toLong, toLongHBO and createIPv6

Note
toLong/fromLong deal in network byte order, use toLongHBO/fromLongHBO if working in host byte order.
See also
IPAddress

Definition at line 58 of file IPAddressV4.h.

Constructor & Destructor Documentation

folly::IPAddressV4::IPAddressV4 ( )

Default constructor for IPAddressV4.

The address value will be 0.0.0.0

Definition at line 81 of file IPAddressV4.cpp.

Referenced by fromInverseArpaName(), fromLong(), fromLongHBO(), longestCommonPrefix(), mask(), toBinary(), and tryFromString().

81 {}
folly::IPAddressV4::IPAddressV4 ( StringPiece  ip)
explicit

Definition at line 87 of file IPAddressV4.cpp.

References folly::gen::move, and tryFromString().

87  : addr_() {
88  auto maybeIp = tryFromString(addr);
89  if (maybeIp.hasError()) {
90  throw IPAddressFormatException(
91  to<std::string>("Invalid IPv4 address '", addr, "'"));
92  }
93  *this = std::move(maybeIp.value());
94 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static Expected< IPAddressV4, IPAddressFormatError > tryFromString(StringPiece str) noexcept
Definition: IPAddressV4.cpp:96
union folly::IPAddressV4::AddressStorage addr_
ThreadPoolListHook * addr
folly::IPAddressV4::IPAddressV4 ( const ByteArray4 src)
explicitnoexcept

Definition at line 84 of file IPAddressV4.cpp.

84 : addr_(src) {}
union folly::IPAddressV4::AddressStorage addr_
folly::IPAddressV4::IPAddressV4 ( const in_addr  src)
explicitnoexcept

Definition at line 106 of file IPAddressV4.cpp.

106 : addr_(src) {}
union folly::IPAddressV4::AddressStorage addr_

Member Function Documentation

static constexpr size_t folly::IPAddressV4::bitCount ( )
inlinestatic
See also
IPAddress::bitCount
Returns
32

Definition at line 156 of file IPAddressV4.h.

References string, and toJson().

Referenced by fetchMask(), getNthLSBit(), mask(), and TEST().

156  {
157  return 32;
158  }
static size_t folly::IPAddressV4::byteCount ( )
inlinestatic

Definition at line 262 of file IPAddressV4.h.

Referenced by getNthLSByte(), and getNthMSByte().

262  {
263  return 4;
264  }
const unsigned char* folly::IPAddressV4::bytes ( ) const
inline
IPAddressV6 folly::IPAddressV4::createIPv6 ( ) const

Definition at line 153 of file IPAddressV4.cpp.

References bytes().

Referenced by toBinary().

153  {
154  ByteArray16 ba{};
155  ba[10] = 0xff;
156  ba[11] = 0xff;
157  std::memcpy(&ba[12], bytes(), 4);
158  return IPAddressV6(ba);
159 }
std::array< uint8_t, 16 > ByteArray16
Definition: IPAddressV6.h:50
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
const ByteArray4 folly::IPAddressV4::fetchMask ( size_t  numBits)
static

Return the mask associated with the given number of bits. If for instance numBits was 24 (e.g. /24) then the V4 mask returned should be {0xff, 0xff, 0xff, 0x00}.

Parameters
[in]numBitsbitmask to retrieve
Exceptions
abortif numBits == 0 or numBits > bitCount()
Returns
mask associated with numBits

Definition at line 282 of file IPAddressV4.cpp.

References folly::Endian::big(), bitCount(), uint32_t, uint64_t, and val.

Referenced by inSubnet(), isLinkLocal(), isLoopback(), mask(), TEST(), and version().

282  {
283  static const size_t bits = bitCount();
284  if (numBits > bits) {
285  throw IPAddressFormatException("IPv4 addresses are 32 bits");
286  }
287  auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits)));
288  ByteArray4 arr;
289  std::memcpy(arr.data(), &val, sizeof(val));
290  return arr;
291 }
double val
Definition: String.cpp:273
static T big(T x)
Definition: Bits.h:259
std::array< uint8_t, 4 > ByteArray4
Definition: IPAddressV4.h:46
static constexpr size_t bitCount()
Definition: IPAddressV4.h:156
IPAddressV4 folly::IPAddressV4::fromBinary ( ByteRange  bytes)
static

Create a new IPAddress instance from the provided binary data.

Exceptions
IPAddressFormatExceptionif the input length is not 4 bytes.

Definition at line 108 of file IPAddressV4.cpp.

References folly::Range< Iter >::size(), and tryFromBinary().

Referenced by TEST(), TEST_P(), and folly::toAppend().

108  {
109  auto maybeIp = tryFromBinary(bytes);
110  if (maybeIp.hasError()) {
111  throw IPAddressFormatException(to<std::string>(
112  "Invalid IPv4 binary data: length must be 4 bytes, got ",
113  bytes.size()));
114  }
115  return maybeIp.value();
116 }
static Expected< IPAddressV4, IPAddressFormatError > tryFromBinary(ByteRange bytes) noexcept
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
IPAddressV4 folly::IPAddressV4::fromInverseArpaName ( const std::string arpaname)
static

Create a new IPAddress instance from the in-addr.arpa representation.

Exceptions
IPAddressFormatExceptionif the input is not a valid in-addr.arpa representation

Definition at line 138 of file IPAddressV4.cpp.

References IPAddressV4(), folly::join(), folly::sformat(), and folly::split().

Referenced by TEST(), and toBinary().

138  {
139  auto piece = StringPiece(arpaname);
140  // input must be something like 1.0.168.192.in-addr.arpa
141  if (!piece.removeSuffix(".in-addr.arpa")) {
142  throw IPAddressFormatException(
143  sformat("input does not end with '.in-addr.arpa': '{}'", arpaname));
144  }
145  std::vector<StringPiece> pieces;
146  split(".", piece, pieces);
147  if (pieces.size() != 4) {
148  throw IPAddressFormatException(sformat("Invalid input. Got {}", piece));
149  }
150  // reverse 1.0.168.192 -> 192.168.0.1
151  return IPAddressV4(join(".", pieces.rbegin(), pieces.rend()));
152 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
void split(const Delim &delimiter, const String &input, std::vector< OutputType > &out, bool ignoreEmpty)
Definition: String-inl.h:382
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
Range< const char * > StringPiece
IPAddressV4 folly::IPAddressV4::fromLong ( uint32_t  src)
static

Definition at line 52 of file IPAddressV4.cpp.

References addr, and IPAddressV4().

Referenced by folly::toAppend().

52  {
53  in_addr addr;
54  addr.s_addr = src;
55  return IPAddressV4(addr);
56 }
ThreadPoolListHook * addr
IPAddressV4 folly::IPAddressV4::fromLongHBO ( uint32_t  src)
static

Definition at line 58 of file IPAddressV4.cpp.

References addr, and IPAddressV4().

Referenced by folly::toAppend().

58  {
59  in_addr addr;
60  addr.s_addr = htonl(src);
61  return IPAddressV4(addr);
62 }
ThreadPoolListHook * addr
IPAddressV6 folly::IPAddressV4::getIPv6For6To4 ( ) const

Return a V6 address in the format of an 6To4 address.

Definition at line 162 of file IPAddressV4.cpp.

References bytes(), folly::IPAddressV6::PREFIX_6TO4, and uint8_t.

Referenced by TEST(), and toBinary().

162  {
163  ByteArray16 ba{};
164  ba[0] = (uint8_t)((IPAddressV6::PREFIX_6TO4 & 0xFF00) >> 8);
165  ba[1] = (uint8_t)(IPAddressV6::PREFIX_6TO4 & 0x00FF);
166  std::memcpy(&ba[2], bytes(), 4);
167  return IPAddressV6(ba);
168 }
static const uint32_t PREFIX_6TO4
Definition: IPAddressV6.h:90
std::array< uint8_t, 16 > ByteArray16
Definition: IPAddressV6.h:50
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
bool folly::IPAddressV4::getNthLSBit ( size_t  bitIndex) const
inline

Definition at line 272 of file IPAddressV4.h.

References bitCount(), and getNthMSBit().

272  {
273  return getNthMSBit(bitCount() - bitIndex - 1);
274  }
bool getNthMSBit(size_t bitIndex) const
Definition: IPAddressV4.h:266
static constexpr size_t bitCount()
Definition: IPAddressV4.h:156
uint8_t folly::IPAddressV4::getNthLSByte ( size_t  byteIndex) const
inline

Definition at line 276 of file IPAddressV4.h.

References byteCount(), and getNthMSByte().

276  {
277  return getNthMSByte(byteCount() - byteIndex - 1);
278  }
static size_t byteCount()
Definition: IPAddressV4.h:262
uint8_t getNthMSByte(size_t byteIndex) const
bool folly::IPAddressV4::getNthMSBit ( size_t  bitIndex) const
inline

Definition at line 266 of file IPAddressV4.h.

References folly::detail::getNthMSBitImpl(), getNthMSByte(), and uint8_t.

Referenced by getNthLSBit().

266  {
267  return detail::getNthMSBitImpl(*this, bitIndex, AF_INET);
268  }
bool getNthMSBitImpl(const IPAddrType &ip, size_t bitIndex, sa_family_t family)
Definition: IPAddress.h:49
uint8_t folly::IPAddressV4::getNthMSByte ( size_t  byteIndex) const

Definition at line 271 of file IPAddressV4.cpp.

References byteCount(), bytes(), folly::detail::familyNameStr(), and folly::sformat().

Referenced by getNthLSByte(), and getNthMSBit().

271  {
272  const auto highestIndex = byteCount() - 1;
273  if (byteIndex > highestIndex) {
274  throw std::invalid_argument(sformat(
275  "Byte index must be <= {} for addresses of type: {}",
276  highestIndex,
277  detail::familyNameStr(AF_INET)));
278  }
279  return bytes()[byteIndex];
280 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static size_t byteCount()
Definition: IPAddressV4.h:262
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
std::string familyNameStr(sa_family_t family)
Definition: IPAddress.h:30
size_t folly::IPAddressV4::hash ( ) const
inline

Definition at line 165 of file IPAddressV4.h.

References addr_, folly::hash::fnv32_buf(), folly::hash::hash_combine(), inSubnet(), seed, and uint32_t.

Referenced by folly::hash_value(), std::hash< folly::IPAddressV4 >::operator()(), and toJson().

165  {
166  static const uint32_t seed = AF_INET;
167  uint32_t hashed = hash::fnv32_buf(&addr_, 4);
168  return hash::hash_combine(seed, hashed);
169  }
static const int seed
union folly::IPAddressV4::AddressStorage addr_
uint32_t fnv32_buf(const void *buf, size_t n, uint32_t hash=FNV_32_HASH_START) noexcept
Definition: Hash.h:163
size_t hash_combine(const T &t, const Ts &...ts) noexcept(noexcept(hash_combine_generic(StdHasher{}, t, ts...)))
Definition: Hash.h:669
bool folly::IPAddressV4::inSubnet ( StringPiece  cidrNetwork) const

Definition at line 176 of file IPAddressV4.cpp.

References addr, fetchMask(), inSubnetWithMask(), and folly::sformat().

Referenced by hash(), and folly::toAppend().

176  {
177  auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
178  auto addr = subnetInfo.first;
179  if (!addr.isV4()) {
180  throw IPAddressFormatException(
181  sformat("Address '{}' is not a V4 address", addr.toJson()));
182  }
183  return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second));
184 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
bool inSubnetWithMask(const IPAddressV4 &subnet, const ByteArray4 mask) const
static const ByteArray4 fetchMask(size_t numBits)
ThreadPoolListHook * addr
bool folly::IPAddressV4::inSubnet ( const IPAddressV4 subnet,
uint8_t  cidr 
) const
inline

Definition at line 176 of file IPAddressV4.h.

References fetchMask(), inSubnetWithMask(), isLinkLocal(), isLoopback(), isMulticast(), isNonroutable(), isPrivate(), and mask().

176  {
177  return inSubnetWithMask(subnet, fetchMask(cidr));
178  }
bool inSubnetWithMask(const IPAddressV4 &subnet, const ByteArray4 mask) const
static const ByteArray4 fetchMask(size_t numBits)
bool folly::IPAddressV4::inSubnetWithMask ( const IPAddressV4 subnet,
const ByteArray4  mask 
) const

Definition at line 187 of file IPAddressV4.cpp.

References folly::detail::Bytes::mask(), mask(), and toByteArray().

Referenced by inSubnet(), isLinkLocal(), isLoopback(), and folly::toAppend().

189  {
190  const auto mask = detail::Bytes::mask(toByteArray(), cidrMask);
191  const auto subMask = detail::Bytes::mask(subnet.toByteArray(), cidrMask);
192  return (mask == subMask);
193 }
IPAddressV4 mask(size_t numBits) const
static std::array< uint8_t, N > mask(const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b)
ByteArray4 toByteArray() const
Definition: IPAddressV4.h:227
bool folly::IPAddressV4::isLinkLocal ( ) const

Definition at line 202 of file IPAddressV4.cpp.

References fetchMask(), and inSubnetWithMask().

Referenced by inSubnet().

202  {
203  static IPAddressV4 linklocal_addr("169.254.0.0");
204  return inSubnetWithMask(linklocal_addr, fetchMask(16));
205 }
bool inSubnetWithMask(const IPAddressV4 &subnet, const ByteArray4 mask) const
static const ByteArray4 fetchMask(size_t numBits)
bool folly::IPAddressV4::isLinkLocalBroadcast ( ) const
inline

Definition at line 202 of file IPAddressV4.h.

References mask(), str(), string, toInverseArpaName(), and toLongHBO().

202  {
203  return (INADDR_BROADCAST == toLongHBO());
204  }
uint32_t toLongHBO() const
Definition: IPAddressV4.h:148
bool folly::IPAddressV4::isLoopback ( ) const

Definition at line 196 of file IPAddressV4.cpp.

References fetchMask(), and inSubnetWithMask().

Referenced by inSubnet().

196  {
197  static IPAddressV4 loopback_addr("127.0.0.0");
198  return inSubnetWithMask(loopback_addr, fetchMask(8));
199 }
bool inSubnetWithMask(const IPAddressV4 &subnet, const ByteArray4 mask) const
static const ByteArray4 fetchMask(size_t numBits)
bool folly::IPAddressV4::isMulticast ( ) const

Definition at line 234 of file IPAddressV4.cpp.

References toLongHBO().

Referenced by inSubnet().

234  {
235  return (toLongHBO() & 0xf0000000) == 0xe0000000;
236 }
uint32_t toLongHBO() const
Definition: IPAddressV4.h:148
bool folly::IPAddressV4::isNonroutable ( ) const

Definition at line 208 of file IPAddressV4.cpp.

References isPrivate(), and toLongHBO().

Referenced by inSubnet().

208  {
209  auto ip = toLongHBO();
210  return isPrivate() ||
211  (/* align */ true && ip <= 0x00FFFFFF) || // 0.0.0.0-0.255.255.255
212  (ip >= 0xC0000000 && ip <= 0xC00000FF) || // 192.0.0.0-192.0.0.255
213  (ip >= 0xC0000200 && ip <= 0xC00002FF) || // 192.0.2.0-192.0.2.255
214  (ip >= 0xC6120000 && ip <= 0xC613FFFF) || // 198.18.0.0-198.19.255.255
215  (ip >= 0xC6336400 && ip <= 0xC63364FF) || // 198.51.100.0-198.51.100.255
216  (ip >= 0xCB007100 && ip <= 0xCB0071FF) || // 203.0.113.0-203.0.113.255
217  (ip >= 0xE0000000 && ip <= 0xFFFFFFFF) || // 224.0.0.0-255.255.255.255
218  false;
219 }
bool isPrivate() const
uint32_t toLongHBO() const
Definition: IPAddressV4.h:148
bool folly::IPAddressV4::isPrivate ( ) const

Definition at line 222 of file IPAddressV4.cpp.

References toLongHBO().

Referenced by inSubnet(), and isNonroutable().

222  {
223  auto ip = toLongHBO();
224  return // some ranges below
225  (ip >= 0x0A000000 && ip <= 0x0AFFFFFF) || // 10.0.0.0-10.255.255.255
226  (ip >= 0x7F000000 && ip <= 0x7FFFFFFF) || // 127.0.0.0-127.255.255.255
227  (ip >= 0xA9FE0000 && ip <= 0xA9FEFFFF) || // 169.254.0.0-169.254.255.255
228  (ip >= 0xAC100000 && ip <= 0xAC1FFFFF) || // 172.16.0.0-172.31.255.255
229  (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF) || // 192.168.0.0-192.168.255.255
230  false;
231 }
uint32_t toLongHBO() const
Definition: IPAddressV4.h:148
bool folly::IPAddressV4::isZero ( ) const
inline

Definition at line 197 of file IPAddressV4.h.

References bytes().

197  {
198  constexpr auto zero = ByteArray4{{}};
199  return 0 == std::memcmp(bytes(), zero.data(), zero.size());
200  }
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
std::array< uint8_t, 4 > ByteArray4
Definition: IPAddressV4.h:46
CIDRNetworkV4 folly::IPAddressV4::longestCommonPrefix ( const CIDRNetworkV4 one,
const CIDRNetworkV4 two 
)
static

Definition at line 293 of file IPAddressV4.cpp.

References IPAddressV4(), folly::detail::Bytes::longestCommonPrefix(), and prefix().

Referenced by folly::operator<(), TEST(), and version().

295  {
297  one.first.addr_.bytes_, one.second, two.first.addr_.bytes_, two.second);
298  return {IPAddressV4(prefix.first), prefix.second};
299 }
bool prefix(Cursor &c, uint32_t expected)
static std::pair< std::array< uint8_t, N >, uint8_t > longestCommonPrefix(const std::array< uint8_t, N > &one, uint8_t oneMask, const std::array< uint8_t, N > &two, uint8_t twoMask)
IPAddressV4 folly::IPAddressV4::mask ( size_t  numBits) const

Definition at line 239 of file IPAddressV4.cpp.

References addr_, bitCount(), folly::IPAddressV4::AddressStorage::bytes_, fetchMask(), IPAddressV4(), folly::detail::Bytes::mask(), and folly::sformat().

Referenced by inSubnet(), inSubnetWithMask(), and isLinkLocalBroadcast().

239  {
240  static const auto bits = bitCount();
241  if (numBits > bits) {
242  throw IPAddressFormatException(
243  sformat("numBits({}) > bitsCount({})", numBits, bits));
244  }
245 
247  return IPAddressV4(ba);
248 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static std::array< uint8_t, N > mask(const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b)
union folly::IPAddressV4::AddressStorage addr_
static const ByteArray4 fetchMask(size_t numBits)
std::array< uint8_t, 4 > ByteArray4
Definition: IPAddressV4.h:46
static constexpr size_t bitCount()
Definition: IPAddressV4.h:156
string folly::IPAddressV4::str ( ) const

Definition at line 251 of file IPAddressV4.cpp.

References addr_, folly::detail::fastIpv4ToString(), and folly::IPAddressV4::AddressStorage::inAddr_.

Referenced by isLinkLocalBroadcast(), folly::operator<<(), folly::toAppend(), toFullyQualified(), toJson(), toLong(), and tryFromString().

251  {
253 }
union folly::IPAddressV4::AddressStorage addr_
std::string fastIpv4ToString(const in_addr &inAddr)
in_addr folly::IPAddressV4::toAddr ( ) const
inline

Definition at line 215 of file IPAddressV4.h.

References addr_, and folly::IPAddressV4::AddressStorage::inAddr_.

Referenced by BENCHMARK(), and toLong().

215  {
216  return addr_.inAddr_;
217  }
union folly::IPAddressV4::AddressStorage addr_
ByteRange folly::IPAddressV4::toBinary ( ) const
inline

Returns the address as a Range.

Definition at line 95 of file IPAddressV4.h.

References addr_, createIPv6(), fromInverseArpaName(), getIPv6For6To4(), folly::IPAddressV4::AddressStorage::inAddr_, IPAddressV4(), folly::pushmi::__adl::noexcept(), string, toLong(), toLongHBO(), and uint32_t.

95  {
96  return ByteRange((const unsigned char*)&addr_.inAddr_.s_addr, 4);
97  }
union folly::IPAddressV4::AddressStorage addr_
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
ByteArray4 folly::IPAddressV4::toByteArray ( ) const
inline

Definition at line 227 of file IPAddressV4.h.

References bytes().

Referenced by inSubnetWithMask().

227  {
228  ByteArray4 ba{{0}};
229  std::memcpy(ba.data(), bytes(), 4);
230  return ba;
231  }
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
std::array< uint8_t, 4 > ByteArray4
Definition: IPAddressV4.h:46
std::string folly::IPAddressV4::toFullyQualified ( ) const
inline

Definition at line 234 of file IPAddressV4.h.

References str(), string, and toFullyQualifiedAppend().

Referenced by BENCHMARK(), and BENCHMARK_RELATIVE().

234  {
235  return str();
236  }
std::string str() const
void folly::IPAddressV4::toFullyQualifiedAppend ( std::string out) const

Definition at line 256 of file IPAddressV4.cpp.

References addr_, folly::detail::fastIpv4AppendToString(), and folly::IPAddressV4::AddressStorage::inAddr_.

Referenced by BENCHMARK_RELATIVE(), and toFullyQualified().

256  {
258 }
union folly::IPAddressV4::AddressStorage addr_
void fastIpv4AppendToString(const in_addr &inAddr, std::string &out)
string folly::IPAddressV4::toInverseArpaName ( ) const

Definition at line 261 of file IPAddressV4.cpp.

References addr_, folly::IPAddressV4::AddressStorage::bytes_, and folly::sformat().

Referenced by isLinkLocalBroadcast(), and TEST().

261  {
262  return sformat(
263  "{}.{}.{}.{}.in-addr.arpa",
264  addr_.bytes_[3],
265  addr_.bytes_[2],
266  addr_.bytes_[1],
267  addr_.bytes_[0]);
268 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
union folly::IPAddressV4::AddressStorage addr_
string folly::IPAddressV4::toJson ( ) const

IPAddress::toJson

Definition at line 171 of file IPAddressV4.cpp.

References hash(), folly::sformat(), and str().

Referenced by bitCount().

171  {
172  return sformat("{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash());
173 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
size_t hash() const
Definition: IPAddressV4.h:165
std::string str() const
uint32_t folly::IPAddressV4::toLong ( StringPiece  ip)
static

Convert a IPv4 address string to a long in network byte order.

Parameters
[in]ipthe address to convert
Returns
the long representation of the address

Definition at line 65 of file IPAddressV4.cpp.

References addr, folly::sformat(), str(), and folly::Range< Iter >::str().

Referenced by folly::operator==().

65  {
66  auto str = ip.str();
67  in_addr addr;
68  if (inet_pton(AF_INET, str.c_str(), &addr) != 1) {
69  throw IPAddressFormatException(
70  sformat("Can't convert invalid IP '{}' to long", ip));
71  }
72  return addr.s_addr;
73 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
std::string str() const
ThreadPoolListHook * addr
uint32_t folly::IPAddressV4::toLong ( ) const
inline

Definition at line 142 of file IPAddressV4.h.

References toAddr().

Referenced by toBinary(), and toLongHBO().

142  {
143  return toAddr().s_addr;
144  }
in_addr toAddr() const
Definition: IPAddressV4.h:215
uint32_t folly::IPAddressV4::toLongHBO ( StringPiece  ip)
static

Definition at line 76 of file IPAddressV4.cpp.

References toLong().

Referenced by folly::operator<().

76  {
77  return ntohl(IPAddressV4::toLong(ip));
78 }
uint32_t toLong() const
Definition: IPAddressV4.h:142
uint32_t folly::IPAddressV4::toLongHBO ( ) const
inline

Definition at line 148 of file IPAddressV4.h.

References toLong().

Referenced by isLinkLocalBroadcast(), isMulticast(), isNonroutable(), isPrivate(), and toBinary().

148  {
149  return ntohl(toLong());
150  }
uint32_t toLong() const
Definition: IPAddressV4.h:142
sockaddr_in folly::IPAddressV4::toSockAddr ( ) const
inline

Definition at line 219 of file IPAddressV4.h.

References addr, addr_, and folly::IPAddressV4::AddressStorage::inAddr_.

219  {
220  sockaddr_in addr;
221  memset(&addr, 0, sizeof(sockaddr_in));
222  addr.sin_family = AF_INET;
223  memcpy(&addr.sin_addr, &addr_.inAddr_, sizeof(in_addr));
224  return addr;
225  }
union folly::IPAddressV4::AddressStorage addr_
ThreadPoolListHook * addr
Expected< IPAddressV4, IPAddressFormatError > folly::IPAddressV4::tryFromBinary ( ByteRange  bytes)
staticnoexcept

Non-throwing version of fromBinary(). On failure returns IPAddressFormatError.

Definition at line 118 of file IPAddressV4.cpp.

References addr, bytes(), folly::makeUnexpected(), folly::gen::move, and trySetFromBinary().

Referenced by fromBinary(), TEST(), TEST_P(), and folly::toAppend().

119  {
121  auto setResult = addr.trySetFromBinary(bytes);
122  if (setResult.hasError()) {
123  return makeUnexpected(std::move(setResult.error()));
124  }
125  return addr;
126 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
Definition: Expected.h:785
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
ThreadPoolListHook * addr
Expected< IPAddressV4, IPAddressFormatError > folly::IPAddressV4::tryFromString ( StringPiece  str)
staticnoexcept

Tries to create a new IPAddressV4 instance from provided string and returns it on success. Returns IPAddressFormatError on failure.

Definition at line 96 of file IPAddressV4.cpp.

References folly::INVALID_IP, IPAddressV4(), folly::makeUnexpected(), and str().

Referenced by IPAddressV4(), TEST_P(), and folly::toAppend().

97  {
98  struct in_addr inAddr;
99  if (inet_pton(AF_INET, str.str().c_str(), &inAddr) != 1) {
101  }
102  return IPAddressV4(inAddr);
103 }
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
Definition: Expected.h:785
std::string str() const
Expected< Unit, IPAddressFormatError > folly::IPAddressV4::trySetFromBinary ( ByteRange  bytes)
privatenoexcept

Set the current IPAddressV4 object to have the address specified by bytes. Returns IPAddressFormatError if bytes.size() is not 4.

Definition at line 128 of file IPAddressV4.cpp.

References addr_, bytes(), folly::IPAddressV4::AddressStorage::inAddr_, folly::INVALID_IP, folly::makeUnexpected(), and folly::unit.

Referenced by folly::IPAddressV4::AddressStorage::AddressStorage(), and tryFromBinary().

129  {
130  if (bytes.size() != 4) {
132  }
133  memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
134  return folly::unit;
135 }
union folly::IPAddressV4::AddressStorage addr_
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
Definition: Expected.h:785
const unsigned char * bytes() const
Definition: IPAddressV4.h:280
constexpr Unit unit
Definition: Unit.h:45
bool folly::IPAddressV4::validate ( StringPiece  ip)
staticnoexcept

Definition at line 47 of file IPAddressV4.cpp.

Referenced by TEST(), and folly::toAppend().

47  {
48  return tryFromString(ip).hasValue();
49 }
static Expected< IPAddressV4, IPAddressFormatError > tryFromString(StringPiece str) noexcept
Definition: IPAddressV4.cpp:96
uint8_t folly::IPAddressV4::version ( ) const
inline

Definition at line 242 of file IPAddressV4.h.

References fetchMask(), and longestCommonPrefix().

242  {
243  return 4;
244  }

Member Data Documentation

constexpr size_t folly::IPAddressV4::kMaxToFullyQualifiedSize
static
Initial value:
=
4 * 3 + 3

Definition at line 61 of file IPAddressV4.h.

Referenced by BENCHMARK_RELATIVE(), and TEST().


The documentation for this class was generated from the following files: