Electroneum
net_peerlist_boost_serialization.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <cstring>
35 
36 #include "common/expect.h"
37 #include "net/net_utils_base.h"
38 #include "net/tor_address.h"
39 #include "net/i2p_address.h"
40 #include "p2p/p2p_protocol_defs.h"
41 
42 #ifdef CRYPTONOTE_PRUNING_DEBUG_SPOOF_SEED
43 #include "common/pruning.h"
44 #endif
45 
47 
48 namespace boost
49 {
50  namespace serialization
51  {
52  template <class T, class Archive>
53  inline void do_serialize(boost::mpl::false_, Archive &a, epee::net_utils::network_address& na)
54  {
55  T addr{};
56  a & addr;
57  na = std::move(addr);
58  }
59 
60  template <class T, class Archive>
61  inline void do_serialize(boost::mpl::true_, Archive &a, const epee::net_utils::network_address& na)
62  {
63  a & na.as<T>();
64  }
65 
66  template <class Archive, class ver_type>
67  inline void serialize(Archive &a, epee::net_utils::network_address& na, const ver_type ver)
68  {
69  static constexpr const typename Archive::is_saving is_saving{};
70 
71  uint8_t type;
72  if (is_saving)
73  type = uint8_t(na.get_type_id());
74  a & type;
75  switch (epee::net_utils::address_type(type))
76  {
78  do_serialize<epee::net_utils::ipv4_network_address>(is_saving, a, na);
79  break;
81  do_serialize<net::tor_address>(is_saving, a, na);
82  break;
84  do_serialize<net::i2p_address>(is_saving, a, na);
85  break;
87  default:
88  throw std::runtime_error("Unsupported network address type");
89  }
90  }
91  template <class Archive, class ver_type>
92  inline void serialize(Archive &a, epee::net_utils::ipv4_network_address& na, const ver_type ver)
93  {
94  uint32_t ip{na.ip()};
95  uint16_t port{na.port()};
96  a & ip;
97  a & port;
98  if (!typename Archive::is_saving())
100  }
101 
102  template <class Archive, class ver_type>
103  inline void save(Archive& a, const net::tor_address& na, const ver_type)
104  {
105  const size_t length = std::strlen(na.host_str());
106  if (length > 255)
107  ELECTRONEUM_THROW(net::error::invalid_tor_address, "Tor address too long");
108 
109  const uint16_t port{na.port()};
110  const uint8_t len = length;
111  a & port;
112  a & len;
113  a.save_binary(na.host_str(), length);
114  }
115 
116  template <class Archive, class ver_type>
117  inline void save(Archive& a, const net::i2p_address& na, const ver_type)
118  {
119  const size_t length = std::strlen(na.host_str());
120  if (length > 255)
121  ELECTRONEUM_THROW(net::error::invalid_i2p_address, "i2p address too long");
122 
123  const uint16_t port{na.port()};
124  const uint8_t len = length;
125  a & port;
126  a & len;
127  a.save_binary(na.host_str(), length);
128  }
129 
130  template <class Archive, class ver_type>
131  inline void load(Archive& a, net::tor_address& na, const ver_type)
132  {
133  uint16_t port = 0;
134  uint8_t length = 0;
135  a & port;
136  a & length;
137 
138  const size_t buffer_size = net::tor_address::buffer_size();
139  if (length > buffer_size)
140  ELECTRONEUM_THROW(net::error::invalid_tor_address, "Tor address too long");
141 
142  char host[buffer_size] = {0};
143  a.load_binary(host, length);
144  host[sizeof(host) - 1] = 0;
145 
146  if (std::strcmp(host, net::tor_address::unknown_str()) == 0)
148  else
150  }
151 
152  template <class Archive, class ver_type>
153  inline void load(Archive& a, net::i2p_address& na, const ver_type)
154  {
155  uint16_t port = 0;
156  uint8_t length = 0;
157  a & port;
158  a & length;
159 
160  const size_t buffer_size = net::i2p_address::buffer_size();
161  if (length > buffer_size)
162  ELECTRONEUM_THROW(net::error::invalid_i2p_address, "i2p address too long");
163 
164  char host[buffer_size] = {0};
165  a.load_binary(host, length);
166  host[sizeof(host) - 1] = 0;
167 
168  if (std::strcmp(host, net::i2p_address::unknown_str()) == 0)
170  else
172  }
173 
174  template <class Archive, class ver_type>
175  inline void serialize(Archive &a, net::tor_address& na, const ver_type ver)
176  {
177  boost::serialization::split_free(a, na, ver);
178  }
179 
180  template <class Archive, class ver_type>
181  inline void serialize(Archive &a, net::i2p_address& na, const ver_type ver)
182  {
183  boost::serialization::split_free(a, na, ver);
184  }
185 
186  template <class Archive, class ver_type>
187  inline void serialize(Archive &a, nodetool::peerlist_entry& pl, const ver_type ver)
188  {
189  a & pl.adr;
190  a & pl.id;
191  a & pl.last_seen;
192  if (ver < 1)
193  {
194  if (!typename Archive::is_saving())
195  pl.pruning_seed = 0;
196  return;
197  }
198  a & pl.pruning_seed;
199 #ifdef CRYPTONOTE_PRUNING_DEBUG_SPOOF_SEED
200  if (!typename Archive::is_saving())
201  {
203  }
204 #endif
205  if (ver < 2)
206  {
207  if (!typename Archive::is_saving())
208  pl.rpc_port = 0;
209  return;
210  }
211  a & pl.rpc_port;
212  }
213 
214  template <class Archive, class ver_type>
215  inline void serialize(Archive &a, nodetool::anchor_peerlist_entry& pl, const ver_type ver)
216  {
217  a & pl.adr;
218  a & pl.id;
219  a & pl.first_seen;
220  }
221  }
222 }
int64_t last_seen
static constexpr std::size_t buffer_size() noexcept
Definition: tor_address.h:61
const uint32_t T[512]
constexpr uint16_t port() const noexcept
b32 i2p address; internal format not condensed/decoded.
Definition: i2p_address.h:51
uint16_t rpc_port
AddressType adr
static const char * unknown_str() noexcept
Definition: tor_address.cpp:95
static const char * unknown_str() noexcept
Definition: i2p_address.cpp:94
#define CRYPTONOTE_PRUNING_LOG_STRIPES
static i2p_address unknown() noexcept
Definition: i2p_address.h:70
AddressType adr
uint32_t pruning_seed
unsigned short uint16_t
Definition: stdint.h:125
static tor_address unknown() noexcept
Definition: tor_address.h:70
unsigned char uint8_t
Definition: stdint.h:124
void save(Archive &a, const net::i2p_address &na, const ver_type)
int64_t first_seen
const char * host_str() const noexcept
Definition: i2p_address.h:104
peerid_type id
static constexpr std::size_t buffer_size() noexcept
Definition: i2p_address.h:61
address_type get_type_id() const
bool serialize(Archive &ar, T &v)
constexpr uint32_t ip() const noexcept
unsigned int uint32_t
Definition: stdint.h:126
peerid_type id
static constexpr address_type get_type_id() noexcept
BOOST_CLASS_VERSION(nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< tests::proxy_core > >, 1)
const char * host_str() const noexcept
Definition: tor_address.h:104
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition: tor_address.h:112
std::uint16_t port() const noexcept
Definition: tor_address.h:107
boost::endian::big_uint32_t ip
Definition: socks.cpp:61
boost::endian::big_uint16_t port
Definition: socks.cpp:60
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
#define ELECTRONEUM_UNWRAP(...)
Definition: expect.h:60
const T & move(const T &t)
Definition: gtest-port.h:1317
Tor onion address; internal format not condensed/decoded.
Definition: tor_address.h:51
Invalid base32 or length.
uint32_t make_pruning_seed(uint32_t stripe, uint32_t log_stripes)
Definition: pruning.cpp:37
static expect< i2p_address > make(boost::string_ref address, std::uint16_t default_port=0)
static constexpr epee::net_utils::address_type get_type_id() noexcept
Definition: i2p_address.h:112
#define ELECTRONEUM_THROW(code, msg)
Definition: expect.h:66
void do_serialize(boost::mpl::true_, Archive &a, const epee::net_utils::network_address &na)
static expect< tor_address > make(boost::string_ref address, std::uint16_t default_port=0)
std::uint16_t port() const noexcept
Definition: i2p_address.h:107
void load(Archive &a, net::i2p_address &na, const ver_type)