Debian Codegraph @

src/p2p/net_peerlist.cpp

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2
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 #include "net_peerlist.h"
31
32 #include <algorithm>
33 #include <functional>
34 #include <fstream>
35 #include <iterator>
36
37 #include <boost/archive/binary_iarchive.hpp>
38 #include <boost/archive/portable_binary_oarchive.hpp>
39 #include <boost/archive/portable_binary_iarchive.hpp>
40 #include <boost/filesystem/operations.hpp>
41 #include <boost/range/join.hpp>
42 #include <boost/serialization/version.hpp>
43
44 #include "net_peerlist_boost_serialization.h"
45 #include "common/util.h"
46
47
48 namespace nodetool
49 {
50 namespace
51 {
52 constexpr unsigned CURRENT_PEERLIST_STORAGE_ARCHIVE_VER = 6;
53
54 struct by_zone
55 {
56 using zone = epee::net_utils::zone;
57
58 template<typename T>
59 bool operator()(const T& left, const zone right) const
60 {
61 return left.adr.get_zone() < right;
62 }
63
64 template<typename T>
65 bool operator()(const zone left, const T& right) const
66 {
67 return left < right.adr.get_zone();
68 }
69
70 template<typename T, typename U>
71 bool operator()(const T& left, const U& right) const
72 {
73 return left.adr.get_zone() < right.adr.get_zone();
74 }
75 };
76
77 template<typename Elem, typename Archive>
78 std::vector<Elem> load_peers(Archive& a, unsigned ver)
79 {
80 // at v6, we drop existing peerlists, because annoying change
81 if (ver < 6)
82 return {};
83
84 uint64_t size = 0;
85 a & size;
86
87 Elem ple{};
88
89 std::vector<Elem> elems{};
90 elems.reserve(size);
91 while (size--)
92 {
93 a & ple;
94 elems.push_back(std::move(ple));
95 }
96
97 return elems;
98 }
99
100 template<typename Archive, typename Range>
101 void save_peers(Archive& a, const Range& elems)
102 {
103 const uint64_t size = elems.size();
104 a & size;
105 for (const auto& elem : elems)
106 a & elem;
107 }
108
109 template<typename T>
110 std::vector<T> do_take_zone(std::vector<T>& src, epee::net_utils::zone zone)
111 {
112 const auto start = std::lower_bound(src.begin(), src.end(), zone, by_zone{});
113 const auto end = std::upper_bound(start, src.end(), zone, by_zone{});
114
115 std::vector<T> out{};
116 out.assign(std::make_move_iterator(start), std::make_move_iterator(end));
117 src.erase(start, end);
118 return out;
119 }
120
121 template<typename Container, typename T>
122 void add_peers(Container& dest, std::vector<T>&& src)
123 {
124 dest.insert(std::make_move_iterator(src.begin()), std::make_move_iterator(src.end()));
125 }
126
127 template<typename Container, typename Range>
128 void copy_peers(Container& dest, const Range& src)
129 {
130 std::copy(src.begin(), src.end(), std::back_inserter(dest));
131 }
132 } // anonymous
133
134 struct peerlist_join
135 {
136 const peerlist_types& ours;
137 const peerlist_types& other;
138 };
139
140 template<typename Archive>
141 void serialize(Archive& a, peerlist_types& elem, unsigned ver)
142 {
143 elem.white = load_peers<peerlist_entry>(a, ver);
144 elem.gray = load_peers<peerlist_entry>(a, ver);
145 elem.anchor = load_peers<anchor_peerlist_entry>(a, ver);
146
147 if (ver == 0)
148 {
149 // from v1, we do not store the peer id anymore
150 peerid_type peer_id{};
151 a & peer_id;
152 }
153 }
154
155 template<typename Archive>
156 void serialize(Archive& a, peerlist_join elem, unsigned ver)
157 {
158 save_peers(a, boost::range::join(elem.ours.white, elem.other.white));
159 save_peers(a, boost::range::join(elem.ours.gray, elem.other.gray));
160 save_peers(a, boost::range::join(elem.ours.anchor, elem.other.anchor));
161 }
162
163 boost::optional<peerlist_storage> peerlist_storage::open(std::istream& src, const bool new_format)
164 {
165 try
166 {
167 peerlist_storage out{};
168 if (new_format)
169 {
170 boost::archive::portable_binary_iarchive a{src};
171 a >> out.m_types;
172 }
173 else
174 {
175 boost::archive::binary_iarchive a{src};
176 a >> out.m_types;
177 }
178
179 if (src.good())
180 {
181 std::sort(out.m_types.white.begin(), out.m_types.white.end(), by_zone{});
182 std::sort(out.m_types.gray.begin(), out.m_types.gray.end(), by_zone{});
183 std::sort(out.m_types.anchor.begin(), out.m_types.anchor.end(), by_zone{});
184 return {std::move(out)};
185 }
186 }
187 catch (const std::exception& e)
188 {}
189
190 return boost::none;
191 }
192
193 boost::optional<peerlist_storage> peerlist_storage::open(const std::string& path)
194 {
195 std::ifstream src_file{};
196 src_file.open( path , std::ios_base::binary | std::ios_base::in);
197 if(src_file.fail())
198 return boost::none;
199
200 boost::optional<peerlist_storage> out = open(src_file, true);
201 if (!out)
202 {
203 // if failed, try reading in unportable mode
204 tools::copy_file(path, path + ".unportable");
205 src_file.close();
206 src_file.open( path , std::ios_base::binary | std::ios_base::in);
207 if(src_file.fail())
208 return boost::none;
209
210 out = open(src_file, false);
211 if (!out)
212 {
213 // This is different from the `return boost::none` cases above. Those
214 // cases could fail due to bad file permissions, so a shutdown is
215 // likely more appropriate.
216 MWARNING("Failed to load p2p config file, falling back to default config");
217 out.emplace();
218 }
219 }
220
221 return out;
222 }
223
224 peerlist_storage::~peerlist_storage() noexcept
225 {}
226
227 bool peerlist_storage::store(std::ostream& dest, const peerlist_types& other) const
228 {
229 try
230 {
231 boost::archive::portable_binary_oarchive a{dest};
232 const peerlist_join pj{std::cref(m_types), std::cref(other)};
233 a << pj;
234 return dest.good();
235 }
236 catch (const boost::archive::archive_exception& e)
237 {}
238
239 return false;
240 }
241
242 bool peerlist_storage::store(const std::string& path, const peerlist_types& other) const
243 {
244 std::ofstream dest_file{};
245 dest_file.open( path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
246 if(dest_file.fail())
247 return false;
248
249 return store(dest_file, other);
250 }
251
252 peerlist_types peerlist_storage::take_zone(epee::net_utils::zone zone)
253 {
254 peerlist_types out{};
255 out.white = do_take_zone(m_types.white, zone);
256 out.gray = do_take_zone(m_types.gray, zone);
257 out.anchor = do_take_zone(m_types.anchor, zone);
258 return out;
259 }
260
261 bool peerlist_manager::init(peerlist_types&& peers, bool allow_local_ip)
262 {
263 CRITICAL_REGION_LOCAL(m_peerlist_lock);
264
265 if (!m_peers_white.empty() || !m_peers_gray.empty() || !m_peers_anchor.empty())
266 return false;
267
268 add_peers(m_peers_white.get<by_addr>(), std::move(peers.white));
269 add_peers(m_peers_gray.get<by_addr>(), std::move(peers.gray));
270 add_peers(m_peers_anchor.get<by_addr>(), std::move(peers.anchor));
271 m_allow_local_ip = allow_local_ip;
272 return true;
273 }
274
275 void peerlist_manager::get_peerlist(std::vector<peerlist_entry>& pl_gray, std::vector<peerlist_entry>& pl_white)
276 {
277 CRITICAL_REGION_LOCAL(m_peerlist_lock);
278 copy_peers(pl_gray, m_peers_gray.get<by_addr>());
279 copy_peers(pl_white, m_peers_white.get<by_addr>());
280 }
281
282 void peerlist_manager::get_peerlist(peerlist_types& peers)
283 {
284 CRITICAL_REGION_LOCAL(m_peerlist_lock);
285 peers.white.reserve(peers.white.size() + m_peers_white.size());
286 peers.gray.reserve(peers.gray.size() + m_peers_gray.size());
287 peers.anchor.reserve(peers.anchor.size() + m_peers_anchor.size());
288
289 copy_peers(peers.white, m_peers_white.get<by_addr>());
290 copy_peers(peers.gray, m_peers_gray.get<by_addr>());
291 copy_peers(peers.anchor, m_peers_anchor.get<by_addr>());
292 }
293
294 void peerlist_manager::evict_host_from_peerlist(bool use_white, const peerlist_entry& pr)
295 {
296 filter(use_white, [&pr](const peerlist_entry& pe){ return pe.adr.is_same_host(pr.adr); });
297 }
298 }
299
300 BOOST_CLASS_VERSION(nodetool::peerlist_types, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER);
301 BOOST_CLASS_VERSION(nodetool::peerlist_join, nodetool::CURRENT_PEERLIST_STORAGE_ARCHIVE_VER);
302