network_v6.ipp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // ip/impl/network_v6.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 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 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_SNPRINTF)
  91. snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  92. #elif defined(ASIO_HAS_SECURE_RTL)
  93. sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  94. #else // defined(ASIO_HAS_SECURE_RTL)
  95. sprintf(prefix_len, "/%u", prefix_length_);
  96. #endif // defined(ASIO_HAS_SECURE_RTL)
  97. return address_.to_string() + prefix_len;
  98. }
  99. network_v6 make_network_v6(const char* str)
  100. {
  101. return make_network_v6(std::string(str));
  102. }
  103. network_v6 make_network_v6(const char* str, asio::error_code& ec)
  104. {
  105. return make_network_v6(std::string(str), ec);
  106. }
  107. network_v6 make_network_v6(const std::string& str)
  108. {
  109. asio::error_code ec;
  110. network_v6 net = make_network_v6(str, ec);
  111. asio::detail::throw_error(ec);
  112. return net;
  113. }
  114. network_v6 make_network_v6(const std::string& str,
  115. asio::error_code& ec)
  116. {
  117. std::string::size_type pos = str.find_first_of("/");
  118. if (pos == std::string::npos)
  119. {
  120. ec = asio::error::invalid_argument;
  121. return network_v6();
  122. }
  123. if (pos == str.size() - 1)
  124. {
  125. ec = asio::error::invalid_argument;
  126. return network_v6();
  127. }
  128. std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
  129. if (end != std::string::npos)
  130. {
  131. ec = asio::error::invalid_argument;
  132. return network_v6();
  133. }
  134. const address_v6 addr = make_address_v6(str.substr(0, pos), ec);
  135. if (ec)
  136. return network_v6();
  137. const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
  138. if (prefix_len < 0 || prefix_len > 128)
  139. {
  140. ec = asio::error::invalid_argument;
  141. return network_v6();
  142. }
  143. return network_v6(addr, static_cast<unsigned short>(prefix_len));
  144. }
  145. #if defined(ASIO_HAS_STRING_VIEW)
  146. network_v6 make_network_v6(string_view str)
  147. {
  148. return make_network_v6(static_cast<std::string>(str));
  149. }
  150. network_v6 make_network_v6(string_view str,
  151. asio::error_code& ec)
  152. {
  153. return make_network_v6(static_cast<std::string>(str), ec);
  154. }
  155. #endif // defined(ASIO_HAS_STRING_VIEW)
  156. } // namespace ip
  157. } // namespace asio
  158. #include "asio/detail/pop_options.hpp"
  159. #endif // ASIO_IP_IMPL_NETWORK_V6_IPP