forked from PurpleI2P/i2pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KadDHT.cpp
372 lines (352 loc) · 7.23 KB
/
KadDHT.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (c) 2023, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*
*/
#include "KadDHT.h"
namespace i2p
{
namespace data
{
DHTNode::DHTNode ():
zero (nullptr), one (nullptr)
{
}
DHTNode::~DHTNode ()
{
if (zero) delete zero;
if (one) delete one;
}
void DHTNode::MoveRouterUp (bool fromOne)
{
DHTNode *& side = fromOne ? one : zero;
if (side)
{
if (router) router = nullptr; // shouldn't happen
router = side->router;
side->router = nullptr;
delete side;
side = nullptr;
}
}
DHTTable::DHTTable ():
m_Size (0)
{
m_Root = new DHTNode;
}
DHTTable::~DHTTable ()
{
delete m_Root;
}
void DHTTable::Clear ()
{
m_Size = 0;
delete m_Root;
m_Root = new DHTNode;
}
void DHTTable::Insert (const std::shared_ptr<RouterInfo>& r)
{
if (!r) return;
return Insert (r, m_Root, 0);
}
void DHTTable::Insert (const std::shared_ptr<RouterInfo>& r, DHTNode * root, int level)
{
if (root->router)
{
if (root->router->GetIdentHash () == r->GetIdentHash ())
{
root->router = r; // replace
return;
}
auto r2 = root->router;
root->router = nullptr; m_Size--;
int bit1, bit2;
do
{
bit1 = r->GetIdentHash ().GetBit (level);
bit2 = r2->GetIdentHash ().GetBit (level);
if (bit1 == bit2)
{
if (bit1)
{
if (root->one) return; // someting wrong
root->one = new DHTNode;
root = root->one;
}
else
{
if (root->zero) return; // someting wrong
root->zero = new DHTNode;
root = root->zero;
}
level++;
}
}
while (bit1 == bit2);
if (!root->zero)
root->zero = new DHTNode;
if (!root->one)
root->one = new DHTNode;
if (bit1)
{
Insert (r2, root->zero, level + 1);
Insert (r, root->one, level + 1);
}
else
{
Insert (r2, root->one, level + 1);
Insert (r, root->zero, level + 1);
}
}
else
{
if (!root->zero && !root->one)
{
root->router = r; m_Size++;
return;
}
int bit = r->GetIdentHash ().GetBit (level);
if (bit)
{
if (!root->one)
root->one = new DHTNode;
Insert (r, root->one, level + 1);
}
else
{
if (!root->zero)
root->zero = new DHTNode;
Insert (r, root->zero, level + 1);
}
}
}
bool DHTTable::Remove (const IdentHash& h)
{
return Remove (h, m_Root, 0);
}
bool DHTTable::Remove (const IdentHash& h, DHTNode * root, int level)
{
if (root)
{
if (root->router && root->router->GetIdentHash () == h)
{
root->router = nullptr;
m_Size--;
return true;
}
int bit = h.GetBit (level);
if (bit)
{
if (root->one && Remove (h, root->one, level + 1))
{
if (root->one->IsEmpty ())
{
delete root->one;
root->one = nullptr;
if (root->zero && root->zero->router)
root->MoveRouterUp (false);
}
else if (root->one->router && !root->zero)
root->MoveRouterUp (true);
return true;
}
}
else
{
if (root->zero && Remove (h, root->zero, level + 1))
{
if (root->zero->IsEmpty ())
{
delete root->zero;
root->zero = nullptr;
if (root->one && root->one->router)
root->MoveRouterUp (true);
}
else if (root->zero->router && !root->one)
root->MoveRouterUp (false);
return true;
}
}
}
return false;
}
std::shared_ptr<RouterInfo> DHTTable::FindClosest (const IdentHash& h, const Filter& filter) const
{
if (filter) m_Filter = filter;
auto r = FindClosest (h, m_Root, 0);
m_Filter = nullptr;
return r;
}
std::shared_ptr<RouterInfo> DHTTable::FindClosest (const IdentHash& h, DHTNode * root, int level) const
{
bool split = false;
do
{
if (root->router)
return (!m_Filter || m_Filter (root->router)) ? root->router : nullptr;
split = root->zero && root->one;
if (!split)
{
if (root->zero) root = root->zero;
else if (root->one) root = root->one;
else return nullptr;
level++;
}
}
while (!split);
int bit = h.GetBit (level);
if (bit)
{
if (root->one)
{
auto r = FindClosest (h, root->one, level + 1);
if (r) return r;
}
if (root->zero)
{
auto r = FindClosest (h, root->zero, level + 1);
if (r) return r;
}
}
else
{
if (root->zero)
{
auto r = FindClosest (h, root->zero, level + 1);
if (r) return r;
}
if (root->one)
{
auto r = FindClosest (h, root->one, level + 1);
if (r) return r;
}
}
return nullptr;
}
std::vector<std::shared_ptr<RouterInfo> > DHTTable::FindClosest (const IdentHash& h, size_t num, const Filter& filter) const
{
std::vector<std::shared_ptr<RouterInfo> > vec;
if (num > 0)
{
if (filter) m_Filter = filter;
FindClosest (h, num, m_Root, 0, vec);
m_Filter = nullptr;
}
return vec;
}
void DHTTable::FindClosest (const IdentHash& h, size_t num, DHTNode * root, int level, std::vector<std::shared_ptr<RouterInfo> >& hashes) const
{
if (hashes.size () >= num) return;
bool split = false;
do
{
if (root->router)
{
if (!m_Filter || m_Filter (root->router))
hashes.push_back (root->router);
return;
}
split = root->zero && root->one;
if (!split)
{
if (root->zero) root = root->zero;
else if (root->one) root = root->one;
else return;
level++;
}
}
while (!split);
int bit = h.GetBit (level);
if (bit)
{
if (root->one)
FindClosest (h, num, root->one, level + 1, hashes);
if (hashes.size () < num && root->zero)
FindClosest (h, num, root->zero, level + 1, hashes);
}
else
{
if (root->zero)
FindClosest (h, num, root->zero, level + 1, hashes);
if (hashes.size () < num && root->one)
FindClosest (h, num, root->one, level + 1, hashes);
}
}
void DHTTable::Cleanup (const Filter& filter)
{
if (filter)
{
m_Filter = filter;
Cleanup (m_Root);
m_Filter = nullptr;
}
else
Clear ();
}
void DHTTable::Cleanup (DHTNode * root)
{
if (!root) return;
if (root->router)
{
if (!m_Filter || !m_Filter (root->router))
{
m_Size--;
root->router = nullptr;
}
return;
}
if (root->zero)
{
Cleanup (root->zero);
if (root->zero->IsEmpty ())
{
delete root->zero;
root->zero = nullptr;
}
}
if (root->one)
{
Cleanup (root->one);
if (root->one->IsEmpty ())
{
delete root->one;
root->one = nullptr;
if (root->zero && root->zero->router)
root->MoveRouterUp (false);
}
else if (root->one->router && !root->zero)
root->MoveRouterUp (true);
}
}
void DHTTable::Print (std::stringstream& s)
{
Print (s, m_Root, 0);
}
void DHTTable::Print (std::stringstream& s, DHTNode * root, int level)
{
if (!root) return;
s << std::string (level, '-');
if (root->router)
{
if (!root->zero && !root->one)
s << '>' << GetIdentHashAbbreviation (root->router->GetIdentHash ());
else
s << "error";
}
s << std::endl;
if (root->zero)
{
s << std::string (level, '-') << "0" << std::endl;
Print (s, root->zero, level + 1);
}
if (root->one)
{
s << std::string (level, '-') << "1" << std::endl;
Print (s, root->one, level + 1);
}
}
}
}