libtorrent

sourceforge page mailing list

libtorrent is a C++ library that aims to be a good alternative to all the other bittorrent implementations around. It is a library and not a full featured client, although it comes with a working example client.

The main goals of libtorrent are:

libtorrent is not finished. It is an ongoing project (including this documentation). The current state includes the following features:

libtorrent is portable at least among windows, macosx, and UNIX-systems. It uses boost.thread, boost.filesystem and various other boost libraries and zlib.

building

To build libtorrent you need boost and bjam installed. Then you can use bjam to build libtorrent.

To make bjam work, you need to set the environment variable BOOST_ROOT to the path where boost is installed (e.g. c:\boost_1_30_2 on windows). Then you can just run bjam in the libtorrent directory.

TODO: more detailed build instructions.

using

The interface of libtorrent consists of a few classes. The main class is the session, it contains the main loop that serves all torrents.

session

The session class has the following synopsis:

class session: public boost::noncopyable
{
	session(int listen_port);

	torrent_handle add_torrent(const torrent_info& t, const std::string& save_path);

	void set_http_settings(const http_settings& settings);
};

Once it's created, it will spawn the main thread that will do all the work. The main thread will be idle as long it doesn't have any torrents to participate in. You add torrents through the add_torrent()-function where you give an object representing the information found in the torrent file and the path where you want to save the files. The save_path will be prepended to the directory- structure in the torrent-file.

How to parse a torrent file and create a torrent_info object is described below.

The torrent_handle returned by add_torrent can be used to retrieve information about the torrent's progress, its peers etc. It is also used to abort a torrent.

The constructor takes a listen port as argument, if the given port is busy it will increase the port number by one and try again. If it still fails it will continue increasing the port number until it succeeds or has failed 9 ports. This will change in the future to give more control of the listen-port.

parsing torrent files

The torrent files are bencoded. There are two functions in libtorrent that can encode and decode bencoded data. They are:

template<class InIt> entry bdecode(InIt start, InIt end);
template<class OutIt> void bencode(OutIt out, const entry& e);

The entry class is the internal representation of the bencoded data and it can be used to retreive information, an entry can also be build by the program and given to bencode() to encode it into the OutIt iterator.

The OutIt and InIt are iterators (InputIterator and OutputIterator respectively). They are templates and are usually instantiated as std::ostream_iterator, std::back_insert_iterator or std::istream_iterator. These functions will assume that the iterator refers to a character (char). So, if you want to encode entry e into a buffer in memory, you can do it like this:

std::vector<char> buffer; bencode(std::back_insert_iterator<std::vector<char> >(buf), e);

If you want to decode a torrent file from a buffer in memory, you can do it like this:

std::vector<char> buffer; // ... entry e = bdecode(buf.begin(), buf.end());

Or, if you have a raw char buffer:

const char* buf; // ... entry e = bdecode(buf, buf + data_size);

Now we just need to know how to retrieve information from the entry.

entry

The entry class represents one node in a bencoded hierarchy. It works as a variant type, it can be either a list, a dictionary (std::map), an integer or a string. This is its synopsis:

class entry
{
public:

	typedef std::map<std::string, entry> dictionary_type;
	typedef std::string string_type;
	typedef std::vector<entry> list_type;
	typedef implementation-defined integer_type;

	enum data_type
	{
		int_t,
		string_t,
		list_t,
		dictionary_t,
		undefined_t
	};

	data_type type() const;

	entry();
	entry(data_type t);
	entry(const entry& e);

	void operator=(const entry& e);

	integer_type& integer()
	const integer_type& integer() const;
	string_type& string();
	const string_type& string() const;
	list_type& list();
	const list_type& list() const;
	dictionary_type& dict();
	const dictionary_type& dict() const;

	void print(std::ostream& os, int indent) const;
};

The integer(), string(), list() and dict() functions are accessorts that return the respecive type. If the entry object isn't of the type you request, the accessor will throw type_error (which derives from std::runtime_error). You can ask an entry for its type through the type() function.

The print() function is there for debug purposes only.

If you want to create an entry you give it the type you want it to have in its constructor, and then use one of the non-const accessors to get a reference which you then can assign the value you want it to have.

The typical code to get info from a torrent file will then look like this:

entry torrent_file; // ... const entry::dictionary_type& dict = torrent_file.dict(); entry::dictionary_type::const_iterator i; i = dict.find("announce"); if (i != dict.end()) { std::string tracker_url= i->second.string(); std::cout << tracker_url << "\n"; }

To make it easier to extract information from a torren file, the class torrent_info exists.

torrent_info

The torrent_info has the following synopsis:

class torrent_info
{
public:

	torrent_info(const entry& torrent_file)

	typedef std::vector<file>::const_iterator file_iterator;
	typedef std::vector<file>::const_reverse_iterator reverse_file_iterator;

	file_iterator begin_files() const;
	file_iterator end_files() const;
	reverse_file_iterator rbegin_files() const;
	reverse_file_iterator rend_files() const;

	std::size_t num_files() const;
	const file& file_at(int index) const;

	const std::vector<announce_entry>& trackers() const;

	int prioritize_tracker(int index);

	entry::integer_type total_size() const;
	entry::integer_type piece_length() const;
	std::size_t num_pieces() const;
	const sha1_hash& info_hash() const;

