1 // 2 // ip/basic_endpoint.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 // 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9 // 10 11 #ifndef BOOST_ASIO_IP_BASIC_ENDPOINT_HPP 12 #define BOOST_ASIO_IP_BASIC_ENDPOINT_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 #include <boost/asio/detail/cstdint.hpp> 20 #include <boost/asio/ip/address.hpp> 21 #include <boost/asio/ip/detail/endpoint.hpp> 22 23 #if defined(BOOST_ASIO_HAS_STD_HASH) 24 # include <functional> 25 #endif // defined(BOOST_ASIO_HAS_STD_HASH) 26 27 #if !defined(BOOST_ASIO_NO_IOSTREAM) 28 # include <iosfwd> 29 #endif // !defined(BOOST_ASIO_NO_IOSTREAM) 30 31 #include <boost/asio/detail/push_options.hpp> 32 33 namespace boost { 34 namespace asio { 35 namespace ip { 36 37 /// Type used for storing port numbers. 38 typedef uint_least16_t port_type; 39 40 /// Describes an endpoint for a version-independent IP socket. 41 /** 42 * The boost::asio::ip::basic_endpoint class template describes an endpoint that 43 * may be associated with a particular socket. 44 * 45 * @par Thread Safety 46 * @e Distinct @e objects: Safe.@n 47 * @e Shared @e objects: Unsafe. 48 * 49 * @par Concepts: 50 * Endpoint. 51 */ 52 template <typename InternetProtocol> 53 class basic_endpoint 54 { 55 public: 56 /// The protocol type associated with the endpoint. 57 typedef InternetProtocol protocol_type; 58 59 /// The type of the endpoint structure. This type is dependent on the 60 /// underlying implementation of the socket layer. 61 #if defined(GENERATING_DOCUMENTATION) 62 typedef implementation_defined data_type; 63 #else 64 typedef boost::asio::detail::socket_addr_type data_type; 65 #endif 66 67 /// Default constructor. basic_endpoint()68 basic_endpoint() BOOST_ASIO_NOEXCEPT 69 : impl_() 70 { 71 } 72 73 /// Construct an endpoint using a port number, specified in the host's byte 74 /// order. The IP address will be the any address (i.e. INADDR_ANY or 75 /// in6addr_any). This constructor would typically be used for accepting new 76 /// connections. 77 /** 78 * @par Examples 79 * To initialise an IPv4 TCP endpoint for port 1234, use: 80 * @code 81 * boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234); 82 * @endcode 83 * 84 * To specify an IPv6 UDP endpoint for port 9876, use: 85 * @code 86 * boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876); 87 * @endcode 88 */ basic_endpoint(const InternetProtocol & internet_protocol,port_type port_num)89 basic_endpoint(const InternetProtocol& internet_protocol, 90 port_type port_num) BOOST_ASIO_NOEXCEPT 91 : impl_(internet_protocol.family(), port_num) 92 { 93 } 94 95 /// Construct an endpoint using a port number and an IP address. This 96 /// constructor may be used for accepting connections on a specific interface 97 /// or for making a connection to a remote endpoint. basic_endpoint(const boost::asio::ip::address & addr,port_type port_num)98 basic_endpoint(const boost::asio::ip::address& addr, 99 port_type port_num) BOOST_ASIO_NOEXCEPT 100 : impl_(addr, port_num) 101 { 102 } 103 104 /// Copy constructor. basic_endpoint(const basic_endpoint & other)105 basic_endpoint(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT 106 : impl_(other.impl_) 107 { 108 } 109 110 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 111 /// Move constructor. basic_endpoint(basic_endpoint && other)112 basic_endpoint(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT 113 : impl_(other.impl_) 114 { 115 } 116 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 117 118 /// Assign from another endpoint. operator =(const basic_endpoint & other)119 basic_endpoint& operator=(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT 120 { 121 impl_ = other.impl_; 122 return *this; 123 } 124 125 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 126 /// Move-assign from another endpoint. operator =(basic_endpoint && other)127 basic_endpoint& operator=(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT 128 { 129 impl_ = other.impl_; 130 return *this; 131 } 132 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 133 134 /// The protocol associated with the endpoint. protocol() const135 protocol_type protocol() const BOOST_ASIO_NOEXCEPT 136 { 137 if (impl_.is_v4()) 138 return InternetProtocol::v4(); 139 return InternetProtocol::v6(); 140 } 141 142 /// Get the underlying endpoint in the native type. data()143 data_type* data() BOOST_ASIO_NOEXCEPT 144 { 145 return impl_.data(); 146 } 147 148 /// Get the underlying endpoint in the native type. data() const149 const data_type* data() const BOOST_ASIO_NOEXCEPT 150 { 151 return impl_.data(); 152 } 153 154 /// Get the underlying size of the endpoint in the native type. size() const155 std::size_t size() const BOOST_ASIO_NOEXCEPT 156 { 157 return impl_.size(); 158 } 159 160 /// Set the underlying size of the endpoint in the native type. resize(std::size_t new_size)161 void resize(std::size_t new_size) 162 { 163 impl_.resize(new_size); 164 } 165 166 /// Get the capacity of the endpoint in the native type. capacity() const167 std::size_t capacity() const BOOST_ASIO_NOEXCEPT 168 { 169 return impl_.capacity(); 170 } 171 172 /// Get the port associated with the endpoint. The port number is always in 173 /// the host's byte order. port() const174 port_type port() const BOOST_ASIO_NOEXCEPT 175 { 176 return impl_.port(); 177 } 178 179 /// Set the port associated with the endpoint. The port number is always in 180 /// the host's byte order. port(port_type port_num)181 void port(port_type port_num) BOOST_ASIO_NOEXCEPT 182 { 183 impl_.port(port_num); 184 } 185 186 /// Get the IP address associated with the endpoint. address() const187 boost::asio::ip::address address() const BOOST_ASIO_NOEXCEPT 188 { 189 return impl_.address(); 190 } 191 192 /// Set the IP address associated with the endpoint. address(const boost::asio::ip::address & addr)193 void address(const boost::asio::ip::address& addr) BOOST_ASIO_NOEXCEPT 194 { 195 impl_.address(addr); 196 } 197 198 /// Compare two endpoints for equality. operator ==(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)199 friend bool operator==(const basic_endpoint<InternetProtocol>& e1, 200 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 201 { 202 return e1.impl_ == e2.impl_; 203 } 204 205 /// Compare two endpoints for inequality. operator !=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)206 friend bool operator!=(const basic_endpoint<InternetProtocol>& e1, 207 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 208 { 209 return !(e1 == e2); 210 } 211 212 /// Compare endpoints for ordering. operator <(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)213 friend bool operator<(const basic_endpoint<InternetProtocol>& e1, 214 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 215 { 216 return e1.impl_ < e2.impl_; 217 } 218 219 /// Compare endpoints for ordering. operator >(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)220 friend bool operator>(const basic_endpoint<InternetProtocol>& e1, 221 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 222 { 223 return e2.impl_ < e1.impl_; 224 } 225 226 /// Compare endpoints for ordering. operator <=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)227 friend bool operator<=(const basic_endpoint<InternetProtocol>& e1, 228 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 229 { 230 return !(e2 < e1); 231 } 232 233 /// Compare endpoints for ordering. operator >=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)234 friend bool operator>=(const basic_endpoint<InternetProtocol>& e1, 235 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT 236 { 237 return !(e1 < e2); 238 } 239 240 private: 241 // The underlying IP endpoint. 242 boost::asio::ip::detail::endpoint impl_; 243 }; 244 245 #if !defined(BOOST_ASIO_NO_IOSTREAM) 246 247 /// Output an endpoint as a string. 248 /** 249 * Used to output a human-readable string for a specified endpoint. 250 * 251 * @param os The output stream to which the string will be written. 252 * 253 * @param endpoint The endpoint to be written. 254 * 255 * @return The output stream. 256 * 257 * @relates boost::asio::ip::basic_endpoint 258 */ 259 template <typename Elem, typename Traits, typename InternetProtocol> 260 std::basic_ostream<Elem, Traits>& operator<<( 261 std::basic_ostream<Elem, Traits>& os, 262 const basic_endpoint<InternetProtocol>& endpoint); 263 264 #endif // !defined(BOOST_ASIO_NO_IOSTREAM) 265 266 } // namespace ip 267 } // namespace asio 268 } // namespace boost 269 270 #if defined(BOOST_ASIO_HAS_STD_HASH) 271 namespace std { 272 273 template <typename InternetProtocol> 274 struct hash<boost::asio::ip::basic_endpoint<InternetProtocol> > 275 { operator ()std::hash276 std::size_t operator()( 277 const boost::asio::ip::basic_endpoint<InternetProtocol>& ep) 278 const BOOST_ASIO_NOEXCEPT 279 { 280 std::size_t hash1 = std::hash<boost::asio::ip::address>()(ep.address()); 281 std::size_t hash2 = std::hash<unsigned short>()(ep.port()); 282 return hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2)); 283 } 284 }; 285 286 } // namespace std 287 #endif // defined(BOOST_ASIO_HAS_STD_HASH) 288 289 #include <boost/asio/detail/pop_options.hpp> 290 291 #include <boost/asio/ip/impl/basic_endpoint.hpp> 292 293 #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP 294