2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
Copyright (c) 2003-2008, Arvid Norberg
|
2003-10-23 01:00:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDED
|
|
|
|
#define TORRENT_TORRENT_INFO_HPP_INCLUDED
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2007-08-10 18:57:53 +02:00
|
|
|
#include <iosfwd>
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2004-03-05 13:04:47 +01:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
2008-05-14 07:29:42 +02:00
|
|
|
#include <boost/shared_array.hpp>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/lazy_entry.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
2004-01-19 20:36:55 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2007-09-01 05:00:31 +02:00
|
|
|
#include "libtorrent/intrusive_ptr_base.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-05-28 10:44:40 +02:00
|
|
|
#include "libtorrent/file_storage.hpp"
|
2003-11-20 20:58:29 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
namespace pt = boost::posix_time;
|
|
|
|
namespace gr = boost::gregorian;
|
2008-07-01 13:00:00 +02:00
|
|
|
namespace fs = boost::filesystem;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// wait 60 seconds before retrying a failed tracker
|
|
|
|
tracker_retry_delay_min = 10
|
|
|
|
// when tracker_failed_max trackers
|
|
|
|
// has failed, wait 60 minutes instead
|
|
|
|
, tracker_retry_delay_max = 60 * 60
|
|
|
|
};
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT announce_entry
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-11-27 21:51:59 +01:00
|
|
|
announce_entry(std::string const& u)
|
2008-11-29 09:38:40 +01:00
|
|
|
: url(u)
|
|
|
|
, tier(0)
|
|
|
|
, fail_limit(3)
|
|
|
|
, fails(0)
|
|
|
|
, source(0)
|
2008-11-27 21:51:59 +01:00
|
|
|
, verified(false)
|
2008-11-29 09:38:40 +01:00
|
|
|
, updating(false)
|
|
|
|
, start_sent(false)
|
|
|
|
, complete_sent(false)
|
2008-11-27 21:51:59 +01:00
|
|
|
{}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
std::string url;
|
2008-11-29 09:38:40 +01:00
|
|
|
|
|
|
|
// the time of next tracker announce
|
|
|
|
ptime next_announce;
|
|
|
|
|
2008-11-27 21:51:59 +01:00
|
|
|
boost::uint8_t tier;
|
|
|
|
// the number of times this tracker can fail
|
|
|
|
// in a row before it's removed. 0 means unlimited
|
|
|
|
boost::uint8_t fail_limit;
|
|
|
|
|
|
|
|
// the number of times in a row this tracker has failed
|
|
|
|
boost::uint8_t fails;
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
enum tracker_source
|
|
|
|
{
|
|
|
|
source_torrent = 1,
|
|
|
|
source_client = 2,
|
|
|
|
source_magnet_link = 4,
|
|
|
|
source_tex = 8
|
|
|
|
};
|
|
|
|
// where did we get this tracker from
|
|
|
|
boost::uint8_t source;
|
|
|
|
|
2008-11-27 21:51:59 +01:00
|
|
|
// is set to true if we have ever received a response from
|
|
|
|
// this tracker
|
|
|
|
bool verified:1;
|
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
// true if we're currently trying to announce with
|
|
|
|
// this tracker
|
|
|
|
bool updating:1;
|
|
|
|
|
|
|
|
// this is true if event start has been sent to the tracker
|
|
|
|
bool start_sent:1;
|
|
|
|
|
|
|
|
// this is true if event completed has been sent to the tracker
|
|
|
|
bool complete_sent:1;
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
start_sent = false;
|
|
|
|
next_announce = min_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
void failed()
|
|
|
|
{
|
|
|
|
++fails;
|
|
|
|
int delay = (std::min)(tracker_retry_delay_min + int(fails) * int(fails) * tracker_retry_delay_min
|
|
|
|
, int(tracker_retry_delay_max));
|
|
|
|
next_announce = time_now() + seconds(delay);
|
|
|
|
updating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_announce(ptime now) const
|
|
|
|
{
|
|
|
|
return now >= next_announce
|
|
|
|
&& (fails < fail_limit || fail_limit == 0)
|
|
|
|
&& !updating;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_working() const
|
|
|
|
{
|
|
|
|
return fails == 0;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT invalid_torrent_file: std::exception
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
virtual const char* what() const throw() { return "invalid torrent file"; }
|
|
|
|
};
|
2008-05-28 10:44:40 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-07-01 13:00:00 +02:00
|
|
|
int TORRENT_EXPORT load_file(fs::path const& filename, std::vector<char>& v);
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
class TORRENT_EXPORT torrent_info : public intrusive_ptr_base<torrent_info>
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-10-10 02:42:48 +02:00
|
|
|
torrent_info(sha1_hash const& info_hash);
|
2008-05-14 07:29:42 +02:00
|
|
|
torrent_info(lazy_entry const& torrent_file);
|
|
|
|
torrent_info(char const* buffer, int size);
|
2008-07-01 13:00:00 +02:00
|
|
|
torrent_info(fs::path const& filename);
|
2008-12-01 08:52:59 +01:00
|
|
|
torrent_info(fs::wpath const& filename);
|
2004-10-29 15:21:09 +02:00
|
|
|
~torrent_info();
|
2004-03-03 14:47:12 +01:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage const& files() const { return m_files; }
|
2008-12-24 21:07:34 +01:00
|
|
|
file_storage const& orig_files() const { return m_orig_files ? *m_orig_files : m_files; }
|
|
|
|
|
|
|
|
void rename_file(int index, std::string const& new_filename)
|
|
|
|
{
|
|
|
|
copy_on_write();
|
|
|
|
m_files.rename_file(index, new_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rename_file(int index, std::wstring const& new_filename)
|
|
|
|
{
|
|
|
|
copy_on_write();
|
|
|
|
m_files.rename_file(index, new_filename);
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
void add_tracker(std::string const& url, int tier = 0);
|
|
|
|
std::vector<announce_entry> const& trackers() const { return m_urls; }
|
2007-08-25 20:26:43 +02:00
|
|
|
|
2007-05-10 00:54:26 +02:00
|
|
|
std::vector<std::string> const& url_seeds() const
|
2008-05-14 07:29:42 +02:00
|
|
|
{ return m_url_seeds; }
|
2008-05-15 09:18:31 +02:00
|
|
|
void add_url_seed(std::string const& url)
|
2008-05-14 07:29:42 +02:00
|
|
|
{ m_url_seeds.push_back(url); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-12-30 04:54:07 +01:00
|
|
|
std::vector<std::string> const& http_seeds() const
|
|
|
|
{ return m_http_seeds; }
|
|
|
|
void add_http_seed(std::string const& url)
|
|
|
|
{ m_http_seeds.push_back(url); }
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
size_type total_size() const { return m_files.total_size(); }
|
|
|
|
int piece_length() const { return m_files.piece_length(); }
|
|
|
|
int num_pieces() const { return m_files.num_pieces(); }
|
2003-10-23 01:00:57 +02:00
|
|
|
const sha1_hash& info_hash() const { return m_info_hash; }
|
2008-05-28 10:44:40 +02:00
|
|
|
const std::string& name() const { return m_files.name(); }
|
|
|
|
|
|
|
|
typedef file_storage::iterator file_iterator;
|
|
|
|
typedef file_storage::reverse_iterator reverse_file_iterator;
|
|
|
|
|
|
|
|
file_iterator begin_files() const { return m_files.begin(); }
|
|
|
|
file_iterator end_files() const { return m_files.end(); }
|
|
|
|
reverse_file_iterator rbegin_files() const { return m_files.rbegin(); }
|
|
|
|
reverse_file_iterator rend_files() const { return m_files.rend(); }
|
|
|
|
int num_files() const { return m_files.num_files(); }
|
|
|
|
file_entry const& file_at(int index) const { return m_files.at(index); }
|
|
|
|
|
2008-07-09 12:45:07 +02:00
|
|
|
file_iterator file_at_offset(size_type offset) const
|
|
|
|
{ return m_files.file_at_offset(offset); }
|
2008-05-28 10:44:40 +02:00
|
|
|
std::vector<file_slice> map_block(int piece, size_type offset, int size) const
|
|
|
|
{ return m_files.map_block(piece, offset, size); }
|
|
|
|
peer_request map_file(int file, size_type offset, int size) const
|
|
|
|
{ return m_files.map_file(file, offset, size); }
|
|
|
|
|
2008-08-03 17:14:08 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2007-04-10 11:11:32 +02:00
|
|
|
// ------- start deprecation -------
|
2008-05-14 07:29:42 +02:00
|
|
|
// these functions will be removed in a future version
|
|
|
|
torrent_info(entry const& torrent_file) TORRENT_DEPRECATED;
|
2007-08-10 18:57:53 +02:00
|
|
|
void print(std::ostream& os) const TORRENT_DEPRECATED;
|
2007-04-10 11:11:32 +02:00
|
|
|
// ------- end deprecation -------
|
2008-08-03 17:14:08 +02:00
|
|
|
#endif
|
2007-04-10 11:11:32 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
bool is_valid() const { return m_files.is_valid(); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
bool priv() const { return m_private; }
|
2004-03-07 21:50:56 +01:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
int piece_size(int index) const { return m_files.piece_size(index); }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
sha1_hash hash_for_piece(int index) const
|
2008-05-28 10:44:40 +02:00
|
|
|
{ return sha1_hash(hash_for_piece_ptr(index)); }
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
char const* hash_for_piece_ptr(int index) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
2008-05-28 10:44:40 +02:00
|
|
|
TORRENT_ASSERT(index < m_files.num_pieces());
|
2008-05-14 07:29:42 +02:00
|
|
|
TORRENT_ASSERT(m_piece_hashes);
|
|
|
|
TORRENT_ASSERT(m_piece_hashes >= m_info_section.get());
|
|
|
|
TORRENT_ASSERT(m_piece_hashes < m_info_section.get() + m_info_section_size);
|
|
|
|
return &m_piece_hashes[index*20];
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
boost::optional<pt::ptime> creation_date() const;
|
2003-11-20 20:58:29 +01:00
|
|
|
|
2005-08-18 00:59:21 +02:00
|
|
|
const std::string& creator() const
|
|
|
|
{ return m_created_by; }
|
|
|
|
|
2003-11-20 20:58:29 +01:00
|
|
|
const std::string& comment() const
|
|
|
|
{ return m_comment; }
|
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
// dht nodes to add to the routing table/bootstrap from
|
|
|
|
typedef std::vector<std::pair<std::string, int> > nodes_t;
|
|
|
|
|
|
|
|
nodes_t const& nodes() const
|
2008-05-15 09:37:34 +02:00
|
|
|
{ return m_nodes; }
|
|
|
|
void add_node(std::pair<std::string, int> const& node)
|
|
|
|
{ m_nodes.push_back(node); }
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
bool parse_info_section(lazy_entry const& e, std::string& error);
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
lazy_entry const* info(char const* key) const
|
|
|
|
{
|
|
|
|
if (m_info_dict.type() == lazy_entry::none_t)
|
|
|
|
lazy_bdecode(m_info_section.get(), m_info_section.get()
|
|
|
|
+ m_info_section_size, m_info_dict);
|
|
|
|
return m_info_dict.dict_find(key);
|
|
|
|
}
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
void swap(torrent_info& ti);
|
2007-03-23 00:37:24 +01:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
boost::shared_array<char> metadata() const
|
|
|
|
{ return m_info_section; }
|
2007-05-10 00:54:26 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
int metadata_size() const { return m_info_section_size; }
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
2008-12-24 21:07:34 +01:00
|
|
|
void copy_on_write();
|
2008-05-14 07:29:42 +02:00
|
|
|
bool parse_torrent_file(lazy_entry const& libtorrent, std::string& error);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage m_files;
|
|
|
|
|
2008-12-24 21:07:34 +01:00
|
|
|
// if m_files is modified, it is first copied into
|
|
|
|
// m_orig_files so that the original name and
|
|
|
|
// filenames are preserved.
|
|
|
|
boost::shared_ptr<const file_storage> m_orig_files;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the urls to the trackers
|
|
|
|
std::vector<announce_entry> m_urls;
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<std::string> m_url_seeds;
|
2008-12-30 04:54:07 +01:00
|
|
|
std::vector<std::string> m_http_seeds;
|
2006-08-01 17:27:08 +02:00
|
|
|
nodes_t m_nodes;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the hash that identifies this torrent
|
2008-05-14 07:29:42 +02:00
|
|
|
sha1_hash m_info_hash;
|
2003-10-28 02:20:50 +01:00
|
|
|
|
2003-11-20 20:58:29 +01:00
|
|
|
// if a creation date is found in the torrent file
|
|
|
|
// this will be set to that, otherwise it'll be
|
|
|
|
// 1970, Jan 1
|
2007-04-05 00:27:36 +02:00
|
|
|
pt::ptime m_creation_date;
|
2003-11-20 20:58:29 +01:00
|
|
|
|
|
|
|
// if a comment is found in the torrent file
|
|
|
|
// this will be set to that comment
|
|
|
|
std::string m_comment;
|
2004-03-23 23:58:18 +01:00
|
|
|
|
|
|
|
// an optional string naming the software used
|
|
|
|
// to create the torrent file
|
|
|
|
std::string m_created_by;
|
2005-05-12 01:03:12 +02:00
|
|
|
|
|
|
|
// this is used when creating a torrent. If there's
|
|
|
|
// only one file there are cases where it's impossible
|
|
|
|
// to know if it should be written as a multifile torrent
|
|
|
|
// or not. e.g. test/test there's one file and one directory
|
|
|
|
// and they have the same name.
|
|
|
|
bool m_multifile;
|
2006-08-01 17:27:08 +02:00
|
|
|
|
|
|
|
// this is true if the torrent is private. i.e., is should not
|
|
|
|
// be announced on the dht
|
|
|
|
bool m_private;
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// this is a copy of the info section from the torrent.
|
|
|
|
// it use maintained in this flat format in order to
|
|
|
|
// make it available through the metadata extension
|
|
|
|
boost::shared_array<char> m_info_section;
|
|
|
|
int m_info_section_size;
|
2007-05-10 00:54:26 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// this is a pointer into the m_info_section buffer
|
|
|
|
// pointing to the first byte of the first sha-1 hash
|
|
|
|
char const* m_piece_hashes;
|
|
|
|
|
|
|
|
// the info section parsed. points into m_info_section
|
|
|
|
// parsed lazily
|
|
|
|
mutable lazy_entry m_info_dict;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_TORRENT_INFO_HPP_INCLUDED
|
2003-10-24 13:11:28 +02:00
|
|
|
|