-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdht-get.cpp
322 lines (299 loc) · 7.83 KB
/
dht-get.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
/*
* Copyright (c) 2022-2024 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <opendht.h>
#include <iostream>
#include <iomanip>
#include <mutex>
#include <condition_variable>
#include "dht-values.h"
#include "dht-helpers.h"
static bool running = true;
static std::condition_variable cv;
static std::mutex mtx;
static char section = 'a';
static bool use_local = false;
static const std::string default_bs("xrf757.openquad.net");
static dht::Where w;
static SMrefdConfig1 mrefdConfig;
static SMrefdPeers1 mrefdPeers;
static SUrfdPeers1 urfdPeers;
static SUrfdConfig1 urfdConfig;
enum class ENodeType { urfd, mrefd };
static void Usage(std::ostream &ostr, const char *comname)
{
ostr << "usage: " << comname << " [-b bootstrap] [-s {c|l|p|u}] [-l] node_name" << std::endl << std::endl;
ostr << "Options:" << std::endl;
ostr << " -b (bootstrap) argument is any running node on the dht network" << std::endl;
ostr << " If not specified, " << default_bs << " will be used." << std::endl;
ostr << " -s (section) arguments is one of:" << std::endl;
ostr << " c - configuration" << std::endl;
ostr << " p - peer list" << std::endl;
ostr << " If no section is specified, both sections will be output." << std::endl;
ostr << " -l will output time values in local time, otherwise gmt is reported." << std::endl;
}
int main(int argc, char *argv[])
{
std::string bs(default_bs);
while (1)
{
int c = getopt(argc, argv, "b:s:l");
if (c < 0)
{
if (1 == argc)
{
Usage(std::cout, argv[0]);
exit(EXIT_SUCCESS);
}
break;
}
switch (c)
{
case 'b':
bs.assign(optarg);
break;
case 'l':
use_local = true;
break;
case 's':
if (optarg[1])
{
std::cerr << argv[0] << ": " << "You can only specify a single section!" << std::endl;
Usage(std::cerr, argv[0]);
}
section = optarg[0];
if ('c'!=section && 'p'!=section)
{
std::cerr << argv[0] << ": " << "You have specified an illegal section!" << std::endl;
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
break;
default:
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind + 1 != argc)
{
std::cerr << argv[0] << ": " << ((optind==argc) ? "No node_name specified!" : "Too many arguments!") << std::endl;
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
auto p = argv[optind];
while (*p)
{
if (std::islower(*p))
*p = std::toupper(*p);
p++;
}
const std::string key(argv[optind]);
auto keyhash = dht::InfoHash::get(key);
mrefdConfig.timestamp = mrefdPeers.timestamp = 0;
urfdConfig.timestamp = urfdPeers.timestamp = 0;
ENodeType keytype;
if (0 == key.compare(0, 4, "M17-"))
keytype = ENodeType::mrefd;
else if (0 == key.compare(0, 3, "URF"))
keytype = ENodeType::urfd;
else
{
std::cerr << "Don't know how to get '" << key << "'" << std::endl;
return EXIT_FAILURE;
}
switch (keytype)
{
case ENodeType::mrefd:
switch (section)
{
case 'c':
w.id(toUType(EMrefdValueID::Config));
break;
case 'p':
w.id(toUType(EMrefdValueID::Peers));
break;
}
break;
case ENodeType::urfd:
switch (section)
{
case 'c':
w.id(toUType(EUrfdValueID::Config));
break;
case 'p':
w.id(toUType(EUrfdValueID::Peers));
break;
}
break;
}
std::string name("HamGet");
name += std::to_string(getpid());
dht::DhtRunner node;
node.run(17171, dht::crypto::generateIdentity(name), true, 59973);
node.bootstrap(bs, "17171");
switch (keytype)
{
case ENodeType::mrefd:
node.get(
keyhash,
[](const std::shared_ptr<dht::Value> &v) {
if (v->checkSignature())
{
switch (v->id)
{
case toUType(EMrefdValueID::Config):
if (0 == v->user_type.compare(MREFD_CONFIG_1))
{
auto rdat = dht::Value::unpack<SMrefdConfig1>(*v);
if (rdat.timestamp > mrefdConfig.timestamp)
mrefdConfig = dht::Value::unpack<SMrefdConfig1>(*v);
}
break;
case toUType(EMrefdValueID::Peers):
if (0 == v->user_type.compare(MREFD_PEERS_1))
{
auto rdat = dht::Value::unpack<SMrefdPeers1>(*v);
if (rdat.timestamp > mrefdPeers.timestamp)
{
mrefdPeers = dht::Value::unpack<SMrefdPeers1>(*v);
} else if (rdat.timestamp==mrefdPeers.timestamp)
{
if (rdat.sequence > mrefdPeers.sequence)
mrefdPeers = dht::Value::unpack<SMrefdPeers1>(*v);
}
}
break;
}
}
else
{
std::cout << "Value signature failed!" << std::endl;
}
return true;
},
[](bool success) {
if (! success)
{
std::cerr << "get() failed!" << std::endl;
}
std::unique_lock<std::mutex> lck(mtx);
running = false;
cv.notify_all();
},
{}, // empty filter
w
);
break;
case ENodeType::urfd:
node.get(
keyhash,
[](const std::shared_ptr<dht::Value> &v) {
if (v->checkSignature())
{
switch (v->id)
{
case toUType(EUrfdValueID::Config):
if (0 == v->user_type.compare(URFD_CONFIG_1))
{
auto rdat = dht::Value::unpack<SUrfdConfig1>(*v);
if (rdat.timestamp > urfdConfig.timestamp)
urfdConfig = dht::Value::unpack<SUrfdConfig1>(*v);
}
break;
case toUType(EUrfdValueID::Peers):
if (0 == v->user_type.compare(URFD_PEERS_1))
{
auto rdat = dht::Value::unpack<SUrfdPeers1>(*v);
if (rdat.timestamp > urfdPeers.timestamp)
{
urfdPeers = dht::Value::unpack<SUrfdPeers1>(*v);
} else if (rdat.timestamp==urfdPeers.timestamp)
{
if (rdat.sequence > urfdPeers.sequence)
urfdPeers = dht::Value::unpack<SUrfdPeers1>(*v);
}
}
break;
}
}
else
{
std::cout << "Value signature failed!" << std::endl;
}
return true;
},
[](bool success) {
if (! success)
{
std::cerr << "get() failed!" << std::endl;
}
std::unique_lock<std::mutex> lck(mtx);
running = false;
cv.notify_all();
},
{}, // empty filter
w
);
break;
}
std::unique_lock<std::mutex> lck(mtx);
while (running)
{
cv.wait(lck);
}
std::cout << '{';
switch (section)
{
case 'c':
switch (keytype)
{
case ENodeType::mrefd: PrintMrefdConfig(mrefdConfig, std::cout); break;
case ENodeType::urfd: PrintUrfdConfig(urfdConfig, std::cout); break;
}
break;
case 'p':
switch (keytype)
{
case ENodeType::mrefd: PrintMrefdPeers(mrefdPeers, use_local, std::cout); break;
case ENodeType::urfd: PrintUrfdPeers(urfdPeers, use_local, std::cout); break;
}
break;
default:
switch (keytype)
{
case ENodeType::mrefd:
if (mrefdConfig.timestamp)
{
PrintMrefdConfig(mrefdConfig, std::cout);
std::cout << ',';
PrintMrefdPeers(mrefdPeers, use_local, std::cout);
}
break;
case ENodeType::urfd:
if (urfdConfig.timestamp)
{
PrintUrfdConfig(urfdConfig, std::cout);
std::cout << ',';
PrintUrfdPeers(urfdPeers, use_local, std::cout);
}
break;
}
}
std::cout << '}' << std::endl;
node.join();
return EXIT_SUCCESS;
}