Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // ip/detail/impl/endpoint.ipp
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_DETAIL_IMPL_ENDPOINT_IPP
0012 #define BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP
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 <cstring>
0020 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0021 # include <sstream>
0022 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0023 #include <boost/asio/detail/socket_ops.hpp>
0024 #include <boost/asio/detail/throw_error.hpp>
0025 #include <boost/asio/error.hpp>
0026 #include <boost/asio/ip/detail/endpoint.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace ip {
0033 namespace detail {
0034 
0035 endpoint::endpoint() noexcept
0036   : data_()
0037 {
0038   data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
0039   data_.v4.sin_port = 0;
0040   data_.v4.sin_addr.s_addr = BOOST_ASIO_OS_DEF(INADDR_ANY);
0041 }
0042 
0043 endpoint::endpoint(int family, unsigned short port_num) noexcept
0044   : data_()
0045 {
0046   using namespace std; // For memcpy.
0047   if (family == BOOST_ASIO_OS_DEF(AF_INET))
0048   {
0049     data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
0050     data_.v4.sin_port =
0051       boost::asio::detail::socket_ops::host_to_network_short(port_num);
0052     data_.v4.sin_addr.s_addr = BOOST_ASIO_OS_DEF(INADDR_ANY);
0053   }
0054   else
0055   {
0056     data_.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
0057     data_.v6.sin6_port =
0058       boost::asio::detail::socket_ops::host_to_network_short(port_num);
0059     data_.v6.sin6_flowinfo = 0;
0060     data_.v6.sin6_addr.s6_addr[0] = 0; data_.v6.sin6_addr.s6_addr[1] = 0;
0061     data_.v6.sin6_addr.s6_addr[2] = 0; data_.v6.sin6_addr.s6_addr[3] = 0;
0062     data_.v6.sin6_addr.s6_addr[4] = 0; data_.v6.sin6_addr.s6_addr[5] = 0;
0063     data_.v6.sin6_addr.s6_addr[6] = 0; data_.v6.sin6_addr.s6_addr[7] = 0;
0064     data_.v6.sin6_addr.s6_addr[8] = 0; data_.v6.sin6_addr.s6_addr[9] = 0;
0065     data_.v6.sin6_addr.s6_addr[10] = 0; data_.v6.sin6_addr.s6_addr[11] = 0;
0066     data_.v6.sin6_addr.s6_addr[12] = 0; data_.v6.sin6_addr.s6_addr[13] = 0;
0067     data_.v6.sin6_addr.s6_addr[14] = 0; data_.v6.sin6_addr.s6_addr[15] = 0;
0068     data_.v6.sin6_scope_id = 0;
0069   }
0070 }
0071 
0072 endpoint::endpoint(const boost::asio::ip::address& addr,
0073     unsigned short port_num) noexcept
0074   : data_()
0075 {
0076   using namespace std; // For memcpy.
0077   if (addr.is_v4())
0078   {
0079     data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
0080     data_.v4.sin_port =
0081       boost::asio::detail::socket_ops::host_to_network_short(port_num);
0082     data_.v4.sin_addr.s_addr =
0083       boost::asio::detail::socket_ops::host_to_network_long(
0084         addr.to_v4().to_uint());
0085   }
0086   else
0087   {
0088     data_.v6.sin6_family = BOOST_ASIO_OS_DEF(AF_INET6);
0089     data_.v6.sin6_port =
0090       boost::asio::detail::socket_ops::host_to_network_short(port_num);
0091     data_.v6.sin6_flowinfo = 0;
0092     boost::asio::ip::address_v6 v6_addr = addr.to_v6();
0093     boost::asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
0094     memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
0095     data_.v6.sin6_scope_id =
0096       static_cast<boost::asio::detail::u_long_type>(
0097         v6_addr.scope_id());
0098   }
0099 }
0100 
0101 void endpoint::resize(std::size_t new_size)
0102 {
0103   if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
0104   {
0105     boost::system::error_code ec(boost::asio::error::invalid_argument);
0106     boost::asio::detail::throw_error(ec);
0107   }
0108 }
0109 
0110 unsigned short endpoint::port() const noexcept
0111 {
0112   if (is_v4())
0113   {
0114     return boost::asio::detail::socket_ops::network_to_host_short(
0115         data_.v4.sin_port);
0116   }
0117   else
0118   {
0119     return boost::asio::detail::socket_ops::network_to_host_short(
0120         data_.v6.sin6_port);
0121   }
0122 }
0123 
0124 void endpoint::port(unsigned short port_num) noexcept
0125 {
0126   if (is_v4())
0127   {
0128     data_.v4.sin_port
0129       = boost::asio::detail::socket_ops::host_to_network_short(port_num);
0130   }
0131   else
0132   {
0133     data_.v6.sin6_port
0134       = boost::asio::detail::socket_ops::host_to_network_short(port_num);
0135   }
0136 }
0137 
0138 boost::asio::ip::address endpoint::address() const noexcept
0139 {
0140   using namespace std; // For memcpy.
0141   if (is_v4())
0142   {
0143     return boost::asio::ip::address_v4(
0144         boost::asio::detail::socket_ops::network_to_host_long(
0145           data_.v4.sin_addr.s_addr));
0146   }
0147   else
0148   {
0149     boost::asio::ip::address_v6::bytes_type bytes;
0150     memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
0151     return boost::asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
0152   }
0153 }
0154 
0155 void endpoint::address(const boost::asio::ip::address& addr) noexcept
0156 {
0157   endpoint tmp_endpoint(addr, port());
0158   data_ = tmp_endpoint.data_;
0159 }
0160 
0161 bool operator==(const endpoint& e1, const endpoint& e2) noexcept
0162 {
0163   return e1.address() == e2.address() && e1.port() == e2.port();
0164 }
0165 
0166 bool operator<(const endpoint& e1, const endpoint& e2) noexcept
0167 {
0168   if (e1.address() < e2.address())
0169     return true;
0170   if (e1.address() != e2.address())
0171     return false;
0172   return e1.port() < e2.port();
0173 }
0174 
0175 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0176 std::string endpoint::to_string() const
0177 {
0178   std::ostringstream tmp_os;
0179   tmp_os.imbue(std::locale::classic());
0180   if (is_v4())
0181     tmp_os << address();
0182   else
0183     tmp_os << '[' << address() << ']';
0184   tmp_os << ':' << port();
0185 
0186   return tmp_os.str();
0187 }
0188 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0189 
0190 } // namespace detail
0191 } // namespace ip
0192 } // namespace asio
0193 } // namespace boost
0194 
0195 #include <boost/asio/detail/pop_options.hpp>
0196 
0197 #endif // BOOST_ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP