Skip to content

Intro to Torrent files

July 22, 2011

To get you started, go here http://en.wikipedia.org/wiki/Torrent_file and read until you gloss over (took me about 12 seconds). The important thing to note is that torrents have an “info hash”. This is tied directly to the contents itself, but just think of it as an id number for the torrent. When you ask a tracker for peers, you give it this id, when you look in the DHT for peers for this, you use this id, when you create a magnet link because you’ve lost the torrent file, you use this id. In addition, there is information about the files that you’re going to be downloading, things like file name, size, etc.

The main flow is this. You go to clearbits.net or somewhere else that has these .torrent files. You download the torrent file and load it into your client (uTorrent hopefully). In the torrent file is enough information to find other people that have the actual contents that the torrent file represents. It might have trackers, or it might just take the aforementioned id and search the dht for peers. Either way, you’ll find some peers and begin downloading. And that’s it, you have your file(s)!

From within your app, the code might look something like the following…

btapp.add.torrent('http://www.clearbits.net/get/1766-the-tunnel.torrent');

There is an alternative to having the entire torrent file however. You can just add a magnet link. The following shows you both how to add a magnet link in an app and what the format for the magnet link is (the info hash is 160 bits…or 40 hex characters)

var hash = '0000000000000000000000000000000000000000';
btapp.add.torrent('magnet:?xt=urn:btih:' + hash);

When you add a magnet link you’ll go through the same process of looking for peers that you would if you had the torrent. You won’t know the specifics of the files that you’re going to be downloading though, until you finally find a peer. The first step is then to download the torrent file itself, which will tell you what you’re download, and potentially other trackers to find peers on etc…

To interact with a specific torrent from within your app, you’ll need to know it’s info hash. This info hash is the same one that you specified when adding the magnet link initially (the info hash can be a bit tricky to get for torrents that you load from a web site or some other source). So lets assume that you are continuing from above.

var torrent = btapp.torrent.get(hash);

torrent.stop();

torrent.start();

var magnet_link = torrent.get_magnet_uri();

torrent.remove(0); //0 means just remove the torrent, not the content

btapp.add.torrent(magnet_link);

For every torrent file there’s a magnet link that you can use instead, and that’s what the get_magnet_uri call does. In this case it’s just returning the same magnet link that you added, but it can be useful, especially if you want to share a torrent with someone. You just have to send this string vs sending an entire torrent file.

Magnet links are of particular interest because they don’t actually have to represent files the way torrents do. If you add the same magnet link to two clients and they discover each other through tracker/dht they’ll connect to each other and stay connected. They won’t know of any other peers that have the torrent file, so they’ll stay connected hoping that the other will acquire the torrent. Before I started playing with app to app messages, there was no real reason to create magnet links that had a random hash, as you’d just be wasting bandwidth. But now you can just have all instances of your client add a magnet link with a constant hash and all of a sudden all of your apps have joined a swarm, where every peer is running your app. And there’s 2^160 different info hashes to choose from, so you can create as many communication swarms as you like and use them for anything!

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: