Go to the documentation of this file.
34 #include <boost/asio/io_service.hpp>
35 #include <boost/asio/ip/tcp.hpp>
36 #include <boost/thread.hpp>
37 #include <boost/optional/optional_fwd.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 #include <boost/uuid/uuid.hpp>
50 #include "net/abstract_tcp_server2.h"
51 #include "net/levin_protocol_handler.h"
52 #include "net/levin_protocol_handler_async.h"
54 #include "storages/levin_abstract_invoke2.h"
56 #include "math_helper.h"
58 #include "net/enums.h"
70 : max_connections(-1),
72 zone(
epee::net_utils::zone::invalid),
78 epee::net_utils::zone
zone;
85 : max_connections(-1),
99 boost::optional<std::vector<proxy>>
get_proxies(
const boost::program_options::variables_map& vm);
100 boost::optional<std::vector<anonymous_inbound>>
get_anonymous_inbounds(
const boost::program_options::variables_map& vm);
106 boost::optional<boost::asio::ip::tcp::socket>
107 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);
110 template<
class base_type>
115 flush_time(
std::chrono::steady_clock::time_point::max()),
118 m_in_timedsync(
false)
129 template<
class t_payload_net_handler>
130 class node_server:
public epee::levin::levin_commands_handler<p2p_connection_context_t<typename t_payload_net_handler::connection_context> >,
131 public i_p2p_endpoint<typename t_payload_net_handler::connection_context>,
132 public epee::net_utils::i_connection_filter
143 typedef epee::net_utils::boosted_tcp_server<epee::levin::async_protocol_handler<p2p_connection_context>>
net_server;
146 using connect_func = boost::optional<p2p_connection_context>(network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
160 typedef epee::misc_utils::struct_init<config_t>
config;
165 : m_connect(nullptr),
166 m_net_server(
epee::net_utils::e_connection_type_P2P),
168 m_bind_ipv6_address(),
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();
184 : m_connect(nullptr),
185 m_net_server(public_service,
epee::net_utils::e_connection_type_P2P),
187 m_bind_ipv6_address(),
195 m_current_number_of_out_peers(0),
196 m_current_number_of_in_peers(0),
197 m_can_pingback(
false)
199 set_config_defaults();
223 m_config.m_net_config.config_id = 0;
227 m_config.m_support_flags = 0;
242 : m_payload_handler(payload_handler),
245 m_rpc_credits_per_hash(0),
246 m_allow_local_ip(
false),
247 m_hide_my_port(
false),
255 static void init_options(boost::program_options::options_description& desc);
258 network_zone& add_zone(epee::net_utils::zone zone);
259 bool init(
const boost::program_options::variables_map& vm);
261 bool send_stop_signal();
263 t_payload_net_handler& get_payload_object();
267 bool log_connections();
270 virtual uint64_t get_public_connections_count();
271 size_t get_public_outgoing_connections_count();
272 size_t get_public_white_peers_count();
273 size_t get_public_gray_peers_count();
274 void get_public_peerlist(std::vector<peerlist_entry>& gray, std::vector<peerlist_entry>& white);
275 void get_peerlist(std::vector<peerlist_entry>& gray, std::vector<peerlist_entry>& white);
277 void change_max_out_public_peers(
size_t count);
278 uint32_t get_max_out_public_peers()
const;
279 void change_max_in_public_peers(
size_t count);
280 uint32_t get_max_in_public_peers()
const;
281 virtual bool block_host(
const epee::net_utils::network_address &adress, time_t seconds =
P2P_IP_BLOCKTIME);
282 virtual bool unblock_host(
const epee::net_utils::network_address &address);
283 virtual bool block_subnet(
const epee::net_utils::ipv4_network_subnet &subnet, time_t seconds =
P2P_IP_BLOCKTIME);
284 virtual bool unblock_subnet(
const epee::net_utils::ipv4_network_subnet &subnet);
285 virtual bool is_host_blocked(
const epee::net_utils::network_address &address, time_t *seconds) { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return !is_remote_host_allowed(address, seconds); }
286 virtual std::map<std::string, time_t>
get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return m_blocked_hosts; }
287 virtual std::map<epee::net_utils::ipv4_network_subnet, time_t>
get_blocked_subnets() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return m_blocked_subnets; }
289 virtual void add_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
290 virtual void remove_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
291 virtual void clear_used_stripe_peers();
294 const std::vector<std::string> m_seed_nodes_list =
295 {
"seeds.moneroseeds.se"
296 ,
"seeds.moneroseeds.ae.org"
297 ,
"seeds.moneroseeds.ch"
298 ,
"seeds.moneroseeds.li"
301 bool islimitup=
false;
302 bool islimitdown=
false;
309 return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED;
315 CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler, typename t_payload_net_handler::connection_context&)
321 int handle_handshake(
int command,
typename COMMAND_HANDSHAKE::request& arg,
typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context&
context);
322 int handle_timed_sync(
int command,
typename COMMAND_TIMED_SYNC::request& arg,
typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context&
context);
336 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);
338 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);
339 virtual bool invoke_notify_to_peer(
int command,
const epee::span<const uint8_t> req_buff,
const epee::net_utils::connection_context_base&
context);
340 virtual bool drop_connection(
const epee::net_utils::connection_context_base&
context);
341 virtual void request_callback(
const epee::net_utils::connection_context_base&
context);
342 virtual void for_each_connection(std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
343 virtual bool for_connection(
const boost::uuids::uuid&, std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
344 virtual bool add_host_fail(
const epee::net_utils::network_address &address,
unsigned int score = 1);
346 virtual bool is_remote_host_allowed(
const epee::net_utils::network_address &address, time_t *t = NULL);
348 bool parse_peer_from_string(epee::net_utils::network_address& pe,
const std::string& node_addr, uint16_t default_port = 0);
349 bool handle_command_line(
350 const boost::program_options::variables_map& vm
353 bool handle_remote_peerlist(
const std::vector<peerlist_entry>& peerlist,
const epee::net_utils::connection_context_base&
context);
354 bool get_local_node_data(
basic_node_data& node_data,
const network_zone& zone);
357 bool sanitize_peerlist(std::vector<peerlist_entry>& local_peerlist);
359 bool connections_maker();
360 bool peer_sync_idle_maker();
362 bool do_peer_timed_sync(
const epee::net_utils::connection_context_base&
context,
peerid_type peer_id);
364 bool make_new_connection_from_anchor_peerlist(
const std::vector<anchor_peerlist_entry>& anchor_peerlist);
365 bool make_new_connection_from_peerlist(network_zone& zone,
bool use_white_list);
366 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);
367 size_t get_random_index_with_fixed_probability(
size_t max_index);
370 bool is_addr_connected(
const epee::net_utils::network_address& peer);
371 void add_upnp_port_mapping_impl(uint32_t port,
bool ipv6=
false);
372 void add_upnp_port_mapping_v4(uint32_t port);
373 void add_upnp_port_mapping_v6(uint32_t port);
374 void add_upnp_port_mapping(uint32_t port,
bool ipv4=
true,
bool ipv6=
false);
375 void delete_upnp_port_mapping_impl(uint32_t port,
bool ipv6=
false);
376 void delete_upnp_port_mapping_v4(uint32_t port);
377 void delete_upnp_port_mapping_v6(uint32_t port);
378 void delete_upnp_port_mapping(uint32_t port);
379 template<
class t_callback>
382 bool make_expected_connections_count(network_zone& zone,
PeerType peer_type,
size_t expected_connections);
383 void record_addr_failed(
const epee::net_utils::network_address& addr);
384 bool is_addr_recently_failed(
const epee::net_utils::network_address& addr);
385 bool is_priority_node(
const epee::net_utils::network_address& na);
387 std::set<std::string> get_seed_nodes();
388 bool connect_to_seed();
390 template <
class Container>
391 bool connect_to_peerlist(
const Container& peers);
393 template <
class Container>
394 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);
396 bool set_max_out_peers(network_zone& zone, int64_t max);
397 bool set_max_in_peers(network_zone& zone, int64_t max);
398 bool set_tos_flag(
const boost::program_options::variables_map& vm,
int limit);
400 bool set_rate_up_limit(
const boost::program_options::variables_map& vm, int64_t limit);
401 bool set_rate_down_limit(
const boost::program_options::variables_map& vm, int64_t limit);
402 bool set_rate_limit(
const boost::program_options::variables_map& vm, int64_t limit);
404 bool has_too_many_connections(
const epee::net_utils::network_address &address);
405 size_t get_incoming_connections_count();
406 size_t get_incoming_connections_count(network_zone&);
407 size_t get_outgoing_connections_count();
408 size_t get_outgoing_connections_count(network_zone&);
410 bool check_connection_and_handshake_with_peer(
const epee::net_utils::network_address& na, uint64_t last_seen_stamp);
411 bool gray_peerlist_housekeeping();
412 bool check_incoming_connections();
415 _info(
"Killing the net_node");
417 if(mPeersLoggerThread !=
nullptr)
418 mPeersLoggerThread->join();
419 _info(
"Joined extra background net_node threads");
423 std::string print_connections_container();
430 m_rpc_port = rpc_port;
435 m_rpc_credits_per_hash = rpc_credits_per_hash;
471 bool m_seed_nodes_initialized =
false;
479 static boost::optional<p2p_connection_context> public_connect(network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
480 static boost::optional<p2p_connection_context> socks_connect(network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
uint32_t support_flags
Definition: net_node.h:124
Definition: p2p_protocol_defs.h:102
#define P2P_DEFAULT_LIMIT_RATE_DOWN
Definition: cryptonote_config.h:144
#define P2P_DEFAULT_LIMIT_RATE_UP
Definition: cryptonote_config.h:143
std::vector< cryptonote::blobdata > fluff_txs
Definition: net_node.h:121
#define P2P_DEFAULT_PEERS_IN_HANDSHAKE
Definition: cryptonote_config.h:134
relay_method
Methods tracking how a tx was received and relayed.
Definition: enums.h:37
#define P2P_DEFAULT_PACKET_MAX_SIZE
Definition: cryptonote_config.h:133
#define P2P_DEFAULT_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:135
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:56
network_type
Definition: cryptonote_config.h:268
crypto namespace.
Definition: crypto.cpp:60
Provides tx notification privacy.
Definition: levin_notify.h:70
bool m_in_timedsync
Definition: net_node.h:125
Definition: cryptonote_config.h:200
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:221
Definition: net_node.h:112
#define P2P_DEFAULT_PING_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:137
if(APPLE) if(DEPENDS) list(APPEND EXTRA_LIBRARIES "-framework Foundation -framework ApplicationServices -framework AppKit -framework IOKit") else() find_library(IOKIT_LIBRARY IOKit) mark_as_advanced(IOKIT_LIBRARY) list(APPEND EXTRA_LIBRARIES $
Definition: CMakeLists.txt:29
#define CRYPTONOTE_PRUNING_LOG_STRIPES
Definition: cryptonote_config.h:192
mdb_size_t count(MDB_cursor *cur)
Definition: value_stream.cpp:39
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:156
#define P2P_DEFAULT_HANDSHAKE_INTERVAL
Definition: cryptonote_config.h:132
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:105
std::set< epee::net_utils::network_address > sent_addresses
Definition: net_node.h:126
#define false
Definition: stdbool.h:37
peerid_type peer_id
Definition: net_node.h:123
std::chrono::steady_clock::time_point flush_time
Definition: net_node.h:122
Definition: blockchain_ancestry.cpp:72
p2p_connection_context_t()
Definition: net_node.h:113
Definition: p2p_protocol_defs.h:72
epee::levin::async_protocol_handler_config< detail::p2p_context > connections
Definition: levin_notify.h:66
PUSH_WARNINGS
Definition: hash-ops.h:53
#define true
Definition: stdbool.h:36
Definition: cryptonote_format_utils.h:44
#define P2P_IP_BLOCKTIME
Definition: cryptonote_config.h:147
Definition: command_line.h:53