Electroneum
socks_connect.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019, 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 
42 namespace net
43 {
44 namespace socks
45 {
46  boost::unique_future<boost::asio::ip::tcp::socket>
47  connector::operator()(const std::string& remote_host, const std::string& remote_port, boost::asio::steady_timer& timeout) const
48  {
49  struct future_socket
50  {
51  boost::promise<boost::asio::ip::tcp::socket> result_;
52 
53  void operator()(boost::system::error_code error, boost::asio::ip::tcp::socket&& socket)
54  {
55  if (error)
56  result_.set_exception(boost::system::system_error{error});
57  else
58  result_.set_value(std::move(socket));
59  }
60  };
61 
62  boost::unique_future<boost::asio::ip::tcp::socket> out{};
63  {
64  std::uint16_t port = 0;
66  throw std::system_error{net::error::invalid_port, "Remote port for socks proxy"};
67 
68  bool is_set = false;
69  std::uint32_t ip_address = 0;
70  boost::promise<boost::asio::ip::tcp::socket> result{};
71  out = result.get_future();
72  const auto proxy = net::socks::make_connect_client(
74  );
75 
76  if (epee::string_tools::get_ip_int32_from_string(ip_address, remote_host))
77  is_set = proxy->set_connect_command(epee::net_utils::ipv4_network_address{ip_address, port});
78  else
79  is_set = proxy->set_connect_command(remote_host, port);
80 
82  throw std::system_error{net::error::invalid_host, "Address for socks proxy"};
83 
84  timeout.async_wait(net::socks::client::async_close{std::move(proxy)});
85  }
86 
87  return out;
88  }
89 } // socks
90 } // net
static bool connect_and_send(std::shared_ptr< client > self, const stream_type::endpoint &proxy_address)
Definition: socks.cpp:294
::std::string string
Definition: gtest-port.h:1097
boost::unique_future< boost::asio::ip::tcp::socket > operator()(const std::string &remote_host, const std::string &remote_port, boost::asio::steady_timer &timeout) const
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
unsigned short uint16_t
Definition: stdint.h:125
std::shared_ptr< client > make_connect_client(client::stream_type::socket &&proxy, socks::version ver, Handler handler)
Definition: socks.h:226
Hostname is not valid.
unsigned int uint32_t
Definition: stdint.h:126
boost::endian::big_uint16_t port
Definition: socks.cpp:60
#define GET_IO_SERVICE(s)
error
Possible errors with socks communication. Defined in https://www.openssh.com/txt/socks4.protocol.
Definition: socks.h:65
const T & move(const T &t)
Definition: gtest-port.h:1317
Outside of 0-65535 range.
PUSH_WARNINGS bool get_xtype_from_string(OUT XType &val, const std::string &str_id)
Definition: string_tools.h:125
bool get_ip_int32_from_string(uint32_t &ip, const std::string &ip_str)
boost::asio::ip::tcp::endpoint proxy_address
Definition: socks_connect.h:43