| 1 | /// @file |
| 2 | /// @author rfree (current maintainer in monero.cc project) |
| 3 | /// @brief base for connection, contains e.g. the ratelimit hooks |
| 4 | |
| 5 | // Copyright (c) 2014-2022, The Monero Project |
| 6 | // |
| 7 | // All rights reserved. |
| 8 | // |
| 9 | // Redistribution and use in source and binary forms, with or without modification, are |
| 10 | // permitted provided that the following conditions are met: |
| 11 | // |
| 12 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 13 | // conditions and the following disclaimer. |
| 14 | // |
| 15 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 16 | // of conditions and the following disclaimer in the documentation and/or other |
| 17 | // materials provided with the distribution. |
| 18 | // |
| 19 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 20 | // used to endorse or promote products derived from this software without specific |
| 21 | // prior written permission. |
| 22 | // |
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 24 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 26 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 31 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
| 33 | /* rfree: implementation for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */ |
| 34 | |
| 35 | #include "net/connection_basic.hpp" |
| 36 | |
| 37 | #include "net/net_utils_base.h" |
| 38 | #include "misc_log_ex.h" |
| 39 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 40 | #include <boost/thread/thread.hpp> |
| 41 | #include "misc_language.h" |
| 42 | #include <iomanip> |
| 43 | |
| 44 | #include <boost/asio/basic_socket.hpp> |
| 45 | |
| 46 | // TODO: |
| 47 | #include "net/network_throttle-detail.hpp" |
| 48 | |
| 49 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 50 | #define MONERO_DEFAULT_LOG_CATEGORY "net.conn" |
| 51 | |
| 52 | // ################################################################################################ |
| 53 | // local (TU local) headers |
| 54 | // ################################################################################################ |
| 55 | |
| 56 | namespace epee |
| 57 | { |
| 58 | namespace net_utils |
| 59 | { |
| 60 | |
| 61 | namespace |
| 62 | { |
| 63 | boost::asio::ssl::context& get_context(connection_basic_shared_state* state) |
| 64 | { |
| 65 | CHECK_AND_ASSERT_THROW_MES(state != nullptr, "state shared_ptr cannot be null"); |
| 66 | return state->ssl_context; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | std::string to_string(t_connection_type type) |
| 71 | { |
| 72 | if (type == e_connection_type_NET) |
| 73 | return std::string("NET"); |
| 74 | else if (type == e_connection_type_RPC) |
| 75 | return std::string("RPC"); |
| 76 | else if (type == e_connection_type_P2P) |
| 77 | return std::string("P2P"); |
| 78 | |
| 79 | return std::string("UNKNOWN"); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* ============================================================================ */ |
| 84 | |
| 85 | class connection_basic_pimpl { |
| 86 | public: |
| 87 | connection_basic_pimpl(const std::string &name); |
| 88 | |
| 89 | static int m_default_tos; |
| 90 | |
| 91 | network_throttle_bw m_throttle; // per-perr |
| 92 | critical_section m_throttle_lock; |
| 93 | |
| 94 | int m_peer_number; // e.g. for debug/stats |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | } // namespace |
| 99 | } // namespace |
| 100 | |
| 101 | // ################################################################################################ |
| 102 | // The implementation part |
| 103 | // ################################################################################################ |
| 104 | |
| 105 | namespace epee |
| 106 | { |
| 107 | namespace net_utils |
| 108 | { |
| 109 | |
| 110 | // ================================================================================================ |
| 111 | // connection_basic_pimpl |
| 112 | // ================================================================================================ |
| 113 | |
| 114 | connection_basic_pimpl::connection_basic_pimpl(const std::string &name) : m_throttle(name), m_peer_number(0) { } |
| 115 | |
| 116 | // ================================================================================================ |
| 117 | // connection_basic |
| 118 | // ================================================================================================ |
| 119 | |
| 120 | // static variables: |
| 121 | int connection_basic_pimpl::m_default_tos; |
| 122 | |
| 123 | // methods: |
| 124 | connection_basic::connection_basic(boost::asio::io_context &io_context, boost::asio::ip::tcp::socket&& sock, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support) |
| 125 | : |
| 126 | m_state(std::move(state)), |
| 127 | mI( new connection_basic_pimpl("peer") ), |
| 128 | strand_(io_context), |
| 129 | socket_(io_context, get_context(m_state.get())), |
| 130 | m_want_close_connection(false), |
| 131 | m_was_shutdown(false), |
| 132 | m_is_multithreaded(false), |
| 133 | m_ssl_support(ssl_support) |
| 134 | { |
| 135 | // add nullptr checks if removed |
| 136 | assert(m_state != nullptr); // release runtime check in get_context |
| 137 | |
| 138 | socket_.next_layer() = std::move(sock); |
| 139 | |
| 140 | ++(m_state->sock_count); // increase the global counter |
| 141 | mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number |
| 142 | |
| 143 | std::string remote_addr_str = "?"; |
| 144 | try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ; |
| 145 | |
| 146 | _note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count); |
| 147 | } |
| 148 | |
| 149 | connection_basic::connection_basic(boost::asio::io_context &io_context, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support) |
| 150 | : |
| 151 | m_state(std::move(state)), |
| 152 | mI( new connection_basic_pimpl("peer") ), |
| 153 | strand_(io_context), |
| 154 | socket_(io_context, get_context(m_state.get())), |
| 155 | m_want_close_connection(false), |
| 156 | m_was_shutdown(false), |
| 157 | m_is_multithreaded(false), |
| 158 | m_ssl_support(ssl_support) |
| 159 | { |
| 160 | // add nullptr checks if removed |
| 161 | assert(m_state != nullptr); // release runtime check in get_context |
| 162 | |
| 163 | ++(m_state->sock_count); // increase the global counter |
| 164 | mI->m_peer_number = m_state->sock_number.fetch_add(1); // use, and increase the generated number |
| 165 | |
| 166 | std::string remote_addr_str = "?"; |
| 167 | try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ; |
| 168 | |
| 169 | _note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count); |
| 170 | } |
| 171 | |
| 172 | connection_basic::~connection_basic() noexcept(false) { |
| 173 | --(m_state->sock_count); |
| 174 | |
| 175 | std::string remote_addr_str = "?"; |
| 176 | try { boost::system::error_code e; remote_addr_str = socket().remote_endpoint(e).address().to_string(); } catch(...){} ; |
| 177 | _note("Destructing connection #"<<mI->m_peer_number << " to " << remote_addr_str); |
| 178 | } |
| 179 | |
| 180 | void connection_basic::set_rate_up_limit(uint64_t limit) { |
| 181 | { |
| 182 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); |
| 183 | network_throttle_manager::get_global_throttle_out().set_target_speed(limit); |
| 184 | } |
| 185 | save_limit_to_file(limit); |
| 186 | } |
| 187 | |
| 188 | void connection_basic::set_rate_down_limit(uint64_t limit) { |
| 189 | { |
| 190 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_in ); |
| 191 | network_throttle_manager::get_global_throttle_in().set_target_speed(limit); |
| 192 | } |
| 193 | |
| 194 | { |
| 195 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_inreq ); |
| 196 | network_throttle_manager::get_global_throttle_inreq().set_target_speed(limit); |
| 197 | } |
| 198 | save_limit_to_file(limit); |
| 199 | } |
| 200 | |
| 201 | uint64_t connection_basic::get_rate_up_limit() { |
| 202 | uint64_t limit; |
| 203 | { |
| 204 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); |
| 205 | limit = network_throttle_manager::get_global_throttle_out().get_target_speed(); |
| 206 | } |
| 207 | return limit; |
| 208 | } |
| 209 | |
| 210 | uint64_t connection_basic::get_rate_down_limit() { |
| 211 | uint64_t limit; |
| 212 | { |
| 213 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_in ); |
| 214 | limit = network_throttle_manager::get_global_throttle_in().get_target_speed(); |
| 215 | } |
| 216 | return limit; |
| 217 | } |
| 218 | |
| 219 | void connection_basic::save_limit_to_file(int limit) { |
| 220 | } |
| 221 | |
| 222 | void connection_basic::set_tos_flag(int tos) { |
| 223 | connection_basic_pimpl::m_default_tos = tos; |
| 224 | } |
| 225 | |
| 226 | int connection_basic::get_tos_flag() { |
| 227 | return connection_basic_pimpl::m_default_tos; |
| 228 | } |
| 229 | |
| 230 | void connection_basic::sleep_before_packet(size_t packet_size, int phase, int q_len) { |
| 231 | double delay=0; // will be calculated |
| 232 | do |
| 233 | { // rate limiting |
| 234 | if (m_was_shutdown) { |
| 235 | _dbg2("m_was_shutdown - so abort sleep"); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | { |
| 240 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); |
| 241 | delay = network_throttle_manager::get_global_throttle_out().get_sleep_time_after_tick( packet_size ); |
| 242 | } |
| 243 | |
| 244 | delay *= 0.50; |
| 245 | if (delay > 0) { |
| 246 | long int ms = (long int)(delay * 1000); |
| 247 | MTRACE("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep |
| 248 | boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) ); |
| 249 | } |
| 250 | } while(delay > 0); |
| 251 | |
| 252 | // XXX LATER XXX |
| 253 | { |
| 254 | CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); |
| 255 | network_throttle_manager::get_global_throttle_out().handle_trafic_exact( packet_size ); // increase counter - global |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | void connection_basic::do_send_handler_write(const void* ptr , size_t cb ) { |
| 261 | // No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write |
| 262 | MTRACE("handler_write (direct) - before ASIO write, for packet="<<cb<<" B (after sleep)"); |
| 263 | } |
| 264 | |
| 265 | void connection_basic::do_send_handler_write_from_queue( const boost::system::error_code& e, size_t cb, int q_len ) { |
| 266 | // No sleeping here; sleeping is done once and for all in connection<t_protocol_handler>::handle_write |
| 267 | MTRACE("handler_write (after write, from queue="<<q_len<<") - before ASIO write, for packet="<<cb<<" B (after sleep)"); |
| 268 | } |
| 269 | |
| 270 | void connection_basic::logger_handle_net_read(size_t size) { // network data read |
| 271 | } |
| 272 | |
| 273 | void connection_basic::logger_handle_net_write(size_t size) { |
| 274 | } |
| 275 | |
| 276 | double connection_basic::get_sleep_time(size_t cb) { |
| 277 | CRITICAL_REGION_LOCAL(epee::net_utils::network_throttle_manager::network_throttle_manager::m_lock_get_global_throttle_out); |
| 278 | auto t = network_throttle_manager::get_global_throttle_out().get_sleep_time(cb); |
| 279 | return t; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | } // namespace |
| 284 | } // namespace |
| 285 |