1 | // |
---|
2 | // IPAddress.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/IPAddress.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: NetCore |
---|
8 | // Module: IPAddress |
---|
9 | // |
---|
10 | // Definition of the IPAddress class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_IPAddress_INCLUDED |
---|
40 | #define Net_IPAddress_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/SocketDefs.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace Net { |
---|
49 | |
---|
50 | |
---|
51 | class IPAddressImpl; |
---|
52 | |
---|
53 | |
---|
54 | class Net_API IPAddress |
---|
55 | /// This class represents an internet (IP) host |
---|
56 | /// address. The address can belong either to the |
---|
57 | /// IPv4 or the IPv6 address family. |
---|
58 | /// |
---|
59 | /// Relational operators (==, !=, <, <=, >, >=) are |
---|
60 | /// supported. However, you must not interpret any |
---|
61 | /// special meaning into the result of these |
---|
62 | /// operations, other than that the results are |
---|
63 | /// consistent. |
---|
64 | /// |
---|
65 | /// Especially, an IPv4 address is never equal to |
---|
66 | /// an IPv6 address, even if the IPv6 address is |
---|
67 | /// IPv4 compatible and the addresses are the same. |
---|
68 | /// |
---|
69 | /// IPv6 addresses are supported only if the target platform |
---|
70 | /// supports IPv6. |
---|
71 | { |
---|
72 | public: |
---|
73 | enum Family |
---|
74 | /// Possible address families for IP addresses. |
---|
75 | { |
---|
76 | IPv4, |
---|
77 | IPv6 |
---|
78 | }; |
---|
79 | |
---|
80 | IPAddress(); |
---|
81 | /// Creates a wildcard (zero) IPv4 IPAddress. |
---|
82 | |
---|
83 | IPAddress(const IPAddress& addr); |
---|
84 | /// Creates an IPAddress by copying another one. |
---|
85 | |
---|
86 | explicit IPAddress(Family family); |
---|
87 | /// Creates a wildcard (zero) IPAddress for the |
---|
88 | /// given address family. |
---|
89 | |
---|
90 | explicit IPAddress(const std::string& addr); |
---|
91 | /// Creates an IPAddress from the string containing |
---|
92 | /// an IP address in presentation format (dotted decimal |
---|
93 | /// for IPv4, hex string for IPv6). |
---|
94 | /// |
---|
95 | /// Depending on the format of addr, either an IPv4 or |
---|
96 | /// an IPv6 address is created. |
---|
97 | /// |
---|
98 | /// See toString() for details on the supported formats. |
---|
99 | /// |
---|
100 | /// Throws an InvalidAddressException if the address cannot be parsed. |
---|
101 | |
---|
102 | IPAddress(const std::string& addr, Family family); |
---|
103 | /// Creates an IPAddress from the string containing |
---|
104 | /// an IP address in presentation format (dotted decimal |
---|
105 | /// for IPv4, hex string for IPv6). |
---|
106 | |
---|
107 | IPAddress(const void* addr, poco_socklen_t length); |
---|
108 | /// Creates an IPAddress from a native internet address. |
---|
109 | /// A pointer to a in_addr or a in6_addr structure may be |
---|
110 | /// passed. |
---|
111 | |
---|
112 | ~IPAddress(); |
---|
113 | /// Destroys the IPAddress. |
---|
114 | |
---|
115 | IPAddress& operator = (const IPAddress& addr); |
---|
116 | /// Assigns an IPAddress. |
---|
117 | |
---|
118 | void swap(IPAddress& address); |
---|
119 | /// Swaps the IPAddress with another one. |
---|
120 | |
---|
121 | Family family() const; |
---|
122 | /// Returns the address family (IPv4 or IPv6) of the address. |
---|
123 | |
---|
124 | std::string toString() const; |
---|
125 | /// Returns a string containing a representation of the address |
---|
126 | /// in presentation format. |
---|
127 | /// |
---|
128 | /// For IPv4 addresses the result will be in dotted-decimal |
---|
129 | /// (d.d.d.d) notation. |
---|
130 | /// |
---|
131 | /// Textual representation of IPv6 address is one of the following forms: |
---|
132 | /// |
---|
133 | /// The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal |
---|
134 | /// values of the eight 16-bit pieces of the address. This is the full form. |
---|
135 | /// Example: 1080:0:0:0:8:600:200A:425C |
---|
136 | /// |
---|
137 | /// It is not necessary to write the leading zeros in an individual field. |
---|
138 | /// However, there must be at least one numeral in every field, except as described below. |
---|
139 | /// |
---|
140 | /// It is common for IPv6 addresses to contain long strings of zero bits. |
---|
141 | /// In order to make writing addresses containing zero bits easier, a special syntax is |
---|
142 | /// available to compress the zeros. The use of "::" indicates multiple groups of 16-bits of zeros. |
---|
143 | /// The "::" can only appear once in an address. The "::" can also be used to compress the leading |
---|
144 | /// and/or trailing zeros in an address. Example: 1080::8:600:200A:425C |
---|
145 | /// |
---|
146 | /// For dealing with IPv4 compatible addresses in a mixed environment, |
---|
147 | /// a special syntax is available: x:x:x:x:x:x:d.d.d.d, where the 'x's are the |
---|
148 | /// hexadecimal values of the six high-order 16-bit pieces of the address, |
---|
149 | /// and the 'd's are the decimal values of the four low-order 8-bit pieces of the |
---|
150 | /// standard IPv4 representation address. Example: ::FFFF:192.168.1.120 |
---|
151 | |
---|
152 | bool isWildcard() const; |
---|
153 | /// Returns true iff the address is a wildcard (all zero) |
---|
154 | /// address. |
---|
155 | |
---|
156 | bool isBroadcast() const; |
---|
157 | /// Returns true iff the address is a broadcast address. |
---|
158 | /// |
---|
159 | /// Only IPv4 addresses can be broadcast addresses. In a broadcast |
---|
160 | /// address, all bits are one. |
---|
161 | /// |
---|
162 | /// For a IPv6 address, returns always false. |
---|
163 | |
---|
164 | bool isLoopback() const; |
---|
165 | /// Returns true iff the address is a loopback address. |
---|
166 | /// |
---|
167 | /// For IPv4, the loopback address is 127.0.0.1. |
---|
168 | /// |
---|
169 | /// For IPv6, the loopback address is ::1. |
---|
170 | |
---|
171 | bool isMulticast() const; |
---|
172 | /// Returns true iff the address is a multicast address. |
---|
173 | /// |
---|
174 | /// IPv4 multicast addresses are in the |
---|
175 | /// 224.0.0.0 to 239.255.255.255 range |
---|
176 | /// (the first four bits have the value 1110). |
---|
177 | /// |
---|
178 | /// IPv6 multicast addresses are in the |
---|
179 | /// FFxx:x:x:x:x:x:x:x range. |
---|
180 | |
---|
181 | bool isUnicast() const; |
---|
182 | /// Returns true iff the address is a unicast address. |
---|
183 | /// |
---|
184 | /// An address is unicast if it is neither a wildcard, |
---|
185 | /// broadcast or multicast address. |
---|
186 | |
---|
187 | bool isLinkLocal() const; |
---|
188 | /// Returns true iff the address is a link local unicast address. |
---|
189 | /// |
---|
190 | /// IPv4 link local addresses are in the 169.254.0.0/16 range, |
---|
191 | /// according to RFC 3927. |
---|
192 | /// |
---|
193 | /// IPv6 link local addresses have 1111 1110 10 as the first |
---|
194 | /// 10 bits, followed by 54 zeros. |
---|
195 | |
---|
196 | bool isSiteLocal() const; |
---|
197 | /// Returns true iff the address is a site local unicast address. |
---|
198 | /// |
---|
199 | /// IPv4 site local addresses are in on of the 10.0.0.0/24, |
---|
200 | /// 192.168.0.0/16 or 172.16.0.0 to 172.31.255.255 ranges. |
---|
201 | /// |
---|
202 | /// IPv6 site local addresses have 1111 1110 11 as the first |
---|
203 | /// 10 bits, followed by 38 zeros. |
---|
204 | |
---|
205 | bool isIPv4Compatible() const; |
---|
206 | /// Returns true iff the address is IPv4 compatible. |
---|
207 | /// |
---|
208 | /// For IPv4 addresses, this is always true. |
---|
209 | /// |
---|
210 | /// For IPv6, the address must be in the ::x:x range (the |
---|
211 | /// first 96 bits are zero). |
---|
212 | |
---|
213 | bool isIPv4Mapped() const; |
---|
214 | /// Returns true iff the address is an IPv4 mapped IPv6 address. |
---|
215 | /// |
---|
216 | /// For IPv4 addresses, this is always true. |
---|
217 | /// |
---|
218 | /// For IPv6, the address must be in the ::FFFF:x:x range. |
---|
219 | |
---|
220 | bool isWellKnownMC() const; |
---|
221 | /// Returns true iff the address is a well-known multicast address. |
---|
222 | /// |
---|
223 | /// For IPv4, well-known multicast addresses are in the |
---|
224 | /// 224.0.0.0/8 range. |
---|
225 | /// |
---|
226 | /// For IPv6, well-known multicast addresses are in the |
---|
227 | /// FF0x:x:x:x:x:x:x:x range. |
---|
228 | |
---|
229 | bool isNodeLocalMC() const; |
---|
230 | /// Returns true iff the address is a node-local multicast address. |
---|
231 | /// |
---|
232 | /// IPv4 does not support node-local addresses, thus the result is |
---|
233 | /// always false for an IPv4 address. |
---|
234 | /// |
---|
235 | /// For IPv6, node-local multicast addresses are in the |
---|
236 | /// FFx1:x:x:x:x:x:x:x range. |
---|
237 | |
---|
238 | bool isLinkLocalMC() const; |
---|
239 | /// Returns true iff the address is a link-local multicast address. |
---|
240 | /// |
---|
241 | /// For IPv4, link-local multicast addresses are in the |
---|
242 | /// 224.0.0.0/24 range. Note that this overlaps with the range for well-known |
---|
243 | /// multicast addresses. |
---|
244 | /// |
---|
245 | /// For IPv6, link-local multicast addresses are in the |
---|
246 | /// FFx2:x:x:x:x:x:x:x range. |
---|
247 | |
---|
248 | bool isSiteLocalMC() const; |
---|
249 | /// Returns true iff the address is a site-local multicast address. |
---|
250 | /// |
---|
251 | /// For IPv4, site local multicast addresses are in the |
---|
252 | /// 239.255.0.0/16 range. |
---|
253 | /// |
---|
254 | /// For IPv6, site-local multicast addresses are in the |
---|
255 | /// FFx5:x:x:x:x:x:x:x range. |
---|
256 | |
---|
257 | bool isOrgLocalMC() const; |
---|
258 | /// Returns true iff the address is a organization-local multicast address. |
---|
259 | /// |
---|
260 | /// For IPv4, organization-local multicast addresses are in the |
---|
261 | /// 239.192.0.0/16 range. |
---|
262 | /// |
---|
263 | /// For IPv6, organization-local multicast addresses are in the |
---|
264 | /// FFx8:x:x:x:x:x:x:x range. |
---|
265 | |
---|
266 | bool isGlobalMC() const; |
---|
267 | /// Returns true iff the address is a global multicast address. |
---|
268 | /// |
---|
269 | /// For IPv4, global multicast addresses are in the |
---|
270 | /// 224.0.1.0 to 238.255.255.255 range. |
---|
271 | /// |
---|
272 | /// For IPv6, global multicast addresses are in the |
---|
273 | /// FFxF:x:x:x:x:x:x:x range. |
---|
274 | |
---|
275 | bool operator == (const IPAddress& addr) const; |
---|
276 | bool operator != (const IPAddress& addr) const; |
---|
277 | bool operator < (const IPAddress& addr) const; |
---|
278 | bool operator <= (const IPAddress& addr) const; |
---|
279 | bool operator > (const IPAddress& addr) const; |
---|
280 | bool operator >= (const IPAddress& addr) const; |
---|
281 | |
---|
282 | poco_socklen_t length() const; |
---|
283 | /// Returns the length in bytes of the internal socket address structure. |
---|
284 | |
---|
285 | const void* addr() const; |
---|
286 | /// Returns the internal address structure. |
---|
287 | |
---|
288 | int af() const; |
---|
289 | /// Returns the address family (AF_INET or AF_INET6) of the address. |
---|
290 | |
---|
291 | void mask(const IPAddress& mask); |
---|
292 | /// Masks the IP address using the given netmask, which is usually |
---|
293 | /// a IPv4 subnet mask. Only supported for IPv4 addresses. |
---|
294 | /// |
---|
295 | /// The new address is (address & mask). |
---|
296 | |
---|
297 | void mask(const IPAddress& mask, const IPAddress& set); |
---|
298 | /// Masks the IP address using the given netmask, which is usually |
---|
299 | /// a IPv4 subnet mask. Only supported for IPv4 addresses. |
---|
300 | /// |
---|
301 | /// The new address is (address & mask) | (set & ~mask). |
---|
302 | |
---|
303 | static IPAddress parse(const std::string& addr); |
---|
304 | /// Creates an IPAddress from the string containing |
---|
305 | /// an IP address in presentation format (dotted decimal |
---|
306 | /// for IPv4, hex string for IPv6). |
---|
307 | /// |
---|
308 | /// Depending on the format of addr, either an IPv4 or |
---|
309 | /// an IPv6 address is created. |
---|
310 | /// |
---|
311 | /// See toString() for details on the supported formats. |
---|
312 | /// |
---|
313 | /// Throws an InvalidAddressException if the address cannot be parsed. |
---|
314 | |
---|
315 | static bool tryParse(const std::string& addr, IPAddress& result); |
---|
316 | /// Tries to interpret the given address string as an |
---|
317 | /// IP address in presentation format (dotted decimal |
---|
318 | /// for IPv4, hex string for IPv6). |
---|
319 | /// |
---|
320 | /// Returns true and stores the IPAddress in result if the |
---|
321 | /// string contains a valid address. |
---|
322 | /// |
---|
323 | /// Returns false and leaves result unchanged otherwise. |
---|
324 | |
---|
325 | enum |
---|
326 | { |
---|
327 | MAX_ADDRESS_LENGTH = |
---|
328 | #if defined(POCO_HAVE_IPv6) |
---|
329 | sizeof(struct in6_addr) |
---|
330 | #else |
---|
331 | sizeof(struct in_addr) |
---|
332 | #endif |
---|
333 | /// Maximum length in bytes of a socket address. |
---|
334 | }; |
---|
335 | |
---|
336 | protected: |
---|
337 | void init(IPAddressImpl* pImpl); |
---|
338 | |
---|
339 | private: |
---|
340 | IPAddressImpl* _pImpl; |
---|
341 | }; |
---|
342 | |
---|
343 | |
---|
344 | // |
---|
345 | // inlines |
---|
346 | // |
---|
347 | inline void swap(IPAddress& a1, IPAddress& a2) |
---|
348 | { |
---|
349 | a1.swap(a2); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | } } // namespace Poco::Net |
---|
354 | |
---|
355 | |
---|
356 | #endif // Net_IPAddress_INCLUDED |
---|