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#include <xrpl/beast/net/IPAddressV4.h>
7#include <xrpl/beast/net/IPAddressV6.h>
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <istream>
13#include <optional>
14#include <string>
15
16namespace beast::ip {
17
19
24{
25public:
29 Endpoint();
30
34 explicit Endpoint(Address addr, Port port = 0);
35
43 static Endpoint
44 fromString(std::string const& s);
45
49 [[nodiscard]] std::string
50 toString() const;
51
55 [[nodiscard]] Port
56 port() const
57 {
58 return port_;
59 }
60
64 [[nodiscard]] Endpoint
66 {
67 return Endpoint(addr_, port);
68 }
69
73 [[nodiscard]] Address const&
74 address() const
75 {
76 return addr_;
77 }
78
83 [[nodiscard]] bool
84 isV4() const
85 {
86 return addr_.is_v4();
87 }
88 [[nodiscard]] bool
89 isV6() const
90 {
91 return addr_.is_v6();
92 }
93 [[nodiscard]] AddressV4
94 toV4() const
95 {
96 return addr_.to_v4();
97 }
98 [[nodiscard]] AddressV6
99 toV6() const
100 {
101 return addr_.to_v6();
102 }
103
104
109 friend bool
110 operator==(Endpoint const& lhs, Endpoint const& rhs);
111 friend bool
112 operator<(Endpoint const& lhs, Endpoint const& rhs);
113 friend bool
114 operator>(Endpoint const& lhs, Endpoint const& rhs)
115 {
116 return rhs < lhs;
117 }
118 friend bool
119 operator<=(Endpoint const& lhs, Endpoint const& rhs)
120 {
121 return !(lhs > rhs);
122 }
123 friend bool
124 operator>=(Endpoint const& lhs, Endpoint const& rhs)
125 {
126 return !(rhs > lhs);
127 }
128
129
130 template <class Hasher>
131 friend void
132 hash_append(Hasher& h, Endpoint const& endpoint)
133 {
134 using ::beast::hash_append;
135 hash_append(h, endpoint.addr_, endpoint.port_);
136 }
137
138private:
141};
142
143//------------------------------------------------------------------------------
144
145// Properties
146
150inline bool
151isLoopback(Endpoint const& endpoint)
152{
153 return isLoopback(endpoint.address());
154}
155
159inline bool
160isUnspecified(Endpoint const& endpoint)
161{
162 return isUnspecified(endpoint.address());
163}
164
168inline bool
169isMulticast(Endpoint const& endpoint)
170{
171 return isMulticast(endpoint.address());
172}
173
177inline bool
178isPrivate(Endpoint const& endpoint)
179{
180 return isPrivate(endpoint.address());
181}
182
186inline bool
187isPublic(Endpoint const& endpoint)
188{
189 return isPublic(endpoint.address());
190}
191
192//------------------------------------------------------------------------------
193
197inline std::string
198to_string(Endpoint const& endpoint)
199{
200 return endpoint.toString();
201}
202
206template <typename OutputStream>
207OutputStream&
208operator<<(OutputStream& os, Endpoint const& endpoint)
209{
210 os << to_string(endpoint);
211 return os;
212}
213
218operator>>(std::istream& is, Endpoint& endpoint);
219
220} // namespace beast::ip
221
222//------------------------------------------------------------------------------
223
224namespace std {
228template <>
230{
231 hash() = default;
232
234 operator()(::beast::ip::Endpoint const& endpoint) const
235 {
236 return ::beast::Uhash<>{}(endpoint);
237 }
238};
239} // namespace std
240
241namespace boost {
245template <>
247{
248 hash() = default;
249
251 operator()(::beast::ip::Endpoint const& endpoint) const
252 {
253 return ::beast::Uhash<>{}(endpoint);
254 }
255};
256} // namespace boost
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:114
std::string toString() const
Returns a string representing the endpoint.
AddressV4 toV4() const
Definition IPEndpoint.h:94
Endpoint()
Create an unspecified endpoint.
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
bool isV6() const
Definition IPEndpoint.h:89
static std::optional< Endpoint > fromStringChecked(std::string const &s)
Create an Endpoint from a string.
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition IPEndpoint.h:132
AddressV6 toV6() const
Definition IPEndpoint.h:99
bool isV4() const
Convenience accessors for the address part.
Definition IPEndpoint.h:84
Endpoint atPort(Port port) const
Returns a new Endpoint with a different port.
Definition IPEndpoint.h:65
static Endpoint fromString(std::string const &s)
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:119
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:56
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:74
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:124
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
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
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:7
bool isUnspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:44
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:7
bool isPrivate(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:62
std::uint16_t Port
Definition IPEndpoint.h:18
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
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition IPEndpoint.h:208
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
void 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:251
std::size_t operator()(::beast::ip::Endpoint const &endpoint) const
Definition IPEndpoint.h:234