Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-18 08:39:26

0001 //
0002 // ip/address_v4.hpp
0003 // ~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP
0012 #define BOOST_ASIO_IP_ADDRESS_V4_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <functional>
0020 #include <string>
0021 #include <boost/asio/detail/array.hpp>
0022 #include <boost/asio/detail/cstdint.hpp>
0023 #include <boost/asio/detail/socket_types.hpp>
0024 #include <boost/asio/detail/string_view.hpp>
0025 #include <boost/asio/detail/winsock_init.hpp>
0026 #include <boost/system/error_code.hpp>
0027 
0028 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0029 # include <iosfwd>
0030 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0031 
0032 #include <boost/asio/detail/push_options.hpp>
0033 
0034 namespace boost {
0035 namespace asio {
0036 namespace ip {
0037 
0038 /// Implements IP version 4 style addresses.
0039 /**
0040  * The boost::asio::ip::address_v4 class provides the ability to use and
0041  * manipulate IP version 4 addresses.
0042  *
0043  * @par Thread Safety
0044  * @e Distinct @e objects: Safe.@n
0045  * @e Shared @e objects: Unsafe.
0046  */
0047 class address_v4
0048 {
0049 public:
0050   /// The type used to represent an address as an unsigned integer.
0051   typedef uint_least32_t uint_type;
0052 
0053   /// The type used to represent an address as an array of bytes.
0054   /**
0055    * @note This type is defined in terms of the C++0x template @c std::array
0056    * when it is available. Otherwise, it uses @c boost:array.
0057    */
0058 #if defined(GENERATING_DOCUMENTATION)
0059   typedef array<unsigned char, 4> bytes_type;
0060 #else
0061   typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
0062 #endif
0063 
0064   /// Default constructor.
0065   /**
0066    * Initialises the @c address_v4 object such that:
0067    * @li <tt>to_bytes()</tt> yields <tt>{0, 0, 0, 0}</tt>; and
0068    * @li <tt>to_uint() == 0</tt>.
0069    */
0070   address_v4() noexcept
0071   {
0072     addr_.s_addr = 0;
0073   }
0074 
0075   /// Construct an address from raw bytes.
0076   /**
0077    * Initialises the @c address_v4 object such that <tt>to_bytes() ==
0078    * bytes</tt>.
0079    *
0080    * @throws out_of_range Thrown if any element in @c bytes is not in the range
0081    * <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
0082    * where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
0083    */
0084   BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
0085 
0086   /// Construct an address from an unsigned integer in host byte order.
0087   /**
0088    * Initialises the @c address_v4 object such that <tt>to_uint() == addr</tt>.
0089    */
0090   BOOST_ASIO_DECL explicit address_v4(uint_type addr);
0091 
0092   /// Copy constructor.
0093   address_v4(const address_v4& other) noexcept
0094     : addr_(other.addr_)
0095   {
0096   }
0097 
0098   /// Move constructor.
0099   address_v4(address_v4&& other) noexcept
0100     : addr_(other.addr_)
0101   {
0102   }
0103 
0104   /// Assign from another address.
0105   address_v4& operator=(const address_v4& other) noexcept
0106   {
0107     addr_ = other.addr_;
0108     return *this;
0109   }
0110 
0111   /// Move-assign from another address.
0112   address_v4& operator=(address_v4&& other) noexcept
0113   {
0114     addr_ = other.addr_;
0115     return *this;
0116   }
0117 
0118   /// Get the address in bytes, in network byte order.
0119   BOOST_ASIO_DECL bytes_type to_bytes() const noexcept;
0120 
0121   /// Get the address as an unsigned integer in host byte order.
0122   BOOST_ASIO_DECL uint_type to_uint() const noexcept;
0123 
0124   /// Get the address as a string in dotted decimal format.
0125   BOOST_ASIO_DECL std::string to_string() const;
0126 
0127   /// Determine whether the address is a loopback address.
0128   /**
0129    * This function tests whether the address is in the address block
0130    * <tt>127.0.0.0/8</tt>, which corresponds to the address range
0131    * <tt>127.0.0.0 - 127.255.255.255</tt>.
0132    *
0133    * @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
0134    */
0135   BOOST_ASIO_DECL bool is_loopback() const noexcept;
0136 
0137   /// Determine whether the address is unspecified.
0138   /**
0139    * This function tests whether the address is the unspecified address
0140    * <tt>0.0.0.0</tt>.
0141    *
0142    * @returns <tt>to_uint() == 0</tt>.
0143    */
0144   BOOST_ASIO_DECL bool is_unspecified() const noexcept;
0145 
0146   /// Determine whether the address is a multicast address.
0147   /**
0148    * This function tests whether the address is in the multicast address block
0149    * <tt>224.0.0.0/4</tt>, which corresponds to the address range
0150    * <tt>224.0.0.0 - 239.255.255.255</tt>.
0151    *
0152    * @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
0153    */
0154   BOOST_ASIO_DECL bool is_multicast() const noexcept;
0155 
0156   /// Compare two addresses for equality.
0157   friend bool operator==(const address_v4& a1,
0158       const address_v4& a2) noexcept
0159   {
0160     return a1.addr_.s_addr == a2.addr_.s_addr;
0161   }
0162 
0163   /// Compare two addresses for inequality.
0164   friend bool operator!=(const address_v4& a1,
0165       const address_v4& a2) noexcept
0166   {
0167     return a1.addr_.s_addr != a2.addr_.s_addr;
0168   }
0169 
0170   /// Compare addresses for ordering.
0171   /**
0172    * Compares two addresses in host byte order.
0173    *
0174    * @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
0175    */
0176   friend bool operator<(const address_v4& a1,
0177       const address_v4& a2) noexcept
0178   {
0179     return a1.to_uint() < a2.to_uint();
0180   }
0181 
0182   /// Compare addresses for ordering.
0183   /**
0184    * Compares two addresses in host byte order.
0185    *
0186    * @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
0187    */
0188   friend bool operator>(const address_v4& a1,
0189       const address_v4& a2) noexcept
0190   {
0191     return a1.to_uint() > a2.to_uint();
0192   }
0193 
0194   /// Compare addresses for ordering.
0195   /**
0196    * Compares two addresses in host byte order.
0197    *
0198    * @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
0199    */
0200   friend bool operator<=(const address_v4& a1,
0201       const address_v4& a2) noexcept
0202   {
0203     return a1.to_uint() <= a2.to_uint();
0204   }
0205 
0206   /// Compare addresses for ordering.
0207   /**
0208    * Compares two addresses in host byte order.
0209    *
0210    * @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
0211    */
0212   friend bool operator>=(const address_v4& a1,
0213       const address_v4& a2) noexcept
0214   {
0215     return a1.to_uint() >= a2.to_uint();
0216   }
0217 
0218   /// Obtain an address object that represents any address.
0219   /**
0220    * This functions returns an address that represents the "any" address
0221    * <tt>0.0.0.0</tt>.
0222    *
0223    * @returns A default-constructed @c address_v4 object.
0224    */
0225   static address_v4 any() noexcept
0226   {
0227     return address_v4();
0228   }
0229 
0230   /// Obtain an address object that represents the loopback address.
0231   /**
0232    * This function returns an address that represents the well-known loopback
0233    * address <tt>127.0.0.1</tt>.
0234    *
0235    * @returns <tt>address_v4(0x7F000001)</tt>.
0236    */
0237   static address_v4 loopback() noexcept
0238   {
0239     return address_v4(0x7F000001);
0240   }
0241 
0242   /// Obtain an address object that represents the broadcast address.
0243   /**
0244    * This function returns an address that represents the broadcast address
0245    * <tt>255.255.255.255</tt>.
0246    *
0247    * @returns <tt>address_v4(0xFFFFFFFF)</tt>.
0248    */
0249   static address_v4 broadcast() noexcept
0250   {
0251     return address_v4(0xFFFFFFFF);
0252   }
0253 
0254 private:
0255   // The underlying IPv4 address.
0256   boost::asio::detail::in4_addr_type addr_;
0257 };
0258 
0259 /// Create an IPv4 address from raw bytes in network order.
0260 /**
0261  * @relates address_v4
0262  */
0263 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
0264 {
0265   return address_v4(bytes);
0266 }
0267 
0268 /// Create an IPv4 address from an unsigned integer in host byte order.
0269 /**
0270  * @relates address_v4
0271  */
0272 inline address_v4 make_address_v4(address_v4::uint_type addr)
0273 {
0274   return address_v4(addr);
0275 }
0276 
0277 /// Create an IPv4 address from an IP address string in dotted decimal form.
0278 /**
0279  * @relates address_v4
0280  */
0281 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
0282 
0283 /// Create an IPv4 address from an IP address string in dotted decimal form.
0284 /**
0285  * @relates address_v4
0286  */
0287 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
0288     boost::system::error_code& ec) noexcept;
0289 
0290 /// Create an IPv4 address from an IP address string in dotted decimal form.
0291 /**
0292  * @relates address_v4
0293  */
0294 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
0295 
0296 /// Create an IPv4 address from an IP address string in dotted decimal form.
0297 /**
0298  * @relates address_v4
0299  */
0300 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
0301     boost::system::error_code& ec) noexcept;
0302 
0303 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
0304   || defined(GENERATING_DOCUMENTATION)
0305 
0306 /// Create an IPv4 address from an IP address string in dotted decimal form.
0307 /**
0308  * @relates address_v4
0309  */
0310 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
0311 
0312 /// Create an IPv4 address from an IP address string in dotted decimal form.
0313 /**
0314  * @relates address_v4
0315  */
0316 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
0317     boost::system::error_code& ec) noexcept;
0318 
0319 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0320        //  || defined(GENERATING_DOCUMENTATION)
0321 
0322 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0323 
0324 /// Output an address as a string.
0325 /**
0326  * Used to output a human-readable string for a specified address.
0327  *
0328  * @param os The output stream to which the string will be written.
0329  *
0330  * @param addr The address to be written.
0331  *
0332  * @return The output stream.
0333  *
0334  * @relates boost::asio::ip::address_v4
0335  */
0336 template <typename Elem, typename Traits>
0337 std::basic_ostream<Elem, Traits>& operator<<(
0338     std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
0339 
0340 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0341 
0342 } // namespace ip
0343 } // namespace asio
0344 } // namespace boost
0345 
0346 namespace std {
0347 
0348 template <>
0349 struct hash<boost::asio::ip::address_v4>
0350 {
0351   std::size_t operator()(const boost::asio::ip::address_v4& addr)
0352     const noexcept
0353   {
0354     return std::hash<unsigned int>()(addr.to_uint());
0355   }
0356 };
0357 
0358 } // namespace std
0359 
0360 #include <boost/asio/detail/pop_options.hpp>
0361 
0362 #include <boost/asio/ip/impl/address_v4.hpp>
0363 #if defined(BOOST_ASIO_HEADER_ONLY)
0364 # include <boost/asio/ip/impl/address_v4.ipp>
0365 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0366 
0367 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP