address_v4.hpp 8.0 KB

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