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 <string>
13
14//------------------------------------------------------------------------------
15
16namespace beast {
17namespace IP {
18
19using Address = boost::asio::ip::address;
20
22inline std::string
23to_string(Address const& addr)
24{
25 return addr.to_string();
26}
27
29inline bool
30isLoopback(Address const& addr)
31{
32 return addr.is_loopback();
33}
34
36inline bool
38{
39 return addr.is_unspecified();
40}
41
43inline bool
44isMulticast(Address const& addr)
45{
46 return addr.is_multicast();
47}
48
50inline bool
51isPrivate(Address const& addr)
52{
53 return (addr.is_v4()) ? isPrivate(addr.to_v4()) : isPrivate(addr.to_v6());
54}
55
57inline bool
58isPublic(Address const& addr)
59{
60 return (addr.is_v4()) ? isPublic(addr.to_v4()) : isPublic(addr.to_v6());
61}
62
63} // namespace IP
64
65//------------------------------------------------------------------------------
66
67template <class Hasher>
68void
69hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
70{
72 if (addr.is_v4())
73 {
74 hash_append(h, addr.to_v4().to_bytes());
75 }
76 else if (addr.is_v6())
77 {
78 hash_append(h, addr.to_v6().to_bytes());
79 }
80 else
81 {
82 // LCOV_EXCL_START
83 UNREACHABLE("beast::hash_append : invalid address type");
84 // LCOV_EXCL_STOP
85 }
86}
87} // namespace beast
88
89namespace boost {
90template <>
92{
93 explicit hash() = default;
94
97 {
98 return ::beast::Uhash<>{}(addr);
99 }
100};
101} // namespace boost
bool isMulticast(Address const &addr)
Returns true if the address is a multicast address.
Definition IPAddress.h:44
bool isPublic(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:58
bool isLoopback(Address const &addr)
Returns true if this is a loopback address.
Definition IPAddress.h:30
bool isUnspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:37
bool isPrivate(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:51
boost::asio::ip::address Address
Definition IPAddress.h:19
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition IPAddress.h:23
std::enable_if_t< IsContiguouslyHashable< T, Hasher >::value > 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:96