address_v4.ipp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // ip/impl/address_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_IMPL_ADDRESS_V4_IPP
  11. #define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <climits>
  17. #include <stdexcept>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/detail/throw_exception.hpp>
  22. #include <boost/asio/ip/address_v4.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ip {
  27. address_v4::address_v4(const address_v4::bytes_type& bytes)
  28. {
  29. #if UCHAR_MAX > 0xFF
  30. if (bytes[0] > 0xFF || bytes[1] > 0xFF
  31. || bytes[2] > 0xFF || bytes[3] > 0xFF)
  32. {
  33. std::out_of_range ex("address_v4 from bytes_type");
  34. boost::asio::detail::throw_exception(ex);
  35. }
  36. #endif // UCHAR_MAX > 0xFF
  37. using namespace std; // For memcpy.
  38. memcpy(&addr_.s_addr, bytes.data(), 4);
  39. }
  40. address_v4::address_v4(unsigned long addr)
  41. {
  42. #if ULONG_MAX > 0xFFFFFFFF
  43. if (addr > 0xFFFFFFFF)
  44. {
  45. std::out_of_range ex("address_v4 from unsigned long");
  46. boost::asio::detail::throw_exception(ex);
  47. }
  48. #endif // ULONG_MAX > 0xFFFFFFFF
  49. addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
  50. static_cast<boost::asio::detail::u_long_type>(addr));
  51. }
  52. address_v4::bytes_type address_v4::to_bytes() const
  53. {
  54. using namespace std; // For memcpy.
  55. bytes_type bytes;
  56. #if defined(BOOST_ASIO_HAS_STD_ARRAY)
  57. memcpy(bytes.data(), &addr_.s_addr, 4);
  58. #else // defined(BOOST_ASIO_HAS_STD_ARRAY)
  59. memcpy(bytes.elems, &addr_.s_addr, 4);
  60. #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
  61. return bytes;
  62. }
  63. unsigned long address_v4::to_ulong() const
  64. {
  65. return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  66. }
  67. std::string address_v4::to_string() const
  68. {
  69. boost::system::error_code ec;
  70. std::string addr = to_string(ec);
  71. boost::asio::detail::throw_error(ec);
  72. return addr;
  73. }
  74. std::string address_v4::to_string(boost::system::error_code& ec) const
  75. {
  76. char addr_str[boost::asio::detail::max_addr_v4_str_len];
  77. const char* addr =
  78. boost::asio::detail::socket_ops::inet_ntop(
  79. BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  80. boost::asio::detail::max_addr_v4_str_len, 0, ec);
  81. if (addr == 0)
  82. return std::string();
  83. return addr;
  84. }
  85. address_v4 address_v4::from_string(const char* str)
  86. {
  87. boost::system::error_code ec;
  88. address_v4 addr = from_string(str, ec);
  89. boost::asio::detail::throw_error(ec);
  90. return addr;
  91. }
  92. address_v4 address_v4::from_string(
  93. const char* str, boost::system::error_code& ec)
  94. {
  95. address_v4 tmp;
  96. if (boost::asio::detail::socket_ops::inet_pton(
  97. BOOST_ASIO_OS_DEF(AF_INET), str, &tmp.addr_, 0, ec) <= 0)
  98. return address_v4();
  99. return tmp;
  100. }
  101. address_v4 address_v4::from_string(const std::string& str)
  102. {
  103. return from_string(str.c_str());
  104. }
  105. address_v4 address_v4::from_string(
  106. const std::string& str, boost::system::error_code& ec)
  107. {
  108. return from_string(str.c_str(), ec);
  109. }
  110. bool address_v4::is_loopback() const
  111. {
  112. return (to_ulong() & 0xFF000000) == 0x7F000000;
  113. }
  114. bool address_v4::is_unspecified() const
  115. {
  116. return to_ulong() == 0;
  117. }
  118. bool address_v4::is_class_a() const
  119. {
  120. return (to_ulong() & 0x80000000) == 0;
  121. }
  122. bool address_v4::is_class_b() const
  123. {
  124. return (to_ulong() & 0xC0000000) == 0x80000000;
  125. }
  126. bool address_v4::is_class_c() const
  127. {
  128. return (to_ulong() & 0xE0000000) == 0xC0000000;
  129. }
  130. bool address_v4::is_multicast() const
  131. {
  132. return (to_ulong() & 0xF0000000) == 0xE0000000;
  133. }
  134. address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
  135. {
  136. return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
  137. }
  138. address_v4 address_v4::netmask(const address_v4& addr)
  139. {
  140. if (addr.is_class_a())
  141. return address_v4(0xFF000000);
  142. if (addr.is_class_b())
  143. return address_v4(0xFFFF0000);
  144. if (addr.is_class_c())
  145. return address_v4(0xFFFFFF00);
  146. return address_v4(0xFFFFFFFF);
  147. }
  148. } // namespace ip
  149. } // namespace asio
  150. } // namespace boost
  151. #include <boost/asio/detail/pop_options.hpp>
  152. #endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP