Monero
Loading...
Searching...
No Matches
connection_basic.hpp
Go to the documentation of this file.
1
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
57namespace epee
58{
59namespace net_utils
60{
61
63 {
65 public:
66 boost::asio::ssl::context ssl_context;
67 std::atomic<long> sock_count;
68 std::atomic<long> sock_number;
69
76
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 /************************************************************************/
90
91class 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
101class 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;
112 std::deque<byte_slice> m_send_que;
113 volatile bool m_is_multithreaded;
115 boost::asio::io_context::strand strand_;
117 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
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
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(); }
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 {
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 {
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 {
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);
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);
188 static double get_sleep_time(size_t cb);
189};
190
191} // nameserver
192} // nameserver
193
194#endif
195
196
Definition syncobj.h:82
Definition buffer.h:47
Definition connection_basic.hpp:63
ssl_options_t ssl_options_
Definition connection_basic.hpp:64
connection_basic_shared_state()
Definition connection_basic.hpp:70
std::atomic< long > sock_count
Definition connection_basic.hpp:67
boost::asio::ssl::context ssl_context
Definition connection_basic.hpp:66
void configure_ssl(ssl_options_t src)
Definition connection_basic.hpp:77
std::atomic< long > sock_number
Definition connection_basic.hpp:68
const ssl_options_t & ssl_options() const noexcept
Definition connection_basic.hpp:83
static void set_tos_flag(int tos)
Definition connection_basic.cpp:222
virtual ~connection_basic() noexcept(false)
Definition connection_basic.cpp:172
std::deque< byte_slice > m_send_que
Definition connection_basic.hpp:112
static void set_rate_down_limit(uint64_t limit)
Definition connection_basic.cpp:188
void sleep_before_packet(size_t packet_size, int phase, int q_len)
Definition connection_basic.cpp:230
void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
Definition connection_basic.hpp:141
std::atomic< bool > m_want_close_connection
Definition connection_basic.hpp:109
void do_send_handler_write_from_queue(const boost::system::error_code &e, size_t cb, int q_len)
Definition connection_basic.cpp:265
const std::shared_ptr< connection_basic_shared_state > m_state
Definition connection_basic.hpp:103
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_
Socket for the connection.
Definition connection_basic.hpp:117
static void save_limit_to_file(int limit)
for dr-monero
Definition connection_basic.cpp:219
static int get_tos_flag()
Definition connection_basic.cpp:226
static double get_sleep_time(size_t cb)
Definition connection_basic.cpp:276
boost::asio::io_context::strand strand_
Strand to ensure the connection's handlers are not called concurrently.
Definition connection_basic.hpp:115
static uint64_t get_rate_up_limit()
Definition connection_basic.cpp:201
void logger_handle_net_read(size_t size)
Definition connection_basic.cpp:270
void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
Definition connection_basic.hpp:159
void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
Definition connection_basic.hpp:150
void logger_handle_net_write(size_t size)
Definition connection_basic.cpp:273
boost::asio::ip::tcp::socket & socket()
Definition connection_basic.hpp:130
ssl_support_t m_ssl_support
Definition connection_basic.hpp:118
connection_basic_shared_state & get_state() noexcept
Definition connection_basic.hpp:128
bool handshake(boost::asio::ssl::stream_base::handshake_type type, boost::asio::const_buffer buffer={})
Definition connection_basic.hpp:134
std::atomic< bool > m_was_shutdown
Definition connection_basic.hpp:110
volatile bool m_is_multithreaded
Definition connection_basic.hpp:113
std::unique_ptr< connection_basic_pimpl > mI
Definition connection_basic.hpp:106
critical_section m_send_que_lock
Definition connection_basic.hpp:111
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)
Definition connection_basic.cpp:124
static void set_rate_up_limit(uint64_t limit)
Definition connection_basic.cpp:180
void disable_ssl()
Definition connection_basic.hpp:132
static uint64_t get_rate_down_limit()
Definition connection_basic.cpp:210
ssl_support_t get_ssl_support() const
Definition connection_basic.hpp:131
void do_send_handler_write(const void *ptr, size_t cb)
Definition connection_basic.cpp:260
Definition net_ssl.h:77
#define false
#define const
Definition ipfrdr.c:80
Definition portable_binary_archive.hpp:29
Definition abstract_http_client.h:36
std::string to_string(t_connection_type type)
Definition connection_basic.cpp:70
t_connection_type
Definition connection_basic.hpp:93
@ e_connection_type_NET
Definition connection_basic.hpp:94
@ e_connection_type_RPC
Definition connection_basic.hpp:95
@ e_connection_type_P2P
Definition connection_basic.hpp:96
ssl_support_t
Definition net_ssl.h:49
@ e_ssl_support_disabled
Definition net_ssl.h:50
@ 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
unsigned __int64 uint64_t
Definition stdint.h:136
Definition blake256.h:36