| 1 | #include "net/net_helper.h" |
| 2 | |
| 3 | namespace epee |
| 4 | { |
| 5 | namespace net_utils |
| 6 | { |
| 7 | namespace |
| 8 | { |
| 9 | struct new_connection |
| 10 | { |
| 11 | boost::promise<boost::asio::ip::tcp::socket> result_; |
| 12 | boost::asio::ip::tcp::socket socket_; |
| 13 | |
| 14 | template<typename T> |
| 15 | explicit new_connection(T&& executor) |
| 16 | : result_(), socket_(std::forward<T>(executor)) |
| 17 | {} |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | boost::unique_future<boost::asio::ip::tcp::socket> |
| 22 | direct_connect::operator()(const std::string& addr, const std::string& port, boost::asio::steady_timer& timeout) const |
| 23 | { |
| 24 | // Get a list of endpoints corresponding to the server name. |
| 25 | ////////////////////////////////////////////////////////////////////////// |
| 26 | boost::asio::ip::tcp::resolver resolver(MONERO_GET_EXECUTOR(timeout)); |
| 27 | |
| 28 | bool try_ipv6 = false; |
| 29 | boost::asio::ip::tcp::resolver::results_type results{}; |
| 30 | boost::system::error_code resolve_error; |
| 31 | |
| 32 | try |
| 33 | { |
| 34 | results = resolver.resolve( |
| 35 | boost::asio::ip::tcp::v4(), addr, port, boost::asio::ip::tcp::resolver::canonical_name, resolve_error |
| 36 | ); |
| 37 | |
| 38 | if (results.empty()) |
| 39 | { |
| 40 | // if IPv4 resolution fails, try IPv6. Unintentional outgoing IPv6 connections should only |
| 41 | // be possible if for some reason a hostname was given and that hostname fails IPv4 resolution, |
| 42 | // so at least for now there should not be a need for a flag "using ipv6 is ok" |
| 43 | try_ipv6 = true; |
| 44 | } |
| 45 | |
| 46 | } |
| 47 | catch (const boost::system::system_error& e) |
| 48 | { |
| 49 | if (resolve_error != boost::asio::error::host_not_found && |
| 50 | resolve_error != boost::asio::error::host_not_found_try_again) |
| 51 | { |
| 52 | throw; |
| 53 | } |
| 54 | try_ipv6 = true; |
| 55 | } |
| 56 | |
| 57 | if (try_ipv6) |
| 58 | { |
| 59 | results = resolver.resolve( |
| 60 | boost::asio::ip::tcp::v6(), addr, port, boost::asio::ip::tcp::resolver::canonical_name |
| 61 | ); |
| 62 | if (results.empty()) |
| 63 | throw boost::system::system_error{boost::asio::error::fault, "Failed to resolve " + addr}; |
| 64 | } |
| 65 | |
| 66 | ////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | |
| 69 | const auto shared = std::make_shared<new_connection>(MONERO_GET_EXECUTOR(timeout)); |
| 70 | timeout.async_wait([shared] (boost::system::error_code error) |
| 71 | { |
| 72 | if (error != boost::system::errc::operation_canceled && shared && shared->socket_.is_open()) |
| 73 | { |
| 74 | shared->socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both); |
| 75 | shared->socket_.close(); |
| 76 | } |
| 77 | }); |
| 78 | shared->socket_.async_connect(*results.begin(), [shared] (boost::system::error_code error) |
| 79 | { |
| 80 | if (shared) |
| 81 | { |
| 82 | if (error) |
| 83 | shared->result_.set_exception(boost::system::system_error{error}); |
| 84 | else |
| 85 | shared->result_.set_value(std::move(shared->socket_)); |
| 86 | } |
| 87 | }); |
| 88 | return shared->result_.get_future(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |