views:

606

answers:

3

For the first time i used a magnet link. Curious about how it works i looked up the specs and didnt find any answers. From the wiki it says xt means exact topic and is followed by the format (btih in this case) with a SHA1 hash. I saw base32 mentioned, knowing its 5bits per letter and 32 letters i found it holds exactly 160bits which is exactly the size of the SHA1.

Theres no room for IP address or anything, its just a SHA1. So how does it find it? i turned on URL Snooper to see if it visits a page (using tcp/http) and do a lookup or any other things similar and nothing happened. I didnt really think it would visit a site but i just did it to check. I have no idea how it finds peers, how does it work?

Also, what is the hash of? is it a hash of an array of all the file hashes together? maybe its a hash of the actual torrent file required (stripping certain information)?


In a VM i tried a magnet link with utorrent which was freshly installed and it manage to find peers. Where did the first peer come from? It was fresh and there were no other torrents.

+2  A: 

When I started answering your question, I didn't realize you were asking how the magnet scheme works. Just thought you wanted to know how the parts relevant to the bittorrent protocol were generated.


The hash listed in the magnet uri is the torrent's info hash encoded in base32. The info hash is the sha1 hash of the bencoded info block of the torrent.

This python code demonstrates how it can be calculated.

I wrote a (very naive) C# implementation to test this out since I didn't have a bencoder on hand and it matches what is expected from the client.

static string CalculateInfoHash(string path)
{
    // assumes info block is last entry in dictionary
    var infokey = "e4:info";
    var offset = File.ReadAllText(path).IndexOf(infokey) + infokey.Length;
    byte[] fileHash = File.ReadAllBytes(path).Skip(offset).ToArray();
    byte[] bytes;
    using (SHA1 sha1 = SHA1.Create())
        bytes = sha1.ComputeHash(fileHash, 0, fileHash.Length - 1); // need to remove last 'e' to compensate for bencoding
    return String.Join("", bytes.Select(b => b.ToString("X2")));
}

As I understand it, this hash does not include any information on how to locate the tracker, the client needs to find this out through other means (the announce url provided). This is just what distinguishes one torrent from another on the tracker.

Everything related to the bittorrent protocol still revolves around the tracker. It is still the primary means of communication among the swarm. The magnet uri scheme was not designed specifically for use by bittorrent. It's used by any P2P protocols as an alternative form of communicating. Bittorrent clients adapted to accept magnet links as another way to identify torrents that way you don't need to download .torrent files anymore. The magnet uri still needs to specify the tracker in order to locate it so the client may participate. It can contain information about other protocols but is irrelevant to the bittorrent protocol. The bittorrent protocol ultimately will not work without the trackers.

Jeff M
This doesnt help. But are you saying it hashes the entire torrent file skipping the infokey block? My question was about how it finds the peers.
acidzombie24
That's all coordinated by the tracker. The tracker knows of all the torrents it contains and clients seeding/leeching it. When someone connects with a magnet link, the info hash is used to determine what torrent the user is trying to find and sends a list of peers back.
Jeff M
@acidzombie24 You're probably thinking about distributed trackers which uses DHT to locate peers. This has nothing to do with magnet links. (http://en.wikipedia.org/wiki/BitTorrent_(protocol)#Distributed_trackers)
Alexander Sagen
I don't know how DHT works technically but can only speculate. All clients keep track of the peers they've connected to. When a new client enters the swarm, it gets all the information it needs from the other peers it initially connects to. It would still need a tracker to find the swarm initially. But once it connects to the swarm, it remembers the peers it connected to the last time and is able to make connections directly with them the next time it wants to continue. After that, it no longer needs the tracker to find the peers.
Jeff M
@Jeff M: But what 'sends' a list of peers back. A link is just a link theres no tracker associated with it. I was trying to figure out WHAT sends back peers.
acidzombie24
@Alexander Sagen: My question is about how do magnet links work. All i mentioned was with it has a SHA1@Jeff M: Are you speculating that when someone uses a magnet link the torrent program connects to random peers it knows of? ok but i still would like an answer (from someone) about how it works
acidzombie24
I moved my comment into my answer. I didn't realize you were asking about how the whole magnet uri scheme works with bittorrent. Hopefully it should be clearer now. In summary, magnet links does not replace the role of the tracker, but replaces the role of the .torrent files.
Jeff M
+1. Also The magnet link in question does not specify tr(acker). Only th sha1 which left me confused. Especially when i am using a fresh install with no torrents running (and not connected to any peers) and have the magnet link find peers. Its magic, i have no idea how it works. There must be some home server it can ask for peers. But does that mean i send out queries to peers looking for a hash and the client passes the message on to many peers until one answers my call?
acidzombie24
I'm not sure how to answer that. All magnet uris I've seen always specify the tracker. It might be your client trying a list of public trackers that it knows about and one happens to have it. What trackers does the associated torrent list as being used? How is it displayed? Is there any relation between the tracker it connects to and the source of the magnet link? Maybe it's a torrent that uses DHT? Does the same work for a private torrent? Again, I don't know how DHT works exactly. I'll see if I can find any more information.
Jeff M
@Jeff M: I did some research and i finally found something. I'm posting the answer now.
acidzombie24
A: 

I finally found specification. For the first time google didnt help. (wiki linked to bittorrent.com which is the main site. I Clicked the developers link, notice the bittorrent.org tab on the right then it was easy from there. Its hard finding links when you have no idea what they are labeled and many clicks away).

It seems like all torrents have a network of peers. You find peers from trackers and you keep them between sessions. The network allows you to find peers and other things. I havent read how its used with magnet links but it seems like it is undefined how a fresh client find peers. Perhaps some is baked in, or they use their home server or known trackers embeded into the client to get the first peer in the network.

acidzombie24
Ah, I guess I was right about it going to DHT to find clients. "If no tracker is specified, the client SHOULD use the DHT (BEP 0005 [3]) to acquire peers."
Jeff M
+1  A: 

the list of peers are probably populated from the torrent that upgrades the client (e.g. there's a torrent for utorrent that upgrades it). as long as everyone's using the same client, it should be good because you have no choice but to share the upgrade.

Moe
Thats a very logical place to search for the hash and other peers. +1
acidzombie24