DefaultBtContext.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "DefaultBtContext.h"
  36. #include "MetaFileUtil.h"
  37. #include "Dictionary.h"
  38. #include "List.h"
  39. #include "Data.h"
  40. #include "DlAbortEx.h"
  41. #include "ShaVisitor.h"
  42. #include "Util.h"
  43. #include <libgen.h>
  44. DefaultBtContext::DefaultBtContext():_peerIdPrefix("-aria2-") {}
  45. DefaultBtContext::~DefaultBtContext() {}
  46. string DefaultBtContext::generatePeerId() const {
  47. string peerId = _peerIdPrefix;
  48. peerId += Util::randomAlpha(20-_peerIdPrefix.size());
  49. if(peerId.size() > 20) {
  50. peerId.erase(20);
  51. }
  52. return peerId;
  53. }
  54. const unsigned char* DefaultBtContext::getInfoHash() const {
  55. return infoHash;
  56. }
  57. int32_t DefaultBtContext::getInfoHashLength() const {
  58. return INFO_HASH_LENGTH;
  59. }
  60. string DefaultBtContext::getInfoHashAsString() const {
  61. return infoHashString;
  62. }
  63. void DefaultBtContext::clear() {
  64. memset(infoHash, 0, INFO_HASH_LENGTH);
  65. infoHashString = "";
  66. pieceHashes.clear();
  67. fileEntries.clear();
  68. totalLength = 0;
  69. pieceLength = 0;
  70. fileMode = BtContext::SINGLE;
  71. numPieces = 0;
  72. name = "";
  73. announceTiers.clear();
  74. }
  75. void DefaultBtContext::extractPieceHash(const unsigned char* hashData,
  76. int32_t hashDataLength,
  77. int32_t hashLength) {
  78. assert(hashDataLength > 0);
  79. assert(hashLength > 0);
  80. int32_t numPieces = hashDataLength/hashLength;
  81. assert(numPieces > 0);
  82. for(int32_t i = 0; i < numPieces; i++) {
  83. pieceHashes.push_back(Util::toHex(&hashData[i*hashLength],
  84. hashLength));
  85. }
  86. }
  87. void DefaultBtContext::extractFileEntries(Dictionary* infoDic,
  88. const string& defaultName) {
  89. // TODO use dynamic_cast
  90. Data* nameData = (Data*)infoDic->get("name");
  91. if(nameData) {
  92. name = nameData->toString();
  93. } else {
  94. char* basec = strdup(defaultName.c_str());
  95. name = string(basename(basec))+".file";
  96. free(basec);
  97. }
  98. // TODO use dynamic_cast
  99. List* files = (List*)infoDic->get("files");
  100. if(files) {
  101. int64_t length = 0;
  102. int64_t offset = 0;
  103. // multi-file mode
  104. fileMode = BtContext::MULTI;
  105. const MetaList& metaList = files->getList();
  106. for(MetaList::const_iterator itr = metaList.begin();
  107. itr != metaList.end(); itr++) {
  108. Dictionary* fileDic = (Dictionary*)(*itr);
  109. // TODO use dynamic_cast
  110. Data* lengthData = (Data*)fileDic->get("length");
  111. length += lengthData->toLLInt();
  112. // TODO use dynamic_cast
  113. List* pathList = (List*)fileDic->get("path");
  114. const MetaList& paths = pathList->getList();
  115. string path;
  116. for(int32_t i = 0; i < (int32_t)paths.size()-1; i++) {
  117. Data* subpath = (Data*)paths[i];
  118. path += subpath->toString()+"/";
  119. }
  120. // TODO use dynamic_cast
  121. Data* lastPath = (Data*)paths.back();
  122. path += lastPath->toString();
  123. FileEntryHandle fileEntry(new FileEntry(path,
  124. lengthData->toLLInt(),
  125. offset));
  126. fileEntries.push_back(fileEntry);
  127. offset += fileEntry->getLength();
  128. }
  129. totalLength = length;
  130. } else {
  131. // single-file mode;
  132. fileMode = BtContext::SINGLE;
  133. Data* length = (Data*)infoDic->get("length");
  134. totalLength = length->toLLInt();
  135. FileEntryHandle fileEntry(new FileEntry(name, totalLength, 0));
  136. fileEntries.push_back(fileEntry);
  137. }
  138. }
  139. void DefaultBtContext::extractAnnounce(Data* announceData) {
  140. Strings urls;
  141. urls.push_back(announceData->toString());
  142. announceTiers.push_back(AnnounceTierHandle(new AnnounceTier(urls)));
  143. }
  144. void DefaultBtContext::extractAnnounceList(List* announceListData) {
  145. for(MetaList::const_iterator itr = announceListData->getList().begin();
  146. itr != announceListData->getList().end(); itr++) {
  147. const List* elem = (List*)*itr;
  148. Strings urls;
  149. for(MetaList::const_iterator elemItr = elem->getList().begin();
  150. elemItr != elem->getList().end(); elemItr++) {
  151. const Data* data = (Data*)*elemItr;
  152. urls.push_back(data->toString());
  153. }
  154. if(urls.size()) {
  155. AnnounceTierHandle tier(new AnnounceTier(urls));
  156. announceTiers.push_back(tier);
  157. }
  158. }
  159. }
  160. void DefaultBtContext::load(const string& torrentFile) {
  161. clear();
  162. MetaEntry* rootEntry = MetaFileUtil::parseMetaFile(torrentFile);
  163. if(!dynamic_cast<Dictionary*>(rootEntry)) {
  164. throw new DlAbortEx("torrent file does not contain a root dictionary .");
  165. }
  166. SharedHandle<Dictionary> rootDic =
  167. SharedHandle<Dictionary>((Dictionary*)rootEntry);
  168. Dictionary* infoDic = (Dictionary*)rootDic->get("info");
  169. // retrieve infoHash
  170. ShaVisitor v;
  171. infoDic->accept(&v);
  172. int32_t len;
  173. v.getHash(infoHash, len);
  174. infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
  175. // calculate the number of pieces
  176. Data* pieceHashData = (Data*)infoDic->get("pieces");
  177. numPieces = pieceHashData->getLen()/PIECE_HASH_LENGTH;
  178. // retrieve piece length
  179. Data* pieceLengthData = (Data*)infoDic->get("piece length");
  180. pieceLength = pieceLengthData->toInt();
  181. // retrieve piece hashes
  182. extractPieceHash((unsigned char*)pieceHashData->getData(),
  183. pieceHashData->getLen(),
  184. PIECE_HASH_LENGTH);
  185. // retrieve file entries
  186. extractFileEntries(infoDic, torrentFile);
  187. // retrieve announce
  188. Data* announceData = (Data*)rootDic->get("announce");
  189. List* announceListData = (List*)rootDic->get("announce-list");
  190. if(announceListData) {
  191. extractAnnounceList(announceListData);
  192. } else if(announceData) {
  193. extractAnnounce(announceData);
  194. }
  195. if(!announceTiers.size()) {
  196. throw new DlAbortEx("No announce URL found.");
  197. }
  198. }
  199. string DefaultBtContext::getPieceHash(int32_t index) const {
  200. if(index < 0 || numPieces <= index) {
  201. return "";
  202. }
  203. return pieceHashes[index];
  204. }
  205. int64_t DefaultBtContext::getTotalLength() const {
  206. return totalLength;
  207. }
  208. BtContext::FILE_MODE DefaultBtContext::getFileMode() const {
  209. return fileMode;
  210. }
  211. FileEntries DefaultBtContext::getFileEntries() const {
  212. return fileEntries;
  213. }
  214. AnnounceTiers DefaultBtContext::getAnnounceTiers() const {
  215. return announceTiers;
  216. }
  217. string DefaultBtContext::getName() const {
  218. return name;
  219. }
  220. int32_t DefaultBtContext::getPieceLength() const {
  221. return pieceLength;
  222. }
  223. int32_t DefaultBtContext::getNumPieces() const {
  224. return numPieces;
  225. }