network_v6.ipp 4.6 KB

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