1//
2// ip/network_v4.hpp
3// ~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
7//
8// Distributed under the Boost Software License, Version 1.0. (See accompanying
9// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10//
11
12#ifndef BOOST_ASIO_IP_NETWORK_V4_HPP
13#define BOOST_ASIO_IP_NETWORK_V4_HPP
14
15#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16# pragma once
17#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19#include <boost/asio/detail/config.hpp>
20#include <string>
21#include <boost/asio/detail/string_view.hpp>
22#include <boost/system/error_code.hpp>
23#include <boost/asio/ip/address_v4_range.hpp>
24
25#include <boost/asio/detail/push_options.hpp>
26
27namespace boost {
28namespace asio {
29namespace ip {
30
31/// Represents an IPv4 network.
32/**
33 * The boost::asio::ip::network_v4 class provides the ability to use and
34 * manipulate IP version 4 networks.
35 *
36 * @par Thread Safety
37 * @e Distinct @e objects: Safe.@n
38 * @e Shared @e objects: Unsafe.
39 */
40class network_v4
41{
42public:
43 /// Default constructor.
44 network_v4() noexcept
45 : address_(),
46 prefix_length_(0)
47 {
48 }
49
50 /// Construct a network based on the specified address and prefix length.
51 BOOST_ASIO_DECL network_v4(const address_v4& addr,
52 unsigned short prefix_len);
53
54 /// Construct network based on the specified address and netmask.
55 BOOST_ASIO_DECL network_v4(const address_v4& addr,
56 const address_v4& mask);
57
58 /// Copy constructor.
59 network_v4(const network_v4& other) noexcept
60 : address_(other.address_),
61 prefix_length_(other.prefix_length_)
62 {
63 }
64
65 /// Move constructor.
66 network_v4(network_v4&& other) noexcept
67 : address_(static_cast<address_v4&&>(other.address_)),
68 prefix_length_(other.prefix_length_)
69 {
70 }
71
72 /// Assign from another network.
73 network_v4& operator=(const network_v4& other) noexcept
74 {
75 address_ = other.address_;
76 prefix_length_ = other.prefix_length_;
77 return *this;
78 }
79
80 /// Move-assign from another network.
81 network_v4& operator=(network_v4&& other) noexcept
82 {
83 address_ = static_cast<address_v4&&>(other.address_);
84 prefix_length_ = other.prefix_length_;
85 return *this;
86 }
87
88 /// Obtain the address object specified when the network object was created.
89 address_v4 address() const noexcept
90 {
91 return address_;
92 }
93
94 /// Obtain the prefix length that was specified when the network object was
95 /// created.
96 unsigned short prefix_length() const noexcept
97 {
98 return prefix_length_;
99 }
100
101 /// Obtain the netmask that was specified when the network object was created.
102 BOOST_ASIO_DECL address_v4 netmask() const noexcept;
103
104 /// Obtain an address object that represents the network address.
105 address_v4 network() const noexcept
106 {
107 return address_v4(address_.to_uint() & netmask().to_uint());
108 }
109
110 /// Obtain an address object that represents the network's broadcast address.
111 address_v4 broadcast() const noexcept
112 {
113 return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF));
114 }
115
116 /// Obtain an address range corresponding to the hosts in the network.
117 BOOST_ASIO_DECL address_v4_range hosts() const noexcept;
118
119 /// Obtain the true network address, omitting any host bits.
120 network_v4 canonical() const noexcept
121 {
122 return network_v4(network(), prefix_length());
123 }
124
125 /// Test if network is a valid host address.
126 bool is_host() const noexcept
127 {
128 return prefix_length_ == 32;
129 }
130
131 /// Test if a network is a real subnet of another network.
132 BOOST_ASIO_DECL bool is_subnet_of(const network_v4& other) const;
133
134 /// Get the network as an address in dotted decimal format.
135 BOOST_ASIO_DECL std::string to_string() const;
136
137 /// Get the network as an address in dotted decimal format.
138 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
139
140 /// Compare two networks for equality.
141 friend bool operator==(const network_v4& a, const network_v4& b)
142 {
143 return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
144 }
145
146 /// Compare two networks for inequality.
147 friend bool operator!=(const network_v4& a, const network_v4& b)
148 {
149 return !(a == b);
150 }
151
152private:
153 address_v4 address_;
154 unsigned short prefix_length_;
155};
156
157/// Create an IPv4 network from an address and prefix length.
158/**
159 * @relates address_v4
160 */
161inline network_v4 make_network_v4(
162 const address_v4& addr, unsigned short prefix_len)
163{
164 return network_v4(addr, prefix_len);
165}
166
167/// Create an IPv4 network from an address and netmask.
168/**
169 * @relates address_v4
170 */
171inline network_v4 make_network_v4(
172 const address_v4& addr, const address_v4& mask)
173{
174 return network_v4(addr, mask);
175}
176
177/// Create an IPv4 network from a string containing IP address and prefix
178/// length.
179/**
180 * @relates network_v4
181 */
182BOOST_ASIO_DECL network_v4 make_network_v4(const char* str);
183
184/// Create an IPv4 network from a string containing IP address and prefix
185/// length.
186/**
187 * @relates network_v4
188 */
189BOOST_ASIO_DECL network_v4 make_network_v4(
190 const char* str, boost::system::error_code& ec);
191
192/// Create an IPv4 network from a string containing IP address and prefix
193/// length.
194/**
195 * @relates network_v4
196 */
197BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str);
198
199/// Create an IPv4 network from a string containing IP address and prefix
200/// length.
201/**
202 * @relates network_v4
203 */
204BOOST_ASIO_DECL network_v4 make_network_v4(
205 const std::string& str, boost::system::error_code& ec);
206
207#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
208 || defined(GENERATING_DOCUMENTATION)
209
210/// Create an IPv4 network from a string containing IP address and prefix
211/// length.
212/**
213 * @relates network_v4
214 */
215BOOST_ASIO_DECL network_v4 make_network_v4(string_view str);
216
217/// Create an IPv4 network from a string containing IP address and prefix
218/// length.
219/**
220 * @relates network_v4
221 */
222BOOST_ASIO_DECL network_v4 make_network_v4(
223 string_view str, boost::system::error_code& ec);
224
225#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
226 // || defined(GENERATING_DOCUMENTATION)
227
228#if !defined(BOOST_ASIO_NO_IOSTREAM)
229
230/// Output a network as a string.
231/**
232 * Used to output a human-readable string for a specified network.
233 *
234 * @param os The output stream to which the string will be written.
235 *
236 * @param net The network to be written.
237 *
238 * @return The output stream.
239 *
240 * @relates boost::asio::ip::address_v4
241 */
242template <typename Elem, typename Traits>
243std::basic_ostream<Elem, Traits>& operator<<(
244 std::basic_ostream<Elem, Traits>& os, const network_v4& net);
245
246#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
247
248} // namespace ip
249} // namespace asio
250} // namespace boost
251
252#include <boost/asio/detail/pop_options.hpp>
253
254#include <boost/asio/ip/impl/network_v4.hpp>
255#if defined(BOOST_ASIO_HEADER_ONLY)
256# include <boost/asio/ip/impl/network_v4.ipp>
257#endif // defined(BOOST_ASIO_HEADER_ONLY)
258
259#endif // BOOST_ASIO_IP_NETWORK_V4_HPP
260

source code of boost/libs/asio/include/boost/asio/ip/network_v4.hpp