xrpld
Loading...
Searching...
No Matches
IPEndpoint.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/IPAddress.h>
6
7#include <cstdint>
8#include <optional>
9#include <string>
10
11namespace beast::IP {
12
14
17{
18public:
20 Endpoint();
21
23 explicit Endpoint(Address addr, Port port = 0);
24
31 static Endpoint
32 fromString(std::string const& s);
33
35 [[nodiscard]] std::string
36 toString() const;
37
39 [[nodiscard]] Port
40 port() const
41 {
42 return port_;
43 }
44
46 [[nodiscard]] Endpoint
48 {
49 return Endpoint(addr_, port);
50 }
51
53 [[nodiscard]] Address const&
54 address() const
55 {
56 return addr_;
57 }
58
61 [[nodiscard]] bool
62 isV4() const
63 {
64 return addr_.is_v4();
65 }
66 [[nodiscard]] bool
67 isV6() const
68 {
69 return addr_.is_v6();
70 }
71 [[nodiscard]] AddressV4
72 toV4() const
73 {
74 return addr_.to_v4();
75 }
76 [[nodiscard]] AddressV6
77 toV6() const
78 {
79 return addr_.to_v6();
80 }
81
82
85 friend bool
86 operator==(Endpoint const& lhs, Endpoint const& rhs);
87 friend bool
88 operator<(Endpoint const& lhs, Endpoint const& rhs);
89
90 friend bool
91 operator!=(Endpoint const& lhs, Endpoint const& rhs)
92 {
93 return !(lhs == rhs);
94 }
95 friend bool
96 operator>(Endpoint const& lhs, Endpoint const& rhs)
97 {
98 return rhs < lhs;
99 }
100 friend bool
101 operator<=(Endpoint const& lhs, Endpoint const& rhs)
102 {
103 return !(lhs > rhs);
104 }
105 friend bool
106 operator>=(Endpoint const& lhs, Endpoint const& rhs)
107 {
108 return !(rhs > lhs);
109 }
110
111
112 template <class Hasher>
113 friend void
114 hash_append(Hasher& h, Endpoint const& endpoint)
115 {
116 using ::beast::hash_append;
117 hash_append(h, endpoint.addr_, endpoint.port_);
118 }
119
120private:
123};
124
125//------------------------------------------------------------------------------
126
127// Properties
128
130inline bool
131isLoopback(Endpoint const& endpoint)
132{
133 return isLoopback(endpoint.address());
134}
135
137inline bool
138isUnspecified(Endpoint const& endpoint)
139{
140 return isUnspecified(endpoint.address());
141}
142
144inline bool
145isMulticast(Endpoint const& endpoint)
146{
147 return isMulticast(endpoint.address());
148}
149
151inline bool
152isPrivate(Endpoint const& endpoint)
153{
154 return isPrivate(endpoint.address());
155}
156
158inline bool
159isPublic(Endpoint const& endpoint)
160{
161 return isPublic(endpoint.address());
162}
163
164//------------------------------------------------------------------------------
165
167inline std::string
168to_string(Endpoint const& endpoint)
169{
170 return endpoint.toString();
171}
172
174template <typename OutputStream>
175OutputStream&
176operator<<(OutputStream& os, Endpoint const& endpoint)
177{
178 os << to_string(endpoint);
179 return os;
180}
181
184operator>>(std::istream& is, Endpoint& endpoint);
185
186} // namespace beast::IP
187
188//------------------------------------------------------------------------------
189
190namespace std {
192template <>
194{
195 hash() = default;
196
198 operator()(::beast::IP::Endpoint const& endpoint) const
199 {
200 return ::beast::Uhash<>{}(endpoint);
201 }
202};
203} // namespace std
204
205namespace boost {
207template <>
209{
210 hash() = default;
211
213 operator()(::beast::IP::Endpoint const& endpoint) const
214 {
215 return ::beast::Uhash<>{}(endpoint);
216 }
217};
218} // namespace boost
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
bool isV4() const
Convenience accessors for the address part.
Definition IPEndpoint.h:62
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:54
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:96
AddressV4 toV4() const
Definition IPEndpoint.h:72
static std::optional< Endpoint > fromStringChecked(std::string const &s)
Create an Endpoint from a string.
Endpoint()
Create an unspecified endpoint.
bool isV6() const
Definition IPEndpoint.h:67
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:91
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Endpoint atPort(Port port) const
Returns a new Endpoint with a different port.
Definition IPEndpoint.h:47
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition IPEndpoint.h:114
std::string toString() const
Returns a string representing the endpoint.
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:101
AddressV6 toV6() const
Definition IPEndpoint.h:77
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:40
static Endpoint fromString(std::string const &s)
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:106
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
bool isMulticast(Address const &addr)
Returns true if the address is a multicast address.
Definition IPAddress.h:44
std::uint16_t Port
Definition IPEndpoint.h:13
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
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:9
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition IPEndpoint.h:176
bool isUnspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:37
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
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
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:9
std::enable_if_t< IsContiguouslyHashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
STL namespace.
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:213
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:198