Posts Tagged ‘bittorrent’

The bitfield tracks which pieces of the transfer are already downloaded, and which aren’t. We therefore only need a structure which can track two states per value, which is why we use a bitarray library. In fact, we use only a subset of these functions, and rename a few to make them more expressive.

We also need to be able to translate the bitfield from/to the network where the high bit (right-most in network order) in the first byte corresponds to piece index 0.

Read more »

Once the Metainfo file can be read, the client ought to know which files compose the transfer, and how to connect to sources for the transfer. In this post, we will define the elements we need to perform the I/O operations, that is, to read data from (and write it to) the disk.
Read more »

To exchange files, a BitTorrent client needs some information about the exchanged file(s). This information is contained within a file named the “metainfo” file.

More details are explaind in the Metainfo File Structure of the BitTorrent specification web page.

In short, the structure is a UTF-8 bencoded dictionary containing some required and optional key/value pairs.

Read more »

Bencoding values for the BitTorrent protocol is explained in a Wikipedia article on Bencoding or in the more general BitTorrent protocol page.

Briefly, there are four types :

  • byte strings : [string length encoded in base ten ASCII]:[string data] ;
  • integers : i[integer encoded in base ten ASCII]e ;
  • lists : l[bencoded values]e ;
  • and dictionaries : d[bencoded string][bencoded element]e.

Byte strings can be strings represented as byte strings (such as the description of the exchanged data), as well as an array of bytes (such as a hash). Representing them with the .Net strings can thus lead to errors. Hence we shall keep the byte array representation.

Integers can be longs, hence we represent them with int64.

Read more »