network_v4.ipp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // ip/impl/network_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
  12. #define BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <climits>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <stdexcept>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/detail/throw_exception.hpp>
  24. #include <boost/asio/ip/network_v4.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace ip {
  29. network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
  30. : address_(addr),
  31. prefix_length_(prefix_len)
  32. {
  33. if (prefix_len > 32)
  34. {
  35. std::out_of_range ex("prefix length too large");
  36. boost::asio::detail::throw_exception(ex);
  37. }
  38. }
  39. network_v4::network_v4(const address_v4& addr, const address_v4& mask)
  40. : address_(addr),
  41. prefix_length_(0)
  42. {
  43. address_v4::bytes_type mask_bytes = mask.to_bytes();
  44. bool finished = false;
  45. for (std::size_t i = 0; i < mask_bytes.size(); ++i)
  46. {
  47. if (finished)
  48. {
  49. if (mask_bytes[i])
  50. {
  51. std::invalid_argument ex("non-contiguous netmask");
  52. boost::asio::detail::throw_exception(ex);
  53. }
  54. continue;
  55. }
  56. else
  57. {
  58. switch (mask_bytes[i])
  59. {
  60. case 255:
  61. prefix_length_ += 8;
  62. break;
  63. case 254: // prefix_length_ += 7
  64. prefix_length_ += 1;
  65. case 252: // prefix_length_ += 6
  66. prefix_length_ += 1;
  67. case 248: // prefix_length_ += 5
  68. prefix_length_ += 1;
  69. case 240: // prefix_length_ += 4
  70. prefix_length_ += 1;
  71. case 224: // prefix_length_ += 3
  72. prefix_length_ += 1;
  73. case 192: // prefix_length_ += 2
  74. prefix_length_ += 1;
  75. case 128: // prefix_length_ += 1
  76. prefix_length_ += 1;
  77. case 0: // nbits += 0
  78. finished = true;
  79. break;
  80. default:
  81. std::out_of_range ex("non-contiguous netmask");
  82. boost::asio::detail::throw_exception(ex);
  83. }
  84. }
  85. }
  86. }
  87. address_v4 network_v4::netmask() const BOOST_ASIO_NOEXCEPT
  88. {
  89. uint32_t nmbits = 0xffffffff;
  90. if (prefix_length_ == 0)
  91. nmbits = 0;
  92. else
  93. nmbits = nmbits << (32 - prefix_length_);
  94. return address_v4(nmbits);
  95. }
  96. address_v4_range network_v4::hosts() const BOOST_ASIO_NOEXCEPT
  97. {
  98. return is_host()
  99. ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
  100. : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
  101. }
  102. bool network_v4::is_subnet_of(const network_v4& other) const
  103. {
  104. if (other.prefix_length_ >= prefix_length_)
  105. return false; // Only real subsets are allowed.
  106. const network_v4 me(address_, other.prefix_length_);
  107. return other.canonical() == me.canonical();
  108. }
  109. std::string network_v4::to_string() const
  110. {
  111. boost::system::error_code ec;
  112. std::string addr = to_string(ec);
  113. boost::asio::detail::throw_error(ec);
  114. return addr;
  115. }
  116. std::string network_v4::to_string(boost::system::error_code& ec) const
  117. {
  118. using namespace std; // For sprintf.
  119. ec = boost::system::error_code();
  120. char prefix_len[16];
  121. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  122. sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  123. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  124. sprintf(prefix_len, "/%u", prefix_length_);
  125. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  126. return address_.to_string() + prefix_len;
  127. }
  128. network_v4 make_network_v4(const char* str)
  129. {
  130. return make_network_v4(std::string(str));
  131. }
  132. network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
  133. {
  134. return make_network_v4(std::string(str), ec);
  135. }
  136. network_v4 make_network_v4(const std::string& str)
  137. {
  138. boost::system::error_code ec;
  139. network_v4 net = make_network_v4(str, ec);
  140. boost::asio::detail::throw_error(ec);
  141. return net;
  142. }
  143. network_v4 make_network_v4(const std::string& str,
  144. boost::system::error_code& ec)
  145. {
  146. std::string::size_type pos = str.find_first_of("/");
  147. if (pos == std::string::npos)
  148. {
  149. ec = boost::asio::error::invalid_argument;
  150. return network_v4();
  151. }
  152. if (pos == str.size() - 1)
  153. {
  154. ec = boost::asio::error::invalid_argument;
  155. return network_v4();
  156. }
  157. std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
  158. if (end != std::string::npos)
  159. {
  160. ec = boost::asio::error::invalid_argument;
  161. return network_v4();
  162. }
  163. const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
  164. if (ec)
  165. return network_v4();
  166. const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
  167. if (prefix_len < 0 || prefix_len > 32)
  168. {
  169. ec = boost::asio::error::invalid_argument;
  170. return network_v4();
  171. }
  172. return network_v4(addr, static_cast<unsigned short>(prefix_len));
  173. }
  174. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  175. network_v4 make_network_v4(string_view str)
  176. {
  177. return make_network_v4(static_cast<std::string>(str));
  178. }
  179. network_v4 make_network_v4(string_view str,
  180. boost::system::error_code& ec)
  181. {
  182. return make_network_v4(static_cast<std::string>(str), ec);
  183. }
  184. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  185. } // namespace ip
  186. } // namespace asio
  187. } // namespace boost
  188. #include <boost/asio/detail/pop_options.hpp>
  189. #endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP