network_v4.ipp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // ip/impl/network_v4.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_V4_IPP
  12. #define 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 "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_v4.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace ip {
  28. network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
  29. : address_(addr),
  30. prefix_length_(prefix_len)
  31. {
  32. if (prefix_len > 32)
  33. {
  34. std::out_of_range ex("prefix length too large");
  35. asio::detail::throw_exception(ex);
  36. }
  37. }
  38. network_v4::network_v4(const address_v4& addr, const address_v4& mask)
  39. : address_(addr),
  40. prefix_length_(0)
  41. {
  42. address_v4::bytes_type mask_bytes = mask.to_bytes();
  43. bool finished = false;
  44. for (std::size_t i = 0; i < mask_bytes.size(); ++i)
  45. {
  46. if (finished)
  47. {
  48. if (mask_bytes[i])
  49. {
  50. std::invalid_argument ex("non-contiguous netmask");
  51. asio::detail::throw_exception(ex);
  52. }
  53. continue;
  54. }
  55. else
  56. {
  57. switch (mask_bytes[i])
  58. {
  59. case 255:
  60. prefix_length_ += 8;
  61. break;
  62. case 254: // prefix_length_ += 7
  63. prefix_length_ += 1;
  64. case 252: // prefix_length_ += 6
  65. prefix_length_ += 1;
  66. case 248: // prefix_length_ += 5
  67. prefix_length_ += 1;
  68. case 240: // prefix_length_ += 4
  69. prefix_length_ += 1;
  70. case 224: // prefix_length_ += 3
  71. prefix_length_ += 1;
  72. case 192: // prefix_length_ += 2
  73. prefix_length_ += 1;
  74. case 128: // prefix_length_ += 1
  75. prefix_length_ += 1;
  76. case 0: // nbits += 0
  77. finished = true;
  78. break;
  79. default:
  80. std::out_of_range ex("non-contiguous netmask");
  81. asio::detail::throw_exception(ex);
  82. }
  83. }
  84. }
  85. }
  86. address_v4 network_v4::netmask() const noexcept
  87. {
  88. uint32_t nmbits = 0xffffffff;
  89. if (prefix_length_ == 0)
  90. nmbits = 0;
  91. else
  92. nmbits = nmbits << (32 - prefix_length_);
  93. return address_v4(nmbits);
  94. }
  95. address_v4_range network_v4::hosts() const noexcept
  96. {
  97. return is_host()
  98. ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
  99. : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
  100. }
  101. bool network_v4::is_subnet_of(const network_v4& other) const
  102. {
  103. if (other.prefix_length_ >= prefix_length_)
  104. return false; // Only real subsets are allowed.
  105. const network_v4 me(address_, other.prefix_length_);
  106. return other.canonical() == me.canonical();
  107. }
  108. std::string network_v4::to_string() const
  109. {
  110. asio::error_code ec;
  111. std::string addr = to_string(ec);
  112. asio::detail::throw_error(ec);
  113. return addr;
  114. }
  115. std::string network_v4::to_string(asio::error_code& ec) const
  116. {
  117. using namespace std; // For sprintf.
  118. ec = asio::error_code();
  119. char prefix_len[16];
  120. #if defined(ASIO_HAS_SNPRINTF)
  121. snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  122. #elif defined(ASIO_HAS_SECURE_RTL)
  123. sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
  124. #else // defined(ASIO_HAS_SECURE_RTL)
  125. sprintf(prefix_len, "/%u", prefix_length_);
  126. #endif // defined(ASIO_HAS_SECURE_RTL)
  127. return address_.to_string() + prefix_len;
  128. }
  129. network_v4 make_network_v4(const char* str)
  130. {
  131. return make_network_v4(std::string(str));
  132. }
  133. network_v4 make_network_v4(const char* str, asio::error_code& ec)
  134. {
  135. return make_network_v4(std::string(str), ec);
  136. }
  137. network_v4 make_network_v4(const std::string& str)
  138. {
  139. asio::error_code ec;
  140. network_v4 net = make_network_v4(str, ec);
  141. asio::detail::throw_error(ec);
  142. return net;
  143. }
  144. network_v4 make_network_v4(const std::string& str,
  145. asio::error_code& ec)
  146. {
  147. std::string::size_type pos = str.find_first_of("/");
  148. if (pos == std::string::npos)
  149. {
  150. ec = asio::error::invalid_argument;
  151. return network_v4();
  152. }
  153. if (pos == str.size() - 1)
  154. {
  155. ec = asio::error::invalid_argument;
  156. return network_v4();
  157. }
  158. std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
  159. if (end != std::string::npos)
  160. {
  161. ec = asio::error::invalid_argument;
  162. return network_v4();
  163. }
  164. const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
  165. if (ec)
  166. return network_v4();
  167. const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
  168. if (prefix_len < 0 || prefix_len > 32)
  169. {
  170. ec = asio::error::invalid_argument;
  171. return network_v4();
  172. }
  173. return network_v4(addr, static_cast<unsigned short>(prefix_len));
  174. }
  175. #if defined(ASIO_HAS_STRING_VIEW)
  176. network_v4 make_network_v4(string_view str)
  177. {
  178. return make_network_v4(static_cast<std::string>(str));
  179. }
  180. network_v4 make_network_v4(string_view str,
  181. asio::error_code& ec)
  182. {
  183. return make_network_v4(static_cast<std::string>(str), ec);
  184. }
  185. #endif // defined(ASIO_HAS_STRING_VIEW)
  186. } // namespace ip
  187. } // namespace asio
  188. #include "asio/detail/pop_options.hpp"
  189. #endif // ASIO_IP_IMPL_NETWORK_V4_IPP