rippled
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 <ripple/beast/hash/hash_append.h>
24 #include <ripple/beast/hash/uhash.h>
25 #include <ripple/beast/net/IPAddress.h>
26 
27 #include <cstdint>
28 #include <ios>
29 #include <optional>
30 #include <string>
31 
32 namespace beast {
33 namespace IP {
34 
36 
38 class Endpoint
39 {
40 public:
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 
68  Endpoint
69  at_port(Port port) const
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 
142 private:
145 };
146 
147 //------------------------------------------------------------------------------
148 
149 // Properties
150 
152 inline bool
153 is_loopback(Endpoint const& endpoint)
154 {
155  return is_loopback(endpoint.address());
156 }
157 
159 inline bool
160 is_unspecified(Endpoint const& endpoint)
161 {
162  return is_unspecified(endpoint.address());
163 }
164 
166 inline bool
167 is_multicast(Endpoint const& endpoint)
168 {
169  return is_multicast(endpoint.address());
170 }
171 
173 inline bool
174 is_private(Endpoint const& endpoint)
175 {
176  return is_private(endpoint.address());
177 }
178 
180 inline bool
181 is_public(Endpoint const& endpoint)
182 {
183  return is_public(endpoint.address());
184 }
185 
186 //------------------------------------------------------------------------------
187 
189 inline std::string
190 to_string(Endpoint const& endpoint)
191 {
192  return endpoint.to_string();
193 }
194 
196 template <typename OutputStream>
197 OutputStream&
198 operator<<(OutputStream& os, Endpoint const& endpoint)
199 {
200  os << to_string(endpoint);
201  return os;
202 }
203 
206 operator>>(std::istream& is, Endpoint& endpoint);
207 
208 } // namespace IP
209 } // namespace beast
210 
211 //------------------------------------------------------------------------------
212 
213 namespace std {
215 template <>
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 
228 namespace boost {
230 template <>
231 struct hash<::beast::IP::Endpoint>
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
beast::IP::Endpoint::operator<=
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:123
std::string
STL class.
beast::IP::Endpoint::to_string
std::string to_string() const
Returns a string representing the endpoint.
Definition: IPEndpoint.cpp:57
beast::IP::Endpoint::hash_append
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition: IPEndpoint.h:136
beast::IP::operator>>
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:96
beast::IP::Endpoint::operator==
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
Definition: IPEndpoint.cpp:78
beast::IP::Endpoint::is_v6
bool is_v6() const
Definition: IPEndpoint.h:89
beast::IP::Endpoint::m_port
Port m_port
Definition: IPEndpoint.h:144
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:76
beast::IP::is_loopback
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:52
boost
Definition: IPAddress.h:103
beast::IP::is_multicast
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:66
beast::IP::Endpoint::operator>
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:118
beast::IP::is_private
bool is_private(AddressV4 const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddressV4.cpp:29
beast::IP::Address
boost::asio::ip::address Address
Definition: IPAddress.h:41
beast::IP::to_string
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition: IPAddress.h:45
beast::IP::Endpoint::Endpoint
Endpoint()
Create an unspecified endpoint.
Definition: IPEndpoint.cpp:26
beast::IP::Endpoint::port
Port port() const
Returns the port number on the endpoint.
Definition: IPEndpoint.h:62
beast::IP::Endpoint::is_v4
bool is_v4() const
Convenience accessors for the address part.
Definition: IPEndpoint.h:84
cstdint
boost::hash<::beast::IP::Endpoint >::operator()
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:236
std::uint16_t
beast::IP::is_public
bool is_public(AddressV4 const &addr)
Returns true if the address is a public routable address.
Definition: IPAddressV4.cpp:41
beast::IP::operator<<
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition: IPEndpoint.h:198
beast::IP::AddressV6
boost::asio::ip::address_v6 AddressV6
Definition: IPAddressV6.h:34
beast::IP::Endpoint::to_v6
const AddressV6 to_v6() const
Definition: IPEndpoint.h:99
beast::IP::Endpoint::to_v4
const AddressV4 to_v4() const
Definition: IPEndpoint.h:94
beast::IP::AddressV4
boost::asio::ip::address_v4 AddressV4
Definition: IPAddressV4.h:34
std
STL namespace.
beast::IP::Endpoint::from_string
static Endpoint from_string(std::string const &s)
Definition: IPEndpoint.cpp:49
optional
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
beast::IP::Endpoint::m_addr
Address m_addr
Definition: IPEndpoint.h:143
ios
beast::IP::Endpoint::at_port
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition: IPEndpoint.h:69
std::istream
STL class.
beast::IP::is_unspecified
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:59
std::hash<::beast::IP::Endpoint >::operator()
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:221
beast::IP::Endpoint::operator!=
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:113
beast::IP::Endpoint::operator>=
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:128
beast::IP::Endpoint::from_string_checked
static std::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Definition: IPEndpoint.cpp:35
beast::IP::Endpoint::operator<
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.cpp:84
std::hash
beast
Definition: base_uint.h:641
string