Debian Codegraph @

src/p2p/net_peerlist_boost_serialization.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 <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"
39 #include "p2p/p2p_protocol_defs.h"
40
41 BOOST_CLASS_VERSION(nodetool::peerlist_entry, 3)
42
43 namespace 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;
70 switch (epee::net_utils::address_type(type))
71 {
72 case epee::net_utils::ipv4_network_address::get_type_id():
73 do_serialize<epee::net_utils::ipv4_network_address>(is_saving, a, na);
74 break;
75 case epee::net_utils::ipv6_network_address::get_type_id():
76 do_serialize<epee::net_utils::ipv6_network_address>(is_saving, a, na);
77 break;
78 case net::tor_address::get_type_id():
79 do_serialize<net::tor_address>(is_saving, a, na);
80 break;
81 case net::i2p_address::get_type_id():
82 do_serialize<net::i2p_address>(is_saving, a, na);
83 break;
84 case epee::net_utils::address_type::invalid:
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())
99 na = epee::net_utils::ipv4_network_address{ip, port};
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())
126 na = epee::net_utils::ipv6_network_address{ip, port};
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)
175 na = net::tor_address::unknown();
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)
197 na = net::i2p_address::unknown();
198 else
199 na = MONERO_UNWRAP(net::i2p_address::make(host));
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())
237 pl.rpc_credits_per_hash = 0;
238 return;
239 }
240 a & pl.rpc_credits_per_hash;
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 }