1 | // |
2 | // ip/address_v4.hpp |
3 | // ~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2015 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/socket_types.hpp> |
22 | #include <boost/asio/detail/winsock_init.hpp> |
23 | #include <boost/system/error_code.hpp> |
24 | |
25 | #if !defined(BOOST_ASIO_NO_IOSTREAM) |
26 | # include <iosfwd> |
27 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) |
28 | |
29 | #include <boost/asio/detail/push_options.hpp> |
30 | |
31 | namespace boost { |
32 | namespace asio { |
33 | namespace ip { |
34 | |
35 | /// Implements IP version 4 style addresses. |
36 | /** |
37 | * The boost::asio::ip::address_v4 class provides the ability to use and |
38 | * manipulate IP version 4 addresses. |
39 | * |
40 | * @par Thread Safety |
41 | * @e Distinct @e objects: Safe.@n |
42 | * @e Shared @e objects: Unsafe. |
43 | */ |
44 | class address_v4 |
45 | { |
46 | public: |
47 | /// The type used to represent an address as an array of bytes. |
48 | /** |
49 | * @note This type is defined in terms of the C++0x template @c std::array |
50 | * when it is available. Otherwise, it uses @c boost:array. |
51 | */ |
52 | #if defined(GENERATING_DOCUMENTATION) |
53 | typedef array<unsigned char, 4> bytes_type; |
54 | #else |
55 | typedef boost::asio::detail::array<unsigned char, 4> bytes_type; |
56 | #endif |
57 | |
58 | /// Default constructor. |
59 | address_v4() |
60 | { |
61 | addr_.s_addr = 0; |
62 | } |
63 | |
64 | /// Construct an address from raw bytes. |
65 | BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes); |
66 | |
67 | /// Construct an address from a unsigned long in host byte order. |
68 | BOOST_ASIO_DECL explicit address_v4(unsigned long addr); |
69 | |
70 | /// Copy constructor. |
71 | address_v4(const address_v4& other) |
72 | : addr_(other.addr_) |
73 | { |
74 | } |
75 | |
76 | #if defined(BOOST_ASIO_HAS_MOVE) |
77 | /// Move constructor. |
78 | address_v4(address_v4&& other) |
79 | : addr_(other.addr_) |
80 | { |
81 | } |
82 | #endif // defined(BOOST_ASIO_HAS_MOVE) |
83 | |
84 | /// Assign from another address. |
85 | address_v4& operator=(const address_v4& other) |
86 | { |
87 | addr_ = other.addr_; |
88 | return *this; |
89 | } |
90 | |
91 | #if defined(BOOST_ASIO_HAS_MOVE) |
92 | /// Move-assign from another address. |
93 | address_v4& operator=(address_v4&& other) |
94 | { |
95 | addr_ = other.addr_; |
96 | return *this; |
97 | } |
98 | #endif // defined(BOOST_ASIO_HAS_MOVE) |
99 | |
100 | /// Get the address in bytes, in network byte order. |
101 | BOOST_ASIO_DECL bytes_type to_bytes() const; |
102 | |
103 | /// Get the address as an unsigned long in host byte order |
104 | BOOST_ASIO_DECL unsigned long to_ulong() const; |
105 | |
106 | /// Get the address as a string in dotted decimal format. |
107 | BOOST_ASIO_DECL std::string to_string() const; |
108 | |
109 | /// Get the address as a string in dotted decimal format. |
110 | BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const; |
111 | |
112 | /// Create an address from an IP address string in dotted decimal form. |
113 | BOOST_ASIO_DECL static address_v4 from_string(const char* str); |
114 | |
115 | /// Create an address from an IP address string in dotted decimal form. |
116 | BOOST_ASIO_DECL static address_v4 from_string( |
117 | const char* str, boost::system::error_code& ec); |
118 | |
119 | /// Create an address from an IP address string in dotted decimal form. |
120 | BOOST_ASIO_DECL static address_v4 from_string(const std::string& str); |
121 | |
122 | /// Create an address from an IP address string in dotted decimal form. |
123 | BOOST_ASIO_DECL static address_v4 from_string( |
124 | const std::string& str, boost::system::error_code& ec); |
125 | |
126 | /// Determine whether the address is a loopback address. |
127 | BOOST_ASIO_DECL bool is_loopback() const; |
128 | |
129 | /// Determine whether the address is unspecified. |
130 | BOOST_ASIO_DECL bool is_unspecified() const; |
131 | |
132 | /// Determine whether the address is a class A address. |
133 | BOOST_ASIO_DECL bool is_class_a() const; |
134 | |
135 | /// Determine whether the address is a class B address. |
136 | BOOST_ASIO_DECL bool is_class_b() const; |
137 | |
138 | /// Determine whether the address is a class C address. |
139 | BOOST_ASIO_DECL bool is_class_c() const; |
140 | |
141 | /// Determine whether the address is a multicast address. |
142 | BOOST_ASIO_DECL bool is_multicast() const; |
143 | |
144 | /// Compare two addresses for equality. |
145 | friend bool operator==(const address_v4& a1, const address_v4& a2) |
146 | { |
147 | return a1.addr_.s_addr == a2.addr_.s_addr; |
148 | } |
149 | |
150 | /// Compare two addresses for inequality. |
151 | friend bool operator!=(const address_v4& a1, const address_v4& a2) |
152 | { |
153 | return a1.addr_.s_addr != a2.addr_.s_addr; |
154 | } |
155 | |
156 | /// Compare addresses for ordering. |
157 | friend bool operator<(const address_v4& a1, const address_v4& a2) |
158 | { |
159 | return a1.to_ulong() < a2.to_ulong(); |
160 | } |
161 | |
162 | /// Compare addresses for ordering. |
163 | friend bool operator>(const address_v4& a1, const address_v4& a2) |
164 | { |
165 | return a1.to_ulong() > a2.to_ulong(); |
166 | } |
167 | |
168 | /// Compare addresses for ordering. |
169 | friend bool operator<=(const address_v4& a1, const address_v4& a2) |
170 | { |
171 | return a1.to_ulong() <= a2.to_ulong(); |
172 | } |
173 | |
174 | /// Compare addresses for ordering. |
175 | friend bool operator>=(const address_v4& a1, const address_v4& a2) |
176 | { |
177 | return a1.to_ulong() >= a2.to_ulong(); |
178 | } |
179 | |
180 | /// Obtain an address object that represents any address. |
181 | static address_v4 any() |
182 | { |
183 | return address_v4(); |
184 | } |
185 | |
186 | /// Obtain an address object that represents the loopback address. |
187 | static address_v4 loopback() |
188 | { |
189 | return address_v4(0x7F000001); |
190 | } |
191 | |
192 | /// Obtain an address object that represents the broadcast address. |
193 | static address_v4 broadcast() |
194 | { |
195 | return address_v4(0xFFFFFFFF); |
196 | } |
197 | |
198 | /// Obtain an address object that represents the broadcast address that |
199 | /// corresponds to the specified address and netmask. |
200 | BOOST_ASIO_DECL static address_v4 broadcast( |
201 | const address_v4& addr, const address_v4& mask); |
202 | |
203 | /// Obtain the netmask that corresponds to the address, based on its address |
204 | /// class. |
205 | BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr); |
206 | |
207 | private: |
208 | // The underlying IPv4 address. |
209 | boost::asio::detail::in4_addr_type addr_; |
210 | }; |
211 | |
212 | #if !defined(BOOST_ASIO_NO_IOSTREAM) |
213 | |
214 | /// Output an address as a string. |
215 | /** |
216 | * Used to output a human-readable string for a specified address. |
217 | * |
218 | * @param os The output stream to which the string will be written. |
219 | * |
220 | * @param addr The address to be written. |
221 | * |
222 | * @return The output stream. |
223 | * |
224 | * @relates boost::asio::ip::address_v4 |
225 | */ |
226 | template <typename Elem, typename Traits> |
227 | std::basic_ostream<Elem, Traits>& operator<<( |
228 | std::basic_ostream<Elem, Traits>& os, const address_v4& addr); |
229 | |
230 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) |
231 | |
232 | } // namespace ip |
233 | } // namespace asio |
234 | } // namespace boost |
235 | |
236 | #include <boost/asio/detail/pop_options.hpp> |
237 | |
238 | #include <boost/asio/ip/impl/address_v4.hpp> |
239 | #if defined(BOOST_ASIO_HEADER_ONLY) |
240 | # include <boost/asio/ip/impl/address_v4.ipp> |
241 | #endif // defined(BOOST_ASIO_HEADER_ONLY) |
242 | |
243 | #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP |
244 | |