network_v6.ipp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // ip/impl/network_v6.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_V6_IPP
  12. #define BOOST_ASIO_IP_IMPL_NETWORK_V6_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_v6.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace ip {
  29. network_v6::network_v6(const address_v6& addr, unsigned short prefix_len)
  30. : address_(addr),
  31. prefix_length_(prefix_len)
  32. {
  33. if (prefix_len > 128)
  34. {
  35. std::out_of_range ex("prefix length too large");
  36. boost::asio::detail::throw_exception(ex);
  37. }
  38. }
  39. BOOST_ASIO_DECL address_v6 network_v6::network() const BOOST_ASIO_NOEXCEPT
  40. {
  41. address_v6::bytes_type bytes(address_.to_bytes());
  42. for (std::size_t i = 0; i < 16; ++i)
  43. {
  44. if (prefix_length_ <= i * 8)
  45. bytes[i] = 0;
  46. else if (prefix_length_ < (i + 1) * 8)
  47. bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
  48. }
  49. return address_v6(bytes, address_.scope_id());
  50. }
  51. address_v6_range network_v6::hosts() const BOOST_ASIO_NOEXCEPT
  52. {
  53. address_v6::bytes_type begin_bytes(address_.to_bytes());
  54. address_v6::bytes_type end_bytes(address_.to_bytes());
  55. for (std::size_t i = 0; i < 16; ++i)
  56. {
  57. if (prefix_length_ <= i * 8)
  58. {
  59. begin_bytes[i] = 0;
  60. end_bytes[i] = 0xFF;
  61. }
  62. else if (prefix_length_ < (i + 1) * 8)
  63. {
  64. begin_bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
  65. end_bytes[i] |= 0xFF >> (prefix_length_ % 8);
  66. }
  67. }
  68. return address_v6_range(
  69. address_v6_iterator(address_v6(begin_bytes, address_.scope_id())),
  70. ++address_v6_iterator(address_v6(end_bytes, address_.scope_id())));
  71. }
  72. bool network_v6::is_subnet_of(const network_v6& other) const
  73. {
  74. if (other.prefix_length_ >= prefix_length_)
  75. return false; // Only real subsets are allowed.
  76. const network_v6 me(address_, other.prefix_length_);
  77. return other.canonical() == me.canonical();
  78. }
  79. std::string network_v6::to_string() const
  80. {
  81. boost::system::error_code ec;
  82. std::string addr = to_string(ec);
  83. boost::asio::detail::throw_error(ec);
  84. return addr;
  85. }
  86. std::string network_v6::to_string(boost::system::error_code& ec) const
  87. {
  88. using namespace std; // For sprintf.
  89. ec = boost::system::error_code();
  90. char prefix_len[16];
  91. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  92. sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  93. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  94. sprintf(prefix_len, "/%u", prefix_length_);
  95. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  96. return address_.to_string() + prefix_len;
  97. }
  98. network_v6 make_network_v6(const char* str)
  99. {
  100. return make_network_v6(std::string(str));
  101. }
  102. network_v6 make_network_v6(const char* str, boost::system::error_code& ec)
  103. {
  104. return make_network_v6(std::string(str), ec);
  105. }
  106. network_v6 make_network_v6(const std::string& str)
  107. {
  108. boost::system::error_code ec;
  109. network_v6 net = make_network_v6(str, ec);
  110. boost::asio::detail::throw_error(ec);
  111. return net;
  112. }
  113. network_v6 make_network_v6(const std::string& str,
  114. boost::system::error_code& ec)
  115. {
  116. std::string::size_type pos = str.find_first_of("/");
  117. if (pos == std::string::npos)
  118. {
  119. ec = boost::asio::error::invalid_argument;
  120. return network_v6();
  121. }
  122. if (pos == str.size() - 1)
  123. {
  124. ec = boost::asio::error::invalid_argument;
  125. return network_v6();
  126. }
  127. std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
  128. if (end != std::string::npos)
  129. {
  130. ec = boost::asio::error::invalid_argument;
  131. return network_v6();
  132. }
  133. const address_v6 addr = make_address_v6(str.substr(0, pos), ec);
  134. if (ec)
  135. return network_v6();
  136. const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
  137. if (prefix_len < 0 || prefix_len > 128)
  138. {
  139. ec = boost::asio::error::invalid_argument;
  140. return network_v6();
  141. }
  142. return network_v6(addr, static_cast<unsigned short>(prefix_len));
  143. }
  144. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  145. network_v6 make_network_v6(string_view str)
  146. {
  147. return make_network_v6(static_cast<std::string>(str));
  148. }
  149. network_v6 make_network_v6(string_view str,
  150. boost::system::error_code& ec)
  151. {
  152. return make_network_v6(static_cast<std::string>(str), ec);
  153. }
  154. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  155. } // namespace ip
  156. } // namespace asio
  157. } // namespace boost
  158. #include <boost/asio/detail/pop_options.hpp>
  159. #endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP