33 #include <boost/thread.hpp> 34 #include <boost/bind.hpp> 35 #include <boost/bimap.hpp> 36 #include <boost/multi_index_container.hpp> 37 #include <boost/multi_index/ordered_index.hpp> 38 #include <boost/multi_index/identity.hpp> 39 #include <boost/multi_index/member.hpp> 40 #include <boost/multi_index/global_fun.hpp> 41 #include <boost/program_options/options_description.hpp> 42 #include <boost/program_options/variables_map.hpp> 43 #include <boost/serialization/version.hpp> 44 #include <boost/uuid/uuid.hpp> 45 #include <boost/serialization/map.hpp> 49 #include "net/levin_server_cp2.h" 51 #include "storages/levin_abstract_invoke2.h" 53 #include "math_helper.h" 62 template<
class base_type>
72 template<
class t_payload_net_handler>
73 class node_server:
public epee::levin::levin_commands_handler<p2p_connection_context_t<typename t_payload_net_handler::connection_context> >,
74 public i_p2p_endpoint<typename t_payload_net_handler::connection_context>,
75 public epee::net_utils::i_connection_filter
90 :m_payload_handler(payload_handler),
91 m_current_number_of_out_peers(0),
92 m_allow_local_ip(
false),
93 m_hide_my_port(
false),
103 static void init_options(boost::program_options::options_description& desc);
106 bool init(
const boost::program_options::variables_map& vm);
108 bool send_stop_signal();
110 t_payload_net_handler& get_payload_object();
112 template <
class Archive,
class t_version_type>
125 bool log_connections();
126 virtual uint64_t get_connections_count();
127 size_t get_outgoing_connections_count();
129 void delete_connections(
size_t count);
130 virtual bool block_host(
const epee::net_utils::network_address &adress, time_t seconds =
P2P_IP_BLOCKTIME);
131 virtual bool unblock_host(
const epee::net_utils::network_address &address);
132 virtual std::map<std::string, time_t>
get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
return m_blocked_hosts; }
134 const std::vector<std::string> m_seed_nodes_list =
136 "seeds.electroneumseed.com",
137 "seeds.electroneumseed.info",
138 "seeds.electroneumseed.org",
139 "seeds.electroneumseed.net",
142 bool islimitup=
false;
143 bool islimitdown=
false;
153 HANDLE_INVOKE_T2(
COMMAND_PING, &node_server::handle_ping)
154 #ifdef ALLOW_DEBUG_COMMANDS 156 HANDLE_INVOKE_T2(COMMAND_REQUEST_NETWORK_STATE, &node_server::handle_get_network_state)
157 HANDLE_INVOKE_T2(COMMAND_REQUEST_PEER_ID, &node_server::handle_get_peer_id)
159 HANDLE_INVOKE_T2(COMMAND_REQUEST_SUPPORT_FLAGS, &node_server::handle_get_support_flags)
160 CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler,
typename t_payload_net_handler::connection_context&)
166 int handle_handshake(
int command,
typename COMMAND_HANDSHAKE::request&
arg,
typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context&
context);
167 int handle_timed_sync(
int command,
typename COMMAND_TIMED_SYNC::request&
arg,
typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context&
context);
169 #ifdef ALLOW_DEBUG_COMMANDS 170 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);
171 int handle_get_network_state(
int command, COMMAND_REQUEST_NETWORK_STATE::request&
arg, COMMAND_REQUEST_NETWORK_STATE::response& rsp, p2p_connection_context&
context);
172 int handle_get_peer_id(
int command, COMMAND_REQUEST_PEER_ID::request&
arg, COMMAND_REQUEST_PEER_ID::response& rsp, p2p_connection_context&
context);
174 int handle_get_support_flags(
int command, COMMAND_REQUEST_SUPPORT_FLAGS::request&
arg, COMMAND_REQUEST_SUPPORT_FLAGS::response& rsp, p2p_connection_context&
context);
176 bool make_default_peer_id();
177 bool make_default_config();
179 bool check_trust(
const proof_of_trust&
tr);
183 virtual void on_connection_new(p2p_connection_context&
context);
184 virtual void on_connection_close(p2p_connection_context&
context);
185 virtual void callback(p2p_connection_context&
context);
187 virtual bool relay_notify_to_list(
int command,
const std::string& data_buff,
const std::list<boost::uuids::uuid> &connections);
188 virtual bool relay_notify_to_all(
int command,
const std::string& data_buff,
const epee::net_utils::connection_context_base&
context);
189 virtual bool invoke_command_to_peer(
int command,
const std::string& req_buff, std::string& resp_buff,
const epee::net_utils::connection_context_base&
context);
190 virtual bool invoke_notify_to_peer(
int command,
const std::string& req_buff,
const epee::net_utils::connection_context_base&
context);
191 virtual bool drop_connection(
const epee::net_utils::connection_context_base&
context);
192 virtual void request_callback(
const epee::net_utils::connection_context_base&
context);
193 virtual void for_each_connection(std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
194 virtual bool for_connection(
const boost::uuids::uuid&, std::function<
bool(
typename t_payload_net_handler::connection_context&,
peerid_type, uint32_t)> f);
195 virtual bool add_host_fail(
const epee::net_utils::network_address &address);
197 virtual bool is_remote_host_allowed(
const epee::net_utils::network_address &address);
199 bool parse_peer_from_string(epee::net_utils::network_address& pe,
const std::string& node_addr, uint16_t default_port = 0);
200 bool handle_command_line(
201 const boost::program_options::variables_map& vm
204 bool handle_remote_peerlist(
const std::list<peerlist_entry>& peerlist, time_t local_time,
const epee::net_utils::connection_context_base&
context);
208 bool merge_peerlist_with_local(
const std::list<peerlist_entry>& bs);
209 bool fix_time_delta(std::list<peerlist_entry>& local_peerlist, time_t local_time, int64_t& delta);
211 bool connections_maker();
212 bool peer_sync_idle_maker();
213 bool do_handshake_with_peer(
peerid_type& pi, p2p_connection_context&
context,
bool just_take_peerlist =
false);
214 bool do_peer_timed_sync(
const epee::net_utils::connection_context_base&
context,
peerid_type peer_id);
216 bool make_new_connection_from_anchor_peerlist(
const std::vector<anchor_peerlist_entry>& anchor_peerlist);
217 bool make_new_connection_from_peerlist(
bool use_white_list);
218 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);
219 size_t get_random_index_with_fixed_probability(
size_t max_index);
222 bool is_addr_connected(
const epee::net_utils::network_address& peer);
223 template<
class t_callback>
225 bool try_get_support_flags(
const p2p_connection_context&
context, std::function<
void(p2p_connection_context&,
const uint32_t&)> f);
226 bool make_expected_connections_count(PeerType peer_type,
size_t expected_connections);
227 void cache_connect_fail_info(
const epee::net_utils::network_address& addr);
228 bool is_addr_recently_failed(
const epee::net_utils::network_address& addr);
229 bool is_priority_node(
const epee::net_utils::network_address& na);
230 std::set<std::string> get_seed_nodes(
bool testnet)
const;
231 bool connect_to_seed();
233 template <
class Container>
234 bool connect_to_peerlist(
const Container& peers);
236 template <
class Container>
237 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);
239 bool set_max_out_peers(
const boost::program_options::variables_map& vm, int64_t max);
240 bool set_tos_flag(
const boost::program_options::variables_map& vm,
int limit);
242 bool set_rate_up_limit(
const boost::program_options::variables_map& vm, int64_t limit);
243 bool set_rate_down_limit(
const boost::program_options::variables_map& vm, int64_t limit);
244 bool set_rate_limit(
const boost::program_options::variables_map& vm, int64_t limit);
246 bool has_too_many_connections(
const epee::net_utils::network_address &address);
248 bool check_connection_and_handshake_with_peer(
const epee::net_utils::network_address& na, uint64_t last_seen_stamp);
249 bool gray_peerlist_housekeeping();
252 _info(
"Killing the net_node");
254 if(mPeersLoggerThread !=
nullptr)
255 mPeersLoggerThread->join();
256 _info(
"Joined extra background net_node threads");
260 std::string print_connections_container();
263 typedef epee::net_utils::boosted_tcp_server<epee::levin::async_protocol_handler<p2p_connection_context> >
net_server;
271 BEGIN_KV_SERIALIZE_MAP()
272 KV_SERIALIZE(m_net_config)
273 KV_SERIALIZE(m_peer_id)
274 KV_SERIALIZE(m_support_flags)
275 END_KV_SERIALIZE_MAP()
280 std::atomic<
unsigned int> m_current_number_of_out_peers;
282 void set_save_graph(
bool save_graph)
284 m_save_graph = save_graph;
315 #ifdef ALLOW_DEBUG_COMMANDS 316 uint64_t m_last_stat_request_time;
#define tr(x)
Definition: common_defines.h:4
PUSH_WARNINGS
Definition: hash-ops.h:54
void init_options(boost::program_options::options_description &hidden_options, boost::program_options::options_description &normal_options)
Definition: posix_daemonizer.inl:57
bool m_in_timedsync
Definition: net_node.h:69
Definition: block_queue.cpp:41
Definition: cryptonote_config.h:156
#define P2P_IP_BLOCKTIME
Definition: cryptonote_config.h:129
static void set_save_graph(bool save_graph)
Definition: connection_basic.cpp:290
Definition: p2p_protocol_defs.h:84
DISABLE_VS_WARNINGS(4244 4345 4503) namespace cryptonote
Definition: tx_pool.cpp:53
Definition: connection_basic.hpp:81
#define false
Definition: stdbool.h:38
Definition: connection_basic.cpp:91
internal::NamedArgWithType< char, T > arg(StringRef name, const T &arg)
Definition: format.h:3450
uint32_t support_flags
Definition: net_node.h:68
string a
Definition: MakeCryptoOps.py:15
Definition: net_node.h:63
p2p_connection_context_t()
Definition: net_node.h:65
peerid_type peer_id
Definition: net_node.h:67
Definition: command_line.h:51
Definition: p2p_protocol_defs.h:69
json::wvalue context
Definition: mustache.h:12