DefaultBtAnnounce.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "DefaultBtAnnounce.h"
  36. #include "LogFactory.h"
  37. #include "Logger.h"
  38. #include "util.h"
  39. #include "prefs.h"
  40. #include "DlAbortEx.h"
  41. #include "message.h"
  42. #include "SimpleRandomizer.h"
  43. #include "DownloadContext.h"
  44. #include "PieceStorage.h"
  45. #include "BtRuntime.h"
  46. #include "PeerStorage.h"
  47. #include "Peer.h"
  48. #include "Option.h"
  49. #include "fmt.h"
  50. #include "A2STR.h"
  51. #include "bencode2.h"
  52. #include "bittorrent_helper.h"
  53. #include "wallclock.h"
  54. #include "uri.h"
  55. #include "UDPTrackerRequest.h"
  56. #include "SocketCore.h"
  57. namespace aria2 {
  58. DefaultBtAnnounce::DefaultBtAnnounce(DownloadContext* downloadContext,
  59. const Option* option)
  60. : downloadContext_{downloadContext},
  61. trackers_(0),
  62. prevAnnounceTimer_(Timer::zero()),
  63. interval_(DEFAULT_ANNOUNCE_INTERVAL),
  64. minInterval_(DEFAULT_ANNOUNCE_INTERVAL),
  65. userDefinedInterval_(0_s),
  66. complete_(0),
  67. incomplete_(0),
  68. announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList),
  69. option_(option),
  70. randomizer_(SimpleRandomizer::getInstance().get()),
  71. tcpPort_(0)
  72. {
  73. }
  74. DefaultBtAnnounce::~DefaultBtAnnounce() = default;
  75. bool DefaultBtAnnounce::isDefaultAnnounceReady()
  76. {
  77. return (trackers_ == 0 &&
  78. prevAnnounceTimer_.difference(global::wallclock()) >=
  79. (userDefinedInterval_.count() == 0 ? minInterval_
  80. : userDefinedInterval_) &&
  81. !announceList_.allTiersFailed());
  82. }
  83. bool DefaultBtAnnounce::isStoppedAnnounceReady()
  84. {
  85. return (trackers_ == 0 && btRuntime_->isHalt() &&
  86. announceList_.countStoppedAllowedTier());
  87. }
  88. bool DefaultBtAnnounce::isCompletedAnnounceReady()
  89. {
  90. return (trackers_ == 0 && pieceStorage_->allDownloadFinished() &&
  91. announceList_.countCompletedAllowedTier());
  92. }
  93. bool DefaultBtAnnounce::isAnnounceReady()
  94. {
  95. return isStoppedAnnounceReady() || isCompletedAnnounceReady() ||
  96. isDefaultAnnounceReady();
  97. }
  98. namespace {
  99. bool uriHasQuery(const std::string& uri)
  100. {
  101. uri_split_result us;
  102. if (uri_split(&us, uri.c_str()) == 0) {
  103. return (us.field_set & (1 << USR_QUERY)) && us.fields[USR_QUERY].len > 0;
  104. }
  105. else {
  106. return false;
  107. }
  108. }
  109. } // namespace
  110. bool DefaultBtAnnounce::adjustAnnounceList()
  111. {
  112. if (isStoppedAnnounceReady()) {
  113. if (!announceList_.currentTierAcceptsStoppedEvent()) {
  114. announceList_.moveToStoppedAllowedTier();
  115. }
  116. announceList_.setEvent(AnnounceTier::STOPPED);
  117. }
  118. else if (isCompletedAnnounceReady()) {
  119. if (!announceList_.currentTierAcceptsCompletedEvent()) {
  120. announceList_.moveToCompletedAllowedTier();
  121. }
  122. announceList_.setEvent(AnnounceTier::COMPLETED);
  123. }
  124. else if (isDefaultAnnounceReady()) {
  125. // If download completed before "started" event is sent to a tracker,
  126. // we change the event to something else to prevent us from
  127. // sending "completed" event.
  128. if (pieceStorage_->allDownloadFinished() &&
  129. announceList_.getEvent() == AnnounceTier::STARTED) {
  130. announceList_.setEvent(AnnounceTier::STARTED_AFTER_COMPLETION);
  131. }
  132. }
  133. else {
  134. return false;
  135. }
  136. return true;
  137. }
  138. std::string DefaultBtAnnounce::getAnnounceUrl()
  139. {
  140. if (!adjustAnnounceList()) {
  141. return A2STR::NIL;
  142. }
  143. int numWant = 50;
  144. if (!btRuntime_->lessThanMinPeers() || btRuntime_->isHalt()) {
  145. numWant = 0;
  146. }
  147. NetStat& stat = downloadContext_->getNetStat();
  148. int64_t left =
  149. pieceStorage_->getTotalLength() - pieceStorage_->getCompletedLength();
  150. // Use last 8 bytes of peer ID as a key
  151. const size_t keyLen = 8;
  152. std::string uri = announceList_.getAnnounce();
  153. uri += uriHasQuery(uri) ? "&" : "?";
  154. uri +=
  155. fmt("info_hash=%s&"
  156. "peer_id=%s&"
  157. "uploaded=%" PRId64 "&"
  158. "downloaded=%" PRId64 "&"
  159. "left=%" PRId64 "&"
  160. "compact=1&"
  161. "key=%s&"
  162. "numwant=%d&"
  163. "no_peer_id=1",
  164. util::percentEncode(bittorrent::getInfoHash(downloadContext_),
  165. INFO_HASH_LENGTH)
  166. .c_str(),
  167. util::percentEncode(bittorrent::getStaticPeerId(), PEER_ID_LENGTH)
  168. .c_str(),
  169. stat.getSessionUploadLength(), stat.getSessionDownloadLength(), left,
  170. util::percentEncode(
  171. bittorrent::getStaticPeerId() + PEER_ID_LENGTH - keyLen, keyLen)
  172. .c_str(),
  173. numWant);
  174. if (tcpPort_) {
  175. uri += fmt("&port=%u", tcpPort_);
  176. }
  177. const char* event = announceList_.getEventString();
  178. if (event[0]) {
  179. uri += "&event=";
  180. uri += event;
  181. }
  182. if (!trackerId_.empty()) {
  183. uri += "&trackerid=";
  184. uri += util::percentEncode(trackerId_);
  185. }
  186. if (option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) ||
  187. option_->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  188. uri += "&requirecrypto=1";
  189. }
  190. else {
  191. uri += "&supportcrypto=1";
  192. }
  193. if (!option_->blank(PREF_BT_EXTERNAL_IP)) {
  194. uri += "&ip=";
  195. uri += option_->get(PREF_BT_EXTERNAL_IP);
  196. }
  197. return uri;
  198. }
  199. std::shared_ptr<UDPTrackerRequest> DefaultBtAnnounce::createUDPTrackerRequest(
  200. const std::string& remoteAddr, uint16_t remotePort, uint16_t localPort)
  201. {
  202. if (!adjustAnnounceList()) {
  203. return nullptr;
  204. }
  205. NetStat& stat = downloadContext_->getNetStat();
  206. int64_t left =
  207. pieceStorage_->getTotalLength() - pieceStorage_->getCompletedLength();
  208. auto req = std::make_shared<UDPTrackerRequest>();
  209. req->remoteAddr = remoteAddr;
  210. req->remotePort = remotePort;
  211. req->action = UDPT_ACT_ANNOUNCE;
  212. req->infohash = bittorrent::getTorrentAttrs(downloadContext_)->infoHash;
  213. const unsigned char* peerId = bittorrent::getStaticPeerId();
  214. req->peerId.assign(peerId, peerId + PEER_ID_LENGTH);
  215. req->downloaded = stat.getSessionDownloadLength();
  216. req->left = left;
  217. req->uploaded = stat.getSessionUploadLength();
  218. switch (announceList_.getEvent()) {
  219. case AnnounceTier::STARTED:
  220. case AnnounceTier::STARTED_AFTER_COMPLETION:
  221. req->event = UDPT_EVT_STARTED;
  222. break;
  223. case AnnounceTier::STOPPED:
  224. req->event = UDPT_EVT_STOPPED;
  225. break;
  226. case AnnounceTier::COMPLETED:
  227. req->event = UDPT_EVT_COMPLETED;
  228. break;
  229. default:
  230. req->event = 0;
  231. }
  232. if (!option_->blank(PREF_BT_EXTERNAL_IP)) {
  233. unsigned char dest[16];
  234. if (net::getBinAddr(dest, option_->get(PREF_BT_EXTERNAL_IP)) == 4) {
  235. memcpy(&req->ip, dest, 4);
  236. }
  237. else {
  238. req->ip = 0;
  239. }
  240. }
  241. else {
  242. req->ip = 0;
  243. }
  244. req->key = randomizer_->getRandomNumber(INT32_MAX);
  245. int numWant = 50;
  246. if (!btRuntime_->lessThanMinPeers() || btRuntime_->isHalt()) {
  247. numWant = 0;
  248. }
  249. req->numWant = numWant;
  250. req->port = localPort;
  251. req->extensions = 0;
  252. return req;
  253. }
  254. void DefaultBtAnnounce::announceStart() { ++trackers_; }
  255. void DefaultBtAnnounce::announceSuccess()
  256. {
  257. trackers_ = 0;
  258. announceList_.announceSuccess();
  259. }
  260. void DefaultBtAnnounce::announceFailure()
  261. {
  262. trackers_ = 0;
  263. announceList_.announceFailure();
  264. }
  265. bool DefaultBtAnnounce::isAllAnnounceFailed()
  266. {
  267. return announceList_.allTiersFailed();
  268. }
  269. void DefaultBtAnnounce::resetAnnounce()
  270. {
  271. prevAnnounceTimer_ = global::wallclock();
  272. announceList_.resetTier();
  273. }
  274. void DefaultBtAnnounce::processAnnounceResponse(
  275. const unsigned char* trackerResponse, size_t trackerResponseLength)
  276. {
  277. A2_LOG_DEBUG("Now processing tracker response.");
  278. auto decodedValue = bencode2::decode(trackerResponse, trackerResponseLength);
  279. const Dict* dict = downcast<Dict>(decodedValue);
  280. if (!dict) {
  281. throw DL_ABORT_EX(MSG_NULL_TRACKER_RESPONSE);
  282. }
  283. const String* failure =
  284. downcast<String>(dict->get(BtAnnounce::FAILURE_REASON));
  285. if (failure) {
  286. throw DL_ABORT_EX(fmt(EX_TRACKER_FAILURE, failure->s().c_str()));
  287. }
  288. const String* warn = downcast<String>(dict->get(BtAnnounce::WARNING_MESSAGE));
  289. if (warn) {
  290. A2_LOG_WARN(fmt(MSG_TRACKER_WARNING_MESSAGE, warn->s().c_str()));
  291. }
  292. const String* tid = downcast<String>(dict->get(BtAnnounce::TRACKER_ID));
  293. if (tid) {
  294. trackerId_ = tid->s();
  295. A2_LOG_DEBUG(fmt("Tracker ID:%s", trackerId_.c_str()));
  296. }
  297. const Integer* ival = downcast<Integer>(dict->get(BtAnnounce::INTERVAL));
  298. if (ival && ival->i() > 0) {
  299. interval_ = std::chrono::seconds(ival->i());
  300. A2_LOG_DEBUG(fmt("Interval:%ld", static_cast<long int>(interval_.count())));
  301. }
  302. const Integer* mival = downcast<Integer>(dict->get(BtAnnounce::MIN_INTERVAL));
  303. if (mival && mival->i() > 0) {
  304. minInterval_ = std::chrono::seconds(mival->i());
  305. A2_LOG_DEBUG(
  306. fmt("Min interval:%ld", static_cast<long int>(minInterval_.count())));
  307. minInterval_ = std::min(minInterval_, interval_);
  308. }
  309. else {
  310. // Use interval as a minInterval if minInterval is not supplied.
  311. minInterval_ = interval_;
  312. }
  313. const Integer* comp = downcast<Integer>(dict->get(BtAnnounce::COMPLETE));
  314. if (comp && comp->i() >= 0) {
  315. complete_ = comp->i();
  316. A2_LOG_DEBUG(fmt("Complete:%d", complete_));
  317. }
  318. const Integer* incomp = downcast<Integer>(dict->get(BtAnnounce::INCOMPLETE));
  319. if (incomp && incomp->i() >= 0) {
  320. incomplete_ = incomp->i();
  321. A2_LOG_DEBUG(fmt("Incomplete:%d", incomplete_));
  322. }
  323. auto peerData = dict->get(BtAnnounce::PEERS);
  324. if (!peerData) {
  325. A2_LOG_INFO(MSG_NO_PEER_LIST_RECEIVED);
  326. }
  327. else {
  328. if (!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
  329. std::vector<std::shared_ptr<Peer>> peers;
  330. bittorrent::extractPeer(peerData, AF_INET, std::back_inserter(peers));
  331. peerStorage_->addPeer(peers);
  332. }
  333. }
  334. auto peer6Data = dict->get(BtAnnounce::PEERS6);
  335. if (!peer6Data) {
  336. A2_LOG_INFO("No peers6 received.");
  337. }
  338. else {
  339. if (!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
  340. std::vector<std::shared_ptr<Peer>> peers;
  341. bittorrent::extractPeer(peer6Data, AF_INET6, std::back_inserter(peers));
  342. peerStorage_->addPeer(peers);
  343. }
  344. }
  345. }
  346. void DefaultBtAnnounce::processUDPTrackerResponse(
  347. const std::shared_ptr<UDPTrackerRequest>& req)
  348. {
  349. const std::shared_ptr<UDPTrackerReply>& reply = req->reply;
  350. A2_LOG_DEBUG("Now processing UDP tracker response.");
  351. if (reply->interval > 0) {
  352. minInterval_ = std::chrono::seconds(reply->interval);
  353. A2_LOG_DEBUG(
  354. fmt("Min interval:%ld", static_cast<long int>(minInterval_.count())));
  355. interval_ = minInterval_;
  356. }
  357. complete_ = reply->seeders;
  358. A2_LOG_DEBUG(fmt("Complete:%d", reply->seeders));
  359. incomplete_ = reply->leechers;
  360. A2_LOG_DEBUG(fmt("Incomplete:%d", reply->leechers));
  361. if (!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
  362. for (auto& elem : reply->peers) {
  363. peerStorage_->addPeer(std::make_shared<Peer>(elem.first, elem.second));
  364. }
  365. }
  366. }
  367. bool DefaultBtAnnounce::noMoreAnnounce()
  368. {
  369. return (trackers_ == 0 && btRuntime_->isHalt() &&
  370. !announceList_.countStoppedAllowedTier());
  371. }
  372. void DefaultBtAnnounce::shuffleAnnounce() { announceList_.shuffle(); }
  373. void DefaultBtAnnounce::setRandomizer(Randomizer* randomizer)
  374. {
  375. randomizer_ = randomizer;
  376. }
  377. void DefaultBtAnnounce::setBtRuntime(
  378. const std::shared_ptr<BtRuntime>& btRuntime)
  379. {
  380. btRuntime_ = btRuntime;
  381. }
  382. void DefaultBtAnnounce::setPieceStorage(
  383. const std::shared_ptr<PieceStorage>& pieceStorage)
  384. {
  385. pieceStorage_ = pieceStorage;
  386. }
  387. void DefaultBtAnnounce::setPeerStorage(
  388. const std::shared_ptr<PeerStorage>& peerStorage)
  389. {
  390. peerStorage_ = peerStorage;
  391. }
  392. void DefaultBtAnnounce::overrideMinInterval(std::chrono::seconds interval)
  393. {
  394. minInterval_ = std::move(interval);
  395. }
  396. } // namespace aria2