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>
93 std::size_t bytes_transferred
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");
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.");
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.");
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));
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
643 #else // older Boost supports only OpenSSL 1.0, so 1.0-only macros are appropriate 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)
669 void async_read(
char* buff,
size_t sz, boost::asio::detail::transfer_at_least_t transfer_at_least,
handler_obj& hndlr)
681 std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>
m_ssl_socket;
void async_write(const void *data, size_t sz, boost::system::error_code &ec)
Definition: net_helper.h:661
void set_connector(std::function< connect_func > connector)
Change the connection routine (proxy, etc.)
Definition: net_helper.h:250
boost::asio::ssl::context m_ctx
Definition: net_helper.h:680
boost::system::error_code & ref_error
Definition: net_helper.h:89
void check_deadline()
Definition: net_helper.h:605
Definition: net_helper.h:71
uint64_t get_bytes_received() const
Definition: net_helper.h:598
void set_ssl(ssl_options_t ssl_options)
Definition: net_helper.h:143
std::shared_ptr< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > m_ssl_socket
Definition: net_helper.h:681
Definition: portable_binary_archive.hpp:29
Definition: net_helper.h:76
var
Definition: console.py:92
size_t & ref_bytes_transferred
Definition: net_helper.h:90
::std::string string
Definition: gtest-port.h:1097
bool shutdown()
Definition: net_helper.h:563
bool send(const void *data, size_t sz)
Definition: net_helper.h:337
std::string data
Definition: base58.cpp:37
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:108
bool m_initialized
Definition: net_helper.h:684
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
bool is_connected(bool *ssl=NULL)
Definition: net_helper.h:394
bool disconnect()
Definition: net_helper.h:256
Definition: net_helper.h:75
bool connect(const std::string &addr, const std::string &port, std::chrono::milliseconds timeout)
Definition: net_helper.h:209
handler_obj(boost::system::error_code &error, size_t &bytes_transferred)
Definition: net_helper.h:84
void shutdown_ssl()
Definition: net_helper.h:628
const T buffer
Definition: byte_slice.cpp:83
ssl_options_t m_ssl_options
Definition: net_helper.h:683
bool connect(const std::string &addr, int port, std::chrono::milliseconds timeout)
Definition: net_helper.h:153
std::atomic< bool > m_shutdowned
Definition: net_helper.h:687
Represents a single connection from a client.
Definition: abstract_tcp_server2.h:95
unsigned __int64 uint64_t
Definition: stdint.h:136
boost::asio::steady_timer m_deadline
Definition: net_helper.h:686
ssl_support_t support
Definition: net_ssl.h:84
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:105
bool write(const void *data, size_t sz, boost::system::error_code &ec)
Definition: net_helper.h:651
Definition: net_helper.h:82
void operator()(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: net_helper.h:92
bool success
Definition: cold-transaction.cpp:57
boost::endian::big_uint32_t ip
Definition: socks.cpp:62
boost::endian::big_uint16_t port
Definition: socks.cpp:61
blocked_mode_client()
Definition: net_helper.h:103
Definition: net_helper.h:77
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
const T & move(const T &t)
Definition: gtest-port.h:1317
Definition: net_helper.h:64
boost::asio::io_context & get_io_service()
Definition: net_helper.h:583
signed __int64 int64_t
Definition: stdint.h:135
~blocked_mode_client()
Definition: net_helper.h:136
std::function< connect_func > m_connector
Definition: net_helper.h:682
bool m_connected
Definition: net_helper.h:685
try_connect_result_t
Definition: net_helper.h:73
handler_obj(const handler_obj &other_obj)
Definition: net_helper.h:86
try_connect_result_t try_connect(const std::string &addr, const std::string &port, std::chrono::milliseconds timeout)
Definition: net_helper.h:159
bool recv_n(std::string &buff, int64_t sz, std::chrono::milliseconds timeout)
Definition: net_helper.h:490
boost::asio::io_context m_io_service
Definition: net_helper.h:679
uint64_t get_bytes_sent() const
Definition: net_helper.h:593
std::string to_string(t_connection_type type)
Definition: connection_basic.cpp:70
boost::asio::ssl::context create_context() const
Definition: net_ssl.cpp:304
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
bool handshake(boost::asio::io_context &io_context, boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &socket, boost::asio::ssl::stream_base::handshake_type type, boost::asio::const_buffer buffer={}, const std::string &host={}, std::chrono::milliseconds timeout=std::chrono::seconds(15)) const
Definition: net_ssl.cpp:554
error
Tracks LMDB error codes.
Definition: error.h:44
boost::asio::ip::tcp::socket & get_socket()
Definition: net_helper.h:588
std::atomic< uint64_t > m_bytes_received
Definition: net_helper.h:689
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
bool send(const boost::string_ref buff, std::chrono::milliseconds timeout)
Definition: net_helper.h:283
bool recv(std::string &buff, std::chrono::milliseconds timeout)
Definition: net_helper.h:404
std::atomic< uint64_t > m_bytes_sent
Definition: net_helper.h:688
static epee::net_utils::http::http_simple_client_template< dummy_client > client
Definition: http-client.cpp:62