]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // ip/network_v4.hpp | |
3 | // ~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
b32b8144 FG |
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 | ||
27 | namespace boost { | |
28 | namespace asio { | |
29 | namespace 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 | */ | |
40 | class network_v4 | |
41 | { | |
42 | public: | |
43 | /// Default constructor. | |
44 | network_v4() BOOST_ASIO_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) BOOST_ASIO_NOEXCEPT | |
60 | : address_(other.address_), | |
61 | prefix_length_(other.prefix_length_) | |
62 | { | |
63 | } | |
64 | ||
65 | #if defined(BOOST_ASIO_HAS_MOVE) | |
66 | /// Move constructor. | |
67 | network_v4(network_v4&& other) BOOST_ASIO_NOEXCEPT | |
68 | : address_(BOOST_ASIO_MOVE_CAST(address_v4)(other.address_)), | |
69 | prefix_length_(other.prefix_length_) | |
70 | { | |
71 | } | |
72 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
73 | ||
74 | /// Assign from another network. | |
75 | network_v4& operator=(const network_v4& other) BOOST_ASIO_NOEXCEPT | |
76 | { | |
77 | address_ = other.address_; | |
78 | prefix_length_ = other.prefix_length_; | |
79 | return *this; | |
80 | } | |
81 | ||
82 | #if defined(BOOST_ASIO_HAS_MOVE) | |
83 | /// Move-assign from another network. | |
84 | network_v4& operator=(network_v4&& other) BOOST_ASIO_NOEXCEPT | |
85 | { | |
86 | address_ = BOOST_ASIO_MOVE_CAST(address_v4)(other.address_); | |
87 | prefix_length_ = other.prefix_length_; | |
88 | return *this; | |
89 | } | |
90 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
91 | ||
92 | /// Obtain the address object specified when the network object was created. | |
93 | address_v4 address() const BOOST_ASIO_NOEXCEPT | |
94 | { | |
95 | return address_; | |
96 | } | |
97 | ||
98 | /// Obtain the prefix length that was specified when the network object was | |
99 | /// created. | |
100 | unsigned short prefix_length() const BOOST_ASIO_NOEXCEPT | |
101 | { | |
102 | return prefix_length_; | |
103 | } | |
104 | ||
105 | /// Obtain the netmask that was specified when the network object was created. | |
106 | BOOST_ASIO_DECL address_v4 netmask() const BOOST_ASIO_NOEXCEPT; | |
107 | ||
108 | /// Obtain an address object that represents the network address. | |
109 | address_v4 network() const BOOST_ASIO_NOEXCEPT | |
110 | { | |
111 | return address_v4(address_.to_uint() & netmask().to_uint()); | |
112 | } | |
113 | ||
114 | /// Obtain an address object that represents the network's broadcast address. | |
115 | address_v4 broadcast() const BOOST_ASIO_NOEXCEPT | |
116 | { | |
117 | return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF)); | |
118 | } | |
119 | ||
120 | /// Obtain an address range corresponding to the hosts in the network. | |
121 | BOOST_ASIO_DECL address_v4_range hosts() const BOOST_ASIO_NOEXCEPT; | |
122 | ||
123 | /// Obtain the true network address, omitting any host bits. | |
124 | network_v4 canonical() const BOOST_ASIO_NOEXCEPT | |
125 | { | |
126 | return network_v4(network(), netmask()); | |
127 | } | |
128 | ||
129 | /// Test if network is a valid host address. | |
130 | bool is_host() const BOOST_ASIO_NOEXCEPT | |
131 | { | |
132 | return prefix_length_ == 32; | |
133 | } | |
134 | ||
135 | /// Test if a network is a real subnet of another network. | |
136 | BOOST_ASIO_DECL bool is_subnet_of(const network_v4& other) const; | |
137 | ||
138 | /// Get the network as an address in dotted decimal format. | |
139 | BOOST_ASIO_DECL std::string to_string() const; | |
140 | ||
141 | /// Get the network as an address in dotted decimal format. | |
142 | BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const; | |
143 | ||
144 | /// Compare two networks for equality. | |
145 | friend bool operator==(const network_v4& a, const network_v4& b) | |
146 | { | |
147 | return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_; | |
148 | } | |
149 | ||
150 | /// Compare two networks for inequality. | |
151 | friend bool operator!=(const network_v4& a, const network_v4& b) | |
152 | { | |
153 | return !(a == b); | |
154 | } | |
155 | ||
156 | private: | |
157 | address_v4 address_; | |
158 | unsigned short prefix_length_; | |
159 | }; | |
160 | ||
161 | /// Create an IPv4 network from an address and prefix length. | |
162 | /** | |
163 | * @relates address_v4 | |
164 | */ | |
165 | inline network_v4 make_network_v4( | |
166 | const address_v4& addr, unsigned short prefix_len) | |
167 | { | |
168 | return network_v4(addr, prefix_len); | |
169 | } | |
170 | ||
171 | /// Create an IPv4 network from an address and netmask. | |
172 | /** | |
173 | * @relates address_v4 | |
174 | */ | |
175 | inline network_v4 make_network_v4( | |
176 | const address_v4& addr, const address_v4& mask) | |
177 | { | |
178 | return network_v4(addr, mask); | |
179 | } | |
180 | ||
181 | /// Create an IPv4 network from a string containing IP address and prefix | |
182 | /// length. | |
183 | /** | |
184 | * @relates network_v4 | |
185 | */ | |
186 | BOOST_ASIO_DECL network_v4 make_network_v4(const char* str); | |
187 | ||
188 | /// Create an IPv4 network from a string containing IP address and prefix | |
189 | /// length. | |
190 | /** | |
191 | * @relates network_v4 | |
192 | */ | |
193 | BOOST_ASIO_DECL network_v4 make_network_v4( | |
194 | const char* str, boost::system::error_code& ec); | |
195 | ||
196 | /// Create an IPv4 network from a string containing IP address and prefix | |
197 | /// length. | |
198 | /** | |
199 | * @relates network_v4 | |
200 | */ | |
201 | BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str); | |
202 | ||
203 | /// Create an IPv4 network from a string containing IP address and prefix | |
204 | /// length. | |
205 | /** | |
206 | * @relates network_v4 | |
207 | */ | |
208 | BOOST_ASIO_DECL network_v4 make_network_v4( | |
209 | const std::string& str, boost::system::error_code& ec); | |
210 | ||
11fdf7f2 | 211 | #if defined(BOOST_ASIO_HAS_STRING_VIEW) \ |
b32b8144 FG |
212 | || defined(GENERATING_DOCUMENTATION) |
213 | ||
214 | /// Create an IPv4 network from a string containing IP address and prefix | |
215 | /// length. | |
216 | /** | |
217 | * @relates network_v4 | |
218 | */ | |
219 | BOOST_ASIO_DECL network_v4 make_network_v4(string_view str); | |
220 | ||
221 | /// Create an IPv4 network from a string containing IP address and prefix | |
222 | /// length. | |
223 | /** | |
224 | * @relates network_v4 | |
225 | */ | |
226 | BOOST_ASIO_DECL network_v4 make_network_v4( | |
227 | string_view str, boost::system::error_code& ec); | |
228 | ||
11fdf7f2 | 229 | #endif // defined(BOOST_ASIO_HAS_STRING_VIEW) |
b32b8144 FG |
230 | // || defined(GENERATING_DOCUMENTATION) |
231 | ||
232 | #if !defined(BOOST_ASIO_NO_IOSTREAM) | |
233 | ||
234 | /// Output a network as a string. | |
235 | /** | |
236 | * Used to output a human-readable string for a specified network. | |
237 | * | |
238 | * @param os The output stream to which the string will be written. | |
239 | * | |
240 | * @param net The network to be written. | |
241 | * | |
242 | * @return The output stream. | |
243 | * | |
244 | * @relates boost::asio::ip::address_v4 | |
245 | */ | |
246 | template <typename Elem, typename Traits> | |
247 | std::basic_ostream<Elem, Traits>& operator<<( | |
248 | std::basic_ostream<Elem, Traits>& os, const network_v4& net); | |
249 | ||
250 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) | |
251 | ||
252 | } // namespace ip | |
253 | } // namespace asio | |
254 | } // namespace boost | |
255 | ||
256 | #include <boost/asio/detail/pop_options.hpp> | |
257 | ||
258 | #include <boost/asio/ip/impl/network_v4.hpp> | |
259 | #if defined(BOOST_ASIO_HEADER_ONLY) | |
260 | # include <boost/asio/ip/impl/network_v4.ipp> | |
261 | #endif // defined(BOOST_ASIO_HEADER_ONLY) | |
262 | ||
263 | #endif // BOOST_ASIO_IP_NETWORK_V4_HPP |