Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // ip/address_v6.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_V6_HPP
0012 #define BOOST_ASIO_IP_ADDRESS_V6_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 #include <boost/asio/ip/address_v4.hpp>
0028 
0029 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0030 # include <iosfwd>
0031 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0032 
0033 #include <boost/asio/detail/push_options.hpp>
0034 
0035 namespace boost {
0036 namespace asio {
0037 namespace ip {
0038 
0039 template <typename> class basic_address_iterator;
0040 
0041 /// Type used for storing IPv6 scope IDs.
0042 typedef uint_least32_t scope_id_type;
0043 
0044 /// Implements IP version 6 style addresses.
0045 /**
0046  * The boost::asio::ip::address_v6 class provides the ability to use and
0047  * manipulate IP version 6 addresses.
0048  *
0049  * @par Thread Safety
0050  * @e Distinct @e objects: Safe.@n
0051  * @e Shared @e objects: Unsafe.
0052  */
0053 class address_v6
0054 {
0055 public:
0056   /// The type used to represent an address as an array of bytes.
0057   /**
0058    * @note This type is defined in terms of the C++0x template @c std::array
0059    * when it is available. Otherwise, it uses @c boost:array.
0060    */
0061 #if defined(GENERATING_DOCUMENTATION)
0062   typedef array<unsigned char, 16> bytes_type;
0063 #else
0064   typedef boost::asio::detail::array<unsigned char, 16> bytes_type;
0065 #endif
0066 
0067   /// Default constructor.
0068   /**
0069    * Initialises the @c address_v6 object such that:
0070    * @li <tt>to_bytes()</tt> yields <tt>{0, 0, ..., 0}</tt>; and
0071    * @li <tt>scope_id() == 0</tt>.
0072    */
0073   BOOST_ASIO_DECL address_v6() noexcept;
0074 
0075   /// Construct an address from raw bytes and scope ID.
0076   /**
0077    * Initialises the @c address_v6 object such that:
0078    * @li <tt>to_bytes() == bytes</tt>; and
0079    * @li <tt>this->scope_id() == scope_id</tt>.
0080    *
0081    * @throws out_of_range Thrown if any element in @c bytes is not in the range
0082    * <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
0083    * where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
0084    */
0085   BOOST_ASIO_DECL explicit address_v6(const bytes_type& bytes,
0086       scope_id_type scope_id = 0);
0087 
0088   /// Copy constructor.
0089   BOOST_ASIO_DECL address_v6(const address_v6& other) noexcept;
0090 
0091   /// Move constructor.
0092   BOOST_ASIO_DECL address_v6(address_v6&& other) noexcept;
0093 
0094   /// Assign from another address.
0095   BOOST_ASIO_DECL address_v6& operator=(
0096       const address_v6& other) noexcept;
0097 
0098   /// Move-assign from another address.
0099   BOOST_ASIO_DECL address_v6& operator=(address_v6&& other) noexcept;
0100 
0101   /// The scope ID of the address.
0102   /**
0103    * Returns the scope ID associated with the IPv6 address.
0104    */
0105   scope_id_type scope_id() const noexcept
0106   {
0107     return scope_id_;
0108   }
0109 
0110   /// The scope ID of the address.
0111   /**
0112    * Modifies the scope ID associated with the IPv6 address.
0113    *
0114    * @param id The new scope ID.
0115    */
0116   void scope_id(scope_id_type id) noexcept
0117   {
0118     scope_id_ = id;
0119   }
0120 
0121   /// Get the address in bytes, in network byte order.
0122   BOOST_ASIO_DECL bytes_type to_bytes() const noexcept;
0123 
0124   /// Get the address as a string.
0125   BOOST_ASIO_DECL std::string to_string() const;
0126 
0127 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0128   /// (Deprecated: Use other overload.) Get the address as a string.
0129   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
0130 
0131   /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
0132   /// address string.
0133   static address_v6 from_string(const char* str);
0134 
0135   /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
0136   /// address string.
0137   static address_v6 from_string(
0138       const char* str, boost::system::error_code& ec);
0139 
0140   /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
0141   /// address string.
0142   static address_v6 from_string(const std::string& str);
0143 
0144   /// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
0145   /// address string.
0146   static address_v6 from_string(
0147       const std::string& str, boost::system::error_code& ec);
0148 
0149   /// (Deprecated: Use make_address_v4().) Converts an IPv4-mapped or
0150   /// IPv4-compatible address to an IPv4 address.
0151   BOOST_ASIO_DECL address_v4 to_v4() const;
0152 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0153 
0154   /// Determine whether the address is a loopback address.
0155   /**
0156    * This function tests whether the address is the loopback address
0157    * <tt>::1</tt>.
0158    */
0159   BOOST_ASIO_DECL bool is_loopback() const noexcept;
0160 
0161   /// Determine whether the address is unspecified.
0162   /**
0163    * This function tests whether the address is the loopback address
0164    * <tt>::</tt>.
0165    */
0166   BOOST_ASIO_DECL bool is_unspecified() const noexcept;
0167 
0168   /// Determine whether the address is link local.
0169   BOOST_ASIO_DECL bool is_link_local() const noexcept;
0170 
0171   /// Determine whether the address is site local.
0172   BOOST_ASIO_DECL bool is_site_local() const noexcept;
0173 
0174   /// Determine whether the address is a mapped IPv4 address.
0175   BOOST_ASIO_DECL bool is_v4_mapped() const noexcept;
0176 
0177 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0178   /// (Deprecated: No replacement.) Determine whether the address is an
0179   /// IPv4-compatible address.
0180   BOOST_ASIO_DECL bool is_v4_compatible() const;
0181 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0182 
0183   /// Determine whether the address is a multicast address.
0184   BOOST_ASIO_DECL bool is_multicast() const noexcept;
0185 
0186   /// Determine whether the address is a global multicast address.
0187   BOOST_ASIO_DECL bool is_multicast_global() const noexcept;
0188 
0189   /// Determine whether the address is a link-local multicast address.
0190   BOOST_ASIO_DECL bool is_multicast_link_local() const noexcept;
0191 
0192   /// Determine whether the address is a node-local multicast address.
0193   BOOST_ASIO_DECL bool is_multicast_node_local() const noexcept;
0194 
0195   /// Determine whether the address is a org-local multicast address.
0196   BOOST_ASIO_DECL bool is_multicast_org_local() const noexcept;
0197 
0198   /// Determine whether the address is a site-local multicast address.
0199   BOOST_ASIO_DECL bool is_multicast_site_local() const noexcept;
0200 
0201   /// Compare two addresses for equality.
0202   BOOST_ASIO_DECL friend bool operator==(const address_v6& a1,
0203       const address_v6& a2) noexcept;
0204 
0205   /// Compare two addresses for inequality.
0206   friend bool operator!=(const address_v6& a1,
0207       const address_v6& a2) noexcept
0208   {
0209     return !(a1 == a2);
0210   }
0211 
0212   /// Compare addresses for ordering.
0213   BOOST_ASIO_DECL friend bool operator<(const address_v6& a1,
0214       const address_v6& a2) noexcept;
0215 
0216   /// Compare addresses for ordering.
0217   friend bool operator>(const address_v6& a1,
0218       const address_v6& a2) noexcept
0219   {
0220     return a2 < a1;
0221   }
0222 
0223   /// Compare addresses for ordering.
0224   friend bool operator<=(const address_v6& a1,
0225       const address_v6& a2) noexcept
0226   {
0227     return !(a2 < a1);
0228   }
0229 
0230   /// Compare addresses for ordering.
0231   friend bool operator>=(const address_v6& a1,
0232       const address_v6& a2) noexcept
0233   {
0234     return !(a1 < a2);
0235   }
0236 
0237   /// Obtain an address object that represents any address.
0238   /**
0239    * This functions returns an address that represents the "any" address
0240    * <tt>::</tt>.
0241    *
0242    * @returns A default-constructed @c address_v6 object.
0243    */
0244   static address_v6 any() noexcept
0245   {
0246     return address_v6();
0247   }
0248 
0249   /// Obtain an address object that represents the loopback address.
0250   /**
0251    * This function returns an address that represents the well-known loopback
0252    * address <tt>::1</tt>.
0253    */
0254   BOOST_ASIO_DECL static address_v6 loopback() noexcept;
0255 
0256 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0257   /// (Deprecated: Use make_address_v6().) Create an IPv4-mapped IPv6 address.
0258   BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
0259 
0260   /// (Deprecated: No replacement.) Create an IPv4-compatible IPv6 address.
0261   BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
0262 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0263 
0264 private:
0265   friend class basic_address_iterator<address_v6>;
0266 
0267   // The underlying IPv6 address.
0268   boost::asio::detail::in6_addr_type addr_;
0269 
0270   // The scope ID associated with the address.
0271   scope_id_type scope_id_;
0272 };
0273 
0274 /// Create an IPv6 address from raw bytes and scope ID.
0275 /**
0276  * @relates address_v6
0277  */
0278 inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
0279     scope_id_type scope_id = 0)
0280 {
0281   return address_v6(bytes, scope_id);
0282 }
0283 
0284 /// Create an IPv6 address from an IP address string.
0285 /**
0286  * @relates address_v6
0287  */
0288 BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
0289 
0290 /// Create an IPv6 address from an IP address string.
0291 /**
0292  * @relates address_v6
0293  */
0294 BOOST_ASIO_DECL address_v6 make_address_v6(const char* str,
0295     boost::system::error_code& ec) noexcept;
0296 
0297 /// Createan IPv6 address from an IP address string.
0298 /**
0299  * @relates address_v6
0300  */
0301 BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
0302 
0303 /// Create an IPv6 address from an IP address string.
0304 /**
0305  * @relates address_v6
0306  */
0307 BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str,
0308     boost::system::error_code& ec) noexcept;
0309 
0310 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
0311   || defined(GENERATING_DOCUMENTATION)
0312 
0313 /// Create an IPv6 address from an IP address string.
0314 /**
0315  * @relates address_v6
0316  */
0317 BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
0318 
0319 /// Create an IPv6 address from an IP address string.
0320 /**
0321  * @relates address_v6
0322  */
0323 BOOST_ASIO_DECL address_v6 make_address_v6(string_view str,
0324     boost::system::error_code& ec) noexcept;
0325 
0326 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0327        //  || defined(GENERATING_DOCUMENTATION)
0328 
0329 /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
0330 /// addresses.
0331 enum v4_mapped_t { v4_mapped };
0332 
0333 /// Create an IPv4 address from a IPv4-mapped IPv6 address.
0334 /**
0335  * @relates address_v4
0336  */
0337 BOOST_ASIO_DECL address_v4 make_address_v4(
0338     v4_mapped_t, const address_v6& v6_addr);
0339 
0340 /// Create an IPv4-mapped IPv6 address from an IPv4 address.
0341 /**
0342  * @relates address_v6
0343  */
0344 BOOST_ASIO_DECL address_v6 make_address_v6(
0345     v4_mapped_t, const address_v4& v4_addr);
0346 
0347 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0348 
0349 /// Output an address as a string.
0350 /**
0351  * Used to output a human-readable string for a specified address.
0352  *
0353  * @param os The output stream to which the string will be written.
0354  *
0355  * @param addr The address to be written.
0356  *
0357  * @return The output stream.
0358  *
0359  * @relates boost::asio::ip::address_v6
0360  */
0361 template <typename Elem, typename Traits>
0362 std::basic_ostream<Elem, Traits>& operator<<(
0363     std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
0364 
0365 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0366 
0367 } // namespace ip
0368 } // namespace asio
0369 } // namespace boost
0370 
0371 namespace std {
0372 
0373 template <>
0374 struct hash<boost::asio::ip::address_v6>
0375 {
0376   std::size_t operator()(const boost::asio::ip::address_v6& addr)
0377     const noexcept
0378   {
0379     const boost::asio::ip::address_v6::bytes_type bytes = addr.to_bytes();
0380     std::size_t result = static_cast<std::size_t>(addr.scope_id());
0381     combine_4_bytes(result, &bytes[0]);
0382     combine_4_bytes(result, &bytes[4]);
0383     combine_4_bytes(result, &bytes[8]);
0384     combine_4_bytes(result, &bytes[12]);
0385     return result;
0386   }
0387 
0388 private:
0389   static void combine_4_bytes(std::size_t& seed, const unsigned char* bytes)
0390   {
0391     const std::size_t bytes_hash =
0392       (static_cast<std::size_t>(bytes[0]) << 24) |
0393       (static_cast<std::size_t>(bytes[1]) << 16) |
0394       (static_cast<std::size_t>(bytes[2]) << 8) |
0395       (static_cast<std::size_t>(bytes[3]));
0396     seed ^= bytes_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
0397   }
0398 };
0399 
0400 } // namespace std
0401 
0402 #include <boost/asio/detail/pop_options.hpp>
0403 
0404 #include <boost/asio/ip/impl/address_v6.hpp>
0405 #if defined(BOOST_ASIO_HEADER_ONLY)
0406 # include <boost/asio/ip/impl/address_v6.ipp>
0407 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0408 
0409 #endif // BOOST_ASIO_IP_ADDRESS_V6_HPP