Monero
Loading...
Searching...
No Matches
net_peerlist_boost_serialization.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 <cstring>
34
35#include "common/expect.h"
36#include "net/net_utils_base.h"
37#include "net/tor_address.h"
38#include "net/i2p_address.h"
40
42
43namespace boost
44{
45 namespace serialization
46 {
47 template <class T, class Archive>
48 inline void do_serialize(boost::mpl::false_, Archive &a, epee::net_utils::network_address& na)
49 {
50 T addr{};
51 a & addr;
52 na = std::move(addr);
53 }
54
55 template <class T, class Archive>
56 inline void do_serialize(boost::mpl::true_, Archive &a, const epee::net_utils::network_address& na)
57 {
58 a & na.as<T>();
59 }
60
61 template <class Archive, class ver_type>
62 inline void serialize(Archive &a, epee::net_utils::network_address& na, const ver_type ver)
63 {
64 static constexpr const typename Archive::is_saving is_saving{};
65
66 uint8_t type;
67 if (is_saving)
68 type = uint8_t(na.get_type_id());
69 a & type;
71 {
74 break;
77 break;
79 do_serialize<net::tor_address>(is_saving, a, na);
80 break;
82 do_serialize<net::i2p_address>(is_saving, a, na);
83 break;
85 default:
86 throw std::runtime_error("Unsupported network address type");
87 }
88 }
89 template <class Archive, class ver_type>
90 inline void serialize(Archive &a, epee::net_utils::ipv4_network_address& na, const ver_type ver)
91 {
92 uint32_t ip{na.ip()};
93 uint16_t port{na.port()};
94 ip = SWAP32LE(ip);
95 a & ip;
96 ip = SWAP32LE(ip);
97 a & port;
98 if (!typename Archive::is_saving())
100 }
101
102 template <class Archive, class ver_type>
103 inline void serialize(Archive &a, boost::asio::ip::address_v6& v6, const ver_type ver)
104 {
105 if (typename Archive::is_saving())
106 {
107 auto bytes = v6.to_bytes();
108 for (auto &e: bytes) a & e;
109 }
110 else
111 {
112 boost::asio::ip::address_v6::bytes_type bytes;
113 for (auto &e: bytes) a & e;
114 v6 = boost::asio::ip::address_v6(bytes);
115 }
116 }
117
118 template <class Archive, class ver_type>
119 inline void serialize(Archive &a, epee::net_utils::ipv6_network_address& na, const ver_type ver)
120 {
121 boost::asio::ip::address_v6 ip{na.ip()};
122 uint16_t port{na.port()};
123 a & ip;
124 a & port;
125 if (!typename Archive::is_saving())
127 }
128
129
130 template <class Archive, class ver_type>
131 inline void save(Archive& a, const net::tor_address& na, const ver_type)
132 {
133 const size_t length = std::strlen(na.host_str());
134 if (length > 255)
135 MONERO_THROW(net::error::invalid_tor_address, "Tor address too long");
136
137 const uint16_t port{na.port()};
138 const uint8_t len = length;
139 a & port;
140 a & len;
141 a.save_binary(na.host_str(), length);
142 }
143
144 template <class Archive, class ver_type>
145 inline void save(Archive& a, const net::i2p_address& na, const ver_type)
146 {
147 const size_t length = std::strlen(na.host_str());
148 if (length > 255)
149 MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
150
151 const uint16_t port{na.port()};
152 const uint8_t len = length;
153 a & port;
154 a & len;
155 a.save_binary(na.host_str(), length);
156 }
157
158 template <class Archive, class ver_type>
159 inline void load(Archive& a, net::tor_address& na, const ver_type)
160 {
161 uint16_t port = 0;
162 uint8_t length = 0;
163 a & port;
164 a & length;
165
166 const size_t buffer_size = net::tor_address::buffer_size();
167 if (length > buffer_size)
168 MONERO_THROW(net::error::invalid_tor_address, "Tor address too long");
169
170 char host[buffer_size] = {0};
171 a.load_binary(host, length);
172 host[sizeof(host) - 1] = 0;
173
174 if (std::strcmp(host, net::tor_address::unknown_str()) == 0)
176 else
177 na = MONERO_UNWRAP(net::tor_address::make(host, port));
178 }
179
180 template <class Archive, class ver_type>
181 inline void load(Archive& a, net::i2p_address& na, const ver_type)
182 {
183 uint16_t port = 0;
184 uint8_t length = 0;
185 a & port;
186 a & length;
187
188 const size_t buffer_size = net::i2p_address::buffer_size();
189 if (length > buffer_size)
190 MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
191
192 char host[buffer_size] = {0};
193 a.load_binary(host, length);
194 host[sizeof(host) - 1] = 0;
195
196 if (std::strcmp(host, net::i2p_address::unknown_str()) == 0)
198 else
200 }
201
202 template <class Archive, class ver_type>
203 inline void serialize(Archive &a, net::tor_address& na, const ver_type ver)
204 {
205 boost::serialization::split_free(a, na, ver);
206 }
207
208 template <class Archive, class ver_type>
209 inline void serialize(Archive &a, net::i2p_address& na, const ver_type ver)
210 {
211 boost::serialization::split_free(a, na, ver);
212 }
213
214 template <class Archive, class ver_type>
215 inline void serialize(Archive &a, nodetool::peerlist_entry& pl, const ver_type ver)
216 {
217 a & pl.adr;
218 a & pl.id;
219 a & pl.last_seen;
220 if (ver < 1)
221 {
222 if (!typename Archive::is_saving())
223 pl.pruning_seed = 0;
224 return;
225 }
226 a & pl.pruning_seed;
227 if (ver < 2)
228 {
229 if (!typename Archive::is_saving())
230 pl.rpc_port = 0;
231 return;
232 }
233 a & pl.rpc_port;
234 if (ver < 3)
235 {
236 if (!typename Archive::is_saving())
238 return;
239 }
241 }
242
243 template <class Archive, class ver_type>
244 inline void serialize(Archive &a, nodetool::anchor_peerlist_entry& pl, const ver_type ver)
245 {
246 a & pl.adr;
247 a & pl.id;
248 a & pl.first_seen;
249 }
250 }
251}
Definition net_utils_base.h:69
static constexpr address_type get_type_id() noexcept
Definition net_utils_base.h:92
constexpr uint16_t port() const noexcept
Definition net_utils_base.h:87
constexpr uint32_t ip() const noexcept
Definition net_utils_base.h:86
Definition net_utils_base.h:172
static constexpr address_type get_type_id() noexcept
Definition net_utils_base.h:198
uint16_t port() const noexcept
Definition net_utils_base.h:193
boost::asio::ip::address_v6 ip() const noexcept
Definition net_utils_base.h:192
Definition net_utils_base.h:225
address_type get_type_id() const
Definition net_utils_base.h:316
const Type & as() const
Definition net_utils_base.h:320
b32 i2p address; internal format not condensed/decoded.
Definition i2p_address.h:52
std::uint16_t port() const noexcept
Definition i2p_address.h:106
static i2p_address unknown() noexcept
Definition i2p_address.h:69
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition i2p_address.h:111
static constexpr std::size_t buffer_size() noexcept
Definition i2p_address.h:60
static expect< i2p_address > make(boost::string_ref address)
Definition i2p_address.cpp:105
const char * host_str() const noexcept
Definition i2p_address.h:103
static const char * unknown_str() noexcept
Definition i2p_address.cpp:93
Tor onion address; internal format not condensed/decoded.
Definition tor_address.h:53
static expect< tor_address > make(boost::string_ref address, std::uint16_t default_port=0)
Definition tor_address.cpp:109
static constexpr std::size_t buffer_size() noexcept
Definition tor_address.h:62
static const char * unknown_str() noexcept
Definition tor_address.cpp:96
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition tor_address.h:113
const char * host_str() const noexcept
Definition tor_address.h:105
std::uint16_t port() const noexcept
Definition tor_address.h:108
static tor_address unknown() noexcept
Definition tor_address.h:71
bool do_serialize(Archive< false > &ar, std::vector< T > &v)
Definition containers.h:109
#define MONERO_THROW(code, msg)
Definition expect.h:67
#define MONERO_UNWRAP(...)
Definition expect.h:61
#define SWAP32LE
Definition int-util.h:277
void load(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition unordered_containers_boost_serialization.h:54
void save(Archive &a, const std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition unordered_containers_boost_serialization.h:42
Definition portable_binary_archive.hpp:29
address_type
Definition enums.h:40
@ invalid
Definition enums.h:42
@ invalid_tor_address
Invalid base32 or length.
Definition error.h:48
@ invalid_i2p_address
Definition error.h:45
anchor_peerlist_entry_base< epee::net_utils::network_address > anchor_peerlist_entry
Definition p2p_protocol_defs.h:120
peerlist_entry_base< epee::net_utils::network_address > peerlist_entry
Definition p2p_protocol_defs.h:99
Definition binary_utils.h:36
bool serialize(Archive &ar, T &v)
Definition serialization.h:294
BOOST_CLASS_VERSION(nodetool::peerlist_types, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
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
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
#define T(x)