bittorrent_helper.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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 "bittorrent_helper.h"
  36. #include <algorithm>
  37. #include <deque>
  38. #include "DownloadContext.h"
  39. #include "Randomizer.h"
  40. #include "bencode.h"
  41. #include "Util.h"
  42. #include "DlAbortEx.h"
  43. #include "message.h"
  44. #include "StringFormat.h"
  45. #include "BtConstants.h"
  46. #include "messageDigest.h"
  47. #include "MessageDigestHelper.h"
  48. #include "PeerMessageUtil.h"
  49. #include "SimpleRandomizer.h"
  50. namespace aria2 {
  51. namespace bittorrent {
  52. static const std::string C_NAME("name");
  53. static const std::string C_NAME_UTF8("name.utf-8");
  54. static const std::string C_FILES("files");
  55. static const std::string C_LENGTH("length");
  56. static const std::string C_PATH("path");
  57. static const std::string C_PATH_UTF8("path.utf-8");
  58. static const std::string C_INFO("info");
  59. static const std::string C_PIECES("pieces");
  60. static const std::string C_PIECE_LENGTH("piece length");
  61. static const std::string C_PRIVATE("private");
  62. static const std::string C_URL_LIST("url-list");
  63. static const std::string C_ANNOUNCE("announce");
  64. static const std::string C_ANNOUNCE_LIST("announce-list");
  65. static const std::string C_NODES("nodes");
  66. static const std::string DEFAULT_PEER_ID_PREFIX("-aria2-");
  67. const std::string INFO_HASH("infoHash");
  68. const std::string MODE("mode");
  69. const std::string PRIVATE("private");
  70. const std::string ANNOUNCE_LIST("announceList");
  71. const std::string NODES("nodes");
  72. const std::string HOSTNAME("hostname");
  73. const std::string PORT("port");
  74. const std::string NAME("name");
  75. const std::string BITTORRENT("bittorrent");
  76. const std::string MULTI("multi");
  77. const std::string SINGLE("single");
  78. static void extractPieceHash(const SharedHandle<DownloadContext>& ctx,
  79. const std::string& hashData,
  80. size_t hashLength,
  81. size_t numPieces)
  82. {
  83. std::vector<std::string> pieceHashes;
  84. pieceHashes.reserve(numPieces);
  85. for(size_t i = 0; i < numPieces; ++i) {
  86. pieceHashes.push_back(Util::toHex(hashData.data()+i*hashLength,
  87. hashLength));
  88. }
  89. ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end());
  90. ctx->setPieceHashAlgo(MessageDigestContext::SHA1);
  91. }
  92. static void extractUrlList(std::vector<std::string>& uris, const BDE& bde)
  93. {
  94. if(bde.isList()) {
  95. for(BDE::List::const_iterator itr = bde.listBegin();
  96. itr != bde.listEnd(); ++itr) {
  97. if((*itr).isString()) {
  98. uris.push_back((*itr).s());
  99. }
  100. }
  101. } else if(bde.isString()) {
  102. uris.push_back(bde.s());
  103. }
  104. }
  105. template<typename InputIterator, typename OutputIterator>
  106. static OutputIterator createUri
  107. (InputIterator first, InputIterator last, OutputIterator out,
  108. const std::string& filePath)
  109. {
  110. for(; first != last; ++first) {
  111. if(Util::endsWith(*first, "/")) {
  112. *out++ = (*first)+filePath;
  113. } else {
  114. *out++ = (*first)+"/"+filePath;
  115. }
  116. }
  117. return out;
  118. }
  119. static void extractFileEntries
  120. (const SharedHandle<DownloadContext>& ctx,
  121. BDE& torrent,
  122. const BDE& infoDict,
  123. const std::string& defaultName,
  124. const std::string& overrideName,
  125. const std::vector<std::string>& urlList)
  126. {
  127. std::string name;
  128. if(overrideName.empty()) {
  129. std::string nameKey;
  130. if(infoDict.containsKey(C_NAME_UTF8)) {
  131. nameKey = C_NAME_UTF8;
  132. } else {
  133. nameKey = C_NAME;
  134. }
  135. const BDE& nameData = infoDict[nameKey];
  136. if(nameData.isString()) {
  137. // Slice path by '/' just in case nasty ".." is included in name
  138. std::deque<std::string> pathelems;
  139. Util::slice(pathelems, nameData.s(), '/');
  140. name = Util::joinPath(pathelems.begin(), pathelems.end());
  141. torrent[NAME] = nameData.s();
  142. } else {
  143. name = strconcat(File(defaultName).getBasename(), ".file");
  144. torrent[NAME] = name;
  145. }
  146. } else {
  147. name = overrideName;
  148. torrent[NAME] = name;
  149. }
  150. const BDE& filesList = infoDict[C_FILES];
  151. std::vector<SharedHandle<FileEntry> > fileEntries;
  152. if(filesList.isList()) {
  153. fileEntries.reserve(filesList.size());
  154. uint64_t length = 0;
  155. off_t offset = 0;
  156. // multi-file mode
  157. torrent[MODE] = MULTI;
  158. for(BDE::List::const_iterator itr = filesList.listBegin();
  159. itr != filesList.listEnd(); ++itr) {
  160. const BDE& fileDict = *itr;
  161. if(!fileDict.isDict()) {
  162. continue;
  163. }
  164. const BDE& fileLengthData = fileDict[C_LENGTH];
  165. if(!fileLengthData.isInteger()) {
  166. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  167. C_LENGTH.c_str()).str());
  168. }
  169. length += fileLengthData.i();
  170. std::string pathKey;
  171. if(fileDict.containsKey(C_PATH_UTF8)) {
  172. pathKey = C_PATH_UTF8;
  173. } else {
  174. pathKey = C_PATH;
  175. }
  176. const BDE& pathList = fileDict[pathKey];
  177. if(!pathList.isList() || pathList.empty()) {
  178. throw DL_ABORT_EX("Path is empty.");
  179. }
  180. std::vector<std::string> pathelem(pathList.size());
  181. std::transform(pathList.listBegin(), pathList.listEnd(), pathelem.begin(),
  182. std::mem_fun_ref(&BDE::s));
  183. std::string path = name;
  184. strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end()));
  185. // Split path with '/' again because each pathList element can
  186. // contain "/" inside.
  187. std::deque<std::string> elements;
  188. Util::slice(elements, path, '/');
  189. path = Util::joinPath(elements.begin(), elements.end());
  190. std::deque<std::string> uris;
  191. createUri(urlList.begin(), urlList.end(), std::back_inserter(uris), path);
  192. SharedHandle<FileEntry> fileEntry
  193. (new FileEntry(strconcat(ctx->getDir(), "/", path),
  194. fileLengthData.i(),
  195. offset, uris));
  196. fileEntries.push_back(fileEntry);
  197. offset += fileEntry->getLength();
  198. }
  199. } else {
  200. // single-file mode;
  201. torrent[MODE] = SINGLE;
  202. const BDE& lengthData = infoDict[C_LENGTH];
  203. if(!lengthData.isInteger()) {
  204. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  205. C_LENGTH.c_str()).str());
  206. }
  207. uint64_t totalLength = lengthData.i();
  208. // For each uri in urlList, if it ends with '/', then
  209. // concatenate name to it. Specification just says so.
  210. std::deque<std::string> uris;
  211. for(std::vector<std::string>::const_iterator i = urlList.begin();
  212. i != urlList.end(); ++i) {
  213. if(Util::endsWith(*i, "/")) {
  214. uris.push_back((*i)+name);
  215. } else {
  216. uris.push_back(*i);
  217. }
  218. }
  219. SharedHandle<FileEntry> fileEntry
  220. (new FileEntry(strconcat(ctx->getDir(), "/", name),totalLength, 0,
  221. uris));
  222. fileEntries.push_back(fileEntry);
  223. }
  224. ctx->setFileEntries(fileEntries.begin(), fileEntries.end());
  225. if(torrent[MODE].s() == MULTI) {
  226. ctx->setBasePath(strconcat(ctx->getDir(), "/", name));
  227. }
  228. }
  229. static void extractAnnounce(BDE& torrent, const BDE& rootDict)
  230. {
  231. const BDE& announceList = rootDict[C_ANNOUNCE_LIST];
  232. if(announceList.isList()) {
  233. torrent[ANNOUNCE_LIST] = announceList;
  234. } else {
  235. const BDE& announce = rootDict[C_ANNOUNCE];
  236. BDE announceList = BDE::list();
  237. if(announce.isString()) {
  238. announceList << BDE::list();
  239. announceList[0] << announce;
  240. }
  241. torrent[ANNOUNCE_LIST] = announceList;
  242. }
  243. }
  244. static void extractNodes(BDE& torrent, const BDE& nodesList)
  245. {
  246. BDE nodes = BDE::list();
  247. if(nodesList.isList()) {
  248. for(BDE::List::const_iterator i = nodesList.listBegin();
  249. i != nodesList.listEnd(); ++i) {
  250. const BDE& addrPairList = (*i);
  251. if(!addrPairList.isList() || addrPairList.size() != 2) {
  252. continue;
  253. }
  254. const BDE& hostname = addrPairList[0];
  255. if(!hostname.isString()) {
  256. continue;
  257. }
  258. if(Util::trim(hostname.s()).empty()) {
  259. continue;
  260. }
  261. const BDE& port = addrPairList[1];
  262. if(!port.isInteger() || !(0 < port.i() && port.i() < 65536)) {
  263. continue;
  264. }
  265. BDE node = BDE::dict();
  266. node[HOSTNAME] = hostname;
  267. node[PORT] = port;
  268. nodes << node;
  269. }
  270. }
  271. torrent[NODES] = nodes;
  272. }
  273. static void processRootDictionary
  274. (const SharedHandle<DownloadContext>& ctx,
  275. const BDE& rootDict,
  276. const std::string& defaultName,
  277. const std::string& overrideName,
  278. const std::deque<std::string>& uris)
  279. {
  280. if(!rootDict.isDict()) {
  281. throw DL_ABORT_EX("torrent file does not contain a root dictionary.");
  282. }
  283. const BDE& infoDict = rootDict[C_INFO];
  284. if(!infoDict.isDict()) {
  285. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  286. C_INFO.c_str()).str());
  287. }
  288. BDE torrent = BDE::dict();
  289. // retrieve infoHash
  290. std::string encodedInfoDict = bencode::encode(infoDict);
  291. unsigned char infoHash[INFO_HASH_LENGTH];
  292. MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
  293. MessageDigestContext::SHA1,
  294. encodedInfoDict.data(),
  295. encodedInfoDict.size());
  296. torrent[INFO_HASH] = std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]);
  297. // calculate the number of pieces
  298. const BDE& piecesData = infoDict[C_PIECES];
  299. if(!piecesData.isString()) {
  300. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  301. C_PIECES.c_str()).str());
  302. }
  303. // Commented out To download 0 length torrent.
  304. // if(piecesData.s().empty()) {
  305. // throw DL_ABORT_EX("The length of piece hash is 0.");
  306. // }
  307. size_t numPieces = piecesData.s().size()/PIECE_HASH_LENGTH;
  308. // Commented out to download 0 length torrent.
  309. // if(numPieces == 0) {
  310. // throw DL_ABORT_EX("The number of pieces is 0.");
  311. // }
  312. // retrieve piece length
  313. const BDE& pieceLengthData = infoDict[C_PIECE_LENGTH];
  314. if(!pieceLengthData.isInteger()) {
  315. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  316. C_PIECE_LENGTH.c_str()).str());
  317. }
  318. size_t pieceLength = pieceLengthData.i();
  319. ctx->setPieceLength(pieceLength);
  320. // retrieve piece hashes
  321. extractPieceHash(ctx, piecesData.s(), PIECE_HASH_LENGTH, numPieces);
  322. // private flag
  323. const BDE& privateData = infoDict[C_PRIVATE];
  324. int privatefg = 0;
  325. if(privateData.isInteger()) {
  326. if(privateData.i() == 1) {
  327. privatefg = 1;
  328. }
  329. }
  330. torrent[PRIVATE] = BDE((int64_t)privatefg);
  331. // retrieve uri-list.
  332. // This implemantation obeys HTTP-Seeding specification:
  333. // see http://www.getright.com/seedtorrent.html
  334. std::vector<std::string> urlList;
  335. extractUrlList(urlList, rootDict[C_URL_LIST]);
  336. urlList.insert(urlList.end(), uris.begin(), uris.end());
  337. std::sort(urlList.begin(), urlList.end());
  338. urlList.erase(std::unique(urlList.begin(), urlList.end()), urlList.end());
  339. // retrieve file entries
  340. extractFileEntries(ctx, torrent, infoDict, defaultName, overrideName, urlList);
  341. if((ctx->getTotalLength()+pieceLength-1)/pieceLength != numPieces) {
  342. throw DL_ABORT_EX("Too few/many piece hash.");
  343. }
  344. // retrieve announce
  345. extractAnnounce(torrent, rootDict);
  346. // retrieve nodes
  347. extractNodes(torrent, rootDict[C_NODES]);
  348. ctx->setAttribute(BITTORRENT, torrent);
  349. }
  350. void load(const std::string& torrentFile,
  351. const SharedHandle<DownloadContext>& ctx,
  352. const std::string& overrideName)
  353. {
  354. processRootDictionary(ctx,
  355. bencode::decodeFromFile(torrentFile),
  356. torrentFile,
  357. overrideName,
  358. std::deque<std::string>());
  359. }
  360. void load(const std::string& torrentFile,
  361. const SharedHandle<DownloadContext>& ctx,
  362. const std::deque<std::string>& uris,
  363. const std::string& overrideName)
  364. {
  365. processRootDictionary(ctx,
  366. bencode::decodeFromFile(torrentFile),
  367. torrentFile,
  368. overrideName,
  369. uris);
  370. }
  371. void loadFromMemory(const unsigned char* content,
  372. size_t length,
  373. const SharedHandle<DownloadContext>& ctx,
  374. const std::string& defaultName,
  375. const std::string& overrideName)
  376. {
  377. processRootDictionary(ctx,
  378. bencode::decode(content, length),
  379. defaultName,
  380. overrideName,
  381. std::deque<std::string>());
  382. }
  383. void loadFromMemory(const unsigned char* content,
  384. size_t length,
  385. const SharedHandle<DownloadContext>& ctx,
  386. const std::deque<std::string>& uris,
  387. const std::string& defaultName,
  388. const std::string& overrideName)
  389. {
  390. processRootDictionary(ctx,
  391. bencode::decode(content, length),
  392. defaultName,
  393. overrideName,
  394. uris);
  395. }
  396. void loadFromMemory(const std::string& context,
  397. const SharedHandle<DownloadContext>& ctx,
  398. const std::string& defaultName,
  399. const std::string& overrideName)
  400. {
  401. processRootDictionary
  402. (ctx,
  403. bencode::decode(reinterpret_cast<const unsigned char*>(context.c_str()),
  404. context.size()),
  405. defaultName, overrideName,
  406. std::deque<std::string>());
  407. }
  408. void loadFromMemory(const std::string& context,
  409. const SharedHandle<DownloadContext>& ctx,
  410. const std::deque<std::string>& uris,
  411. const std::string& defaultName,
  412. const std::string& overrideName)
  413. {
  414. processRootDictionary
  415. (ctx,
  416. bencode::decode(reinterpret_cast<const unsigned char*>(context.c_str()),
  417. context.size()),
  418. defaultName, overrideName,
  419. uris);
  420. }
  421. const unsigned char*
  422. getInfoHash(const SharedHandle<DownloadContext>& downloadContext)
  423. {
  424. return downloadContext->getAttribute(BITTORRENT)[INFO_HASH].uc();
  425. }
  426. std::string
  427. getInfoHashString(const SharedHandle<DownloadContext>& downloadContext)
  428. {
  429. return Util::toHex(downloadContext->getAttribute(BITTORRENT)[INFO_HASH].s());
  430. }
  431. void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx)
  432. {
  433. const BDE& torrentAttrs = dctx->getAttribute(BITTORRENT);
  434. o << "*** BitTorrent File Information ***" << "\n";
  435. o << "Mode: " << torrentAttrs[MODE].s() << "\n";
  436. o << "Announce:" << "\n";
  437. const BDE& announceList = torrentAttrs[ANNOUNCE_LIST];
  438. for(BDE::List::const_iterator tieritr = announceList.listBegin();
  439. tieritr != announceList.listEnd(); ++tieritr) {
  440. if(!(*tieritr).isList()) {
  441. continue;
  442. }
  443. for(BDE::List::const_iterator i = (*tieritr).listBegin();
  444. i != (*tieritr).listEnd(); ++i) {
  445. o << " " << (*i).s();
  446. }
  447. o << "\n";
  448. }
  449. o << "Info Hash: " << Util::toHex(torrentAttrs[INFO_HASH].s()) << "\n";
  450. o << "Piece Length: " << Util::abbrevSize(dctx->getPieceLength()) << "B\n";
  451. o << "The Number of Pieces: " << dctx->getNumPieces() << "\n";
  452. o << "Total Length: " << Util::abbrevSize(dctx->getTotalLength()) << "B\n";
  453. o << "Name: " << torrentAttrs[NAME].s() << "\n";
  454. Util::toStream(dctx->getFileEntries().begin(), dctx->getFileEntries().end(), o);
  455. }
  456. void computeFastSet
  457. (std::vector<size_t>& fastSet, const std::string& ipaddr,
  458. size_t numPieces, const unsigned char* infoHash, size_t fastSetSize)
  459. {
  460. unsigned char compact[6];
  461. if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
  462. return;
  463. }
  464. if(numPieces < fastSetSize) {
  465. fastSetSize = numPieces;
  466. }
  467. unsigned char tx[24];
  468. memcpy(tx, compact, 4);
  469. if((tx[0] & 0x80) == 0 || (tx[0] & 0x40) == 0) {
  470. tx[2] = 0x00;
  471. tx[3] = 0x00;
  472. } else {
  473. tx[3] = 0x00;
  474. }
  475. memcpy(tx+4, infoHash, 20);
  476. unsigned char x[20];
  477. MessageDigestHelper::digest(x, sizeof(x), MessageDigestContext::SHA1, tx, 24);
  478. while(fastSet.size() < fastSetSize) {
  479. for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
  480. size_t j = i*4;
  481. uint32_t ny;
  482. memcpy(&ny, x+j, 4);
  483. uint32_t y = ntohl(ny);
  484. size_t index = y%numPieces;
  485. if(std::find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
  486. fastSet.push_back(index);
  487. }
  488. }
  489. unsigned char temp[20];
  490. MessageDigestHelper::digest(temp, sizeof(temp), MessageDigestContext::SHA1, x, sizeof(x));
  491. memcpy(x, temp, sizeof(x));
  492. }
  493. }
  494. std::string generatePeerId
  495. (const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer)
  496. {
  497. std::string peerId = peerIdPrefix;
  498. peerId += Util::randomAlpha(20-peerIdPrefix.size(), randomizer);
  499. if(peerId.size() > 20) {
  500. peerId.erase(20);
  501. }
  502. return peerId;
  503. }
  504. static std::string peerId;
  505. const std::string& generateStaticPeerId
  506. (const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer)
  507. {
  508. if(peerId.empty()) {
  509. peerId = generatePeerId(peerIdPrefix, randomizer);
  510. }
  511. return peerId;
  512. }
  513. void setStaticPeerId(const std::string& newPeerId)
  514. {
  515. peerId = newPeerId;
  516. }
  517. // If PeerID is not generated, it is created with default peerIdPrefix
  518. // (-aria2-) and SimpleRandomizer.
  519. const unsigned char* getStaticPeerId()
  520. {
  521. if(peerId.empty()) {
  522. return
  523. reinterpret_cast<const unsigned char*>(generateStaticPeerId(DEFAULT_PEER_ID_PREFIX, SimpleRandomizer::getInstance()).data());
  524. } else {
  525. return reinterpret_cast<const unsigned char*>(peerId.data());
  526. }
  527. }
  528. } // namespace bittorrent
  529. } // namespace aria2