folly/folly/IPAddress.h

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Provides a unified interface for IP addresses.
 *
 * @refcode folly/docs/examples/folly/ipaddress.cpp
 *
 * @class folly::IPAddress
 * @see IPAddressV6
 * @see IPAddressV4
 */

#pragma once

#include <functional>
#include <iosfwd>
#include <memory>
#include <string>
#include <type_traits>
#include <utility> // std::pair

#include <folly/ConstexprMath.h>
#include <folly/IPAddressException.h>
#include <folly/IPAddressV4.h>
#include <folly/IPAddressV6.h>
#include <folly/Range.h>
#include <folly/detail/IPAddress.h>
#include <folly/lang/Exception.h>

namespace folly {

class IPAddress;

/**
 * Pair of IPAddress, netmask
 */
CIDRNetwork;

class IPAddress {};

/**
 * `boost::hash` uses hash_value(), so this allows `boost::hash` to work
 * automatically for IPAddress
 */
std::size_t hash_value(const IPAddress& addr);

/**
 * Appends a string representation of the IP address to the stream using str().
 */
std::ostream& operator<<(std::ostream& os, const IPAddress& addr);

/**
 * @overloadbrief Define toAppend() to allow IPAddress to be used with
 * `folly::to<string>`
 */
void toAppend(IPAddress addr, std::string* result);
void toAppend(IPAddress addr, fbstring* result);

/**
 * Return true if two addresses are equal.
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 *
 * @return true if the two addresses are equal.
 */
bool operator==(const IPAddress& addr1, const IPAddress& addr2);

/**
 * Return true if `addr1 < addr2`
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 */
bool operator<(const IPAddress& addr1, const IPAddress& addr2);

/**
 * Return true if two address are not equal
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 */
inline bool operator!=(const IPAddress& addr1, const IPAddress& addr2) {}

/**
 * Return true if `addr1 > addr2`
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 */
inline bool operator>(const IPAddress& addr1, const IPAddress& addr2) {}

/**
 * Return true if `addr1 <= addr2`
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 */
inline bool operator<=(const IPAddress& addr1, const IPAddress& addr2) {}

/**
 * Return true if `addr1 >= addr2`
 *
 * V4-to-V6-mapped addresses are compared as V4 addresses.
 */
inline bool operator>=(const IPAddress& addr1, const IPAddress& addr2) {}

} // namespace folly

namespace std {
template <>
struct hash<folly::IPAddress> {};
} // namespace std