21 Internet protocol [internet]

21.13 Class template ip::basic_endpoint [internet.endpoint]

An object of type basic_endpoint<InternetProtocol> represents a protocol-specific endpoint, where an endpoint consists of an IP address and port number. Endpoints may be used to identify sources and destinations for socket connections and datagrams.

namespace std {
namespace experimental {
namespace net {
inline namespace v1 {
namespace ip {

  template<class InternetProtocol>
  class basic_endpoint
  {
  public:
    // types:
    using protocol_type = InternetProtocol;

    // [internet.endpoint.cons], constructors:
    constexpr basic_endpoint() noexcept;
    constexpr basic_endpoint(const protocol_type& proto,
                             port_type port_num) noexcept;
    constexpr basic_endpoint(const ip::address& addr,
                             port_type port_num) noexcept;

    // [internet.endpoint.members], members:
    constexpr protocol_type protocol() const noexcept;
    constexpr ip::address address() const noexcept;
    void address(const ip::address& addr) noexcept;
    constexpr port_type port() const noexcept;
    void port(port_type port_num) noexcept;
  };

  // [internet.endpoint.comparisons], basic_endpoint comparisons:
  template<class InternetProtocol>
    constexpr bool operator==(const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;
  template<class InternetProtocol>
    constexpr bool operator!=(const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;
  template<class InternetProtocol>
    constexpr bool operator< (const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;
  template<class InternetProtocol>
    constexpr bool operator> (const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;
  template<class InternetProtocol>
    constexpr bool operator<=(const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;
  template<class InternetProtocol>
    constexpr bool operator>=(const basic_endpoint<InternetProtocol>& a,
                              const basic_endpoint<InternetProtocol>& b) noexcept;

  // [internet.endpoint.io], basic_endpoint I/O:
  template<class CharT, class Traits, class InternetProtocol>
    basic_ostream<CharT, Traits>& operator<<(
      basic_ostream<CharT, Traits>& os,
      const basic_endpoint<InternetProtocol>& ep);

} // namespace ip
} // inline namespace v1
} // namespace net
} // namespace experimental
} // namespace std

Instances of the basic_endpoint class template meet the requirements for an Endpoint ([socket.reqmts.endpoint]).

Extensible implementations provide the following member functions:

namespace std {
namespace experimental {
namespace net {
inline namespace v1 {
namespace ip {

  template<class InternetProtocol>
  class basic_endpoint
  {
  public:
    void* data() noexcept;
    const void* data() const noexcept;
    constexpr size_t size() const noexcept;
    void resize(size_t s);
    constexpr size_t capacity() const noexcept;
    // remainder unchanged
  private:
    union
    {
      sockaddr_in v4_;
      sockaddr_in6 v6_;
    } data_; // exposition only
  };

} // namespace ip
} // inline namespace v1
} // namespace net
} // namespace experimental
} // namespace std

21.13.1 ip::basic_endpoint constructors [internet.endpoint.cons]

constexpr basic_endpoint() noexcept;

Postconditions: this->address() == ip::address() and port() == 0.

constexpr basic_endpoint(const protocol_type& proto, port_type port_num) noexcept;

Requires: proto == protocol_type::v4() || proto == protocol_type::v6().

Postconditions:

  • If proto == protocol_type::v6(), this->address() == ip::address_v6(); otherwise,
    this->address() == ip::address_v4().

  • port() == port_num.

constexpr basic_endpoint(const ip::address& addr, port_type port_num) noexcept;

Postconditions: this->address() == addr and port() == port_num.

21.13.2 ip::basic_endpoint members [internet.endpoint.members]

constexpr protocol_type protocol() const noexcept;

Returns: protocol_type::v6() if the expression this->address().is_v6() is true, otherwise protocol_type::v4().

constexpr ip::address address() const noexcept;

Returns: The address associated with the endpoint.

void address(const ip::address& addr) noexcept;

Postconditions: this->address() == addr.

constexpr port_type port() const noexcept;

Returns: The port number associated with the endpoint.

void port(port_type port_num) noexcept;

Postconditions: port() == port_num.

21.13.3 ip::basic_endpoint comparisons [internet.endpoint.comparisons]

template<class InternetProtocol> constexpr bool operator==(const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: a.address() == b.address() && a.port() == b.port()).

template<class InternetProtocol> constexpr bool operator!=(const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: !(a == b).

template<class InternetProtocol> constexpr bool operator< (const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: tie(a.address(), a.port()) < tie(b.address(), b.port()).

template<class InternetProtocol> constexpr bool operator> (const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: b < a.

template<class InternetProtocol> constexpr bool operator<=(const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: !(b < a).

template<class InternetProtocol> constexpr bool operator>=(const basic_endpoint<InternetProtocol>& a, const basic_endpoint<InternetProtocol>& b) noexcept;

Returns: !(a < b).

21.13.4 ip::basic_endpoint I/O [internet.endpoint.io]

template<class CharT, class Traits, class InternetProtocol> basic_ostream<CharT, Traits>& operator<<( basic_ostream<CharT, Traits>& os, const basic_endpoint<InternetProtocol>& ep);

Effects: Outputs a representation of the endpoint to the stream, as if it were implemented as follows:

basic_ostringstream<CharT, Traits> ss;
if (ep.protocol() == basic_endpoint<InternetProtocol>::protocol_type::v6())
  ss << "[" << ep.address() << "]";
else
  ss << ep.address();
ss << ":" << ep.port();
os << ss.str();

Returns: os.

Note: The representation of the endpoint when it contains an IP version 6 address is based on [RFC2732].  — end note ]

21.13.5 ip::basic_endpoint members (extensible implementations) [internet.endpoint.extensible]

void* data() noexcept;

Returns: addressof(data_).

const void* data() const noexcept;

Returns: addressof(data_).

constexpr size_t size() const noexcept;

Returns: sizeof(sockaddr_in6) if protocol().family() == AF_INET6, otherwise sizeof(sockaddr_in).

void resize(size_t s);

Remarks: length_error if either of the following conditions is true:

  • protocol().family() == AF_INET6 && s != sizeof(sockaddr_in6),

  • protocol().family() == AF_INET4 && s != sizeof(sockaddr_in).

constexpr size_t capacity() const noexcept;

Returns: sizeof(data_).