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
142 static_assert(p2p_connection_context::handshake_command() == COMMAND_HANDSHAKE::ID,
"invalid handshake command id");
144 typedef epee::net_utils::boosted_tcp_server<epee::levin::async_protocol_handler<p2p_connection_context>>
net_server;
147 using connect_func = boost::optional<p2p_connection_context>(
network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
161 typedef epee::misc_utils::struct_init<config_t>
config;
166 : m_connect(nullptr),
167 m_net_server(
epee::net_utils::e_connection_type_P2P),
170 m_bind_ipv6_address(),
178 m_current_number_of_out_peers(0),
179 m_current_number_of_in_peers(0),
181 m_can_pingback(
false),
182 m_seed_nodes_initialized(
false)
184 set_config_defaults();
188 : m_connect(nullptr),
189 m_net_server(public_service,
epee::net_utils::e_connection_type_P2P),
192 m_bind_ipv6_address(),
200 m_current_number_of_out_peers(0),
201 m_current_number_of_in_peers(0),
203 m_can_pingback(
false),
204 m_seed_nodes_initialized(
false)
206 set_config_defaults();
233 m_config.m_net_config.config_id = 0;
237 m_config.m_support_flags = 0;
252 : m_payload_handler(payload_handler),
255 m_rpc_credits_per_hash(0),
256 m_allow_local_ip(
false),
257 m_hide_my_port(
false),
265 static void init_options(boost::program_options::options_description& desc);
268 network_zone& add_zone(epee::net_utils::zone zone);
269 bool init(
const boost::program_options::variables_map& vm);
271 bool send_stop_signal();
273 t_payload_net_handler& get_payload_object();
277 bool log_connections();
280 virtual uint64_t get_public_connections_count();
281 size_t get_public_outgoing_connections_count();
282 size_t get_public_white_peers_count();
283 size_t get_public_gray_peers_count();
284 void get_public_peerlist(std::vector<peerlist_entry>& gray, std::vector<peerlist_entry>& white);
285 void get_peerlist(std::vector<peerlist_entry>& gray, std::vector<peerlist_entry>& white);
287 void change_max_out_public_peers(
size_t count);
288 uint32_t get_max_out_public_peers()
const;
289 void change_max_in_public_peers(
size_t count);
290 uint32_t get_max_in_public_peers()
const;
291 virtual bool block_host(epee::net_utils::network_address address, time_t seconds =
P2P_IP_BLOCKTIME,
bool add_only =
false);
292 virtual bool unblock_host(
const epee::net_utils::network_address &address);
293 virtual bool block_subnet(
const epee::net_utils::ipv4_network_subnet &subnet, time_t seconds =
P2P_IP_BLOCKTIME);
294 virtual bool unblock_subnet(
const epee::net_utils::ipv4_network_subnet &subnet);
295 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); }
296 virtual std::map<std::string, time_t>
get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return m_blocked_hosts; }
297 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; }
299 virtual void add_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
300 virtual void remove_used_stripe_peer(
const typename t_payload_net_handler::connection_context &
context);
301 virtual void clear_used_stripe_peers();
304 const std::vector<std::string> m_seed_nodes_list =
305 {
"seeds.moneroseeds.se"
306 ,
"seeds.moneroseeds.ae.org"
307 ,
"seeds.moneroseeds.ch"
308 ,
"seeds.moneroseeds.li"
311 bool islimitup=
false;
312 bool islimitdown=
false;
319 return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED;
325 CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler, typename t_payload_net_handler::connection_context&)
331 int handle_handshake(
int command,
typename COMMAND_HANDSHAKE::request& arg,
typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context&
context);
332 int handle_timed_sync(
int command,
typename COMMAND_TIMED_SYNC::request& arg,
typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context&
context);
346 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);
348 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);
349 virtual bool invoke_notify_to_peer(
int command,
const epee::span<const uint8_t> req_buff,
const epee::net_utils::connection_context_base&
context);
350 virtual bool drop_connection(
const epee::net_utils::connection_context_base&
context);
351 virtual void request_callback(
const epee::net_utils::connection_context_base&
context);
352 virtual void for_each_connection(std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
353 virtual bool for_connection(
const boost::uuids::uuid&, std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
354 virtual bool add_host_fail(
const epee::net_utils::network_address &address,
unsigned int score = 1);
356 virtual bool is_remote_host_allowed(
const epee::net_utils::network_address &address, time_t *t = NULL);
358 bool parse_peer_from_string(epee::net_utils::network_address& pe,
const std::string& node_addr, uint16_t default_port = 0);
359 bool handle_command_line(
360 const boost::program_options::variables_map& vm
363 bool handle_remote_peerlist(
const std::vector<peerlist_entry>& peerlist,
const epee::net_utils::connection_context_base&
context);
367 bool sanitize_peerlist(std::vector<peerlist_entry>& local_peerlist);
369 bool connections_maker();
370 bool peer_sync_idle_maker();
372 bool do_peer_timed_sync(
const epee::net_utils::connection_context_base&
context,
peerid_type peer_id);
373 bool update_dns_blocklist();
375 bool make_new_connection_from_anchor_peerlist(
const std::vector<anchor_peerlist_entry>& anchor_peerlist);
376 bool make_new_connection_from_peerlist(
network_zone& zone,
bool use_white_list);
377 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);
378 size_t get_random_index_with_fixed_probability(
size_t max_index);
381 bool is_addr_connected(
const epee::net_utils::network_address& peer);
382 void add_upnp_port_mapping_impl(uint32_t port,
bool ipv6=
false);
383 void add_upnp_port_mapping_v4(uint32_t port);
384 void add_upnp_port_mapping_v6(uint32_t port);
385 void add_upnp_port_mapping(uint32_t port,
bool ipv4=
true,
bool ipv6=
false);
386 void delete_upnp_port_mapping_impl(uint32_t port,
bool ipv6=
false);
387 void delete_upnp_port_mapping_v4(uint32_t port);
388 void delete_upnp_port_mapping_v6(uint32_t port);
389 void delete_upnp_port_mapping(uint32_t port);
390 template<
class t_callback>
393 bool make_expected_connections_count(
network_zone& zone,
PeerType peer_type,
size_t expected_connections);
394 void record_addr_failed(
const epee::net_utils::network_address& addr);
395 bool is_addr_recently_failed(
const epee::net_utils::network_address& addr);
396 bool is_priority_node(
const epee::net_utils::network_address& na);
397 std::set<std::string> get_ip_seed_nodes()
const;
398 std::set<std::string> get_dns_seed_nodes();
399 std::set<std::string> get_seed_nodes(epee::net_utils::zone);
400 bool connect_to_seed(epee::net_utils::zone);
402 template <
class Container>
403 bool connect_to_peerlist(
const Container& peers);
405 template <
class Container>
406 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);
408 bool set_max_out_peers(
network_zone& zone, int64_t max);
410 bool set_tos_flag(
const boost::program_options::variables_map& vm,
int limit);
412 bool set_rate_up_limit(
const boost::program_options::variables_map& vm, int64_t limit);
413 bool set_rate_down_limit(
const boost::program_options::variables_map& vm, int64_t limit);
414 bool set_rate_limit(
const boost::program_options::variables_map& vm, int64_t limit);
416 bool has_too_many_connections(
const epee::net_utils::network_address &address);
417 size_t get_incoming_connections_count();
419 size_t get_outgoing_connections_count();
422 bool check_connection_and_handshake_with_peer(
const epee::net_utils::network_address& na, uint64_t last_seen_stamp);
423 bool gray_peerlist_housekeeping();
424 bool check_incoming_connections();
427 _info(
"Killing the net_node");
429 if(mPeersLoggerThread !=
nullptr)
430 mPeersLoggerThread->join();
431 _info(
"Joined extra background net_node threads");
435 std::string print_connections_container();
442 m_rpc_port = rpc_port;
447 m_rpc_credits_per_hash = rpc_credits_per_hash;
489 static boost::optional<p2p_connection_context> public_connect(
network_zone&, epee::net_utils::network_address
const&, epee::net_utils::ssl_support_t);
490 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:221
Provides tx notification privacy.
Definition: levin_notify.h:70
#define P2P_DEFAULT_PACKET_MAX_SIZE
Definition: cryptonote_config.h:135
#define P2P_DEFAULT_LIMIT_RATE_DOWN
Definition: cryptonote_config.h:147
#define P2P_IP_BLOCKTIME
Definition: cryptonote_config.h:150
#define P2P_DEFAULT_PEERS_IN_HANDSHAKE
Definition: cryptonote_config.h:136
#define CRYPTONOTE_PRUNING_LOG_STRIPES
Definition: cryptonote_config.h:195
#define P2P_DEFAULT_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:138
#define P2P_DEFAULT_LIMIT_RATE_UP
Definition: cryptonote_config.h:146
#define P2P_DEFAULT_PING_CONNECTION_TIMEOUT
Definition: cryptonote_config.h:140
#define P2P_DEFAULT_HANDSHAKE_INTERVAL
Definition: cryptonote_config.h:134
PUSH_WARNINGS
Definition: hash-ops.h:53
Definition: cryptonote_config.h:205
epee::levin::async_protocol_handler_config< detail::p2p_context > connections
Definition: levin_notify.h:66
network_type
Definition: cryptonote_config.h:273
relay_method
Methods tracking how a tx was received and relayed.
Definition: enums.h:37
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:56
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:105
Definition: blockchain_ancestry.cpp:72
#define true
Definition: stdbool.h:36
#define false
Definition: stdbool.h:37
Definition: command_line.h:53
Definition: p2p_protocol_defs.h:102
Definition: net_node.h:112
std::vector< cryptonote::blobdata > fluff_txs
Definition: net_node.h:121
uint32_t support_flags
Definition: net_node.h:124
peerid_type peer_id
Definition: net_node.h:123
std::set< epee::net_utils::network_address > sent_addresses
Definition: net_node.h:126
p2p_connection_context_t()
Definition: net_node.h:113
bool m_in_timedsync
Definition: net_node.h:125
std::chrono::steady_clock::time_point flush_time
Definition: net_node.h:122
Definition: p2p_protocol_defs.h:72
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto