2 // local/detail/impl/endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Derived from a public domain implementation written by Daniel Casimiro.
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
13 #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
24 #include <boost/asio/detail/socket_ops.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/local/detail/endpoint.hpp>
29 #include <boost/asio/detail/push_options.hpp>
41 endpoint::endpoint(const char* path_name)
43 using namespace std; // For strlen.
44 init(path_name, strlen(path_name));
47 endpoint::endpoint(const std::string& path_name)
49 init(path_name.data(), path_name.length());
52 void endpoint::resize(std::size_t new_size)
54 if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
56 boost::system::error_code ec(boost::asio::error::invalid_argument);
57 boost::asio::detail::throw_error(ec);
59 else if (new_size == 0)
65 path_length_ = new_size
66 - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
68 // The path returned by the operating system may be NUL-terminated.
69 if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
74 std::string endpoint::path() const
76 return std::string(data_.local.sun_path, path_length_);
79 void endpoint::path(const char* p)
81 using namespace std; // For strlen.
85 void endpoint::path(const std::string& p)
87 init(p.data(), p.length());
90 bool operator==(const endpoint& e1, const endpoint& e2)
92 return e1.path() == e2.path();
95 bool operator<(const endpoint& e1, const endpoint& e2)
97 return e1.path() < e2.path();
100 void endpoint::init(const char* path_name, std::size_t path_length)
102 if (path_length > sizeof(data_.local.sun_path) - 1)
104 // The buffer is not large enough to store this address.
105 boost::system::error_code ec(boost::asio::error::name_too_long);
106 boost::asio::detail::throw_error(ec);
109 using namespace std; // For memcpy.
110 data_.local = boost::asio::detail::sockaddr_un_type();
111 data_.local.sun_family = AF_UNIX;
112 memcpy(data_.local.sun_path, path_name, path_length);
113 path_length_ = path_length;
115 // NUL-terminate normal path names. Names that start with a NUL are in the
116 // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
117 if (path_length > 0 && data_.local.sun_path[0] == 0)
118 data_.local.sun_path[path_length] = 0;
121 } // namespace detail
126 #include <boost/asio/detail/pop_options.hpp>
128 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
130 #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP