2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE
|
|
|
|
#define TORRENT_TORRENT_INFO_HPP_INCLUDE
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2004-01-25 19:18:36 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2004-01-19 20:36:55 +01:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2004-03-05 13:04:47 +01:00
|
|
|
#include <boost/date_time/gregorian/gregorian_types.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/filesystem/path.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"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
2004-01-19 20:36:55 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-11-20 20:58:29 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2004-01-16 00:57:11 +01:00
|
|
|
struct file_entry
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-03-05 13:04:47 +01:00
|
|
|
boost::filesystem::path path;
|
2004-01-19 20:36:55 +01:00
|
|
|
size_type size;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct announce_entry
|
|
|
|
{
|
|
|
|
std::string url;
|
|
|
|
int tier;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct invalid_torrent_file: std::exception
|
|
|
|
{
|
|
|
|
virtual const char* what() const throw() { return "invalid torrent file"; }
|
|
|
|
};
|
|
|
|
|
2003-11-26 15:54:56 +01:00
|
|
|
// TODO: add a check to see if filenames are accepted on the
|
|
|
|
// current platform.
|
2004-01-26 11:29:00 +01:00
|
|
|
// also add a filename converter function that will transform
|
2003-11-26 15:54:56 +01:00
|
|
|
// invalid filenames to valid filenames on the current platform
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
class torrent_info
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2004-03-03 14:47:12 +01:00
|
|
|
torrent_info(const entry& torrent_file);
|
2004-09-10 02:47:30 +02:00
|
|
|
torrent_info(
|
|
|
|
int piece_size
|
|
|
|
, const char* name
|
|
|
|
, sha1_hash const& info_hash = sha1_hash(0));
|
2004-03-03 14:47:12 +01:00
|
|
|
|
2004-03-23 23:58:18 +01:00
|
|
|
entry create_torrent() const;
|
2004-07-24 13:54:17 +02:00
|
|
|
entry create_info_metadata() const;
|
2004-03-23 23:58:18 +01:00
|
|
|
void set_comment(char const* str);
|
|
|
|
void set_creator(char const* str);
|
2004-03-05 13:04:47 +01:00
|
|
|
void set_hash(int index, const sha1_hash& h);
|
|
|
|
void add_tracker(std::string const& url, int tier = 0);
|
|
|
|
void add_file(boost::filesystem::path file, size_type size);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-16 00:57:11 +01:00
|
|
|
typedef std::vector<file_entry>::const_iterator file_iterator;
|
|
|
|
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// list the files in the torrent file
|
|
|
|
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(); }
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
int num_files() const
|
|
|
|
{ assert(m_piece_length > 0); return (int)m_files.size(); }
|
|
|
|
const file_entry& file_at(int index) const
|
|
|
|
{ assert(index >= 0 && index < (int)m_files.size()); return m_files[index]; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
const std::vector<announce_entry>& trackers() const { return m_urls; }
|
|
|
|
|
|
|
|
// this will move the tracker with the given index
|
|
|
|
// to a prioritized position in the list (move it towards
|
|
|
|
// the begining) and return the new index to the tracker.
|
|
|
|
int prioritize_tracker(int index);
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
size_type total_size() const { assert(m_piece_length > 0); return m_total_size; }
|
|
|
|
size_type piece_length() const { assert(m_piece_length > 0); return m_piece_length; }
|
|
|
|
int num_pieces() const { assert(m_piece_length > 0); return (int)m_piece_hash.size(); }
|
2003-10-23 01:00:57 +02:00
|
|
|
const sha1_hash& info_hash() const { return m_info_hash; }
|
2004-06-14 01:30:42 +02:00
|
|
|
const std::string& name() const { assert(m_piece_length > 0); return m_name; }
|
2003-10-23 01:00:57 +02:00
|
|
|
void print(std::ostream& os) const;
|
2004-06-14 01:30:42 +02:00
|
|
|
bool is_valid() const { return m_piece_length > 0; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
void convert_file_names();
|
2004-03-07 21:50:56 +01:00
|
|
|
|
|
|
|
size_type piece_size(int index) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-24 20:19:17 +01:00
|
|
|
const sha1_hash& hash_for_piece(int index) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
assert(index >= 0);
|
|
|
|
assert(index < (int)m_piece_hash.size());
|
2003-10-23 01:00:57 +02:00
|
|
|
return m_piece_hash[index];
|
|
|
|
}
|
|
|
|
|
2004-03-05 13:04:47 +01:00
|
|
|
boost::optional<boost::posix_time::ptime>
|
|
|
|
creation_date() const;
|
2003-11-20 20:58:29 +01:00
|
|
|
|
|
|
|
const std::string& comment() const
|
|
|
|
{ return m_comment; }
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
void parse_info_section(entry const& e);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
void read_torrent_info(const entry& libtorrent);
|
|
|
|
|
|
|
|
// the urls to the trackers
|
|
|
|
std::vector<announce_entry> m_urls;
|
|
|
|
|
|
|
|
// the length of one piece
|
2004-06-14 01:30:42 +02:00
|
|
|
// if this is 0, the torrent_info is
|
|
|
|
// in an uninitialized state
|
2004-03-07 21:50:56 +01:00
|
|
|
size_type m_piece_length;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the sha-1 hashes of each piece
|
|
|
|
std::vector<sha1_hash> m_piece_hash;
|
|
|
|
|
|
|
|
// the list of files that this torrent consists of
|
2004-01-16 00:57:11 +01:00
|
|
|
std::vector<file_entry> m_files;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the sum of all filesizes
|
2004-01-16 04:58:24 +01:00
|
|
|
size_type m_total_size;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the hash that identifies this torrent
|
|
|
|
sha1_hash m_info_hash;
|
2003-10-28 02:20:50 +01:00
|
|
|
|
|
|
|
std::string m_name;
|
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
|
|
|
|
boost::posix_time::ptime m_creation_date;
|
|
|
|
|
|
|
|
// 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;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_TORRENT_INFO_HPP_INCLUDED
|
2003-10-24 13:11:28 +02:00
|
|
|
|