diff options
author | DongHun Kwak <dh0128.kwak@samsung.com> | 2019-12-05 15:11:01 +0900 |
---|---|---|
committer | DongHun Kwak <dh0128.kwak@samsung.com> | 2019-12-05 15:11:01 +0900 |
commit | 3fdc3e5ee96dca5b11d1694975a65200787eab86 (patch) | |
tree | 5c1733853892b8397d67706fa453a9bd978d2102 /boost/asio/ip/impl | |
parent | 88e602c57797660ebe0f9e15dbd64c1ff16dead3 (diff) | |
download | boost-3fdc3e5ee96dca5b11d1694975a65200787eab86.tar.gz boost-3fdc3e5ee96dca5b11d1694975a65200787eab86.tar.bz2 boost-3fdc3e5ee96dca5b11d1694975a65200787eab86.zip |
Imported Upstream version 1.66.0upstream/1.66.0
Diffstat (limited to 'boost/asio/ip/impl')
-rw-r--r-- | boost/asio/ip/impl/address.hpp | 40 | ||||
-rw-r--r-- | boost/asio/ip/impl/address.ipp | 100 | ||||
-rw-r--r-- | boost/asio/ip/impl/address_v4.hpp | 40 | ||||
-rw-r--r-- | boost/asio/ip/impl/address_v4.ipp | 118 | ||||
-rw-r--r-- | boost/asio/ip/impl/address_v6.hpp | 40 | ||||
-rw-r--r-- | boost/asio/ip/impl/address_v6.ipp | 116 | ||||
-rw-r--r-- | boost/asio/ip/impl/basic_endpoint.hpp | 14 | ||||
-rw-r--r-- | boost/asio/ip/impl/network_v4.hpp | 56 | ||||
-rw-r--r-- | boost/asio/ip/impl/network_v4.ipp | 217 | ||||
-rw-r--r-- | boost/asio/ip/impl/network_v6.hpp | 55 | ||||
-rw-r--r-- | boost/asio/ip/impl/network_v6.ipp | 186 |
11 files changed, 809 insertions, 173 deletions
diff --git a/boost/asio/ip/impl/address.hpp b/boost/asio/ip/impl/address.hpp index e595d7063b..552cb5fd6d 100644 --- a/boost/asio/ip/impl/address.hpp +++ b/boost/asio/ip/impl/address.hpp @@ -25,23 +25,37 @@ namespace boost { namespace asio { namespace ip { +#if !defined(BOOST_ASIO_NO_DEPRECATED) + +inline address address::from_string(const char* str) +{ + return boost::asio::ip::make_address(str); +} + +inline address address::from_string( + const char* str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address(str, ec); +} + +inline address address::from_string(const std::string& str) +{ + return boost::asio::ip::make_address(str); +} + +inline address address::from_string( + const std::string& str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address(str, ec); +} + +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) + template <typename Elem, typename Traits> std::basic_ostream<Elem, Traits>& operator<<( std::basic_ostream<Elem, Traits>& os, const address& addr) { - boost::system::error_code ec; - std::string s = addr.to_string(ec); - if (ec) - { - if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) - boost::asio::detail::throw_error(ec); - else - os.setstate(std::basic_ostream<Elem, Traits>::failbit); - } - else - for (std::string::iterator i = s.begin(); i != s.end(); ++i) - os << os.widen(*i); - return os; + return os << addr.to_string().c_str(); } } // namespace ip diff --git a/boost/asio/ip/impl/address.ipp b/boost/asio/ip/impl/address.ipp index 5eda3e9ecf..6c615592e5 100644 --- a/boost/asio/ip/impl/address.ipp +++ b/boost/asio/ip/impl/address.ipp @@ -21,6 +21,7 @@ #include <boost/asio/detail/throw_exception.hpp> #include <boost/asio/error.hpp> #include <boost/asio/ip/address.hpp> +#include <boost/asio/ip/bad_address_cast.hpp> #include <boost/system/system_error.hpp> #include <boost/asio/detail/push_options.hpp> @@ -100,11 +101,60 @@ address& address::operator=(const boost::asio::ip::address_v6& ipv6_address) return *this; } +address make_address(const char* str) +{ + boost::system::error_code ec; + address addr = make_address(str, ec); + boost::asio::detail::throw_error(ec); + return addr; +} + +address make_address(const char* str, boost::system::error_code& ec) +{ + boost::asio::ip::address_v6 ipv6_address = + boost::asio::ip::make_address_v6(str, ec); + if (!ec) + return address(ipv6_address); + + boost::asio::ip::address_v4 ipv4_address = + boost::asio::ip::make_address_v4(str, ec); + if (!ec) + return address(ipv4_address); + + return address(); +} + +address make_address(const std::string& str) +{ + return make_address(str.c_str()); +} + +address make_address(const std::string& str, + boost::system::error_code& ec) +{ + return make_address(str.c_str(), ec); +} + +#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +address make_address(string_view str) +{ + return make_address(static_cast<std::string>(str)); +} + +address make_address(string_view str, + boost::system::error_code& ec) +{ + return make_address(static_cast<std::string>(str), ec); +} + +#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + boost::asio::ip::address_v4 address::to_v4() const { if (type_ != ipv4) { - std::bad_cast ex; + bad_address_cast ex; boost::asio::detail::throw_exception(ex); } return ipv4_address_; @@ -114,7 +164,7 @@ boost::asio::ip::address_v6 address::to_v6() const { if (type_ != ipv6) { - std::bad_cast ex; + bad_address_cast ex; boost::asio::detail::throw_exception(ex); } return ipv6_address_; @@ -127,56 +177,14 @@ std::string address::to_string() const return ipv4_address_.to_string(); } +#if !defined(BOOST_ASIO_NO_DEPRECATED) std::string address::to_string(boost::system::error_code& ec) const { if (type_ == ipv6) return ipv6_address_.to_string(ec); return ipv4_address_.to_string(ec); } - -address address::from_string(const char* str) -{ - boost::system::error_code ec; - address addr = from_string(str, ec); - boost::asio::detail::throw_error(ec); - return addr; -} - -address address::from_string(const char* str, boost::system::error_code& ec) -{ - boost::asio::ip::address_v6 ipv6_address = - boost::asio::ip::address_v6::from_string(str, ec); - if (!ec) - { - address tmp; - tmp.type_ = ipv6; - tmp.ipv6_address_ = ipv6_address; - return tmp; - } - - boost::asio::ip::address_v4 ipv4_address = - boost::asio::ip::address_v4::from_string(str, ec); - if (!ec) - { - address tmp; - tmp.type_ = ipv4; - tmp.ipv4_address_ = ipv4_address; - return tmp; - } - - return address(); -} - -address address::from_string(const std::string& str) -{ - return from_string(str.c_str()); -} - -address address::from_string(const std::string& str, - boost::system::error_code& ec) -{ - return from_string(str.c_str(), ec); -} +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) bool address::is_loopback() const { diff --git a/boost/asio/ip/impl/address_v4.hpp b/boost/asio/ip/impl/address_v4.hpp index 028b69b676..a273f5e73e 100644 --- a/boost/asio/ip/impl/address_v4.hpp +++ b/boost/asio/ip/impl/address_v4.hpp @@ -25,23 +25,37 @@ namespace boost { namespace asio { namespace ip { +#if !defined(BOOST_ASIO_NO_DEPRECATED) + +inline address_v4 address_v4::from_string(const char* str) +{ + return boost::asio::ip::make_address_v4(str); +} + +inline address_v4 address_v4::from_string( + const char* str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address_v4(str, ec); +} + +inline address_v4 address_v4::from_string(const std::string& str) +{ + return boost::asio::ip::make_address_v4(str); +} + +inline address_v4 address_v4::from_string( + const std::string& str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address_v4(str, ec); +} + +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) + template <typename Elem, typename Traits> std::basic_ostream<Elem, Traits>& operator<<( std::basic_ostream<Elem, Traits>& os, const address_v4& addr) { - boost::system::error_code ec; - std::string s = addr.to_string(ec); - if (ec) - { - if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) - boost::asio::detail::throw_error(ec); - else - os.setstate(std::basic_ostream<Elem, Traits>::failbit); - } - else - for (std::string::iterator i = s.begin(); i != s.end(); ++i) - os << os.widen(*i); - return os; + return os << addr.to_string().c_str(); } } // namespace ip diff --git a/boost/asio/ip/impl/address_v4.ipp b/boost/asio/ip/impl/address_v4.ipp index 440217bcb2..d6db61f80d 100644 --- a/boost/asio/ip/impl/address_v4.ipp +++ b/boost/asio/ip/impl/address_v4.ipp @@ -17,6 +17,7 @@ #include <boost/asio/detail/config.hpp> #include <climits> +#include <limits> #include <stdexcept> #include <boost/asio/error.hpp> #include <boost/asio/detail/socket_ops.hpp> @@ -45,15 +46,13 @@ address_v4::address_v4(const address_v4::bytes_type& bytes) memcpy(&addr_.s_addr, bytes.data(), 4); } -address_v4::address_v4(unsigned long addr) +address_v4::address_v4(address_v4::uint_type addr) { -#if ULONG_MAX > 0xFFFFFFFF - if (addr > 0xFFFFFFFF) + if ((std::numeric_limits<uint_type>::max)() > 0xFFFFFFFF) { - std::out_of_range ex("address_v4 from unsigned long"); + std::out_of_range ex("address_v4 from unsigned integer"); boost::asio::detail::throw_exception(ex); } -#endif // ULONG_MAX > 0xFFFFFFFF addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long( static_cast<boost::asio::detail::u_long_type>(addr)); @@ -71,19 +70,32 @@ address_v4::bytes_type address_v4::to_bytes() const return bytes; } +address_v4::uint_type address_v4::to_uint() const +{ + return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr); +} + +#if !defined(BOOST_ASIO_NO_DEPRECATED) unsigned long address_v4::to_ulong() const { return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr); } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) std::string address_v4::to_string() const { boost::system::error_code ec; - std::string addr = to_string(ec); - boost::asio::detail::throw_error(ec); + char addr_str[boost::asio::detail::max_addr_v4_str_len]; + const char* addr = + boost::asio::detail::socket_ops::inet_ntop( + BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str, + boost::asio::detail::max_addr_v4_str_len, 0, ec); + if (addr == 0) + boost::asio::detail::throw_error(ec); return addr; } +#if !defined(BOOST_ASIO_NO_DEPRECATED) std::string address_v4::to_string(boost::system::error_code& ec) const { char addr_str[boost::asio::detail::max_addr_v4_str_len]; @@ -95,69 +107,44 @@ std::string address_v4::to_string(boost::system::error_code& ec) const return std::string(); return addr; } - -address_v4 address_v4::from_string(const char* str) -{ - boost::system::error_code ec; - address_v4 addr = from_string(str, ec); - boost::asio::detail::throw_error(ec); - return addr; -} - -address_v4 address_v4::from_string( - const char* str, boost::system::error_code& ec) -{ - address_v4 tmp; - if (boost::asio::detail::socket_ops::inet_pton( - BOOST_ASIO_OS_DEF(AF_INET), str, &tmp.addr_, 0, ec) <= 0) - return address_v4(); - return tmp; -} - -address_v4 address_v4::from_string(const std::string& str) -{ - return from_string(str.c_str()); -} - -address_v4 address_v4::from_string( - const std::string& str, boost::system::error_code& ec) -{ - return from_string(str.c_str(), ec); -} +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v4::is_loopback() const { - return (to_ulong() & 0xFF000000) == 0x7F000000; + return (to_uint() & 0xFF000000) == 0x7F000000; } bool address_v4::is_unspecified() const { - return to_ulong() == 0; + return to_uint() == 0; } +#if !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v4::is_class_a() const { - return (to_ulong() & 0x80000000) == 0; + return (to_uint() & 0x80000000) == 0; } bool address_v4::is_class_b() const { - return (to_ulong() & 0xC0000000) == 0x80000000; + return (to_uint() & 0xC0000000) == 0x80000000; } bool address_v4::is_class_c() const { - return (to_ulong() & 0xE0000000) == 0xC0000000; + return (to_uint() & 0xE0000000) == 0xC0000000; } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v4::is_multicast() const { - return (to_ulong() & 0xF0000000) == 0xE0000000; + return (to_uint() & 0xF0000000) == 0xE0000000; } +#if !defined(BOOST_ASIO_NO_DEPRECATED) address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask) { - return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF)); + return address_v4(addr.to_uint() | (mask.to_uint() ^ 0xFFFFFFFF)); } address_v4 address_v4::netmask(const address_v4& addr) @@ -170,6 +157,51 @@ address_v4 address_v4::netmask(const address_v4& addr) return address_v4(0xFFFFFF00); return address_v4(0xFFFFFFFF); } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) + +address_v4 make_address_v4(const char* str) +{ + boost::system::error_code ec; + address_v4 addr = make_address_v4(str, ec); + boost::asio::detail::throw_error(ec); + return addr; +} + +address_v4 make_address_v4( + const char* str, boost::system::error_code& ec) +{ + address_v4::bytes_type bytes; + if (boost::asio::detail::socket_ops::inet_pton( + BOOST_ASIO_OS_DEF(AF_INET), str, &bytes, 0, ec) <= 0) + return address_v4(); + return address_v4(bytes); +} + +address_v4 make_address_v4(const std::string& str) +{ + return make_address_v4(str.c_str()); +} + +address_v4 make_address_v4( + const std::string& str, boost::system::error_code& ec) +{ + return make_address_v4(str.c_str(), ec); +} + +#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +address_v4 make_address_v4(string_view str) +{ + return make_address_v4(static_cast<std::string>(str)); +} + +address_v4 make_address_v4(string_view str, + boost::system::error_code& ec) +{ + return make_address_v4(static_cast<std::string>(str), ec); +} + +#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW) } // namespace ip } // namespace asio diff --git a/boost/asio/ip/impl/address_v6.hpp b/boost/asio/ip/impl/address_v6.hpp index 4b88129c50..e1d9932d30 100644 --- a/boost/asio/ip/impl/address_v6.hpp +++ b/boost/asio/ip/impl/address_v6.hpp @@ -25,23 +25,37 @@ namespace boost { namespace asio { namespace ip { +#if !defined(BOOST_ASIO_NO_DEPRECATED) + +inline address_v6 address_v6::from_string(const char* str) +{ + return boost::asio::ip::make_address_v6(str); +} + +inline address_v6 address_v6::from_string( + const char* str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address_v6(str, ec); +} + +inline address_v6 address_v6::from_string(const std::string& str) +{ + return boost::asio::ip::make_address_v6(str); +} + +inline address_v6 address_v6::from_string( + const std::string& str, boost::system::error_code& ec) +{ + return boost::asio::ip::make_address_v6(str, ec); +} + +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) + template <typename Elem, typename Traits> std::basic_ostream<Elem, Traits>& operator<<( std::basic_ostream<Elem, Traits>& os, const address_v6& addr) { - boost::system::error_code ec; - std::string s = addr.to_string(ec); - if (ec) - { - if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) - boost::asio::detail::throw_error(ec); - else - os.setstate(std::basic_ostream<Elem, Traits>::failbit); - } - else - for (std::string::iterator i = s.begin(); i != s.end(); ++i) - os << os.widen(*i); - return os; + return os << addr.to_string().c_str(); } } // namespace ip diff --git a/boost/asio/ip/impl/address_v6.ipp b/boost/asio/ip/impl/address_v6.ipp index 2d1a1974bf..1741bbcf79 100644 --- a/boost/asio/ip/impl/address_v6.ipp +++ b/boost/asio/ip/impl/address_v6.ipp @@ -24,6 +24,7 @@ #include <boost/asio/detail/throw_exception.hpp> #include <boost/asio/error.hpp> #include <boost/asio/ip/address_v6.hpp> +#include <boost/asio/ip/bad_address_cast.hpp> #include <boost/asio/detail/push_options.hpp> @@ -101,11 +102,17 @@ address_v6::bytes_type address_v6::to_bytes() const std::string address_v6::to_string() const { boost::system::error_code ec; - std::string addr = to_string(ec); - boost::asio::detail::throw_error(ec); + char addr_str[boost::asio::detail::max_addr_v6_str_len]; + const char* addr = + boost::asio::detail::socket_ops::inet_ntop( + BOOST_ASIO_OS_DEF(AF_INET6), &addr_, addr_str, + boost::asio::detail::max_addr_v6_str_len, scope_id_, ec); + if (addr == 0) + boost::asio::detail::throw_error(ec); return addr; } +#if !defined(BOOST_ASIO_NO_DEPRECATED) std::string address_v6::to_string(boost::system::error_code& ec) const { char addr_str[boost::asio::detail::max_addr_v6_str_len]; @@ -118,40 +125,11 @@ std::string address_v6::to_string(boost::system::error_code& ec) const return addr; } -address_v6 address_v6::from_string(const char* str) -{ - boost::system::error_code ec; - address_v6 addr = from_string(str, ec); - boost::asio::detail::throw_error(ec); - return addr; -} - -address_v6 address_v6::from_string( - const char* str, boost::system::error_code& ec) -{ - address_v6 tmp; - if (boost::asio::detail::socket_ops::inet_pton( - BOOST_ASIO_OS_DEF(AF_INET6), str, &tmp.addr_, &tmp.scope_id_, ec) <= 0) - return address_v6(); - return tmp; -} - -address_v6 address_v6::from_string(const std::string& str) -{ - return from_string(str.c_str()); -} - -address_v6 address_v6::from_string( - const std::string& str, boost::system::error_code& ec) -{ - return from_string(str.c_str(), ec); -} - address_v4 address_v6::to_v4() const { if (!is_v4_mapped() && !is_v4_compatible()) { - std::bad_cast ex; + bad_address_cast ex; boost::asio::detail::throw_exception(ex); } @@ -159,6 +137,7 @@ address_v4 address_v6::to_v4() const addr_.s6_addr[13], addr_.s6_addr[14], addr_.s6_addr[15] } }; return address_v4(v4_bytes); } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v6::is_loopback() const { @@ -204,6 +183,7 @@ bool address_v6::is_v4_mapped() const && (addr_.s6_addr[10] == 0xff) && (addr_.s6_addr[11] == 0xff)); } +#if !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v6::is_v4_compatible() const { return ((addr_.s6_addr[0] == 0) && (addr_.s6_addr[1] == 0) @@ -217,6 +197,7 @@ bool address_v6::is_v4_compatible() const && (addr_.s6_addr[14] == 0) && ((addr_.s6_addr[15] == 0) || (addr_.s6_addr[15] == 1)))); } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) bool address_v6::is_multicast() const { @@ -275,6 +256,7 @@ address_v6 address_v6::loopback() return tmp; } +#if !defined(BOOST_ASIO_NO_DEPRECATED) address_v6 address_v6::v4_mapped(const address_v4& addr) { address_v4::bytes_type v4_bytes = addr.to_bytes(); @@ -290,6 +272,76 @@ address_v6 address_v6::v4_compatible(const address_v4& addr) v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } }; return address_v6(v6_bytes); } +#endif // !defined(BOOST_ASIO_NO_DEPRECATED) + +address_v6 make_address_v6(const char* str) +{ + boost::system::error_code ec; + address_v6 addr = make_address_v6(str, ec); + boost::asio::detail::throw_error(ec); + return addr; +} + +address_v6 make_address_v6( + const char* str, boost::system::error_code& ec) +{ + address_v6::bytes_type bytes; + unsigned long scope_id = 0; + if (boost::asio::detail::socket_ops::inet_pton( + BOOST_ASIO_OS_DEF(AF_INET6), str, &bytes[0], &scope_id, ec) <= 0) + return address_v6(); + return address_v6(bytes, scope_id); +} + +address_v6 make_address_v6(const std::string& str) +{ + return make_address_v6(str.c_str()); +} + +address_v6 make_address_v6( + const std::string& str, boost::system::error_code& ec) +{ + return make_address_v6(str.c_str(), ec); +} + +#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +address_v6 make_address_v6(string_view str) +{ + return make_address_v6(static_cast<std::string>(str)); +} + +address_v6 make_address_v6(string_view str, + boost::system::error_code& ec) +{ + return make_address_v6(static_cast<std::string>(str), ec); +} + +#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +address_v4 make_address_v4( + v4_mapped_t, const address_v6& v6_addr) +{ + if (!v6_addr.is_v4_mapped()) + { + bad_address_cast ex; + boost::asio::detail::throw_exception(ex); + } + + address_v6::bytes_type v6_bytes = v6_addr.to_bytes(); + address_v4::bytes_type v4_bytes = { { v6_bytes[12], + v6_bytes[13], v6_bytes[14], v6_bytes[15] } }; + return address_v4(v4_bytes); +} + +address_v6 make_address_v6( + v4_mapped_t, const address_v4& v4_addr) +{ + address_v4::bytes_type v4_bytes = v4_addr.to_bytes(); + address_v6::bytes_type v6_bytes = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0xFF, 0xFF, v4_bytes[0], v4_bytes[1], v4_bytes[2], v4_bytes[3] } }; + return address_v6(v6_bytes); +} } // namespace ip } // namespace asio diff --git a/boost/asio/ip/impl/basic_endpoint.hpp b/boost/asio/ip/impl/basic_endpoint.hpp index 4d9c4505c3..590e9748f3 100644 --- a/boost/asio/ip/impl/basic_endpoint.hpp +++ b/boost/asio/ip/impl/basic_endpoint.hpp @@ -31,19 +31,7 @@ std::basic_ostream<Elem, Traits>& operator<<( const basic_endpoint<InternetProtocol>& endpoint) { boost::asio::ip::detail::endpoint tmp_ep(endpoint.address(), endpoint.port()); - boost::system::error_code ec; - std::string s = tmp_ep.to_string(ec); - if (ec) - { - if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) - boost::asio::detail::throw_error(ec); - else - os.setstate(std::basic_ostream<Elem, Traits>::failbit); - } - else - for (std::string::iterator i = s.begin(); i != s.end(); ++i) - os << os.widen(*i); - return os; + return os << tmp_ep.to_string().c_str(); } } // namespace ip diff --git a/boost/asio/ip/impl/network_v4.hpp b/boost/asio/ip/impl/network_v4.hpp new file mode 100644 index 0000000000..6d9ebaa184 --- /dev/null +++ b/boost/asio/ip/impl/network_v4.hpp @@ -0,0 +1,56 @@ +// +// ip/impl/network_v4.hpp +// ~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP +#define BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) + +#if !defined(BOOST_ASIO_NO_IOSTREAM) + +#include <boost/asio/detail/throw_error.hpp> + +#include <boost/asio/detail/push_options.hpp> + +namespace boost { +namespace asio { +namespace ip { + +template <typename Elem, typename Traits> +std::basic_ostream<Elem, Traits>& operator<<( + std::basic_ostream<Elem, Traits>& os, const network_v4& addr) +{ + boost::system::error_code ec; + std::string s = addr.to_string(ec); + if (ec) + { + if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) + boost::asio::detail::throw_error(ec); + else + os.setstate(std::basic_ostream<Elem, Traits>::failbit); + } + else + for (std::string::iterator i = s.begin(); i != s.end(); ++i) + os << os.widen(*i); + return os; +} + +} // namespace ip +} // namespace asio +} // namespace boost + +#include <boost/asio/detail/pop_options.hpp> + +#endif // !defined(BOOST_ASIO_NO_IOSTREAM) + +#endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP diff --git a/boost/asio/ip/impl/network_v4.ipp b/boost/asio/ip/impl/network_v4.ipp new file mode 100644 index 0000000000..f59949d9f0 --- /dev/null +++ b/boost/asio/ip/impl/network_v4.ipp @@ -0,0 +1,217 @@ +// +// ip/impl/network_v4.ipp +// ~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP +#define BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) + +#include <boost/asio/detail/config.hpp> +#include <climits> +#include <cstdio> +#include <cstdlib> +#include <stdexcept> +#include <boost/asio/error.hpp> +#include <boost/asio/detail/throw_error.hpp> +#include <boost/asio/detail/throw_exception.hpp> +#include <boost/asio/ip/network_v4.hpp> + +#include <boost/asio/detail/push_options.hpp> + +namespace boost { +namespace asio { +namespace ip { + +network_v4::network_v4(const address_v4& addr, unsigned short prefix_len) + : address_(addr), + prefix_length_(prefix_len) +{ + if (prefix_len > 32) + { + std::out_of_range ex("prefix length too large"); + boost::asio::detail::throw_exception(ex); + } +} + +network_v4::network_v4(const address_v4& addr, const address_v4& mask) + : address_(addr), + prefix_length_(0) +{ + address_v4::bytes_type mask_bytes = mask.to_bytes(); + bool finished = false; + for (std::size_t i = 0; i < mask_bytes.size(); ++i) + { + if (finished) + { + if (mask_bytes[i]) + { + std::invalid_argument ex("non-contiguous netmask"); + boost::asio::detail::throw_exception(ex); + } + continue; + } + else + { + switch (mask_bytes[i]) + { + case 255: + prefix_length_ += 8; + break; + case 254: // prefix_length_ += 7 + prefix_length_ += 1; + case 252: // prefix_length_ += 6 + prefix_length_ += 1; + case 248: // prefix_length_ += 5 + prefix_length_ += 1; + case 240: // prefix_length_ += 4 + prefix_length_ += 1; + case 224: // prefix_length_ += 3 + prefix_length_ += 1; + case 192: // prefix_length_ += 2 + prefix_length_ += 1; + case 128: // prefix_length_ += 1 + prefix_length_ += 1; + case 0: // nbits += 0 + finished = true; + break; + default: + std::out_of_range ex("non-contiguous netmask"); + boost::asio::detail::throw_exception(ex); + } + } + } +} + +address_v4 network_v4::netmask() const BOOST_ASIO_NOEXCEPT +{ + uint32_t nmbits = 0xffffffff; + if (prefix_length_ == 0) + nmbits = 0; + else + nmbits = nmbits << (32 - prefix_length_); + return address_v4(nmbits); +} + +address_v4_range network_v4::hosts() const BOOST_ASIO_NOEXCEPT +{ + return is_host() + ? address_v4_range(address_, address_v4(address_.to_uint() + 1)) + : address_v4_range(address_v4(network().to_uint() + 1), broadcast()); +} + +bool network_v4::is_subnet_of(const network_v4& other) const +{ + if (other.prefix_length_ >= prefix_length_) + return false; // Only real subsets are allowed. + const network_v4 me(address_, other.prefix_length_); + return other.canonical() == me.canonical(); +} + +std::string network_v4::to_string() const +{ + boost::system::error_code ec; + std::string addr = to_string(ec); + boost::asio::detail::throw_error(ec); + return addr; +} + +std::string network_v4::to_string(boost::system::error_code& ec) const +{ + ec = boost::system::error_code(); + char prefix_len[16]; +#if defined(BOOST_ASIO_HAS_SECURE_RTL) + sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); +#else // defined(BOOST_ASIO_HAS_SECURE_RTL) + sprintf(prefix_len, "/%u", prefix_length_); +#endif // defined(BOOST_ASIO_HAS_SECURE_RTL) + return address_.to_string() + prefix_len; +} + +network_v4 make_network_v4(const char* str) +{ + return make_network_v4(std::string(str)); +} + +network_v4 make_network_v4(const char* str, boost::system::error_code& ec) +{ + return make_network_v4(std::string(str), ec); +} + +network_v4 make_network_v4(const std::string& str) +{ + boost::system::error_code ec; + network_v4 net = make_network_v4(str, ec); + boost::asio::detail::throw_error(ec); + return net; +} + +network_v4 make_network_v4(const std::string& str, + boost::system::error_code& ec) +{ + std::string::size_type pos = str.find_first_of("/"); + + if (pos == std::string::npos) + { + ec = boost::asio::error::invalid_argument; + return network_v4(); + } + + if (pos == str.size() - 1) + { + ec = boost::asio::error::invalid_argument; + return network_v4(); + } + + std::string::size_type end = str.find_first_not_of("0123456789", pos + 1); + if (end != std::string::npos) + { + ec = boost::asio::error::invalid_argument; + return network_v4(); + } + + const address_v4 addr = make_address_v4(str.substr(0, pos), ec); + if (ec) + return network_v4(); + + const int prefix_len = std::atoi(str.substr(pos + 1).c_str()); + if (prefix_len < 0 || prefix_len > 32) + { + ec = boost::asio::error::invalid_argument; + return network_v4(); + } + + return network_v4(addr, static_cast<unsigned short>(prefix_len)); +} + +#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +network_v4 make_network_v4(string_view str) +{ + return make_network_v4(static_cast<std::string>(str)); +} + +network_v4 make_network_v4(string_view str, + boost::system::error_code& ec) +{ + return make_network_v4(static_cast<std::string>(str), ec); +} + +#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +} // namespace ip +} // namespace asio +} // namespace boost + +#include <boost/asio/detail/pop_options.hpp> + +#endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_IPP diff --git a/boost/asio/ip/impl/network_v6.hpp b/boost/asio/ip/impl/network_v6.hpp new file mode 100644 index 0000000000..c6bac2d2db --- /dev/null +++ b/boost/asio/ip/impl/network_v6.hpp @@ -0,0 +1,55 @@ +// +// ip/impl/network_v6.hpp +// ~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP +#define BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) + +#if !defined(BOOST_ASIO_NO_IOSTREAM) + +#include <boost/asio/detail/throw_error.hpp> + +#include <boost/asio/detail/push_options.hpp> + +namespace boost { +namespace asio { +namespace ip { + +template <typename Elem, typename Traits> +std::basic_ostream<Elem, Traits>& operator<<( + std::basic_ostream<Elem, Traits>& os, const network_v6& addr) +{ + boost::system::error_code ec; + std::string s = addr.to_string(ec); + if (ec) + { + if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit) + boost::asio::detail::throw_error(ec); + else + os.setstate(std::basic_ostream<Elem, Traits>::failbit); + } + else + for (std::string::iterator i = s.begin(); i != s.end(); ++i) + os << os.widen(*i); + return os; +} + +} // namespace ip +} // namespace asio +} // namespace boost + +#include <boost/asio/detail/pop_options.hpp> + +#endif // !defined(BOOST_ASIO_NO_IOSTREAM) + +#endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP diff --git a/boost/asio/ip/impl/network_v6.ipp b/boost/asio/ip/impl/network_v6.ipp new file mode 100644 index 0000000000..2fb28a2fbc --- /dev/null +++ b/boost/asio/ip/impl/network_v6.ipp @@ -0,0 +1,186 @@ +// +// ip/impl/network_v6.ipp +// ~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP +#define BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP + +#if defined(_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) + +#include <boost/asio/detail/config.hpp> +#include <climits> +#include <cstdio> +#include <cstdlib> +#include <stdexcept> +#include <boost/asio/error.hpp> +#include <boost/asio/detail/throw_error.hpp> +#include <boost/asio/detail/throw_exception.hpp> +#include <boost/asio/ip/network_v6.hpp> + +#include <boost/asio/detail/push_options.hpp> + +namespace boost { +namespace asio { +namespace ip { + +network_v6::network_v6(const address_v6& addr, unsigned short prefix_len) + : address_(addr), + prefix_length_(prefix_len) +{ + if (prefix_len > 128) + { + std::out_of_range ex("prefix length too large"); + boost::asio::detail::throw_exception(ex); + } +} + +BOOST_ASIO_DECL address_v6 network_v6::network() const BOOST_ASIO_NOEXCEPT +{ + address_v6::bytes_type bytes(address_.to_bytes()); + for (std::size_t i = 0; i < 16; ++i) + { + if (prefix_length_ <= i * 8) + bytes[i] = 0; + else if (prefix_length_ < (i + 1) * 8) + bytes[i] &= 0xFF00 >> (prefix_length_ % 8); + } + return address_v6(bytes, address_.scope_id()); +} + +address_v6_range network_v6::hosts() const BOOST_ASIO_NOEXCEPT +{ + address_v6::bytes_type begin_bytes(address_.to_bytes()); + address_v6::bytes_type end_bytes(address_.to_bytes()); + for (std::size_t i = 0; i < 16; ++i) + { + if (prefix_length_ <= i * 8) + { + begin_bytes[i] = 0; + end_bytes[i] = 0xFF; + } + else if (prefix_length_ < (i + 1) * 8) + { + begin_bytes[i] &= 0xFF00 >> (prefix_length_ % 8); + end_bytes[i] |= 0xFF >> (prefix_length_ % 8); + } + } + return address_v6_range( + address_v6_iterator(address_v6(begin_bytes, address_.scope_id())), + ++address_v6_iterator(address_v6(end_bytes, address_.scope_id()))); +} + +bool network_v6::is_subnet_of(const network_v6& other) const +{ + if (other.prefix_length_ >= prefix_length_) + return false; // Only real subsets are allowed. + const network_v6 me(address_, other.prefix_length_); + return other.canonical() == me.canonical(); +} + +std::string network_v6::to_string() const +{ + boost::system::error_code ec; + std::string addr = to_string(ec); + boost::asio::detail::throw_error(ec); + return addr; +} + +std::string network_v6::to_string(boost::system::error_code& ec) const +{ + ec = boost::system::error_code(); + char prefix_len[16]; +#if defined(BOOST_ASIO_HAS_SECURE_RTL) + sprintf_s(prefix_len, sizeof(prefix_len), "/%u", prefix_length_); +#else // defined(BOOST_ASIO_HAS_SECURE_RTL) + sprintf(prefix_len, "/%u", prefix_length_); +#endif // defined(BOOST_ASIO_HAS_SECURE_RTL) + return address_.to_string() + prefix_len; +} + +network_v6 make_network_v6(const char* str) +{ + return make_network_v6(std::string(str)); +} + +network_v6 make_network_v6(const char* str, boost::system::error_code& ec) +{ + return make_network_v6(std::string(str), ec); +} + +network_v6 make_network_v6(const std::string& str) +{ + boost::system::error_code ec; + network_v6 net = make_network_v6(str, ec); + boost::asio::detail::throw_error(ec); + return net; +} + +network_v6 make_network_v6(const std::string& str, + boost::system::error_code& ec) +{ + std::string::size_type pos = str.find_first_of("/"); + + if (pos == std::string::npos) + { + ec = boost::asio::error::invalid_argument; + return network_v6(); + } + + if (pos == str.size() - 1) + { + ec = boost::asio::error::invalid_argument; + return network_v6(); + } + + std::string::size_type end = str.find_first_not_of("0123456789", pos + 1); + if (end != std::string::npos) + { + ec = boost::asio::error::invalid_argument; + return network_v6(); + } + + const address_v6 addr = make_address_v6(str.substr(0, pos), ec); + if (ec) + return network_v6(); + + const int prefix_len = std::atoi(str.substr(pos + 1).c_str()); + if (prefix_len < 0 || prefix_len > 128) + { + ec = boost::asio::error::invalid_argument; + return network_v6(); + } + + return network_v6(addr, static_cast<unsigned short>(prefix_len)); +} + +#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +network_v6 make_network_v6(string_view str) +{ + return make_network_v6(static_cast<std::string>(str)); +} + +network_v6 make_network_v6(string_view str, + boost::system::error_code& ec) +{ + return make_network_v6(static_cast<std::string>(str), ec); +} + +#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW) + +} // namespace ip +} // namespace asio +} // namespace boost + +#include <boost/asio/detail/pop_options.hpp> + +#endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_IPP |