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 <ios>
29#include <optional>
30#include <string>
31
32namespace beast {
33namespace IP {
34
36
39{
40public:
42 Endpoint();
43
45 explicit Endpoint(Address const& addr, Port port = 0);
46
53 static Endpoint
54 from_string(std::string const& s);
55
58 to_string() const;
59
61 Port
62 port() const
63 {
64 return m_port;
65 }
66
70 {
71 return Endpoint(m_addr, port);
72 }
73
75 Address const&
76 address() const
77 {
78 return m_addr;
79 }
80
83 bool
84 is_v4() const
85 {
86 return m_addr.is_v4();
87 }
88 bool
89 is_v6() const
90 {
91 return m_addr.is_v6();
92 }
93 AddressV4 const
94 to_v4() const
95 {
96 return m_addr.to_v4();
97 }
98 AddressV6 const
99 to_v6() const
100 {
101 return m_addr.to_v6();
102 }
107 friend bool
108 operator==(Endpoint const& lhs, Endpoint const& rhs);
109 friend bool
110 operator<(Endpoint const& lhs, Endpoint const& rhs);
111
112 friend bool
113 operator!=(Endpoint const& lhs, Endpoint const& rhs)
114 {
115 return !(lhs == rhs);
116 }
117 friend bool
118 operator>(Endpoint const& lhs, Endpoint const& rhs)
119 {
120 return rhs < lhs;
121 }
122 friend bool
123 operator<=(Endpoint const& lhs, Endpoint const& rhs)
124 {
125 return !(lhs > rhs);
126 }
127 friend bool
128 operator>=(Endpoint const& lhs, Endpoint const& rhs)
129 {
130 return !(rhs > lhs);
131 }
134 template <class Hasher>
135 friend void
136 hash_append(Hasher& h, Endpoint const& endpoint)
137 {
138 using ::beast::hash_append;
139 hash_append(h, endpoint.m_addr, endpoint.m_port);
140 }
141
142private:
145};
146
147//------------------------------------------------------------------------------
148
149// Properties
150
152inline bool
153is_loopback(Endpoint const& endpoint)
154{
155 return is_loopback(endpoint.address());
156}
157
159inline bool
160is_unspecified(Endpoint const& endpoint)
161{
162 return is_unspecified(endpoint.address());
163}
164
166inline bool
167is_multicast(Endpoint const& endpoint)
168{
169 return is_multicast(endpoint.address());
170}
171
173inline bool
174is_private(Endpoint const& endpoint)
175{
176 return is_private(endpoint.address());
177}
178
180inline bool
181is_public(Endpoint const& endpoint)
182{
183 return is_public(endpoint.address());
184}
185
186//------------------------------------------------------------------------------
187
189inline std::string
190to_string(Endpoint const& endpoint)
191{
192 return endpoint.to_string();
193}
194
196template <typename OutputStream>
197OutputStream&
198operator<<(OutputStream& os, Endpoint const& endpoint)
199{
200 os << to_string(endpoint);
201 return os;
202}
203
206operator>>(std::istream& is, Endpoint& endpoint);
207
208} // namespace IP
209} // namespace beast
210
211//------------------------------------------------------------------------------
212
213namespace std {
215template <>
217{
218 explicit hash() = default;
219
221 operator()(::beast::IP::Endpoint const& endpoint) const
222 {
223 return ::beast::uhash<>{}(endpoint);
224 }
225};
226} // namespace std
227
228namespace boost {
230template <>
232{
233 explicit hash() = default;
234
236 operator()(::beast::IP::Endpoint const& endpoint) const
237 {
238 return ::beast::uhash<>{}(endpoint);
239 }
240};
241} // namespace boost
242
243#endif
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:76
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:118
Endpoint()
Create an unspecified endpoint.
Definition: IPEndpoint.cpp:36
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:113
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:136
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition: IPEndpoint.h:69
AddressV4 const to_v4() const
Definition: IPEndpoint.h:94
bool is_v4() const
Convenience accessors for the address part.
Definition: IPEndpoint.h:84
AddressV6 const to_v6() const
Definition: IPEndpoint.h:99
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:123
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:59
bool is_v6() const
Definition: IPEndpoint.h:89
Port port() const
Returns the port number on the endpoint.
Definition: IPEndpoint.h:62
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:128
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:68
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:54
boost::asio::ip::address_v6 AddressV6
Definition: IPAddressV6.h:36
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition: IPAddress.h:82
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition: IPEndpoint.h:198
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:61
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:106
boost::asio::ip::address_v4 AddressV4
Definition: IPAddressV4.h:36
boost::asio::ip::address Address
Definition: IPAddress.h:43
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddress.h:75
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition: IPAddress.h:47
STL namespace.
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:236
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:221