Chris@16: // Chris@16: // local/detail/impl/endpoint.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Derived from a public domain implementation written by Daniel Casimiro. Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP Chris@16: #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace local { Chris@16: namespace detail { Chris@16: Chris@16: endpoint::endpoint() Chris@16: { Chris@16: init("", 0); Chris@16: } Chris@16: Chris@16: endpoint::endpoint(const char* path_name) Chris@16: { Chris@16: using namespace std; // For strlen. Chris@16: init(path_name, strlen(path_name)); Chris@16: } Chris@16: Chris@16: endpoint::endpoint(const std::string& path_name) Chris@16: { Chris@16: init(path_name.data(), path_name.length()); Chris@16: } Chris@16: Chris@16: void endpoint::resize(std::size_t new_size) Chris@16: { Chris@16: if (new_size > sizeof(boost::asio::detail::sockaddr_un_type)) Chris@16: { Chris@16: boost::system::error_code ec(boost::asio::error::invalid_argument); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: else if (new_size == 0) Chris@16: { Chris@16: path_length_ = 0; Chris@16: } Chris@16: else Chris@16: { Chris@16: path_length_ = new_size Chris@16: - offsetof(boost::asio::detail::sockaddr_un_type, sun_path); Chris@16: Chris@16: // The path returned by the operating system may be NUL-terminated. Chris@16: if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0) Chris@16: --path_length_; Chris@16: } Chris@16: } Chris@16: Chris@16: std::string endpoint::path() const Chris@16: { Chris@16: return std::string(data_.local.sun_path, path_length_); Chris@16: } Chris@16: Chris@16: void endpoint::path(const char* p) Chris@16: { Chris@16: using namespace std; // For strlen. Chris@16: init(p, strlen(p)); Chris@16: } Chris@16: Chris@16: void endpoint::path(const std::string& p) Chris@16: { Chris@16: init(p.data(), p.length()); Chris@16: } Chris@16: Chris@16: bool operator==(const endpoint& e1, const endpoint& e2) Chris@16: { Chris@16: return e1.path() == e2.path(); Chris@16: } Chris@16: Chris@16: bool operator<(const endpoint& e1, const endpoint& e2) Chris@16: { Chris@16: return e1.path() < e2.path(); Chris@16: } Chris@16: Chris@16: void endpoint::init(const char* path_name, std::size_t path_length) Chris@16: { Chris@16: if (path_length > sizeof(data_.local.sun_path) - 1) Chris@16: { Chris@16: // The buffer is not large enough to store this address. Chris@16: boost::system::error_code ec(boost::asio::error::name_too_long); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: using namespace std; // For memcpy. Chris@16: data_.local = boost::asio::detail::sockaddr_un_type(); Chris@16: data_.local.sun_family = AF_UNIX; Chris@16: memcpy(data_.local.sun_path, path_name, path_length); Chris@16: path_length_ = path_length; Chris@16: Chris@16: // NUL-terminate normal path names. Names that start with a NUL are in the Chris@16: // UNIX domain protocol's "abstract namespace" and are not NUL-terminated. Chris@16: if (path_length > 0 && data_.local.sun_path[0] == 0) Chris@16: data_.local.sun_path[path_length] = 0; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace local Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) Chris@16: Chris@16: #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP