Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
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;
65
if
(!
epee::string_tools::get_xtype_from_string
(port, remote_port))
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
(
73
boost::asio::ip::tcp::socket{
GET_IO_SERVICE
(timeout)},
net::socks::version::v4a
, future_socket{std::move(result)}
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
81
if
(!is_set || !
net::socks::client::connect_and_send
(proxy,
proxy_address
))
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
epee::net_utils::ipv4_network_address
Definition
net_utils_base.h:64
net::socks::client::connect_and_send
static bool connect_and_send(std::shared_ptr< client > self, const stream_type::endpoint &proxy_address)
Definition
socks.cpp:294
epee::string_tools::get_xtype_from_string
PUSH_WARNINGS bool get_xtype_from_string(OUT XType &val, const std::string &str_id)
Definition
string_tools.h:125
epee::string_tools::get_ip_int32_from_string
bool get_ip_int32_from_string(uint32_t &ip, const std::string &ip_str)
Definition
string_tools.cpp:53
net::socks
Definition
fwd.h:40
net::socks::make_connect_client
std::shared_ptr< client > make_connect_client(client::stream_type::socket &&proxy, socks::version ver, Handler handler)
Definition
socks.h:226
net::socks::error
error
Possible errors with socks communication. Defined in https://www.openssh.com/txt/socks4....
Definition
socks.h:66
net::socks::version::v4a
@ v4a
Definition
socks.h:60
net
Definition
net_utils_base.h:54
net::error::invalid_port
@ invalid_port
Outside of 0-65535 range.
Definition
error.h:43
net::error::invalid_host
@ invalid_host
Hostname is not valid.
Definition
error.h:41
net_utils_base.h
GET_IO_SERVICE
#define GET_IO_SERVICE(s)
Definition
net_utils_base.h:50
socks.h
socks_connect.h
error.h
string_tools.h
net::socks::client::async_close
Definition
socks.h:200
net::socks::connector::proxy_address
boost::asio::ip::tcp::endpoint proxy_address
Definition
socks_connect.h:43
net::socks::connector::operator()
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
Definition
socks_connect.cpp:47
src
net
socks_connect.cpp
Generated on
for Electroneum by
1.17.0