address_v4.ipp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // ip/impl/address_v4.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_IP_IMPL_ADDRESS_V4_IPP
  11. #define ASIO_IP_IMPL_ADDRESS_V4_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <climits>
  17. #include <stdexcept>
  18. #include "asio/error.hpp"
  19. #include "asio/detail/socket_ops.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/detail/throw_exception.hpp"
  22. #include "asio/ip/address_v4.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace ip {
  26. address_v4::address_v4(const address_v4::bytes_type& bytes)
  27. {
  28. #if UCHAR_MAX > 0xFF
  29. if (bytes[0] > 0xFF || bytes[1] > 0xFF
  30. || bytes[2] > 0xFF || bytes[3] > 0xFF)
  31. {
  32. std::out_of_range ex("address_v4 from bytes_type");
  33. asio::detail::throw_exception(ex);
  34. }
  35. #endif // UCHAR_MAX > 0xFF
  36. using namespace std; // For memcpy.
  37. memcpy(&addr_.s_addr, bytes.data(), 4);
  38. }
  39. address_v4::address_v4(unsigned long addr)
  40. {
  41. #if ULONG_MAX > 0xFFFFFFFF
  42. if (addr > 0xFFFFFFFF)
  43. {
  44. std::out_of_range ex("address_v4 from unsigned long");
  45. asio::detail::throw_exception(ex);
  46. }
  47. #endif // ULONG_MAX > 0xFFFFFFFF
  48. addr_.s_addr = asio::detail::socket_ops::host_to_network_long(
  49. static_cast<asio::detail::u_long_type>(addr));
  50. }
  51. address_v4::bytes_type address_v4::to_bytes() const
  52. {
  53. using namespace std; // For memcpy.
  54. bytes_type bytes;
  55. #if defined(ASIO_HAS_STD_ARRAY)
  56. memcpy(bytes.data(), &addr_.s_addr, 4);
  57. #else // defined(ASIO_HAS_STD_ARRAY)
  58. memcpy(bytes.elems, &addr_.s_addr, 4);
  59. #endif // defined(ASIO_HAS_STD_ARRAY)
  60. return bytes;
  61. }
  62. unsigned long address_v4::to_ulong() const
  63. {
  64. return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
  65. }
  66. std::string address_v4::to_string() const
  67. {
  68. asio::error_code ec;
  69. std::string addr = to_string(ec);
  70. asio::detail::throw_error(ec);
  71. return addr;
  72. }
  73. std::string address_v4::to_string(asio::error_code& ec) const
  74. {
  75. char addr_str[asio::detail::max_addr_v4_str_len];
  76. const char* addr =
  77. asio::detail::socket_ops::inet_ntop(
  78. ASIO_OS_DEF(AF_INET), &addr_, addr_str,
  79. asio::detail::max_addr_v4_str_len, 0, ec);
  80. if (addr == 0)
  81. return std::string();
  82. return addr;
  83. }
  84. address_v4 address_v4::from_string(const char* str)
  85. {
  86. asio::error_code ec;
  87. address_v4 addr = from_string(str, ec);
  88. asio::detail::throw_error(ec);
  89. return addr;
  90. }
  91. address_v4 address_v4::from_string(
  92. const char* str, asio::error_code& ec)
  93. {
  94. address_v4 tmp;
  95. if (asio::detail::socket_ops::inet_pton(
  96. ASIO_OS_DEF(AF_INET), str, &tmp.addr_, 0, ec) <= 0)
  97. return address_v4();
  98. return tmp;
  99. }
  100. address_v4 address_v4::from_string(const std::string& str)
  101. {
  102. return from_string(str.c_str());
  103. }
  104. address_v4 address_v4::from_string(
  105. const std::string& str, asio::error_code& ec)
  106. {
  107. return from_string(str.c_str(), ec);
  108. }
  109. bool address_v4::is_loopback() const
  110. {
  111. return (to_ulong() & 0xFF000000) == 0x7F000000;
  112. }
  113. bool address_v4::is_unspecified() const
  114. {
  115. return to_ulong() == 0;
  116. }
  117. bool address_v4::is_class_a() const
  118. {
  119. return (to_ulong() & 0x80000000) == 0;
  120. }
  121. bool address_v4::is_class_b() const
  122. {
  123. return (to_ulong() & 0xC0000000) == 0x80000000;
  124. }
  125. bool address_v4::is_class_c() const
  126. {
  127. return (to_ulong() & 0xE0000000) == 0xC0000000;
  128. }
  129. bool address_v4::is_multicast() const
  130. {
  131. return (to_ulong() & 0xF0000000) == 0xE0000000;
  132. }
  133. address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
  134. {
  135. return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
  136. }
  137. address_v4 address_v4::netmask(const address_v4& addr)
  138. {
  139. if (addr.is_class_a())
  140. return address_v4(0xFF000000);
  141. if (addr.is_class_b())
  142. return address_v4(0xFFFF0000);
  143. if (addr.is_class_c())
  144. return address_v4(0xFFFFFF00);
  145. return address_v4(0xFFFFFFFF);
  146. }
  147. } // namespace ip
  148. } // namespace asio
  149. #include "asio/detail/pop_options.hpp"
  150. #endif // ASIO_IP_IMPL_ADDRESS_V4_IPP