rippled
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 {
12namespace IP {
13
15
18{
19public:
21 Endpoint();
22
24 explicit Endpoint(Address const& addr, Port port = 0);
25
32 static Endpoint
33 from_string(std::string const& s);
34
37 to_string() const;
38
40 Port
41 port() const
42 {
43 return m_port;
44 }
45
49 {
50 return Endpoint(m_addr, port);
51 }
52
54 Address const&
55 address() const
56 {
57 return m_addr;
58 }
59
62 bool
63 is_v4() const
64 {
65 return m_addr.is_v4();
66 }
67 bool
68 is_v6() const
69 {
70 return m_addr.is_v6();
71 }
72 AddressV4 const
73 to_v4() const
74 {
75 return m_addr.to_v4();
76 }
77 AddressV6 const
78 to_v6() const
79 {
80 return m_addr.to_v6();
81 }
86 friend bool
87 operator==(Endpoint const& lhs, Endpoint const& rhs);
88 friend bool
89 operator<(Endpoint const& lhs, Endpoint const& rhs);
90
91 friend bool
92 operator!=(Endpoint const& lhs, Endpoint const& rhs)
93 {
94 return !(lhs == rhs);
95 }
96 friend bool
97 operator>(Endpoint const& lhs, Endpoint const& rhs)
98 {
99 return rhs < lhs;
100 }
101 friend bool
102 operator<=(Endpoint const& lhs, Endpoint const& rhs)
103 {
104 return !(lhs > rhs);
105 }
106 friend bool
107 operator>=(Endpoint const& lhs, Endpoint const& rhs)
108 {
109 return !(rhs > lhs);
110 }
113 template <class Hasher>
114 friend void
115 hash_append(Hasher& h, Endpoint const& endpoint)
116 {
117 using ::beast::hash_append;
118 hash_append(h, endpoint.m_addr, endpoint.m_port);
119 }
120
121private:
124};
125
126//------------------------------------------------------------------------------
127
128// Properties
129
131inline bool
132is_loopback(Endpoint const& endpoint)
133{
134 return is_loopback(endpoint.address());
135}
136
138inline bool
139is_unspecified(Endpoint const& endpoint)
140{
141 return is_unspecified(endpoint.address());
142}
143
145inline bool
146is_multicast(Endpoint const& endpoint)
147{
148 return is_multicast(endpoint.address());
149}
150
152inline bool
153is_private(Endpoint const& endpoint)
154{
155 return is_private(endpoint.address());
156}
157
159inline bool
160is_public(Endpoint const& endpoint)
161{
162 return is_public(endpoint.address());
163}
164
165//------------------------------------------------------------------------------
166
168inline std::string
169to_string(Endpoint const& endpoint)
170{
171 return endpoint.to_string();
172}
173
175template <typename OutputStream>
176OutputStream&
177operator<<(OutputStream& os, Endpoint const& endpoint)
178{
179 os << to_string(endpoint);
180 return os;
181}
182
185operator>>(std::istream& is, Endpoint& endpoint);
186
187} // namespace IP
188} // namespace beast
189
190//------------------------------------------------------------------------------
191
192namespace std {
194template <>
196{
197 hash() = default;
198
200 operator()(::beast::IP::Endpoint const& endpoint) const
201 {
202 return ::beast::uhash<>{}(endpoint);
203 }
204};
205} // namespace std
206
207namespace boost {
209template <>
211{
212 hash() = default;
213
215 operator()(::beast::IP::Endpoint const& endpoint) const
216 {
217 return ::beast::uhash<>{}(endpoint);
218 }
219};
220} // namespace boost
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:55
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:97
Endpoint()
Create an unspecified endpoint.
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:92
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition IPEndpoint.h:115
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition IPEndpoint.h:48
AddressV4 const to_v4() const
Definition IPEndpoint.h:73
bool is_v4() const
Convenience accessors for the address part.
Definition IPEndpoint.h:63
AddressV6 const to_v6() const
Definition IPEndpoint.h:78
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:102
static Endpoint from_string(std::string const &s)
bool is_v6() const
Definition IPEndpoint.h:68
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:41
std::string to_string() const
Returns a string representing the endpoint.
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:107
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition IPAddress.h:44
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition IPAddress.h:30
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:10
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:58
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition IPEndpoint.h:177
bool is_unspecified(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.
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:10
boost::asio::ip::address Address
Definition IPAddress.h:19
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:51
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition IPAddress.h:23
STL namespace.
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:215
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:200