network_v4.ipp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // ip/impl/network_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 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 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_SNPRINTF)
  122. snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  123. #elif defined(BOOST_ASIO_HAS_SECURE_RTL)
  124. sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  125. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  126. sprintf(prefix_len, "/%u", prefix_length_);
  127. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  128. return address_.to_string() + prefix_len;
  129. }
  130. network_v4 make_network_v4(const char* str)
  131. {
  132. return make_network_v4(std::string(str));
  133. }
  134. network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
  135. {
  136. return make_network_v4(std::string(str), ec);
  137. }
  138. network_v4 make_network_v4(const std::string& str)
  139. {
  140. boost::system::error_code ec;
  141. network_v4 net = make_network_v4(str, ec);
  142. boost::asio::detail::throw_error(ec);
  143. return net;
  144. }
  145. network_v4 make_network_v4(const std::string& str,
  146. boost::system::error_code& ec)
  147. {
  148. std::string::size_type pos = str.find_first_of("/");
  149. if (pos == std::string::npos)
  150. {
  151. ec = boost::asio::error::invalid_argument;
  152. return network_v4();
  153. }
  154. if (pos == str.size() - 1)
  155. {
  156. ec = boost::asio::error::invalid_argument;
  157. return network_v4();
  158. }
  159. std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
  160. if (end != std::string::npos)
  161. {
  162. ec = boost::asio::error::invalid_argument;
  163. return network_v4();
  164. }
  165. const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
  166. if (ec)
  167. return network_v4();
  168. const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
  169. if (prefix_len < 0 || prefix_len > 32)
  170. {
  171. ec = boost::asio::error::invalid_argument;
  172. return network_v4();
  173. }
  174. return network_v4(addr, static_cast<unsigned short>(prefix_len));
  175. }
  176. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  177. network_v4 make_network_v4(string_view str)
  178. {
  179. return make_network_v4(static_cast<std::string>(str));
  180. }
  181. network_v4 make_network_v4(string_view str,
  182. boost::system::error_code& ec)
  183. {
  184. return make_network_v4(static_cast<std::string>(str), ec);
  185. }
  186. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  187. } // namespace ip
  188. } // namespace asio
  189. } // namespace boost
  190. #include <boost/asio/detail/pop_options.hpp>
  191. #endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP