1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2022 The Limenka developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 #ifndef LIMENKA_ADDRMAN_H
7 #define LIMENKA_ADDRMAN_H
8 9 #include <netaddress.h>
10 #include <netgroup.h>
11 #include <protocol.h>
12 #include <streams.h>
13 #include <util/time.h>
14 15 #include <cstdint>
16 #include <memory>
17 #include <optional>
18 #include <unordered_set>
19 #include <utility>
20 #include <vector>
21 22 class InvalidAddrManVersionError : public std::ios_base::failure
23 {
24 public:
25 InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
26 };
27 28 class AddrManImpl;
29 class AddrInfo;
30 31 /** Default for -checkaddrman */
32 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
33 34 /** Location information for an address in AddrMan */
35 struct AddressPosition {
36 // Whether the address is in the new or tried table
37 const bool tried;
38 39 // Addresses in the tried table should always have a multiplicity of 1.
40 // Addresses in the new table can have multiplicity between 1 and
41 // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
42 const int multiplicity;
43 44 // If the address is in the new table, the bucket and position are
45 // populated based on the first source who sent the address.
46 // In certain edge cases, this may not be where the address is currently
47 // located.
48 const int bucket;
49 const int position;
50 51 bool operator==(AddressPosition other) {
52 return std::tie(tried, multiplicity, bucket, position) ==
53 std::tie(other.tried, other.multiplicity, other.bucket, other.position);
54 }
55 explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
56 : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
57 };
58 59 /** Stochastic address manager
60 *
61 * Design goals:
62 * * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
63 * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
64 *
65 * To that end:
66 * * Addresses are organized into buckets that can each store up to 64 entries.
67 * * Addresses to which our node has not successfully connected go into 1024 "new" buckets.
68 * * Based on the address range (/16 for IPv4) of the source of information, or if an asmap is provided,
69 * the AS it belongs to (for IPv4/IPv6), 64 buckets are selected at random.
70 * * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
71 * * The position in the bucket is chosen based on the full address.
72 * * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
73 * are seen frequently. The chance for increasing this multiplicity decreases exponentially.
74 * * When adding a new address to an occupied position of a bucket, it will not replace the existing entry
75 * unless that address is also stored in another bucket or it doesn't meet one of several quality criteria
76 * (see IsTerrible for exact criteria).
77 * * Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
78 * * Each address range selects at random 8 of these buckets.
79 * * The actual bucket is chosen from one of these, based on the full address.
80 * * When adding a new good address to an occupied position of a bucket, a FEELER connection to the
81 * old address is attempted. The old entry is only replaced and moved back to the "new" buckets if this
82 * attempt was unsuccessful.
83 * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
84 * be observable by adversaries.
85 * * Several indexes are kept for high performance. Setting m_consistency_check_ratio with the -checkaddrman
86 * configuration option will introduce (expensive) consistency checks for the entire data structure.
87 */
88 class AddrMan
89 {
90 protected:
91 const std::unique_ptr<AddrManImpl> m_impl;
92 93 public:
94 explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
95 96 ~AddrMan();
97 98 template <typename Stream>
99 void Serialize(Stream& s_) const;
100 101 template <typename Stream>
102 void Unserialize(Stream& s_);
103 104 /**
105 * Return size information about addrman.
106 *
107 * @param[in] net Select addresses only from specified network (nullopt = all)
108 * @param[in] in_new Select addresses only from one table (true = new, false = tried, nullopt = both)
109 * @return Number of unique addresses that match specified options.
110 */
111 size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const;
112 113 /**
114 * Attempt to add one or more addresses to addrman's new table.
115 * If an address already exists in addrman, the existing entry may be updated
116 * (e.g. adding additional service flags). If the existing entry is in the new table,
117 * it may be added to more buckets, improving the probability of selection.
118 *
119 * @param[in] vAddr Address records to attempt to add.
120 * @param[in] source The address of the node that sent us these addr records.
121 * @param[in] time_penalty A "time penalty" to apply to the address record's nTime. If a peer
122 * sends us an address record with nTime=n, then we'll add it to our
123 * addrman with nTime=(n - time_penalty).
124 * @return true if at least one address is successfully added, or added to an additional bucket. Unaffected by updates. */
125 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
126 127 /**
128 * Mark an address record as accessible and attempt to move it to addrman's tried table.
129 *
130 * @param[in] addr Address record to attempt to move to tried table.
131 * @param[in] time The time that we were last connected to this peer.
132 * @return true if the address is successfully moved from the new table to the tried table.
133 */
134 bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
135 136 //! Mark an entry as connection attempted to.
137 void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
138 139 //! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
140 void ResolveCollisions();
141 142 /**
143 * Randomly select an address in the tried table that another address is
144 * attempting to evict.
145 *
146 * @return CAddress The record for the selected tried peer.
147 * seconds The last time we attempted to connect to that peer.
148 */
149 std::pair<CAddress, NodeSeconds> SelectTriedCollision();
150 151 /**
152 * Choose an address to connect to.
153 *
154 * @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
155 * an address from the new table or an empty pair. Passing `false` will return an
156 * empty pair or an address from either the new or tried table (it does not
157 * guarantee a tried entry).
158 * @param[in] networks Select only addresses of these networks (empty = all). Passing networks may
159 * slow down the search.
160 * @return CAddress The record for the selected peer.
161 * seconds The last time we attempted to connect to that peer.
162 */
163 std::pair<CAddress, NodeSeconds> Select(bool new_only = false, const std::unordered_set<Network>& networks = {}) const;
164 165 /**
166 * Return all or many randomly selected addresses, optionally by network.
167 *
168 * @param[in] max_addresses Maximum number of addresses to return (0 = all).
169 * @param[in] max_pct Maximum percentage of addresses to return (0 = all). Value must be from 0 to 100.
170 * @param[in] network Select only addresses of this network (nullopt = all).
171 * @param[in] filtered Select only addresses that are considered good quality (false = all).
172 *
173 * @return A vector of randomly selected addresses from vRandom.
174 */
175 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
176 177 /**
178 * Returns an information-location pair for all addresses in the selected addrman table.
179 * If an address appears multiple times in the new table, an information-location pair
180 * is returned for each occurrence. Addresses only ever appear once in the tried table.
181 *
182 * @param[in] from_tried Selects which table to return entries from.
183 *
184 * @return A vector consisting of pairs of AddrInfo and AddressPosition.
185 */
186 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const;
187 188 /** We have successfully connected to this peer. Calling this function
189 * updates the CAddress's nTime, which is used in our IsTerrible()
190 * decisions and gossiped to peers. Callers should be careful that updating
191 * this information doesn't leak topology information to network spies.
192 *
193 * net_processing calls this function when it *disconnects* from a peer to
194 * not leak information about currently connected peers.
195 *
196 * @param[in] addr The address of the peer we were connected to
197 * @param[in] time The time that we were last connected to this peer
198 */
199 void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
200 201 //! Update an entry's service bits.
202 void SetServices(const CService& addr, ServiceFlags nServices);
203 204 /** Test-only function
205 * Find the address record in AddrMan and return information about its
206 * position.
207 * @param[in] addr The address record to look up.
208 * @return Information about the address record in AddrMan
209 * or nullopt if address is not found.
210 */
211 std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
212 };
213 214 #endif // LIMENKA_ADDRMAN_H
215