Debian Codegraph @

contrib/epee/include/net/connection_basic.hpp

raw symbols SCIP
1 /// @file
2 /// @author rfree (current maintainer in monero.cc project)
3 /// @brief base for connection, contains e.g. the ratelimit hooks
4
5 // ! This file might contain variable names same as in template class connection<>
6 // ! from files contrib/epee/include/net/abstract_tcp_server2.*
7 // ! I am not a lawyer; afaik APIs, var names etc are not copyrightable ;)
8 // ! (how ever if in some wonderful juristdictions that is not the case, then why not make another sub-class withat that members and licence it as epee part)
9 // ! Working on above premise, IF this is valid in your juristdictions, then consider this code as released as:
10
11 // Copyright (c) 2014-2022, The Monero Project
12 //
13 // All rights reserved.
14 //
15 // Redistribution and use in source and binary forms, with or without modification, are
16 // permitted provided that the following conditions are met:
17 //
18 // 1. Redistributions of source code must retain the above copyright notice, this list of
19 // conditions and the following disclaimer.
20 //
21 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
22 // of conditions and the following disclaimer in the documentation and/or other
23 // materials provided with the distribution.
24 //
25 // 3. Neither the name of the copyright holder nor the names of its contributors may be
26 // used to endorse or promote products derived from this software without specific
27 // prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
30 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
37 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 /* rfree: place for hanlers for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
40
41 #ifndef INCLUDED_p2p_connection_basic_hpp
42 #define INCLUDED_p2p_connection_basic_hpp
43
44
45 #include <string>
46 #include <atomic>
47 #include <memory>
48
49 #include <boost/asio.hpp>
50 #include <boost/asio/ssl.hpp>
51
52 #include "byte_slice.h"
53 #include "net/net_utils_base.h"
54 #include "net/net_ssl.h"
55 #include "syncobj.h"
56
57 namespace epee
58 {
59 namespace net_utils
60 {
61
62 class connection_basic_shared_state
63 {
64 ssl_options_t ssl_options_;
65 public:
66 boost::asio::ssl::context ssl_context;
67 std::atomic<long> sock_count;
68 std::atomic<long> sock_number;
69
70 connection_basic_shared_state()
71 : ssl_options_(ssl_support_t::e_ssl_support_disabled),
72 ssl_context(boost::asio::ssl::context::tlsv12),
73 sock_count(0),
74 sock_number(0)
75 {}
76
77 void configure_ssl(ssl_options_t src)
78 {
79 ssl_options_ = std::move(src);
80 ssl_context = ssl_options_.create_context();
81 }
82
83 const ssl_options_t& ssl_options() const noexcept { return ssl_options_; }
84 };
85
86 /************************************************************************/
87 /* */
88 /************************************************************************/
89 /// Represents a single connection from a client.
90
91 class connection_basic_pimpl; // PIMPL for this class
92
93 enum t_connection_type { // type of the connection (of this server), e.g. so that we will know how to limit it
94 e_connection_type_NET = 0, // default (not used?)
95 e_connection_type_RPC = 1, // the rpc commands (probably not rate limited, not chunked, etc)
96 e_connection_type_P2P = 2 // to other p2p node (probably limited)
97 };
98
99 std::string to_string(t_connection_type type);
100
101 class connection_basic { // not-templated base class for rapid developmet of some code parts
102 // beware of removing const, net_utils::connection is sketchily doing a cast to prevent storing ptr twice
103 const std::shared_ptr<connection_basic_shared_state> m_state;
104 public:
105
106 std::unique_ptr< connection_basic_pimpl > mI; // my Implementation
107
108 // moved here from orginal connecton<> - common member variables that do not depend on template in connection<>
109 std::atomic<bool> m_want_close_connection;
110 std::atomic<bool> m_was_shutdown;
111 critical_section m_send_que_lock;
112 std::deque<byte_slice> m_send_que;
113 volatile bool m_is_multithreaded;
114 /// Strand to ensure the connection's handlers are not called concurrently.
115 boost::asio::io_context::strand strand_;
116 /// Socket for the connection.
117 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
118 ssl_support_t m_ssl_support;
119
120 public:
121 // first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
122 connection_basic(boost::asio::io_context &context, boost::asio::ip::tcp::socket&& sock, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
123 connection_basic(boost::asio::io_context &context, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
124
125 virtual ~connection_basic() noexcept(false);
126
127 //! \return `shared_state` object passed in construction (ptr never changes).
128 connection_basic_shared_state& get_state() noexcept { return *m_state; /* verified in constructor */ }
129
130 boost::asio::ip::tcp::socket& socket() { return socket_.next_layer(); }
131 ssl_support_t get_ssl_support() const { return m_ssl_support; }
132 void disable_ssl() { m_ssl_support = epee::net_utils::ssl_support_t::e_ssl_support_disabled; }
133
134 bool handshake(boost::asio::ssl::stream_base::handshake_type type, boost::asio::const_buffer buffer = {})
135 {
136 //m_state != nullptr verified in constructor
137 return m_state->ssl_options().handshake(strand_.context(), socket_, type, buffer);
138 }
139
140 template<typename MutableBufferSequence, typename ReadHandler>
141 void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
142 {
143 if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
144 socket_.async_read_some(buffers, std::forward<ReadHandler>(handler));
145 else
146 socket().async_read_some(buffers, std::forward<ReadHandler>(handler));
147 }
148
149 template<typename ConstBufferSequence, typename WriteHandler>
150 void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
151 {
152 if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
153 socket_.async_write_some(buffers, std::forward<WriteHandler>(handler));
154 else
155 socket().async_write_some(buffers, std::forward<WriteHandler>(handler));
156 }
157
158 template<typename ConstBufferSequence, typename WriteHandler>
159 void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
160 {
161 if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
162 boost::asio::async_write(socket_, buffers, std::forward<WriteHandler>(handler));
163 else
164 boost::asio::async_write(socket(), buffers, std::forward<WriteHandler>(handler));
165 }
166
167 // various handlers to be called from connection class:
168 void do_send_handler_write(const void * ptr , size_t cb);
169 void do_send_handler_write_from_queue(const boost::system::error_code& e, size_t cb , int q_len); // from handle_write, sending next part
170
171 void logger_handle_net_write(size_t size); // network data written
172 void logger_handle_net_read(size_t size); // network data read
173
174 // config for rate limit
175
176 static void set_rate_up_limit(uint64_t limit);
177 static void set_rate_down_limit(uint64_t limit);
178 static uint64_t get_rate_up_limit();
179 static uint64_t get_rate_down_limit();
180
181 // config misc
182 static void set_tos_flag(int tos); // ToS / QoS flag
183 static int get_tos_flag();
184
185 // handlers and sleep
186 void sleep_before_packet(size_t packet_size, int phase, int q_len); // execute a sleep ; phase is not really used now(?)
187 static void save_limit_to_file(int limit); ///< for dr-monero
188 static double get_sleep_time(size_t cb);
189 };
190
191 } // nameserver
192 } // nameserver
193
194 #endif
195
196