Debian Codegraph @

monero/src/net/socks_connect.cpp

raw symbols SCIP
1 // Copyright (c) 2019-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 #include "socks_connect.h"
30
31 #include <boost/system/error_code.hpp>
32 #include <boost/system/system_error.hpp>
33 #include <cstdint>
34 #include <memory>
35 #include <system_error>
36
37 #include "net/error.h"
38 #include "net/net_utils_base.h"
39 #include "net/socks.h"
40 #include "string_tools.h"
41 #include "string_tools_lexical.h"
42
43 namespace net
44 {
45 namespace socks
46 {
47 boost::unique_future<boost::asio::ip::tcp::socket>
48 connector::operator()(const std::string& remote_host, const std::string& remote_port, boost::asio::steady_timer& timeout) const
49 {
50 struct future_socket
51 {
52 boost::promise<boost::asio::ip::tcp::socket> result_;
53
54 void operator()(boost::system::error_code error, boost::asio::ip::tcp::socket&& socket)
55 {
56 if (error)
57 result_.set_exception(boost::system::system_error{error});
58 else
59 result_.set_value(std::move(socket));
60 }
61 };
62
63 boost::unique_future<boost::asio::ip::tcp::socket> out{};
64 {
65 std::uint16_t port = 0;
66 if (!epee::string_tools::get_xtype_from_string(port, remote_port))
67 throw std::system_error{net::error::invalid_port, "Remote port for socks proxy"};
68
69 bool is_set = false;
70 std::uint32_t ip_address = 0;
71 boost::promise<boost::asio::ip::tcp::socket> result{};
72 out = result.get_future();
73 const auto proxy = net::socks::make_connect_client(
74 boost::asio::ip::tcp::socket{MONERO_GET_EXECUTOR(timeout)}, net::socks::version::v4a, future_socket{std::move(result)}
75 );
76
77 if (epee::string_tools::get_ip_int32_from_string(ip_address, remote_host))
78 is_set = proxy->set_connect_command(epee::net_utils::ipv4_network_address{ip_address, port});
79 else
80 is_set = proxy->set_connect_command(remote_host, port);
81
82 if (!is_set || !net::socks::client::connect_and_send(proxy, proxy_address))
83 throw std::system_error{net::error::invalid_host, "Address for socks proxy"};
84
85 timeout.async_wait(net::socks::client::async_close{std::move(proxy)});
86 }
87
88 return out;
89 }
90 } // socks
91 } // net