Clementine
address_v4.hpp
1 //
2 // ip/address_v4.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_IP_ADDRESS_V4_HPP
12 #define 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 "asio/detail/config.hpp"
19 #include <string>
20 #include "asio/detail/array.hpp"
21 #include "asio/detail/cstdint.hpp"
22 #include "asio/detail/socket_types.hpp"
23 #include "asio/detail/string_view.hpp"
24 #include "asio/detail/winsock_init.hpp"
25 #include "asio/error_code.hpp"
26 
27 #if !defined(ASIO_NO_IOSTREAM)
28 # include <iosfwd>
29 #endif // !defined(ASIO_NO_IOSTREAM)
30 
31 #include "asio/detail/push_options.hpp"
32 
33 namespace asio {
34 namespace ip {
35 
37 
46 {
47 public:
49  typedef uint_least32_t uint_type;
50 
52 
56 #if defined(GENERATING_DOCUMENTATION)
57  typedef array<unsigned char, 4> bytes_type;
58 #else
60 #endif
61 
63  address_v4() ASIO_NOEXCEPT
64  {
65  addr_.s_addr = 0;
66  }
67 
69  ASIO_DECL explicit address_v4(const bytes_type& bytes);
70 
72  ASIO_DECL explicit address_v4(uint_type addr);
73 
75  address_v4(const address_v4& other) ASIO_NOEXCEPT
76  : addr_(other.addr_)
77  {
78  }
79 
80 #if defined(ASIO_HAS_MOVE)
81  address_v4(address_v4&& other) ASIO_NOEXCEPT
83  : addr_(other.addr_)
84  {
85  }
86 #endif // defined(ASIO_HAS_MOVE)
87 
89  address_v4& operator=(const address_v4& other) ASIO_NOEXCEPT
90  {
91  addr_ = other.addr_;
92  return *this;
93  }
94 
95 #if defined(ASIO_HAS_MOVE)
96  address_v4& operator=(address_v4&& other) ASIO_NOEXCEPT
98  {
99  addr_ = other.addr_;
100  return *this;
101  }
102 #endif // defined(ASIO_HAS_MOVE)
103 
105  ASIO_DECL bytes_type to_bytes() const ASIO_NOEXCEPT;
106 
108  ASIO_DECL uint_type to_uint() const ASIO_NOEXCEPT;
109 
110 #if !defined(ASIO_NO_DEPRECATED)
111  ASIO_DECL unsigned long to_ulong() const;
113 #endif // !defined(ASIO_NO_DEPRECATED)
114 
116  ASIO_DECL std::string to_string() const;
117 
118 #if !defined(ASIO_NO_DEPRECATED)
119  ASIO_DECL std::string to_string(asio::error_code& ec) const;
122 
125  static address_v4 from_string(const char* str);
126 
129  static address_v4 from_string(
130  const char* str, asio::error_code& ec);
131 
134  static address_v4 from_string(const std::string& str);
135 
138  static address_v4 from_string(
139  const std::string& str, asio::error_code& ec);
140 #endif // !defined(ASIO_NO_DEPRECATED)
141 
143  ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT;
144 
146  ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT;
147 
148 #if !defined(ASIO_NO_DEPRECATED)
149  ASIO_DECL bool is_class_a() const;
152 
155  ASIO_DECL bool is_class_b() const;
156 
159  ASIO_DECL bool is_class_c() const;
160 #endif // !defined(ASIO_NO_DEPRECATED)
161 
163  ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT;
164 
166  friend bool operator==(const address_v4& a1,
167  const address_v4& a2) ASIO_NOEXCEPT
168  {
169  return a1.addr_.s_addr == a2.addr_.s_addr;
170  }
171 
173  friend bool operator!=(const address_v4& a1,
174  const address_v4& a2) ASIO_NOEXCEPT
175  {
176  return a1.addr_.s_addr != a2.addr_.s_addr;
177  }
178 
180  friend bool operator<(const address_v4& a1,
181  const address_v4& a2) ASIO_NOEXCEPT
182  {
183  return a1.to_uint() < a2.to_uint();
184  }
185 
187  friend bool operator>(const address_v4& a1,
188  const address_v4& a2) ASIO_NOEXCEPT
189  {
190  return a1.to_uint() > a2.to_uint();
191  }
192 
194  friend bool operator<=(const address_v4& a1,
195  const address_v4& a2) ASIO_NOEXCEPT
196  {
197  return a1.to_uint() <= a2.to_uint();
198  }
199 
201  friend bool operator>=(const address_v4& a1,
202  const address_v4& a2) ASIO_NOEXCEPT
203  {
204  return a1.to_uint() >= a2.to_uint();
205  }
206 
208  static address_v4 any() ASIO_NOEXCEPT
209  {
210  return address_v4();
211  }
212 
214  static address_v4 loopback() ASIO_NOEXCEPT
215  {
216  return address_v4(0x7F000001);
217  }
218 
220  static address_v4 broadcast() ASIO_NOEXCEPT
221  {
222  return address_v4(0xFFFFFFFF);
223  }
224 
225 #if !defined(ASIO_NO_DEPRECATED)
226  ASIO_DECL static address_v4 broadcast(
230  const address_v4& addr, const address_v4& mask);
231 
234  ASIO_DECL static address_v4 netmask(const address_v4& addr);
235 #endif // !defined(ASIO_NO_DEPRECATED)
236 
237 private:
238  // The underlying IPv4 address.
239  asio::detail::in4_addr_type addr_;
240 };
241 
243 
247 {
248  return address_v4(bytes);
249 }
250 
252 
256 {
257  return address_v4(addr);
258 }
259 
261 
264 ASIO_DECL address_v4 make_address_v4(const char* str);
265 
267 
270 ASIO_DECL address_v4 make_address_v4(const char* str,
271  asio::error_code& ec) ASIO_NOEXCEPT;
272 
274 
277 ASIO_DECL address_v4 make_address_v4(const std::string& str);
278 
280 
283 ASIO_DECL address_v4 make_address_v4(const std::string& str,
284  asio::error_code& ec) ASIO_NOEXCEPT;
285 
286 #if defined(ASIO_HAS_STRING_VIEW) \
287  || defined(GENERATING_DOCUMENTATION)
288 
290 
293 ASIO_DECL address_v4 make_address_v4(string_view str);
294 
296 
300  asio::error_code& ec) ASIO_NOEXCEPT;
301 
302 #endif // defined(ASIO_HAS_STRING_VIEW)
303  // || defined(GENERATING_DOCUMENTATION)
304 
305 #if !defined(ASIO_NO_IOSTREAM)
306 
308 
319 template <typename Elem, typename Traits>
320 std::basic_ostream<Elem, Traits>& operator<<(
321  std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
322 
323 #endif // !defined(ASIO_NO_IOSTREAM)
324 
325 } // namespace ip
326 } // namespace asio
327 
328 #include "asio/detail/pop_options.hpp"
329 
330 #include "asio/ip/impl/address_v4.hpp"
331 #if defined(ASIO_HEADER_ONLY)
332 # include "asio/ip/impl/address_v4.ipp"
333 #endif // defined(ASIO_HEADER_ONLY)
334 
335 #endif // ASIO_IP_ADDRESS_V4_HPP
friend bool operator<(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address_v4.hpp:180
address_v4 make_address_v4(const address_v4::bytes_type &bytes)
Create an IPv4 address from raw bytes in network order.
Definition: address_v4.hpp:246
ASIO_DECL bool is_class_a() const
(Deprecated: Use network_v4 class.) Determine whether the address is a class A address.
Definition: address_v4.ipp:122
static ASIO_DECL address_v4 netmask(const address_v4 &addr)
(Deprecated: Use network_v4 class.) Obtain the netmask that corresponds to the address, based on its address class.
Definition: address_v4.ipp:149
ASIO_DECL bool is_class_b() const
(Deprecated: Use network_v4 class.) Determine whether the address is a class B address.
Definition: address_v4.ipp:127
address_v4() ASIO_NOEXCEPT
Default constructor.
Definition: address_v4.hpp:63
std::basic_ostream< Elem, Traits > & operator<<(std::basic_ostream< Elem, Traits > &os, const address_v4 &addr)
Output an address as a string.
Definition: address_v4.hpp:54
asio::detail::array< unsigned char, 4 > bytes_type
The type used to represent an address as an array of bytes.
Definition: address_v4.hpp:59
friend bool operator==(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare two addresses for equality.
Definition: address_v4.hpp:166
friend bool operator!=(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare two addresses for inequality.
Definition: address_v4.hpp:173
ASIO_DECL bytes_type to_bytes() const ASIO_NOEXCEPT
Get the address in bytes, in network byte order.
Definition: address_v4.ipp:60
Implements IP version 4 style addresses.
Definition: address_v4.hpp:45
uint_least32_t uint_type
The type used to represent an address as an unsigned integer.
Definition: address_v4.hpp:49
ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT
Determine whether the address is a loopback address.
Definition: address_v4.ipp:111
ASIO_DECL bool is_class_c() const
(Deprecated: Use network_v4 class.) Determine whether the address is a class C address.
Definition: address_v4.ipp:132
address_v4 & operator=(const address_v4 &other) ASIO_NOEXCEPT
Assign from another address.
Definition: address_v4.hpp:89
friend bool operator>(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address_v4.hpp:187
static address_v4 from_string(const char *str)
(Deprecated: Use make_address_v4().) Create an address from an IP address string in dotted decimal fo...
Definition: address_v4.hpp:29
address_v4 make_address_v4(address_v4::uint_type addr)
Create an IPv4 address from an unsigned integer in host byte order.
Definition: address_v4.hpp:255
ASIO_DECL uint_type to_uint() const ASIO_NOEXCEPT
Get the address as an unsigned integer in host byte order.
Definition: address_v4.ipp:72
friend bool operator<=(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address_v4.hpp:194
static address_v4 any() ASIO_NOEXCEPT
Obtain an address object that represents any address.
Definition: address_v4.hpp:208
ASIO_DECL std::string to_string() const
Get the address as a string in dotted decimal format.
Definition: address_v4.ipp:84
ASIO_DECL unsigned long to_ulong() const
Get the address as an unsigned long in host byte order.
Definition: address_v4.ipp:78
static address_v4 loopback() ASIO_NOEXCEPT
Obtain an address object that represents the loopback address.
Definition: address_v4.hpp:214
static address_v4 broadcast() ASIO_NOEXCEPT
Obtain an address object that represents the broadcast address.
Definition: address_v4.hpp:220
Class to represent an error code value.
Definition: error_code.hpp:80
address_v4(const address_v4 &other) ASIO_NOEXCEPT
Copy constructor.
Definition: address_v4.hpp:75
ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT
Determine whether the address is a multicast address.
Definition: address_v4.ipp:138
ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT
Determine whether the address is unspecified.
Definition: address_v4.ipp:116
Definition: any_io_executor.hpp:28
Definition: format.h:3611
Definition: array_fwd.hpp:23
friend bool operator>=(const address_v4 &a1, const address_v4 &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address_v4.hpp:201