1 // 2 // ip/detail/endpoint.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2015 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 ASIO_IP_DETAIL_ENDPOINT_HPP 12 #define ASIO_IP_DETAIL_ENDPOINT_HPP 13 14 15 #include "asio/detail/config.hpp" 16 #include <string> 17 #include "asio/detail/socket_types.hpp" 18 #include "asio/detail/winsock_init.hpp" 19 #include "asio/error_code.hpp" 20 #include "asio/ip/address.hpp" 21 22 #include "asio/detail/push_options.hpp" 23 24 namespace asio { 25 namespace ip { 26 namespace detail { 27 28 // Helper class for implementating an IP endpoint. 29 class endpoint 30 { 31 public: 32 // Default constructor. 33 ASIO_DECL endpoint(); 34 35 // Construct an endpoint using a family and port number. 36 ASIO_DECL endpoint(int family, unsigned short port_num); 37 38 // Construct an endpoint using an address and port number. 39 ASIO_DECL endpoint(const asio::ip::address& addr, 40 unsigned short port_num); 41 42 // Copy constructor. endpoint(const endpoint & other)43 endpoint(const endpoint& other) 44 : data_(other.data_) 45 { 46 } 47 48 // Assign from another endpoint. operator =(const endpoint & other)49 endpoint& operator=(const endpoint& other) 50 { 51 data_ = other.data_; 52 return *this; 53 } 54 55 // Get the underlying endpoint in the native type. data()56 asio::detail::socket_addr_type* data() 57 { 58 return &data_.base; 59 } 60 61 // Get the underlying endpoint in the native type. data() const62 const asio::detail::socket_addr_type* data() const 63 { 64 return &data_.base; 65 } 66 67 // Get the underlying size of the endpoint in the native type. size() const68 std::size_t size() const 69 { 70 if (is_v4()) 71 return sizeof(asio::detail::sockaddr_in4_type); 72 else 73 return sizeof(asio::detail::sockaddr_in6_type); 74 } 75 76 // Set the underlying size of the endpoint in the native type. 77 ASIO_DECL void resize(std::size_t new_size); 78 79 // Get the capacity of the endpoint in the native type. capacity() const80 std::size_t capacity() const 81 { 82 return sizeof(data_); 83 } 84 85 // Get the port associated with the endpoint. 86 ASIO_DECL unsigned short port() const; 87 88 // Set the port associated with the endpoint. 89 ASIO_DECL void port(unsigned short port_num); 90 91 // Get the IP address associated with the endpoint. 92 ASIO_DECL asio::ip::address address() const; 93 94 // Set the IP address associated with the endpoint. 95 ASIO_DECL void address(const asio::ip::address& addr); 96 97 // Compare two endpoints for equality. 98 ASIO_DECL friend bool operator==( 99 const endpoint& e1, const endpoint& e2); 100 101 // Compare endpoints for ordering. 102 ASIO_DECL friend bool operator<( 103 const endpoint& e1, const endpoint& e2); 104 105 // Determine whether the endpoint is IPv4. is_v4() const106 bool is_v4() const 107 { 108 return data_.base.sa_family == ASIO_OS_DEF(AF_INET); 109 } 110 111 112 private: 113 // The underlying IP socket address. 114 union data_union 115 { 116 asio::detail::socket_addr_type base; 117 asio::detail::sockaddr_in4_type v4; 118 asio::detail::sockaddr_in6_type v6; 119 } data_; 120 }; 121 122 } // namespace detail 123 } // namespace ip 124 } // namespace asio 125 126 #include "asio/detail/pop_options.hpp" 127 128 # include "asio/ip/detail/impl/endpoint.ipp" 129 130 #endif // ASIO_IP_DETAIL_ENDPOINT_HPP 131