21 Internet protocol [internet]

21.6 Class ip::address_v6 [internet.address.v6]

The class address_v6 is a representation of an IPv6 address.

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

  class address_v6
  {
  public:
    // [internet.address.v6.bytes], types:
    struct bytes_type;

    // [internet.address.v6.cons], constructors:
    constexpr address_v6() noexcept;
    constexpr address_v6(const address_v6& a) noexcept;
    constexpr address_v6(const bytes_type& bytes,
                         scope_id_type scope = 0);

    // assignment:
    address_v6& operator=(const address_v6& a) noexcept;

    // [internet.address.v6.members], members:
    void scope_id(scope_id_type id) noexcept;
    constexpr scope_id_type scope_id() const noexcept;
    constexpr bool is_unspecified() const noexcept;
    constexpr bool is_loopback() const noexcept;
    constexpr bool is_multicast() const noexcept;
    constexpr bool is_link_local() const noexcept;
    constexpr bool is_site_local() const noexcept;
    constexpr bool is_v4_mapped() const noexcept;
    constexpr bool is_multicast_node_local() const noexcept;
    constexpr bool is_multicast_link_local() const noexcept;
    constexpr bool is_multicast_site_local() const noexcept;
    constexpr bool is_multicast_org_local() const noexcept;
    constexpr bool is_multicast_global() const noexcept;
    constexpr bytes_type to_bytes() const noexcept;
    template<class Allocator = allocator<char>>
      basic_string<char, char_traits<char>, Allocator>
        to_string(const Allocator& a = Allocator()) const;

    // [internet.address.v6.static], static members:
    static constexpr address_v6 any() noexcept;
    static constexpr address_v6 loopback() noexcept;
  };

  // [internet.address.v6.comparisons], address_v6 comparisons:
  constexpr bool operator==(const address_v6& a, const address_v6& b) noexcept;
  constexpr bool operator!=(const address_v6& a, const address_v6& b) noexcept;
  constexpr bool operator< (const address_v6& a, const address_v6& b) noexcept;
  constexpr bool operator> (const address_v6& a, const address_v6& b) noexcept;
  constexpr bool operator<=(const address_v6& a, const address_v6& b) noexcept;
  constexpr bool operator>=(const address_v6& a, const address_v6& b) noexcept;

  // [internet.address.v6.creation], address_v6 creation:
  constexpr address_v6 make_address_v6(const address_v6::bytes_type& bytes,
                                       scope_id_type scope_id = 0);
  constexpr address_v6 make_address_v6(v4_mapped_t, const address_v4& a) noexcept;
  address_v6 make_address_v6(const char* str);
  address_v6 make_address_v6(const char* str, error_code& ec) noexcept;
  address_v6 make_address_v6(const string& str);
  address_v6 make_address_v6(const string& str, error_code& ec) noexcept;
  address_v6 make_address_v6(string_view str);
  address_v6 make_address_v6(string_view str, error_code& ec) noexcept;

  // [internet.address.v6.io], address_v6 I/O:
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& operator<<(
      basic_ostream<CharT, Traits>& os, const address_v6& addr);

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

address_v6 satisfies the requirements for Destructible (C++ 2014 [destructible]), CopyConstructible (C++ 2014 [copyconstructible]), and CopyAssignable (C++ 2014 [copyassignable]).

Note: The implementations of the functions is_unspecified, is_loopback, is_multicast, is_link_local, is_site_local, is_v4_mapped, is_multicast_node_local, is_multicast_link_local, is_multicast_site_local, is_multicast_org_local and is_multicast_global are determined by [RFC4291].  — end note ]

21.6.1 Struct ip::address_v6::bytes_type [internet.address.v6.bytes]

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

  struct address_v6::bytes_type : array<unsigned char, 16>
  {
    template<class... T> explicit constexpr bytes_type(T... t)
      : array<unsigned char, 16>{{static_cast<unsigned char>(t)...}} {}
  };

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

The ip::address_v6::bytes_type type is a standard-layout struct that provides a byte-level representation of an IPv6 address in network byte order.

21.6.2 ip::address_v6 constructors [internet.address.v6.cons]

constexpr address_v6() noexcept;

Postconditions: is_unspecified() == true and scope_id() == 0.

constexpr address_v6(const bytes_type& bytes, scope_id_type scope = 0);

Remarks: out_of_range if any element of bytes is not in the range [0, 0xFF]. [ Note: For implementations where numeric_limits<unsigned char>::max() == 0xFF, no out-of-range detection is needed.  — end note ]

Postconditions: to_bytes() == bytes and scope_id() == scope.

21.6.3 ip::address_v6 members [internet.address.v6.members]

void scope_id(scope_id_type id) noexcept;

Postconditions: scope_id() == id.

constexpr scope_id_type scope_id() const noexcept;

Returns: The scope identifier associated with the address.

constexpr bool is_unspecified() const noexcept;

Returns: *this == make_address_v6("::").

constexpr bool is_loopback() const noexcept;

Returns: *this == make_address_v6("::1").

constexpr bool is_multicast() const noexcept;

Returns: A boolean indicating whether the address_v6 object represents a multicast address, as if computed by the following method:

bytes_type b = to_bytes();
return b[0] == 0xFF;

constexpr bool is_link_local() const noexcept;

Returns: A boolean indicating whether the address_v6 object represents a unicast link-local address, as if computed by the following method:

bytes_type b = to_bytes();
return b[0] == 0xFE && (b[1] & 0xC0) == 0x80;

