rippled
Loading...
Searching...
No Matches
IPEndpoint.h
1#ifndef BEAST_NET_IPENDPOINT_H_INCLUDED
2#define BEAST_NET_IPENDPOINT_H_INCLUDED
3
4#include <xrpl/beast/hash/hash_append.h>
5#include <xrpl/beast/hash/uhash.h>
6#include <xrpl/beast/net/IPAddress.h>
7
8#include <cstdint>
9#include <optional>
10#include <string>
11
12namespace beast {
13namespace IP {
14
16
19{
20public:
22 Endpoint();
23
25 explicit Endpoint(Address const& addr, Port port = 0);
26
33 static Endpoint
34 from_string(std::string const& s);
35
38 to_string() const;
39
41 Port
42 port() const
43 {
44 return m_port;
45 }
46
50 {
51 return Endpoint(m_addr, port);
52 }
53
55 Address const&
56 address() const
57 {
58 return m_addr;
59 }
60
63 bool
64 is_v4() const
65 {
66 return m_addr.is_v4();
67 }
68 bool
69 is_v6() const
70 {
71 return m_addr.is_v6();
72 }
73 AddressV4 const
74 to_v4() const
75 {
76 return m_addr.to_v4();
77 }
78 AddressV6 const
79 to_v6() const
80 {
81 return m_addr.to_v6();
82 }
87 friend bool
88 operator==(Endpoint const& lhs, Endpoint const& rhs);
89 friend bool
90 operator<(Endpoint const& lhs, Endpoint const& rhs);
91
92 friend bool
93 operator!=(Endpoint const& lhs, Endpoint const& rhs)
94 {
95 return !(lhs == rhs);
96 }
97 friend bool
98 operator>(Endpoint const& lhs, Endpoint const& rhs)
99 {
100 return rhs < lhs;
101 }
102 friend bool
103 operator<=(Endpoint const& lhs, Endpoint const& rhs)
104 {
105 return !(lhs > rhs);
106 }
107 friend bool
108 operator>=(Endpoint const& lhs, Endpoint const& rhs)
109 {
110 return !(rhs > lhs);
111 }
114 template <class Hasher>
115 friend void
116 hash_append(Hasher& h, Endpoint const& endpoint)
117 {
118 using ::beast::hash_append;
119 hash_append(h, endpoint.m_addr, endpoint.m_port);
120 }
121
122private:
125};
126
127//------------------------------------------------------------------------------
128
129// Properties
130
132inline bool
133is_loopback(Endpoint const& endpoint)
134{
135 return is_loopback(endpoint.address());
136}
137
139inline bool
140is_unspecified(Endpoint const& endpoint)
141{
142 return is_unspecified(endpoint.address());
143}
144
146inline bool
147is_multicast(Endpoint const& endpoint)
148{
149 return is_multicast(endpoint.address());
150}
151
153inline bool
154is_private(Endpoint const& endpoint)
155{
156 return is_private(endpoint.address());
157}
158
160inline bool
161is_public(Endpoint const& endpoint)
162{
163 return is_public(endpoint.address());
164}
165
166//------------------------------------------------------------------------------
167
169inline std::string
170to_string(Endpoint const& endpoint)
171{
172 return endpoint.to_string();
173}
174
176template <typename OutputStream>
177OutputStream&
178operator<<(OutputStream& os, Endpoint const& endpoint)
179{
180 os << to_string(endpoint);
181 return os;
182}
183
186operator>>(std::istream& is, Endpoint& endpoint);
187
188} // namespace IP
189} // namespace beast
190
191//------------------------------------------------------------------------------
192
193namespace std {
195template <>
197{
198 hash() = default;
199
201 operator()(::beast::IP::Endpoint const& endpoint) const
202 {
203 return ::beast::uhash<>{}(endpoint);
204 }
205};
206} // namespace std
207
208namespace boost {
210template <>
212{
213 hash() = default;
214
216 operator()(::beast::IP::Endpoint const& endpoint) const
217 {
218 return ::beast::uhash<>{}(endpoint);
219 }
220};
221} // namespace boost
222
223#endif
A version-independent IP address and port combination.
Definition IPEndpoint.h:19
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:56
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:98
Endpoint()
Create an unspecified endpoint.
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:93
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition IPEndpoint.h:116
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition IPEndpoint.h:49
AddressV4 const to_v4() const
Definition IPEndpoint.h:74
bool is_v4() const
Convenience accessors for the address part.
Definition IPEndpoint.h:64
AddressV6 const to_v6() const
Definition IPEndpoint.h:79
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:103
static Endpoint from_string(std::string const &s)
bool is_v6() const
Definition IPEndpoint.h:69
Port port() const
Returns the port number on the endpoint.
Definition IPEndpoint.h:42
std::string to_string() const
Returns a string representing the endpoint.
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition IPEndpoint.h:108
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:45
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition IPAddress.h:31
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:11
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:59
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition IPEndpoint.h:178
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition IPAddress.h:38
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:11
boost::asio::ip::address Address
Definition IPAddress.h:20
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition IPAddress.h:52
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition IPAddress.h:24
STL namespace.
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:216
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition IPEndpoint.h:201