Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:59

0001 //
0002 // ip/impl/network_v6.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP
0013 #define BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 #include <climits>
0021 #include <cstdio>
0022 #include <cstdlib>
0023 #include <stdexcept>
0024 #include <boost/asio/error.hpp>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/detail/throw_exception.hpp>
0027 #include <boost/asio/ip/network_v6.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace ip {
0034 
0035 network_v6::network_v6(const address_v6& addr, unsigned short prefix_len)
0036   : address_(addr),
0037     prefix_length_(prefix_len)
0038 {
0039   if (prefix_len > 128)
0040   {
0041     std::out_of_range ex("prefix length too large");
0042     boost::asio::detail::throw_exception(ex);
0043   }
0044 }
0045 
0046 BOOST_ASIO_DECL address_v6 network_v6::network() const noexcept
0047 {
0048   address_v6::bytes_type bytes(address_.to_bytes());
0049   for (std::size_t i = 0; i < 16; ++i)
0050   {
0051     if (prefix_length_ <= i * 8)
0052       bytes[i] = 0;
0053     else if (prefix_length_ < (i + 1) * 8)
0054       bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
0055   }
0056   return address_v6(bytes, address_.scope_id());
0057 }
0058 
0059 address_v6_range network_v6::hosts() const noexcept
0060 {
0061   address_v6::bytes_type begin_bytes(address_.to_bytes());
0062   address_v6::bytes_type end_bytes(address_.to_bytes());
0063   for (std::size_t i = 0; i < 16; ++i)
0064   {
0065     if (prefix_length_ <= i * 8)
0066     {
0067       begin_bytes[i] = 0;
0068       end_bytes[i] = 0xFF;
0069     }
0070     else if (prefix_length_ < (i + 1) * 8)
0071     {
0072       begin_bytes[i] &= 0xFF00 >> (prefix_length_ % 8);
0073       end_bytes[i] |= 0xFF >> (prefix_length_ % 8);
0074     }
0075   }
0076   return address_v6_range(
0077       address_v6_iterator(address_v6(begin_bytes, address_.scope_id())),
0078       ++address_v6_iterator(address_v6(end_bytes, address_.scope_id())));
0079 }
0080 
0081 bool network_v6::is_subnet_of(const network_v6& other) const
0082 {
0083   if (other.prefix_length_ >= prefix_length_)
0084     return false; // Only real subsets are allowed.
0085   const network_v6 me(address_, other.prefix_length_);
0086   return other.canonical() == me.canonical();
0087 }
0088 
0089 std::string network_v6::to_string() const
0090 {
0091   boost::system::error_code ec;
0092   std::string addr = to_string(ec);
0093   boost::asio::detail::throw_error(ec);
0094   return addr;
0095 }
0096 
0097 std::string network_v6::to_string(boost::system::error_code& ec) const
0098 {
0099   using namespace std; // For sprintf.
0100   ec = boost::system::error_code();
0101   char prefix_len[16];
0102 #if defined(BOOST_ASIO_HAS_SNPRINTF)
0103   snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0104 #elif defined(BOOST_ASIO_HAS_SECURE_RTL)
0105   sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0106 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
0107   sprintf(prefix_len, "/%u", prefix_length_);
0108 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
0109   return address_.to_string() + prefix_len;
0110 }
0111 
0112 network_v6 make_network_v6(const char* str)
0113 {
0114   return make_network_v6(std::string(str));
0115 }
0116 
0117 network_v6 make_network_v6(const char* str, boost::system::error_code& ec)
0118 {
0119   return make_network_v6(std::string(str), ec);
0120 }
0121 
0122 network_v6 make_network_v6(const std::string& str)
0123 {
0124   boost::system::error_code ec;
0125   network_v6 net = make_network_v6(str, ec);
0126   boost::asio::detail::throw_error(ec);
0127   return net;
0128 }
0129 
0130 network_v6 make_network_v6(const std::string& str,
0131     boost::system::error_code& ec)
0132 {
0133   std::string::size_type pos = str.find_first_of("/");
0134 
0135   if (pos == std::string::npos)
0136   {
0137     ec = boost::asio::error::invalid_argument;
0138     return network_v6();
0139   }
0140 
0141   if (pos == str.size() - 1)
0142   {
0143     ec = boost::asio::error::invalid_argument;
0144     return network_v6();
0145   }
0146 
0147   std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
0148   if (end != std::string::npos)
0149   {
0150     ec = boost::asio::error::invalid_argument;
0151     return network_v6();
0152   }
0153 
0154   const address_v6 addr = make_address_v6(str.substr(0, pos), ec);
0155   if (ec)
0156     return network_v6();
0157 
0158   const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
0159   if (prefix_len < 0 || prefix_len > 128)
0160   {
0161     ec = boost::asio::error::invalid_argument;
0162     return network_v6();
0163   }
0164 
0165   return network_v6(addr, static_cast<unsigned short>(prefix_len));
0166 }
0167 
0168 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0169 
0170 network_v6 make_network_v6(string_view str)
0171 {
0172   return make_network_v6(static_cast<std::string>(str));
0173 }
0174 
0175 network_v6 make_network_v6(string_view str,
0176     boost::system::error_code& ec)
0177 {
0178   return make_network_v6(static_cast<std::string>(str), ec);
0179 }
0180 
0181 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0182 
0183 } // namespace ip
0184 } // namespace asio
0185 } // namespace boost
0186 
0187 #include <boost/asio/detail/pop_options.hpp>
0188 
0189 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP