123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2009 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
- /* copyright --> */
- #include "bittorrent_helper.h"
- #include <cassert>
- #include <cstring>
- #include <algorithm>
- #include "DownloadContext.h"
- #include "Randomizer.h"
- #include "util.h"
- #include "DlAbortEx.h"
- #include "message.h"
- #include "StringFormat.h"
- #include "BtConstants.h"
- #include "MessageDigest.h"
- #include "MessageDigestHelper.h"
- #include "a2netcompat.h"
- #include "BtConstants.h"
- #include "bitfield.h"
- #include "base32.h"
- #include "magnet.h"
- #include "bencode2.h"
- #include "TorrentAttribute.h"
- #include "SocketCore.h"
- #include "Option.h"
- #include "prefs.h"
- namespace aria2 {
- namespace bittorrent {
- namespace {
- const std::string C_NAME("name");
- const std::string C_NAME_UTF8("name.utf-8");
- const std::string C_FILES("files");
- const std::string C_LENGTH("length");
- const std::string C_PATH("path");
- const std::string C_PATH_UTF8("path.utf-8");
- const std::string C_INFO("info");
- const std::string C_PIECES("pieces");
- const std::string C_PIECE_LENGTH("piece length");
- const std::string C_PRIVATE("private");
- const std::string C_URL_LIST("url-list");
- const std::string C_ANNOUNCE("announce");
- const std::string C_ANNOUNCE_LIST("announce-list");
- const std::string C_NODES("nodes");
- const std::string C_CREATION_DATE("creation date");
- const std::string C_COMMENT("comment");
- const std::string C_COMMENT_UTF8("comment.utf-8");
- const std::string C_CREATED_BY("created by");
- const std::string DEFAULT_PEER_ID_PREFIX("aria2-");
- } // namespace
- const std::string BITTORRENT("bittorrent");
- const std::string MULTI("multi");
- const std::string SINGLE("single");
- namespace {
- void extractPieceHash(const SharedHandle<DownloadContext>& ctx,
- const std::string& hashData,
- size_t hashLength,
- size_t numPieces)
- {
- std::vector<std::string> pieceHashes;
- pieceHashes.reserve(numPieces);
- for(size_t i = 0; i < numPieces; ++i) {
- pieceHashes.push_back(util::toHex(hashData.data()+i*hashLength,
- hashLength));
- }
- ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end());
- ctx->setPieceHashAlgo("sha-1");
- }
- } // namespace
- namespace {
- void extractUrlList
- (const SharedHandle<TorrentAttribute>& torrent, std::vector<std::string>& uris,
- const ValueBase* v)
- {
- class UrlListVisitor:public ValueBaseVisitor {
- private:
- std::vector<std::string>& uris_;
- const SharedHandle<TorrentAttribute>& torrent_;
- public:
- UrlListVisitor
- (std::vector<std::string>& uris,
- const SharedHandle<TorrentAttribute>& torrent):
- uris_(uris), torrent_(torrent) {}
- virtual void visit(const String& v)
- {
- uris_.push_back(v.s());
- torrent_->urlList.push_back(v.s());
- }
- virtual void visit(const Integer& v) {}
- virtual void visit(const List& v)
- {
- for(List::ValueType::const_iterator itr = v.begin(), eoi = v.end();
- itr != eoi; ++itr) {
- const String* uri = asString(*itr);
- if(uri) {
- uris_.push_back(uri->s());
- torrent_->urlList.push_back(uri->s());
- }
- }
- }
- virtual void visit(const Dict& v) {}
- };
- if(v) {
- UrlListVisitor visitor(uris, torrent);
- v->accept(visitor);
- }
- }
- } // namespace
- namespace {
- template<typename InputIterator, typename OutputIterator>
- OutputIterator createUri
- (InputIterator first, InputIterator last, OutputIterator out,
- const std::string& filePath)
- {
- for(; first != last; ++first) {
- if(util::endsWith(*first, "/")) {
- *out++ = (*first)+filePath;
- } else {
- *out++ = (*first)+"/"+filePath;
- }
- }
- return out;
- }
- } // namespace
- namespace {
- void extractFileEntries
- (const SharedHandle<DownloadContext>& ctx,
- const SharedHandle<TorrentAttribute>& torrent,
- const Dict* infoDict,
- const std::string& defaultName,
- const std::string& overrideName,
- const std::vector<std::string>& urlList)
- {
- std::string name;
- std::string utf8Name;
- if(overrideName.empty()) {
- std::string nameKey;
- if(infoDict->containsKey(C_NAME_UTF8)) {
- nameKey = C_NAME_UTF8;
- } else {
- nameKey = C_NAME;
- }
- const String* nameData = asString(infoDict->get(nameKey));
- if(nameData) {
- utf8Name = util::encodeNonUtf8(nameData->s());
- if(util::detectDirTraversal(utf8Name)) {
- throw DL_ABORT_EX
- (StringFormat
- (MSG_DIR_TRAVERSAL_DETECTED,nameData->s().c_str()).str());
- }
- name = nameData->s();
- } else {
- name = utf8Name = strconcat(File(defaultName).getBasename(), ".file");
- }
- } else {
- name = utf8Name = overrideName;
- }
- torrent->name = name;
- std::vector<SharedHandle<FileEntry> > fileEntries;
- const List* filesList = asList(infoDict->get(C_FILES));
- if(filesList) {
- fileEntries.reserve(filesList->size());
- uint64_t length = 0;
- off_t offset = 0;
- // multi-file mode
- torrent->mode = MULTI;
- for(List::ValueType::const_iterator itr = filesList->begin(),
- eoi = filesList->end(); itr != eoi; ++itr) {
- const Dict* fileDict = asDict(*itr);
- if(!fileDict) {
- continue;
- }
- const Integer* fileLengthData = asInteger(fileDict->get(C_LENGTH));
- if(!fileLengthData) {
- throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
- C_LENGTH.c_str()).str());
- }
- length += fileLengthData->i();
- std::string pathKey;
- if(fileDict->containsKey(C_PATH_UTF8)) {
- pathKey = C_PATH_UTF8;
- } else {
- pathKey = C_PATH;
- }
- const List* pathList = asList(fileDict->get(pathKey));
- if(!pathList || pathList->empty()) {
- throw DL_ABORT_EX("Path is empty.");
- }
-
- std::vector<std::string> pathelem(pathList->size()+1);
- pathelem[0] = name;
- std::vector<std::string>::iterator pathelemOutItr = pathelem.begin();
- ++pathelemOutItr;
- for(List::ValueType::const_iterator itr = pathList->begin(),
- eoi = pathList->end(); itr != eoi; ++itr) {
- const String* elem = asString(*itr);
- if(elem) {
- (*pathelemOutItr++) = elem->s();
- } else {
- throw DL_ABORT_EX("Path element is not string.");
- }
- }
- std::string path = strjoin(pathelem.begin(), pathelem.end(), '/');
- std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), '/',
- std::ptr_fun(util::encodeNonUtf8));
- if(util::detectDirTraversal(utf8Path)) {
- throw DL_ABORT_EX
- (StringFormat(MSG_DIR_TRAVERSAL_DETECTED, utf8Path.c_str()).str());
- }
- std::string pePath =
- strjoin(pathelem.begin(), pathelem.end(), '/',
- std::ptr_fun(static_cast<std::string (*)(const std::string&)>
- (util::percentEncode)));
- std::vector<std::string> uris;
- createUri(urlList.begin(), urlList.end(),std::back_inserter(uris),pePath);
- SharedHandle<FileEntry> fileEntry
- (new FileEntry(util::applyDir(ctx->getDir(),util::escapePath(utf8Path)),
- fileLengthData->i(), offset, uris));
- fileEntry->setOriginalName(path);
- fileEntries.push_back(fileEntry);
- offset += fileEntry->getLength();
- }
- } else {
- // single-file mode;
- torrent->mode = SINGLE;
- const Integer* lengthData = asInteger(infoDict->get(C_LENGTH));
- if(!lengthData) {
- throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
- C_LENGTH.c_str()).str());
- }
- uint64_t totalLength = lengthData->i();
- // For each uri in urlList, if it ends with '/', then
- // concatenate name to it. Specification just says so.
- std::vector<std::string> uris;
- for(std::vector<std::string>::const_iterator i = urlList.begin(),
- eoi = urlList.end(); i != eoi; ++i) {
- if(util::endsWith(*i, A2STR::SLASH_C)) {
- uris.push_back((*i)+util::percentEncode(name));
- } else {
- uris.push_back(*i);
- }
- }
- SharedHandle<FileEntry> fileEntry
- (new FileEntry(util::applyDir(ctx->getDir(), util::escapePath(utf8Name)),
- totalLength, 0, uris));
- fileEntry->setOriginalName(name);
- fileEntries.push_back(fileEntry);
- }
- ctx->setFileEntries(fileEntries.begin(), fileEntries.end());
- if(torrent->mode == MULTI) {
- ctx->setBasePath(util::applyDir(ctx->getDir(), utf8Name));
- }
- }
- } // namespace
- namespace {
- void extractAnnounce
- (const SharedHandle<TorrentAttribute>& torrent, const Dict* rootDict)
- {
- const List* announceList = asList(rootDict->get(C_ANNOUNCE_LIST));
- if(announceList) {
- for(List::ValueType::const_iterator tierIter = announceList->begin(),
- eoi = announceList->end(); tierIter != eoi; ++tierIter) {
- const List* tier = asList(*tierIter);
- if(!tier) {
- continue;
- }
- std::vector<std::string> ntier;
- for(List::ValueType::const_iterator uriIter = tier->begin(),
- eoi2 = tier->end(); uriIter != eoi2; ++uriIter) {
- const String* uri = asString(*uriIter);
- if(uri) {
- ntier.push_back(util::strip(uri->s()));
- }
- }
- if(!ntier.empty()) {
- torrent->announceList.push_back(ntier);
- }
- }
- } else {
- const String* announce = asString(rootDict->get(C_ANNOUNCE));
- if(announce) {
- std::vector<std::string> tier;
- tier.push_back(util::strip(announce->s()));
- torrent->announceList.push_back(tier);
- }
- }
- }
- } // namespace
- namespace {
- void extractNodes
- (const SharedHandle<TorrentAttribute>& torrent, const ValueBase* nodesListSrc)
- {
- const List* nodesList = asList(nodesListSrc);
- if(nodesList) {
- for(List::ValueType::const_iterator i = nodesList->begin(),
- eoi = nodesList->end(); i != eoi; ++i) {
- const List* addrPairList = asList(*i);
- if(!addrPairList || addrPairList->size() != 2) {
- continue;
- }
- const String* hostname = asString(addrPairList->get(0));
- if(!hostname) {
- continue;
- }
- if(util::strip(hostname->s()).empty()) {
- continue;
- }
- const Integer* port = asInteger(addrPairList->get(1));
- if(!port || !(0 < port->i() && port->i() < 65536)) {
- continue;
- }
- torrent->nodes.push_back(std::make_pair(hostname->s(), port->i()));
- }
- }
- }
- } // namespace
- namespace {
- void processRootDictionary
- (const SharedHandle<DownloadContext>& ctx,
- const SharedHandle<ValueBase>& root,
- const std::string& defaultName,
- const std::string& overrideName,
- const std::vector<std::string>& uris)
- {
- const Dict* rootDict = asDict(root);
- if(!rootDict) {
- throw DL_ABORT_EX("torrent file does not contain a root dictionary.");
- }
- const Dict* infoDict = asDict(rootDict->get(C_INFO));
- if(!infoDict) {
- throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
- C_INFO.c_str()).str());
- }
- SharedHandle<TorrentAttribute> torrent(new TorrentAttribute());
- // retrieve infoHash
- std::string encodedInfoDict = bencode2::encode(infoDict);
- unsigned char infoHash[INFO_HASH_LENGTH];
- MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
- MessageDigest::sha1(),
- encodedInfoDict.data(),
- encodedInfoDict.size());
- torrent->infoHash = std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]);
- torrent->metadata = encodedInfoDict;
- torrent->metadataSize = encodedInfoDict.size();
- // calculate the number of pieces
- const String* piecesData = asString(infoDict->get(C_PIECES));
- if(!piecesData) {
- throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
- C_PIECES.c_str()).str());
- }
- // Commented out To download 0 length torrent.
- // if(piecesData.s().empty()) {
- // throw DL_ABORT_EX("The length of piece hash is 0.");
- // }
- size_t numPieces = piecesData->s().size()/PIECE_HASH_LENGTH;
- // Commented out to download 0 length torrent.
- // if(numPieces == 0) {
- // throw DL_ABORT_EX("The number of pieces is 0.");
- // }
- // retrieve piece length
- const Integer* pieceLengthData = asInteger(infoDict->get(C_PIECE_LENGTH));
- if(!pieceLengthData) {
- throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
- C_PIECE_LENGTH.c_str()).str());
- }
- size_t pieceLength = pieceLengthData->i();
- ctx->setPieceLength(pieceLength);
- // retrieve piece hashes
- extractPieceHash(ctx, piecesData->s(), PIECE_HASH_LENGTH, numPieces);
- // private flag
- const Integer* privateData = asInteger(infoDict->get(C_PRIVATE));
- int privatefg = 0;
- if(privateData) {
- if(privateData->i() == 1) {
- privatefg = 1;
- }
- }
- if(privatefg) {
- torrent->privateTorrent = true;
- }
- // retrieve uri-list.
- // This implemantation obeys HTTP-Seeding specification:
- // see http://www.getright.com/seedtorrent.html
- std::vector<std::string> urlList;
- extractUrlList(torrent, urlList, rootDict->get(C_URL_LIST).get());
- urlList.insert(urlList.end(), uris.begin(), uris.end());
- std::sort(urlList.begin(), urlList.end());
- urlList.erase(std::unique(urlList.begin(), urlList.end()), urlList.end());
- // retrieve file entries
- extractFileEntries
- (ctx, torrent, infoDict, defaultName, overrideName, urlList);
- if((ctx->getTotalLength()+pieceLength-1)/pieceLength != numPieces) {
- throw DL_ABORT_EX("Too few/many piece hash.");
- }
- // retrieve announce
- extractAnnounce(torrent, rootDict);
- // retrieve nodes
- extractNodes(torrent, rootDict->get(C_NODES).get());
- const Integer* creationDate = asInteger(rootDict->get(C_CREATION_DATE));
- if(creationDate) {
- torrent->creationDate = creationDate->i();
- }
- const String* commentUtf8 = asString(rootDict->get(C_COMMENT_UTF8));
- if(commentUtf8) {
- torrent->comment = commentUtf8->s();
- } else {
- const String* comment = asString(rootDict->get(C_COMMENT));
- if(comment) {
- torrent->comment = comment->s();
- }
- }
- const String* createdBy = asString(rootDict->get(C_CREATED_BY));
- if(createdBy) {
- torrent->createdBy = createdBy->s();
- }
- ctx->setAttribute(BITTORRENT, torrent);
- }
- } // namespace
- void load(const std::string& torrentFile,
- const SharedHandle<DownloadContext>& ctx,
- const std::string& overrideName)
- {
- processRootDictionary(ctx,
- bencode2::decodeFromFile(torrentFile),
- torrentFile,
- overrideName,
- std::vector<std::string>());
- }
- void load(const std::string& torrentFile,
- const SharedHandle<DownloadContext>& ctx,
- const std::vector<std::string>& uris,
- const std::string& overrideName)
- {
- processRootDictionary(ctx,
- bencode2::decodeFromFile(torrentFile),
- torrentFile,
- overrideName,
- uris);
- }
- void loadFromMemory(const unsigned char* content,
- size_t length,
- const SharedHandle<DownloadContext>& ctx,
- const std::string& defaultName,
- const std::string& overrideName)
- {
- processRootDictionary(ctx,
- bencode2::decode(content, length),
- defaultName,
- overrideName,
- std::vector<std::string>());
- }
- void loadFromMemory(const unsigned char* content,
- size_t length,
- const SharedHandle<DownloadContext>& ctx,
- const std::vector<std::string>& uris,
- const std::string& defaultName,
- const std::string& overrideName)
- {
- processRootDictionary(ctx,
- bencode2::decode(content, length),
- defaultName,
- overrideName,
- uris);
- }
- void loadFromMemory(const std::string& context,
- const SharedHandle<DownloadContext>& ctx,
- const std::string& defaultName,
- const std::string& overrideName)
- {
- processRootDictionary
- (ctx,
- bencode2::decode(context),
- defaultName, overrideName,
- std::vector<std::string>());
- }
- void loadFromMemory(const std::string& context,
- const SharedHandle<DownloadContext>& ctx,
- const std::vector<std::string>& uris,
- const std::string& defaultName,
- const std::string& overrideName)
- {
- processRootDictionary
- (ctx,
- bencode2::decode(context),
- defaultName, overrideName,
- uris);
- }
- SharedHandle<TorrentAttribute> getTorrentAttrs
- (const SharedHandle<DownloadContext>& dctx)
- {
- return static_pointer_cast<TorrentAttribute>(dctx->getAttribute(BITTORRENT));
- }
- const unsigned char*
- getInfoHash(const SharedHandle<DownloadContext>& dctx)
- {
- return reinterpret_cast<const unsigned char*>
- (getTorrentAttrs(dctx)->infoHash.data());
- }
- std::string
- getInfoHashString(const SharedHandle<DownloadContext>& dctx)
- {
- return util::toHex(getTorrentAttrs(dctx)->infoHash);
- }
- void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx)
- {
- SharedHandle<TorrentAttribute> torrentAttrs = getTorrentAttrs(dctx);
- o << "*** BitTorrent File Information ***" << "\n";
- if(!torrentAttrs->comment.empty()) {
- o << "Comment: " << torrentAttrs->comment << "\n";
- }
- if(torrentAttrs->creationDate) {
- o << "Creation Date: " << Time(torrentAttrs->creationDate).toHTTPDate()
- << std::endl;
- }
- if(!torrentAttrs->createdBy.empty()) {
- o << "Created By: " << torrentAttrs->createdBy << "\n";
- }
- o << "Mode: " << torrentAttrs->mode << "\n";
- o << "Announce:" << "\n";
- for(std::vector<std::vector<std::string> >::const_iterator tierIter =
- torrentAttrs->announceList.begin(),
- eoi = torrentAttrs->announceList.end(); tierIter != eoi; ++tierIter) {
- for(std::vector<std::string>::const_iterator i = (*tierIter).begin(),
- eoi2 = (*tierIter).end(); i != eoi2; ++i) {
- o << " " << *i;
- }
- o << "\n";
- }
- o << "Info Hash: "
- << util::toHex(torrentAttrs->infoHash) << "\n"
- << "Piece Length: "
- << util::abbrevSize(dctx->getPieceLength()) << "B\n"
- << "The Number of Pieces: "
- << dctx->getNumPieces() << "\n"
- << "Total Length: "
- << util::abbrevSize(dctx->getTotalLength()) << "B ("
- << util::uitos(dctx->getTotalLength(), true) << ")\n";
- if(!torrentAttrs->urlList.empty()) {
- o << "URL List: " << "\n";
- for(std::vector<std::string>::const_iterator i =
- torrentAttrs->urlList.begin(),
- eoi = torrentAttrs->urlList.end(); i != eoi; ++i) {
- o << " " << *i << "\n";
- }
- }
- o << "Name: " << torrentAttrs->name << "\n"
- << "Magnet URI: " << torrent2Magnet(torrentAttrs) << "\n";
- util::toStream
- (dctx->getFileEntries().begin(), dctx->getFileEntries().end(), o);
- }
- void computeFastSet
- (std::vector<size_t>& fastSet, const std::string& ipaddr,
- size_t numPieces, const unsigned char* infoHash, size_t fastSetSize)
- {
- unsigned char compact[COMPACT_LEN_IPV6];
- int compactlen = packcompact(compact, ipaddr, 0);
- if(compactlen != COMPACT_LEN_IPV4) {
- return;
- }
- if(numPieces < fastSetSize) {
- fastSetSize = numPieces;
- }
- unsigned char tx[24];
- memcpy(tx, compact, 4);
- if((tx[0] & 0x80u) == 0 || (tx[0] & 0x40u) == 0) {
- tx[2] = 0x00u;
- tx[3] = 0x00u;
- } else {
- tx[3] = 0x00u;
- }
- memcpy(tx+4, infoHash, 20);
- unsigned char x[20];
- SharedHandle<MessageDigest> sha1 = MessageDigest::sha1();
- MessageDigestHelper::digest(x, sizeof(x), sha1, tx, 24);
- while(fastSet.size() < fastSetSize) {
- for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
- size_t j = i*4;
- uint32_t ny;
- memcpy(&ny, x+j, 4);
- uint32_t y = ntohl(ny);
- size_t index = y%numPieces;
- if(std::find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
- fastSet.push_back(index);
- }
- }
- unsigned char temp[20];
- sha1->reset();
- MessageDigestHelper::digest(temp, sizeof(temp), sha1, x, sizeof(x));
- memcpy(x, temp, sizeof(x));
- }
- }
- std::string generatePeerId(const std::string& peerIdPrefix)
- {
- std::string peerId = peerIdPrefix;
- unsigned char buf[20];
- int len = 20-peerIdPrefix.size();
- if(len > 0) {
- util::generateRandomData(buf, len);
- peerId += std::string(&buf[0], &buf[len]);
- } if(peerId.size() > 20) {
- peerId.erase(20);
- }
- return peerId;
- }
- namespace {
- std::string peerId;
- } // namespace
- const std::string& generateStaticPeerId(const std::string& peerIdPrefix)
- {
- if(peerId.empty()) {
- peerId = generatePeerId(peerIdPrefix);
- }
- return peerId;
- }
- void setStaticPeerId(const std::string& newPeerId)
- {
- peerId = newPeerId;
- }
- // If PeerID is not generated, it is created with default peerIdPrefix
- // (aria2-).
- const unsigned char* getStaticPeerId()
- {
- if(peerId.empty()) {
- return
- reinterpret_cast<const unsigned char*>
- (generateStaticPeerId(DEFAULT_PEER_ID_PREFIX).data());
- } else {
- return reinterpret_cast<const unsigned char*>(peerId.data());
- }
- }
- uint8_t getId(const unsigned char* msg)
- {
- return msg[0];
- }
- uint32_t getIntParam(const unsigned char* msg, size_t pos)
- {
- uint32_t nParam;
- memcpy(&nParam, msg+pos, sizeof(nParam));
- return ntohl(nParam);
- }
- uint16_t getShortIntParam(const unsigned char* msg, size_t pos)
- {
- uint16_t nParam;
- memcpy(&nParam, msg+pos, sizeof(nParam));
- return ntohs(nParam);
- }
- void checkIndex(size_t index, size_t pieces)
- {
- if(!(index < pieces)) {
- throw DL_ABORT_EX(StringFormat("Invalid index: %lu",
- static_cast<unsigned long>(index)).str());
- }
- }
- void checkBegin(uint32_t begin, size_t pieceLength)
- {
- if(!(begin < pieceLength)) {
- throw DL_ABORT_EX(StringFormat("Invalid begin: %u", begin).str());
- }
- }
- void checkLength(size_t length)
- {
- if(length > MAX_BLOCK_LENGTH) {
- throw DL_ABORT_EX
- (StringFormat("Length too long: %lu > %uKB",
- static_cast<unsigned long>(length),
- MAX_BLOCK_LENGTH/1024).str());
- }
- if(length == 0) {
- throw DL_ABORT_EX
- (StringFormat("Invalid length: %lu",
- static_cast<unsigned long>(length)).str());
- }
- }
- void checkRange(uint32_t begin, size_t length, size_t pieceLength)
- {
- if(!(0 < length)) {
- throw DL_ABORT_EX
- (StringFormat("Invalid range: begin=%u, length=%lu",
- begin,
- static_cast<unsigned long>(length)).str());
- }
- uint32_t end = begin+length;
- if(!(end <= pieceLength)) {
- throw DL_ABORT_EX
- (StringFormat("Invalid range: begin=%u, length=%lu",
- begin,
- static_cast<unsigned long>(length)).str());
- }
- }
- void checkBitfield
- (const unsigned char* bitfield, size_t bitfieldLength, size_t pieces)
- {
- if(!(bitfieldLength == (pieces+7)/8)) {
- throw DL_ABORT_EX
- (StringFormat("Invalid bitfield length: %lu",
- static_cast<unsigned long>(bitfieldLength)).str());
- }
- // Check if last byte contains garbage set bit.
- if(bitfield[bitfieldLength-1]&~bitfield::lastByteMask(pieces)) {
- throw DL_ABORT_EX("Invalid bitfield");
- }
- }
- void setIntParam(unsigned char* dest, uint32_t param)
- {
- uint32_t nParam = htonl(param);
- memcpy(dest, &nParam, sizeof(nParam));
- }
- void setShortIntParam(unsigned char* dest, uint16_t param)
- {
- uint16_t nParam = htons(param);
- memcpy(dest, &nParam, sizeof(nParam));
- }
- void createPeerMessageString
- (unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId)
- {
- assert(msgLength >= 5);
- memset(msg, 0, msgLength);
- setIntParam(msg, payloadLength);
- msg[4] = messageId;
- }
- int packcompact
- (unsigned char* compact, const std::string& addr, uint16_t port)
- {
- struct addrinfo* res;
- int s =
- callGetaddrinfo(&res, addr.c_str(), 0, AF_UNSPEC, 0, AI_NUMERICHOST, 0);
- if(s != 0) {
- return 0;
- }
- WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
- uint16_t portN = htons(port);
- for(struct addrinfo* rp = res; rp; rp = rp->ai_next) {
- if(rp->ai_family == AF_INET) {
- struct sockaddr_in* in =
- reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
- memcpy(compact, &(in->sin_addr), 4);
- memcpy(compact+4, &portN, sizeof(portN));
- return COMPACT_LEN_IPV4;
- } else if(rp->ai_family == AF_INET6) {
- struct sockaddr_in6* in6 =
- reinterpret_cast<struct sockaddr_in6*>(res->ai_addr);
- memcpy(compact, &(in6->sin6_addr), 16);
- memcpy(compact+16, &portN, sizeof(portN));
- return COMPACT_LEN_IPV6;
- }
- }
- return 0;
- }
- std::pair<std::string, uint16_t> unpackcompact
- (const unsigned char* compact, int family)
- {
- std::pair<std::string, uint16_t> r;
- #ifdef HAVE_INET_NTOP
- int portOffset = family == AF_INET?4:16;
- char buf[INET6_ADDRSTRLEN];
- if(!inet_ntop(family, compact, buf, sizeof(buf))) {
- return r;
- }
- r.first = buf;
- uint16_t portN;
- memcpy(&portN, compact+portOffset, sizeof(portN));
- r.second = ntohs(portN);
- #else // !HAVE_INET_NTOP
- if(family != AF_INET) {
- return r;
- }
- struct in_addr addr;
- memcpy(&addr, compact, sizeof(in_addr));
- r.first = inet_ntoa(addr);
- uint16_t portN;
- memcpy(&portN, compact+4, sizeof(portN));
- r.second = ntohs(portN);
- #endif // !HAVE_INET_NTOP
- return r;
- }
- void assertPayloadLengthGreater
- (size_t threshold, size_t actual, const std::string& msgName)
- {
- if(actual <= threshold) {
- throw DL_ABORT_EX
- (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(),
- static_cast<unsigned long>(actual)).str());
- }
- }
- void assertPayloadLengthEqual
- (size_t expected, size_t actual, const std::string& msgName)
- {
- if(expected != actual) {
- throw DL_ABORT_EX
- (StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
- static_cast<unsigned long>(actual),
- static_cast<unsigned long>(expected)).str());
- }
- }
- void assertID
- (uint8_t expected, const unsigned char* data, const std::string& msgName)
- {
- uint8_t id = getId(data);
- if(expected != id) {
- throw DL_ABORT_EX
- (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
- expected).str());
- }
- }
- SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet)
- {
- SharedHandle<Dict> r = magnet::parse(magnet);
- if(!r) {
- throw DL_ABORT_EX("Bad BitTorrent Magnet URI.");
- }
- const List* xts = asList(r->get("xt"));
- if(!xts) {
- throw DL_ABORT_EX("Missing xt parameter in Magnet URI.");
- }
- SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
- std::string infoHash;
- for(List::ValueType::const_iterator xtiter = xts->begin(),
- eoi = xts->end(); xtiter != eoi && infoHash.empty(); ++xtiter) {
- const String* xt = asString(*xtiter);
- if(util::startsWith(xt->s(), "urn:btih:")) {
- std::string xtarg = xt->s().substr(9);
- size_t size = xtarg.size();
- if(size == 32) {
- std::string rawhash = base32::decode(xtarg);
- if(rawhash.size() == 20) {
- infoHash.swap(rawhash);
- }
- } else if(size == 40) {
- std::string rawhash = util::fromHex(xtarg);
- if(!rawhash.empty()) {
- infoHash.swap(rawhash);
- }
- }
- }
- }
- if(infoHash.empty()) {
- throw DL_ABORT_EX("Bad BitTorrent Magnet URI. "
- "No valid BitTorrent Info Hash found.");
- }
- const List* trs = asList(r->get("tr"));
- if(trs) {
- for(List::ValueType::const_iterator i = trs->begin(), eoi = trs->end();
- i != eoi; ++i) {
- std::vector<std::string> tier;
- tier.push_back(asString(*i)->s());
- attrs->announceList.push_back(tier);
- }
- }
- std::string name = "[METADATA]";
- const List* dns = asList(r->get("dn"));
- if(dns && !dns->empty()) {
- const String* dn = asString(dns->get(0));
- name += dn->s();
- } else {
- name += util::toHex(infoHash);
- }
- attrs->infoHash = infoHash;
- attrs->name = name;
- return attrs;
- }
- void loadMagnet
- (const std::string& magnet, const SharedHandle<DownloadContext>& dctx)
- {
- SharedHandle<TorrentAttribute> attrs = parseMagnet(magnet);
- dctx->setAttribute(BITTORRENT, attrs);
- }
- std::string metadata2Torrent
- (const std::string& metadata, const SharedHandle<TorrentAttribute>& attrs)
- {
- std::string torrent = "d";
- List announceList;
- for(std::vector<std::vector<std::string> >::const_iterator tierIter =
- attrs->announceList.begin(),
- eoi = attrs->announceList.end(); tierIter != eoi; ++tierIter) {
- SharedHandle<List> tier = List::g();
- for(std::vector<std::string>::const_iterator uriIter = (*tierIter).begin(),
- eoi2 = (*tierIter).end(); uriIter != eoi2; ++uriIter) {
- tier->append(String::g(*uriIter));
- }
- if(!tier->empty()) {
- announceList.append(tier);
- }
- }
- if(!announceList.empty()) {
- torrent += "13:announce-list";
- torrent += bencode2::encode(&announceList);
- }
- torrent +=
- strconcat("4:info", metadata, "e");
- return torrent;
- }
- std::string torrent2Magnet(const SharedHandle<TorrentAttribute>& attrs)
- {
- std::string uri = "magnet:?";
- if(!attrs->infoHash.empty()) {
- uri += "xt=urn:btih:";
- uri += util::toUpper(util::toHex(attrs->infoHash));
- } else {
- return A2STR::NIL;
- }
- if(!attrs->name.empty()) {
- uri += "&dn=";
- uri += util::percentEncode(attrs->name);
- }
- for(std::vector<std::vector<std::string> >::const_iterator tierIter =
- attrs->announceList.begin(),
- eoi = attrs->announceList.end(); tierIter != eoi; ++tierIter) {
- for(std::vector<std::string>::const_iterator uriIter = (*tierIter).begin(),
- eoi2 = (*tierIter).end(); uriIter != eoi2; ++uriIter) {
- uri += "&tr=";
- uri += util::percentEncode(*uriIter);
- }
- }
- return uri;
- }
- int getCompactLength(int family)
- {
- if(family == AF_INET) {
- return COMPACT_LEN_IPV4;
- } else if(family == AF_INET6) {
- return COMPACT_LEN_IPV6;
- } else {
- return 0;
- }
- }
- void removeAnnounceUri
- (const SharedHandle<TorrentAttribute>& attrs,
- const std::vector<std::string>& uris)
- {
- if(uris.empty()) {
- return;
- }
- if(std::find(uris.begin(), uris.end(), "*") == uris.end()) {
- for(std::vector<std::vector<std::string> >::iterator i =
- attrs->announceList.begin(); i != attrs->announceList.end();) {
- for(std::vector<std::string>::iterator j =(*i).begin();j != (*i).end();) {
- if(std::find(uris.begin(), uris.end(), *j) == uris.end()) {
- ++j;
- } else {
- j = (*i).erase(j);
- }
- }
- if((*i).empty()) {
- i = attrs->announceList.erase(i);
- } else {
- ++i;
- }
- }
- } else {
- attrs->announceList.clear();
- }
- }
- void addAnnounceUri
- (const SharedHandle<TorrentAttribute>& attrs,
- const std::vector<std::string>& uris)
- {
- for(std::vector<std::string>::const_iterator i = uris.begin(),
- eoi = uris.end(); i != eoi; ++i) {
- std::vector<std::string> tier;
- tier.push_back(*i);
- attrs->announceList.push_back(tier);
- }
- }
- void adjustAnnounceUri
- (const SharedHandle<TorrentAttribute>& attrs,
- const SharedHandle<Option>& option)
- {
- std::vector<std::string> excludeUris;
- std::vector<std::string> addUris;
- util::split(option->get(PREF_BT_EXCLUDE_TRACKER),
- std::back_inserter(excludeUris), A2STR::COMMA_C, true);
- util::split(option->get(PREF_BT_TRACKER),
- std::back_inserter(addUris), A2STR::COMMA_C, true);
- removeAnnounceUri(attrs, excludeUris);
- addAnnounceUri(attrs, addUris);
- }
- } // namespace bittorrent
- } // namespace aria2
|