IPAddress.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //
  2. // IPAddress.h
  3. //
  4. // $Id: //poco/1.4/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-2011, 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. #ifndef Net_IPAddress_INCLUDED
  38. #define Net_IPAddress_INCLUDED
  39. #include "Poco/Net/Net.h"
  40. #include "Poco/Net/SocketDefs.h"
  41. #include "Poco/Net/IPAddressImpl.h"
  42. #include <vector>
  43. #ifdef POCO_ENABLE_CPP11
  44. #include <type_traits>
  45. #else
  46. #include "Poco/Alignment.h"
  47. #endif
  48. namespace Poco {
  49. class BinaryReader;
  50. class BinaryWriter;
  51. namespace Net {
  52. class Net_API IPAddress
  53. /// This class represents an internet (IP) host
  54. /// address. The address can belong either to the
  55. /// IPv4 or the IPv6 address family.
  56. ///
  57. /// Relational operators (==, !=, <, <=, >, >=) are
  58. /// supported. However, you must not interpret any
  59. /// special meaning into the result of these
  60. /// operations, other than that the results are
  61. /// consistent.
  62. ///
  63. /// Especially, an IPv4 address is never equal to
  64. /// an IPv6 address, even if the IPv6 address is
  65. /// IPv4 compatible and the addresses are the same.
  66. ///
  67. /// IPv6 addresses are supported only if the target platform
  68. /// supports IPv6.
  69. {
  70. public:
  71. typedef std::vector<IPAddress> List;
  72. enum Family
  73. /// Possible address families for IP addresses.
  74. {
  75. IPv4 = Poco::Net::Impl::IPAddressImpl::IPv4
  76. #ifdef POCO_HAVE_IPv6
  77. ,IPv6 = Poco::Net::Impl::IPAddressImpl::IPv6
  78. #endif
  79. };
  80. IPAddress();
  81. /// Creates a wildcard (zero) IPv4 IPAddress.
  82. IPAddress(const IPAddress& addr);
  83. /// Creates an IPAddress by copying another one.
  84. explicit IPAddress(Family family);
  85. /// Creates a wildcard (zero) IPAddress for the
  86. /// given address family.
  87. explicit IPAddress(const std::string& addr);
  88. /// Creates an IPAddress from the string containing
  89. /// an IP address in presentation format (dotted decimal
  90. /// for IPv4, hex string for IPv6).
  91. ///
  92. /// Depending on the format of addr, either an IPv4 or
  93. /// an IPv6 address is created.
  94. ///
  95. /// See toString() for details on the supported formats.
  96. ///
  97. /// Throws an InvalidAddressException if the address cannot be parsed.
  98. IPAddress(const std::string& addr, Family family);
  99. /// Creates an IPAddress from the string containing
  100. /// an IP address in presentation format (dotted decimal
  101. /// for IPv4, hex string for IPv6).
  102. IPAddress(const void* addr, poco_socklen_t length);
  103. /// Creates an IPAddress from a native internet address.
  104. /// A pointer to a in_addr or a in6_addr structure may be
  105. /// passed.
  106. IPAddress(const void* addr, poco_socklen_t length, Poco::UInt32 scope);
  107. /// Creates an IPAddress from a native internet address.
  108. /// A pointer to a in_addr or a in6_addr structure may be
  109. /// passed. Additionally, for an IPv6 address, a scope ID
  110. /// may be specified. The scope ID will be ignored if an IPv4
  111. /// address is specified.
  112. IPAddress(unsigned prefix, Family family);
  113. /// Creates an IPAddress mask with the given length of prefix.
  114. #if defined(_WIN32)
  115. IPAddress(const SOCKET_ADDRESS& socket_address);
  116. /// Creates an IPAddress from Windows SOCKET_ADDRESS structure.
  117. #endif
  118. IPAddress(const struct sockaddr& sockaddr);
  119. /// Same for struct sock_addr on POSIX.
  120. ~IPAddress();
  121. /// Destroys the IPAddress.
  122. IPAddress& operator = (const IPAddress& addr);
  123. /// Assigns an IPAddress.
  124. Family family() const;
  125. /// Returns the address family (IPv4 or IPv6) of the address.
  126. Poco::UInt32 scope() const;
  127. /// Returns the IPv6 scope identifier of the address. Returns 0 if
  128. /// the address is an IPv4 address, or the address is an
  129. /// IPv6 address but does not have a scope identifier.
  130. std::string toString() const;
  131. /// Returns a string containing a representation of the address
  132. /// in presentation format.
  133. ///
  134. /// For IPv4 addresses the result will be in dotted-decimal
  135. /// (d.d.d.d) notation.
  136. ///
  137. /// Textual representation of IPv6 address is one of the following forms:
  138. ///
  139. /// The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal
  140. /// values of the eight 16-bit pieces of the address. This is the full form.
  141. /// Example: 1080:0:0:0:8:600:200A:425C
  142. ///
  143. /// It is not necessary to write the leading zeros in an individual field.
  144. /// However, there must be at least one numeral in every field, except as described below.
  145. ///
  146. /// It is common for IPv6 addresses to contain long strings of zero bits.
  147. /// In order to make writing addresses containing zero bits easier, a special syntax is
  148. /// available to compress the zeros. The use of "::" indicates multiple groups of 16-bits of zeros.
  149. /// The "::" can only appear once in an address. The "::" can also be used to compress the leading
  150. /// and/or trailing zeros in an address. Example: 1080::8:600:200A:425C
  151. ///
  152. /// For dealing with IPv4 compatible addresses in a mixed environment,
  153. /// a special syntax is available: x:x:x:x:x:x:d.d.d.d, where the 'x's are the
  154. /// hexadecimal values of the six high-order 16-bit pieces of the address,
  155. /// and the 'd's are the decimal values of the four low-order 8-bit pieces of the
  156. /// standard IPv4 representation address. Example: ::FFFF:192.168.1.120
  157. ///
  158. /// If an IPv6 address contains a non-zero scope identifier, it is added
  159. /// to the string, delimited by a percent character. On Windows platforms,
  160. /// the numeric value (which specifies an interface index) is directly
  161. /// appended. On Unix platforms, the name of the interface corresponding
  162. /// to the index (interpretation of the scope identifier) is added.
  163. bool isWildcard() const;
  164. /// Returns true iff the address is a wildcard (all zero)
  165. /// address.
  166. bool isBroadcast() const;
  167. /// Returns true iff the address is a broadcast address.
  168. ///
  169. /// Only IPv4 addresses can be broadcast addresses. In a broadcast
  170. /// address, all bits are one.
  171. ///
  172. /// For an IPv6 address, returns always false.
  173. bool isLoopback() const;
  174. /// Returns true iff the address is a loopback address.
  175. ///
  176. /// For IPv4, the loopback address is 127.0.0.1.
  177. ///
  178. /// For IPv6, the loopback address is ::1.
  179. bool isMulticast() const;
  180. /// Returns true iff the address is a multicast address.
  181. ///
  182. /// IPv4 multicast addresses are in the
  183. /// 224.0.0.0 to 239.255.255.255 range
  184. /// (the first four bits have the value 1110).
  185. ///
  186. /// IPv6 multicast addresses are in the
  187. /// FFxx:x:x:x:x:x:x:x range.
  188. bool isUnicast() const;
  189. /// Returns true iff the address is a unicast address.
  190. ///
  191. /// An address is unicast if it is neither a wildcard,
  192. /// broadcast or multicast address.
  193. bool isLinkLocal() const;
  194. /// Returns true iff the address is a link local unicast address.
  195. ///
  196. /// IPv4 link local addresses are in the 169.254.0.0/16 range,
  197. /// according to RFC 3927.
  198. ///
  199. /// IPv6 link local addresses have 1111 1110 10 as the first
  200. /// 10 bits, followed by 54 zeros.
  201. bool isSiteLocal() const;
  202. /// Returns true iff the address is a site local unicast address.
  203. ///
  204. /// IPv4 site local addresses are in on of the 10.0.0.0/24,
  205. /// 192.168.0.0/16 or 172.16.0.0 to 172.31.255.255 ranges.
  206. ///
  207. /// Originally, IPv6 site-local addresses had FEC0/10 (1111 1110 11)
  208. /// prefix (RFC 4291), followed by 38 zeros. Interfaces using
  209. /// this mask are supported, but obsolete; RFC 4193 prescribes
  210. /// fc00::/7 (1111 110) as local unicast prefix.
  211. bool isIPv4Compatible() const;
  212. /// Returns true iff the address is IPv4 compatible.
  213. ///
  214. /// For IPv4 addresses, this is always true.
  215. ///
  216. /// For IPv6, the address must be in the ::x:x range (the
  217. /// first 96 bits are zero).
  218. bool isIPv4Mapped() const;
  219. /// Returns true iff the address is an IPv4 mapped IPv6 address.
  220. ///
  221. /// For IPv4 addresses, this is always true.
  222. ///
  223. /// For IPv6, the address must be in the ::FFFF:x:x range.
  224. bool isWellKnownMC() const;
  225. /// Returns true iff the address is a well-known multicast address.
  226. ///
  227. /// For IPv4, well-known multicast addresses are in the
  228. /// 224.0.0.0/8 range.
  229. ///
  230. /// For IPv6, well-known multicast addresses are in the
  231. /// FF0x:x:x:x:x:x:x:x range.
  232. bool isNodeLocalMC() const;
  233. /// Returns true iff the address is a node-local multicast address.
  234. ///
  235. /// IPv4 does not support node-local addresses, thus the result is
  236. /// always false for an IPv4 address.
  237. ///
  238. /// For IPv6, node-local multicast addresses are in the
  239. /// FFx1:x:x:x:x:x:x:x range.
  240. bool isLinkLocalMC() const;
  241. /// Returns true iff the address is a link-local multicast address.
  242. ///
  243. /// For IPv4, link-local multicast addresses are in the
  244. /// 224.0.0.0/24 range. Note that this overlaps with the range for well-known
  245. /// multicast addresses.
  246. ///
  247. /// For IPv6, link-local multicast addresses are in the
  248. /// FFx2:x:x:x:x:x:x:x range.
  249. bool isSiteLocalMC() const;
  250. /// Returns true iff the address is a site-local multicast address.
  251. ///
  252. /// For IPv4, site local multicast addresses are in the
  253. /// 239.255.0.0/16 range.
  254. ///
  255. /// For IPv6, site-local multicast addresses are in the
  256. /// FFx5:x:x:x:x:x:x:x range.
  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. bool isGlobalMC() const;
  266. /// Returns true iff the address is a global multicast address.
  267. ///
  268. /// For IPv4, global multicast addresses are in the
  269. /// 224.0.1.0 to 238.255.255.255 range.
  270. ///
  271. /// For IPv6, global multicast addresses are in the
  272. /// FFxF:x:x:x:x:x:x:x range.
  273. bool operator == (const IPAddress& addr) const;
  274. bool operator != (const IPAddress& addr) const;
  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. IPAddress operator & (const IPAddress& addr) const;
  280. IPAddress operator | (const IPAddress& addr) const;
  281. IPAddress operator ^ (const IPAddress& addr) const;
  282. IPAddress operator ~ () const;
  283. poco_socklen_t length() const;
  284. /// Returns the length in bytes of the internal socket address structure.
  285. const void* addr() const;
  286. /// Returns the internal address structure.
  287. int af() const;
  288. /// Returns the address family (AF_INET or AF_INET6) of the address.
  289. unsigned prefixLength() const;
  290. /// Returns the prefix length.
  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. void mask(const IPAddress& mask, const IPAddress& set);
  297. /// Masks the IP address using the given netmask, which is usually
  298. /// a IPv4 subnet mask. Only supported for IPv4 addresses.
  299. ///
  300. /// The new address is (address & mask) | (set & ~mask).
  301. static IPAddress parse(const std::string& addr);
  302. /// Creates an IPAddress from the string containing
  303. /// an IP address in presentation format (dotted decimal
  304. /// for IPv4, hex string for IPv6).
  305. ///
  306. /// Depending on the format of addr, either an IPv4 or
  307. /// an IPv6 address is created.
  308. ///
  309. /// See toString() for details on the supported formats.
  310. ///
  311. /// Throws an InvalidAddressException if the address cannot be parsed.
  312. static bool tryParse(const std::string& addr, IPAddress& result);
  313. /// Tries to interpret the given address string as an
  314. /// IP address in presentation format (dotted decimal
  315. /// for IPv4, hex string for IPv6).
  316. ///
  317. /// Returns true and stores the IPAddress in result if the
  318. /// string contains a valid address.
  319. ///
  320. /// Returns false and leaves result unchanged otherwise.
  321. static IPAddress wildcard(Family family = IPv4);
  322. /// Returns a wildcard IPv4 or IPv6 address (0.0.0.0).
  323. static IPAddress broadcast();
  324. /// Returns a broadcast IPv4 address (255.255.255.255).
  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. private:
  336. typedef Poco::Net::Impl::IPAddressImpl Impl;
  337. typedef Impl* Ptr;
  338. Ptr pImpl() const;
  339. void destruct();
  340. char* storage();
  341. #ifdef POCO_ENABLE_CPP11
  342. static const unsigned sz = sizeof(Poco::Net::Impl::IPv6AddressImpl);
  343. union
  344. {
  345. std::aligned_storage<sz> a;
  346. char buffer[sz];
  347. }
  348. #else // !POCO_ENABLE_CPP11
  349. AlignedCharArrayUnion <Poco::Net::Impl::IPv6AddressImpl,
  350. Poco::Net::Impl::IPv4AddressImpl>
  351. #endif // POCO_ENABLE_CPP11
  352. _memory;
  353. };
  354. inline void IPAddress::destruct()
  355. {
  356. pImpl()->~IPAddressImpl();
  357. }
  358. inline IPAddress::Ptr IPAddress::pImpl() const
  359. {
  360. return reinterpret_cast<Ptr>(const_cast<char *>(_memory.buffer));
  361. }
  362. inline char* IPAddress::storage()
  363. {
  364. return _memory.buffer;
  365. }
  366. BinaryWriter& operator << (BinaryWriter& writer, const IPAddress& value);
  367. BinaryReader& operator >> (BinaryReader& reader, IPAddress& value);
  368. } } // namespace Poco::Net
  369. #endif // Net_IPAddress_INCLUDED