UDPTrackerClient.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "UDPTrackerClient.h"
  36. #include "UDPTrackerRequest.h"
  37. #include "bittorrent_helper.h"
  38. #include "util.h"
  39. #include "LogFactory.h"
  40. #include "SimpleRandomizer.h"
  41. #include "fmt.h"
  42. namespace aria2 {
  43. UDPTrackerClient::UDPTrackerClient()
  44. : numWatchers_(0)
  45. {}
  46. namespace {
  47. template<typename InputIterator>
  48. void failRequest(InputIterator first, InputIterator last, int error)
  49. {
  50. for(; first != last; ++first) {
  51. (*first)->state = UDPT_STA_COMPLETE;
  52. (*first)->error = error;
  53. }
  54. }
  55. } // namespace
  56. namespace {
  57. int32_t generateTransactionId()
  58. {
  59. return SimpleRandomizer::getInstance()->getRandomNumber(INT32_MAX);
  60. }
  61. } // namespace
  62. namespace {
  63. void logInvalidLength(const std::string& remoteAddr, uint16_t remotePort,
  64. int action, unsigned long expected, unsigned long actual)
  65. {
  66. A2_LOG_INFO(fmt("UDPT received %s reply from %s:%u invalid length "
  67. "expected:%lu, actual:%lu",
  68. getUDPTrackerActionStr(action),
  69. remoteAddr.c_str(), remotePort, expected, actual));
  70. }
  71. } // namespace
  72. namespace {
  73. void logInvalidTransaction(const std::string& remoteAddr, uint16_t remotePort,
  74. int action, int32_t transactionId)
  75. {
  76. A2_LOG_INFO(fmt("UDPT received %s reply from %s:%u invalid transaction_id=%d",
  77. getUDPTrackerActionStr(action),
  78. remoteAddr.c_str(), remotePort, transactionId));
  79. }
  80. } // namespace
  81. namespace {
  82. void logTooShortLength(const std::string& remoteAddr, uint16_t remotePort,
  83. int action,
  84. unsigned long minLength, unsigned long actual)
  85. {
  86. A2_LOG_INFO(fmt("UDPT received %s reply from %s:%u length too short "
  87. "min:%lu, actual:%lu",
  88. getUDPTrackerActionStr(action),
  89. remoteAddr.c_str(), remotePort, minLength, actual));
  90. }
  91. } // namespace
  92. UDPTrackerClient::~UDPTrackerClient()
  93. {
  94. // Make all contained requests fail
  95. int error = UDPT_ERR_SHUTDOWN;
  96. failRequest(inflightRequests_.begin(), inflightRequests_.end(), error);
  97. failRequest(pendingRequests_.begin(), pendingRequests_.end(), error);
  98. failRequest(connectRequests_.begin(), connectRequests_.end(), error);
  99. }
  100. namespace {
  101. struct CollectAddrPortMatch {
  102. bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
  103. {
  104. if(req->remoteAddr == remoteAddr && req->remotePort == remotePort) {
  105. dest.push_back(req);
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. std::vector<std::shared_ptr<UDPTrackerRequest> >& dest;
  112. std::string remoteAddr;
  113. uint16_t remotePort;
  114. CollectAddrPortMatch(std::vector<std::shared_ptr<UDPTrackerRequest> >& dest,
  115. std::string remoteAddr, uint16_t remotePort)
  116. : dest(dest), remoteAddr(std::move(remoteAddr)), remotePort(remotePort)
  117. {}
  118. };
  119. } // namespace
  120. int UDPTrackerClient::receiveReply
  121. (const unsigned char* data, size_t length, const std::string& remoteAddr,
  122. uint16_t remotePort, const Timer& now)
  123. {
  124. int32_t action = bittorrent::getIntParam(data, 0);
  125. switch(action) {
  126. case UDPT_ACT_CONNECT: {
  127. if(length != 16) {
  128. logInvalidLength(remoteAddr, remotePort, action, 16, length);
  129. return -1;
  130. }
  131. int32_t transactionId = bittorrent::getIntParam(data, 4);
  132. std::shared_ptr<UDPTrackerRequest> req =
  133. findInflightRequest(remoteAddr, remotePort, transactionId, true);
  134. if(!req) {
  135. logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
  136. return -1;
  137. }
  138. req->state = UDPT_STA_COMPLETE;
  139. int64_t connectionId = bittorrent::getLLIntParam(data, 8);
  140. A2_LOG_INFO(fmt("UDPT received CONNECT reply from %s:%u transaction_id=%u,"
  141. "connection_id=%" PRId64, remoteAddr.c_str(), remotePort,
  142. transactionId, connectionId));
  143. UDPTrackerConnection c(UDPT_CST_CONNECTED, connectionId, now);
  144. connectionIdCache_[std::make_pair(remoteAddr, remotePort)] = c;
  145. // Now we have connection ID, push requests which are waiting for
  146. // it.
  147. std::vector<std::shared_ptr<UDPTrackerRequest> > reqs;
  148. connectRequests_.erase(std::remove_if
  149. (connectRequests_.begin(), connectRequests_.end(),
  150. CollectAddrPortMatch(reqs, remoteAddr, remotePort)),
  151. connectRequests_.end());
  152. pendingRequests_.insert(pendingRequests_.begin(),
  153. reqs.begin(), reqs.end());
  154. break;
  155. }
  156. case UDPT_ACT_ANNOUNCE: {
  157. if(length < 20) {
  158. logTooShortLength(remoteAddr, remotePort, action, 20, length);
  159. return - 1;
  160. }
  161. int32_t transactionId = bittorrent::getIntParam(data, 4);
  162. std::shared_ptr<UDPTrackerRequest> req =
  163. findInflightRequest(remoteAddr, remotePort, transactionId, true);
  164. if(!req) {
  165. logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
  166. return -1;
  167. }
  168. req->state = UDPT_STA_COMPLETE;
  169. req->reply = std::make_shared<UDPTrackerReply>();
  170. req->reply->action = action;
  171. req->reply->transactionId = transactionId;
  172. req->reply->interval = bittorrent::getIntParam(data, 8);
  173. req->reply->leechers = bittorrent::getIntParam(data, 12);
  174. req->reply->seeders = bittorrent::getIntParam(data, 16);
  175. int numPeers = 0;
  176. for(size_t i = 20; i < length; i += 6) {
  177. std::pair<std::string, uint16_t> hostport =
  178. bittorrent::unpackcompact(data+i, AF_INET);
  179. if(!hostport.first.empty()) {
  180. req->reply->peers.push_back(hostport);
  181. ++numPeers;
  182. }
  183. }
  184. A2_LOG_INFO(fmt("UDPT received ANNOUNCE reply from %s:%u transaction_id=%u,"
  185. "connection_id=%" PRId64 ", event=%s, infohash=%s, "
  186. "interval=%d, leechers=%d, "
  187. "seeders=%d, num_peers=%d", remoteAddr.c_str(), remotePort,
  188. transactionId, req->connectionId,
  189. getUDPTrackerEventStr(req->event),
  190. util::toHex(req->infohash).c_str(),
  191. req->reply->interval, req->reply->leechers,
  192. req->reply->seeders, numPeers));
  193. break;
  194. }
  195. case UDPT_ACT_ERROR: {
  196. if(length < 8) {
  197. logTooShortLength(remoteAddr, remotePort, action, 8, length);
  198. return -1;
  199. }
  200. int32_t transactionId = bittorrent::getIntParam(data, 4);
  201. std::shared_ptr<UDPTrackerRequest> req =
  202. findInflightRequest(remoteAddr, remotePort, transactionId, true);
  203. if(!req) {
  204. logInvalidTransaction(remoteAddr, remotePort, action, transactionId);
  205. return -1;
  206. }
  207. std::string errorString(data+8, data+length);
  208. errorString = util::encodeNonUtf8(errorString);
  209. req->state = UDPT_STA_COMPLETE;
  210. req->error = UDPT_ERR_TRACKER;
  211. A2_LOG_INFO(fmt("UDPT received ERROR reply from %s:%u transaction_id=%u,"
  212. "connection_id=%" PRId64 ", action=%d, error_string=%s",
  213. remoteAddr.c_str(),
  214. remotePort, transactionId, req->connectionId, action,
  215. errorString.c_str()));
  216. if(req->action == UDPT_ACT_CONNECT) {
  217. failConnect(req->remoteAddr, req->remotePort, UDPT_ERR_TRACKER);
  218. }
  219. break;
  220. }
  221. case UDPT_ACT_SCRAPE:
  222. A2_LOG_INFO(fmt("unexpected scrape action reply from %s:%u",
  223. remoteAddr.c_str(), remotePort));
  224. return -1;
  225. default:
  226. A2_LOG_INFO(fmt("unknown action reply from %s:%u",
  227. remoteAddr.c_str(), remotePort));
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. ssize_t UDPTrackerClient::createRequest
  233. (unsigned char* data, size_t length, std::string& remoteAddr,
  234. uint16_t& remotePort, const Timer& now)
  235. {
  236. if(pendingRequests_.empty()) {
  237. return -1;
  238. }
  239. while(!pendingRequests_.empty()) {
  240. const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
  241. if(req->action == UDPT_ACT_CONNECT) {
  242. ssize_t rv;
  243. rv = createUDPTrackerConnect(data, length, remoteAddr, remotePort, req);
  244. return rv;
  245. }
  246. UDPTrackerConnection* c = getConnectionId(req->remoteAddr,
  247. req->remotePort,
  248. now);
  249. if(!c) {
  250. auto creq = std::make_shared<UDPTrackerRequest>();
  251. creq->action = UDPT_ACT_CONNECT;
  252. creq->remoteAddr = req->remoteAddr;
  253. creq->remotePort = req->remotePort;
  254. creq->transactionId = generateTransactionId();
  255. pendingRequests_.push_front(creq);
  256. ssize_t rv;
  257. rv = createUDPTrackerConnect(data, length, remoteAddr, remotePort, creq);
  258. return rv;
  259. }
  260. if(c->state == UDPT_CST_CONNECTING) {
  261. connectRequests_.push_back(req);
  262. pendingRequests_.pop_front();
  263. continue;
  264. }
  265. req->connectionId = c->connectionId;
  266. req->transactionId = generateTransactionId();
  267. ssize_t rv;
  268. rv = createUDPTrackerAnnounce(data, length, remoteAddr, remotePort, req);
  269. return rv;
  270. }
  271. return -1;
  272. }
  273. void UDPTrackerClient::requestSent(const Timer& now)
  274. {
  275. if(pendingRequests_.empty()) {
  276. A2_LOG_WARN("pendingRequests_ is empty");
  277. return;
  278. }
  279. const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
  280. switch(req->action) {
  281. case UDPT_ACT_CONNECT:
  282. A2_LOG_INFO(fmt("UDPT sent CONNECT to %s:%u transaction_id=%u",
  283. req->remoteAddr.c_str(), req->remotePort,
  284. req->transactionId));
  285. break;
  286. case UDPT_ACT_ANNOUNCE:
  287. A2_LOG_INFO(fmt("UDPT sent ANNOUNCE to %s:%u transaction_id=%u, "
  288. "connection_id=%" PRId64 ", event=%s, infohash=%s",
  289. req->remoteAddr.c_str(), req->remotePort,
  290. req->transactionId, req->connectionId,
  291. getUDPTrackerEventStr(req->event),
  292. util::toHex(req->infohash).c_str()));
  293. break;
  294. default:
  295. // unreachable
  296. assert(0);
  297. }
  298. req->dispatched = now;
  299. switch(req->action) {
  300. case UDPT_ACT_CONNECT: {
  301. connectionIdCache_[std::make_pair(req->remoteAddr, req->remotePort)]
  302. = UDPTrackerConnection();
  303. break;
  304. }
  305. }
  306. inflightRequests_.push_back(req);
  307. pendingRequests_.pop_front();
  308. }
  309. void UDPTrackerClient::requestFail(int error)
  310. {
  311. if(pendingRequests_.empty()) {
  312. A2_LOG_WARN("pendingRequests_ is empty");
  313. return;
  314. }
  315. const std::shared_ptr<UDPTrackerRequest>& req = pendingRequests_.front();
  316. switch(req->action) {
  317. case UDPT_ACT_CONNECT:
  318. A2_LOG_INFO(fmt("UDPT fail CONNECT to %s:%u transaction_id=%u",
  319. req->remoteAddr.c_str(), req->remotePort,
  320. req->transactionId));
  321. failConnect(req->remoteAddr, req->remotePort, error);
  322. break;
  323. case UDPT_ACT_ANNOUNCE:
  324. A2_LOG_INFO(fmt("UDPT fail ANNOUNCE to %s:%u transaction_id=%u, "
  325. "connection_id=%" PRId64 ", event=%s, infohash=%s",
  326. req->remoteAddr.c_str(), req->remotePort,
  327. req->transactionId, req->connectionId,
  328. getUDPTrackerEventStr(req->event),
  329. util::toHex(req->infohash).c_str()));
  330. break;
  331. default:
  332. // unreachable
  333. assert(0);
  334. }
  335. req->state = UDPT_STA_COMPLETE;
  336. req->error = error;
  337. pendingRequests_.pop_front();
  338. }
  339. void UDPTrackerClient::addRequest(const std::shared_ptr<UDPTrackerRequest>& req)
  340. {
  341. req->state = UDPT_STA_PENDING;
  342. req->error = UDPT_ERR_SUCCESS;
  343. pendingRequests_.push_back(req);
  344. }
  345. namespace {
  346. struct TimeoutCheck {
  347. bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
  348. {
  349. int t = req->dispatched.difference(now);
  350. if(req->failCount == 0) {
  351. if(t >= 15) {
  352. switch(req->action) {
  353. case UDPT_ACT_CONNECT:
  354. A2_LOG_INFO(fmt("UDPT resend CONNECT to %s:%u transaction_id=%u",
  355. req->remoteAddr.c_str(), req->remotePort,
  356. req->transactionId));
  357. break;
  358. case UDPT_ACT_ANNOUNCE:
  359. A2_LOG_INFO(fmt("UDPT resend ANNOUNCE to %s:%u transaction_id=%u, "
  360. "connection_id=%" PRId64 ", event=%s, infohash=%s",
  361. req->remoteAddr.c_str(), req->remotePort,
  362. req->transactionId, req->connectionId,
  363. getUDPTrackerEventStr(req->event),
  364. util::toHex(req->infohash).c_str()));
  365. break;
  366. default:
  367. // unreachable
  368. assert(0);
  369. }
  370. ++req->failCount;
  371. dest.push_back(req);
  372. return true;
  373. } else {
  374. return false;
  375. }
  376. } else {
  377. if(t >= 60) {
  378. switch(req->action) {
  379. case UDPT_ACT_CONNECT:
  380. A2_LOG_INFO(fmt("UDPT timeout CONNECT to %s:%u transaction_id=%u",
  381. req->remoteAddr.c_str(), req->remotePort,
  382. req->transactionId));
  383. client->failConnect(req->remoteAddr, req->remotePort,
  384. UDPT_ERR_TIMEOUT);
  385. break;
  386. case UDPT_ACT_ANNOUNCE:
  387. A2_LOG_INFO(fmt("UDPT timeout ANNOUNCE to %s:%u transaction_id=%u, "
  388. "connection_id=%" PRId64 ", event=%s, infohash=%s",
  389. req->remoteAddr.c_str(), req->remotePort,
  390. req->transactionId, req->connectionId,
  391. getUDPTrackerEventStr(req->event),
  392. util::toHex(req->infohash).c_str()));
  393. break;
  394. default:
  395. // unreachable
  396. assert(0);
  397. }
  398. ++req->failCount;
  399. req->state = UDPT_STA_COMPLETE;
  400. req->error = UDPT_ERR_TIMEOUT;
  401. return true;
  402. } else {
  403. return false;
  404. }
  405. }
  406. }
  407. std::vector<std::shared_ptr<UDPTrackerRequest> >& dest;
  408. UDPTrackerClient* client;
  409. const Timer& now;
  410. TimeoutCheck(std::vector<std::shared_ptr<UDPTrackerRequest> >& dest,
  411. UDPTrackerClient* client,
  412. const Timer& now)
  413. : dest(dest), client(client), now(now)
  414. {}
  415. };
  416. } // namespace
  417. void UDPTrackerClient::handleTimeout(const Timer& now)
  418. {
  419. std::vector<std::shared_ptr<UDPTrackerRequest> > dest;
  420. inflightRequests_.erase(std::remove_if(inflightRequests_.begin(),
  421. inflightRequests_.end(),
  422. TimeoutCheck(dest, this, now)),
  423. inflightRequests_.end());
  424. pendingRequests_.insert(pendingRequests_.begin(), dest.begin(), dest.end());
  425. }
  426. std::shared_ptr<UDPTrackerRequest> UDPTrackerClient::findInflightRequest
  427. (const std::string& remoteAddr, uint16_t remotePort, int32_t transactionId,
  428. bool remove)
  429. {
  430. std::shared_ptr<UDPTrackerRequest> res;
  431. for(auto i = inflightRequests_.begin(), eoi = inflightRequests_.end();
  432. i != eoi;
  433. ++i) {
  434. if((*i)->remoteAddr == remoteAddr && (*i)->remotePort == remotePort &&
  435. (*i)->transactionId == transactionId) {
  436. res = *i;
  437. if(remove) {
  438. inflightRequests_.erase(i);
  439. }
  440. break;
  441. }
  442. }
  443. return res;
  444. }
  445. UDPTrackerConnection* UDPTrackerClient::getConnectionId
  446. (const std::string& remoteAddr, uint16_t remotePort, const Timer& now)
  447. {
  448. auto i = connectionIdCache_.find(std::make_pair(remoteAddr, remotePort));
  449. if(i == connectionIdCache_.end()) {
  450. return nullptr;
  451. }
  452. if((*i).second.state == UDPT_CST_CONNECTED &&
  453. (*i).second.lastUpdated.difference(now) > 60) {
  454. connectionIdCache_.erase(i);
  455. return nullptr;
  456. } else {
  457. return &(*i).second;
  458. }
  459. }
  460. namespace {
  461. struct FailConnectDelete {
  462. bool operator()(const std::shared_ptr<UDPTrackerRequest>& req) const
  463. {
  464. if(req->action == UDPT_ACT_ANNOUNCE &&
  465. req->remoteAddr == remoteAddr && req->remotePort == remotePort) {
  466. A2_LOG_INFO(fmt("Force fail infohash=%s",
  467. util::toHex(req->infohash).c_str()));
  468. req->state = UDPT_STA_COMPLETE;
  469. req->error = error;
  470. return true;
  471. } else {
  472. return false;
  473. }
  474. }
  475. std::string remoteAddr;
  476. uint16_t remotePort;
  477. int error;
  478. FailConnectDelete(std::string remoteAddr, uint16_t remotePort,
  479. int error)
  480. : remoteAddr(std::move(remoteAddr)), remotePort(remotePort), error(error)
  481. {}
  482. };
  483. } // namespace
  484. void UDPTrackerClient::failConnect(const std::string& remoteAddr,
  485. uint16_t remotePort, int error)
  486. {
  487. connectionIdCache_.erase(std::make_pair(remoteAddr, remotePort));
  488. // Fail all requests which are waiting for connection ID of the host.
  489. connectRequests_.erase(std::remove_if(connectRequests_.begin(),
  490. connectRequests_.end(),
  491. FailConnectDelete
  492. (remoteAddr, remotePort, error)),
  493. connectRequests_.end());
  494. pendingRequests_.erase(std::remove_if(pendingRequests_.begin(),
  495. pendingRequests_.end(),
  496. FailConnectDelete
  497. (remoteAddr, remotePort, error)),
  498. pendingRequests_.end());
  499. }
  500. void UDPTrackerClient::failAll()
  501. {
  502. int error = UDPT_ERR_SHUTDOWN;
  503. failRequest(inflightRequests_.begin(), inflightRequests_.end(), error);
  504. failRequest(pendingRequests_.begin(), pendingRequests_.end(), error);
  505. failRequest(connectRequests_.begin(), connectRequests_.end(), error);
  506. }
  507. void UDPTrackerClient::increaseWatchers()
  508. {
  509. ++numWatchers_;
  510. }
  511. void UDPTrackerClient::decreaseWatchers()
  512. {
  513. --numWatchers_;
  514. }
  515. ssize_t createUDPTrackerConnect
  516. (unsigned char* data, size_t length,
  517. std::string& remoteAddr, uint16_t& remotePort,
  518. const std::shared_ptr<UDPTrackerRequest>& req)
  519. {
  520. assert(length >= 16);
  521. remoteAddr = req->remoteAddr;
  522. remotePort = req->remotePort;
  523. bittorrent::setLLIntParam(data, UDPT_INITIAL_CONNECTION_ID);
  524. bittorrent::setIntParam(data+8, req->action);
  525. bittorrent::setIntParam(data+12, req->transactionId);
  526. return 16;
  527. }
  528. ssize_t createUDPTrackerAnnounce
  529. (unsigned char* data, size_t length,
  530. std::string& remoteAddr, uint16_t& remotePort,
  531. const std::shared_ptr<UDPTrackerRequest>& req)
  532. {
  533. assert(length >= 100);
  534. remoteAddr = req->remoteAddr;
  535. remotePort = req->remotePort;
  536. bittorrent::setLLIntParam(data, req->connectionId);
  537. bittorrent::setIntParam(data+8, req->action);
  538. bittorrent::setIntParam(data+12, req->transactionId);
  539. memcpy(data+16, req->infohash.c_str(), req->infohash.size());
  540. memcpy(data+36, req->peerId.c_str(), req->peerId.size());
  541. bittorrent::setLLIntParam(data+56, req->downloaded);
  542. bittorrent::setLLIntParam(data+64, req->left);
  543. bittorrent::setLLIntParam(data+72, req->uploaded);
  544. bittorrent::setIntParam(data+80, req->event);
  545. // ip is already network-byte order
  546. memcpy(data+84, &req->ip, sizeof(req->ip));
  547. bittorrent::setIntParam(data+88, req->key);
  548. bittorrent::setIntParam(data+92, req->numWant);
  549. bittorrent::setShortIntParam(data+96, req->port);
  550. // extensions is always 0
  551. bittorrent::setShortIntParam(data+98, 0);
  552. return 100;
  553. }
  554. const char* getUDPTrackerActionStr(int action)
  555. {
  556. switch(action) {
  557. case UDPT_ACT_CONNECT:
  558. return "CONNECT";
  559. case UDPT_ACT_ANNOUNCE:
  560. return "ANNOUNCE";
  561. case UDPT_ACT_ERROR:
  562. return "ERROR";
  563. default:
  564. return "(unknown)";
  565. }
  566. }
  567. const char* getUDPTrackerEventStr(int event)
  568. {
  569. switch(event) {
  570. case UDPT_EVT_NONE:
  571. return "NONE";
  572. case UDPT_EVT_COMPLETED:
  573. return "COMPLETED";
  574. case UDPT_EVT_STARTED:
  575. return "STARTED";
  576. case UDPT_EVT_STOPPED:
  577. return "STOPPED";
  578. default:
  579. return "(unknown)";
  580. }
  581. }
  582. } // namespace aria2