bittorrent_helper.h 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* <!-- 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 --> */
#ifndef D_BITTORRENT_HELPER_H
#define D_BITTORRENT_HELPER_H

#include "common.h"

#include <cstring>
#include <string>
#include <vector>
#include <utility>
44
#include <memory>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

#include "TorrentAttribute.h"
#include "a2netcompat.h"
#include "Peer.h"
#include "ValueBase.h"
#include "util.h"
#include "DownloadContext.h"
#include "TimeA2.h"

namespace aria2 {

class DownloadContext;
class Randomizer;
class Option;

namespace bittorrent {

extern const std::string SINGLE;

extern const std::string MULTI;

void load(const std::string& torrentFile,
67 68
          const std::shared_ptr<DownloadContext>& ctx,
          const std::shared_ptr<Option>& option,
69 70 71
          const std::string& overrideName = "");

void load(const std::string& torrentFile,
72 73
          const std::shared_ptr<DownloadContext>& ctx,
          const std::shared_ptr<Option>& option,
74 75 76 77
          const std::vector<std::string>& uris,
          const std::string& overrideName = "");

void loadFromMemory(const unsigned char* content, size_t length,
78 79
                    const std::shared_ptr<DownloadContext>& ctx,
                    const std::shared_ptr<Option>& option,
80 81 82 83
                    const std::string& defaultName,
                    const std::string& overrideName = "");

void loadFromMemory(const unsigned char* content, size_t length,
84 85
                    const std::shared_ptr<DownloadContext>& ctx,
                    const std::shared_ptr<Option>& option,
86 87 88 89 90
                    const std::vector<std::string>& uris,
                    const std::string& defaultName,
                    const std::string& overrideName = "");

void loadFromMemory(const std::string& context,
91 92
                    const std::shared_ptr<DownloadContext>& ctx,
                    const std::shared_ptr<Option>& option,
93 94 95 96
                    const std::string& defaultName,
                    const std::string& overrideName = "");

void loadFromMemory(const std::string& context,
97 98
                    const std::shared_ptr<DownloadContext>& ctx,
                    const std::shared_ptr<Option>& option,
99 100 101 102
                    const std::vector<std::string>& uris,
                    const std::string& defaultName,
                    const std::string& overrideName = "");

103 104 105
void loadFromMemory(const ValueBase* torrent,
                    const std::shared_ptr<DownloadContext>& ctx,
                    const std::shared_ptr<Option>& option,
106 107 108 109
                    const std::vector<std::string>& uris,
                    const std::string& defaultName,
                    const std::string& overrideName = "");

110
// Parses BitTorrent Magnet URI and returns
111
// std::unique_ptr<TorrentAttribute> which includes infoHash, name and
112 113 114 115 116 117 118
// announceList. If parsing operation failed, an RecoverableException
// will be thrown.  infoHash and name are string and announceList is a
// list of list of announce URI.
//
// magnet:?xt=urn:btih:<info-hash>&dn=<name>&tr=<tracker-url>
// <info-hash> comes in 2 flavors: 40bytes hexadecimal ascii string,
// or 32bytes Base32 encoded string.
119
std::unique_ptr<TorrentAttribute> parseMagnet(const std::string& magnet);
120 121

// Parses BitTorrent Magnet URI and set them in ctx as a
122
// bittorrent::BITTORRENT attribute. If parsing operation failed, an
123
// RecoverableException will be thrown.
124 125
void loadMagnet(const std::string& magnet,
                const std::shared_ptr<DownloadContext>& ctx);
126 127 128

// Generates Peer ID. BitTorrent specification says Peer ID is 20-byte
// length.  This function uses peerIdPrefix as a Peer ID and it is
129
// less than 20bytes, random bytes are generated and appended to it. If
130 131 132 133 134 135 136
// peerIdPrefix is larger than 20bytes, first 20bytes are used.
std::string generatePeerId(const std::string& peerIdPrefix);

// Generates Peer ID and stores it in static variable. This function
// uses generatePeerId(peerIdPrefix) to produce Peer ID.
const std::string& generateStaticPeerId(const std::string& peerIdPrefix);

137 138
const std::string& generateStaticPeerAgent(const std::string& peerAgent);

139 140 141 142 143
// Returns Peer ID statically stored by generateStaticPeerId().  If
// Peer ID is not stored yet, this function calls
// generateStaticPeerId("aria2-")
const unsigned char* getStaticPeerId();

144 145
const std::string& getStaticPeerAgent();

146 147 148 149
// Set newPeerId as a static Peer ID. newPeerId must be 20-byte
// length.
void setStaticPeerId(const std::string& newPeerId);

150 151
void setStaticPeerAgent(const std::string& newPeerAgent);

152
// Computes fast set index and returns them.
153 154 155
std::vector<size_t> computeFastSet(const std::string& ipaddr, size_t numPieces,
                                   const unsigned char* infoHash,
                                   size_t fastSetSize);
156

157
// Make sure that don't receive return value into std::shared_ptr.
158
TorrentAttribute* getTorrentAttrs(DownloadContext* dctx);
159
TorrentAttribute* getTorrentAttrs(const std::shared_ptr<DownloadContext>& dctx);
160 161 162

// Returns the value associated with INFO_HASH key in BITTORRENT
// attribute.
163
const unsigned char* getInfoHash(DownloadContext* downloadContext);
164 165
const unsigned char*
getInfoHash(const std::shared_ptr<DownloadContext>& downloadContext);
166

167
std::string getInfoHashString(DownloadContext* downloadContext);
168 169
std::string
getInfoHashString(const std::shared_ptr<DownloadContext>& downloadContext);
170

171 172 173 174 175
// Returns 8bytes unsigned integer located at offset pos.  The integer
// in msg is network byte order. This function converts it into host
// byte order and returns it.
uint64_t getLLIntParam(const unsigned char* msg, size_t pos);

176 177 178 179 180 181 182 183 184 185
// Returns 4bytes unsigned integer located at offset pos.  The integer
// in msg is network byte order. This function converts it into host
// byte order and returns it.
uint32_t getIntParam(const unsigned char* msg, size_t pos);

// Returns 2bytes unsigned integer located at offset pos.  The integer
// in msg is network byte order. This function converts it into host
// byte order and returns it.
uint16_t getShortIntParam(const unsigned char* msg, size_t pos);

186 187 188 189
// Put param at location pointed by dest. param is converted into
// network byte order.
void setLLIntParam(unsigned char* dest, uint64_t param);

190 191 192 193 194 195 196 197 198 199
// Put param at location pointed by dest. param is converted into
// network byte order.
void setIntParam(unsigned char* dest, uint32_t param);

// Put param at location pointed by dest. param is converted into
// network byte order.
void setShortIntParam(unsigned char* dest, uint16_t param);

// Returns message ID located at first byte:msg[0]
uint8_t getId(const unsigned char* msg);
200

201 202 203 204
void checkIndex(size_t index, size_t pieces);
void checkBegin(int32_t begin, int32_t pieceLength);
void checkLength(int32_t length);
void checkRange(int32_t begin, int32_t length, int32_t pieceLength);
205 206
void checkBitfield(const unsigned char* bitfield, size_t bitfieldLength,
                   size_t pieces);
207 208

// Initialize msg with 0 and set payloadLength and messageId.
209 210
void createPeerMessageString(unsigned char* msg, size_t msgLength,
                             size_t payloadLength, uint8_t messageId);
211 212 213 214 215 216 217 218 219

/**
 * Creates compact form(packed addresss + 2bytes port) and stores the
 * results in compact.  This function looks addr and if it is IPv4
 * address, it stores 6bytes in compact and if it is IPv6, it stores
 * 18bytes in compact.  So compact must be at least 18 bytes and
 * pre-allocated.  Returns the number of written bytes; for IPv4
 * address, it is 6 and for IPv6, it is 18. On failure, returns 0.
 */
220 221
size_t packcompact(unsigned char* compact, const std::string& addr,
                   uint16_t port);
222 223 224 225 226 227 228 229

/**
 * Unpack packed address and port in compact and returns address and
 * port pair.  family must be AF_INET or AF_INET6.  If family is
 * AF_INET, first 6 bytes from compact is used.  If family is
 * AF_INET6, first 18 bytes from compact is used.  On failure, returns
 * std::pair<std::string, uint16_t>().
 */
230 231
std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact,
                                               int family);
232 233

// Throws exception if threshold >= actual
234 235
void assertPayloadLengthGreater(size_t threshold, size_t actual,
                                const char* msgName);
236 237

// Throws exception if expected != actual
238 239
void assertPayloadLengthEqual(size_t expected, size_t actual,
                              const char* msgName);
240 241

// Throws exception if expected is not equal to id from data.
242
void assertID(uint8_t expected, const unsigned char* data, const char* msgName);
243 244 245

// Converts attrs into torrent data. This function does not guarantee
// the returned string is valid torrent data.
246 247
std::string metadata2Torrent(const std::string& metadata,
                             const TorrentAttribute* attrs);
248 249

// Constructs BitTorrent Magnet URI using attrs.
250
std::string torrent2Magnet(const TorrentAttribute* attrs);
251 252 253

// Removes announce URI in uris from attrs.  If uris contains '*', all
// announce URIs are removed.
254 255
void removeAnnounceUri(TorrentAttribute* attrs,
                       const std::vector<std::string>& uris);
256 257 258

// Adds announce URI in uris to attrs. Each URI in uris creates its
// own tier.
259 260
void addAnnounceUri(TorrentAttribute* attrs,
                    const std::vector<std::string>& uris);
261 262 263 264 265 266

// This helper function uses 2 option values: PREF_BT_TRACKER and
// PREF_BT_EXCLUDE_TRACKER. First, the value of
// PREF_BT_EXCLUDE_TRACKER is converted to std::vector<std::string>
// and call removeAnnounceUri(). Then the value of PREF_BT_TRACKER is
// converted to std::vector<std::string> and call addAnnounceUri().
267 268
void adjustAnnounceUri(TorrentAttribute* attrs,
                       const std::shared_ptr<Option>& option);
269

270
template <typename OutputIterator>
271 272
void extractPeer(const ValueBase* peerData, int family, OutputIterator dest)
{
273
  class PeerListValueBaseVisitor : public ValueBaseVisitor {
274 275 276
  private:
    OutputIterator dest_;
    int family_;
277

278
  public:
279 280 281 282
    PeerListValueBaseVisitor(OutputIterator dest, int family)
        : dest_(dest), family_(family)
    {
    }
283

284
    virtual ~PeerListValueBaseVisitor() = default;
285

286
    virtual void visit(const String& peerData) CXX11_OVERRIDE
287
    {
288
      int unit = family_ == AF_INET ? 6 : 18;
289
      size_t length = peerData.s().size();
290
      if (length % unit == 0) {
291
        const unsigned char* base =
292 293 294
            reinterpret_cast<const unsigned char*>(peerData.s().data());
        const unsigned char* end = base + length;
        for (; base != end; base += unit) {
295
          std::pair<std::string, uint16_t> p = unpackcompact(base, family_);
296
          if (p.first.empty()) {
297 298
            continue;
          }
299
          *dest_++ = std::make_shared<Peer>(p.first, p.second);
300 301 302 303
        }
      }
    }

304 305 306
    virtual void visit(const Integer& v) CXX11_OVERRIDE {}
    virtual void visit(const Bool& v) CXX11_OVERRIDE {}
    virtual void visit(const Null& v) CXX11_OVERRIDE {}
307

308
    virtual void visit(const List& peerData) CXX11_OVERRIDE
309
    {
310
      for (auto& elem : peerData) {
311
        const Dict* peerDict = downcast<Dict>(elem);
312
        if (!peerDict) {
313 314 315 316 317 318
          continue;
        }
        static const std::string IP = "ip";
        static const std::string PORT = "port";
        const String* ip = downcast<String>(peerDict->get(IP));
        const Integer* port = downcast<Integer>(peerDict->get(PORT));
319
        if (!ip || !port || !(0 < port->i() && port->i() < 65536)) {
320 321
          continue;
        }
322
        *dest_ = std::make_shared<Peer>(ip->s(), port->i());
323 324 325 326
        ++dest_;
      }
    }

327
    virtual void visit(const Dict& v) CXX11_OVERRIDE {}
328
  };
329
  if (peerData) {
330 331 332 333 334 335 336
    PeerListValueBaseVisitor visitor(dest, family);
    peerData->accept(visitor);
  }
}

int getCompactLength(int family);

337 338 339
// Returns textual representation of the |mode|.
const char* getModeString(BtFileMode mode);

340
// Writes the detailed information about torrent loaded in dctx.
341
template <typename Output>
342
void print(Output& o, const std::shared_ptr<DownloadContext>& dctx)
343
{
344
  TorrentAttribute* torrentAttrs = getTorrentAttrs(dctx);
345
  o.write("*** BitTorrent File Information ***\n");
346
  if (!torrentAttrs->comment.empty()) {
347 348
    o.printf("Comment: %s\n", torrentAttrs->comment.c_str());
  }
349
  if (torrentAttrs->creationDate) {
350 351 352
    o.printf("Creation Date: %s\n",
             Time(torrentAttrs->creationDate).toHTTPDate().c_str());
  }
353
  if (!torrentAttrs->createdBy.empty()) {
354 355
    o.printf("Created By: %s\n", torrentAttrs->createdBy.c_str());
  }
356
  o.printf("Mode: %s\n", getModeString(torrentAttrs->mode));
357
  o.write("Announce:\n");
358 359 360 361 362
  for (std::vector<std::vector<std::string>>::const_iterator
           tierIter = torrentAttrs->announceList.begin(),
           eoi = torrentAttrs->announceList.end();
       tierIter != eoi; ++tierIter) {
    for (auto& elem : *tierIter) {
363
      o.printf(" %s", elem.c_str());
364 365 366 367 368 369 370 371 372 373 374
    }
    o.write("\n");
  }
  o.printf("Info Hash: %s\n", util::toHex(torrentAttrs->infoHash).c_str());
  o.printf("Piece Length: %sB\n",
           util::abbrevSize(dctx->getPieceLength()).c_str());
  o.printf("The Number of Pieces: %lu\n",
           static_cast<unsigned long>(dctx->getNumPieces()));
  o.printf("Total Length: %sB (%s)\n",
           util::abbrevSize(dctx->getTotalLength()).c_str(),
           util::uitos(dctx->getTotalLength(), true).c_str());
375
  if (!torrentAttrs->urlList.empty()) {
376
    o.write("URL List:\n");
377 378 379 380
    for (std::vector<std::string>::const_iterator
             i = torrentAttrs->urlList.begin(),
             eoi = torrentAttrs->urlList.end();
         i != eoi; ++i) {
381 382 383
      o.printf(" %s\n", (*i).c_str());
    }
  }
384 385 386 387 388 389
  if (!torrentAttrs->nodes.empty()) {
    o.write("Nodes:\n");
    for (auto& p : torrentAttrs->nodes) {
      o.printf(" %s:%u\n", p.first.c_str(), p.second);
    }
  }
390 391
  o.printf("Name: %s\n", torrentAttrs->name.c_str());
  o.printf("Magnet URI: %s\n", torrent2Magnet(torrentAttrs).c_str());
392 393
  util::toStream(dctx->getFileEntries().begin(), dctx->getFileEntries().end(),
                 o);
394 395 396 397 398 399 400
}

} // namespace bittorrent

} // namespace aria2

#endif // D_BITTORRENT_HELPER_H