bittorrent_helper.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 <cassert>
  37. #include <cstring>
  38. #include <algorithm>
  39. #include "DownloadContext.h"
  40. #include "Randomizer.h"
  41. #include "bencode.h"
  42. #include "util.h"
  43. #include "DlAbortEx.h"
  44. #include "message.h"
  45. #include "StringFormat.h"
  46. #include "BtConstants.h"
  47. #include "messageDigest.h"
  48. #include "MessageDigestHelper.h"
  49. #include "SimpleRandomizer.h"
  50. #include "a2netcompat.h"
  51. #include "BtConstants.h"
  52. #include "bitfield.h"
  53. #include "base32.h"
  54. #include "magnet.h"
  55. namespace aria2 {
  56. namespace bittorrent {
  57. static const std::string C_NAME("name");
  58. static const std::string C_NAME_UTF8("name.utf-8");
  59. static const std::string C_FILES("files");
  60. static const std::string C_LENGTH("length");
  61. static const std::string C_PATH("path");
  62. static const std::string C_PATH_UTF8("path.utf-8");
  63. static const std::string C_INFO("info");
  64. static const std::string C_PIECES("pieces");
  65. static const std::string C_PIECE_LENGTH("piece length");
  66. static const std::string C_PRIVATE("private");
  67. static const std::string C_URL_LIST("url-list");
  68. static const std::string C_ANNOUNCE("announce");
  69. static const std::string C_ANNOUNCE_LIST("announce-list");
  70. static const std::string C_NODES("nodes");
  71. static const std::string C_CREATION_DATE("creation date");
  72. static const std::string C_COMMENT("comment");
  73. static const std::string C_COMMENT_UTF8("comment.utf-8");
  74. static const std::string C_CREATED_BY("created by");
  75. static const std::string DEFAULT_PEER_ID_PREFIX("aria2-");
  76. const std::string INFO_HASH("infoHash");
  77. const std::string MODE("mode");
  78. const std::string PRIVATE("private");
  79. const std::string ANNOUNCE_LIST("announceList");
  80. const std::string NODES("nodes");
  81. const std::string HOSTNAME("hostname");
  82. const std::string PORT("port");
  83. const std::string NAME("name");
  84. const std::string URL_LIST("urlList");
  85. const std::string CREATION_DATE("creationDate");
  86. const std::string COMMENT("comment");
  87. const std::string CREATED_BY("createdBy");
  88. const std::string BITTORRENT("bittorrent");
  89. const std::string MULTI("multi");
  90. const std::string SINGLE("single");
  91. const std::string METADATA_SIZE("metadataSize");
  92. const std::string METADATA("metadata");
  93. static void extractPieceHash(const SharedHandle<DownloadContext>& ctx,
  94. const std::string& hashData,
  95. size_t hashLength,
  96. size_t numPieces)
  97. {
  98. std::vector<std::string> pieceHashes;
  99. pieceHashes.reserve(numPieces);
  100. for(size_t i = 0; i < numPieces; ++i) {
  101. pieceHashes.push_back(util::toHex(hashData.data()+i*hashLength,
  102. hashLength));
  103. }
  104. ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end());
  105. ctx->setPieceHashAlgo(MessageDigestContext::SHA1);
  106. }
  107. static void extractUrlList
  108. (BDE& torrent, std::vector<std::string>& uris, const BDE& bde)
  109. {
  110. if(bde.isList()) {
  111. for(BDE::List::const_iterator itr = bde.listBegin(), eoi = bde.listEnd();
  112. itr != eoi; ++itr) {
  113. if((*itr).isString()) {
  114. uris.push_back((*itr).s());
  115. }
  116. }
  117. torrent[URL_LIST] = bde;
  118. } else if(bde.isString()) {
  119. uris.push_back(bde.s());
  120. BDE urlList = BDE::list();
  121. urlList << bde;
  122. torrent[URL_LIST] = urlList;
  123. } else {
  124. torrent[URL_LIST] = BDE::list();
  125. }
  126. }
  127. template<typename InputIterator, typename OutputIterator>
  128. static OutputIterator createUri
  129. (InputIterator first, InputIterator last, OutputIterator out,
  130. const std::string& filePath)
  131. {
  132. for(; first != last; ++first) {
  133. if(util::endsWith(*first, "/")) {
  134. *out++ = (*first)+filePath;
  135. } else {
  136. *out++ = (*first)+"/"+filePath;
  137. }
  138. }
  139. return out;
  140. }
  141. static void extractFileEntries
  142. (const SharedHandle<DownloadContext>& ctx,
  143. BDE& torrent,
  144. const BDE& infoDict,
  145. const std::string& defaultName,
  146. const std::string& overrideName,
  147. const std::vector<std::string>& urlList)
  148. {
  149. std::string name;
  150. if(overrideName.empty()) {
  151. std::string nameKey;
  152. if(infoDict.containsKey(C_NAME_UTF8)) {
  153. nameKey = C_NAME_UTF8;
  154. } else {
  155. nameKey = C_NAME;
  156. }
  157. const BDE& nameData = infoDict[nameKey];
  158. if(nameData.isString()) {
  159. if(util::detectDirTraversal(nameData.s())) {
  160. throw DL_ABORT_EX
  161. (StringFormat(MSG_DIR_TRAVERSAL_DETECTED,nameData.s().c_str()).str());
  162. }
  163. name = nameData.s();
  164. } else {
  165. name = strconcat(File(defaultName).getBasename(), ".file");
  166. }
  167. } else {
  168. name = overrideName;
  169. }
  170. torrent[NAME] = name;
  171. const BDE& filesList = infoDict[C_FILES];
  172. std::vector<SharedHandle<FileEntry> > fileEntries;
  173. if(filesList.isList()) {
  174. fileEntries.reserve(filesList.size());
  175. uint64_t length = 0;
  176. off_t offset = 0;
  177. // multi-file mode
  178. torrent[MODE] = MULTI;
  179. for(BDE::List::const_iterator itr = filesList.listBegin(),
  180. eoi = filesList.listEnd(); itr != eoi; ++itr) {
  181. const BDE& fileDict = *itr;
  182. if(!fileDict.isDict()) {
  183. continue;
  184. }
  185. const BDE& fileLengthData = fileDict[C_LENGTH];
  186. if(!fileLengthData.isInteger()) {
  187. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  188. C_LENGTH.c_str()).str());
  189. }
  190. length += fileLengthData.i();
  191. std::string pathKey;
  192. if(fileDict.containsKey(C_PATH_UTF8)) {
  193. pathKey = C_PATH_UTF8;
  194. } else {
  195. pathKey = C_PATH;
  196. }
  197. const BDE& pathList = fileDict[pathKey];
  198. if(!pathList.isList() || pathList.empty()) {
  199. throw DL_ABORT_EX("Path is empty.");
  200. }
  201. std::vector<std::string> pathelem(pathList.size()+1);
  202. pathelem[0] = name;
  203. std::transform(pathList.listBegin(), pathList.listEnd(),
  204. pathelem.begin()+1, std::mem_fun_ref(&BDE::s));
  205. std::string path = strjoin(pathelem.begin(), pathelem.end(), '/');
  206. if(util::detectDirTraversal(path)) {
  207. throw DL_ABORT_EX
  208. (StringFormat(MSG_DIR_TRAVERSAL_DETECTED, path.c_str()).str());
  209. }
  210. std::string pePath =
  211. strjoin(pathelem.begin(), pathelem.end(), '/',
  212. std::ptr_fun(static_cast<std::string (*)(const std::string&)>
  213. (util::percentEncode)));
  214. std::vector<std::string> uris;
  215. createUri(urlList.begin(), urlList.end(),std::back_inserter(uris),pePath);
  216. SharedHandle<FileEntry> fileEntry
  217. (new FileEntry(util::applyDir(ctx->getDir(), util::escapePath(path)),
  218. fileLengthData.i(),
  219. offset, uris));
  220. fileEntry->setOriginalName(path);
  221. fileEntries.push_back(fileEntry);
  222. offset += fileEntry->getLength();
  223. }
  224. } else {
  225. // single-file mode;
  226. torrent[MODE] = SINGLE;
  227. const BDE& lengthData = infoDict[C_LENGTH];
  228. if(!lengthData.isInteger()) {
  229. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  230. C_LENGTH.c_str()).str());
  231. }
  232. uint64_t totalLength = lengthData.i();
  233. // For each uri in urlList, if it ends with '/', then
  234. // concatenate name to it. Specification just says so.
  235. std::vector<std::string> uris;
  236. for(std::vector<std::string>::const_iterator i = urlList.begin(),
  237. eoi = urlList.end(); i != eoi; ++i) {
  238. if(util::endsWith(*i, A2STR::SLASH_C)) {
  239. uris.push_back((*i)+util::percentEncode(name));
  240. } else {
  241. uris.push_back(*i);
  242. }
  243. }
  244. SharedHandle<FileEntry> fileEntry
  245. (new FileEntry(util::applyDir(ctx->getDir(), util::escapePath(name)),
  246. totalLength, 0,
  247. uris));
  248. fileEntry->setOriginalName(name);
  249. fileEntries.push_back(fileEntry);
  250. }
  251. ctx->setFileEntries(fileEntries.begin(), fileEntries.end());
  252. if(torrent[MODE].s() == MULTI) {
  253. ctx->setBasePath(util::applyDir(ctx->getDir(), name));
  254. }
  255. }
  256. static void extractAnnounce(BDE& torrent, const BDE& rootDict)
  257. {
  258. const BDE& announceList = rootDict[C_ANNOUNCE_LIST];
  259. if(announceList.isList()) {
  260. torrent[ANNOUNCE_LIST] = announceList;
  261. BDE& tiers = torrent[ANNOUNCE_LIST];
  262. for(BDE::List::iterator tieriter = tiers.listBegin(),
  263. eoi = tiers.listEnd(); tieriter != eoi; ++tieriter) {
  264. for(BDE::List::iterator uriiter = (*tieriter).listBegin(),
  265. eoi2 = (*tieriter).listEnd(); uriiter != eoi2; ++uriiter) {
  266. if((*uriiter).isString()) {
  267. *uriiter = util::trim((*uriiter).s());
  268. }
  269. }
  270. }
  271. } else {
  272. const BDE& announce = rootDict[C_ANNOUNCE];
  273. BDE announceList = BDE::list();
  274. if(announce.isString()) {
  275. announceList << BDE::list();
  276. announceList[0] << util::trim(announce.s());
  277. }
  278. torrent[ANNOUNCE_LIST] = announceList;
  279. }
  280. }
  281. static void extractNodes(BDE& torrent, const BDE& nodesList)
  282. {
  283. BDE nodes = BDE::list();
  284. if(nodesList.isList()) {
  285. for(BDE::List::const_iterator i = nodesList.listBegin(),
  286. eoi = nodesList.listEnd(); i != eoi; ++i) {
  287. const BDE& addrPairList = (*i);
  288. if(!addrPairList.isList() || addrPairList.size() != 2) {
  289. continue;
  290. }
  291. const BDE& hostname = addrPairList[0];
  292. if(!hostname.isString()) {
  293. continue;
  294. }
  295. if(util::trim(hostname.s()).empty()) {
  296. continue;
  297. }
  298. const BDE& port = addrPairList[1];
  299. if(!port.isInteger() || !(0 < port.i() && port.i() < 65536)) {
  300. continue;
  301. }
  302. BDE node = BDE::dict();
  303. node[HOSTNAME] = hostname;
  304. node[PORT] = port;
  305. nodes << node;
  306. }
  307. }
  308. torrent[NODES] = nodes;
  309. }
  310. static void processRootDictionary
  311. (const SharedHandle<DownloadContext>& ctx,
  312. const BDE& rootDict,
  313. const std::string& defaultName,
  314. const std::string& overrideName,
  315. const std::vector<std::string>& uris)
  316. {
  317. if(!rootDict.isDict()) {
  318. throw DL_ABORT_EX("torrent file does not contain a root dictionary.");
  319. }
  320. const BDE& infoDict = rootDict[C_INFO];
  321. if(!infoDict.isDict()) {
  322. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  323. C_INFO.c_str()).str());
  324. }
  325. BDE torrent = BDE::dict();
  326. // retrieve infoHash
  327. std::string encodedInfoDict = bencode::encode(infoDict);
  328. unsigned char infoHash[INFO_HASH_LENGTH];
  329. MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
  330. MessageDigestContext::SHA1,
  331. encodedInfoDict.data(),
  332. encodedInfoDict.size());
  333. torrent[INFO_HASH] = std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]);
  334. torrent[METADATA] = encodedInfoDict;
  335. torrent[METADATA_SIZE] = encodedInfoDict.size();
  336. // calculate the number of pieces
  337. const BDE& piecesData = infoDict[C_PIECES];
  338. if(!piecesData.isString()) {
  339. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  340. C_PIECES.c_str()).str());
  341. }
  342. // Commented out To download 0 length torrent.
  343. // if(piecesData.s().empty()) {
  344. // throw DL_ABORT_EX("The length of piece hash is 0.");
  345. // }
  346. size_t numPieces = piecesData.s().size()/PIECE_HASH_LENGTH;
  347. // Commented out to download 0 length torrent.
  348. // if(numPieces == 0) {
  349. // throw DL_ABORT_EX("The number of pieces is 0.");
  350. // }
  351. // retrieve piece length
  352. const BDE& pieceLengthData = infoDict[C_PIECE_LENGTH];
  353. if(!pieceLengthData.isInteger()) {
  354. throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
  355. C_PIECE_LENGTH.c_str()).str());
  356. }
  357. size_t pieceLength = pieceLengthData.i();
  358. ctx->setPieceLength(pieceLength);
  359. // retrieve piece hashes
  360. extractPieceHash(ctx, piecesData.s(), PIECE_HASH_LENGTH, numPieces);
  361. // private flag
  362. const BDE& privateData = infoDict[C_PRIVATE];
  363. int privatefg = 0;
  364. if(privateData.isInteger()) {
  365. if(privateData.i() == 1) {
  366. privatefg = 1;
  367. }
  368. }
  369. torrent[PRIVATE] = BDE((int64_t)privatefg);
  370. // retrieve uri-list.
  371. // This implemantation obeys HTTP-Seeding specification:
  372. // see http://www.getright.com/seedtorrent.html
  373. std::vector<std::string> urlList;
  374. extractUrlList(torrent, urlList, rootDict[C_URL_LIST]);
  375. urlList.insert(urlList.end(), uris.begin(), uris.end());
  376. std::sort(urlList.begin(), urlList.end());
  377. urlList.erase(std::unique(urlList.begin(), urlList.end()), urlList.end());
  378. // retrieve file entries
  379. extractFileEntries(ctx, torrent, infoDict, defaultName, overrideName, urlList);
  380. if((ctx->getTotalLength()+pieceLength-1)/pieceLength != numPieces) {
  381. throw DL_ABORT_EX("Too few/many piece hash.");
  382. }
  383. // retrieve announce
  384. extractAnnounce(torrent, rootDict);
  385. // retrieve nodes
  386. extractNodes(torrent, rootDict[C_NODES]);
  387. const BDE& creationDate = rootDict[C_CREATION_DATE];
  388. if(creationDate.isInteger()) {
  389. torrent[CREATION_DATE] = creationDate;
  390. }
  391. const BDE& commentUtf8 = rootDict[C_COMMENT_UTF8];
  392. if(commentUtf8.isString()) {
  393. torrent[COMMENT] = commentUtf8;
  394. } else {
  395. const BDE& comment = rootDict[C_COMMENT];
  396. if(comment.isString()) {
  397. torrent[COMMENT] = comment;
  398. }
  399. }
  400. const BDE& createdBy = rootDict[C_CREATED_BY];
  401. if(createdBy.isString()) {
  402. torrent[CREATED_BY] = createdBy;
  403. }
  404. ctx->setAttribute(BITTORRENT, torrent);
  405. }
  406. void load(const std::string& torrentFile,
  407. const SharedHandle<DownloadContext>& ctx,
  408. const std::string& overrideName)
  409. {
  410. processRootDictionary(ctx,
  411. bencode::decodeFromFile(torrentFile),
  412. torrentFile,
  413. overrideName,
  414. std::vector<std::string>());
  415. }
  416. void load(const std::string& torrentFile,
  417. const SharedHandle<DownloadContext>& ctx,
  418. const std::vector<std::string>& uris,
  419. const std::string& overrideName)
  420. {
  421. processRootDictionary(ctx,
  422. bencode::decodeFromFile(torrentFile),
  423. torrentFile,
  424. overrideName,
  425. uris);
  426. }
  427. void loadFromMemory(const unsigned char* content,
  428. size_t length,
  429. const SharedHandle<DownloadContext>& ctx,
  430. const std::string& defaultName,
  431. const std::string& overrideName)
  432. {
  433. processRootDictionary(ctx,
  434. bencode::decode(content, length),
  435. defaultName,
  436. overrideName,
  437. std::vector<std::string>());
  438. }
  439. void loadFromMemory(const unsigned char* content,
  440. size_t length,
  441. const SharedHandle<DownloadContext>& ctx,
  442. const std::vector<std::string>& uris,
  443. const std::string& defaultName,
  444. const std::string& overrideName)
  445. {
  446. processRootDictionary(ctx,
  447. bencode::decode(content, length),
  448. defaultName,
  449. overrideName,
  450. uris);
  451. }
  452. void loadFromMemory(const std::string& context,
  453. const SharedHandle<DownloadContext>& ctx,
  454. const std::string& defaultName,
  455. const std::string& overrideName)
  456. {
  457. processRootDictionary
  458. (ctx,
  459. bencode::decode(context),
  460. defaultName, overrideName,
  461. std::vector<std::string>());
  462. }
  463. void loadFromMemory(const std::string& context,
  464. const SharedHandle<DownloadContext>& ctx,
  465. const std::vector<std::string>& uris,
  466. const std::string& defaultName,
  467. const std::string& overrideName)
  468. {
  469. processRootDictionary
  470. (ctx,
  471. bencode::decode(context),
  472. defaultName, overrideName,
  473. uris);
  474. }
  475. const unsigned char*
  476. getInfoHash(const SharedHandle<DownloadContext>& downloadContext)
  477. {
  478. return downloadContext->getAttribute(BITTORRENT)[INFO_HASH].uc();
  479. }
  480. std::string
  481. getInfoHashString(const SharedHandle<DownloadContext>& downloadContext)
  482. {
  483. return util::toHex(downloadContext->getAttribute(BITTORRENT)[INFO_HASH].s());
  484. }
  485. void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx)
  486. {
  487. const BDE& torrentAttrs = dctx->getAttribute(BITTORRENT);
  488. o << "*** BitTorrent File Information ***" << "\n";
  489. if(torrentAttrs.containsKey(COMMENT)) {
  490. o << "Comment: " << torrentAttrs[COMMENT].s() << "\n";
  491. }
  492. if(torrentAttrs.containsKey(CREATION_DATE)) {
  493. struct tm* staticNowtmPtr;
  494. char buf[26];
  495. time_t t = torrentAttrs[CREATION_DATE].i();
  496. if((staticNowtmPtr = localtime(&t)) != 0 &&
  497. asctime_r(staticNowtmPtr, buf) != 0) {
  498. // buf includes "\n"
  499. o << "Creation Date: " << buf;
  500. }
  501. }
  502. if(torrentAttrs.containsKey(CREATED_BY)) {
  503. o << "Created By: " << torrentAttrs[CREATED_BY].s() << "\n";
  504. }
  505. o << "Mode: " << torrentAttrs[MODE].s() << "\n";
  506. o << "Announce:" << "\n";
  507. const BDE& announceList = torrentAttrs[ANNOUNCE_LIST];
  508. for(BDE::List::const_iterator tieritr = announceList.listBegin(),
  509. eoi = announceList.listEnd(); tieritr != eoi; ++tieritr) {
  510. if(!(*tieritr).isList()) {
  511. continue;
  512. }
  513. for(BDE::List::const_iterator i = (*tieritr).listBegin(),
  514. eoi2 = (*tieritr).listEnd(); i != eoi2; ++i) {
  515. o << " " << (*i).s();
  516. }
  517. o << "\n";
  518. }
  519. o << "Info Hash: " << util::toHex(torrentAttrs[INFO_HASH].s()) << "\n";
  520. o << "Piece Length: " << util::abbrevSize(dctx->getPieceLength()) << "B\n";
  521. o << "The Number of Pieces: " << dctx->getNumPieces() << "\n";
  522. o << "Total Length: " << util::abbrevSize(dctx->getTotalLength()) << "B ("
  523. << util::uitos(dctx->getTotalLength(), true) << ")\n";
  524. if(!torrentAttrs[URL_LIST].empty()) {
  525. const BDE& urlList = torrentAttrs[URL_LIST];
  526. o << "URL List: " << "\n";
  527. for(BDE::List::const_iterator i = urlList.listBegin(),
  528. eoi = urlList.listEnd(); i != eoi; ++i) {
  529. o << " " << (*i).s() << "\n";
  530. }
  531. }
  532. o << "Name: " << torrentAttrs[NAME].s() << "\n";
  533. o << "Magnet URI: " << torrent2Magnet(torrentAttrs) << "\n";
  534. util::toStream(dctx->getFileEntries().begin(), dctx->getFileEntries().end(), o);
  535. }
  536. void computeFastSet
  537. (std::vector<size_t>& fastSet, const std::string& ipaddr,
  538. size_t numPieces, const unsigned char* infoHash, size_t fastSetSize)
  539. {
  540. unsigned char compact[6];
  541. if(!createcompact(compact, ipaddr, 0)) {
  542. return;
  543. }
  544. if(numPieces < fastSetSize) {
  545. fastSetSize = numPieces;
  546. }
  547. unsigned char tx[24];
  548. memcpy(tx, compact, 4);
  549. if((tx[0] & 0x80) == 0 || (tx[0] & 0x40) == 0) {
  550. tx[2] = 0x00;
  551. tx[3] = 0x00;
  552. } else {
  553. tx[3] = 0x00;
  554. }
  555. memcpy(tx+4, infoHash, 20);
  556. unsigned char x[20];
  557. MessageDigestHelper::digest(x, sizeof(x), MessageDigestContext::SHA1, tx, 24);
  558. while(fastSet.size() < fastSetSize) {
  559. for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
  560. size_t j = i*4;
  561. uint32_t ny;
  562. memcpy(&ny, x+j, 4);
  563. uint32_t y = ntohl(ny);
  564. size_t index = y%numPieces;
  565. if(std::find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
  566. fastSet.push_back(index);
  567. }
  568. }
  569. unsigned char temp[20];
  570. MessageDigestHelper::digest(temp, sizeof(temp), MessageDigestContext::SHA1, x, sizeof(x));
  571. memcpy(x, temp, sizeof(x));
  572. }
  573. }
  574. std::string generatePeerId(const std::string& peerIdPrefix)
  575. {
  576. std::string peerId = peerIdPrefix;
  577. unsigned char buf[20];
  578. int len = 20-peerIdPrefix.size();
  579. if(len > 0) {
  580. util::generateRandomData(buf, len);
  581. peerId += std::string(&buf[0], &buf[len]);
  582. } if(peerId.size() > 20) {
  583. peerId.erase(20);
  584. }
  585. return peerId;
  586. }
  587. static std::string peerId;
  588. const std::string& generateStaticPeerId(const std::string& peerIdPrefix)
  589. {
  590. if(peerId.empty()) {
  591. peerId = generatePeerId(peerIdPrefix);
  592. }
  593. return peerId;
  594. }
  595. void setStaticPeerId(const std::string& newPeerId)
  596. {
  597. peerId = newPeerId;
  598. }
  599. // If PeerID is not generated, it is created with default peerIdPrefix
  600. // (aria2-).
  601. const unsigned char* getStaticPeerId()
  602. {
  603. if(peerId.empty()) {
  604. return
  605. reinterpret_cast<const unsigned char*>
  606. (generateStaticPeerId(DEFAULT_PEER_ID_PREFIX).data());
  607. } else {
  608. return reinterpret_cast<const unsigned char*>(peerId.data());
  609. }
  610. }
  611. uint8_t getId(const unsigned char* msg)
  612. {
  613. return msg[0];
  614. }
  615. uint32_t getIntParam(const unsigned char* msg, size_t pos)
  616. {
  617. uint32_t nParam;
  618. memcpy(&nParam, msg+pos, sizeof(nParam));
  619. return ntohl(nParam);
  620. }
  621. uint16_t getShortIntParam(const unsigned char* msg, size_t pos)
  622. {
  623. uint16_t nParam;
  624. memcpy(&nParam, msg+pos, sizeof(nParam));
  625. return ntohs(nParam);
  626. }
  627. void checkIndex(size_t index, size_t pieces)
  628. {
  629. if(!(index < pieces)) {
  630. throw DL_ABORT_EX(StringFormat("Invalid index: %lu",
  631. static_cast<unsigned long>(index)).str());
  632. }
  633. }
  634. void checkBegin(uint32_t begin, size_t pieceLength)
  635. {
  636. if(!(begin < pieceLength)) {
  637. throw DL_ABORT_EX(StringFormat("Invalid begin: %u", begin).str());
  638. }
  639. }
  640. void checkLength(size_t length)
  641. {
  642. if(length > MAX_BLOCK_LENGTH) {
  643. throw DL_ABORT_EX
  644. (StringFormat("Length too long: %lu > %uKB",
  645. static_cast<unsigned long>(length),
  646. MAX_BLOCK_LENGTH/1024).str());
  647. }
  648. if(length == 0) {
  649. throw DL_ABORT_EX
  650. (StringFormat("Invalid length: %lu",
  651. static_cast<unsigned long>(length)).str());
  652. }
  653. }
  654. void checkRange(uint32_t begin, size_t length, size_t pieceLength)
  655. {
  656. if(!(0 < length)) {
  657. throw DL_ABORT_EX
  658. (StringFormat("Invalid range: begin=%u, length=%lu",
  659. begin,
  660. static_cast<unsigned long>(length)).str());
  661. }
  662. uint32_t end = begin+length;
  663. if(!(end <= pieceLength)) {
  664. throw DL_ABORT_EX
  665. (StringFormat("Invalid range: begin=%u, length=%lu",
  666. begin,
  667. static_cast<unsigned long>(length)).str());
  668. }
  669. }
  670. void checkBitfield
  671. (const unsigned char* bitfield, size_t bitfieldLength, size_t pieces)
  672. {
  673. if(!(bitfieldLength == (pieces+7)/8)) {
  674. throw DL_ABORT_EX
  675. (StringFormat("Invalid bitfield length: %lu",
  676. static_cast<unsigned long>(bitfieldLength)).str());
  677. }
  678. // Check if last byte contains garbage set bit.
  679. if(bitfield[bitfieldLength-1]&~bitfield::lastByteMask(pieces)) {
  680. throw DL_ABORT_EX("Invalid bitfield");
  681. }
  682. }
  683. void setIntParam(unsigned char* dest, uint32_t param)
  684. {
  685. uint32_t nParam = htonl(param);
  686. memcpy(dest, &nParam, sizeof(nParam));
  687. }
  688. void setShortIntParam(unsigned char* dest, uint16_t param)
  689. {
  690. uint16_t nParam = htons(param);
  691. memcpy(dest, &nParam, sizeof(nParam));
  692. }
  693. void createPeerMessageString
  694. (unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId)
  695. {
  696. assert(msgLength >= 5);
  697. memset(msg, 0, msgLength);
  698. setIntParam(msg, payloadLength);
  699. msg[4] = messageId;
  700. }
  701. bool createcompact
  702. (unsigned char* compact, const std::string& addr, uint16_t port)
  703. {
  704. struct addrinfo hints;
  705. struct addrinfo* res;
  706. memset(&hints, 0, sizeof(hints));
  707. hints.ai_family = AF_INET; // since compact peer format is ipv4 only.
  708. hints.ai_flags = AI_NUMERICHOST;
  709. if(getaddrinfo(addr.c_str(), 0, &hints, &res)) {
  710. return false;
  711. }
  712. struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
  713. memcpy(compact, &(in->sin_addr.s_addr), sizeof(uint32_t));
  714. uint16_t port_nworder(htons(port));
  715. memcpy(compact+4, &port_nworder, sizeof(uint16_t));
  716. freeaddrinfo(res);
  717. return true;
  718. }
  719. std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact)
  720. {
  721. struct sockaddr_in in;
  722. memset(&in, 0, sizeof(in));
  723. #ifdef HAVE_SOCKADDR_IN_SIN_LEN
  724. // For netbsd
  725. in.sin_len = sizeof(in);
  726. #endif // HAVE_SOCKADDR_IN_SIN_LEN
  727. in.sin_family = AF_INET;
  728. memcpy(&(in.sin_addr.s_addr), compact, sizeof(uint32_t));
  729. in.sin_port = 0;
  730. char host[NI_MAXHOST];
  731. int s;
  732. s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&in), sizeof(in),
  733. host, NI_MAXHOST, 0, 0,
  734. NI_NUMERICHOST);
  735. if(s) {
  736. return std::pair<std::string, uint16_t>();
  737. }
  738. uint16_t port_nworder;
  739. memcpy(&port_nworder, compact+sizeof(uint32_t), sizeof(uint16_t));
  740. uint16_t port = ntohs(port_nworder);
  741. return std::make_pair(host, port);
  742. }
  743. void assertPayloadLengthGreater
  744. (size_t threshold, size_t actual, const std::string& msgName)
  745. {
  746. if(actual <= threshold) {
  747. throw DL_ABORT_EX
  748. (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str());
  749. }
  750. }
  751. void assertPayloadLengthEqual
  752. (size_t expected, size_t actual, const std::string& msgName)
  753. {
  754. if(expected != actual) {
  755. throw DL_ABORT_EX
  756. (StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
  757. actual, expected).str());
  758. }
  759. }
  760. void assertID
  761. (uint8_t expected, const unsigned char* data, const std::string& msgName)
  762. {
  763. uint8_t id = getId(data);
  764. if(expected != id) {
  765. throw DL_ABORT_EX
  766. (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
  767. expected).str());
  768. }
  769. }
  770. BDE parseMagnet(const std::string& magnet)
  771. {
  772. BDE result;
  773. BDE r = magnet::parse(magnet);
  774. if(r.isNone()) {
  775. throw DL_ABORT_EX("Bad BitTorrent Magnet URI.");
  776. }
  777. if(!r.containsKey("xt")) {
  778. throw DL_ABORT_EX("Missing xt parameter in Magnet URI.");
  779. }
  780. std::string infoHash;
  781. const BDE& xts = r["xt"];
  782. for(BDE::List::const_iterator xtiter = xts.listBegin(),
  783. eoi = xts.listEnd(); xtiter != eoi && infoHash.empty(); ++xtiter) {
  784. if(util::startsWith((*xtiter).s(), "urn:btih:")) {
  785. std::string xtarg = (*xtiter).s().substr(9);
  786. size_t size = xtarg.size();
  787. if(size == 32) {
  788. std::string rawhash = base32::decode(xtarg);
  789. if(rawhash.size() == 20) {
  790. infoHash.swap(rawhash);
  791. }
  792. } else if(size == 40) {
  793. std::string rawhash = util::fromHex(xtarg);
  794. if(!rawhash.empty()) {
  795. infoHash.swap(rawhash);
  796. }
  797. }
  798. }
  799. }
  800. if(infoHash.empty()) {
  801. throw DL_ABORT_EX("Bad BitTorrent Magnet URI. "
  802. "No valid BitTorrent Info Hash found.");
  803. }
  804. BDE announceList = BDE::list();
  805. if(r.containsKey("tr")) {
  806. const BDE& uris = r["tr"];
  807. for(BDE::List::const_iterator i = uris.listBegin(), eoi = uris.listEnd();
  808. i != eoi; ++i) {
  809. BDE tier = BDE::list();
  810. tier << *i;
  811. announceList << tier;
  812. }
  813. }
  814. std::string name = "[METADATA]";
  815. if(r.containsKey("dn") && r["dn"].size()) {
  816. name += r["dn"][0].s();
  817. } else {
  818. name += util::toHex(infoHash);
  819. }
  820. BDE attrs = BDE::dict();
  821. attrs[INFO_HASH] = infoHash;
  822. attrs[NAME] = name;
  823. attrs[ANNOUNCE_LIST] = announceList;
  824. result = attrs;
  825. return result;
  826. }
  827. void loadMagnet
  828. (const std::string& magnet, const SharedHandle<DownloadContext>& dctx)
  829. {
  830. BDE attrs = parseMagnet(magnet);
  831. dctx->setAttribute(BITTORRENT, attrs);
  832. }
  833. std::string metadata2Torrent(const std::string& metadata, const BDE& attrs)
  834. {
  835. std::string torrent = "d";
  836. if(attrs.containsKey(bittorrent::ANNOUNCE_LIST)) {
  837. const BDE& announceList = attrs[bittorrent::ANNOUNCE_LIST];
  838. if(announceList.size() > 0) {
  839. torrent += "13:announce-list";
  840. torrent += bencode::encode(announceList);
  841. }
  842. }
  843. torrent +=
  844. strconcat("4:info", metadata, "e");
  845. return torrent;
  846. }
  847. std::string torrent2Magnet(const BDE& attrs)
  848. {
  849. std::string uri = "magnet:?";
  850. if(attrs.containsKey(INFO_HASH)) {
  851. uri += "xt=urn:btih:";
  852. uri += util::toUpper(util::toHex(attrs[INFO_HASH].s()));
  853. } else {
  854. return A2STR::NIL;
  855. }
  856. if(attrs.containsKey(NAME)) {
  857. uri += "&dn=";
  858. uri += util::percentEncode(attrs[NAME].s());
  859. }
  860. if(attrs.containsKey(ANNOUNCE_LIST)) {
  861. const BDE& tiers = attrs[ANNOUNCE_LIST];
  862. for(BDE::List::const_iterator tieriter = tiers.listBegin(),
  863. eoi = tiers.listEnd(); tieriter != eoi; ++tieriter) {
  864. for(BDE::List::const_iterator uriiter = (*tieriter).listBegin(),
  865. eoi2 = (*tieriter).listEnd(); uriiter != eoi2; ++uriiter) {
  866. uri += "&tr=";
  867. uri += util::percentEncode((*uriiter).s());
  868. }
  869. }
  870. }
  871. return uri;
  872. }
  873. } // namespace bittorrent
  874. } // namespace aria2