Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // ip/impl/network_v4.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_V4_IPP
0013 #define BOOST_ASIO_IP_IMPL_NETWORK_V4_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_v4.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace ip {
0034 
0035 network_v4::network_v4(const address_v4& addr, unsigned short prefix_len)
0036   : address_(addr),
0037     prefix_length_(prefix_len)
0038 {
0039   if (prefix_len > 32)
0040   {
0041     std::out_of_range ex("prefix length too large");
0042     boost::asio::detail::throw_exception(ex);
0043   }
0044 }
0045 
0046 network_v4::network_v4(const address_v4& addr, const address_v4& mask)
0047   : address_(addr),
0048     prefix_length_(0)
0049 {
0050   address_v4::bytes_type mask_bytes = mask.to_bytes();
0051   bool finished = false;
0052   for (std::size_t i = 0; i < mask_bytes.size(); ++i)
0053   {
0054     if (finished)
0055     {
0056       if (mask_bytes[i])
0057       {
0058         std::invalid_argument ex("non-contiguous netmask");
0059         boost::asio::detail::throw_exception(ex);
0060       }
0061       continue;
0062     }
0063     else
0064     {
0065       switch (mask_bytes[i])
0066       {
0067       case 255:
0068         prefix_length_ += 8;
0069         break;
0070       case 254: // prefix_length_ += 7
0071         prefix_length_ += 1;
0072       case 252: // prefix_length_ += 6
0073         prefix_length_ += 1;
0074       case 248: // prefix_length_ += 5
0075         prefix_length_ += 1;
0076       case 240: // prefix_length_ += 4
0077         prefix_length_ += 1;
0078       case 224: // prefix_length_ += 3
0079         prefix_length_ += 1;
0080       case 192: // prefix_length_ += 2
0081         prefix_length_ += 1;
0082       case 128: // prefix_length_ += 1
0083         prefix_length_ += 1;
0084       case 0:   // nbits += 0
0085         finished = true;
0086         break;
0087       default:
0088         std::out_of_range ex("non-contiguous netmask");
0089         boost::asio::detail::throw_exception(ex);
0090       }
0091     }
0092   }
0093 }
0094 
0095 address_v4 network_v4::netmask() const noexcept
0096 {
0097   uint32_t nmbits = 0xffffffff;
0098   if (prefix_length_ == 0)
0099     nmbits = 0;
0100   else
0101     nmbits = nmbits << (32 - prefix_length_);
0102   return address_v4(nmbits);
0103 }
0104 
0105 address_v4_range network_v4::hosts() const noexcept
0106 {
0107   return is_host()
0108     ? address_v4_range(address_, address_v4(address_.to_uint() + 1))
0109     : address_v4_range(address_v4(network().to_uint() + 1), broadcast());
0110 }
0111 
0112 bool network_v4::is_subnet_of(const network_v4& other) const
0113 {
0114   if (other.prefix_length_ >= prefix_length_)
0115     return false; // Only real subsets are allowed.
0116   const network_v4 me(address_, other.prefix_length_);
0117   return other.canonical() == me.canonical();
0118 }
0119 
0120 std::string network_v4::to_string() const
0121 {
0122   boost::system::error_code ec;
0123   std::string addr = to_string(ec);
0124   boost::asio::detail::throw_error(ec);
0125   return addr;
0126 }
0127 
0128 std::string network_v4::to_string(boost::system::error_code& ec) const
0129 {
0130   using namespace std; // For sprintf.
0131   ec = boost::system::error_code();
0132   char prefix_len[16];
0133 #if defined(BOOST_ASIO_HAS_SNPRINTF)
0134   snprintf(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0135 #elif defined(BOOST_ASIO_HAS_SECURE_RTL)
0136   sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_);
0137 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
0138   sprintf(prefix_len, "/%u", prefix_length_);
0139 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
0140   return address_.to_string() + prefix_len;
0141 }
0142 
0143 network_v4 make_network_v4(const char* str)
0144 {
0145   return make_network_v4(std::string(str));
0146 }
0147 
0148 network_v4 make_network_v4(const char* str, boost::system::error_code& ec)
0149 {
0150   return make_network_v4(std::string(str), ec);
0151 }
0152 
0153 network_v4 make_network_v4(const std::string& str)
0154 {
0155   boost::system::error_code ec;
0156   network_v4 net = make_network_v4(str, ec);
0157   boost::asio::detail::throw_error(ec);
0158   return net;
0159 }
0160 
0161 network_v4 make_network_v4(const std::string& str,
0162     boost::system::error_code& ec)
0163 {
0164   std::string::size_type pos = str.find_first_of("/");
0165 
0166   if (pos == std::string::npos)
0167   {
0168     ec = boost::asio::error::invalid_argument;
0169     return network_v4();
0170   }
0171 
0172   if (pos == str.size() - 1)
0173   {
0174     ec = boost::asio::error::invalid_argument;
0175     return network_v4();
0176   }
0177 
0178   std::string::size_type end = str.find_first_not_of("0123456789", pos + 1);
0179   if (end != std::string::npos)
0180   {
0181     ec = boost::asio::error::invalid_argument;
0182     return network_v4();
0183   }
0184 
0185   const address_v4 addr = make_address_v4(str.substr(0, pos), ec);
0186   if (ec)
0187     return network_v4();
0188 
0189   const int prefix_len = std::atoi(str.substr(pos + 1).c_str());
0190   if (prefix_len < 0 || prefix_len > 32)
0191   {
0192     ec = boost::asio::error::invalid_argument;
0193     return network_v4();
0194   }
0195 
0196   return network_v4(addr, static_cast<unsigned short>(prefix_len));
0197 }
0198 
0199 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0200 
0201 network_v4 make_network_v4(string_view str)
0202 {
0203   return make_network_v4(static_cast<std::string>(str));
0204 }
0205 
0206 network_v4 make_network_v4(string_view str,
0207     boost::system::error_code& ec)
0208 {
0209   return make_network_v4(static_cast<std::string>(str), ec);
0210 }
0211 
0212 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0213 
0214 } // namespace ip
0215 } // namespace asio
0216 } // namespace boost
0217 
0218 #include <boost/asio/detail/pop_options.hpp>
0219 
0220 #endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP