|
| 1 | +// Copyright (c) 2012-2013 The Cryptonote developers |
| 2 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#pragma once |
| 6 | +#include <boost/thread.hpp> |
| 7 | +#include <boost/bind.hpp> |
| 8 | +#include <boost/foreach.hpp> |
| 9 | +#include <boost/bimap.hpp> |
| 10 | +#include <boost/multi_index_container.hpp> |
| 11 | +#include <boost/multi_index/ordered_index.hpp> |
| 12 | +#include <boost/multi_index/identity.hpp> |
| 13 | +#include <boost/multi_index/member.hpp> |
| 14 | +#include <boost/multi_index/global_fun.hpp> |
| 15 | +#include <boost/program_options/options_description.hpp> |
| 16 | +#include <boost/program_options/variables_map.hpp> |
| 17 | +#include <boost/serialization/version.hpp> |
| 18 | + |
| 19 | +#include "warnings.h" |
| 20 | +#include "net/levin_server_cp2.h" |
| 21 | +#include "p2p_protocol_defs.h" |
| 22 | +#include "storages/levin_abstract_invoke2.h" |
| 23 | +#include "net_peerlist.h" |
| 24 | +#include "p2p_networks.h" |
| 25 | +#include "math_helper.h" |
| 26 | +#include "net_node_common.h" |
| 27 | + |
| 28 | +using namespace epee; |
| 29 | + |
| 30 | +PUSH_WARNINGS |
| 31 | +DISABLE_VS_WARNINGS(4355) |
| 32 | + |
| 33 | +namespace nodetool |
| 34 | +{ |
| 35 | + template<class base_type> |
| 36 | + struct p2p_connection_context_t: base_type //t_payload_net_handler::connection_context //public net_utils::connection_context_base |
| 37 | + { |
| 38 | + peerid_type peer_id; |
| 39 | + }; |
| 40 | + |
| 41 | + template<class t_payload_net_handler> |
| 42 | + class node_server: public levin::levin_commands_handler<p2p_connection_context_t<typename t_payload_net_handler::connection_context> >, |
| 43 | + public i_p2p_endpoint<typename t_payload_net_handler::connection_context> |
| 44 | + { |
| 45 | + struct by_conn_id{}; |
| 46 | + struct by_peer_id{}; |
| 47 | + struct by_addr{}; |
| 48 | + |
| 49 | + typedef p2p_connection_context_t<typename t_payload_net_handler::connection_context> p2p_connection_context; |
| 50 | + |
| 51 | + typedef COMMAND_HANDSHAKE_T<typename t_payload_net_handler::payload_type> COMMAND_HANDSHAKE; |
| 52 | + typedef COMMAND_TIMED_SYNC_T<typename t_payload_net_handler::payload_type> COMMAND_TIMED_SYNC; |
| 53 | + |
| 54 | + public: |
| 55 | + typedef t_payload_net_handler payload_net_handler; |
| 56 | + // Some code |
| 57 | + node_server(t_payload_net_handler& payload_handler):m_payload_handler(payload_handler), m_allow_local_ip(false), m_hide_my_port(false) |
| 58 | + {} |
| 59 | + |
| 60 | + static void init_options(boost::program_options::options_description& desc); |
| 61 | + |
| 62 | + bool run(); |
| 63 | + bool init(const boost::program_options::variables_map& vm); |
| 64 | + bool deinit(); |
| 65 | + bool send_stop_signal(); |
| 66 | + uint32_t get_this_peer_port(){return m_listenning_port;} |
| 67 | + t_payload_net_handler& get_payload_object(); |
| 68 | + |
| 69 | + template <class Archive, class t_version_type> |
| 70 | + void serialize(Archive &a, const t_version_type ver) |
| 71 | + { |
| 72 | + a & m_peerlist; |
| 73 | + a & m_config.m_peer_id; |
| 74 | + } |
| 75 | + // debug functions |
| 76 | + bool log_peerlist(); |
| 77 | + bool log_connections(); |
| 78 | + virtual uint64_t get_connections_count(); |
| 79 | + size_t get_outgoing_connections_count(); |
| 80 | + peerlist_manager& get_peerlist_manager(){return m_peerlist;} |
| 81 | + private: |
| 82 | + typedef COMMAND_REQUEST_STAT_INFO_T<typename t_payload_net_handler::stat_info> COMMAND_REQUEST_STAT_INFO; |
| 83 | + |
| 84 | + CHAIN_LEVIN_INVOKE_MAP2(p2p_connection_context); //move levin_commands_handler interface invoke(...) callbacks into invoke map |
| 85 | + CHAIN_LEVIN_NOTIFY_MAP2(p2p_connection_context); //move levin_commands_handler interface notify(...) callbacks into nothing |
| 86 | + |
| 87 | + BEGIN_INVOKE_MAP2(node_server) |
| 88 | + HANDLE_INVOKE_T2(COMMAND_HANDSHAKE, &node_server::handle_handshake) |
| 89 | + HANDLE_INVOKE_T2(COMMAND_TIMED_SYNC, &node_server::handle_timed_sync) |
| 90 | + HANDLE_INVOKE_T2(COMMAND_PING, &node_server::handle_ping) |
| 91 | +#ifdef ALLOW_DEBUG_COMMANDS |
| 92 | + HANDLE_INVOKE_T2(COMMAND_REQUEST_STAT_INFO, &node_server::handle_get_stat_info) |
| 93 | + HANDLE_INVOKE_T2(COMMAND_REQUEST_NETWORK_STATE, &node_server::handle_get_network_state) |
| 94 | + HANDLE_INVOKE_T2(COMMAND_REQUEST_PEER_ID, &node_server::handle_get_peer_id) |
| 95 | +#endif |
| 96 | + CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler, typename t_payload_net_handler::connection_context&) |
| 97 | + END_INVOKE_MAP2() |
| 98 | + |
| 99 | + //----------------- commands handlers ---------------------------------------------- |
| 100 | + int handle_handshake(int command, typename COMMAND_HANDSHAKE::request& arg, typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context& context); |
| 101 | + int handle_timed_sync(int command, typename COMMAND_TIMED_SYNC::request& arg, typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context& context); |
| 102 | + int handle_ping(int command, COMMAND_PING::request& arg, COMMAND_PING::response& rsp, p2p_connection_context& context); |
| 103 | +#ifdef ALLOW_DEBUG_COMMANDS |
| 104 | + 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); |
| 105 | + int handle_get_network_state(int command, COMMAND_REQUEST_NETWORK_STATE::request& arg, COMMAND_REQUEST_NETWORK_STATE::response& rsp, p2p_connection_context& context); |
| 106 | + int handle_get_peer_id(int command, COMMAND_REQUEST_PEER_ID::request& arg, COMMAND_REQUEST_PEER_ID::response& rsp, p2p_connection_context& context); |
| 107 | +#endif |
| 108 | + bool init_config(); |
| 109 | + bool make_default_config(); |
| 110 | + bool store_config(); |
| 111 | + bool check_trust(const proof_of_trust& tr); |
| 112 | + |
| 113 | + |
| 114 | + //----------------- levin_commands_handler ------------------------------------------------------------- |
| 115 | + virtual void on_connection_new(p2p_connection_context& context); |
| 116 | + virtual void on_connection_close(p2p_connection_context& context); |
| 117 | + virtual void callback(p2p_connection_context& context); |
| 118 | + //----------------- i_p2p_endpoint ------------------------------------------------------------- |
| 119 | + virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context); |
| 120 | + 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); |
| 121 | + virtual bool invoke_notify_to_peer(int command, const std::string& req_buff, const epee::net_utils::connection_context_base& context); |
| 122 | + virtual bool drop_connection(const epee::net_utils::connection_context_base& context); |
| 123 | + virtual void request_callback(const epee::net_utils::connection_context_base& context); |
| 124 | + virtual void for_each_connection(std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type)> f); |
| 125 | + //----------------------------------------------------------------------------------------------- |
| 126 | + bool parse_peer_from_string(nodetool::net_address& pe, const std::string& node_addr); |
| 127 | + bool handle_command_line(const boost::program_options::variables_map& vm); |
| 128 | + bool idle_worker(); |
| 129 | + bool handle_remote_peerlist(const std::list<peerlist_entry>& peerlist, time_t local_time, const net_utils::connection_context_base& context); |
| 130 | + bool get_local_node_data(basic_node_data& node_data); |
| 131 | + //bool get_local_handshake_data(handshake_data& hshd); |
| 132 | + |
| 133 | + bool merge_peerlist_with_local(const std::list<peerlist_entry>& bs); |
| 134 | + bool fix_time_delta(std::list<peerlist_entry>& local_peerlist, time_t local_time, int64_t& delta); |
| 135 | + |
| 136 | + bool connections_maker(); |
| 137 | + bool peer_sync_idle_maker(); |
| 138 | + bool do_handshake_with_peer(peerid_type& pi, p2p_connection_context& context, bool just_take_peerlist = false); |
| 139 | + bool do_peer_timed_sync(const net_utils::connection_context_base& context, peerid_type peer_id); |
| 140 | + |
| 141 | + bool make_new_connection_from_peerlist(bool use_white_list); |
| 142 | + bool try_to_connect_and_handshake_with_new_peer(const net_address& na, bool just_take_peerlist = false, uint64_t last_seen_stamp = 0, bool white = true); |
| 143 | + size_t get_random_index_with_fixed_probability(size_t max_index); |
| 144 | + bool is_peer_used(const peerlist_entry& peer); |
| 145 | + bool is_addr_connected(const net_address& peer); |
| 146 | + template<class t_callback> |
| 147 | + bool try_ping(basic_node_data& node_data, p2p_connection_context& context, t_callback cb); |
| 148 | + bool make_expected_connections_count(bool white_list, size_t expected_connections); |
| 149 | + |
| 150 | + //debug functions |
| 151 | + std::string print_connections_container(); |
| 152 | + |
| 153 | + |
| 154 | + typedef net_utils::boosted_tcp_server<levin::async_protocol_handler<p2p_connection_context> > net_server; |
| 155 | + |
| 156 | + struct config |
| 157 | + { |
| 158 | + network_config m_net_config; |
| 159 | + uint64_t m_peer_id; |
| 160 | + |
| 161 | + BEGIN_KV_SERIALIZE_MAP() |
| 162 | + KV_SERIALIZE(m_net_config) |
| 163 | + KV_SERIALIZE(m_peer_id) |
| 164 | + END_KV_SERIALIZE_MAP() |
| 165 | + }; |
| 166 | + |
| 167 | + config m_config; |
| 168 | + std::string m_config_folder; |
| 169 | + |
| 170 | + bool m_have_address; |
| 171 | + bool m_first_connection_maker_call; |
| 172 | + uint32_t m_listenning_port; |
| 173 | + uint32_t m_external_port; |
| 174 | + uint32_t m_ip_address; |
| 175 | + bool m_allow_local_ip; |
| 176 | + bool m_hide_my_port; |
| 177 | + |
| 178 | + //critical_section m_connections_lock; |
| 179 | + //connections_indexed_container m_connections; |
| 180 | + |
| 181 | + t_payload_net_handler& m_payload_handler; |
| 182 | + peerlist_manager m_peerlist; |
| 183 | + |
| 184 | + math_helper::once_a_time_seconds<P2P_DEFAULT_HANDSHAKE_INTERVAL> m_peer_handshake_idle_maker_interval; |
| 185 | + math_helper::once_a_time_seconds<1> m_connections_maker_interval; |
| 186 | + math_helper::once_a_time_seconds<60*30, false> m_peerlist_store_interval; |
| 187 | + |
| 188 | + std::string m_bind_ip; |
| 189 | + std::string m_port; |
| 190 | +#ifdef ALLOW_DEBUG_COMMANDS |
| 191 | + uint64_t m_last_stat_request_time; |
| 192 | +#endif |
| 193 | + std::list<net_address> m_priority_peers; |
| 194 | + std::vector<net_address> m_seed_nodes; |
| 195 | + std::list<nodetool::peerlist_entry> m_command_line_peers; |
| 196 | + uint64_t m_peer_livetime; |
| 197 | + //keep connections to initiate some interactions |
| 198 | + net_server m_net_server; |
| 199 | + }; |
| 200 | +} |
| 201 | + |
| 202 | +#include "net_node.inl" |
| 203 | + |
| 204 | +POP_WARNINGS |
0 commit comments