Monero
Loading...
Searching...
No Matches
p2p_protocol_defs.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31#pragma once
32
33#include <iomanip>
34#include <boost/uuid/uuid.hpp>
35#include <boost/serialization/version.hpp>
37#include "net/net_utils_base.h"
38#include "net/tor_address.h" // needed for serialization
39#include "net/i2p_address.h" // needed for serialization
40#include "misc_language.h"
41#include "string_tools.h"
42#include "time_helper.h"
44#include "cryptonote_config.h"
45
46namespace nodetool
47{
48 typedef boost::uuids::uuid uuid;
50
51 static inline std::string peerid_to_string(peerid_type peer_id)
52 {
53 std::ostringstream s;
54 s << std::hex << peer_id;
55 return epee::string_tools::pad_string(s.str(), 16, '0', true);
56 }
57
58#pragma pack (push, 1)
59
61 {
64
69 };
70
71 template<typename AddressType>
73 {
74 AddressType adr;
80
82 KV_SERIALIZE(adr)
83 KV_SERIALIZE(id)
84 KV_SERIALIZE_OPT(last_seen, (int64_t)0)
85 KV_SERIALIZE_OPT(pruning_seed, (uint32_t)0)
86 KV_SERIALIZE_OPT(rpc_port, (uint16_t)0)
87 KV_SERIALIZE_OPT(rpc_credits_per_hash, (uint32_t)0)
89
91 FIELD(adr)
92 FIELD(id)
93 VARINT_FIELD(last_seen)
94 VARINT_FIELD(pruning_seed)
95 VARINT_FIELD(rpc_port)
96 VARINT_FIELD(rpc_credits_per_hash)
98 };
99 typedef peerlist_entry_base<epee::net_utils::network_address> peerlist_entry;
100
101 template<typename AddressType>
103 {
104 AddressType adr;
107
109 KV_SERIALIZE(adr)
110 KV_SERIALIZE(id)
111 KV_SERIALIZE(first_seen)
113
115 FIELD(adr)
116 FIELD(id)
117 VARINT_FIELD(first_seen)
119 };
120 typedef anchor_peerlist_entry_base<epee::net_utils::network_address> anchor_peerlist_entry;
121
122 template<typename AddressType>
124 {
125 AddressType adr;
128
130 KV_SERIALIZE(adr)
131 KV_SERIALIZE(id)
132 KV_SERIALIZE(is_income)
134
136 FIELD(adr)
137 FIELD(id)
138 FIELD(is_income)
140 };
141 typedef connection_entry_base<epee::net_utils::network_address> connection_entry;
142
143#pragma pack(pop)
144
145 inline
146 std::string print_peerlist_to_string(const std::vector<peerlist_entry>& pl)
147 {
148 time_t now_time = 0;
149 time(&now_time);
150 std::stringstream ss;
151 ss << std::setfill ('0') << std::setw (8) << std::hex << std::noshowbase;
152 for(const peerlist_entry& pe: pl)
153 {
154 ss << peerid_to_string(pe.id) << "\t" << pe.adr.str()
155 << " \trpc port " << (pe.rpc_port > 0 ? std::to_string(pe.rpc_port) : "-")
156 << " \trpc credits per hash " << (pe.rpc_credits_per_hash > 0 ? std::to_string(pe.rpc_credits_per_hash) : "-")
157 << " \tpruning seed " << pe.pruning_seed
158 << " \tlast_seen: " << (pe.last_seen == 0 ? std::string("never") : epee::misc_utils::get_time_interval_string(now_time - pe.last_seen))
159 << std::endl;
160 }
161 return ss.str();
162 }
163
164
166 {
168 KV_SERIALIZE(max_out_connection_count)
169 KV_SERIALIZE(max_in_connection_count)
170 KV_SERIALIZE(handshake_interval)
171 KV_SERIALIZE(packet_max_size)
172 KV_SERIALIZE(config_id)
174
175 uint32_t max_out_connection_count;
176 uint32_t max_in_connection_count;
177 uint32_t connection_timeout;
178 uint32_t ping_connection_timeout;
179 uint32_t handshake_interval;
180 uint32_t packet_max_size;
181 uint32_t config_id;
182 uint32_t send_peerlist_sz;
183 };
184
186 {
193
196 KV_SERIALIZE(peer_id)
197 KV_SERIALIZE(my_port)
198 KV_SERIALIZE_OPT(rpc_port, (uint16_t)(0))
199 KV_SERIALIZE_OPT(rpc_credits_per_hash, (uint32_t)0)
200 KV_SERIALIZE_OPT(support_flags, (uint32_t)0)
202 };
203
204
205#define P2P_COMMANDS_POOL_BASE 1000
206
207 /************************************************************************/
208 /* */
209 /************************************************************************/
210 template<class t_playload_type>
212 {
213 const static int ID = P2P_COMMANDS_POOL_BASE + 1;
214
216 {
218 t_playload_type payload_data;
219
221 KV_SERIALIZE(node_data)
222 KV_SERIALIZE(payload_data)
224 };
225 typedef epee::misc_utils::struct_init<request_t> request;
226
228 {
230 t_playload_type payload_data;
231 std::vector<peerlist_entry> local_peerlist_new;
232
234 KV_SERIALIZE(node_data)
235 KV_SERIALIZE(payload_data)
236 KV_SERIALIZE(local_peerlist_new)
238 };
239 typedef epee::misc_utils::struct_init<response_t> response;
240 };
241
242
243 /************************************************************************/
244 /* */
245 /************************************************************************/
246 template<class t_playload_type>
248 {
249 const static int ID = P2P_COMMANDS_POOL_BASE + 2;
250
252 {
253 t_playload_type payload_data;
255 KV_SERIALIZE(payload_data)
257 };
258 typedef epee::misc_utils::struct_init<request_t> request;
259
261 {
262 t_playload_type payload_data;
263 std::vector<peerlist_entry> local_peerlist_new;
264
266 KV_SERIALIZE(payload_data)
267 KV_SERIALIZE(local_peerlist_new)
269 };
270 typedef epee::misc_utils::struct_init<response_t> response;
271 };
272
273 /************************************************************************/
274 /* */
275 /************************************************************************/
276
278 {
279 /*
280 Used to make "callback" connection, to be sure that opponent node
281 have accessible connection point. Only other nodes can add peer to peerlist,
282 and ONLY in case when peer has accepted connection and answered to ping.
283 */
284 const static int ID = P2P_COMMANDS_POOL_BASE + 3;
285
286#define PING_OK_RESPONSE_STATUS_TEXT "OK"
287
289 {
290 /*actually we don't need to send any real data*/
291
294 };
296
298 {
299 std::string status;
301
303 KV_SERIALIZE(status)
304 KV_SERIALIZE(peer_id)
306 };
307 typedef epee::misc_utils::struct_init<response_t> response;
308 };
309
310
311 /************************************************************************/
312 /* */
313 /************************************************************************/
315 {
316 const static int ID = P2P_COMMANDS_POOL_BASE + 7;
317
319 {
322 };
324
326 {
328
330 KV_SERIALIZE(support_flags)
332 };
333 typedef epee::misc_utils::struct_init<response_t> response;
334 };
335}
#define s(x, c)
Definition: aesb.c:47
#define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble)
Definition: keyvalue_serialization.h:119
#define KV_SERIALIZE(varialble)
Definition: keyvalue_serialization.h:118
#define KV_SERIALIZE_OPT(variable, default_value)
Definition: keyvalue_serialization.h:123
#define END_KV_SERIALIZE_MAP()
Definition: keyvalue_serialization.h:116
#define BEGIN_KV_SERIALIZE_MAP()
Definition: keyvalue_serialization.h:43
std::string get_time_interval_string(const time_t &time_)
Definition: time_helper.h:58
std::string pad_string(std::string s, size_t n, char c=' ', bool prepend=false)
Definition: string_tools.cpp:200
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:40
Definition: levin_notify.h:52
boost::uuids::uuid uuid
Definition: net_node_common.h:48
static std::string peerid_to_string(peerid_type peer_id)
Definition: p2p_protocol_defs.h:51
uint64_t peerid_type
Definition: p2p_protocol_defs.h:49
std::string print_peerlist_to_string(const std::vector< peerlist_entry > &pl)
Definition: p2p_protocol_defs.h:146
#define P2P_COMMANDS_POOL_BASE
Definition: p2p_protocol_defs.h:205
Simple DSL AAPI based on.
#define BEGIN_SERIALIZE()
Begins the environment of the DSL \detailed for describing how to serialize an of an archive type.
Definition: serialization.h:175
#define FIELD(f)
tags the field with the variable name and then serializes it
Definition: serialization.h:237
#define END_SERIALIZE()
self-explanatory
Definition: serialization.h:208
#define VARINT_FIELD(f)
tags and serializes the varint f
Definition: serialization.h:257
unsigned short uint16_t
Definition: stdint.h:125
signed __int64 int64_t
Definition: stdint.h:135
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
Definition: misc_language.h:104
Definition: p2p_protocol_defs.h:216
t_playload_type payload_data
Definition: p2p_protocol_defs.h:218
basic_node_data node_data
Definition: p2p_protocol_defs.h:217
Definition: p2p_protocol_defs.h:228
basic_node_data node_data
Definition: p2p_protocol_defs.h:229
t_playload_type payload_data
Definition: p2p_protocol_defs.h:230
std::vector< peerlist_entry > local_peerlist_new
Definition: p2p_protocol_defs.h:231
Definition: p2p_protocol_defs.h:212
Definition: p2p_protocol_defs.h:289
Definition: p2p_protocol_defs.h:298
std::string status
Definition: p2p_protocol_defs.h:299
peerid_type peer_id
Definition: p2p_protocol_defs.h:300
Definition: p2p_protocol_defs.h:278
epee::misc_utils::struct_init< request_t > request
Definition: p2p_protocol_defs.h:295
Definition: p2p_protocol_defs.h:319
Definition: p2p_protocol_defs.h:326
uint32_t support_flags
Definition: p2p_protocol_defs.h:327
Definition: p2p_protocol_defs.h:315
epee::misc_utils::struct_init< request_t > request
Definition: p2p_protocol_defs.h:323
Definition: p2p_protocol_defs.h:252
t_playload_type payload_data
Definition: p2p_protocol_defs.h:253
Definition: p2p_protocol_defs.h:261
t_playload_type payload_data
Definition: p2p_protocol_defs.h:262
std::vector< peerlist_entry > local_peerlist_new
Definition: p2p_protocol_defs.h:263
Definition: p2p_protocol_defs.h:248
Definition: p2p_protocol_defs.h:103
int64_t first_seen
Definition: p2p_protocol_defs.h:106
AddressType adr
Definition: p2p_protocol_defs.h:104
peerid_type id
Definition: p2p_protocol_defs.h:105
Definition: p2p_protocol_defs.h:186
uint32_t support_flags
Definition: p2p_protocol_defs.h:192
uint32_t my_port
Definition: p2p_protocol_defs.h:188
peerid_type peer_id
Definition: p2p_protocol_defs.h:191
uint16_t rpc_port
Definition: p2p_protocol_defs.h:189
uint32_t rpc_credits_per_hash
Definition: p2p_protocol_defs.h:190
uuid network_id
Definition: p2p_protocol_defs.h:187
Definition: p2p_protocol_defs.h:124
bool is_income
Definition: p2p_protocol_defs.h:127
AddressType adr
Definition: p2p_protocol_defs.h:125
peerid_type id
Definition: p2p_protocol_defs.h:126
Definition: p2p_protocol_defs.h:61
uint32_t port
Definition: p2p_protocol_defs.h:63
uint32_t ip
Definition: p2p_protocol_defs.h:62
Definition: p2p_protocol_defs.h:166
Definition: p2p_protocol_defs.h:73
uint32_t pruning_seed
Definition: p2p_protocol_defs.h:77
AddressType adr
Definition: p2p_protocol_defs.h:74
uint16_t rpc_port
Definition: p2p_protocol_defs.h:78
peerid_type id
Definition: p2p_protocol_defs.h:75
int64_t last_seen
Definition: p2p_protocol_defs.h:76
uint32_t rpc_credits_per_hash
Definition: p2p_protocol_defs.h:79