35 #include <boost/asio/io_service.hpp>
36 #include <boost/asio/ip/tcp.hpp>
37 #include <boost/thread.hpp>
38 #include <boost/optional/optional_fwd.hpp>
39 #include <boost/program_options/options_description.hpp>
40 #include <boost/program_options/variables_map.hpp>
41 #include <boost/uuid/uuid.hpp>
48 #include "net/abstract_tcp_server2.h"
49 #include "net/levin_protocol_handler.h"
50 #include "net/levin_protocol_handler_async.h"
52 #include "storages/levin_abstract_invoke2.h"
54 #include "math_helper.h"
56 #include "net/enums.h"
68 : max_connections(-1),
70 zone(
epee::net_utils::zone::invalid)
75 epee::net_utils::zone
zone;
81 : max_connections(-1),
95 boost::optional<std::vector<proxy>>
get_proxies(
const boost::program_options::variables_map& vm);
96 boost::optional<std::vector<anonymous_inbound>>
get_anonymous_inbounds(
const boost::program_options::variables_map& vm);
102 boost::optional<boost::asio::ip::tcp::socket>
103 socks_connect_internal(
const std::atomic<bool>& stop_signal, boost::asio::io_service& service,
const boost::asio::ip::tcp::endpoint&
proxy,
const epee::net_utils::network_address& remote);
106 template<
class base_type>
116 template<
class t_payload_net_handler>
117 class node_server:
public epee::levin::levin_commands_handler<p2p_connection_context_t<typename t_payload_net_handler::connection_context> >,
118 public i_p2p_endpoint<typename t_payload_net_handler::connection_context>,
119 public epee::net_utils::i_connection_filter
130 typedef epee::net_utils::boosted_tcp_server<epee::levin::async_protocol_handler<p2p_connection_context>>
net_server;
133 using connect_func = boost::optional<p2p_connection_context>(
network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
147 typedef epee::misc_utils::struct_init<config_t>
config;
152 : m_connect(nullptr),
153 m_net_server(
epee::net_utils::e_connection_type_P2P),
160 m_current_number_of_out_peers(0),
161 m_current_number_of_in_peers(0),
162 m_can_pingback(
false)
164 set_config_defaults();
168 : m_connect(nullptr),
169 m_net_server(public_service,
epee::net_utils::e_connection_type_P2P),
176 m_current_number_of_out_peers(0),
177 m_current_number_of_in_peers(0),
178 m_can_pingback(
false)
180 set_config_defaults();
201 m_config.m_net_config.config_id = 0;
205 m_config.m_support_flags = 0;
213 : m_payload_handler(payload_handler),
216 m_allow_local_ip(
false),
217 m_hide_my_port(
false),
226 static void init_options(boost::program_options::options_description& desc);
229 network_zone& add_zone(epee::net_utils::zone zone);
230 bool init(
const boost::program_options::variables_map& vm);
232 bool send_stop_signal();
234 t_payload_net_handler& get_payload_object();
238 bool log_connections();
241 virtual uint64_t get_public_connections_count();
242 size_t get_public_outgoing_connections_count();
243 size_t get_public_white_peers_count();
244 size_t get_public_gray_peers_count();
245 void get_public_peerlist(std::vector<peerlist_entry>& gray, std::vector<peerlist_entry>& white);
248 void change_max_out_public_peers(
size_t count);
249 void change_max_in_public_peers(
size_t count);
250 virtual bool block_host(
const epee::net_utils::network_address &adress, time_t seconds =
P2P_IP_BLOCKTIME);
251 virtual bool unblock_host(
const epee::net_utils::network_address &address);
252 virtual std::map<std::string, time_t>
get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return m_blocked_hosts; }
254 virtual void add_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
255 virtual void remove_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
256 virtual void clear_used_stripe_peers();
259 const std::vector<std::string> m_seed_nodes_list =
261 "seeds.electroneumseed.com",
262 "seeds.electroneumseed.info",
263 "seeds.electroneumseed.org",
264 "seeds.electroneumseed.net",
267 bool islimitup=
false;
268 bool islimitdown=
false;
277 return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED;
282 #ifdef ALLOW_DEBUG_COMMANDS
284 HANDLE_INVOKE_T2(COMMAND_REQUEST_NETWORK_STATE, &node_server::handle_get_network_state)
285 HANDLE_INVOKE_T2(COMMAND_REQUEST_PEER_ID, &node_server::handle_get_peer_id)
287 HANDLE_INVOKE_T2(COMMAND_REQUEST_SUPPORT_FLAGS, &node_server::handle_get_support_flags)
288 CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler,
typename t_payload_net_handler::connection_context&)
294 int handle_handshake(
int command,
typename COMMAND_HANDSHAKE::request& arg,
typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context&
context);
295 int handle_timed_sync(
int command,
typename COMMAND_TIMED_SYNC::request& arg,
typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context&
context);
297 #ifdef ALLOW_DEBUG_COMMANDS
298 int handle_get_stat_info(
int command,
typename COMMAND_REQUEST_STAT_INFO::request& arg,
typename COMMAND_REQUEST_STAT_INFO::response& rsp, p2p_connection_context&
context);
299 int handle_get_network_state(
int command, COMMAND_REQUEST_NETWORK_STATE::request& arg, COMMAND_REQUEST_NETWORK_STATE::response& rsp, p2p_connection_context&
context);
300 int handle_get_peer_id(
int command, COMMAND_REQUEST_PEER_ID::request& arg, COMMAND_REQUEST_PEER_ID::response& rsp, p2p_connection_context&
context);
302 int handle_get_support_flags(
int command, COMMAND_REQUEST_SUPPORT_FLAGS::request& arg, COMMAND_REQUEST_SUPPORT_FLAGS::response& rsp, p2p_connection_context&
context);
307 bool check_trust(
const proof_of_trust&
tr, epee::net_utils::zone zone_type);
315 virtual bool relay_notify_to_list(
int command,
const epee::span<const uint8_t> data_buff, std::vector<std::pair<epee::net_utils::zone, boost::uuids::uuid>> connections);
316 virtual bool invoke_command_to_peer(
int command,
const epee::span<const uint8_t> req_buff, std::string& resp_buff,
const epee::net_utils::connection_context_base&
context);
317 virtual bool invoke_notify_to_peer(
int command,
const epee::span<const uint8_t> req_buff,
const epee::net_utils::connection_context_base&
context);
318 virtual bool drop_connection(
const epee::net_utils::connection_context_base&
context);
319 virtual void request_callback(
const epee::net_utils::connection_context_base&
context);
320 virtual void for_each_connection(std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
321 virtual bool for_connection(
const boost::uuids::uuid&, std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
322 virtual bool add_host_fail(
const epee::net_utils::network_address &address);
324 virtual bool is_remote_host_allowed(
const epee::net_utils::network_address &address);
326 bool parse_peer_from_string(epee::net_utils::network_address& pe,
const std::string& node_addr, uint16_t default_port = 0);
327 bool handle_command_line(
328 const boost::program_options::variables_map& vm
331 bool handle_remote_peerlist(
const std::vector<peerlist_entry>& peerlist, time_t local_time,
const epee::net_utils::connection_context_base&
context);
336 bool fix_time_delta(std::vector<peerlist_entry>& local_peerlist, time_t local_time, int64_t& delta);
338 bool connections_maker();
339 bool peer_sync_idle_maker();
341 bool do_peer_timed_sync(
const epee::net_utils::connection_context_base&
context,
peerid_type peer_id);
343 bool make_new_connection_from_anchor_peerlist(
const std::vector<anchor_peerlist_entry>& anchor_peerlist);
344 bool make_new_connection_from_peerlist(
network_zone& zone,
bool use_white_list);
345 bool try_to_connect_and_handshake_with_new_peer(
const epee::net_utils::network_address& na,
bool just_take_peerlist =
false, uint64_t last_seen_stamp = 0,
PeerType peer_type = white, uint64_t first_seen_stamp = 0);
346 size_t get_random_index_with_fixed_probability(
size_t max_index);
349 bool is_addr_connected(
const epee::net_utils::network_address& peer);
350 void add_upnp_port_mapping(uint32_t port);
351 void delete_upnp_port_mapping(uint32_t port);
352 template<
class t_callback>
355 bool make_expected_connections_count(
network_zone& zone,
PeerType peer_type,
size_t expected_connections);
357 bool is_addr_recently_failed(
const epee::net_utils::network_address& addr);
358 bool is_priority_node(
const epee::net_utils::network_address& na);
360 bool connect_to_seed();
362 template <
class Container>
363 bool connect_to_peerlist(
const Container& peers);
365 template <
class Container>
366 bool parse_peers_and_add_to_container(
const boost::program_options::variables_map& vm,
const command_line::arg_descriptor<std::vector<std::string> > & arg, Container& container);
368 bool set_max_out_peers(
network_zone& zone, int64_t max);
370 bool set_tos_flag(
const boost::program_options::variables_map& vm,
int limit);
372 bool set_rate_up_limit(
const boost::program_options::variables_map& vm, int64_t limit);
373 bool set_rate_down_limit(
const boost::program_options::variables_map& vm, int64_t limit);
374 bool set_rate_limit(
const boost::program_options::variables_map& vm, int64_t limit);
376 bool has_too_many_connections(
const epee::net_utils::network_address &address);
377 uint64_t get_connections_count();
378 size_t get_incoming_connections_count();
380 size_t get_outgoing_connections_count();
383 bool check_connection_and_handshake_with_peer(
const epee::net_utils::network_address& na, uint64_t last_seen_stamp);
384 bool gray_peerlist_housekeeping();
385 bool check_incoming_connections();
388 _info(
"Killing the net_node");
390 if(mPeersLoggerThread !=
nullptr)
391 mPeersLoggerThread->join();
392 _info(
"Joined extra background net_node threads");
396 std::string print_connections_container();
403 m_save_graph = save_graph;
404 epee::net_utils::connection_basic::set_save_graph(save_graph);
409 m_rpc_port = rpc_port;
439 #ifdef ALLOW_DEBUG_COMMANDS
440 uint64_t m_last_stat_request_time;
451 static boost::optional<p2p_connection_context> public_connect(
network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
452 static boost::optional<p2p_connection_context> socks_connect(
network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:224
#define tr(x)
Definition: common_defines.h:4
#define P2P_DEFAULT_PACKET_MAX_SIZE
Definition: cryptonote_config.h:124
#define P2P_DEFAULT_LIMIT_RATE_DOWN
Definition: cryptonote_config.h:135
#define P2P_IP_BLOCKTIME
Definition: cryptonote_config.h:138
#define P2P_DEFAULT_PEERS_IN_HANDSHAKE
Definition: cryptonote_config.h:125
#define CRYPTONOTE_PRUNING_LOG_STRIPES
Definition: cryptonote_config.h:184
#define P2P_DEFAULT_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:126
#define P2P_DEFAULT_LIMIT_RATE_UP
Definition: cryptonote_config.h:134
#define P2P_DEFAULT_PING_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:128
#define P2P_DEFAULT_HANDSHAKE_INTERVAL
Definition: cryptonote_config.h:123
PUSH_WARNINGS
Definition: hash-ops.h:54
Definition: cryptonote_config.h:190
crypto namespace.
Definition: crypto.cpp:58
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:197
network_type
Definition: cryptonote_config.h:243
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:57
Definition: cryptonote_format_utils.h:44
mdb_size_t count(MDB_cursor *cur)
Definition: value_stream.cpp:39
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
#define false
Definition: stdbool.h:38
Definition: command_line.h:54
Definition: p2p_protocol_defs.h:95
Definition: net_node.h:108
uint32_t support_flags
Definition: net_node.h:112
peerid_type peer_id
Definition: net_node.h:111
p2p_connection_context_t()
Definition: net_node.h:109
bool m_in_timedsync
Definition: net_node.h:113
Definition: p2p_protocol_defs.h:75
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto