21 Internet protocol [internet]

21.11 Class template ip::network_v4 [internet.network.v4]

The class network_v4 provides the ability to use and manipulate IPv4 network addresses.

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

  class network_v4
  {
  public:
    // [internet.network.v4.cons], constructors:
    constexpr network_v4() noexcept;
    constexpr network_v4(const address_v4& addr, int prefix_len);
    constexpr network_v4(const address_v4& addr, const address_v4& mask);

    // [internet.network.v4.members], members:
    constexpr address_v4 address() const noexcept;
    constexpr int prefix_length() const noexcept;
    constexpr address_v4 netmask() const noexcept;
    constexpr address_v4 network() const noexcept;
    constexpr address_v4 broadcast() const noexcept;
    address_v4_range hosts() const noexcept;
    constexpr network_v4 canonical() const noexcept;
    constexpr bool is_host() const noexcept;
    constexpr bool is_subnet_of(const network_v4& other) const noexcept;
    template<class Allocator = allocator<char>>
      basic_string<char, char_traits<char>, Allocator>
        to_string(const Allocator& a = Allocator()) const;
  };

  // [internet.network.v4.comparisons], network_v4 comparisons:
  constexpr bool operator==(const network_v4& a, const network_v4& b) noexcept;
  constexpr bool operator!=(const network_v4& a, const network_v4& b) noexcept;

  // [internet.network.v4.creation], network_v4 creation:
  constexpr network_v4 make_network_v4(const address_v4& addr, int prefix_len);
  constexpr network_v4 make_network_v4(const address_v4& addr, const address_v4& mask);
  network_v4 make_network_v4(const char* str);
  network_v4 make_network_v4(const char* str, error_code& ec) noexcept;
  network_v4 make_network_v4(const string& str);
  network_v4 make_network_v4(const string& str, error_code& ec) noexcept;
  network_v4 make_network_v4(string_view str);
  network_v4 make_network_v4(string_view str, error_code& ec) noexcept;

  // [internet.network.v4.io], network_v4 I/O:
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& operator<<(
      basic_ostream<CharT, Traits>& os, const network_v4& net);

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

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

21.11.1 ip::network_v4 constructors [internet.network.v4.cons]

constexpr network_v4() noexcept;

Postconditions: this->address().is_unspecified() == true and prefix_length() == 0.

constexpr network_v4(const address_v4& addr, int prefix_len);

Postconditions: this->address() == addr and prefix_length() == prefix_len.

Remarks: out_of_range if prefix_len < 0 or prefix_len > 32.

constexpr network_v4(const address_v4& addr, const address_v4& mask);

Postconditions: this->address() == addr and prefix_length() is equal to the number of contiguous non-zero bits in mask.

Remarks: invalid_argument if mask contains non-contiguous non-zero bits, or if the most significant bit is zero and any other bits are non-zero.

21.11.2 ip::network_v4 members [internet.network.v4.members]

constexpr address_v4 address() const noexcept;

Returns: The address specified when the network_v4 object was constructed.

constexpr int prefix_length() const noexcept;

Returns: The prefix length of the network.

constexpr address_v4 netmask() const noexcept;

Returns: An address_v4 object with prefix_length() contiguous non-zero bits set, starting from the most significant bit in network byte order. All other bits are zero.

constexpr address_v4 network() const noexcept;

Returns: An address_v4 object with the first prefix_length() bits, starting from the most significant bit in network byte order, set to the corresponding bit value of this->address(). All other bits are zero.

constexpr address_v4 broadcast() const noexcept;

Returns: An address_v4 object with the first prefix_length() bits, starting from the most significant bit in network byte order, set to the corresponding bit value of this->address(). All other bits are non-zero.

address_v4_range hosts() const noexcept;

Returns: If is_host() is true, an address_v4_range object representing the single address this->address(). Otherwise, an address_v4_range object representing the range of unique host IP addresses in the network.

Note: For IPv4, the network address and the broadcast address are not included in the range of host IP addresses. For example, given a network 192.168.1.0/24, the range returned by hosts() is from 192.168.1.1 to 192.168.1.254 inclusive, and neither 192.168.1.0 nor the broadcast address 192.168.1.255 are in the range.  — end note ]

constexpr network_v4 canonical() const noexcept;

Returns: network_v4(network(), prefix_length()).

constexpr bool is_host() const noexcept;

Returns: prefix_length() == 32.

constexpr bool is_subnet_of(const network_v4& other) const noexcept;

Returns: true if other.prefix_length() < prefix_length() and network_v4(this->address(), other.prefix_length()).canonical() == other.canonical(), otherwise false.

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

Returns: this->address().to_string(a) + "/" + std::to_string(prefix_length()).

21.11.3 ip::network_v4 comparisons [internet.network.v4.comparisons]

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

Returns: true if a.address() == b.address() and a.prefix_length() == b.prefix_length(), otherwise false.

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

Returns: !(a == b).

21.11.4 ip::network_v4 creation [internet.network.v4.creation]

constexpr network_v4 make_network_v4(const address_v4& addr, int prefix_len);

Returns: network_v4(addr, prefix_len).

constexpr network_v4 make_network_v4(const address_v4& addr, const address_v4& mask);

Returns: network_v4(addr, mask).

network_v4 make_network_v4(const char* str); network_v4 make_network_v4(const char* str, error_code& ec) noexcept; network_v4 make_network_v4(const string& str); network_v4 make_network_v4(const string& str, error_code& ec) noexcept; network_v4 make_network_v4(string_view str); network_v4 make_network_v4(string_view str, error_code& ec) noexcept;

Returns: If str contains a value of the form address '/' prefix-length, a network_v4 object constructed with the result of applying make_address_v4() to the address portion of the string, and the result of converting prefix-length to an integer of type int. Otherwise returns network_v4() and sets ec to reflect the error.

Error conditions:

  • errc::invalid_argument — if str is not a valid textual representation of an IPv4 address and prefix length.

21.11.5 ip::network_v4 I/O [internet.network.v4.io]

template<class CharT, class Traits> basic_ostream<CharT, Traits>& operator<<( basic_ostream<CharT, Traits>& os, const network_v4& net);

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