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 <boost/optional.hpp>
28 
29 #include <cstdint>
30 #include <ios>
31 #include <string>
32 
33 namespace beast {
34 namespace IP {
35 
37 
39 class Endpoint
40 {
41 public:
43  Endpoint();
44 
46  explicit Endpoint(Address const& addr, Port port = 0);
47 
52  static boost::optional<Endpoint>
54  static Endpoint
55  from_string(std::string const& s);
56 
59  to_string() const;
60 
62  Port
63  port() const
64  {
65  return m_port;
66  }
67 
69  Endpoint
70  at_port(Port port) const
71  {
72  return Endpoint(m_addr, port);
73  }
74 
76  Address const&
77  address() const
78  {
79  return m_addr;
80  }
81 
84  bool
85  is_v4() const
86  {
87  return m_addr.is_v4();
88  }
89  bool
90  is_v6() const
91  {
92  return m_addr.is_v6();
93  }
94  AddressV4 const
95  to_v4() const
96  {
97  return m_addr.to_v4();
98  }
99  AddressV6 const
100  to_v6() const
101  {
102  return m_addr.to_v6();
103  }
108  friend bool
109  operator==(Endpoint const& lhs, Endpoint const& rhs);
110  friend bool
111  operator<(Endpoint const& lhs, Endpoint const& rhs);
112 
113  friend bool
114  operator!=(Endpoint const& lhs, Endpoint const& rhs)
115  {
116  return !(lhs == rhs);
117  }
118  friend bool
119  operator>(Endpoint const& lhs, Endpoint const& rhs)
120  {
121  return rhs < lhs;
122  }
123  friend bool
124  operator<=(Endpoint const& lhs, Endpoint const& rhs)
125  {
126  return !(lhs > rhs);
127  }
128  friend bool
129  operator>=(Endpoint const& lhs, Endpoint const& rhs)
130  {
131  return !(rhs > lhs);
132  }
135  template <class Hasher>
136  friend void
137  hash_append(Hasher& h, Endpoint const& endpoint)
138  {
139  using ::beast::hash_append;
140  hash_append(h, endpoint.m_addr, endpoint.m_port);
141  }
142 
143 private:
146 };
147 
148 //------------------------------------------------------------------------------
149 
150 // Properties
151 
153 inline bool
154 is_loopback(Endpoint const& endpoint)
155 {
156  return is_loopback(endpoint.address());
157 }
158 
160 inline bool
161 is_unspecified(Endpoint const& endpoint)
162 {
163  return is_unspecified(endpoint.address());
164 }
165 
167 inline bool
168 is_multicast(Endpoint const& endpoint)
169 {
170  return is_multicast(endpoint.address());
171 }
172 
174 inline bool
175 is_private(Endpoint const& endpoint)
176 {
177  return is_private(endpoint.address());
178 }
179 
181 inline bool
182 is_public(Endpoint const& endpoint)
183 {
184  return is_public(endpoint.address());
185 }
186 
187 //------------------------------------------------------------------------------
188 
190 inline std::string
191 to_string(Endpoint const& endpoint)
192 {
193  return endpoint.to_string();
194 }
195 
197 template <typename OutputStream>
198 OutputStream&
199 operator<<(OutputStream& os, Endpoint const& endpoint)
200 {
201  os << to_string(endpoint);
202  return os;
203 }
204 
207 operator>>(std::istream& is, Endpoint& endpoint);
208 
209 } // namespace IP
210 } // namespace beast
211 
212 //------------------------------------------------------------------------------
213 
214 namespace std {
216 template <>
218 {
219  explicit hash() = default;
220 
222  operator()(::beast::IP::Endpoint const& endpoint) const
223  {
224  return ::beast::uhash<>{}(endpoint);
225  }
226 };
227 } // namespace std
228 
229 namespace boost {
231 template <>
232 struct hash<::beast::IP::Endpoint>
233 {
234  explicit hash() = default;
235 
237  operator()(::beast::IP::Endpoint const& endpoint) const
238  {
239  return ::beast::uhash<>{}(endpoint);
240  }
241 };
242 } // namespace boost
243 
244 #endif
beast::IP::Endpoint::operator<=
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:124
std::string
STL class.
beast::IP::Endpoint::to_string
std::string to_string() const
Returns a string representing the endpoint.
Definition: IPEndpoint.cpp:54
beast::IP::Endpoint::hash_append
friend void hash_append(Hasher &h, Endpoint const &endpoint)
Definition: IPEndpoint.h:137
beast::IP::operator>>
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:93
beast::IP::Endpoint::operator==
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
Definition: IPEndpoint.cpp:75
beast::IP::Endpoint::is_v6
bool is_v6() const
Definition: IPEndpoint.h:90
beast::IP::Endpoint::m_port
Port m_port
Definition: IPEndpoint.h:145
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:77
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:117
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:119
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:63
beast::IP::Endpoint::is_v4
bool is_v4() const
Convenience accessors for the address part.
Definition: IPEndpoint.h:85
cstdint
boost::hash<::beast::IP::Endpoint >::operator()
std::size_t operator()(::beast::IP::Endpoint const &endpoint) const
Definition: IPEndpoint.h:237
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:199
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:100
beast::IP::Endpoint::to_v4
const AddressV4 to_v4() const
Definition: IPEndpoint.h:95
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:46
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
beast::IP::Endpoint::m_addr
Address m_addr
Definition: IPEndpoint.h:144
ios
beast::IP::Endpoint::from_string_checked
static boost::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Definition: IPEndpoint.cpp:35
beast::IP::Endpoint::at_port
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition: IPEndpoint.h:70
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:222
beast::IP::Endpoint::operator!=
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:114
beast::IP::Endpoint::operator>=
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:129
beast::IP::Endpoint::operator<
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.cpp:81
std::hash
beast
Definition: base_uint.h:585
string