	void print(std::ostream& os) const;

	entry::integer_type piece_size(unsigned int index) const;
	const sha1_hash& hash_for_piece(unsigned int index) const;
};

This class will need some explanation. First of all, to get a list of all files in the torrent, you can use begin_files(), end_files(), rbegin_files() and rend_files(). These will give you standard vector iterators with the type file.

struct file
{
	std::string path;
	std::string filename;
	entry::integer_type size;
};

If you need index-access to files you can use the num_files() and file_at() to access files using indices.

The print() function is there for debug purposes only. It will print the info from the torrent file to the given outstream.

The trackers() function will return a sorted vector of announce_entry. Each announce entry contains a string, which is the tracker url, and a tier index. The tier index is the high-level priority. No matter which trackers that works or not, the ones with lower tier will always be tried before the one with higher tier number.

struct announce_entry
{
	std::string url;
	int tier;
};

The prioritize_tracker() is used internally to move a tracker to the front of its tier group. i.e. It will never be moved pass a tracker with a different tier number. For more information about how multiple trackers are dealt with, see the specification.

total_size(), piece_length() and num_pieces() returns the total number of bytes the torrent-file represents (all the files in it), the number of byte for each piece and the total number of pieces, respectively. The difference between piece_size() and piece_length() is that piece_size() takes the piece index as argument and gives you the exact size of that piece. It will always be the same as piece_length() except in the case of the last piece, which may be smaller.

hash_for_piece() takes a piece-index and returns the 20-bytes sha1-hash for that piece and info_hash() returns the 20-bytes sha1-hash for the info-section of the torrent file. For more information on the sha1_hash, see the big_number class.

torrent_hande

You will usually have to store your torrent_handles somewhere, since it's the object thought which you retrieve infromation about the torrent and aborts the torrent. Its declaration looks like this:

struct torrent_handle
{
	torrent_handle();

	float progress() const;
	void get_peer_info(std::vector<peer_info>& v);
	void abort();

	enum state_t
	{
		checking_files,
		connecting_to_tracker,
		downloading,
		seeding
	};
	state_t state() const;
};

progress() and state()is not implemented yet.

progress() will return a value in the range [0, 1], that represents the progress of the torrent's current task. It may be checking files, connecting to tracker, or downloading etc. You can get the torrent's current task bu calling state(), it will return one of the following:

checking_files The torrent has not started its download yet, and is currently checking existing files or is queued for having its files checked.
connecting_to_tracker The torrent is waiting for tracker reply or waiting to retry a tracker connection. If it's waiting to retry the progress meter will hint about when it will retry.
downloading The torrent is being downloaded. This is the state most torrents will be in most of the time. The progress meter will tell how much of the files that has been downloaded.
seeding In this state the torrent has finished downloading and is a pure seeder.

abort() will close all peer connections associated with this torrent and tell the tracker that we've stopped participating in the swarm. This handle will become invalid shortly after this call has been made.

get_peer_info() takes a reference to a vector that will be cleared and filled with one entry for each peer connected to this torrent. Each entry contains information about that particular peer. It contains the following information:

struct peer_info
{
	enum
	{
		interesting = 0x1,
		choked = 0x2,
		remote_interested = 0x4,
		remote_choked = 0x8
	};
	unsigned int flags;
	address ip;
	float up_speed;
	float down_speed;
	peer_id id;
	std::vector<bool> pieces;
};

The flags attribute tells you in which state the peer is. It is set to any combination of the four enums above. Where interesting means that we are interested in pieces from this peer. choked means that we has choked this peer. remote_interested and remote_choked means the same thing but that the peer is interested in pieces from us and the peer has choked us.

The ip field is the IP-address to this peer. Its type is a wrapper around the actual address and the port number. See address class.

up_speed and down_speed is the current upload and download speed we have to and from this peer. These figures are updated aproximately once every second.

id is the peer's id as used in the bit torrent protocol. This id can be used to extract 'fingerprints' from the peer. Sometimes it can tell you which client the peer is using.

pieces is a vector of booleans that has as many entries as there are pieces in the torrent. Each boolean tells you if the peer has that piece (if it's set to true) or if the peer miss that piece (set to false).

http_settings

You have some control over tracker requests through the http_settings object. You create it and fill it with your settings and the use session::set_http_settings() to apply them. You have control over proxy and authorization settings and also the user-agent that will be sent to the tracker. The user-agent is a good way to identify your client.

struct http_settings
{
	http_settings();
	std::string proxy_ip;
	int proxy_port;
	std::string proxy_login;
	std::string proxy_password;
	std::string user_agent;
};

TODO: address

big_number

Both the peer_id and sha1_hash types are typedefs of the class big_number. It represents 20 bytes of data. Its synopsis follows:

class big_number
{
public:
	bool operator==(const big_number& n) const;
	bool operator!=(const big_number& n) const;
	bool operator<(const big_number& n) const;

	const unsigned char* begin() const;
	const unsigned char* end() const;

	unsigned char* begin();
	unsigned char* end();
};

The iterators gives you access to individual bytes.

Credits

Copyright © 2003 Arvid Norberg

SourceForge Valid HTML 4.01! Valid CSS!