Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:00

0001 //
0002 // ip/network_v4.hpp
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_NETWORK_V4_HPP
0013 #define BOOST_ASIO_IP_NETWORK_V4_HPP
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 <string>
0021 #include <boost/asio/detail/string_view.hpp>
0022 #include <boost/system/error_code.hpp>
0023 #include <boost/asio/ip/address_v4_range.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace ip {
0030 
0031 /// Represents an IPv4 network.
0032 /**
0033  * The boost::asio::ip::network_v4 class provides the ability to use and
0034  * manipulate IP version 4 networks.
0035  *
0036  * @par Thread Safety
0037  * @e Distinct @e objects: Safe.@n
0038  * @e Shared @e objects: Unsafe.
0039  */
0040 class network_v4
0041 {
0042 public:
0043   /// Default constructor.
0044   network_v4() noexcept
0045     : address_(),
0046       prefix_length_(0)
0047   {
0048   }
0049 
0050   /// Construct a network based on the specified address and prefix length.
0051   BOOST_ASIO_DECL network_v4(const address_v4& addr,
0052       unsigned short prefix_len);
0053 
0054   /// Construct network based on the specified address and netmask.
0055   BOOST_ASIO_DECL network_v4(const address_v4& addr,
0056       const address_v4& mask);
0057 
0058   /// Copy constructor.
0059   network_v4(const network_v4& other) noexcept
0060     : address_(other.address_),
0061       prefix_length_(other.prefix_length_)
0062   {
0063   }
0064 
0065   /// Move constructor.
0066   network_v4(network_v4&& other) noexcept
0067     : address_(static_cast<address_v4&&>(other.address_)),
0068       prefix_length_(other.prefix_length_)
0069   {
0070   }
0071 
0072   /// Assign from another network.
0073   network_v4& operator=(const network_v4& other) noexcept
0074   {
0075     address_ = other.address_;
0076     prefix_length_ = other.prefix_length_;
0077     return *this;
0078   }
0079 
0080   /// Move-assign from another network.
0081   network_v4& operator=(network_v4&& other) noexcept
0082   {
0083     address_ = static_cast<address_v4&&>(other.address_);
0084     prefix_length_ = other.prefix_length_;
0085     return *this;
0086   }
0087 
0088   /// Obtain the address object specified when the network object was created.
0089   address_v4 address() const noexcept
0090   {
0091     return address_;
0092   }
0093 
0094   /// Obtain the prefix length that was specified when the network object was
0095   /// created.
0096   unsigned short prefix_length() const noexcept
0097   {
0098     return prefix_length_;
0099   }
0100 
0101   /// Obtain the netmask that was specified when the network object was created.
0102   BOOST_ASIO_DECL address_v4 netmask() const noexcept;
0103 
0104   /// Obtain an address object that represents the network address.
0105   address_v4 network() const noexcept
0106   {
0107     return address_v4(address_.to_uint() & netmask().to_uint());
0108   }
0109 
0110   /// Obtain an address object that represents the network's broadcast address.
0111   address_v4 broadcast() const noexcept
0112   {
0113     return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF));
0114   }
0115 
0116   /// Obtain an address range corresponding to the hosts in the network.
0117   BOOST_ASIO_DECL address_v4_range hosts() const noexcept;
0118 
0119   /// Obtain the true network address, omitting any host bits.
0120   network_v4 canonical() const noexcept
0121   {
0122     return network_v4(network(), prefix_length());
0123   }
0124 
0125   /// Test if network is a valid host address.
0126   bool is_host() const noexcept
0127   {
0128     return prefix_length_ == 32;
0129   }
0130 
0131   /// Test if a network is a real subnet of another network.
0132   BOOST_ASIO_DECL bool is_subnet_of(const network_v4& other) const;
0133 
0134   /// Get the network as an address in dotted decimal format.
0135   BOOST_ASIO_DECL std::string to_string() const;
0136 
0137   /// Get the network as an address in dotted decimal format.
0138   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
0139 
0140   /// Compare two networks for equality.
0141   friend bool operator==(const network_v4& a, const network_v4& b)
0142   {
0143     return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
0144   }
0145 
0146   /// Compare two networks for inequality.
0147   friend bool operator!=(const network_v4& a, const network_v4& b)
0148   {
0149     return !(a == b);
0150   }
0151 
0152 private:
0153   address_v4 address_;
0154   unsigned short prefix_length_;
0155 };
0156 
0157 /// Create an IPv4 network from an address and prefix length.
0158 /**
0159  * @relates address_v4
0160  */
0161 inline network_v4 make_network_v4(
0162     const address_v4& addr, unsigned short prefix_len)
0163 {
0164   return network_v4(addr, prefix_len);
0165 }
0166 
0167 /// Create an IPv4 network from an address and netmask.
0168 /**
0169  * @relates address_v4
0170  */
0171 inline network_v4 make_network_v4(
0172     const address_v4& addr, const address_v4& mask)
0173 {
0174   return network_v4(addr, mask);
0175 }
0176 
0177 /// Create an IPv4 network from a string containing IP address and prefix
0178 /// length.
0179 /**
0180  * @relates network_v4
0181  */
0182 BOOST_ASIO_DECL network_v4 make_network_v4(const char* str);
0183 
0184 /// Create an IPv4 network from a string containing IP address and prefix
0185 /// length.
0186 /**
0187  * @relates network_v4
0188  */
0189 BOOST_ASIO_DECL network_v4 make_network_v4(
0190     const char* str, boost::system::error_code& ec);
0191 
0192 /// Create an IPv4 network from a string containing IP address and prefix
0193 /// length.
0194 /**
0195  * @relates network_v4
0196  */
0197 BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str);
0198 
0199 /// Create an IPv4 network from a string containing IP address and prefix
0200 /// length.
0201 /**
0202  * @relates network_v4
0203  */
0204 BOOST_ASIO_DECL network_v4 make_network_v4(
0205     const std::string& str, boost::system::error_code& ec);
0206 
0207 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
0208   || defined(GENERATING_DOCUMENTATION)
0209 
0210 /// Create an IPv4 network from a string containing IP address and prefix
0211 /// length.
0212 /**
0213  * @relates network_v4
0214  */
0215 BOOST_ASIO_DECL network_v4 make_network_v4(string_view str);
0216 
0217 /// Create an IPv4 network from a string containing IP address and prefix
0218 /// length.
0219 /**
0220  * @relates network_v4
0221  */
0222 BOOST_ASIO_DECL network_v4 make_network_v4(
0223     string_view str, boost::system::error_code& ec);
0224 
0225 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0226        //  || defined(GENERATING_DOCUMENTATION)
0227 
0228 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0229 
0230 /// Output a network as a string.
0231 /**
0232  * Used to output a human-readable string for a specified network.
0233  *
0234  * @param os The output stream to which the string will be written.
0235  *
0236  * @param net The network to be written.
0237  *
0238  * @return The output stream.
0239  *
0240  * @relates boost::asio::ip::address_v4
0241  */
0242 template <typename Elem, typename Traits>
0243 std::basic_ostream<Elem, Traits>& operator<<(
0244     std::basic_ostream<Elem, Traits>& os, const network_v4& net);
0245 
0246 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0247 
0248 } // namespace ip
0249 } // namespace asio
0250 } // namespace boost
0251 
0252 #include <boost/asio/detail/pop_options.hpp>
0253 
0254 #include <boost/asio/ip/impl/network_v4.hpp>
0255 #if defined(BOOST_ASIO_HEADER_ONLY)
0256 # include <boost/asio/ip/impl/network_v4.ipp>
0257 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0258 
0259 #endif // BOOST_ASIO_IP_NETWORK_V4_HPP