5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11
#ifndef ASIO_IP_ADDRESS_V4_HPP
12
#define ASIO_IP_ADDRESS_V4_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
22
#include <boost/array.hpp>
23
#include <boost/throw_exception.hpp>
24
#include "asio/detail/pop_options.hpp"
26
#include "asio/error.hpp"
27
#include "asio/detail/socket_ops.hpp"
28
#include "asio/detail/socket_types.hpp"
29
#include "asio/detail/throw_error.hpp"
34
/// Implements IP version 4 style addresses.
36
* The asio::ip::address_v4 class provides the ability to use and
37
* manipulate IP version 4 addresses.
40
* @e Distinct @e objects: Safe.@n
41
* @e Shared @e objects: Unsafe.
46
/// The type used to represent an address as an array of bytes.
47
typedef boost::array<unsigned char, 4> bytes_type;
49
/// Default constructor.
55
/// Construct an address from raw bytes.
56
explicit address_v4(const bytes_type& bytes)
58
using namespace std; // For memcpy.
59
memcpy(&addr_.s_addr, bytes.elems, 4);
62
/// Construct an address from a unsigned long in host byte order.
63
explicit address_v4(unsigned long addr)
65
addr_.s_addr = asio::detail::socket_ops::host_to_network_long(addr);
69
address_v4(const address_v4& other)
74
/// Assign from another address.
75
address_v4& operator=(const address_v4& other)
81
/// Get the address in bytes.
82
bytes_type to_bytes() const
84
using namespace std; // For memcpy.
86
memcpy(bytes.elems, &addr_.s_addr, 4);
90
/// Get the address as an unsigned long in host byte order
91
unsigned long to_ulong() const
93
return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
96
/// Get the address as a string in dotted decimal format.
97
std::string to_string() const
100
std::string addr = to_string(ec);
101
asio::detail::throw_error(ec);
105
/// Get the address as a string in dotted decimal format.
106
std::string to_string(asio::error_code& ec) const
108
char addr_str[asio::detail::max_addr_v4_str_len];
110
asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str,
111
asio::detail::max_addr_v4_str_len, 0, ec);
113
return std::string();
117
/// Create an address from an IP address string in dotted decimal form.
118
static address_v4 from_string(const char* str)
121
address_v4 addr = from_string(str, ec);
122
asio::detail::throw_error(ec);
126
/// Create an address from an IP address string in dotted decimal form.
127
static address_v4 from_string(const char* str, asio::error_code& ec)
130
if (asio::detail::socket_ops::inet_pton(
131
AF_INET, str, &tmp.addr_, 0, ec) <= 0)
136
/// Create an address from an IP address string in dotted decimal form.
137
static address_v4 from_string(const std::string& str)
139
return from_string(str.c_str());
142
/// Create an address from an IP address string in dotted decimal form.
143
static address_v4 from_string(const std::string& str,
144
asio::error_code& ec)
146
return from_string(str.c_str(), ec);
149
/// Determine whether the address is a class A address.
150
bool is_class_a() const
152
return IN_CLASSA(to_ulong());
155
/// Determine whether the address is a class B address.
156
bool is_class_b() const
158
return IN_CLASSB(to_ulong());
161
/// Determine whether the address is a class C address.
162
bool is_class_c() const
164
return IN_CLASSC(to_ulong());
167
/// Determine whether the address is a multicast address.
168
bool is_multicast() const
170
return IN_MULTICAST(to_ulong());
173
/// Compare two addresses for equality.
174
friend bool operator==(const address_v4& a1, const address_v4& a2)
176
return a1.addr_.s_addr == a2.addr_.s_addr;
179
/// Compare two addresses for inequality.
180
friend bool operator!=(const address_v4& a1, const address_v4& a2)
182
return a1.addr_.s_addr != a2.addr_.s_addr;
185
/// Compare addresses for ordering.
186
friend bool operator<(const address_v4& a1, const address_v4& a2)
188
return a1.to_ulong() < a2.to_ulong();
191
/// Compare addresses for ordering.
192
friend bool operator>(const address_v4& a1, const address_v4& a2)
194
return a1.to_ulong() > a2.to_ulong();
197
/// Compare addresses for ordering.
198
friend bool operator<=(const address_v4& a1, const address_v4& a2)
200
return a1.to_ulong() <= a2.to_ulong();
203
/// Compare addresses for ordering.
204
friend bool operator>=(const address_v4& a1, const address_v4& a2)
206
return a1.to_ulong() >= a2.to_ulong();
209
/// Obtain an address object that represents any address.
210
static address_v4 any()
212
return address_v4(static_cast<unsigned long>(INADDR_ANY));
215
/// Obtain an address object that represents the loopback address.
216
static address_v4 loopback()
218
return address_v4(static_cast<unsigned long>(INADDR_LOOPBACK));
221
/// Obtain an address object that represents the broadcast address.
222
static address_v4 broadcast()
224
return address_v4(static_cast<unsigned long>(INADDR_BROADCAST));
227
/// Obtain an address object that represents the broadcast address that
228
/// corresponds to the specified address and netmask.
229
static address_v4 broadcast(const address_v4& addr, const address_v4& mask)
231
return address_v4(addr.to_ulong() | ~mask.to_ulong());
234
/// Obtain the netmask that corresponds to the address, based on its address
236
static address_v4 netmask(const address_v4& addr)
238
if (addr.is_class_a())
239
return address_v4(0xFF000000);
240
if (addr.is_class_b())
241
return address_v4(0xFFFF0000);
242
if (addr.is_class_c())
243
return address_v4(0xFFFFFF00);
244
return address_v4(0xFFFFFFFF);
248
// The underlying IPv4 address.
249
asio::detail::in4_addr_type addr_;
252
/// Output an address as a string.
254
* Used to output a human-readable string for a specified address.
256
* @param os The output stream to which the string will be written.
258
* @param addr The address to be written.
260
* @return The output stream.
262
* @relates asio::ip::address_v4
264
template <typename Elem, typename Traits>
265
std::basic_ostream<Elem, Traits>& operator<<(
266
std::basic_ostream<Elem, Traits>& os, const address_v4& addr)
269
std::string s = addr.to_string(ec);
272
if (os.exceptions() & std::ios::failbit)
273
asio::detail::throw_error(ec);
275
os.setstate(std::ios_base::failbit);
278
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
286
#include "asio/detail/pop_options.hpp"
288
#endif // ASIO_IP_ADDRESS_V4_HPP