1 //
2 // ip/address_v4.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP
12 #define BOOST_ASIO_IP_ADDRESS_V4_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 #include <string>
20 #include <boost/asio/detail/array.hpp>
21 #include <boost/asio/detail/cstdint.hpp>
22 #include <boost/asio/detail/socket_types.hpp>
23 #include <boost/asio/detail/string_view.hpp>
24 #include <boost/asio/detail/winsock_init.hpp>
25 #include <boost/system/error_code.hpp>
26 
27 #if !defined(BOOST_ASIO_NO_IOSTREAM)
28 # include <iosfwd>
29 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
30 
31 #include <boost/asio/detail/push_options.hpp>
32 
33 namespace boost {
34 namespace asio {
35 namespace ip {
36 
37 /// Implements IP version 4 style addresses.
38 /**
39  * The boost::asio::ip::address_v4 class provides the ability to use and
40  * manipulate IP version 4 addresses.
41  *
42  * @par Thread Safety
43  * @e Distinct @e objects: Safe.@n
44  * @e Shared @e objects: Unsafe.
45  */
46 class address_v4
47 {
48 public:
49   /// The type used to represent an address as an unsigned integer.
50   typedef uint_least32_t uint_type;
51 
52   /// The type used to represent an address as an array of bytes.
53   /**
54    * @note This type is defined in terms of the C++0x template @c std::array
55    * when it is available. Otherwise, it uses @c boost:array.
56    */
57 #if defined(GENERATING_DOCUMENTATION)
58   typedef array<unsigned char, 4> bytes_type;
59 #else
60   typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
61 #endif
62 
63   /// Default constructor.
address_v4()64   address_v4() BOOST_ASIO_NOEXCEPT
65   {
66     addr_.s_addr = 0;
67   }
68 
69   /// Construct an address from raw bytes.
70   BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
71 
72   /// Construct an address from an unsigned integer in host byte order.
73   BOOST_ASIO_DECL explicit address_v4(uint_type addr);
74 
75   /// Copy constructor.
address_v4(const address_v4 & other)76   address_v4(const address_v4& other) BOOST_ASIO_NOEXCEPT
77     : addr_(other.addr_)
78   {
79   }
80 
81 #if defined(BOOST_ASIO_HAS_MOVE)
82   /// Move constructor.
address_v4(address_v4 && other)83   address_v4(address_v4&& other) BOOST_ASIO_NOEXCEPT
84     : addr_(other.addr_)
85   {
86   }
87 #endif // defined(BOOST_ASIO_HAS_MOVE)
88 
89   /// Assign from another address.
operator =(const address_v4 & other)90   address_v4& operator=(const address_v4& other) BOOST_ASIO_NOEXCEPT
91   {
92     addr_ = other.addr_;
93     return *this;
94   }
95 
96 #if defined(BOOST_ASIO_HAS_MOVE)
97   /// Move-assign from another address.
operator =(address_v4 && other)98   address_v4& operator=(address_v4&& other) BOOST_ASIO_NOEXCEPT
99   {
100     addr_ = other.addr_;
101     return *this;
102   }
103 #endif // defined(BOOST_ASIO_HAS_MOVE)
104 
105   /// Get the address in bytes, in network byte order.
106   BOOST_ASIO_DECL bytes_type to_bytes() const BOOST_ASIO_NOEXCEPT;
107 
108   /// Get the address as an unsigned integer in host byte order
109   BOOST_ASIO_DECL uint_type to_uint() const BOOST_ASIO_NOEXCEPT;
110 
111 #if !defined(BOOST_ASIO_NO_DEPRECATED)
112   /// Get the address as an unsigned long in host byte order
113   BOOST_ASIO_DECL unsigned long to_ulong() const;
114 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
115 
116   /// Get the address as a string in dotted decimal format.
117   BOOST_ASIO_DECL std::string to_string() const;
118 
119 #if !defined(BOOST_ASIO_NO_DEPRECATED)
120   /// (Deprecated: Use other overload.) Get the address as a string in dotted
121   /// decimal format.
122   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
123 
124   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
125   /// string in dotted decimal form.
126   static address_v4 from_string(const char* str);
127 
128   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
129   /// string in dotted decimal form.
130   static address_v4 from_string(
131       const char* str, boost::system::error_code& ec);
132 
133   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
134   /// string in dotted decimal form.
135   static address_v4 from_string(const std::string& str);
136 
137   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
138   /// string in dotted decimal form.
139   static address_v4 from_string(
140       const std::string& str, boost::system::error_code& ec);
141 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
142 
143   /// Determine whether the address is a loopback address.
144   BOOST_ASIO_DECL bool is_loopback() const BOOST_ASIO_NOEXCEPT;
145 
146   /// Determine whether the address is unspecified.
147   BOOST_ASIO_DECL bool is_unspecified() const BOOST_ASIO_NOEXCEPT;
148 
149 #if !defined(BOOST_ASIO_NO_DEPRECATED)
150   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
151   /// class A address.
152   BOOST_ASIO_DECL bool is_class_a() const;
153 
154   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
155   /// class B address.
156   BOOST_ASIO_DECL bool is_class_b() const;
157 
158   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
159   /// class C address.
160   BOOST_ASIO_DECL bool is_class_c() const;
161 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
162 
163   /// Determine whether the address is a multicast address.
164   BOOST_ASIO_DECL bool is_multicast() const BOOST_ASIO_NOEXCEPT;
165 
166   /// Compare two addresses for equality.
operator ==(const address_v4 & a1,const address_v4 & a2)167   friend bool operator==(const address_v4& a1,
168       const address_v4& a2) BOOST_ASIO_NOEXCEPT
169   {
170     return a1.addr_.s_addr == a2.addr_.s_addr;
171   }
172 
173   /// Compare two addresses for inequality.
operator !=(const address_v4 & a1,const address_v4 & a2)174   friend bool operator!=(const address_v4& a1,
175       const address_v4& a2) BOOST_ASIO_NOEXCEPT
176   {
177     return a1.addr_.s_addr != a2.addr_.s_addr;
178   }
179 
180   /// Compare addresses for ordering.
operator <(const address_v4 & a1,const address_v4 & a2)181   friend bool operator<(const address_v4& a1,
182       const address_v4& a2) BOOST_ASIO_NOEXCEPT
183   {
184     return a1.to_uint() < a2.to_uint();
185   }
186 
187   /// Compare addresses for ordering.
operator >(const address_v4 & a1,const address_v4 & a2)188   friend bool operator>(const address_v4& a1,
189       const address_v4& a2) BOOST_ASIO_NOEXCEPT
190   {
191     return a1.to_uint() > a2.to_uint();
192   }
193 
194   /// Compare addresses for ordering.
operator <=(const address_v4 & a1,const address_v4 & a2)195   friend bool operator<=(const address_v4& a1,
196       const address_v4& a2) BOOST_ASIO_NOEXCEPT
197   {
198     return a1.to_uint() <= a2.to_uint();
199   }
200 
201   /// Compare addresses for ordering.
operator >=(const address_v4 & a1,const address_v4 & a2)202   friend bool operator>=(const address_v4& a1,
203       const address_v4& a2) BOOST_ASIO_NOEXCEPT
204   {
205     return a1.to_uint() >= a2.to_uint();
206   }
207 
208   /// Obtain an address object that represents any address.
any()209   static address_v4 any() BOOST_ASIO_NOEXCEPT
210   {
211     return address_v4();
212   }
213 
214   /// Obtain an address object that represents the loopback address.
loopback()215   static address_v4 loopback() BOOST_ASIO_NOEXCEPT
216   {
217     return address_v4(0x7F000001);
218   }
219 
220   /// Obtain an address object that represents the broadcast address.
broadcast()221   static address_v4 broadcast() BOOST_ASIO_NOEXCEPT
222   {
223     return address_v4(0xFFFFFFFF);
224   }
225 
226 #if !defined(BOOST_ASIO_NO_DEPRECATED)
227   /// (Deprecated: Use network_v4 class.) Obtain an address object that
228   /// represents the broadcast address that corresponds to the specified
229   /// address and netmask.
230   BOOST_ASIO_DECL static address_v4 broadcast(
231       const address_v4& addr, const address_v4& mask);
232 
233   /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
234   /// to the address, based on its address class.
235   BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
236 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
237 
238 private:
239   // The underlying IPv4 address.
240   boost::asio::detail::in4_addr_type addr_;
241 };
242 
243 /// Create an IPv4 address from raw bytes in network order.
244 /**
245  * @relates address_v4
246  */
make_address_v4(const address_v4::bytes_type & bytes)247 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
248 {
249   return address_v4(bytes);
250 }
251 
252 /// Create an IPv4 address from an unsigned integer in host byte order.
253 /**
254  * @relates address_v4
255  */
make_address_v4(address_v4::uint_type addr)256 inline address_v4 make_address_v4(address_v4::uint_type addr)
257 {
258   return address_v4(addr);
259 }
260 
261 /// Create an IPv4 address from an IP address string in dotted decimal form.
262 /**
263  * @relates address_v4
264  */
265 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
266 
267 /// Create an IPv4 address from an IP address string in dotted decimal form.
268 /**
269  * @relates address_v4
270  */
271 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
272     boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
273 
274 /// Create an IPv4 address from an IP address string in dotted decimal form.
275 /**
276  * @relates address_v4
277  */
278 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
279 
280 /// Create an IPv4 address from an IP address string in dotted decimal form.
281 /**
282  * @relates address_v4
283  */
284 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
285     boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
286 
287 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
288   || defined(GENERATING_DOCUMENTATION)
289 
290 /// Create an IPv4 address from an IP address string in dotted decimal form.
291 /**
292  * @relates address_v4
293  */
294 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
295 
296 /// Create an IPv4 address from an IP address string in dotted decimal form.
297 /**
298  * @relates address_v4
299  */
300 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
301     boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;
302 
303 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
304        //  || defined(GENERATING_DOCUMENTATION)
305 
306 #if !defined(BOOST_ASIO_NO_IOSTREAM)
307 
308 /// Output an address as a string.
309 /**
310  * Used to output a human-readable string for a specified address.
311  *
312  * @param os The output stream to which the string will be written.
313  *
314  * @param addr The address to be written.
315  *
316  * @return The output stream.
317  *
318  * @relates boost::asio::ip::address_v4
319  */
320 template <typename Elem, typename Traits>
321 std::basic_ostream<Elem, Traits>& operator<<(
322     std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
323 
324 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
325 
326 } // namespace ip
327 } // namespace asio
328 } // namespace boost
329 
330 #include <boost/asio/detail/pop_options.hpp>
331 
332 #include <boost/asio/ip/impl/address_v4.hpp>
333 #if defined(BOOST_ASIO_HEADER_ONLY)
334 # include <boost/asio/ip/impl/address_v4.ipp>
335 #endif // defined(BOOST_ASIO_HEADER_ONLY)
336 
337 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP
338