Clementine
network_v4.hpp
1 //
2 // ip/network_v4.hpp
3 // ~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_IP_NETWORK_V4_HPP
13 #define 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 "asio/detail/config.hpp"
20 #include <string>
21 #include "asio/detail/string_view.hpp"
22 #include "asio/error_code.hpp"
23 #include "asio/ip/address_v4_range.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 namespace ip {
29 
31 
40 {
41 public:
43  network_v4() ASIO_NOEXCEPT
44  : address_(),
45  prefix_length_(0)
46  {
47  }
48 
50  ASIO_DECL network_v4(const address_v4& addr,
51  unsigned short prefix_len);
52 
54  ASIO_DECL network_v4(const address_v4& addr,
55  const address_v4& mask);
56 
58  network_v4(const network_v4& other) ASIO_NOEXCEPT
59  : address_(other.address_),
60  prefix_length_(other.prefix_length_)
61  {
62  }
63 
64 #if defined(ASIO_HAS_MOVE)
65  network_v4(network_v4&& other) ASIO_NOEXCEPT
67  : address_(ASIO_MOVE_CAST(address_v4)(other.address_)),
68  prefix_length_(other.prefix_length_)
69  {
70  }
71 #endif // defined(ASIO_HAS_MOVE)
72 
74  network_v4& operator=(const network_v4& other) ASIO_NOEXCEPT
75  {
76  address_ = other.address_;
77  prefix_length_ = other.prefix_length_;
78  return *this;
79  }
80 
81 #if defined(ASIO_HAS_MOVE)
82  network_v4& operator=(network_v4&& other) ASIO_NOEXCEPT
84  {
85  address_ = ASIO_MOVE_CAST(address_v4)(other.address_);
86  prefix_length_ = other.prefix_length_;
87  return *this;
88  }
89 #endif // defined(ASIO_HAS_MOVE)
90 
92  address_v4 address() const ASIO_NOEXCEPT
93  {
94  return address_;
95  }
96 
99  unsigned short prefix_length() const ASIO_NOEXCEPT
100  {
101  return prefix_length_;
102  }
103 
105  ASIO_DECL address_v4 netmask() const ASIO_NOEXCEPT;
106 
108  address_v4 network() const ASIO_NOEXCEPT
109  {
110  return address_v4(address_.to_uint() & netmask().to_uint());
111  }
112 
114  address_v4 broadcast() const ASIO_NOEXCEPT
115  {
116  return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF));
117  }
118 
120  ASIO_DECL address_v4_range hosts() const ASIO_NOEXCEPT;
121 
123  network_v4 canonical() const ASIO_NOEXCEPT
124  {
125  return network_v4(network(), netmask());
126  }
127 
129  bool is_host() const ASIO_NOEXCEPT
130  {
131  return prefix_length_ == 32;
132  }
133 
135  ASIO_DECL bool is_subnet_of(const network_v4& other) const;
136 
138  ASIO_DECL std::string to_string() const;
139 
141  ASIO_DECL std::string to_string(asio::error_code& ec) const;
142 
144  friend bool operator==(const network_v4& a, const network_v4& b)
145  {
146  return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
147  }
148 
150  friend bool operator!=(const network_v4& a, const network_v4& b)
151  {
152  return !(a == b);
153  }
154 
155 private:
156  address_v4 address_;
157  unsigned short prefix_length_;
158 };
159 
161 
165  const address_v4& addr, unsigned short prefix_len)
166 {
167  return network_v4(addr, prefix_len);
168 }
169 
171 
175  const address_v4& addr, const address_v4& mask)
176 {
177  return network_v4(addr, mask);
178 }
179 
182 
185 ASIO_DECL network_v4 make_network_v4(const char* str);
186 
189 
192 ASIO_DECL network_v4 make_network_v4(
193  const char* str, asio::error_code& ec);
194 
197 
200 ASIO_DECL network_v4 make_network_v4(const std::string& str);
201 
204 
207 ASIO_DECL network_v4 make_network_v4(
208  const std::string& str, asio::error_code& ec);
209 
210 #if defined(ASIO_HAS_STRING_VIEW) \
211  || defined(GENERATING_DOCUMENTATION)
212 
215 
218 ASIO_DECL network_v4 make_network_v4(string_view str);
219 
222 
225 ASIO_DECL network_v4 make_network_v4(
226  string_view str, asio::error_code& ec);
227 
228 #endif // defined(ASIO_HAS_STRING_VIEW)
229  // || defined(GENERATING_DOCUMENTATION)
230 
231 #if !defined(ASIO_NO_IOSTREAM)
232 
234 
245 template <typename Elem, typename Traits>
246 std::basic_ostream<Elem, Traits>& operator<<(
247  std::basic_ostream<Elem, Traits>& os, const network_v4& net);
248 
249 #endif // !defined(ASIO_NO_IOSTREAM)
250 
251 } // namespace ip
252 } // namespace asio
253 
254 #include "asio/detail/pop_options.hpp"
255 
256 #include "asio/ip/impl/network_v4.hpp"
257 #if defined(ASIO_HEADER_ONLY)
258 # include "asio/ip/impl/network_v4.ipp"
259 #endif // defined(ASIO_HEADER_ONLY)
260 
261 #endif // ASIO_IP_NETWORK_V4_HPP
ASIO_DECL network_v4 make_network_v4(const char *str)
Create an IPv4 network from a string containing IP address and prefix length.
Definition: network_v4.ipp:140
friend bool operator!=(const network_v4 &a, const network_v4 &b)
Compare two networks for inequality.
Definition: network_v4.hpp:150
unsigned short prefix_length() const ASIO_NOEXCEPT
Obtain the prefix length that was specified when the network object was created.
Definition: network_v4.hpp:99
Implements IP version 4 style addresses.
Definition: address_v4.hpp:45
Represents a range of IPv4 addresses.
Definition: address_v4_range.hpp:34
network_v4 make_network_v4(const address_v4 &addr, unsigned short prefix_len)
Create an IPv4 network from an address and prefix length.
Definition: network_v4.hpp:164
network_v4 & operator=(const network_v4 &other) ASIO_NOEXCEPT
Assign from another network.
Definition: network_v4.hpp:74
address_v4 broadcast() const ASIO_NOEXCEPT
Obtain an address object that represents the network&#39;s broadcast address.
Definition: network_v4.hpp:114
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
Represents an IPv4 network.
Definition: network_v4.hpp:39
bool is_host() const ASIO_NOEXCEPT
Test if network is a valid host address.
Definition: network_v4.hpp:129
address_v4 address() const ASIO_NOEXCEPT
Obtain the address object specified when the network object was created.
Definition: network_v4.hpp:92
network_v4(const network_v4 &other) ASIO_NOEXCEPT
Copy constructor.
Definition: network_v4.hpp:58
ASIO_DECL std::string to_string() const
Get the network as an address in dotted decimal format.
Definition: network_v4.ipp:119
friend bool operator==(const network_v4 &a, const network_v4 &b)
Compare two networks for equality.
Definition: network_v4.hpp:144
network_v4 make_network_v4(const address_v4 &addr, const address_v4 &mask)
Create an IPv4 network from an address and netmask.
Definition: network_v4.hpp:174
Class to represent an error code value.
Definition: error_code.hpp:80
network_v4 canonical() const ASIO_NOEXCEPT
Obtain the true network address, omitting any host bits.
Definition: network_v4.hpp:123
ASIO_DECL bool is_subnet_of(const network_v4 &other) const
Test if a network is a real subnet of another network.
Definition: network_v4.ipp:111
ASIO_DECL address_v4_range hosts() const ASIO_NOEXCEPT
Obtain an address range corresponding to the hosts in the network.
Definition: network_v4.ipp:104
ASIO_DECL address_v4 netmask() const ASIO_NOEXCEPT
Obtain the netmask that was specified when the network object was created.
Definition: network_v4.ipp:94
Definition: any_io_executor.hpp:28
address_v4 network() const ASIO_NOEXCEPT
Obtain an address object that represents the network address.
Definition: network_v4.hpp:108
network_v4() ASIO_NOEXCEPT
Default constructor.
Definition: network_v4.hpp:43