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