File indexing completed on 2025-01-18 09:28:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IP_IMPL_ADDRESS_IPP
0012 #define BOOST_ASIO_IP_IMPL_ADDRESS_IPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019 #include <typeinfo>
0020 #include <boost/asio/detail/throw_error.hpp>
0021 #include <boost/asio/detail/throw_exception.hpp>
0022 #include <boost/asio/error.hpp>
0023 #include <boost/asio/ip/address.hpp>
0024 #include <boost/asio/ip/bad_address_cast.hpp>
0025 #include <boost/system/system_error.hpp>
0026
0027 #include <boost/asio/detail/push_options.hpp>
0028
0029 namespace boost {
0030 namespace asio {
0031 namespace ip {
0032
0033 address::address() noexcept
0034 : type_(ipv4),
0035 ipv4_address_(),
0036 ipv6_address_()
0037 {
0038 }
0039
0040 address::address(
0041 const boost::asio::ip::address_v4& ipv4_address) noexcept
0042 : type_(ipv4),
0043 ipv4_address_(ipv4_address),
0044 ipv6_address_()
0045 {
0046 }
0047
0048 address::address(
0049 const boost::asio::ip::address_v6& ipv6_address) noexcept
0050 : type_(ipv6),
0051 ipv4_address_(),
0052 ipv6_address_(ipv6_address)
0053 {
0054 }
0055
0056 address::address(const address& other) noexcept
0057 : type_(other.type_),
0058 ipv4_address_(other.ipv4_address_),
0059 ipv6_address_(other.ipv6_address_)
0060 {
0061 }
0062
0063 address::address(address&& other) noexcept
0064 : type_(other.type_),
0065 ipv4_address_(other.ipv4_address_),
0066 ipv6_address_(other.ipv6_address_)
0067 {
0068 }
0069
0070 address& address::operator=(const address& other) noexcept
0071 {
0072 type_ = other.type_;
0073 ipv4_address_ = other.ipv4_address_;
0074 ipv6_address_ = other.ipv6_address_;
0075 return *this;
0076 }
0077
0078 address& address::operator=(address&& other) noexcept
0079 {
0080 type_ = other.type_;
0081 ipv4_address_ = other.ipv4_address_;
0082 ipv6_address_ = other.ipv6_address_;
0083 return *this;
0084 }
0085
0086 address& address::operator=(
0087 const boost::asio::ip::address_v4& ipv4_address) noexcept
0088 {
0089 type_ = ipv4;
0090 ipv4_address_ = ipv4_address;
0091 ipv6_address_ = boost::asio::ip::address_v6();
0092 return *this;
0093 }
0094
0095 address& address::operator=(
0096 const boost::asio::ip::address_v6& ipv6_address) noexcept
0097 {
0098 type_ = ipv6;
0099 ipv4_address_ = boost::asio::ip::address_v4();
0100 ipv6_address_ = ipv6_address;
0101 return *this;
0102 }
0103
0104 address make_address(const char* str)
0105 {
0106 boost::system::error_code ec;
0107 address addr = make_address(str, ec);
0108 boost::asio::detail::throw_error(ec);
0109 return addr;
0110 }
0111
0112 address make_address(const char* str,
0113 boost::system::error_code& ec) noexcept
0114 {
0115 boost::asio::ip::address_v6 ipv6_address =
0116 boost::asio::ip::make_address_v6(str, ec);
0117 if (!ec)
0118 return address(ipv6_address);
0119
0120 boost::asio::ip::address_v4 ipv4_address =
0121 boost::asio::ip::make_address_v4(str, ec);
0122 if (!ec)
0123 return address(ipv4_address);
0124
0125 return address();
0126 }
0127
0128 address make_address(const std::string& str)
0129 {
0130 return make_address(str.c_str());
0131 }
0132
0133 address make_address(const std::string& str,
0134 boost::system::error_code& ec) noexcept
0135 {
0136 return make_address(str.c_str(), ec);
0137 }
0138
0139 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0140
0141 address make_address(string_view str)
0142 {
0143 return make_address(static_cast<std::string>(str));
0144 }
0145
0146 address make_address(string_view str,
0147 boost::system::error_code& ec) noexcept
0148 {
0149 return make_address(static_cast<std::string>(str), ec);
0150 }
0151
0152 #endif
0153
0154 boost::asio::ip::address_v4 address::to_v4() const
0155 {
0156 if (type_ != ipv4)
0157 {
0158 bad_address_cast ex;
0159 boost::asio::detail::throw_exception(ex);
0160 }
0161 return ipv4_address_;
0162 }
0163
0164 boost::asio::ip::address_v6 address::to_v6() const
0165 {
0166 if (type_ != ipv6)
0167 {
0168 bad_address_cast ex;
0169 boost::asio::detail::throw_exception(ex);
0170 }
0171 return ipv6_address_;
0172 }
0173
0174 std::string address::to_string() const
0175 {
0176 if (type_ == ipv6)
0177 return ipv6_address_.to_string();
0178 return ipv4_address_.to_string();
0179 }
0180
0181 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0182 std::string address::to_string(boost::system::error_code& ec) const
0183 {
0184 if (type_ == ipv6)
0185 return ipv6_address_.to_string(ec);
0186 return ipv4_address_.to_string(ec);
0187 }
0188 #endif
0189
0190 bool address::is_loopback() const noexcept
0191 {
0192 return (type_ == ipv4)
0193 ? ipv4_address_.is_loopback()
0194 : ipv6_address_.is_loopback();
0195 }
0196
0197 bool address::is_unspecified() const noexcept
0198 {
0199 return (type_ == ipv4)
0200 ? ipv4_address_.is_unspecified()
0201 : ipv6_address_.is_unspecified();
0202 }
0203
0204 bool address::is_multicast() const noexcept
0205 {
0206 return (type_ == ipv4)
0207 ? ipv4_address_.is_multicast()
0208 : ipv6_address_.is_multicast();
0209 }
0210
0211 bool operator==(const address& a1, const address& a2) noexcept
0212 {
0213 if (a1.type_ != a2.type_)
0214 return false;
0215 if (a1.type_ == address::ipv6)
0216 return a1.ipv6_address_ == a2.ipv6_address_;
0217 return a1.ipv4_address_ == a2.ipv4_address_;
0218 }
0219
0220 bool operator<(const address& a1, const address& a2) noexcept
0221 {
0222 if (a1.type_ < a2.type_)
0223 return true;
0224 if (a1.type_ > a2.type_)
0225 return false;
0226 if (a1.type_ == address::ipv6)
0227 return a1.ipv6_address_ < a2.ipv6_address_;
0228 return a1.ipv4_address_ < a2.ipv4_address_;
0229 }
0230
0231 }
0232 }
0233 }
0234
0235 #include <boost/asio/detail/pop_options.hpp>
0236
0237 #endif