address_v4.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // address_v4.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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. #ifndef ASIO_IP_ADDRESS_V4_HPP
  11. #define ASIO_IP_ADDRESS_V4_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #if !defined(BOOST_NO_IOSTREAM)
  19. # include <iosfwd>
  20. #endif // !defined(BOOST_NO_IOSTREAM)
  21. #include <climits>
  22. #include <string>
  23. #include <stdexcept>
  24. #include <boost/array.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #include "asio/detail/pop_options.hpp"
  27. #include "asio/error.hpp"
  28. #include "asio/detail/socket_ops.hpp"
  29. #include "asio/detail/socket_types.hpp"
  30. #include "asio/detail/throw_error.hpp"
  31. namespace asio {
  32. namespace ip {
  33. /// Implements IP version 4 style addresses.
  34. /**
  35. * The asio::ip::address_v4 class provides the ability to use and
  36. * manipulate IP version 4 addresses.
  37. *
  38. * @par Thread Safety
  39. * @e Distinct @e objects: Safe.@n
  40. * @e Shared @e objects: Unsafe.
  41. */
  42. class address_v4
  43. {
  44. public:
  45. /// The type used to represent an address as an array of bytes.
  46. typedef boost::array<unsigned char, 4> bytes_type;
  47. /// Default constructor.
  48. address_v4()
  49. {
  50. addr_.s_addr = 0;
  51. }
  52. /// Construct an address from raw bytes.
  53. explicit address_v4(const bytes_type& bytes)
  54. {
  55. #if UCHAR_MAX > 0xFF
  56. if (bytes[0] > 0xFF || bytes[1] > 0xFF
  57. || bytes[2] > 0xFF || bytes[3] > 0xFF)
  58. {
  59. std::out_of_range ex("address_v4 from bytes_type");
  60. boost::throw_exception(ex);
  61. }
  62. #endif // UCHAR_MAX > 0xFF
  63. using namespace std; // For memcpy.
  64. memcpy(&addr_.s_addr, bytes.elems, 4);
  65. }
  66. /// Construct an address from a unsigned long in host byte order.
  67. explicit address_v4(unsigned long addr)
  68. {
  69. #if ULONG_MAX > 0xFFFFFFFF
  70. if (addr > 0xFFFFFFFF)
  71. {
  72. std::out_of_range ex("address_v4 from unsigned long");
  73. boost::throw_exception(ex);
  74. }
  75. #endif // ULONG_MAX > 0xFFFFFFFF
  76. addr_.s_addr = asio::detail::socket_ops::host_to_network_long(addr);
  77. }
  78. /// Copy constructor.
  79. address_v4(const address_v4& other)
  80. : addr_(other.addr_)
  81. {
  82. }
  83. /// Assign from another address.
  84. address_v4& operator=(const address_v4& other)
  85. {
  86. addr_ = other.addr_;
  87. return *this;
  88. }
  89. /// Get the address in bytes, in network byte order.
  90. bytes_type to_bytes() const
  91. {
  92. using namespace std; // For memcpy.
  93. bytes_type bytes;
  94. memcpy(bytes.elems, &addr_.s_addr, 4);
  95. return bytes;
  96. }
  97. /// Get the address as an unsigned long in host byte order
  98. unsigned long to_ulong() const
  99. {
  100. return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  101. }
  102. /// Get the address as a string in dotted decimal format.
  103. std::string to_string() const
  104. {
  105. asio::error_code ec;
  106. std::string addr = to_string(ec);
  107. asio::detail::throw_error(ec);
  108. return addr;
  109. }
  110. /// Get the address as a string in dotted decimal format.
  111. std::string to_string(asio::error_code& ec) const
  112. {
  113. char addr_str[asio::detail::max_addr_v4_str_len];
  114. const char* addr =
  115. asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str,
  116. asio::detail::max_addr_v4_str_len, 0, ec);
  117. if (addr == 0)
  118. return std::string();
  119. return addr;
  120. }
  121. /// Create an address from an IP address string in dotted decimal form.
  122. static address_v4 from_string(const char* str)
  123. {
  124. asio::error_code ec;
  125. address_v4 addr = from_string(str, ec);
  126. asio::detail::throw_error(ec);
  127. return addr;
  128. }
  129. /// Create an address from an IP address string in dotted decimal form.
  130. static address_v4 from_string(const char* str, asio::error_code& ec)
  131. {
  132. address_v4 tmp;
  133. if (asio::detail::socket_ops::inet_pton(
  134. AF_INET, str, &tmp.addr_, 0, ec) <= 0)
  135. return address_v4();
  136. return tmp;
  137. }
  138. /// Create an address from an IP address string in dotted decimal form.
  139. static address_v4 from_string(const std::string& str)
  140. {
  141. return from_string(str.c_str());
  142. }
  143. /// Create an address from an IP address string in dotted decimal form.
  144. static address_v4 from_string(const std::string& str,
  145. asio::error_code& ec)
  146. {
  147. return from_string(str.c_str(), ec);
  148. }
  149. /// Determine whether the address is a class A address.
  150. bool is_class_a() const
  151. {
  152. return IN_CLASSA(to_ulong());
  153. }
  154. /// Determine whether the address is a class B address.
  155. bool is_class_b() const
  156. {
  157. return IN_CLASSB(to_ulong());
  158. }
  159. /// Determine whether the address is a class C address.
  160. bool is_class_c() const
  161. {
  162. return IN_CLASSC(to_ulong());
  163. }
  164. /// Determine whether the address is a multicast address.
  165. bool is_multicast() const
  166. {
  167. return IN_MULTICAST(to_ulong());
  168. }
  169. /// Compare two addresses for equality.
  170. friend bool operator==(const address_v4& a1, const address_v4& a2)
  171. {
  172. return a1.addr_.s_addr == a2.addr_.s_addr;
  173. }
  174. /// Compare two addresses for inequality.
  175. friend bool operator!=(const address_v4& a1, const address_v4& a2)
  176. {
  177. return a1.addr_.s_addr != a2.addr_.s_addr;
  178. }
  179. /// Compare addresses for ordering.
  180. friend bool operator<(const address_v4& a1, const address_v4& a2)
  181. {
  182. return a1.to_ulong() < a2.to_ulong();
  183. }
  184. /// Compare addresses for ordering.
  185. friend bool operator>(const address_v4& a1, const address_v4& a2)
  186. {
  187. return a1.to_ulong() > a2.to_ulong();
  188. }
  189. /// Compare addresses for ordering.
  190. friend bool operator<=(const address_v4& a1, const address_v4& a2)
  191. {
  192. return a1.to_ulong() <= a2.to_ulong();
  193. }
  194. /// Compare addresses for ordering.
  195. friend bool operator>=(const address_v4& a1, const address_v4& a2)
  196. {
  197. return a1.to_ulong() >= a2.to_ulong();
  198. }
  199. /// Obtain an address object that represents any address.
  200. static address_v4 any()
  201. {
  202. return address_v4(static_cast<unsigned long>(INADDR_ANY));
  203. }
  204. /// Obtain an address object that represents the loopback address.
  205. static address_v4 loopback()
  206. {
  207. return address_v4(static_cast<unsigned long>(INADDR_LOOPBACK));
  208. }
  209. /// Obtain an address object that represents the broadcast address.
  210. static address_v4 broadcast()
  211. {
  212. return address_v4(static_cast<unsigned long>(INADDR_BROADCAST));
  213. }
  214. /// Obtain an address object that represents the broadcast address that
  215. /// corresponds to the specified address and netmask.
  216. static address_v4 broadcast(const address_v4& addr, const address_v4& mask)
  217. {
  218. return address_v4(addr.to_ulong() | ~mask.to_ulong());
  219. }
  220. /// Obtain the netmask that corresponds to the address, based on its address
  221. /// class.
  222. static address_v4 netmask(const address_v4& addr)
  223. {
  224. if (addr.is_class_a())
  225. return address_v4(0xFF000000);
  226. if (addr.is_class_b())
  227. return address_v4(0xFFFF0000);
  228. if (addr.is_class_c())
  229. return address_v4(0xFFFFFF00);
  230. return address_v4(0xFFFFFFFF);
  231. }
  232. private:
  233. // The underlying IPv4 address.
  234. asio::detail::in4_addr_type addr_;
  235. };
  236. #if !defined(BOOST_NO_IOSTREAM)
  237. /// Output an address as a string.
  238. /**
  239. * Used to output a human-readable string for a specified address.
  240. *
  241. * @param os The output stream to which the string will be written.
  242. *
  243. * @param addr The address to be written.
  244. *
  245. * @return The output stream.
  246. *
  247. * @relates asio::ip::address_v4
  248. */
  249. template <typename Elem, typename Traits>
  250. std::basic_ostream<Elem, Traits>& operator<<(
  251. std::basic_ostream<Elem, Traits>& os, const address_v4& addr)
  252. {
  253. asio::error_code ec;
  254. std::string s = addr.to_string(ec);
  255. if (ec)
  256. {
  257. if (os.exceptions() & std::ios::failbit)
  258. asio::detail::throw_error(ec);
  259. else
  260. os.setstate(std::ios_base::failbit);
  261. }
  262. else
  263. for (std::string::iterator i = s.begin(); i != s.end(); ++i)
  264. os << os.widen(*i);
  265. return os;
  266. }
  267. #endif // !defined(BOOST_NO_IOSTREAM)
  268. } // namespace ip
  269. } // namespace asio
  270. #include "asio/detail/pop_options.hpp"
  271. #endif // ASIO_IP_ADDRESS_V4_HPP