1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// Copyright (c) 2014-2023, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
#include <cstring>
#include "common/expect.h"
#include "net/net_utils_base.h"
#include "net/tor_address.h"
#include "net/i2p_address.h"
#include "p2p/p2p_protocol_defs.h"
BOOST_CLASS_VERSION(nodetool::peerlist_entry, 3)
namespace boost
{
namespace serialization
{
template <class T, class Archive>
inline void do_serialize(boost::mpl::false_, Archive &a, epee::net_utils::network_address& na)
{
T addr{};
a & addr;
na = std::move(addr);
}
template <class T, class Archive>
inline void do_serialize(boost::mpl::true_, Archive &a, const epee::net_utils::network_address& na)
{
a & na.as<T>();
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, epee::net_utils::network_address& na, const ver_type ver)
{
static constexpr const typename Archive::is_saving is_saving{};
uint8_t type;
if (is_saving)
type = uint8_t(na.get_type_id());
a & type;
switch (epee::net_utils::address_type(type))
{
case epee::net_utils::ipv4_network_address::get_type_id():
do_serialize<epee::net_utils::ipv4_network_address>(is_saving, a, na);
break;
case epee::net_utils::ipv6_network_address::get_type_id():
do_serialize<epee::net_utils::ipv6_network_address>(is_saving, a, na);
break;
case net::tor_address::get_type_id():
do_serialize<net::tor_address>(is_saving, a, na);
break;
case net::i2p_address::get_type_id():
do_serialize<net::i2p_address>(is_saving, a, na);
break;
case epee::net_utils::address_type::invalid:
default:
throw std::runtime_error("Unsupported network address type");
}
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, epee::net_utils::ipv4_network_address& na, const ver_type ver)
{
uint32_t ip{na.ip()};
uint16_t port{na.port()};
ip = SWAP32LE(ip);
a & ip;
ip = SWAP32LE(ip);
a & port;
if (!typename Archive::is_saving())
na = epee::net_utils::ipv4_network_address{ip, port};
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, boost::asio::ip::address_v6& v6, const ver_type ver)
{
if (typename Archive::is_saving())
{
auto bytes = v6.to_bytes();
for (auto &e: bytes) a & e;
}
else
{
boost::asio::ip::address_v6::bytes_type bytes;
for (auto &e: bytes) a & e;
v6 = boost::asio::ip::address_v6(bytes);
}
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, epee::net_utils::ipv6_network_address& na, const ver_type ver)
{
boost::asio::ip::address_v6 ip{na.ip()};
uint16_t port{na.port()};
a & ip;
a & port;
if (!typename Archive::is_saving())
na = epee::net_utils::ipv6_network_address{ip, port};
}
template <class Archive, class ver_type>
inline void save(Archive& a, const net::tor_address& na, const ver_type)
{
const size_t length = std::strlen(na.host_str());
if (length > 255)
MONERO_THROW(net::error::invalid_tor_address, "Tor address too long");
const uint16_t port{na.port()};
const uint8_t len = length;
a & port;
a & len;
a.save_binary(na.host_str(), length);
}
template <class Archive, class ver_type>
inline void save(Archive& a, const net::i2p_address& na, const ver_type)
{
const size_t length = std::strlen(na.host_str());
if (length > 255)
MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
const uint16_t port{na.port()};
const uint8_t len = length;
a & port;
a & len;
a.save_binary(na.host_str(), length);
}
template <class Archive, class ver_type>
inline void load(Archive& a, net::tor_address& na, const ver_type)
{
uint16_t port = 0;
uint8_t length = 0;
a & port;
a & length;
const size_t buffer_size = net::tor_address::buffer_size();
if (length > buffer_size)
MONERO_THROW(net::error::invalid_tor_address, "Tor address too long");
char host[buffer_size] = {0};
a.load_binary(host, length);
host[sizeof(host) - 1] = 0;
if (std::strcmp(host, net::tor_address::unknown_str()) == 0)
na = net::tor_address::unknown();
else
na = MONERO_UNWRAP(net::tor_address::make(host, port));
}
template <class Archive, class ver_type>
inline void load(Archive& a, net::i2p_address& na, const ver_type)
{
uint16_t port = 0;
uint8_t length = 0;
a & port;
a & length;
const size_t buffer_size = net::i2p_address::buffer_size();
if (length > buffer_size)
MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
char host[buffer_size] = {0};
a.load_binary(host, length);
host[sizeof(host) - 1] = 0;
if (std::strcmp(host, net::i2p_address::unknown_str()) == 0)
na = net::i2p_address::unknown();
else
na = MONERO_UNWRAP(net::i2p_address::make(host, port));
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, net::tor_address& na, const ver_type ver)
{
boost::serialization::split_free(a, na, ver);
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, net::i2p_address& na, const ver_type ver)
{
boost::serialization::split_free(a, na, ver);
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, nodetool::peerlist_entry& pl, const ver_type ver)
{
a & pl.adr;
a & pl.id;
a & pl.last_seen;
if (ver < 1)
{
if (!typename Archive::is_saving())
pl.pruning_seed = 0;
return;
}
a & pl.pruning_seed;
if (ver < 2)
{
if (!typename Archive::is_saving())
pl.rpc_port = 0;
return;
}
a & pl.rpc_port;
if (ver < 3)
{
if (!typename Archive::is_saving())
pl.rpc_credits_per_hash = 0;
return;
}
a & pl.rpc_credits_per_hash;
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, nodetool::anchor_peerlist_entry& pl, const ver_type ver)
{
a & pl.adr;
a & pl.id;
a & pl.first_seen;
}
}
}
|