38 typedef std::pair<IPAddress, uint8_t> CIDRNetwork;
70 auto pick(F
f)
const {
71 return isV4() ?
f(asV4()) : f(asV6());
81 static IPAddressV4 createIPv4(
const IPAddress&
addr);
85 static IPAddressV6 createIPv6(
const IPAddress&
addr);
97 static Expected<CIDRNetwork, CIDRNetworkError> tryCreateNetwork(
110 static CIDRNetwork createNetwork(
112 int defaultCidr = -1,
121 static std::string networkToString(
const CIDRNetwork& network);
128 static IPAddress fromBinary(
ByteRange bytes);
134 static Expected<IPAddress, IPAddressFormatError> tryFromBinary(
141 static Expected<IPAddress, IPAddressFormatError> tryFromString(
148 static IPAddress fromLong(
uint32_t src);
150 static IPAddress fromLongHBO(
uint32_t src);
154 static CIDRNetwork longestCommonPrefix(
155 const CIDRNetwork& one,
156 const CIDRNetwork& two);
179 explicit IPAddress(
const sockaddr*
addr);
182 IPAddress(
const IPAddressV4 ipV4Addr)
noexcept;
186 IPAddress(
const IPAddressV6& ipV6Addr)
noexcept;
190 IPAddress& operator=(
const IPAddressV4& ipV4Addr)
noexcept;
193 IPAddress& operator=(
const IPAddressV6& ipV6Addr)
noexcept;
202 const IPAddressV4& asV4()
const {
206 return addr_.ipV4Addr;
213 const IPAddressV6& asV6()
const {
217 return addr_.ipV6Addr;
221 sa_family_t family()
const {
226 int toSockaddrStorage(sockaddr_storage*
dest,
uint16_t port = 0)
const {
227 if (dest ==
nullptr) {
228 throw IPAddressFormatException(
"dest must not be null");
230 memset(dest, 0,
sizeof(sockaddr_storage));
231 dest->ss_family = family();
234 sockaddr_in* sin =
reinterpret_cast<sockaddr_in*
>(
dest);
235 sin->sin_addr = asV4().toAddr();
236 sin->sin_port = port;
237 #if defined(__APPLE__) 238 sin->sin_len =
sizeof(*sin);
242 sockaddr_in6* sin =
reinterpret_cast<sockaddr_in6*
>(
dest);
243 sin->sin6_addr = asV6().toAddr();
244 sin->sin6_port = port;
245 sin->sin6_scope_id = asV6().getScopeId();
246 #if defined(__APPLE__) 247 sin->sin6_len =
sizeof(*sin);
251 throw InvalidAddressFamilyException(family());
277 bool inSubnet(
const IPAddress& subnet,
uint8_t cidr)
const;
287 bool inSubnetWithMask(
const IPAddress& subnet,
ByteRange mask)
const;
290 bool isIPv4Mapped()
const {
291 return isV6() && asV6().isIPv4Mapped();
296 return family_ == AF_UNSPEC;
300 explicit operator bool()
const {
306 return family_ == AF_INET;
311 return family_ == AF_INET6;
315 bool isZero()
const {
316 return pick([&](
auto&
_) {
return _.isZero(); });
320 size_t bitCount()
const {
321 return pick([&](
auto&
_) {
return _.bitCount(); });
324 size_t byteCount()
const {
325 return bitCount() / 8;
328 bool getNthMSBit(
size_t bitIndex)
const {
332 uint8_t getNthMSByte(
size_t byteIndex)
const;
334 bool getNthLSBit(
size_t bitIndex)
const {
335 return getNthMSBit(bitCount() - bitIndex - 1);
338 uint8_t getNthLSByte(
size_t byteIndex)
const {
339 return getNthMSByte(byteCount() - byteIndex - 1);
349 return pick([&](
auto&
_) {
return _.toJson(); });
353 std::size_t hash()
const {
354 return pick([&](
auto&
_) {
return _.hash(); });
358 bool isLoopback()
const {
359 return pick([&](
auto&
_) {
return _.isLoopback(); });
363 bool isLinkLocal()
const {
364 return pick([&](
auto&
_) {
return _.isLinkLocal(); });
368 bool isLinkLocalBroadcast()
const {
369 return pick([&](
auto&
_) {
return _.isLinkLocalBroadcast(); });
378 bool isNonroutable()
const {
379 return pick([&](
auto&
_) {
return _.isNonroutable(); });
386 bool isPrivate()
const {
387 return pick([&](
auto&
_) {
return _.isPrivate(); });
391 bool isMulticast()
const {
392 return pick([&](
auto&
_) {
return _.isMulticast(); });
401 IPAddress mask(
uint8_t numBits)
const {
402 return pick([&](
auto&
_) {
return IPAddress(
_.mask(numBits)); });
411 return pick([&](
auto&
_) {
return _.str(); });
420 return pick([&](
auto&
_) {
return _.toFullyQualified(); });
424 void toFullyQualifiedAppend(
std::string& out)
const {
425 return pick([&](
auto&
_) {
return _.toFullyQualifiedAppend(out); });
430 return pick([&](
auto&
_) {
return _.version(); });
436 const unsigned char* bytes()
const {
437 return pick([&](
auto&
_) {
return _.bytes(); });
441 [[noreturn]]
void asV4Throw()
const;
442 [[noreturn]]
void asV6Throw()
const;
444 typedef union IPAddressV46 {
445 IPAddressV4 ipV4Addr;
446 IPAddressV6 ipV6Addr;
448 IPAddressV46() noexcept {
449 std::memset(
this, 0,
sizeof(IPAddressV46));
451 explicit IPAddressV46(
const IPAddressV4&
addr) noexcept : ipV4Addr(
addr) {}
452 explicit IPAddressV46(
const IPAddressV6&
addr) noexcept : ipV6Addr(
addr) {}
461 std::ostream&
operator<<(std::ostream& os,
const IPAddress&
addr);
474 bool operator==(
const IPAddress& addr1,
const IPAddress& addr2);
476 bool operator<(
const IPAddress& addr1,
const IPAddress& addr2);
478 inline bool operator!=(
const IPAddress&
a,
const IPAddress&
b) {
481 inline bool operator>(
const IPAddress&
a,
const IPAddress&
b) {
484 inline bool operator<=(
const IPAddress&
a,
const IPAddress&
b) {
487 inline bool operator>=(
const IPAddress&
a,
const IPAddress&
b) {
495 struct hash<
folly::IPAddress> {
496 size_t operator()(
const folly::IPAddress&
addr)
const {
bool getNthMSBitImpl(const IPAddrType &ip, size_t bitIndex, sa_family_t family)
bool operator>(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
bool operator!=(const Unexpected< Error > &lhs, const Unexpected< Error > &rhs)
constexpr auto empty(C const &c) -> decltype(c.empty())
size_t hash_value(const IPAddress &addr)
void toAppend(char value, Tgt *result)
bool operator>=(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
bool operator==(const Unexpected< Error > &lhs, const Unexpected< Error > &rhs)
bool operator<=(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
Range< const unsigned char * > ByteRange
const internal::AnythingMatcher _
basic_fbstring< char > fbstring
Range< const char * > StringPiece
std::string toJson(dynamic const &dyn)
ThreadPoolListHook * addr
std::enable_if< IsLessThanComparable< Value >::value, bool >::type operator<(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
std::ostream & operator<<(std::ostream &out, dynamic const &d)