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/net/IPAddress.h>
24 #include <ripple/beast/hash/hash_append.h>
25 #include <ripple/beast/hash/uhash.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> from_string_checked (std::string const& s);
53  static Endpoint from_string (std::string const& s);
54 
56  std::string to_string () const;
57 
59  Port port () const
60  { return m_port; }
61 
64  { return Endpoint (m_addr, port); }
65 
67  Address const& address () const
68  { return m_addr; }
69 
72  bool is_v4 () const
73  { return m_addr.is_v4(); }
74  bool is_v6 () const
75  { return m_addr.is_v6(); }
76  AddressV4 const to_v4 () const
77  { return m_addr.to_v4 (); }
78  AddressV6 const to_v6 () const
79  { return m_addr.to_v6 (); }
84  friend bool operator== (Endpoint const& lhs, Endpoint const& rhs);
85  friend bool operator< (Endpoint const& lhs, Endpoint const& rhs);
86 
87  friend bool operator!= (Endpoint const& lhs, Endpoint const& rhs)
88  { return ! (lhs == rhs); }
89  friend bool operator> (Endpoint const& lhs, Endpoint const& rhs)
90  { return rhs < lhs; }
91  friend bool operator<= (Endpoint const& lhs, Endpoint const& rhs)
92  { return ! (lhs > rhs); }
93  friend bool operator>= (Endpoint const& lhs, Endpoint const& rhs)
94  { return ! (rhs > lhs); }
97  template <class Hasher>
98  friend
99  void
100  hash_append (Hasher& h, Endpoint const& endpoint)
101  {
102  using ::beast::hash_append;
103  hash_append(h, endpoint.m_addr, endpoint.m_port);
104  }
105 
106 private:
109 };
110 
111 //------------------------------------------------------------------------------
112 
113 // Properties
114 
116 inline bool is_loopback (Endpoint const& endpoint)
117  { return is_loopback (endpoint.address ()); }
118 
120 inline bool is_unspecified (Endpoint const& endpoint)
121  { return is_unspecified (endpoint.address ()); }
122 
124 inline bool is_multicast (Endpoint const& endpoint)
125  { return is_multicast (endpoint.address ()); }
126 
128 inline bool is_private (Endpoint const& endpoint)
129  { return is_private (endpoint.address ()); }
130 
132 inline bool is_public (Endpoint const& endpoint)
133  { return is_public (endpoint.address ()); }
134 
135 //------------------------------------------------------------------------------
136 
138 inline std::string to_string (Endpoint const& endpoint)
139  { return endpoint.to_string(); }
140 
142 template <typename OutputStream>
143 OutputStream& operator<< (OutputStream& os, Endpoint const& endpoint)
144 {
145  os << to_string (endpoint);
146  return os;
147 }
148 
150 std::istream& operator>> (std::istream& is, Endpoint& endpoint);
151 
152 }
153 }
154 
155 //------------------------------------------------------------------------------
156 
157 namespace std {
159 template <>
161 {
162  explicit hash() = default;
163 
164  std::size_t operator() (::beast::IP::Endpoint const& endpoint) const
165  { return ::beast::uhash<>{} (endpoint); }
166 };
167 }
168 
169 namespace boost {
171 template <>
172 struct hash <::beast::IP::Endpoint>
173 {
174  explicit hash() = default;
175 
176  std::size_t operator() (::beast::IP::Endpoint const& endpoint) const
177  { return ::beast::uhash<>{} (endpoint); }
178 };
179 }
180 
181 #endif
std::hash::operator()
T operator()(T... args)
beast::IP::Endpoint::operator<=
friend bool operator<=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:91
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:100
beast::IP::operator>>
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:91
beast::IP::Endpoint::operator==
friend bool operator==(Endpoint const &lhs, Endpoint const &rhs)
Arithmetic comparison.
Definition: IPEndpoint.cpp:74
beast::IP::Endpoint::is_v6
bool is_v6() const
Definition: IPEndpoint.h:74
beast::IP::Endpoint::m_port
Port m_port
Definition: IPEndpoint.h:108
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:67
beast::IP::is_loopback
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:54
boost
Definition: IPAddress.h:127
beast::IP::is_multicast
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:70
beast::IP::Endpoint::operator>
friend bool operator>(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:89
beast::IP::is_private
bool is_private(AddressV4 const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddressV4.cpp:28
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:46
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:59
beast::IP::Endpoint::is_v4
bool is_v4() const
Convenience accessors for the address part.
Definition: IPEndpoint.h:72
cstdint
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:37
beast::IP::operator<<
OutputStream & operator<<(OutputStream &os, Endpoint const &endpoint)
Output stream conversion.
Definition: IPEndpoint.h:143
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:78
beast::IP::Endpoint::to_v4
const AddressV4 to_v4() const
Definition: IPEndpoint.h:76
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:47
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:107
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:37
beast::IP::Endpoint::at_port
Endpoint at_port(Port port) const
Returns a new Endpoint with a different port.
Definition: IPEndpoint.h:63
std::istream
STL class.
beast::IP::is_unspecified
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:62
beast::IP::Endpoint::operator!=
friend bool operator!=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:87
beast::IP::Endpoint::operator>=
friend bool operator>=(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.h:93
beast::IP::Endpoint::operator<
friend bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: IPEndpoint.cpp:80
std::hash
beast
Definition: base_uint.h:582
string