constexpr bool is_site_local() const noexcept;

Returns: A boolean indicating whether the address_v6 object represents a unicast site-local address, as if computed by the following method:

bytes_type b = to_bytes();
return b[0] == 0xFE && (b[1] & 0xC0) == 0xC0;

constexpr bool is_v4_mapped() const noexcept;

Returns: A boolean indicating whether the address_v6 object represents an IPv4-mapped IPv6 address, as if computed by the following method:

bytes_type b = to_bytes();
return b[ 0] == 0 && b[ 1] == 0 && b[ 2] == 0    && b[ 3] == 0
    && b[ 4] == 0 && b[ 5] == 0 && b[ 6] == 0    && b[ 7] == 0
    && b[ 8] == 0 && b[ 9] == 0 && b[10] == 0xFF && b[11] == 0xFF;

constexpr bool is_multicast_node_local() const noexcept;

Returns: is_multicast() && (to_bytes()[1] & 0x0F) == 0x01.

constexpr bool is_multicast_link_local() const noexcept;

Returns: is_multicast() && (to_bytes()[1] & 0x0F) == 0x02.

constexpr bool is_multicast_site_local() const noexcept;

Returns: is_multicast() && (to_bytes()[1] & 0x0F) == 0x05.

constexpr bool is_multicast_org_local() const noexcept;

Returns: is_multicast() && (to_bytes()[1] & 0x0F) == 0x08.

constexpr bool is_multicast_global() const noexcept;

Returns: is_multicast() && (to_bytes()[1] & 0x0F) == 0x0E.

constexpr bytes_type to_bytes() const noexcept;

Returns: A representation of the address in network byte order ([defs.net.byte.order]).

template<class Allocator = allocator<char>> basic_string<char, char_traits<char>, Allocator> to_string(const Allocator& a = Allocator()) const;

Effects: Converts an address into a textual representation. If scope_id() == 0, converts as if by POSIX inet_ntop when invoked with address family AF_INET6. If scope_id() != 0, the format is address%scope-id, where address is the textual representation of the equivalent address having scope_id() == 0, and scope-id is an implementation-defined textual representation of the scope identifier.

Returns: If successful, the textual representation of the address. Otherwise basic_string<char, char_traits<char>, Allocator>(a).

21.6.4 ip::address_v6 static members [internet.address.v6.static]

static constexpr address_v6 any() noexcept;

Returns: An address a such that the a.is_unspecified() == true and a.scope_id() == 0.

static constexpr address_v6 loopback() noexcept;

Returns: An address a such that the a.is_loopback() == true and a.scope_id() == 0.

21.6.5 ip::address_v6 comparisons [internet.address.v6.comparisons]

constexpr bool operator==(const address_v6& a, const address_v6& b) noexcept;

Returns: a.to_bytes() == b.to_bytes() && a.scope_id() == b.scope_id().

constexpr bool operator!=(const address_v6& a, const address_v6& b) noexcept;

Returns: !(a == b).

constexpr bool operator< (const address_v6& a, const address_v6& b) noexcept;

Returns: tie(a.to_bytes(), a.scope_id()) < tie(b.to_bytes(), b.scope_id()).

constexpr bool operator> (const address_v6& a, const address_v6& b) noexcept;

Returns: b < a.

constexpr bool operator<=(const address_v6& a, const address_v6& b) noexcept;

Returns: !(b < a).

constexpr bool operator>=(const address_v6& a, const address_v6& b) noexcept;

Returns: !(a < b).

21.6.6 ip::address_v6 creation [internet.address.v6.creation]

constexpr address_v6 make_address_v6(const address_v6::bytes_type& bytes, scope_id_type scope_id);

Returns: address_v6(bytes, scope_id).

constexpr address_v6 make_address_v6(v4_mapped_t, const address_v4& a) noexcept;

Returns: An address_v6 object containing the IPv4-mapped IPv6 address corresponding to the specified IPv4 address, as if computed by the following method:

address_v4::bytes_type v4b = a.to_bytes();
address_v6::bytes_type v6b(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0xFF, 0xFF, v4b[0], v4b[1], v4b[2], v4b[3]);
return address_v6(v6b);

address_v6 make_address_v6(const char* str); address_v6 make_address_v6(const char* str, error_code& ec) noexcept; address_v4 make_address_v6(const string& str); address_v4 make_address_v6(const string& str, error_code& ec) noexcept; address_v6 make_address_v6(string_view str); address_v6 make_address_v6(string_view str, error_code& ec) noexcept;

Effects: Converts a textual representation of an address into a corresponding address_v6 value. The format is either address or address%scope-id, where address is in the format specified by POSIX inet_pton when invoked with address family AF_INET6, and scope-id is an optional string specifying the scope identifier. All implementations accept as scope-id a textual representation of an unsigned decimal integer. It is implementation-defined whether alternative scope identifier representations are permitted. If scope-id is not supplied, an address_v6 object is returned such that scope_id() == 0.

Returns: If successful, an address_v6 value corresponding to the string str. Otherwise returns address_v6().

Error conditions:

  • errc::invalid_argument — if str is not a valid textual representation of an IPv6 address.

21.6.7 ip::address_v6 I/O [internet.address.v6.io]

template<class CharT, class Traits> basic_ostream<CharT, Traits>& operator<<( basic_ostream<CharT, Traits>& os, const address_v6& addr);

Returns: os << addr.to_string().c_str().