2009-06-28 10:37:15 +00:00
|
|
|
/* <!-- 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-06-28 10:37:15 +00:00
|
|
|
*
|
|
|
|
* 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 --> */
|
2010-10-31 07:23:53 +00:00
|
|
|
#ifndef D_BITTORRENT_HELPER_H
|
|
|
|
#define D_BITTORRENT_HELPER_H
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-06-20 12:12:18 +00:00
|
|
|
#include <cstring>
|
2009-06-28 10:37:15 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2009-09-29 14:52:42 +00:00
|
|
|
#include <utility>
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
#include "SharedHandle.h"
|
2010-06-18 14:47:09 +00:00
|
|
|
#include "TorrentAttribute.h"
|
2010-06-20 12:12:18 +00:00
|
|
|
#include "a2netcompat.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "ValueBase.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class DownloadContext;
|
|
|
|
class Randomizer;
|
2010-08-24 14:21:12 +00:00
|
|
|
class Option;
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
namespace bittorrent {
|
|
|
|
|
2009-08-19 14:09:19 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
extern const std::string SINGLE;
|
|
|
|
|
|
|
|
extern const std::string MULTI;
|
|
|
|
|
|
|
|
extern const std::string BITTORRENT;
|
|
|
|
|
2009-11-20 15:42:25 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
void load(const std::string& torrentFile,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& overrideName = "");
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
void load(const std::string& torrentFile,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& uris,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& overrideName = "");
|
2009-06-29 08:42:58 +00:00
|
|
|
|
|
|
|
void loadFromMemory(const unsigned char* content, size_t length,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& defaultName,
|
|
|
|
const std::string& overrideName = "");
|
2009-06-29 08:42:58 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
void loadFromMemory(const unsigned char* content, size_t length,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& uris,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& defaultName,
|
|
|
|
const std::string& overrideName = "");
|
2009-06-29 08:42:58 +00:00
|
|
|
|
|
|
|
void loadFromMemory(const std::string& context,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& defaultName,
|
|
|
|
const std::string& overrideName = "");
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
void loadFromMemory(const std::string& context,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& ctx,
|
2010-12-03 04:08:22 +00:00
|
|
|
const SharedHandle<Option>& option,
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<std::string>& uris,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& defaultName,
|
|
|
|
const std::string& overrideName = "");
|
2009-06-28 10:37:15 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Parses BitTorrent Magnet URI and returns
|
|
|
|
// SharedHandle<TorrentAttribute> which includes infoHash, name and
|
|
|
|
// 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.
|
2009-11-24 14:47:42 +00:00
|
|
|
//
|
2009-11-22 14:35:35 +00:00
|
|
|
// magnet:?xt=urn:btih:<info-hash>&dn=<name>&tr=<tracker-url>
|
2009-11-24 14:47:42 +00:00
|
|
|
// <info-hash> comes in 2 flavors: 40bytes hexadecimal ascii string,
|
|
|
|
// or 32bytes Base32 encoded string.
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet);
|
2009-11-24 14:47:42 +00:00
|
|
|
|
|
|
|
// Parses BitTorrent Magnet URI and set them in ctx as a
|
|
|
|
// bittorrent::BITTORRENT attibute. If parsing operation failed, an
|
|
|
|
// RecoverableException will be thrown.
|
|
|
|
void loadMagnet
|
|
|
|
(const std::string& magnet, const SharedHandle<DownloadContext>& ctx);
|
2009-11-22 14:35:35 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
// Generates Peer ID. BitTorrent specification says Peer ID is 20-byte
|
|
|
|
// length. This function uses peerIdPrefix as a Peer ID and it is
|
|
|
|
// less than 20bytes, random bytes are generated and appened to it. If
|
|
|
|
// peerIdPrefix is larger than 20bytes, first 20bytes are used.
|
2009-10-05 13:04:06 +00:00
|
|
|
std::string generatePeerId(const std::string& peerIdPrefix);
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
// Generates Peer ID and stores it in static variable. This function
|
2009-10-05 13:04:06 +00:00
|
|
|
// uses generatePeerId(peerIdPrefix) to produce Peer ID.
|
|
|
|
const std::string& generateStaticPeerId(const std::string& peerIdPrefix);
|
2009-06-28 10:37:15 +00:00
|
|
|
|
|
|
|
// Returns Peer ID statically stored by generateStaticPeerId(). If
|
|
|
|
// Peer ID is not stored yet, this function calls
|
2009-10-05 13:04:06 +00:00
|
|
|
// generateStaticPeerId("aria2-")
|
2009-06-28 10:37:15 +00:00
|
|
|
const unsigned char* getStaticPeerId();
|
|
|
|
|
|
|
|
// Set newPeerId as a static Peer ID. newPeerId must be 20-byte
|
|
|
|
// length.
|
|
|
|
void setStaticPeerId(const std::string& newPeerId);
|
|
|
|
|
|
|
|
// Computes fast set index and stores them in fastset.
|
|
|
|
void computeFastSet
|
|
|
|
(std::vector<size_t>& fastSet, const std::string& ipaddr,
|
|
|
|
size_t numPieces, const unsigned char* infoHash, size_t fastSetSize);
|
|
|
|
|
|
|
|
// Writes the detailed information about torrent loaded in dctx.
|
|
|
|
void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
SharedHandle<TorrentAttribute> getTorrentAttrs
|
|
|
|
(const SharedHandle<DownloadContext>& dctx);
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
// Returns the value associated with INFO_HASH key in BITTORRENT
|
|
|
|
// attribute.
|
|
|
|
const unsigned char*
|
|
|
|
getInfoHash(const SharedHandle<DownloadContext>& downloadContext);
|
|
|
|
|
|
|
|
std::string
|
|
|
|
getInfoHashString(const SharedHandle<DownloadContext>& downloadContext);
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
void checkIndex(size_t index, size_t pieces);
|
|
|
|
void checkBegin(uint32_t begin, size_t pieceLength);
|
|
|
|
void checkLength(size_t length);
|
|
|
|
void checkRange(uint32_t begin, size_t length, size_t pieceLength);
|
|
|
|
void checkBitfield
|
|
|
|
(const unsigned char* bitfield, size_t bitfieldLength, size_t pieces);
|
|
|
|
|
|
|
|
// Initialize msg with 0 and set payloadLength and messageId.
|
|
|
|
void createPeerMessageString
|
|
|
|
(unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId);
|
|
|
|
|
|
|
|
/**
|
2010-08-03 11:44:24 +00:00
|
|
|
* 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.
|
2009-09-29 14:52:42 +00:00
|
|
|
*/
|
2010-08-03 11:44:24 +00:00
|
|
|
int packcompact
|
2009-09-29 14:52:42 +00:00
|
|
|
(unsigned char* compact, const std::string& addr, uint16_t port);
|
|
|
|
|
2010-08-03 11:44:24 +00:00
|
|
|
/**
|
|
|
|
* 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>().
|
|
|
|
*/
|
|
|
|
std::pair<std::string, uint16_t>
|
|
|
|
unpackcompact(const unsigned char* compact, int family);
|
2009-09-29 14:52:42 +00:00
|
|
|
|
|
|
|
// Throws exception if threshold >= actual
|
|
|
|
void assertPayloadLengthGreater
|
|
|
|
(size_t threshold, size_t actual, const std::string& msgName);
|
|
|
|
|
|
|
|
// Throws exception if expected != actual
|
|
|
|
void assertPayloadLengthEqual
|
|
|
|
(size_t expected, size_t actual, const std::string& msgName);
|
|
|
|
|
|
|
|
// Throws exception if expected is not equal to id from data.
|
|
|
|
void assertID
|
|
|
|
(uint8_t expected, const unsigned char* data, const std::string& msgName);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Converts attrs into torrent data. This function does not guarantee
|
|
|
|
// the returned string is valid torrent data.
|
|
|
|
std::string metadata2Torrent
|
|
|
|
(const std::string& metadata, const SharedHandle<TorrentAttribute>& attrs);
|
2009-11-28 09:58:49 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Constructs BitTorrent Magnet URI using attrs.
|
|
|
|
std::string torrent2Magnet(const SharedHandle<TorrentAttribute>& attrs);
|
2009-12-10 12:52:59 +00:00
|
|
|
|
2010-08-24 14:21:12 +00:00
|
|
|
// Removes announce URI in uris from attrs. If uris contains '*', all
|
|
|
|
// announce URIs are removed.
|
|
|
|
void removeAnnounceUri
|
|
|
|
(const SharedHandle<TorrentAttribute>& attrs,
|
|
|
|
const std::vector<std::string>& uris);
|
|
|
|
|
|
|
|
// Adds announce URI in uris to attrs. Each URI in uris creates its
|
|
|
|
// own tier.
|
|
|
|
void addAnnounceUri
|
|
|
|
(const SharedHandle<TorrentAttribute>& attrs,
|
|
|
|
const std::vector<std::string>& uris);
|
|
|
|
|
|
|
|
// 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().
|
|
|
|
void adjustAnnounceUri
|
|
|
|
(const SharedHandle<TorrentAttribute>& attrs,
|
|
|
|
const SharedHandle<Option>& option);
|
|
|
|
|
2010-06-20 12:12:18 +00:00
|
|
|
template<typename OutputIterator>
|
2010-08-03 11:44:24 +00:00
|
|
|
void extractPeer(const ValueBase* peerData, int family, OutputIterator dest)
|
2010-06-20 12:12:18 +00:00
|
|
|
{
|
|
|
|
class PeerListValueBaseVisitor:public ValueBaseVisitor {
|
|
|
|
private:
|
|
|
|
OutputIterator dest_;
|
2010-08-03 11:44:24 +00:00
|
|
|
int family_;
|
2010-06-20 12:12:18 +00:00
|
|
|
public:
|
2010-08-03 11:44:24 +00:00
|
|
|
PeerListValueBaseVisitor(OutputIterator dest, int family):
|
|
|
|
dest_(dest),
|
|
|
|
family_(family) {}
|
2010-06-20 12:12:18 +00:00
|
|
|
|
|
|
|
virtual ~PeerListValueBaseVisitor() {}
|
|
|
|
|
|
|
|
virtual void visit(const String& peerData)
|
|
|
|
{
|
2010-08-03 11:44:24 +00:00
|
|
|
int unit = family_ == AF_INET?6:18;
|
2010-06-20 12:12:18 +00:00
|
|
|
size_t length = peerData.s().size();
|
2010-08-03 11:44:24 +00:00
|
|
|
if(length%unit == 0) {
|
|
|
|
const unsigned char* base =
|
|
|
|
reinterpret_cast<const unsigned char*>(peerData.s().data());
|
|
|
|
const unsigned char* end = base+length;
|
|
|
|
for(; base != end; base += unit) {
|
|
|
|
std::pair<std::string, uint16_t> p = unpackcompact(base, family_);
|
2010-08-07 14:15:21 +00:00
|
|
|
if(p.first.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-03 11:44:24 +00:00
|
|
|
*dest_++ = SharedHandle<Peer>(new Peer(p.first, p.second));
|
2010-06-20 12:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const Integer& v) {}
|
2011-03-09 14:07:27 +00:00
|
|
|
virtual void visit(const Bool& v) {}
|
|
|
|
virtual void visit(const Null& v) {}
|
2010-06-20 12:12:18 +00:00
|
|
|
|
|
|
|
virtual void visit(const List& peerData)
|
|
|
|
{
|
|
|
|
for(List::ValueType::const_iterator itr = peerData.begin(),
|
|
|
|
eoi = peerData.end(); itr != eoi; ++itr) {
|
|
|
|
const Dict* peerDict = asDict(*itr);
|
|
|
|
if(!peerDict) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
static const std::string IP = "ip";
|
|
|
|
static const std::string PORT = "port";
|
|
|
|
const String* ip = asString(peerDict->get(IP));
|
|
|
|
const Integer* port = asInteger(peerDict->get(PORT));
|
|
|
|
if(!ip || !port || !(0 < port->i() && port->i() < 65536)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*dest_ = SharedHandle<Peer>(new Peer(ip->s(), port->i()));
|
|
|
|
++dest_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visit(const Dict& v) {}
|
|
|
|
};
|
|
|
|
if(peerData) {
|
2010-08-03 11:44:24 +00:00
|
|
|
PeerListValueBaseVisitor visitor(dest, family);
|
2010-06-20 12:12:18 +00:00
|
|
|
peerData->accept(visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OutputIterator>
|
2010-08-03 11:44:24 +00:00
|
|
|
void extractPeer
|
|
|
|
(const SharedHandle<ValueBase>& peerData, int family, OutputIterator dest)
|
2010-06-20 12:12:18 +00:00
|
|
|
{
|
2010-08-03 11:44:24 +00:00
|
|
|
return extractPeer(peerData.get(), family, dest);
|
2010-06-20 12:12:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-07 14:15:21 +00:00
|
|
|
int getCompactLength(int family);
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
} // namespace bittorrent
|
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_BITTORRENT_HELPER_H
|