xrpld
Loading...
Searching...
No Matches
IPAddress.h
1#pragma once
2
3#include <xrpl/beast/hash/hash_append.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/beast/net/IPAddressV4.h>
6#include <xrpl/beast/net/IPAddressV6.h>
7#include <xrpl/beast/utility/instrumentation.h>
8
9#include <boost/asio/ip/address.hpp>
10#include <boost/functional/hash.hpp>
11
12#include <cstddef>
13#include <string>
14
15//------------------------------------------------------------------------------
16
17namespace beast {
18namespace ip {
19
20using Address = boost::asio::ip::address;
21
25inline std::string
26to_string(Address const& addr)
27{
28 return addr.to_string();
29}
30
34inline bool
35isLoopback(Address const& addr)
36{
37 return addr.is_loopback();
38}
39
43inline bool
45{
46 return addr.is_unspecified();
47}
48
52inline bool
53isMulticast(Address const& addr)
54{
55 return addr.is_multicast();
56}
57
61inline bool
62isPrivate(Address const& addr)
63{
64 return (addr.is_v4()) ? isPrivate(addr.to_v4()) : isPrivate(addr.to_v6());
65}
66
70inline bool
71isPublic(Address const& addr)
72{
73 return (addr.is_v4()) ? isPublic(addr.to_v4()) : isPublic(addr.to_v6());
74}
75
76} // namespace ip
77
78//------------------------------------------------------------------------------
79
80template <class Hasher>
81void
82hash_append(Hasher& h, beast::ip::Address const& addr) noexcept
83{
85 if (addr.is_v4())
86 {
87 hash_append(h, addr.to_v4().to_bytes());
88 }
89 else if (addr.is_v6())
90 {
91 hash_append(h, addr.to_v6().to_bytes());
92 }
93 else
94 {
95 // LCOV_EXCL_START
96 UNREACHABLE("beast::hash_append : invalid address type");
97 // LCOV_EXCL_STOP
98 }
99}
100} // namespace beast
101
102namespace boost {
103template <>
105{
106 explicit hash() = default;
107
110 {
111 return ::beast::Uhash<>{}(addr);
112 }
113};
114} // namespace boost
bool isLoopback(Address const &addr)
Returns true if this is a loopback address.
Definition IPAddress.h:35
boost::asio::ip::address Address
Definition IPAddress.h:20
bool isPublic(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:71
bool isUnspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:44
bool isPrivate(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:62
bool isMulticast(Address const &addr)
Returns true if the address is a multicast address.
Definition IPAddress.h:53
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition IPAddress.h:26
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
std::size_t operator()(::beast::ip::Address const &addr) const
Definition IPAddress.h:109