Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:00

0001 //
0002 // local/detail/impl/endpoint.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Derived from a public domain implementation written by Daniel Casimiro.
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
0013 #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 
0021 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
0022 
0023 #include <cstring>
0024 #include <boost/asio/detail/socket_ops.hpp>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/error.hpp>
0027 #include <boost/asio/local/detail/endpoint.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace local {
0034 namespace detail {
0035 
0036 endpoint::endpoint()
0037 {
0038   init("", 0);
0039 }
0040 
0041 endpoint::endpoint(const char* path_name)
0042 {
0043   using namespace std; // For strlen.
0044   init(path_name, strlen(path_name));
0045 }
0046 
0047 endpoint::endpoint(const std::string& path_name)
0048 {
0049   init(path_name.data(), path_name.length());
0050 }
0051 
0052 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
0053 endpoint::endpoint(string_view path_name)
0054 {
0055   init(path_name.data(), path_name.length());
0056 }
0057 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
0058 
0059 void endpoint::resize(std::size_t new_size)
0060 {
0061   if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
0062   {
0063     boost::system::error_code ec(boost::asio::error::invalid_argument);
0064     boost::asio::detail::throw_error(ec);
0065   }
0066   else if (new_size == 0)
0067   {
0068     path_length_ = 0;
0069   }
0070   else
0071   {
0072     path_length_ = new_size
0073       - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
0074 
0075     // The path returned by the operating system may be NUL-terminated.
0076     if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
0077       --path_length_;
0078   }
0079 }
0080 
0081 std::string endpoint::path() const
0082 {
0083   return std::string(data_.local.sun_path, path_length_);
0084 }
0085 
0086 void endpoint::path(const char* p)
0087 {
0088   using namespace std; // For strlen.
0089   init(p, strlen(p));
0090 }
0091 
0092 void endpoint::path(const std::string& p)
0093 {
0094   init(p.data(), p.length());
0095 }
0096 
0097 bool operator==(const endpoint& e1, const endpoint& e2)
0098 {
0099   return e1.path() == e2.path();
0100 }
0101 
0102 bool operator<(const endpoint& e1, const endpoint& e2)
0103 {
0104   return e1.path() < e2.path();
0105 }
0106 
0107 void endpoint::init(const char* path_name, std::size_t path_length)
0108 {
0109   if (path_length > sizeof(data_.local.sun_path) - 1)
0110   {
0111     // The buffer is not large enough to store this address.
0112     boost::system::error_code ec(boost::asio::error::name_too_long);
0113     boost::asio::detail::throw_error(ec);
0114   }
0115 
0116   using namespace std; // For memset and memcpy.
0117   memset(&data_.local, 0, sizeof(boost::asio::detail::sockaddr_un_type));
0118   data_.local.sun_family = AF_UNIX;
0119   if (path_length > 0)
0120     memcpy(data_.local.sun_path, path_name, path_length);
0121   path_length_ = path_length;
0122 }
0123 
0124 } // namespace detail
0125 } // namespace local
0126 } // namespace asio
0127 } // namespace boost
0128 
0129 #include <boost/asio/detail/pop_options.hpp>
0130 
0131 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
0132 
0133 #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP