Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // ip/address_v4.hpp
0003 // ~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 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 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0125   /// (Deprecated: Use to_uint().) Get the address as an unsigned long in host
0126   /// byte order.
0127   BOOST_ASIO_DECL unsigned long to_ulong() const;
0128 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0129 
0130   /// Get the address as a string in dotted decimal format.
0131   BOOST_ASIO_DECL std::string to_string() const;
0132 
0133 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0134   /// (Deprecated: Use other overload.) Get the address as a string in dotted
0135   /// decimal format.
0136   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
0137 
0138   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
0139   /// string in dotted decimal form.
0140   static address_v4 from_string(const char* str);
0141 
0142   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
0143   /// string in dotted decimal form.
0144   static address_v4 from_string(
0145       const char* str, boost::system::error_code& ec);
0146 
0147   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
0148   /// string in dotted decimal form.
0149   static address_v4 from_string(const std::string& str);
0150 
0151   /// (Deprecated: Use make_address_v4().) Create an address from an IP address
0152   /// string in dotted decimal form.
0153   static address_v4 from_string(
0154       const std::string& str, boost::system::error_code& ec);
0155 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0156 
0157   /// Determine whether the address is a loopback address.
0158   /**
0159    * This function tests whether the address is in the address block
0160    * <tt>127.0.0.0/8</tt>, which corresponds to the address range
0161    * <tt>127.0.0.0 - 127.255.255.255</tt>.
0162    *
0163    * @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
0164    */
0165   BOOST_ASIO_DECL bool is_loopback() const noexcept;
0166 
0167   /// Determine whether the address is unspecified.
0168   /**
0169    * This function tests whether the address is the unspecified address
0170    * <tt>0.0.0.0</tt>.
0171    *
0172    * @returns <tt>to_uint() == 0</tt>.
0173    */
0174   BOOST_ASIO_DECL bool is_unspecified() const noexcept;
0175 
0176 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0177   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
0178   /// class A address.
0179   BOOST_ASIO_DECL bool is_class_a() const;
0180 
0181   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
0182   /// class B address.
0183   BOOST_ASIO_DECL bool is_class_b() const;
0184 
0185   /// (Deprecated: Use network_v4 class.) Determine whether the address is a
0186   /// class C address.
0187   BOOST_ASIO_DECL bool is_class_c() const;
0188 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0189 
0190   /// Determine whether the address is a multicast address.
0191   /**
0192    * This function tests whether the address is in the multicast address block
0193    * <tt>224.0.0.0/4</tt>, which corresponds to the address range
0194    * <tt>224.0.0.0 - 239.255.255.255</tt>.
0195    *
0196    * @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
0197    */
0198   BOOST_ASIO_DECL bool is_multicast() const noexcept;
0199 
0200   /// Compare two addresses for equality.
0201   friend bool operator==(const address_v4& a1,
0202       const address_v4& a2) noexcept
0203   {
0204     return a1.addr_.s_addr == a2.addr_.s_addr;
0205   }
0206 
0207   /// Compare two addresses for inequality.
0208   friend bool operator!=(const address_v4& a1,
0209       const address_v4& a2) noexcept
0210   {
0211     return a1.addr_.s_addr != a2.addr_.s_addr;
0212   }
0213 
0214   /// Compare addresses for ordering.
0215   /**
0216    * Compares two addresses in host byte order.
0217    *
0218    * @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
0219    */
0220   friend bool operator<(const address_v4& a1,
0221       const address_v4& a2) noexcept
0222   {
0223     return a1.to_uint() < a2.to_uint();
0224   }
0225 
0226   /// Compare addresses for ordering.
0227   /**
0228    * Compares two addresses in host byte order.
0229    *
0230    * @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
0231    */
0232   friend bool operator>(const address_v4& a1,
0233       const address_v4& a2) noexcept
0234   {
0235     return a1.to_uint() > a2.to_uint();
0236   }
0237 
0238   /// Compare addresses for ordering.
0239   /**
0240    * Compares two addresses in host byte order.
0241    *
0242    * @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
0243    */
0244   friend bool operator<=(const address_v4& a1,
0245       const address_v4& a2) noexcept
0246   {
0247     return a1.to_uint() <= a2.to_uint();
0248   }
0249 
0250   /// Compare addresses for ordering.
0251   /**
0252    * Compares two addresses in host byte order.
0253    *
0254    * @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
0255    */
0256   friend bool operator>=(const address_v4& a1,
0257       const address_v4& a2) noexcept
0258   {
0259     return a1.to_uint() >= a2.to_uint();
0260   }
0261 
0262   /// Obtain an address object that represents any address.
0263   /**
0264    * This functions returns an address that represents the "any" address
0265    * <tt>0.0.0.0</tt>.
0266    *
0267    * @returns A default-constructed @c address_v4 object.
0268    */
0269   static address_v4 any() noexcept
0270   {
0271     return address_v4();
0272   }
0273 
0274   /// Obtain an address object that represents the loopback address.
0275   /**
0276    * This function returns an address that represents the well-known loopback
0277    * address <tt>127.0.0.1</tt>.
0278    *
0279    * @returns <tt>address_v4(0x7F000001)</tt>.
0280    */
0281   static address_v4 loopback() noexcept
0282   {
0283     return address_v4(0x7F000001);
0284   }
0285 
0286   /// Obtain an address object that represents the broadcast address.
0287   /**
0288    * This function returns an address that represents the broadcast address
0289    * <tt>255.255.255.255</tt>.
0290    *
0291    * @returns <tt>address_v4(0xFFFFFFFF)</tt>.
0292    */
0293   static address_v4 broadcast() noexcept
0294   {
0295     return address_v4(0xFFFFFFFF);
0296   }
0297 
0298 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0299   /// (Deprecated: Use network_v4 class.) Obtain an address object that
0300   /// represents the broadcast address that corresponds to the specified
0301   /// address and netmask.
0302   BOOST_ASIO_DECL static address_v4 broadcast(
0303       const address_v4& addr, const address_v4& mask);
0304 
0305   /// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
0306   /// to the address, based on its address class.
0307   BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
0308 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0309 
0310 private:
0311   // The underlying IPv4 address.
0312   boost::asio::detail::in4_addr_type addr_;
0313 };
0314 
0315 /// Create an IPv4 address from raw bytes in network order.
0316 /**
0317  * @relates address_v4
0318  */
0319 inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
0320 {
0321   return address_v4(bytes);
0322 }
0323 
0324 /// Create an IPv4 address from an unsigned integer in host byte order.
0325 /**
0326  * @relates address_v4
0327  */
0328 inline address_v4 make_address_v4(address_v4::uint_type addr)
0329 {
0330   return address_v4(addr);
0331 }
0332 
0333 /// Create an IPv4 address from an IP address string in dotted decimal form.
0334 /**
0335  * @relates address_v4
0336  */
0337 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
0338 
0339 /// Create an IPv4 address from an IP address string in dotted decimal form.
0340 /**
0341  * @relates address_v4
0342  */
0343 BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
0344     boost::system::error_code& ec) noexcept;
0345 
0346 /// Create an IPv4 address from an IP address string in dotted decimal form.
0347 /**
0348  * @relates address_v4
0349  */
0350 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
0351 
0352 /// Create an IPv4 address from an IP address string in dotted decimal form.
0353 /**
0354  * @relates address_v4
0355  */
0356 BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
0357     boost::system::error_code& ec) noexcept;
0358 
0359 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
0360   || defined(GENERATING_DOCUMENTATION)
0361 
0362 /// Create an IPv4 address from an IP address string in dotted decimal form.
0363 /**
0364  * @relates address_v4
0365  */
0366 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
0367 
0368 /// Create an IPv4 address from an IP address string in dotted decimal form.
0369 /**
0370  * @relates address_v4
0371  */
0372 BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
0373     boost::system::error_code& ec) noexcept;
0374 
0375 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0376        //  || defined(GENERATING_DOCUMENTATION)
0377 
0378 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0379 
0380 /// Output an address as a string.
0381 /**
0382  * Used to output a human-readable string for a specified address.
0383  *
0384  * @param os The output stream to which the string will be written.
0385  *
0386  * @param addr The address to be written.
0387  *
0388  * @return The output stream.
0389  *
0390  * @relates boost::asio::ip::address_v4
0391  */
0392 template <typename Elem, typename Traits>
0393 std::basic_ostream<Elem, Traits>& operator<<(
0394     std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
0395 
0396 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0397 
0398 } // namespace ip
0399 } // namespace asio
0400 } // namespace boost
0401 
0402 namespace std {
0403 
0404 template <>
0405 struct hash<boost::asio::ip::address_v4>
0406 {
0407   std::size_t operator()(const boost::asio::ip::address_v4& addr)
0408     const noexcept
0409   {
0410     return std::hash<unsigned int>()(addr.to_uint());
0411   }
0412 };
0413 
0414 } // namespace std
0415 
0416 #include <boost/asio/detail/pop_options.hpp>
0417 
0418 #include <boost/asio/ip/impl/address_v4.hpp>
0419 #if defined(BOOST_ASIO_HEADER_ONLY)
0420 # include <boost/asio/ip/impl/address_v4.ipp>
0421 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0422 
0423 #endif // BOOST_ASIO_IP_ADDRESS_V4_HPP