<p>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.</p>
<p>The main goals of libtorrent are:</p>
<blockquote>
<ulclass="simple">
<li>to be cpu efficient</li>
<li>to be memory efficient</li>
<li>to be very easy to use</li>
</ul>
</blockquote>
<p>libtorrent is not finished. It is an ongoing project (including this documentation).
The current state includes the following features:</p>
<blockquote>
<ulclass="simple">
<li>multitracker extension support (as <aclass="reference"href="http://home.elp.rr.com/tur/multitracker-spec.txt">described by John Hoffman</a>)</li>
<li>serves multiple torrents on a single port and a single thread</li>
<li>supports http proxies and proxy authentication</li>
<li>gzipped tracker-responses</li>
<li>piece picking on block-level like in <aclass="reference"href="http://azureus.sourceforge.net">Azureus</a> (as opposed to piece-level).</li>
<li>queues torrents for file check, instead of checking all of them in parallel.</li>
<li>uses separate threads for checking files and for main downloader, with a fool-proof
thread-safe library interface. (i.e. There's no way for the user to cause a deadlock).</li>
<li>can limit the upload and download bandwidth usage and the maximum number of unchoked peers</li>
<li>piece-wise, unordered, file allocation</li>
<li>implements fair trade. User settable trade-ratio, must at least be 1:1,
but one can choose to trade 1 for 2 or any other ratio that isn't unfair to the other
party. (i.e. real tit for tat)</li>
<li>fast resume support, a way to get rid of the costly piece check at the start
of a resumed torrent. Saves the storage state, piece_picker state as well as all local
peers in a separate fast-resume file.</li>
<li>supports the extension protocol <aclass="reference"href="http://nolar.com/azureus/extended.htm">described by Nolar</a>. See <aclass="reference"href="#extensions">extensions</a>.</li>
<li>supports files > 2 gigabytes.</li>
<li>supports the <ttclass="docutils literal"><spanclass="pre">no_peer_id=1</span></tt> extension that will ease the load off trackers.</li>
<li>supports the <aclass="reference"href="udp_tracker_protocol.html">udp-tracker protocol</a> by Olaf van der Spek.</li>
<li>possibility to limit the number of connections.</li>
<li>delays have messages if there's no other outgoing traffic to the peer, and doesn't
send have messages to peers that already has the piece. This saves bandwidth.</li>
<li>does not have any requirements on the piece order in a torrent that it resumes. This
means it can resume a torrent downloaded by any client.</li>
<li>adjusts the length of the request queue depending on download rate.</li>
<li>supports the <ttclass="docutils literal"><spanclass="pre">compact=1</span></tt> tracker parameter.</li>
<li>GCC 2.95.4 (<ttclass="docutils literal"><spanclass="pre">std::ios_base</span></tt> is missing)</li>
<li>msvc6 sp5</li>
</ul>
</blockquote>
<p>libtorrent is released under the <aclass="reference"href="http://www.opensource.org/licenses/bsd-license.php">BSD-license</a>.</p>
</div>
<divclass="section"id="downloading-and-building">
<h1><aname="downloading-and-building">downloading and building</a></h1>
<p>To acquire the latest version of libtorrent, you'll have to grab it from CVS. You'll find instructions
on how to do this <aclass="reference"href="http://sourceforge.net/cvs/?group_id=79942">here</a> (see Anonymous CVS access).</p>
<p>The easiest way to build libtorrent is probably to use <aclass="reference"href="http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=80982&release_id=278763">boost-build</a>. Make sure you install it
correctly by setting the environment variable <ttclass="docutils literal"><spanclass="pre">BOOST_BUILD_PATH</span></tt> to point to your boost build
installation. Also you have to modify the <ttclass="docutils literal"><spanclass="pre">user_config.jam</span></tt> to reflect the toolsets you have installed.
(if you're building with gcc, uncomment the line "using gcc ;")</p>
<p>You also need to install <aclass="reference"href="http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041&release_id=284047">boost</a> (at least version 1.31.0).</p>
<p>Before you invoke <ttclass="docutils literal"><spanclass="pre">bjam</span></tt> you have to set the environment variable <ttclass="docutils literal"><spanclass="pre">BOOST_ROOT</span></tt> to the
path where you installed boost. This will be used to build and link against the required
boost libraries as well as be used as include path for boost headers.</p>
<p>To build you just have to run:</p>
<preclass="literal-block">
bjam <toolset> link=static
</pre>
<p>in the libtorrent directory.</p>
<p>If you're building on a platform where dlls share the same heap, you can build libtorrent
as a dll too, by typing <ttclass="docutils literal"><spanclass="pre">link=shared</span></tt> instead of <ttclass="docutils literal"><spanclass="pre">link=static</span></tt>.</p>
<p>To build on MacOS X, you need the latest version of boost-build, from the <aclass="reference"href="http://sourceforge.net/cvs/?group_id=7586">boost cvs</a>.</p>
<p>If you're making your own project file, note that there are two versions of the file
abstraction. There's one <ttclass="docutils literal"><spanclass="pre">file_win.cpp</span></tt> which relies on windows file API that supports
files larger than 2 Gigabytes. This does not work in vc6 for some reason, possibly because
it may require windows NT and above. The other file, <ttclass="docutils literal"><spanclass="pre">file.cpp</span></tt> is the default
implementation that simply relies on the standard low level io routines (read, write etc.),
this is the preferred implementation that should be used in all cases. The <ttclass="docutils literal"><spanclass="pre">file_win.cpp</span></tt>
have had some problems with failing seeks (I don't know why), so I advise everyone to use
the other file.</p>
<divclass="section"id="cygwin-and-msvc">
<h2><aname="cygwin-and-msvc">cygwin and msvc</a></h2>
<p>Note that if you're building on windows using the <ttclass="docutils literal"><spanclass="pre">msvc</span></tt> toolset, you cannot run it
from a cygwin terminal, you'll have to run it from a <ttclass="docutils literal"><spanclass="pre">cmd</span></tt> terminal. The same goes for
cygwin, if you're building with gcc (mingw) you'll have to run it from a cygwin terminal.
Also, make sure the paths are correct in the different environments. In cygwin, the paths
(<ttclass="docutils literal"><spanclass="pre">BOOST_BUILD_PATH</span></tt> and <ttclass="docutils literal"><spanclass="pre">BOOST_ROOT</span></tt>) should be in the typical unix-format (e.g.
<ttclass="docutils literal"><spanclass="pre">/cygdrive/c/boost_1_32_0</span></tt>). In the windows environment, they should have the typical
windows format (<ttclass="docutils literal"><spanclass="pre">c:/boost_1_32_0</span></tt>).</p>
<p>If you're building in developer studio, you may have to set the compiler options
"force conformance in for loop scope", "treat wchar_t as built-in type" and
"Enable Run-Time Type Info" to Yes.</p>
</div>
<divclass="section"id="release-and-debug-builds">
<h2><aname="release-and-debug-builds">release and debug builds</a></h2>
<p>The <ttclass="docutils literal"><spanclass="pre">Jamfile</span></tt> can build both a release and debug version of libtorrent. In debug mode,
libtorrent will have pretty expensive invariant checks and asserts built into it. If you
want to disable such checks (you want to do that in a release build) you can see the
table below for which defines you can use to control the build. The <ttclass="docutils literal"><spanclass="pre">Jamfile</span></tt> will define
<p>You add torrents through the <ttclass="docutils literal"><spanclass="pre">add_torrent()</span></tt> 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 <ttclass="docutils literal"><spanclass="pre">save_path</span></tt> will be prepended to the directory
structure in the torrent-file.</p>
<p>If the torrent you are trying to add already exists in the session (is either queued
for checking, being checked or downloading) <ttclass="docutils literal"><spanclass="pre">add_torrent()</span></tt> will throw
<aclass="reference"href="#duplicate-torrent">duplicate_torrent</a> which derives from <ttclass="docutils literal"><spanclass="pre">std::exception</span></tt>.</p>
is available. The fast-resume data can be acquired from a running torrent by calling
<ttclass="docutils literal"><spanclass="pre">torrent_handle::write_resume_data()</span></tt>. See <aclass="reference"href="#fast-resume">fast resume</a>.</p>
<p>The <aclass="reference"href="#torrent-handle">torrent_handle</a> returned by <ttclass="docutils literal"><spanclass="pre">add_torrent()</span></tt> can be used to retrieve information
about the torrent's progress, its peers etc. It is also used to abort a torrent.</p>
<p>The second overload that takes a tracker url and an info-hash instead of metadata (<ttclass="docutils literal"><spanclass="pre">entry</span></tt>)
can be used with torrents where (at least some) peers support the metadata extension. For
the overload to be available, libtorrent must be built with extensions enabled
<p>These functions will set a global limit on the number of unchoked peers (uploads)
and the number of connections opened. The number of connections is set to a hard
minimum of at least two connections per torrent, so if you set a too low
connections limit, and open too many torrents, the limit will not be met. The
number of uploads is at least one per torrent.</p>
</div>
<divclass="section"id="status">
<h2><aname="status">status()</a></h2>
<blockquote>
<preclass="literal-block">
session_status status() const;
</pre>
</blockquote>
<p><ttclass="docutils literal"><spanclass="pre">status()</span></tt> returns session wide-statistics and status. The <ttclass="docutils literal"><spanclass="pre">session_status</span></tt>
struct has the following members:</p>
<preclass="literal-block">
struct session_status
{
bool has_incoming_connections;
float upload_rate;
float download_rate;
float payload_upload_rate;
float payload_download_rate;
size_type total_download;
size_type total_upload;
size_type total_payload_download;
size_type total_payload_upload;
int num_peers;
};
</pre>
<p><ttclass="docutils literal"><spanclass="pre">has_incoming_connections</span></tt> is false as long as no incoming connections have been
established on the listening socket. Every time you change the listen port, this will
be reset to false.</p>
<p><ttclass="docutils literal"><spanclass="pre">upload_rate</span></tt>, <ttclass="docutils literal"><spanclass="pre">download_rate</span></tt>, <ttclass="docutils literal"><spanclass="pre">payload_download_rate</span></tt> and <ttclass="docutils literal"><spanclass="pre">payload_upload_rate</span></tt>
are the total download and upload rates accumulated from all torrents. The payload
versions is the payload download only.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_download</span></tt> and <ttclass="docutils literal"><spanclass="pre">total_upload</span></tt> are the total number of bytes downloaded and
uploaded to and from all torrents. <ttclass="docutils literal"><spanclass="pre">total_payload_download</span></tt> and <ttclass="docutils literal"><spanclass="pre">total_payload_upload</span></tt>
are the same thing but where only the payload is considered.</p>
<p><ttclass="docutils literal"><spanclass="pre">num_peers</span></tt> is the total number of peer connections this session have.</p>
<p><ttclass="docutils literal"><spanclass="pre">pop_alert()</span></tt> is used to ask the session if any errors or events has occured. With
<ttclass="docutils literal"><spanclass="pre">set_severity_level()</span></tt> you can filter how serious the event has to be for you to
receive it through <ttclass="docutils literal"><spanclass="pre">pop_alert()</span></tt>. For information, see <aclass="reference"href="#alerts">alerts</a>.</p>
</div>
</div>
<divclass="section"id="entry">
<h1><aname="entry">entry</a></h1>
<p>The <ttclass="docutils literal"><spanclass="pre">entry</span></tt> class represents one node in a bencoded hierarchy. It works as a
variant type, it can be either a list, a dictionary (<ttclass="docutils literal"><spanclass="pre">std::map</span></tt>), an integer
are accessors that return the respecive type. If the <ttclass="docutils literal"><spanclass="pre">entry</span></tt> object isn't of the
type you request, the accessor will throw <aclass="reference"href="#type-error">type_error</a> (which derives from
<ttclass="docutils literal"><spanclass="pre">std::runtime_error</span></tt>). You can ask an <ttclass="docutils literal"><spanclass="pre">entry</span></tt> for its type through the
<p>This class will need some explanation. First of all, to get a list of all files
in the torrent, you can use <ttclass="docutils literal"><spanclass="pre">begin_files()</span></tt>, <ttclass="docutils literal"><spanclass="pre">end_files()</span></tt>,
<ttclass="docutils literal"><spanclass="pre">rbegin_files()</span></tt> and <ttclass="docutils literal"><spanclass="pre">rend_files()</span></tt>. These will give you standard vector
iterators with the type <ttclass="docutils literal"><spanclass="pre">file_entry</span></tt>.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">path</span></tt> is the full (relative) path of each file. i.e. if it is a multi-file
torrent, all the files starts with a directory with the same name as <ttclass="docutils literal"><spanclass="pre">torrent_info::name()</span></tt>.</p>
<p>If you need index-access to files you can use the <ttclass="docutils literal"><spanclass="pre">num_files()</span></tt> and <ttclass="docutils literal"><spanclass="pre">file_at()</span></tt>
to access files using indices.</p>
</div>
<divclass="section"id="print">
<h2><aname="print">print()</a></h2>
<blockquote>
<preclass="literal-block">
void print(std::ostream& os) const;
</pre>
</blockquote>
<p>The <ttclass="docutils literal"><spanclass="pre">print()</span></tt> function is there for debug purposes only. It will print the info from
<p>The <ttclass="docutils literal"><spanclass="pre">trackers()</span></tt> function will return a sorted vector of <ttclass="docutils literal"><spanclass="pre">announce_entry</span></tt>.
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.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_size()</span></tt>, <ttclass="docutils literal"><spanclass="pre">piece_length()</span></tt> and <ttclass="docutils literal"><spanclass="pre">num_pieces()</span></tt> 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
<ttclass="docutils literal"><spanclass="pre">piece_size()</span></tt> and <ttclass="docutils literal"><spanclass="pre">piece_length()</span></tt> is that <ttclass="docutils literal"><spanclass="pre">piece_size()</span></tt> takes
the piece index as argument and gives you the exact size of that piece. It will always
be the same as <ttclass="docutils literal"><spanclass="pre">piece_length()</span></tt> except in the case of the last piece, which may
const sha1_hash& hash_for_piece(unsigned int index) const;
</pre>
</blockquote>
<p><ttclass="docutils literal"><spanclass="pre">hash_for_piece()</span></tt> takes a piece-index and returns the 20-bytes sha1-hash for that
piece and <ttclass="docutils literal"><spanclass="pre">info_hash()</span></tt> returns the 20-bytes sha1-hash for the info-section of the
torrent file. For more information on the <ttclass="docutils literal"><spanclass="pre">sha1_hash</span></tt>, see the <aclass="reference"href="#big-number">big_number</a> class.</p>
<p><ttclass="docutils literal"><spanclass="pre">name()</span></tt> returns the name of the torrent.</p>
<p><ttclass="docutils literal"><spanclass="pre">comment()</span></tt> returns the comment associated with the torrent. If there's no comment,
it will return an empty string. <ttclass="docutils literal"><spanclass="pre">creation_date()</span></tt> returns a <aclass="reference"href="http://www.boost.org/libs/date_time/doc/class_ptime.html">boost::posix_time::ptime</a>
object, representing the time when this torrent file was created. If there's no timestamp
in the torrent file, this will return a date of january 1:st 1970.</p>
<p><ttclass="docutils literal"><spanclass="pre">save_path()</span></tt> returns the path that was given to <aclass="reference"href="#add-torrent">add_torrent()</a> when this torrent
<p><ttclass="docutils literal"><spanclass="pre">set_upload_limit</span></tt> will limit the upload bandwidth used by this particular torrent to the
limit you set. It is given as the number of bytes per second the torrent is allowed to upload.
<ttclass="docutils literal"><spanclass="pre">set_download_limit</span></tt> works the same way but for download bandwidth instead of upload bandwidth.
Note that setting a higher limit on a torrent then the global limit (<ttclass="docutils literal"><spanclass="pre">session::set_upload_rate_limit</span></tt>)
will not override the global rate limit. The torrent can never upload more than the global rate
<p><ttclass="docutils literal"><spanclass="pre">pause()</span></tt>, and <ttclass="docutils literal"><spanclass="pre">resume()</span></tt> will disconnect all peers and reconnect all peers respectively.
When a torrent is paused, it will however remember all share ratios to all peers and remember
all potential (not connected) peers. You can use <ttclass="docutils literal"><spanclass="pre">is_paused()</span></tt> to determine if a torrent
is currently paused. Torrents may be paused automatically if there is a file error (eg. disk full)
or something similar. See <aclass="reference"href="#file-error-alert">file_error_alert</a>.</p>
</div>
<divclass="section"id="is-seed">
<h2><aname="is-seed">is_seed()</a></h2>
<blockquote>
<preclass="literal-block">
bool is_seed() const;
</pre>
</blockquote>
<p>Returns true if the torrent is in seed mode (i.e. if it has finished downloading).</p>
</div>
<divclass="section"id="has-metadata">
<h2><aname="has-metadata">has_metadata()</a></h2>
<blockquote>
<preclass="literal-block">
bool has_metadata() const;
</pre>
</blockquote>
<p>Returns true if this torrent has metadata (either it was started from a .torrent file or the
metadata has been downloaded). The only scenario where this can return false is when the torrent
was started torrent-less (i.e. with just an info-hash and tracker ip). Note that if the torrent
doesn't have metadata, the member <aclass="reference"href="#get-torrent-info">get_torrent_info()</a> will throw.</p>
<p><ttclass="docutils literal"><spanclass="pre">set_tracker_login()</span></tt> sets a username and password that will be sent along in the HTTP-request
of the tracker announce. Set this if the tracker requires authorization.</p>
<p><ttclass="docutils literal"><spanclass="pre">set_max_uploads()</span></tt> sets the maximum number of peers that's unchoked at the same time on this
torrent. If you set this to -1, there will be no limit.</p>
<p><ttclass="docutils literal"><spanclass="pre">set_max_connections()</span></tt> sets the maximum number of connection this torrent will open. If all
connections are used up, incoming connections may be refused or poor connections may be closed.
This must be at least 2. The default is unlimited number of connections. If -1 is given to the
<p><ttclass="docutils literal"><spanclass="pre">write_resume_data()</span></tt> generates fast-resume data and returns it as an <aclass="reference"href="#entry">entry</a>. This <aclass="reference"href="#entry">entry</a>
is suitable for being bencoded. For more information about how fast-resume works, see <aclass="reference"href="#fast-resume">fast resume</a>.</p>
<p>There are three cases where this function will just return an empty <ttclass="docutils literal"><spanclass="pre">entry</span></tt>:</p>
<blockquote>
<olclass="arabic simple">
<li>The torrent handle is invalid.</li>
<li>The torrent is checking (or is queued for checking) its storage, it will obviously
not be ready to write resume data.</li>
<li>The torrent hasn't received valid metadata and was started without metadata
(see libtorrent's <aclass="reference"href="#metadata-from-peers">metadata from peers</a> extension)</li>
</ol>
</blockquote>
<p>Note that by the time this function returns, the resume data may already be invalid if the torrent
is still downloading! The recommended practice is to first pause the torrent, then generate the
<p><ttclass="docutils literal"><spanclass="pre">metadata()</span></tt> will return a reference to a buffer containing the exact info part of the
.torrent file. This buffer will be valid as long as the torrent is still running. When hashed,
it will produce the same hash as the info-hash.</p>
</div>
<divclass="section"id="id11">
<h2><aname="id11">status()</a></h2>
<blockquote>
<preclass="literal-block">
torrent_status status();
</pre>
</blockquote>
<p><ttclass="docutils literal"><spanclass="pre">status()</span></tt> will return a structure with information about the status of this
torrent. If the <aclass="reference"href="#torrent-handle">torrent_handle</a> is invalid, it will throw <aclass="reference"href="#invalid-handle">invalid_handle</a> exception.
See <aclass="reference"href="#torrent-status">torrent_status</a>.</p>
<p><ttclass="docutils literal"><spanclass="pre">piece_index</span></tt> is the index of the piece in question. <ttclass="docutils literal"><spanclass="pre">blocks_in_piece</span></tt> is the
number of blocks in this particular piece. This number will be the same for most pieces, but
the last piece may have fewer blocks than the standard pieces.</p>
<p><ttclass="docutils literal"><spanclass="pre">requested_blocks</span></tt> is a bitset with one bit per block in the piece. If a bit is set, it
means that that block has been requested, but not necessarily fully downloaded yet. To know
from whom the block has been requested, have a look in the <ttclass="docutils literal"><spanclass="pre">peer</span></tt> array. The bit-index
in the <ttclass="docutils literal"><spanclass="pre">requested_blocks</span></tt> and <ttclass="docutils literal"><spanclass="pre">finished_blocks</span></tt> correspons to the array-index into
<ttclass="docutils literal"><spanclass="pre">peers</span></tt> and <ttclass="docutils literal"><spanclass="pre">num_downloads</span></tt>. The array of peers is contains the id of the
peer the piece was requested from. If a piece hasn't been requested (the bit in
<ttclass="docutils literal"><spanclass="pre">requested_blocks</span></tt> is not set) the peer array entry will be undefined.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">finished_blocks</span></tt> is a bitset where each bit says if the block is fully downloaded
or not. And the <ttclass="docutils literal"><spanclass="pre">num_downloads</span></tt> array says how many times that block has been downloaded.
When a piece fails a hash verification, single blocks may be redownloaded to see if the hash teast
<p><ttclass="docutils literal"><spanclass="pre">get_peer_info()</span></tt> takes a reference to a vector that will be cleared and filled
with one entry for each peer connected to this torrent, given the handle is valid. If the
<aclass="reference"href="#torrent-handle">torrent_handle</a> is invalid, it will throw <aclass="reference"href="#invalid-handle">invalid_handle</a> exception. Each entry in
the vector contains information about that particular peer. See <aclass="reference"href="#peer-info">peer_info</a>.</p>
<p>Returns a const reference to the <aclass="reference"href="#torrent-info">torrent_info</a> object associated with this torrent.
This reference is valid as long as the <aclass="reference"href="#torrent-handle">torrent_handle</a> is valid, no longer. If the
<aclass="reference"href="#torrent-handle">torrent_handle</a> is invalid or if it doesn't have any metadata, <aclass="reference"href="#invalid-handle">invalid_handle</a>
exception will be thrown. The torrent may be in a state without metadata only if
it was started without a .torrent file, i.e. by using the libtorrent extension of
just supplying a tracker and info-hash.</p>
</div>
<divclass="section"id="is-valid">
<h2><aname="is-valid">is_valid()</a></h2>
<blockquote>
<preclass="literal-block">
bool is_valid() const;
</pre>
</blockquote>
<p>Returns true if this handle refers to a valid torrent and false if it hasn't been initialized
or if the torrent it refers to has been aborted. Note that a handle may become invalid after
it has been added to the session. Usually this is because the storage for the torrent is
somehow invalid or if the filenames are not allowed (and hence cannot be opened/created) on
your filesystem. If such an error occurs, a <aclass="reference"href="#file-error-alert">file_error_alert</a> is generated and all handles
that refers to that torrent will become invalid.</p>
<p>When downloading, the progress is <ttclass="docutils literal"><spanclass="pre">total_wanted_done</span></tt> / <ttclass="docutils literal"><spanclass="pre">total_wanted</span></tt>.</p>
<p><ttclass="docutils literal"><spanclass="pre">paused</span></tt> is set to true if the torrent is paused and false otherwise.</p>
<p><ttclass="docutils literal"><spanclass="pre">next_announce</span></tt> is the time until the torrent will announce itself to the tracker. And
<ttclass="docutils literal"><spanclass="pre">announce_interval</span></tt> is the time the tracker want us to wait until we announce ourself
again the next time.</p>
<p><ttclass="docutils literal"><spanclass="pre">current_tracker</span></tt> is the URL of the last working tracker. If no tracker request has
been successful yet, it's set to an empty string.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_download</span></tt> and <ttclass="docutils literal"><spanclass="pre">total_upload</span></tt> is the number of bytes downloaded and
uploaded to all peers, accumulated, <em>this session</em> only.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_payload_download</span></tt> and <ttclass="docutils literal"><spanclass="pre">total_payload_upload</span></tt> counts the amount of bytes
send and received this session, but only the actual oayload data (i.e the interesting
data), these counters ignore any protocol overhead.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_failed_bytes</span></tt> is the number of bytes that has been downloaded and that
has failed the piece hash test. In other words, this is just how much crap that
has been downloaded.</p>
<p><ttclass="docutils literal"><spanclass="pre">pieces</span></tt> is the bitmask that represents which pieces we have (set to true) and
the pieces we don't have. It's a pointer and may be set to 0 if the torrent isn't
downloading or seeding.</p>
<p><ttclass="docutils literal"><spanclass="pre">download_rate</span></tt> and <ttclass="docutils literal"><spanclass="pre">upload_rate</span></tt> are the total rates for all peers for this
torrent. These will usually have better precision than summing the rates from
all peers. The rates are given as the number of bytes per second. The
<ttclass="docutils literal"><spanclass="pre">download_payload_rate</span></tt> and <ttclass="docutils literal"><spanclass="pre">upload_payload_rate</span></tt> respectively is the
total transfer rate of payload only, not counting protocol chatter. This might
be slightly smaller than the other rates, but if projected over a long time
(e.g. when calculating ETA:s) the difference may be noticable.</p>
<p><ttclass="docutils literal"><spanclass="pre">num_peers</span></tt> is the number of peers this torrent currently is connected to.</p>
<p><ttclass="docutils literal"><spanclass="pre">num_complete</span></tt> and <ttclass="docutils literal"><spanclass="pre">num_incomplete</span></tt> are set to -1 if the tracker did not
send any scrape data in its announce reply. This data is optional and may
not be available from all trackers. If these are not -1, they are the total
number of peers that are seeding (complete) and the total number of peers
that are still downloading (incomplete) this torrent.</p>
<p><ttclass="docutils literal"><spanclass="pre">up_speed</span></tt> and <ttclass="docutils literal"><spanclass="pre">down_speed</span></tt> contains the current upload and download speed
we have to and from this peer (including any protocol messages). The transfer rates
of payload data only are found in <ttclass="docutils literal"><spanclass="pre">payload_up_speed</span></tt> and <ttclass="docutils literal"><spanclass="pre">payload_down_speed</span></tt>.
These figures are updated aproximately once every second.</p>
<p><ttclass="docutils literal"><spanclass="pre">total_download</span></tt> and <ttclass="docutils literal"><spanclass="pre">total_upload</span></tt> are the total number of bytes downloaded
from and uploaded to this peer. These numbers do not include the protocol chatter, but only
the payload data.</p>
<p><ttclass="docutils literal"><spanclass="pre">id</span></tt> 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. See identify_client()_</p>
<p><ttclass="docutils literal"><spanclass="pre">pieces</span></tt> 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).</p>
<p><ttclass="docutils literal"><spanclass="pre">seed</span></tt> is true if this peer is a seed.</p>
<p><ttclass="docutils literal"><spanclass="pre">upload_limit</span></tt> is the number of bytes per second we are allowed to send to this
peer every second. It may be -1 if there's no limit. The upload limits of all peers
should sum up to the upload limit set by <ttclass="docutils literal"><spanclass="pre">session::set_upload_limit</span></tt>.</p>
<p><ttclass="docutils literal"><spanclass="pre">upload_ceiling</span></tt> is the current maximum allowed upload rate given the cownload
rate and share ratio. If the global upload rate is inlimited, the <ttclass="docutils literal"><spanclass="pre">upload_limit</span></tt>
for every peer will be the same as their <ttclass="docutils literal"><spanclass="pre">upload_ceiling</span></tt>.</p>
<p><ttclass="docutils literal"><spanclass="pre">load_balancing</span></tt> is a measurment of the balancing of free download (that we get)
and free upload that we give. Every peer gets a certain amount of free upload, but
this member says how much <em>extra</em> free upload this peer has got. If it is a negative
number it means that this was a peer from which we have got this amount of free
download.</p>
<p><ttclass="docutils literal"><spanclass="pre">download_queue_length</span></tt> is the number of piece-requests we have sent to this peer
that hasn't been answered with a piece yet.</p>
<p><ttclass="docutils literal"><spanclass="pre">upload_queue_length</span></tt> is the number of piece-requests we have received from this peer
that we haven't answered with a piece yet.</p>
<p>You can know which piece, and which part of that piece, that is currently being
downloaded from a specific peer by looking at the next four members.
<ttclass="docutils literal"><spanclass="pre">downloading_piece_index</span></tt> is the index of the piece that is currently being downloaded.
This may be set to -1 if there's currently no piece downloading from this peer. If it is
>= 0, the other three members are valid. <ttclass="docutils literal"><spanclass="pre">downloading_block_index</span></tt> is the index of the
block (or sub-piece) that is being downloaded. <ttclass="docutils literal"><spanclass="pre">downloading_progress</span></tt> is the number
of bytes of this block we have received from the peer, and <ttclass="docutils literal"><spanclass="pre">downloading_total</span></tt> is
the total number of bytes in this block.</p>
</div>
<divclass="section"id="address">
<h1><aname="address">address</a></h1>
<p>The <ttclass="docutils literal"><spanclass="pre">address</span></tt> class represents a name of a network endpoint (usually referred to as
IP-address) and a port number. This is the same thing as a <ttclass="docutils literal"><spanclass="pre">sockaddr_in</span></tt> would contain.
Its declaration looks like this:</p>
<preclass="literal-block">
class address
{
public:
address();
address(unsigned char a
, unsigned char b
, unsigned char c
, unsigned char d
, unsigned short port);
address(unsigned int addr, unsigned short port);
address(const std::string& addr, unsigned short port);
address(const address& a);
~address();
std::string as_string() const;
unsigned int ip() const;
unsigned short port() const;
bool operator<(const address& a) const;
bool operator!=(const address& a) const;
bool operator==(const address& a) const;
};
</pre>
<p>It is less-than comparable to make it possible to use it as a key in a map. <ttclass="docutils literal"><spanclass="pre">as_string()</span></tt> may block
while it does the DNS lookup, it returns a string that points to the address represented by the object.</p>
<p><ttclass="docutils literal"><spanclass="pre">ip()</span></tt> will return the 32-bit ip-address as an integer. <ttclass="docutils literal"><spanclass="pre">port()</span></tt> returns the port number.</p>
</div>
<divclass="section"id="http-settings">
<h1><aname="http-settings">http_settings</a></h1>
<p>You have some control over tracker requests through the <ttclass="docutils literal"><spanclass="pre">http_settings</span></tt> object. You
create it and fill it with your settings and then use <ttclass="docutils literal"><spanclass="pre">session::set_http_settings()</span></tt>
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.</p>
<preclass="literal-block">
struct http_settings
{
http_settings();
std::string proxy_ip;
int proxy_port;
std::string proxy_login;
std::string proxy_password;
std::string user_agent;
int tracker_timeout;
int tracker_maximum_response_length;
};
</pre>
<p><ttclass="docutils literal"><spanclass="pre">proxy_ip</span></tt> may be a hostname or ip to a http proxy to use. If this is
an empty string, no http proxy will be used.</p>
<p><ttclass="docutils literal"><spanclass="pre">proxy_port</span></tt> is the port on which the http proxy listens. If <ttclass="docutils literal"><spanclass="pre">proxy_ip</span></tt>
is empty, this will be ignored.</p>
<p><ttclass="docutils literal"><spanclass="pre">proxy_login</span></tt> should be the login username for the http proxy, if this
<p><ttclass="docutils literal"><spanclass="pre">proxy_password</span></tt> the password string for the http proxy.</p>
<p><ttclass="docutils literal"><spanclass="pre">user_agent</span></tt> this is the client identification to the tracker. It will
be followed by the string "(libtorrent)" to identify that this library
is being used. This should be set to your client's name and version number.</p>
<p><ttclass="docutils literal"><spanclass="pre">tracker_timeout</span></tt> is the number of seconds the tracker connection will
wait until it considers the tracker to have timed-out. Default value is 10
seconds.</p>
<p><ttclass="docutils literal"><spanclass="pre">tracker_maximum_response_length</span></tt> is the maximum number of bytes in a
tracker response. If a response size passes this number it will be rejected
and the connection will be closed. On gzipped responses this size is measured
on the uncompressed data. So, if you get 20 bytes of gzip response that'll
expand to 2 megs, it will be interrupted before the entire response has been
uncompressed (given your limit is lower than 2 megs). Default limit is
1 megabyte.</p>
</div>
<divclass="section"id="big-number">
<h1><aname="big-number">big_number</a></h1>
<p>Both the <ttclass="docutils literal"><spanclass="pre">peer_id</span></tt> and <ttclass="docutils literal"><spanclass="pre">sha1_hash</span></tt> types are typedefs of the class
<ttclass="docutils literal"><spanclass="pre">big_number</span></tt>. It represents 20 bytes of data. Its synopsis follows:</p>
<preclass="literal-block">
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();
};
</pre>
<p>The iterators gives you access to individual bytes.</p>
</div>
<divclass="section"id="hasher">
<h1><aname="hasher">hasher</a></h1>
<p>This class creates sha1-hashes. Its declaration looks like this:</p>
<preclass="literal-block">
class hasher
{
public:
hasher();
void update(const char* data, unsigned int len);
sha1_hash final();
void reset();
};
</pre>
<p>You use it by first instantiating it, then call <ttclass="docutils literal"><spanclass="pre">update()</span></tt> to feed it
with data. i.e. you don't have to keep the entire buffer of which you want to
create the hash in memory. You can feed the hasher parts of it at a time. When
You have fed the hasher with all the data, you call <ttclass="docutils literal"><spanclass="pre">final()</span></tt> and it
will return the sha1-hash of the data.</p>
<p>If you want to reuse the hasher object once you have created a hash, you have to
call <ttclass="docutils literal"><spanclass="pre">reset()</span></tt> to reinitialize it.</p>
<p>The sha1-algorithm used was implemented by Steve Reid and released as public domain.
For more info, see <ttclass="docutils literal"><spanclass="pre">src/sha1.cpp</span></tt>.</p>
</div>
<divclass="section"id="fingerprint">
<h1><aname="fingerprint">fingerprint</a></h1>
<p>The fingerprint class represents information about a client and its version. It is used
to encode this information into the client's peer id.</p>
<p>This is the class declaration:</p>
<preclass="literal-block">
struct fingerprint
{
fingerprint(const char* id_string, int major, int minor, int revision, int tag);
std::string to_string() const;
char id[2];
char major_version;
char minor_version;
char revision_version;
char tag_version;
};
</pre>
<p>The constructor takes a <ttclass="docutils literal"><spanclass="pre">char</span><spanclass="pre">const*</span></tt> that should point to a string constant containing
exactly two characters. These are the characters that should be unique for your client. Make
sure not to clash with anybody else. Here are some taken id's:</p>
<tableborder="1"class="docutils">
<colgroup>
<colwidth="30%"/>
<colwidth="70%"/>
</colgroup>
<theadvalign="bottom">
<tr><th>id chars</th>
<th>client</th>
</tr>
</thead>
<tbodyvalign="top">
<tr><td>'AZ'</td>
<td>Azureus</td>
</tr>
<tr><td>'LT'</td>
<td>libtorrent (default)</td>
</tr>
<tr><td>'BX'</td>
<td>BittorrentX</td>
</tr>
<tr><td>'MT'</td>
<td>Moonlight Torrent</td>
</tr>
<tr><td>'TS'</td>
<td>Torrent Storm</td>
</tr>
<tr><td>'SS'</td>
<td>Swarm Scope</td>
</tr>
<tr><td>'XT'</td>
<td>Xan Torrent</td>
</tr>
</tbody>
</table>
<p>The <ttclass="docutils literal"><spanclass="pre">major</span></tt>, <ttclass="docutils literal"><spanclass="pre">minor</span></tt>, <ttclass="docutils literal"><spanclass="pre">revision</span></tt> and <ttclass="docutils literal"><spanclass="pre">tag</span></tt> parameters are used to identify the
version of your client. All these numbers must be within the range [0, 9].</p>
<p><ttclass="docutils literal"><spanclass="pre">to_string()</span></tt> will generate the actual string put in the peer-id, and return it.</p>
<p>This function is declared in the header <ttclass="docutils literal"><spanclass="pre"><libtorrent/identify_client.hpp></span></tt>. It can can be used
to extract a string describing a client version from its peer-id. It will recognize most clients
that have this kind of identification in the peer-id.</p>
<p>These functions will encode data to <aclass="reference"href="http://wiki.theory.org/index.php/BitTorrentSpecification">bencoded</a> or decode <aclass="reference"href="http://wiki.theory.org/index.php/BitTorrentSpecification">bencoded</a> data.</p>
<p>The <aclass="reference"href="#entry">entry</a> class is the internal representation of the bencoded data
and it can be used to retreive information, an <aclass="reference"href="#entry">entry</a> can also be build by
the program and given to <ttclass="docutils literal"><spanclass="pre">bencode()</span></tt> to encode it into the <ttclass="docutils literal"><spanclass="pre">OutIt</span></tt>
iterator.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">OutIt</span></tt> and <ttclass="docutils literal"><spanclass="pre">InIt</span></tt> are iterators
(<aclass="reference"href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a> and <aclass="reference"href="http://www.sgi.com/tech/stl/OutputIterator.html">OutputIterator</a> respectively). They
are templates and are usually instantiated as <aclass="reference"href="http://www.sgi.com/tech/stl/ostream_iterator.html">ostream_iterator</a>,
<aclass="reference"href="http://www.sgi.com/tech/stl/back_insert_iterator.html">back_insert_iterator</a> or <aclass="reference"href="http://www.sgi.com/tech/stl/istream_iterator.html">istream_iterator</a>. These
functions will assume that the iterator refers to a character
(<ttclass="docutils literal"><spanclass="pre">char</span></tt>). So, if you want to encode entry <ttclass="docutils literal"><spanclass="pre">e</span></tt> into a buffer
<td>This will include alot of debug events that can be used
both for debugging libtorrent but also when debugging
other clients that are connected to libtorrent. It will
report strange behaviors among the connected peers.</td>
</tr>
</tbody>
</table>
<p>When setting a severity level, you will receive messages of that severity and all
messages that are more sever. If you set <ttclass="docutils literal"><spanclass="pre">alert::none</span></tt> (the default) you will not recieve
any events at all.</p>
<p>When you set a severuty level other than <ttclass="docutils literal"><spanclass="pre">none</span></tt>, you have the responsibility to call
<ttclass="docutils literal"><spanclass="pre">pop_alert()</span></tt> from time to time. If you don't do that, the alert queue will just grow.</p>
<p>When you get an alert, you can use <ttclass="docutils literal"><spanclass="pre">typeid()</span></tt> or <ttclass="docutils literal"><spanclass="pre">dynamic_cast<></span></tt> to get more detailed
information on exactly which type it is. i.e. what kind of error it is. You can also use a
<aclass="reference"href="#dispatcher">dispatcher</a> mechanism that's available in libtorrent.</p>
<p>All alert types are defined in the <ttclass="docutils literal"><spanclass="pre"><libtorrent/alert_types.hpp></span></tt> header file.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">alert</span></tt> class is the base class that specific messages are derived from. This
<p>This alert is generated when a peer is banned because it has sent too many corrupt pieces
to us. It is generated at severity level <ttclass="docutils literal"><spanclass="pre">info</span></tt>. The <ttclass="docutils literal"><spanclass="pre">handle</span></tt> member is a <aclass="reference"href="#torrent-handle">torrent_handle</a>
to the torrent that this peer was a member of.</p>
<ttclass="docutils literal"><spanclass="pre">request</span></tt> is the actual incoming request from the peer. The alert is generated as severity level
<p>The <ttclass="docutils literal"><spanclass="pre">peer_request</span></tt> contains the values the client sent in its <ttclass="docutils literal"><spanclass="pre">request</span></tt> message. <ttclass="docutils literal"><spanclass="pre">piece</span></tt> is
the index of the piece it want data from, <ttclass="docutils literal"><spanclass="pre">start</span></tt> is the offset within the piece where the data
should be read, and <ttclass="docutils literal"><spanclass="pre">length</span></tt> is the amount of data it wants.</p>
<p>This is thrown by <ttclass="docutils literal"><spanclass="pre">bdecode()</span></tt> if the input data is not a valid bencoding.</p>
<preclass="literal-block">
struct invalid_encoding: std::exception
{
const char* what() const throw();
};
</pre>
</div>
<divclass="section"id="type-error">
<h2><aname="type-error">type_error</a></h2>
<p>This is thrown from the accessors of <ttclass="docutils literal"><spanclass="pre">entry</span></tt> if the data type of the <ttclass="docutils literal"><spanclass="pre">entry</span></tt> doesn't
<p>This exception is thrown from the constructor of <ttclass="docutils literal"><spanclass="pre">torrent_info</span></tt> if the given bencoded information
doesn't meet the requirements on what information has to be present in a torrent file.</p>
<preclass="literal-block">
struct invalid_torrent_file: std::exception
{
const char* what() const throw();
};
</pre>
</div>
</div>
<divclass="section"id="examples">
<h1><aname="examples">examples</a></h1>
<divclass="section"id="dump-torrent">
<h2><aname="dump-torrent">dump_torrent</a></h2>
<p>This is an example of a program that will take a torrent-file as a parameter and
<p>The fast resume mechanism is a way to remember which pieces are downloaded and where they
are put between sessions. You can generate fast resume data by calling
<ttclass="docutils literal"><spanclass="pre">torrent_handle::write_resume_data()</span></tt> on <aclass="reference"href="#torrent-handle">torrent_handle</a>. You can then save this data
to disk and use it when resuming the torrent. libtorrent will not check the piece hashes
then, and rely on the information given in the fast-resume data. The fast-resume data
also contains information about which blocks, in the unfinished pieces, were downloaded,
so it will not have to start from scratch on the partially downloaded pieces.</p>
<p>To use the fast-resume data you simply give it to <aclass="reference"href="#add-torrent">add_torrent()</a>, and it
will skip the time consuming checks. It may have to do the checking anyway, if the
fast-resume data is corrupt or doesn't fit the storage for that torrent, then it will
not trust the fast-resume data and just do the checking.</p>
<divclass="section"id="file-format">
<h2><aname="file-format">file format</a></h2>
<p>The file format is a bencoded dictionary containing the following fields:</p>
<p>for example. For more information, see the <aclass="reference"href="http://www.boost.org/libs/filesystem/doc/index.htm">Boost.Filesystem docs</a>.</p>