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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 Tatsuhiro Tsujikawa
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "UDPTrackerClient.h"
#include "UDPTrackerRequest.h"
#include "bittorrent_helper.h"
#include "util.h"
#include "LogFactory.h"
#include "SimpleRandomizer.h"
#include "fmt.h"
namespace aria2 {
UDPTrackerClient::UDPTrackerClient() : numWatchers_(0) {}
namespace {
template <typename InputIterator>
void failRequest(InputIterator first, InputIterator last, int error)
{
for (; first != last; ++first) {
(*first)->state = UDPT_STA_COMPLETE;
(*first)->error = error;
}
}
} // namespace
namespace {
uint32_t generateTransactionId()
{
uint32_t res;
SimpleRandomizer::getInstance()->getRandomBytes(
reinterpret_cast<unsigned char*>(&res), sizeof(res));
return res;
}
} // namespace
namespace {
void logInvalidLength(const std::string& remoteAddr, uint16_t remotePort,
int action, unsigned long expected, unsigned long actual)
{
A2_LOG_INFO(fmt("UDPT received %s reply from %s:%u invalid length "
"expected:%lu, actual:%lu",
getUDPTrackerActionStr(action), remoteAddr.c_str(),
remotePort, expected, actual));
}
} // namespace
namespace {
void logInvalidTransaction(const std::string& remoteAddr, uint16_t remotePort,
int action, uint32_t transactionId)
{
A2_LOG_INFO(
fmt("UDPT received %s reply from %s:%u invalid transaction_id=%08x",
getUDPTrackerActionStr(action), remoteAddr.c_str(), remotePort,
transactionId));
}
} // namespace
namespace {
void logTooShortLength(const std::string& remoteAddr, uint16_t remotePort,
int action, unsigned long minLength,
unsigned long actual)
{
A2_LOG_INFO(fmt("UDPT received %s reply from %s:%u length too short "
"min:%lu, actual:%lu",
getUDPTrackerActionStr(action), remoteAddr.c_str(),
remotePort, minLength, actual));
}
} // namespace
UDPTrackerClient::~UDPTrackerClient()
{
// Make all contained requests fail
int error = UDPT_ERR_SHUTDOWN;
failRequest(inflightRequests_.begin(), inflightRequests_.end(), error);
failRequest(pendingRequests_.begin(), pendingRequests_.end(), error);
failRequest(connectRequests_.begin(), connectRequests_.end(), error);
}
namespace {
struct CollectAddrPortMatch {
bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
{
if (req->remoteAddr == remoteAddr && req->remotePort == remotePort) {
dest.push_back(req);
return true;
}
else {
return false;
}
}
std::vector<std::shared_ptr<UDPTrackerRequest>>& dest;
std::string remoteAddr;
uint16_t remotePort;
CollectAddrPortMatch(std::vector<std::shared_ptr<UDPTrackerRequest>>& dest,
std::string remoteAddr, uint16_t remotePort)
: dest(dest), remoteAddr(std::move(remoteAddr)), remotePort(remotePort)
{
}
};
} // namespace
int UDPTrackerClient::receiveReply(std::shared_ptr<UDPTrackerRequest>& recvReq,
const unsigned char* data, size_t length,
const std::string& remoteAddr,
uint16_t remotePort, const Timer& now)
{
int32_t action = bittorrent::getIntParam(data, 0);
switch (action) {
case UDPT_ACT_CONNECT: {
if (length != 16) {
logInvalidLength(remoteAddr, remotePort, action, 16, length);
return -1;
}
auto transactionId = bittorrent::getIntParam(data, 4);
std::shared_ptr<UDPTrackerRequest> req =
findInflightRequest(remoteAddr, remotePort, transactionId, true);
if (!req) {
logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
return -1;
}
auto connectionId = bittorrent::getLLIntParam(data, 8);
A2_LOG_INFO(
fmt("UDPT received CONNECT reply from %s:%u transaction_id=%08x,"
"connection_id=%016" PRIx64,
remoteAddr.c_str(), remotePort, transactionId, connectionId));
UDPTrackerConnection c(UDPT_CST_CONNECTED, connectionId, now);
connectionIdCache_[std::make_pair(remoteAddr, remotePort)] = c;
// Now we have connection ID, push requests which are waiting for
// it.
std::vector<std::shared_ptr<UDPTrackerRequest>> reqs;
connectRequests_.erase(
std::remove_if(connectRequests_.begin(), connectRequests_.end(),
CollectAddrPortMatch(reqs, remoteAddr, remotePort)),
connectRequests_.end());
pendingRequests_.insert(pendingRequests_.begin(), reqs.begin(), reqs.end());
recvReq = std::move(req);
break;
}
case UDPT_ACT_ANNOUNCE: {
if (length < 20) {
logTooShortLength(remoteAddr, remotePort, action, 20, length);
return -1;
}
auto transactionId = bittorrent::getIntParam(data, 4);
std::shared_ptr<UDPTrackerRequest> req =
findInflightRequest(remoteAddr, remotePort, transactionId, true);
if (!req) {
logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
return -1;
}
req->state = UDPT_STA_COMPLETE;
req->reply = std::make_shared<UDPTrackerReply>();
req->reply->action = action;
req->reply->transactionId = transactionId;
req->reply->interval = bittorrent::getIntParam(data, 8);
req->reply->leechers = bittorrent::getIntParam(data, 12);
req->reply->seeders = bittorrent::getIntParam(data, 16);
int numPeers = 0;
for (size_t i = 20; i < length; i += 6) {
std::pair<std::string, uint16_t> hostport =
bittorrent::unpackcompact(data + i, AF_INET);
if (!hostport.first.empty()) {
req->reply->peers.push_back(hostport);
++numPeers;
}
}
A2_LOG_INFO(
fmt("UDPT received ANNOUNCE reply from %s:%u transaction_id=%08x,"
"connection_id=%016" PRIx64 ", event=%s, infohash=%s, "
"interval=%d, leechers=%d, "
"seeders=%d, num_peers=%d",
remoteAddr.c_str(), remotePort, transactionId, req->connectionId,
getUDPTrackerEventStr(req->event),
util::toHex(req->infohash).c_str(), req->reply->interval,
req->reply->leechers, req->reply->seeders, numPeers));
recvReq = std::move(req);
break;
}
case UDPT_ACT_ERROR: {
if (length < 8) {
logTooShortLength(remoteAddr, remotePort, action, 8, length);
return -1;
}
auto transactionId = bittorrent::getIntParam(data, 4);
std::shared_ptr<UDPTrackerRequest> req =
findInflightRequest(remoteAddr, remotePort, transactionId, true);
if (!req) {
logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
return -1;
}
std::string errorString(data + 8, data + length);
errorString = util::encodeNonUtf8(errorString);
req->state = UDPT_STA_COMPLETE;
req->error = UDPT_ERR_TRACKER;
A2_LOG_INFO(fmt("UDPT received ERROR reply from %s:%u transaction_id=%08x,"
"connection_id=%016" PRIx64 ", action=%d, error_string=%s",
remoteAddr.c_str(), remotePort, transactionId,
req->connectionId, action, errorString.c_str()));
if (req->action == UDPT_ACT_CONNECT) {
failConnect(req->remoteAddr, req->remotePort, UDPT_ERR_TRACKER);
}
recvReq = std::move(req);
break;
}
case UDPT_ACT_SCRAPE:
A2_LOG_INFO(fmt("unexpected scrape action reply from %s:%u",
remoteAddr.c_str(), remotePort));
return -1;
default:
A2_LOG_INFO(
fmt("unknown action reply from %s:%u", remoteAddr.c_str(), remotePort));
return -1;
}
return 0;
}
ssize_t UDPTrackerClient::createRequest(unsigned char* data, size_t length,
std::string& remoteAddr,
uint16_t& remotePort, const Timer& now)
{
if (pendingRequests_.empty()) {
return -1;
}
while (!pendingRequests_.empty()) {
const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
if (req->action == UDPT_ACT_CONNECT) {
ssize_t rv;
rv = createUDPTrackerConnect(data, length, remoteAddr, remotePort, req);
return rv;
}
UDPTrackerConnection* c =
getConnectionId(req->remoteAddr, req->remotePort, now);
if (!c) {
auto creq = std::make_shared<UDPTrackerRequest>();
creq->action = UDPT_ACT_CONNECT;
creq->remoteAddr = req->remoteAddr;
creq->remotePort = req->remotePort;
creq->transactionId = generateTransactionId();
pendingRequests_.push_front(creq);
ssize_t rv;
rv = createUDPTrackerConnect(data, length, remoteAddr, remotePort, creq);
return rv;
}
if (c->state == UDPT_CST_CONNECTING) {
connectRequests_.push_back(req);
pendingRequests_.pop_front();
continue;
}
req->connectionId = c->connectionId;
req->transactionId = generateTransactionId();
ssize_t rv;
rv = createUDPTrackerAnnounce(data, length, remoteAddr, remotePort, req);
return rv;
}
return -1;
}
void UDPTrackerClient::requestSent(const Timer& now)
{
if (pendingRequests_.empty()) {
A2_LOG_WARN("pendingRequests_ is empty");
return;
}
const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
switch (req->action) {
case UDPT_ACT_CONNECT:
A2_LOG_INFO(fmt("UDPT sent CONNECT to %s:%u transaction_id=%08x",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId));
break;
case UDPT_ACT_ANNOUNCE:
A2_LOG_INFO(fmt("UDPT sent ANNOUNCE to %s:%u transaction_id=%08x, "
"connection_id=%016" PRIx64 ", event=%s, infohash=%s",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId, req->connectionId,
getUDPTrackerEventStr(req->event),
util::toHex(req->infohash).c_str()));
break;
default:
// unreachable
assert(0);
}
req->dispatched = now;
switch (req->action) {
case UDPT_ACT_CONNECT: {
connectionIdCache_[std::make_pair(req->remoteAddr, req->remotePort)] =
UDPTrackerConnection();
break;
}
}
inflightRequests_.push_back(req);
pendingRequests_.pop_front();
}
void UDPTrackerClient::requestFail(int error)
{
if (pendingRequests_.empty()) {
A2_LOG_WARN("pendingRequests_ is empty");
return;
}
const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
switch (req->action) {
case UDPT_ACT_CONNECT:
A2_LOG_INFO(fmt("UDPT fail CONNECT to %s:%u transaction_id=%08x",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId));
failConnect(req->remoteAddr, req->remotePort, error);
break;
case UDPT_ACT_ANNOUNCE:
A2_LOG_INFO(fmt("UDPT fail ANNOUNCE to %s:%u transaction_id=%08x, "
"connection_id=%016" PRIx64 ", event=%s, infohash=%s",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId, req->connectionId,
getUDPTrackerEventStr(req->event),
util::toHex(req->infohash).c_str()));
break;
default:
// unreachable
assert(0);
}
req->state = UDPT_STA_COMPLETE;
req->error = error;
pendingRequests_.pop_front();
}
void UDPTrackerClient::addRequest(const std::shared_ptr<UDPTrackerRequest>& req)
{
req->state = UDPT_STA_PENDING;
req->error = UDPT_ERR_SUCCESS;
pendingRequests_.push_back(req);
}
namespace {
struct TimeoutCheck {
bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
{
auto t = req->dispatched.difference(now);
if (req->failCount == 0) {
if (t >= 5_s) {
switch (req->action) {
case UDPT_ACT_CONNECT:
A2_LOG_INFO(fmt("UDPT resend CONNECT to %s:%u transaction_id=%08x",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId));
break;
case UDPT_ACT_ANNOUNCE:
A2_LOG_INFO(fmt("UDPT resend ANNOUNCE to %s:%u transaction_id=%08x, "
"connection_id=%016" PRIx64 ", event=%s, infohash=%s",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId, req->connectionId,
getUDPTrackerEventStr(req->event),
util::toHex(req->infohash).c_str()));
break;
default:
// unreachable
assert(0);
}
++req->failCount;
dest.push_back(req);
return true;
}
else {
return false;
}
}
else {
if (t >= 10_s) {
switch (req->action) {
case UDPT_ACT_CONNECT:
A2_LOG_INFO(fmt("UDPT timeout CONNECT to %s:%u transaction_id=%08x",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId));
client->failConnect(req->remoteAddr, req->remotePort,
UDPT_ERR_TIMEOUT);
break;
case UDPT_ACT_ANNOUNCE:
A2_LOG_INFO(fmt("UDPT timeout ANNOUNCE to %s:%u transaction_id=%08x, "
"connection_id=%016" PRIx64 ", event=%s, infohash=%s",
req->remoteAddr.c_str(), req->remotePort,
req->transactionId, req->connectionId,
getUDPTrackerEventStr(req->event),
util::toHex(req->infohash).c_str()));
break;
default:
// unreachable
assert(0);
}
++req->failCount;
req->state = UDPT_STA_COMPLETE;
req->error = UDPT_ERR_TIMEOUT;
return true;
}
else {
return false;
}
}
}
std::vector<std::shared_ptr<UDPTrackerRequest>>& dest;
UDPTrackerClient* client;
const Timer& now;
TimeoutCheck(std::vector<std::shared_ptr<UDPTrackerRequest>>& dest,
UDPTrackerClient* client, const Timer& now)
: dest(dest), client(client), now(now)
{
}
};
} // namespace
void UDPTrackerClient::handleTimeout(const Timer& now)
{
std::vector<std::shared_ptr<UDPTrackerRequest>> dest;
inflightRequests_.erase(std::remove_if(inflightRequests_.begin(),
inflightRequests_.end(),
TimeoutCheck(dest, this, now)),
inflightRequests_.end());
pendingRequests_.insert(pendingRequests_.begin(), dest.begin(), dest.end());
}
std::shared_ptr<UDPTrackerRequest>
UDPTrackerClient::findInflightRequest(const std::string& remoteAddr,
uint16_t remotePort,
uint32_t transactionId, bool remove)
{
std::shared_ptr<UDPTrackerRequest> res;
for (auto i = inflightRequests_.begin(), eoi = inflightRequests_.end();
i != eoi; ++i) {
if ((*i)->remoteAddr == remoteAddr && (*i)->remotePort == remotePort &&
(*i)->transactionId == transactionId) {
res = *i;
if (remove) {
inflightRequests_.erase(i);
}
break;
}
}
return res;
}
UDPTrackerConnection*
UDPTrackerClient::getConnectionId(const std::string& remoteAddr,
uint16_t remotePort, const Timer& now)
{
auto i = connectionIdCache_.find(std::make_pair(remoteAddr, remotePort));
if (i == connectionIdCache_.end()) {
return nullptr;
}
if ((*i).second.state == UDPT_CST_CONNECTED &&
(*i).second.lastUpdated.difference(now) > 1_min) {
connectionIdCache_.erase(i);
return nullptr;
}
else {
return &(*i).second;
}
}
namespace {
struct FailConnectDelete {
bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
{
if (req->action == UDPT_ACT_ANNOUNCE && req->remoteAddr == remoteAddr &&
req->remotePort == remotePort) {
A2_LOG_INFO(
fmt("Force fail infohash=%s", util::toHex(req->infohash).c_str()));
req->state = UDPT_STA_COMPLETE;
req->error = error;
return true;
}
else {
return false;
}
}
std::string remoteAddr;
uint16_t remotePort;
int error;
FailConnectDelete(std::string remoteAddr, uint16_t remotePort, int error)
: remoteAddr(std::move(remoteAddr)), remotePort(remotePort), error(error)
{
}
};
} // namespace
void UDPTrackerClient::failConnect(const std::string& remoteAddr,
uint16_t remotePort, int error)
{
connectionIdCache_.erase(std::make_pair(remoteAddr, remotePort));
// Fail all requests which are waiting for connection ID of the host.
connectRequests_.erase(
std::remove_if(connectRequests_.begin(), connectRequests_.end(),
FailConnectDelete(remoteAddr, remotePort, error)),
connectRequests_.end());
pendingRequests_.erase(
std::remove_if(pendingRequests_.begin(), pendingRequests_.end(),
FailConnectDelete(remoteAddr, remotePort, error)),
pendingRequests_.end());
}
void UDPTrackerClient::failAll()
{
int error = UDPT_ERR_SHUTDOWN;
failRequest(inflightRequests_.begin(), inflightRequests_.end(), error);
failRequest(pendingRequests_.begin(), pendingRequests_.end(), error);
failRequest(connectRequests_.begin(), connectRequests_.end(), error);
}
void UDPTrackerClient::increaseWatchers() { ++numWatchers_; }
void UDPTrackerClient::decreaseWatchers() { --numWatchers_; }
ssize_t createUDPTrackerConnect(unsigned char* data, size_t length,
std::string& remoteAddr, uint16_t& remotePort,
const std::shared_ptr<UDPTrackerRequest>& req)
{
assert(length >= 16);
remoteAddr = req->remoteAddr;
remotePort = req->remotePort;
bittorrent::setLLIntParam(data, UDPT_INITIAL_CONNECTION_ID);
bittorrent::setIntParam(data + 8, req->action);
bittorrent::setIntParam(data + 12, req->transactionId);
return 16;
}
ssize_t createUDPTrackerAnnounce(unsigned char* data, size_t length,
std::string& remoteAddr, uint16_t& remotePort,
const std::shared_ptr<UDPTrackerRequest>& req)
{
assert(length >= 100);
remoteAddr = req->remoteAddr;
remotePort = req->remotePort;
bittorrent::setLLIntParam(data, req->connectionId);
bittorrent::setIntParam(data + 8, req->action);
bittorrent::setIntParam(data + 12, req->transactionId);
memcpy(data + 16, req->infohash.c_str(), req->infohash.size());
memcpy(data + 36, req->peerId.c_str(), req->peerId.size());
bittorrent::setLLIntParam(data + 56, req->downloaded);
bittorrent::setLLIntParam(data + 64, req->left);
bittorrent::setLLIntParam(data + 72, req->uploaded);
bittorrent::setIntParam(data + 80, req->event);
// ip is already network-byte order
memcpy(data + 84, &req->ip, sizeof(req->ip));
bittorrent::setIntParam(data + 88, req->key);
bittorrent::setIntParam(data + 92, req->numWant);
bittorrent::setShortIntParam(data + 96, req->port);
// extensions is always 0
bittorrent::setShortIntParam(data + 98, 0);
return 100;
}
const char* getUDPTrackerActionStr(int action)
{
switch (action) {
case UDPT_ACT_CONNECT:
return "CONNECT";
case UDPT_ACT_ANNOUNCE:
return "ANNOUNCE";
case UDPT_ACT_ERROR:
return "ERROR";
default:
return "(unknown)";
}
}
const char* getUDPTrackerEventStr(int event)
{
switch (event) {
case UDPT_EVT_NONE:
return "NONE";
case UDPT_EVT_COMPLETED:
return "COMPLETED";
case UDPT_EVT_STARTED:
return "STARTED";
case UDPT_EVT_STOPPED:
return "STOPPED";
default:
return "(unknown)";
}
}
} // namespace aria2
|