rippled
Loading...
Searching...
No Matches
IPEndpoint.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_NET_IPENDPOINT_H_INCLUDED
21#define BEAST_NET_IPENDPOINT_H_INCLUDED
22
23#include <xrpl/beast/hash/hash_append.h>
24#include <xrpl/beast/hash/uhash.h>
25#include <xrpl/beast/net/IPAddress.h>
26
27#include <cstdint>
28#include <optional>
29#include <string>
30
31namespace beast {
32namespace IP {
33
35
38{
39public:
41 Endpoint();
42
44 explicit Endpoint(Address const& addr, Port port = 0);
45
52 static Endpoint
53 from_string(std::string const& s);
54
57 to_string() const;
58
60 Port
61 port() const
62 {
63 return m_port;
64 }
65
69 {
70 return Endpoint(m_addr, port);
71 }
72
74 Address const&
75 address() const
76 {
77 return m_addr;
78 }
79
82 bool
83 is_v4() const
84 {
85 return m_addr.is_v4();
86 }
87 bool
88 is_v6() const
89 {
90 return m_addr.is_v6();
91 }
92 AddressV4 const
93 to_v4() const
94 {
95 return m_addr.to_v4();
96 }
97 AddressV6 const
98 to_v6() const
99 {
100 return m_addr.to_v6();
101 }
106 friend bool
107 operator==(Endpoint const& lhs, Endpoint const& rhs);
108 friend bool
109 operator<(Endpoint const& lhs, Endpoint const& rhs);
110
111 friend bool
112 operator!=(Endpoint const& lhs, Endpoint const& rhs)
113 {
114 return !(lhs == rhs);
115 }
116 friend bool
117 operator>(Endpoint const& lhs, Endpoint const& rhs)
118 {
119 return rhs < lhs;
120 }
121 friend bool
122 operator<=(Endpoint const& lhs, Endpoint const& rhs)
123 {
124 return !(lhs > rhs);
125 }
126 friend bool
127 operator>=(Endpoint const& lhs, Endpoint const& rhs)
128 {
129 return !(rhs > lhs);
130 }
133 template <class Hasher>
134 friend void
135 hash_append(Hasher& h, Endpoint const& endpoint)
136 {
137 using ::beast::hash_append;
138 hash_append(h, endpoint.m_addr, endpoint.m_port);
139 }
140
141private:
144};
145
146//------------------------------------------------------------------------------
147
148// Properties
149
151inline bool
152is_loopback(Endpoint const& endpoint)
153{
154 return is_loopback(endpoint.address());
155}
156
158inline bool
159is_unspecified(Endpoint const& endpoint)
160{
161 return is_unspecified(endpoint.address());
162}
163
165inline bool
166is_multicast(Endpoint const& endpoint)
167{
168 return is_multicast(endpoint.address());
169}
170
172inline bool
173is_private(Endpoint const& endpoint)
174{
175 return is_private(endpoint.address());
176}
177
179inline bool
180is_public(Endpoint const& endpoint)
181{
182 return is_public(endpoint.address());
183}
184
185//------------------------------------------------------------------------------
186
188inline std::string
189to_string(Endpoint const& endpoint)
190{
191 return endpoint.to_string();
192}
193
195template <typename OutputStream>
196OutputStream&
197operator<<(OutputStream& os, Endpoint const& endpoint)
198{
199 os << to_string(endpoint);
200 return os;
201}
202
205operator>>(std::istream& is, Endpoint& endpoint);
206
207} // namespace IP
208} // namespace beast
209
210//------------------------------------------------------------------------------
211
212namespace std {
214template <>
216{
217 hash() = default;
218
220 operator()(::beast::IP::Endpoint const& endpoint) const
221 {
222 return ::beast::uhash<>{}(endpoint);
223 }
224};
225} // namespace std
226
227namespace boost {
229template <>
231{
232 hash() = default;
233
235 operator()(::beast::IP::Endpoint const& endpoint) const
236 {
237 return ::beast::uhash<>{}(endpoint);
238 }
239};
240} // namespace boost
241
242#endif
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:75
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Definition: IPEndpoint.cpp:45
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:117
Endpoint()
Create an unspecified endpoint.
Definition: IPEndpoint.cpp:36
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:112
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.cpp:94
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition: IPEndpoint.h:135
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition: IPEndpoint.h:68
AddressV4 const to_v4() const
Definition: IPEndpoint.h:93
bool is_v4() const
Convenience accessors for the address part.
Definition: IPEndpoint.h:83
AddressV6 const to_v6() const
Definition: IPEndpoint.h:98
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:122
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:59
bool is_v6() const
Definition: IPEndpoint.h:88
Port port() const
Returns the port number on the endpoint.
Definition: IPEndpoint.h:61
std::string to_string() const
Returns a string representing the endpoint.
Definition: IPEndpoint.cpp:67
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:127
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
Definition: IPEndpoint.cpp:88
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:64
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:50
boost::asio::ip::address_v6 AddressV6
Definition: IPAddressV6.h:30
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition: IPAddress.h:78
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition: IPEndpoint.h:197
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:57
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:106
boost::asio::ip::address_v4 AddressV4
Definition: IPAddressV4.h:30
boost::asio::ip::address Address
Definition: IPAddress.h:39
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddress.h:71
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition: IPAddress.h:43
STL namespace.
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:235
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:220