36#include <boost/version.hpp>
37#include <boost/asio/io_context.hpp>
38#include <boost/asio/ip/tcp.hpp>
39#include <boost/asio/read.hpp>
40#include <boost/asio/ssl.hpp>
41#include <boost/asio/steady_timer.hpp>
42#include <boost/thread/future.hpp>
43#include <boost/lambda/bind.hpp>
44#include <boost/lambda/lambda.hpp>
45#include <boost/system/error_code.hpp>
46#include <boost/utility/string_ref.hpp>
52#undef MONERO_DEFAULT_LOG_CATEGORY
53#define MONERO_DEFAULT_LOG_CATEGORY "net"
56#define MAKE_IP( a1, a2, a3, a4 ) (a1|(a2<<8)|(a3<<16)|(a4<<24))
66 boost::unique_future<boost::asio::ip::tcp::socket>
67 operator()(
const std::string& addr,
const std::string& port, boost::asio::steady_timer&)
const;
93 std::size_t bytes_transferred
133 using connect_func = boost::unique_future<boost::asio::ip::tcp::socket>(
const std::string&,
const std::string&, boost::asio::steady_timer&);
148 m_ctx = boost::asio::ssl::context(boost::asio::ssl::context::tlsv12);
153 bool connect(
const std::string& addr,
int port, std::chrono::milliseconds timeout)
155 return connect(addr, std::to_string(port), timeout);
177 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
185 boost::system::error_code ignored_ec;
186 m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
193 MWARNING(
"Failed to establish SSL connection");
202 MWARNING(
"Some problems at connect, expected open socket");
209 bool connect(
const std::string& addr,
const std::string& port, std::chrono::milliseconds timeout)
229 MERROR(
"SSL handshake failed on an autodetect connection, reconnecting without SSL");
236 catch(
const boost::system::system_error& er)
238 MDEBUG(
"Some problems at connect, message: " << er.what());
243 MDEBUG(
"Some fatal problems.");
265 m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
268 catch(
const boost::system::system_error& )
283 bool send(
const boost::string_ref buff, std::chrono::milliseconds timeout)
295 boost::system::error_code ec = boost::asio::error::would_block;
304 while (ec == boost::asio::error::would_block)
312 LOG_PRINT_L3(
"Problems at write: " << ec.message());
317 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
322 catch(
const boost::system::system_error& er)
324 LOG_ERROR(
"Some problems at connect, message: " << er.what());
329 LOG_ERROR(
"Some fatal problems.");
363 boost::system::error_code ec;
369 LOG_PRINT_L3(
"Problems at write: " << ec.message());
374 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
379 catch(
const boost::system::system_error& er)
381 LOG_ERROR(
"Some problems at send, message: " << er.what());
387 LOG_ERROR(
"Some fatal problems.");
404 bool recv(std::string& buff, std::chrono::milliseconds timeout)
426 boost::system::error_code ec = boost::asio::error::would_block;
427 size_t bytes_transfered = 0;
431 static const size_t max_size = 16384;
432 buff.resize(max_size);
434 async_read(&buff[0], max_size, boost::asio::transfer_at_least(1), hndlr);
437 while (ec == boost::asio::error::would_block && !
m_shutdowned)
446 MTRACE(
"READ ENDS: Connection err_code " << ec.value());
447 if(ec == boost::asio::error::eof)
449 MTRACE(
"Connection err_code eof.");
455 MDEBUG(
"Problems at read: " << ec.message());
460 MTRACE(
"READ ENDS: Success. bytes_tr: " << bytes_transfered);
461 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
468 buff.resize(bytes_transfered);
472 catch(
const boost::system::system_error& er)
474 LOG_ERROR(
"Some problems at read, message: " << er.what());
480 LOG_ERROR(
"Some fatal problems at read.");
490 inline bool recv_n(std::string& buff,
int64_t sz, std::chrono::milliseconds timeout)
512 buff.resize(
static_cast<size_t>(sz));
513 boost::system::error_code ec = boost::asio::error::would_block;
514 size_t bytes_transfered = 0;
518 async_read((
char*)buff.data(), buff.size(), boost::asio::transfer_at_least(buff.size()), hndlr);
521 while (ec == boost::asio::error::would_block && !
m_shutdowned)
528 LOG_PRINT_L3(
"Problems at read: " << ec.message());
533 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
537 if(bytes_transfered != buff.size())
539 LOG_ERROR(
"Transferred mismatch with transfer_at_least value: m_bytes_transferred=" << bytes_transfered <<
" at_least value=" << buff.size());
546 catch(
const boost::system::system_error& er)
548 LOG_ERROR(
"Some problems at read, message: " << er.what());
554 LOG_ERROR(
"Some fatal problems at read.");
566 boost::system::error_code ec;
571 MDEBUG(
"Problems at cancel: " << ec.message());
572 m_ssl_socket->next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
574 MDEBUG(
"Problems at shutdown: " << ec.message());
577 MDEBUG(
"Problems at close: " << ec.message());
610 if (
m_deadline.expiry() <= std::chrono::steady_clock::now())
615 LOG_PRINT_L3(
"Timed out socket");
621 m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
630 boost::system::error_code ec = boost::asio::error::would_block;
631 m_deadline.expires_after(std::chrono::milliseconds(2000));
632 m_ssl_socket->async_shutdown(boost::lambda::var(ec) = boost::lambda::_1);
633 while (ec == boost::asio::error::would_block)
639 if (ec.category() == boost::asio::error::get_ssl_category() &&
641#
if BOOST_VERSION >= 106200
642 boost::asio::ssl::error::stream_truncated
644 ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)
647 MDEBUG(
"Problems at ssl shutdown: " << ec.message());
651 bool write(
const void*
data,
size_t sz, boost::system::error_code& ec)
664 boost::asio::async_write(*
m_ssl_socket, boost::asio::buffer(
data, sz), boost::lambda::var(ec) = boost::lambda::_1);
666 boost::asio::async_write(
m_ssl_socket->next_layer(), boost::asio::buffer(
data, sz), boost::lambda::var(ec) = boost::lambda::_1);
669 void async_read(
char* buff,
size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least,
handler_obj& hndlr)
672 boost::asio::async_read(
m_ssl_socket->next_layer(), boost::asio::buffer(buff, sz), transfer_at_least, hndlr);
674 boost::asio::async_read(*
m_ssl_socket, boost::asio::buffer(buff, sz), transfer_at_least, hndlr);
681 std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>
m_ssl_socket;
uint64_t get_bytes_received() const
Definition net_helper.h:598
boost::asio::steady_timer m_deadline
Definition net_helper.h:686
boost::unique_future< boost::asio::ip::tcp::socket >(const std::string &, const std::string &, boost::asio::steady_timer &) connect_func
Definition net_helper.h:133
void async_read(char *buff, size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least, handler_obj &hndlr)
Definition net_helper.h:669
uint64_t get_bytes_sent() const
Definition net_helper.h:593
bool shutdown()
Definition net_helper.h:563
bool connect(const std::string &addr, const std::string &port, std::chrono::milliseconds timeout)
Definition net_helper.h:209
bool write(const void *data, size_t sz, boost::system::error_code &ec)
Definition net_helper.h:651
try_connect_result_t try_connect(const std::string &addr, const std::string &port, std::chrono::milliseconds timeout)
Definition net_helper.h:159
void set_connector(std::function< connect_func > connector)
Change the connection routine (proxy, etc.).
Definition net_helper.h:250
bool recv(std::string &buff, std::chrono::milliseconds timeout)
Definition net_helper.h:404
void shutdown_ssl()
Definition net_helper.h:628
bool recv_n(std::string &buff, int64_t sz, std::chrono::milliseconds timeout)
Definition net_helper.h:490
std::atomic< bool > m_shutdowned
Definition net_helper.h:687
void async_write(const void *data, size_t sz, boost::system::error_code &ec)
Definition net_helper.h:661
void check_deadline()
Definition net_helper.h:605
~blocked_mode_client()
Definition net_helper.h:136
bool m_connected
Definition net_helper.h:685
std::atomic< uint64_t > m_bytes_received
Definition net_helper.h:689
try_connect_result_t
Definition net_helper.h:74
@ CONNECT_FAILURE
Definition net_helper.h:76
@ CONNECT_NO_SSL
Definition net_helper.h:77
@ CONNECT_SUCCESS
Definition net_helper.h:75
blocked_mode_client()
Definition net_helper.h:103
boost::asio::io_context & get_io_service()
Definition net_helper.h:583
std::function< connect_func > m_connector
Definition net_helper.h:682
bool send(const void *data, size_t sz)
Definition net_helper.h:337
bool connect(const std::string &addr, int port, std::chrono::milliseconds timeout)
Definition net_helper.h:153
boost::asio::io_context m_io_service
Definition net_helper.h:679
bool disconnect()
Definition net_helper.h:256
boost::asio::ip::tcp::socket & get_socket()
Definition net_helper.h:588
bool is_connected(bool *ssl=NULL)
Definition net_helper.h:394
void set_ssl(ssl_options_t ssl_options)
Definition net_helper.h:143
std::atomic< uint64_t > m_bytes_sent
Definition net_helper.h:688
bool send(const boost::string_ref buff, std::chrono::milliseconds timeout)
Definition net_helper.h:283
boost::asio::ssl::context m_ctx
Definition net_helper.h:680
ssl_options_t m_ssl_options
Definition net_helper.h:683
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition net_helper.h:681
bool m_initialized
Definition net_helper.h:684
Represents a single connection from a client.
Definition abstract_tcp_server2.h:100
boost::asio::ssl::context create_context() const
Definition net_ssl.cpp:304
bool success
Definition cold-transaction.cpp:57
Definition portable_binary_archive.hpp:29
Definition abstract_http_client.h:36
ssl_support_t
Definition net_ssl.h:49
@ e_ssl_support_disabled
Definition net_ssl.h:50
@ e_ssl_support_autodetect
Definition net_ssl.h:52
@ e_ssl_support_enabled
Definition net_ssl.h:51
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136
Definition net_helper.h:83
handler_obj(const handler_obj &other_obj)
Definition net_helper.h:86
void operator()(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition net_helper.h:92
boost::system::error_code & ref_error
Definition net_helper.h:89
handler_obj(boost::system::error_code &error, size_t &bytes_transferred)
Definition net_helper.h:84
size_t & ref_bytes_transferred
Definition net_helper.h:90
Definition net_helper.h:65
boost::unique_future< boost::asio::ip::tcp::socket > operator()(const std::string &addr, const std::string &port, boost::asio::steady_timer &) const
Definition net_helper.cpp:22
std::string data
Definition base58.cpp:37