Electroneum
net_node.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
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>
46 
47 #include "cryptonote_config.h"
48 #include "warnings.h"
49 #include "net/levin_server_cp2.h"
50 #include "p2p_protocol_defs.h"
51 #include "storages/levin_abstract_invoke2.h"
52 #include "net_peerlist.h"
53 #include "math_helper.h"
54 #include "net_node_common.h"
55 #include "common/command_line.h"
56 
59 
60 namespace nodetool
61 {
62  template<class base_type>
63  struct p2p_connection_context_t: base_type //t_payload_net_handler::connection_context //public net_utils::connection_context_base
64  {
65  p2p_connection_context_t(): peer_id(0), support_flags(0), m_in_timedsync(false) {}
66 
68  uint32_t support_flags;
70  };
71 
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
76  {
77  struct by_conn_id{};
78  struct by_peer_id{};
79  struct by_addr{};
80 
82 
85 
86  public:
87  typedef t_payload_net_handler payload_net_handler;
88 
89  node_server(t_payload_net_handler& payload_handler)
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),
94  m_no_igd(false),
95  m_offline(false),
96  m_save_graph(false),
97  is_closing(false),
98  m_net_server( epee::net_utils::e_connection_type_P2P ) // this is a P2P connection of the main p2p node server, because this is class node_server<>
99  {}
100  virtual ~node_server()
101  {}
102 
103  static void init_options(boost::program_options::options_description& desc);
104 
105  bool run();
106  bool init(const boost::program_options::variables_map& vm);
107  bool deinit();
108  bool send_stop_signal();
109  uint32_t get_this_peer_port(){return m_listenning_port;}
110  t_payload_net_handler& get_payload_object();
111 
112  template <class Archive, class t_version_type>
113  void serialize(Archive &a, const t_version_type ver)
114  {
115  a & m_peerlist;
116  if (ver == 0)
117  {
118  // from v1, we do not store the peer id anymore
119  peerid_type peer_id = AUTO_VAL_INIT (peer_id);
120  a & peer_id;
121  }
122  }
123  // debug functions
124  bool log_peerlist();
125  bool log_connections();
126  virtual uint64_t get_connections_count();
127  size_t get_outgoing_connections_count();
128  peerlist_manager& get_peerlist_manager(){return m_peerlist;}
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; }
133  private:
134  const std::vector<std::string> m_seed_nodes_list =
135  {
136  "seeds.electroneumseed.com",
137  "seeds.electroneumseed.info",
138  "seeds.electroneumseed.org",
139  "seeds.electroneumseed.net",
140  };
141 
142  bool islimitup=false;
143  bool islimitdown=false;
144 
145  typedef COMMAND_REQUEST_STAT_INFO_T<typename t_payload_net_handler::stat_info> COMMAND_REQUEST_STAT_INFO;
146 
147  CHAIN_LEVIN_INVOKE_MAP2(p2p_connection_context); //move levin_commands_handler interface invoke(...) callbacks into invoke map
148  CHAIN_LEVIN_NOTIFY_MAP2(p2p_connection_context); //move levin_commands_handler interface notify(...) callbacks into nothing
149 
150  BEGIN_INVOKE_MAP2(node_server)
151  HANDLE_INVOKE_T2(COMMAND_HANDSHAKE, &node_server::handle_handshake)
152  HANDLE_INVOKE_T2(COMMAND_TIMED_SYNC, &node_server::handle_timed_sync)
153  HANDLE_INVOKE_T2(COMMAND_PING, &node_server::handle_ping)
154 #ifdef ALLOW_DEBUG_COMMANDS
155  HANDLE_INVOKE_T2(COMMAND_REQUEST_STAT_INFO, &node_server::handle_get_stat_info)
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)
158 #endif
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&)
161  END_INVOKE_MAP2()
162 
163  enum PeerType { anchor = 0, white, gray };
164 
165  //----------------- commands handlers ----------------------------------------------
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);
168  int handle_ping(int command, COMMAND_PING::request& arg, COMMAND_PING::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);
173 #endif
174  int handle_get_support_flags(int command, COMMAND_REQUEST_SUPPORT_FLAGS::request& arg, COMMAND_REQUEST_SUPPORT_FLAGS::response& rsp, p2p_connection_context& context);
175  bool init_config();
176  bool make_default_peer_id();
177  bool make_default_config();
178  bool store_config();
179  bool check_trust(const proof_of_trust& tr);
180 
181 
182  //----------------- levin_commands_handler -------------------------------------------------------------
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);
186  //----------------- i_p2p_endpoint -------------------------------------------------------------
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);
196  //----------------- i_connection_filter --------------------------------------------------------
197  virtual bool is_remote_host_allowed(const epee::net_utils::network_address &address);
198  //-----------------------------------------------------------------------------------------------
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
202  );
203  bool idle_worker();
204  bool handle_remote_peerlist(const std::list<peerlist_entry>& peerlist, time_t local_time, const epee::net_utils::connection_context_base& context);
205  bool get_local_node_data(basic_node_data& node_data);
206  //bool get_local_handshake_data(handshake_data& hshd);
207 
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);
210 
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);
215 
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);
220  bool is_peer_used(const peerlist_entry& peer);
221  bool is_peer_used(const anchor_peerlist_entry& peer);
222  bool is_addr_connected(const epee::net_utils::network_address& peer);
223  template<class t_callback>
224  bool try_ping(basic_node_data& node_data, p2p_connection_context& context, t_callback cb);
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();
232 
233  template <class Container>
234  bool connect_to_peerlist(const Container& peers);
235 
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);
238 
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);
241 
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);
245 
246  bool has_too_many_connections(const epee::net_utils::network_address &address);
247 
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();
250 
251  void kill() {
252  _info("Killing the net_node");
253  is_closing = true;
254  if(mPeersLoggerThread != nullptr)
255  mPeersLoggerThread->join(); // make sure the thread finishes
256  _info("Joined extra background net_node threads");
257  }
258 
259  //debug functions
260  std::string print_connections_container();
261 
262 
263  typedef epee::net_utils::boosted_tcp_server<epee::levin::async_protocol_handler<p2p_connection_context> > net_server;
264 
265  struct config
266  {
268  uint64_t m_peer_id;
269  uint32_t m_support_flags;
270 
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()
276  };
277 
278  public:
279  config m_config; // TODO was private, add getters?
280  std::atomic<unsigned int> m_current_number_of_out_peers;
281 
282  void set_save_graph(bool save_graph)
283  {
284  m_save_graph = save_graph;
286  }
287  private:
288  std::string m_config_folder;
289 
293  uint32_t m_external_port;
294  uint32_t m_ip_address;
297  bool m_no_igd;
298  bool m_offline;
299  std::atomic<bool> m_save_graph;
300  std::atomic<bool> is_closing;
301  std::unique_ptr<boost::thread> mPeersLoggerThread;
302  //critical_section m_connections_lock;
303  //connections_indexed_container m_connections;
304 
305  t_payload_net_handler& m_payload_handler;
307 
308  epee::math_helper::once_a_time_seconds<P2P_DEFAULT_HANDSHAKE_INTERVAL> m_peer_handshake_idle_maker_interval;
309  epee::math_helper::once_a_time_seconds<1> m_connections_maker_interval;
310  epee::math_helper::once_a_time_seconds<60*30, false> m_peerlist_store_interval;
311  epee::math_helper::once_a_time_seconds<60> m_gray_peerlist_housekeeping_interval;
312 
313  std::string m_bind_ip;
314  std::string m_port;
315 #ifdef ALLOW_DEBUG_COMMANDS
316  uint64_t m_last_stat_request_time;
317 #endif
318  std::list<epee::net_utils::network_address> m_priority_peers;
319  std::vector<epee::net_utils::network_address> m_exclusive_peers;
320  std::vector<epee::net_utils::network_address> m_seed_nodes;
321  std::list<nodetool::peerlist_entry> m_command_line_peers;
322  uint64_t m_peer_livetime;
323  //keep connections to initiate some interactions
326 
327  std::map<epee::net_utils::network_address, time_t> m_conn_fails_cache;
328  epee::critical_section m_conn_fails_cache_lock;
329 
330  epee::critical_section m_blocked_hosts_lock;
331  std::map<std::string, time_t> m_blocked_hosts;
332 
333  epee::critical_section m_host_fails_score_lock;
334  std::map<std::string, uint64_t> m_host_fails_score;
335 
336  bool m_testnet;
337  };
338 }
339 
340 #include "net_node.inl"
341 
342 POP_WARNINGS
#define tr(x)
Definition: common_defines.h:4
bool m_testnet
Definition: net_node.h:336
std::unique_ptr< boost::thread > mPeersLoggerThread
Definition: net_node.h:301
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
std::vector< epee::net_utils::network_address > m_exclusive_peers
Definition: net_node.h:319
bool m_hide_my_port
Definition: net_node.h:296
network_config m_net_config
Definition: net_node.h:267
boost::uuids::uuid uuid
Definition: net_node_common.h:41
uint64_t m_peer_id
Definition: net_node.h:268
Definition: p2p_protocol_defs.h:309
Definition: p2p_protocol_defs.h:290
epee::net_utils::boosted_tcp_server< epee::levin::async_protocol_handler< p2p_connection_context > > net_server
Definition: net_node.h:263
bool m_in_timedsync
Definition: net_node.h:69
Definition: p2p_protocol_defs.h:170
Definition: block_queue.cpp:41
epee::math_helper::once_a_time_seconds< 60 *30, false > m_peerlist_store_interval
Definition: net_node.h:310
Definition: cryptonote_config.h:156
Definition: net_node.h:163
COMMAND_HANDSHAKE_T< typename t_payload_net_handler::payload_type > COMMAND_HANDSHAKE
Definition: net_node.h:83
bool m_no_igd
Definition: net_node.h:297
bool m_offline
Definition: net_node.h:298
uint32_t m_listenning_port
Definition: net_node.h:292
epee::math_helper::once_a_time_seconds< 1 > m_connections_maker_interval
Definition: net_node.h:309
bool m_allow_local_ip
Definition: net_node.h:295
#define P2P_IP_BLOCKTIME
Definition: cryptonote_config.h:129
COMMAND_TIMED_SYNC_T< typename t_payload_net_handler::payload_type > COMMAND_TIMED_SYNC
Definition: net_node.h:84
uint32_t m_ip_address
Definition: net_node.h:294
epee::critical_section m_blocked_hosts_lock
Definition: net_node.h:330
Definition: net_node.h:79
Definition: net_node.h:78
epee::critical_section m_conn_fails_cache_lock
Definition: net_node.h:328
std::string m_config_folder
Definition: net_node.h:288
static void set_save_graph(bool save_graph)
Definition: connection_basic.cpp:290
std::atomic< bool > is_closing
Definition: net_node.h:300
virtual std::map< std::string, time_t > get_blocked_hosts()
Definition: net_node.h:132
std::list< epee::net_utils::network_address > m_priority_peers
Definition: net_node.h:318
virtual ~node_server()
Definition: net_node.h:100
peerlist_manager & get_peerlist_manager()
Definition: net_node.h:128
epee::math_helper::once_a_time_seconds< 60 > m_gray_peerlist_housekeeping_interval
Definition: net_node.h:311
std::map< epee::net_utils::network_address, time_t > m_conn_fails_cache
Definition: net_node.h:327
epee::critical_section m_host_fails_score_lock
Definition: net_node.h:333
Definition: p2p_protocol_defs.h:84
Definition: p2p_protocol_defs.h:148
DISABLE_VS_WARNINGS(4244 4345 4503) namespace cryptonote
Definition: tx_pool.cpp:53
std::map< std::string, uint64_t > m_host_fails_score
Definition: net_node.h:334
peerlist_manager m_peerlist
Definition: net_node.h:306
std::string m_bind_ip
Definition: net_node.h:313
uint64_t peerid_type
Definition: p2p_protocol_defs.h:46
Definition: connection_basic.hpp:81
uint32_t m_support_flags
Definition: net_node.h:269
#define false
Definition: stdbool.h:38
void kill()
Definition: net_node.h:251
Definition: net_peerlist.h:67
void serialize(Archive &a, const t_version_type ver)
Definition: net_node.h:113
Definition: net_node.h:77
uint32_t m_external_port
Definition: net_node.h:293
t_payload_net_handler & m_payload_handler
Definition: net_node.h:305
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
bool m_have_address
Definition: net_node.h:290
Definition: net_node.h:63
Definition: net_node.h:60
bool m_first_connection_maker_call
Definition: net_node.h:291
p2p_connection_context_t()
Definition: net_node.h:65
std::atomic< bool > m_save_graph
Definition: net_node.h:299
peerid_type peer_id
Definition: net_node.h:67
uint32_t get_this_peer_port()
Definition: net_node.h:109
t_payload_net_handler payload_net_handler
Definition: net_node.h:87
p2p_connection_context_t< typename t_payload_net_handler::connection_context > p2p_connection_context
Definition: net_node.h:81
node_server(t_payload_net_handler &payload_handler)
Definition: net_node.h:89
std::map< std::string, time_t > m_blocked_hosts
Definition: net_node.h:331
Definition: p2p_protocol_defs.h:232
std::list< nodetool::peerlist_entry > m_command_line_peers
Definition: net_node.h:321
net_server m_net_server
Definition: net_node.h:324
COMMAND_REQUEST_STAT_INFO_T< typename t_payload_net_handler::stat_info > COMMAND_REQUEST_STAT_INFO
Definition: net_node.h:145
Definition: p2p_protocol_defs.h:301
boost::uuids::uuid m_network_id
Definition: net_node.h:325
Definition: command_line.h:51
Definition: p2p_protocol_defs.h:130
Definition: net_node_common.h:45
std::string m_port
Definition: net_node.h:314
uint64_t m_peer_livetime
Definition: net_node.h:322
Definition: net_node.h:73
std::vector< epee::net_utils::network_address > m_seed_nodes
Definition: net_node.h:320
Definition: p2p_protocol_defs.h:69
epee::math_helper::once_a_time_seconds< P2P_DEFAULT_HANDSHAKE_INTERVAL > m_peer_handshake_idle_maker_interval
Definition: net_node.h:308
json::wvalue context
Definition: mustache.h:12