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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright (c) 2018-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.
#include "net_peerlist.h"
#include <algorithm>
#include <functional>
#include <fstream>
#include <iterator>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/portable_binary_oarchive.hpp>
#include <boost/archive/portable_binary_iarchive.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/range/join.hpp>
#include <boost/serialization/version.hpp>
#include "net_peerlist_boost_serialization.h"
namespace nodetool
{
namespace
{
constexpr unsigned CURRENT_PEERLIST_STORAGE_ARCHIVE_VER = 6;
struct by_zone
{
using zone = epee::net_utils::zone;
template<typename T>
bool operator()(const T& left, const zone right) const
{
return left.adr.get_zone() < right;
}
template<typename T>
bool operator()(const zone left, const T& right) const
{
return left < right.adr.get_zone();
}
template<typename T, typename U>
bool operator()(const T& left, const U& right) const
{
return left.adr.get_zone() < right.adr.get_zone();
}
};
template<typename Elem, typename Archive>
std::vector<Elem> load_peers(Archive& a, unsigned ver)
{
// at v6, we drop existing peerlists, because annoying change
if (ver < 6)
return {};
uint64_t size = 0;
a & size;
Elem ple{};
std::vector<Elem> elems{};
elems.reserve(size);
while (size--)
{
a & ple;
elems.push_back(std::move(ple));
}
return elems;
}
template<typename Archive, typename Range>
void save_peers(Archive& a, const Range& elems)
{
const uint64_t size = elems.size();
a & size;
for (const auto& elem : elems)
a & elem;
}
template<typename T>
std::vector<T> do_take_zone(std::vector<T>& src, epee::net_utils::zone zone)
{
const auto start = std::lower_bound(src.begin(), src.end(), zone, by_zone{});
const auto end = std::upper_bound(start, src.end(), zone, by_zone{});
std::vector<T> out{};
out.assign(std::make_move_iterator(start), std::make_move_iterator(end));
src.erase(start, end);
return out;
}
template<typename Container, typename T>
void add_peers(Container& dest, std::vector<T>&& src)
{
dest.insert(std::make_move_iterator(src.begin()), std::make_move_iterator(src.end()));
}
template<typename Container, typename Range>
void copy_peers(Container& dest, const Range& src)
{
std::copy(src.begin(), src.end(), std::back_inserter(dest));
}
} // anonymous
struct peerlist_join
{
const peerlist_types& ours;
const peerlist_types& other;
};
template<typename Archive>
void serialize(Archive& a, peerlist_types& elem, unsigned ver)
{
elem.white = load_peers<peerlist_entry>(a, ver);
elem.gray = load_peers<peerlist_entry>(a, ver);
elem.anchor = load_peers<anchor_peerlist_entry>(a, ver);
if (ver == 0)
{
// from v1, we do not store the peer id anymore
peerid_type peer_id{};
a & peer_id;
}
}
template<typename Archive>
void serialize(Archive& a, peerlist_join elem, unsigned ver)
{
save_peers(a, boost::range::join(elem.ours.white, elem.other.white));
save_peers(a, boost::range::join(elem.ours.gray, elem.other.gray));
save_peers(a, boost::range::join(elem.ours.anchor, elem.other.anchor));
}
boost::optional<peerlist_storage> peerlist_storage::open(std::istream& src, const bool new_format)
{
try
{
peerlist_storage out{};
if (new_format)
{
boost::archive::portable_binary_iarchive a{src};
a >> out.m_types;
}
else
{
boost::archive::binary_iarchive a{src};
a >> out.m_types;
}
if (src.good())
{
std::sort(out.m_types.white.begin(), out.m_types.white.end(), by_zone{});
std::sort(out.m_types.gray.begin(), out.m_types.gray.end(), by_zone{});
std::sort(out.m_types.anchor.begin(), out.m_types.anchor.end(), by_zone{});
return {std::move(out)};
}
}
catch (const std::exception& e)
{}
return boost::none;
}
boost::optional<peerlist_storage> peerlist_storage::open(const std::string& path)
{
std::ifstream src_file{};
src_file.open( path , std::ios_base::binary | std::ios_base::in);
if(src_file.fail())
return boost::none;
boost::optional<peerlist_storage> out = open(src_file, true);
if (!out)
{
// if failed, try reading in unportable mode
boost::filesystem::copy_file(path, path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
src_file.close();
src_file.open( path , std::ios_base::binary | std::ios_base::in);
if(src_file.fail())
return boost::none;
out = open(src_file, false);
if (!out)
{
// This is different from the `return boost::none` cases above. Those
// cases could fail due to bad file permissions, so a shutdown is
// likely more appropriate.
MWARNING("Failed to load p2p config file, falling back to default config");
out.emplace();
}
}
return out;
}
peerlist_storage::~peerlist_storage() noexcept
{}
bool peerlist_storage::store(std::ostream& dest, const peerlist_types& other) const
{
try
{
boost::archive::portable_binary_oarchive a{dest};
const peerlist_join pj{std::cref(m_types), std::cref(other)};
a << pj;
return dest.good();
}
catch (const boost::archive::archive_exception& e)
{}
return false;
}
bool peerlist_storage::store(const std::string& path, const peerlist_types& other) const
{
std::ofstream dest_file{};
dest_file.open( path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
if(dest_file.fail())
return false;
return store(dest_file, other);
}
peerlist_types peerlist_storage::take_zone(epee::net_utils::zone zone)
{
peerlist_types out{};
out.white = do_take_zone(m_types.white, zone);
out.gray = do_take_zone(m_types.gray, zone);
out.anchor = do_take_zone(m_types.anchor, zone);
return out;
}
bool peerlist_manager::init(peerlist_types&& peers, bool allow_local_ip)
{
CRITICAL_REGION_LOCAL(m_peerlist_lock);
if (!m_peers_white.empty() || !m_peers_gray.empty() || !m_peers_anchor.empty())
return false;
add_peers(m_peers_white.get<by_addr>(), std::move(peers.white));
add_peers(m_peers_gray.get<by_addr>(), std::move(peers.gray));
add_peers(m_peers_anchor.get<by_addr>(), std::move(peers.anchor));
m_allow_local_ip = allow_local_ip;
return true;
}
void peerlist_manager::get_peerlist(std::vector<peerlist_entry>& pl_gray, std::vector<peerlist_entry>& pl_white)
{
CRITICAL_REGION_LOCAL(m_peerlist_lock);
copy_peers(pl_gray, m_peers_gray.get<by_addr>());
copy_peers(pl_white, m_peers_white.get<by_addr>());
}
void peerlist_manager::get_peerlist(peerlist_types& peers)
{
CRITICAL_REGION_LOCAL(m_peerlist_lock);
peers.white.reserve(peers.white.size() + m_peers_white.size());
peers.gray.reserve(peers.gray.size() + m_peers_gray.size());
peers.anchor.reserve(peers.anchor.size() + m_peers_anchor.size());
copy_peers(peers.white, m_peers_white.get<by_addr>());
copy_peers(peers.gray, m_peers_gray.get<by_addr>());
copy_peers(peers.anchor, m_peers_anchor.get<by_addr>());
}
void peerlist_manager::evict_host_from_peerlist(bool use_white, const peerlist_entry& pr)
{
filter(use_white, [&pr](const peerlist_entry& pe){ return pe.adr.is_same_host(pr.adr); });
}
}
BOOST_CLASS_VERSION(nodetool::peerlist_types, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER);
BOOST_CLASS_VERSION(nodetool::peerlist_join, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER);
|