Debian Codegraph @

src/p2p/p2p_protocol_defs.h

raw symbols SCIP
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>
36 #include "serialization/keyvalue_serialization.h"
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"
43 #include "serialization/serialization.h"
44 #include "cryptonote_config.h"
45
46 namespace nodetool
47 {
48 typedef boost::uuids::uuid uuid;
49 typedef uint64_t peerid_type;
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
60 struct network_address_old
61 {
62 uint32_t ip;
63 uint32_t port;
64
65 BEGIN_KV_SERIALIZE_MAP()
66 KV_SERIALIZE(ip)
67 KV_SERIALIZE(port)
68 END_KV_SERIALIZE_MAP()
69 };
70
71 template<typename AddressType>
72 struct peerlist_entry_base
73 {
74 AddressType adr;
75 peerid_type id;
76 int64_t last_seen;
77 uint32_t pruning_seed;
78 uint16_t rpc_port;
79 uint32_t rpc_credits_per_hash;
80
81 BEGIN_KV_SERIALIZE_MAP()
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)
88 END_KV_SERIALIZE_MAP()
89
90 BEGIN_SERIALIZE()
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)
97 END_SERIALIZE()
98 };
99 typedef peerlist_entry_base<epee::net_utils::network_address> peerlist_entry;
100
101 template<typename AddressType>
102 struct anchor_peerlist_entry_base
103 {
104 AddressType adr;
105 peerid_type id;
106 int64_t first_seen;
107
108 BEGIN_KV_SERIALIZE_MAP()
109 KV_SERIALIZE(adr)
110 KV_SERIALIZE(id)
111 KV_SERIALIZE(first_seen)
112 END_KV_SERIALIZE_MAP()
113
114 BEGIN_SERIALIZE()
115 FIELD(adr)
116 FIELD(id)
117 VARINT_FIELD(first_seen)
118 END_SERIALIZE()
119 };
120 typedef anchor_peerlist_entry_base<epee::net_utils::network_address> anchor_peerlist_entry;
121
122 template<typename AddressType>
123 struct connection_entry_base
124 {
125 AddressType adr;
126 peerid_type id;
127 bool is_income;
128
129 BEGIN_KV_SERIALIZE_MAP()
130 KV_SERIALIZE(adr)
131 KV_SERIALIZE(id)
132 KV_SERIALIZE(is_income)
133 END_KV_SERIALIZE_MAP()
134
135 BEGIN_SERIALIZE()
136 FIELD(adr)
137 FIELD(id)
138 FIELD(is_income)
139 END_SERIALIZE()
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
165 struct network_config
166 {
167 BEGIN_KV_SERIALIZE_MAP()
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)
173 END_KV_SERIALIZE_MAP()
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
185 struct basic_node_data
186 {
187 uuid network_id;
188 uint32_t my_port;
189 uint16_t rpc_port;
190 uint32_t rpc_credits_per_hash;
191 peerid_type peer_id;
192 uint32_t support_flags;
193
194 BEGIN_KV_SERIALIZE_MAP()
195 KV_SERIALIZE_VAL_POD_AS_BLOB(network_id)
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)
201 END_KV_SERIALIZE_MAP()
202 };
203
204
205 #define P2P_COMMANDS_POOL_BASE 1000
206
207 /************************************************************************/
208 /* */
209 /************************************************************************/
210 template<class t_playload_type>
211 struct COMMAND_HANDSHAKE_T
212 {
213 const static int ID = P2P_COMMANDS_POOL_BASE + 1;
214
215 struct request_t
216 {
217 basic_node_data node_data;
218 t_playload_type payload_data;
219
220 BEGIN_KV_SERIALIZE_MAP()
221 KV_SERIALIZE(node_data)
222 KV_SERIALIZE(payload_data)
223 END_KV_SERIALIZE_MAP()
224 };
225 typedef epee::misc_utils::struct_init<request_t> request;
226
227 struct response_t
228 {
229 basic_node_data node_data;
230 t_playload_type payload_data;
231 std::vector<peerlist_entry> local_peerlist_new;
232
233 BEGIN_KV_SERIALIZE_MAP()
234 KV_SERIALIZE(node_data)
235 KV_SERIALIZE(payload_data)
236 KV_SERIALIZE(local_peerlist_new)
237 END_KV_SERIALIZE_MAP()
238 };
239 typedef epee::misc_utils::struct_init<response_t> response;
240 };
241
242
243 /************************************************************************/
244 /* */
245 /************************************************************************/
246 template<class t_playload_type>
247 struct COMMAND_TIMED_SYNC_T
248 {
249 const static int ID = P2P_COMMANDS_POOL_BASE + 2;
250
251 struct request_t
252 {
253 t_playload_type payload_data;
254 BEGIN_KV_SERIALIZE_MAP()
255 KV_SERIALIZE(payload_data)
256 END_KV_SERIALIZE_MAP()
257 };
258 typedef epee::misc_utils::struct_init<request_t> request;
259
260 struct response_t
261 {
262 t_playload_type payload_data;
263 std::vector<peerlist_entry> local_peerlist_new;
264
265 BEGIN_KV_SERIALIZE_MAP()
266 KV_SERIALIZE(payload_data)
267 KV_SERIALIZE(local_peerlist_new)
268 END_KV_SERIALIZE_MAP()
269 };
270 typedef epee::misc_utils::struct_init<response_t> response;
271 };
272
273 /************************************************************************/
274 /* */
275 /************************************************************************/
276
277 struct COMMAND_PING
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
288 struct request_t
289 {
290 /*actually we don't need to send any real data*/
291
292 BEGIN_KV_SERIALIZE_MAP()
293 END_KV_SERIALIZE_MAP()
294 };
295 typedef epee::misc_utils::struct_init<request_t> request;
296
297 struct response_t
298 {
299 std::string status;
300 peerid_type peer_id;
301
302 BEGIN_KV_SERIALIZE_MAP()
303 KV_SERIALIZE(status)
304 KV_SERIALIZE(peer_id)
305 END_KV_SERIALIZE_MAP()
306 };
307 typedef epee::misc_utils::struct_init<response_t> response;
308 };
309
310
311 /************************************************************************/
312 /* */
313 /************************************************************************/
314 struct COMMAND_REQUEST_SUPPORT_FLAGS
315 {
316 const static int ID = P2P_COMMANDS_POOL_BASE + 7;
317
318 struct request_t
319 {
320 BEGIN_KV_SERIALIZE_MAP()
321 END_KV_SERIALIZE_MAP()
322 };
323 typedef epee::misc_utils::struct_init<request_t> request;
324
325 struct response_t
326 {
327 uint32_t support_flags;
328
329 BEGIN_KV_SERIALIZE_MAP()
330 KV_SERIALIZE(support_flags)
331 END_KV_SERIALIZE_MAP()
332 };
333 typedef epee::misc_utils::struct_init<response_t> response;
334 };
335 }