Debian Codegraph @

src/net/socks.h

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #pragma once
30
31 #include <array>
32 #include <cstdint>
33 #include <boost/asio/io_context.hpp>
34 #include <boost/asio/ip/tcp.hpp>
35 #include <boost/asio/strand.hpp>
36 #include <boost/system/error_code.hpp>
37 #include <boost/type_traits/integral_constant.hpp>
38 #include <boost/utility/string_ref.hpp>
39 #include <memory>
40 #include <utility>
41
42 #include "net/fwd.h"
43 #include "span.h"
44
45 namespace epee
46 {
47 namespace net_utils
48 {
49 class ipv4_network_address;
50 class ipv6_network_address;
51 }
52 }
53
54 namespace net
55 {
56 namespace socks
57 {
58 //! Supported socks variants.
59 enum class version : std::uint8_t
60 {
61 v4 = 0,
62 v4a,
63 v4a_tor, //!< Extensions defined in Tor codebase
64 v5
65 };
66
67 //! Possible errors with socks communication. Defined in https://www.openssh.com/txt/socks4.protocol
68 enum class error : int
69 {
70 // 0 is reserved for success value
71 // v5 errors
72 general_failure = 1,
73 not_allowed,
74 network_unreachable,
75 host_unreachable,
76 connection_refused,
77 ttl_expired,
78 command_not_supported,
79 address_type_not_supported,
80 // v4 errors
81 rejected = 92,
82 identd_connection,
83 identd_user,
84 // Specific to application
85 auth_failure = 257,
86 bad_read,
87 bad_write,
88 unexpected_version
89 };
90
91 /* boost::system::error_code is extended for easier compatibility with
92 boost::asio errors. If std::error_code is needed (with expect<T> for
93 instance), then upgrade to boost 1.65+ or use conversion code in
94 develop branch at boost/system/detail/std_interoperability.hpp */
95
96 //! \return boost::system::error_category for net::socks namespace
97 const boost::system::error_category& error_category() noexcept;
98
99 //! \return net::socks::error as a boost::system::error_code.
100 inline boost::system::error_code make_error_code(error value) noexcept
101 {
102 return boost::system::error_code{int(value), socks::error_category()};
103 }
104
105 //! Client support for socks connect and resolve commands.
106 class client
107 {
108 boost::asio::ip::tcp::socket proxy_;
109 boost::asio::strand<boost::asio::ip::tcp::socket::executor_type> strand_;
110 std::array<std::uint16_t, 3> buffer_size_;
111 std::uint8_t buffer_[1024];
112 socks::version ver_;
113
114 /*!
115 Only invoked after `*send(...)` function completes or fails.
116 `bool(error) == false` indicates success; `self.get()` is always
117 `this` and allows implementations to skip
118 `std::enable_shared_from_this<T>` (ASIO callbacks need shared_ptr).
119 The design saves space and reduces cycles (everything uses moves,
120 so no atomic operations are ever necessary).
121
122 \param error when processing last command (if any).
123 \param self `shared_ptr<client>` handle to `this`.
124 */
125 virtual void done(boost::system::error_code error, const std::shared_ptr<client>& self) = 0;
126
127 public:
128 using stream_type = boost::asio::ip::tcp;
129
130 // defined in cpp
131 struct write;
132 struct read;
133 struct completed;
134 struct process_v5;
135
136 /*!
137 \param proxy ownership is passed into `this`. Does not have to be
138 in connected state.
139 \param ver socks version for the connection.
140 */
141 explicit client(stream_type::socket&& proxy, socks::version ver);
142
143 client(const client&) = delete;
144 virtual ~client();
145 client& operator=(const client&) = delete;
146
147 //! \return Ownership of socks client socket object.
148 stream_type::socket take_socket()
149 {
150 return stream_type::socket{std::move(proxy_)};
151 }
152
153 //! \return Socks version.
154 socks::version socks_version() const noexcept { return ver_; }
155
156 //! \return Contents of first internal buffer
157 epee::span<const std::uint8_t> buffer() const noexcept
158 {
159 return {buffer_, std::get<0>(buffer_size_)};
160 }
161
162 //! \post `buffer_[0] = 0, buffer_[1] = 0`.
163 void clear_command() noexcept { buffer_size_ = {}; }
164
165 //! Try to set `address` as remote connection request.
166 bool set_connect_command(
167 const epee::net_utils::ipv4_network_address& address,
168 const user_and_pass* userinfo = nullptr);
169
170 //! Try to set `address` as remote connection request.
171 bool set_connect_command(
172 const epee::net_utils::ipv6_network_address& address,
173 const user_and_pass* userinfo = nullptr);
174
175 //! Try to set `domain` + `port` as remote connection request.
176 bool set_connect_command(
177 boost::string_ref domain,
178 std::uint16_t port,
179 const user_and_pass* userinfo = nullptr);
180
181 //! Try to set `address` as remote Tor hidden service connection request.
182 bool set_connect_command(
183 const net::tor_address& address,
184 const user_and_pass* userinfo = nullptr);
185
186 //! Try to set `address` as remote i2p hidden service connection request.
187 bool set_connect_command(
188 const net::i2p_address& address,
189 const user_and_pass* userinfo = nullptr);
190
191 //! Try to set `domain` as remote DNS A record lookup request.
192 bool set_resolve_command(boost::string_ref domain);
193
194 /*!
195 Asynchronously connect to `proxy_address` then issue command(s) in
196 `buffer_`. The `done(...)` method will be invoked upon completion
197 with `self` and potential `error`s.
198
199 \note Must use one of the `self->set_*_command` calls before using
200 this function.
201 \note Only `async_close` can be invoked on `self` until the `done`
202 callback is invoked.
203
204 \param self ownership of object is given to function.
205 \param proxy_address of the socks server.
206 \return False if `self->buffer().empty()` (no command set).
207 */
208 static bool connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address);
209
210 /*!
211 Assume existing connection to proxy server; asynchronously issue
212 command in `buffer_`. The `done(...)` method will be invoked
213 upon completion with `self` and potential `error`s.
214
215 \note Must use one of the `self->set_*_command` calls before using
216 the function.
217 \note Only `async_close` can be invoked on `self` until the `done`
218 callback is invoked.
219
220 \param self ownership of object is given to function.
221 \return False if `self->buffer().empty()` (no command set).
222 */
223 static bool send(std::shared_ptr<client> self);
224
225 /*! Callback for closing socket. Thread-safe with `*send` functions;
226 never blocks (uses strands). */
227 struct async_close
228 {
229 std::shared_ptr<client> self_;
230 void operator()(boost::system::error_code error = boost::system::error_code{});
231 };
232
233 //! Calls `async_close` on `self` at destruction. NOP if `nullptr`.
234 struct close_on_exit
235 {
236 std::shared_ptr<client> self;
237 ~close_on_exit() { async_close{std::move(self)}(); }
238 };
239 };
240
241 template<typename Handler>
242 class connect_client : public client
243 {
244 Handler handler_;
245
246 virtual void done(boost::system::error_code error, const std::shared_ptr<client>&) override
247 {
248 handler_(error, take_socket());
249 }
250
251 public:
252 explicit connect_client(stream_type::socket&& proxy, socks::version ver, Handler&& handler)
253 : client(std::move(proxy), ver), handler_(std::move(handler))
254 {}
255
256 virtual ~connect_client() override {}
257 };
258
259 template<typename Handler>
260 inline std::shared_ptr<client>
261 make_connect_client(client::stream_type::socket&& proxy, socks::version ver, Handler handler)
262 {
263 return std::make_shared<connect_client<Handler>>(std::move(proxy), ver, std::move(handler));
264 }
265 } // socks
266 } // net
267
268 namespace boost
269 {
270 namespace system
271 {
272 template<>
273 struct is_error_code_enum<net::socks::error>
274 : true_type
275 {};
276 } // system
277 } // boost