UDPTrackerClient.cc 21 KB

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