DefaultBtContext.cc 7.4 KB

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