Chris@16
|
1 //
|
Chris@16
|
2 // local/detail/impl/endpoint.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 // Derived from a public domain implementation written by Daniel Casimiro.
|
Chris@16
|
7 //
|
Chris@16
|
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
|
Chris@16
|
13 #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
|
Chris@16
|
14
|
Chris@16
|
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
16 # pragma once
|
Chris@16
|
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstring>
|
Chris@16
|
24 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
26 #include <boost/asio/error.hpp>
|
Chris@16
|
27 #include <boost/asio/local/detail/endpoint.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace asio {
|
Chris@16
|
33 namespace local {
|
Chris@16
|
34 namespace detail {
|
Chris@16
|
35
|
Chris@16
|
36 endpoint::endpoint()
|
Chris@16
|
37 {
|
Chris@16
|
38 init("", 0);
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 endpoint::endpoint(const char* path_name)
|
Chris@16
|
42 {
|
Chris@16
|
43 using namespace std; // For strlen.
|
Chris@16
|
44 init(path_name, strlen(path_name));
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 endpoint::endpoint(const std::string& path_name)
|
Chris@16
|
48 {
|
Chris@16
|
49 init(path_name.data(), path_name.length());
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 void endpoint::resize(std::size_t new_size)
|
Chris@16
|
53 {
|
Chris@16
|
54 if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
|
Chris@16
|
55 {
|
Chris@16
|
56 boost::system::error_code ec(boost::asio::error::invalid_argument);
|
Chris@16
|
57 boost::asio::detail::throw_error(ec);
|
Chris@16
|
58 }
|
Chris@16
|
59 else if (new_size == 0)
|
Chris@16
|
60 {
|
Chris@16
|
61 path_length_ = 0;
|
Chris@16
|
62 }
|
Chris@16
|
63 else
|
Chris@16
|
64 {
|
Chris@16
|
65 path_length_ = new_size
|
Chris@16
|
66 - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
|
Chris@16
|
67
|
Chris@16
|
68 // The path returned by the operating system may be NUL-terminated.
|
Chris@16
|
69 if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
|
Chris@16
|
70 --path_length_;
|
Chris@16
|
71 }
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 std::string endpoint::path() const
|
Chris@16
|
75 {
|
Chris@16
|
76 return std::string(data_.local.sun_path, path_length_);
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 void endpoint::path(const char* p)
|
Chris@16
|
80 {
|
Chris@16
|
81 using namespace std; // For strlen.
|
Chris@16
|
82 init(p, strlen(p));
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 void endpoint::path(const std::string& p)
|
Chris@16
|
86 {
|
Chris@16
|
87 init(p.data(), p.length());
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 bool operator==(const endpoint& e1, const endpoint& e2)
|
Chris@16
|
91 {
|
Chris@16
|
92 return e1.path() == e2.path();
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 bool operator<(const endpoint& e1, const endpoint& e2)
|
Chris@16
|
96 {
|
Chris@16
|
97 return e1.path() < e2.path();
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 void endpoint::init(const char* path_name, std::size_t path_length)
|
Chris@16
|
101 {
|
Chris@16
|
102 if (path_length > sizeof(data_.local.sun_path) - 1)
|
Chris@16
|
103 {
|
Chris@16
|
104 // The buffer is not large enough to store this address.
|
Chris@16
|
105 boost::system::error_code ec(boost::asio::error::name_too_long);
|
Chris@16
|
106 boost::asio::detail::throw_error(ec);
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 using namespace std; // For memcpy.
|
Chris@16
|
110 data_.local = boost::asio::detail::sockaddr_un_type();
|
Chris@16
|
111 data_.local.sun_family = AF_UNIX;
|
Chris@16
|
112 memcpy(data_.local.sun_path, path_name, path_length);
|
Chris@16
|
113 path_length_ = path_length;
|
Chris@16
|
114
|
Chris@16
|
115 // NUL-terminate normal path names. Names that start with a NUL are in the
|
Chris@16
|
116 // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
|
Chris@16
|
117 if (path_length > 0 && data_.local.sun_path[0] == 0)
|
Chris@16
|
118 data_.local.sun_path[path_length] = 0;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121 } // namespace detail
|
Chris@16
|
122 } // namespace local
|
Chris@16
|
123 } // namespace asio
|
Chris@16
|
124 } // namespace boost
|
Chris@16
|
125
|
Chris@16
|
126 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
127
|
Chris@16
|
128 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
Chris@16
|
129
|
Chris@16
|
130 #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
|