Library bittorrent

Bittorrent and DHT protocol library which enables users to read information from a torrent file, decode bencoded (bittorrent encoded) buffers, find peers associated with a certain torrent and retrieve nodes discovered during the search for peers.

For more information on the Bittorrent and DHT protocol go to: http://www.bittorrent.org/beps/bep_0000.html

The library contains the class Torrent and the function bdecode(buf)

How this library is likely to be used:

 local filename = "/home/user/name.torrent"
 local torrent = bittorrent.Torrent:new()
 torrent:load_from_file(filename)
 torrent:trackers_peers() -- to load peers from the trackers
 torrent:dht_peers() -- to further load peers using the DHT protocol from existing peers
After these operations the peers and nodes can be found in torrent.peers and torrent.nodes tables respectively

Author:

  • Gorjan Petrovski

Source: https://svn.nmap.org/nmap/nselib/bittorrent.lua

Functions

bdecode (buf)

Parses a bencoded buffer

calc_info_hash (self)

Calculates the info hash using self.info_buf.

calc_torrent_size (self)

Calculates the size of the torrent in bytes

dht_peers (self, timeout)

Runs the three threads which do a DHT discovery of nodes and peers.

generate_peer_id (self)

Generates a peer_id similar to the ones generated by Ktorrent version 4.1.1

http_tracker_peers (self, tracker)

Gets the peers from a http tracker when supplied the URL of the tracker

load_from_file (self, filename)

Reads a torrent file, loads self.buffer and parses it using self:parse_buffer(), then self:calc_info_hash()

load_from_magnet (self, magnet)

Loads trackers and similar information for a torrent from a magnet link.

load_trackers (self)

Loads the list of trackers in self.trackers from self.tor_struct

parse_buffer (self)

Parses self.buffer, fills self.tor_struct, self.info_buf

trackers_peers (self)

Gets peers available from the loaded trackers

udp_tracker_peers (self, tracker)

Gets the peers from udp trackers when supplied the URL of the tracker.

Functions

bdecode (buf)

Parses a bencoded buffer

Parameters

buf
 

Return values:

  1. bool indicating if parsing went ok
  2. table containing the decoded structure, or error string
calc_info_hash (self)

Calculates the info hash using self.info_buf.

The info_hash value is used in many communication transactions for identifying the file shared among the bittorrent peers

Parameters

self
 
calc_torrent_size (self)

Calculates the size of the torrent in bytes

Parameters

self
 
dht_peers (self, timeout)

Runs the three threads which do a DHT discovery of nodes and peers.

The default timeout for this discovery is 30 seconds but it can be set through the timeout argument.

Parameters

self
 
timeout
 
generate_peer_id (self)

Generates a peer_id similar to the ones generated by Ktorrent version 4.1.1

Parameters

self
 
http_tracker_peers (self, tracker)

Gets the peers from a http tracker when supplied the URL of the tracker

Parameters

self
 
tracker
 
load_from_file (self, filename)

Reads a torrent file, loads self.buffer and parses it using self:parse_buffer(), then self:calc_info_hash()

Parameters

self
 
filename
 

Return values:

  1. boolean indicating whether loading went alright
  2. err string with error message if loadin went wrong
load_from_magnet (self, magnet)

Loads trackers and similar information for a torrent from a magnet link.

Parameters

self
 
magnet
 
load_trackers (self)

Loads the list of trackers in self.trackers from self.tor_struct

Parameters

self
 
parse_buffer (self)

Parses self.buffer, fills self.tor_struct, self.info_buf

This function is similar to the bdecode function but it has a few additions for calculating torrent file specific fields

Parameters

self
 
trackers_peers (self)

Gets peers available from the loaded trackers

Parameters

self
 
udp_tracker_peers (self, tracker)

Gets the peers from udp trackers when supplied the URL of the tracker.

First we establish a connection to the udp server and then we can request peers. For a good specification refer to: http://www.rasterbar.com/products/libtorrent/udp_tracker_protocol.html

Parameters

self
 
tracker