Introduction to the Bittorrent Protocol

Preview:

DESCRIPTION

How the BitTorrent protocol works, along with PHP code on how to write a torrent tracker.

Citation preview

Introduction to the BitTorrent Protocol

Tommy Montgomery // March 2, 2010

What is BitTorrent?

What is BitTorrent? NOT illegal

What is BitTorrent? NOT illegal Peer-to-peer (P2P) file transfer

What is BitTorrent? NOT illegal Peer-to-peer (P2P) file transfer Awesome

What is BitTorrent? NOT illegal Peer-to-peer (P2P) file transfer Awesome

Poppycock! BitTorrent historically has a bad reputation,

due to its decentralized method of file transfer

This makes it difficult to “remove” a download, such as one that is illegal

BitTorrent is not inherently evil People are inherently evil I like bullet points

How does it work?

How does it work: Consumer Download torrent:

lots-o-cool-stuff-n-things.lulz.torrent

Open it in your favorite client:

How does it work Files are downloaded piecemeal, from many

different places instead of just one Trackers are the central hub of control for

each torrent Each tracker manages your “peers”, which

are other people who are trying to download the same file

Glossary

Glossary Seeder

Glossary Seeder

Someone who has finished downloading and is only uploading

Glossary Seeder

Someone who has finished downloading and is only uploading

Leecher

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

Peer

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

PeerThe collective term for seeders and leechers

What is a torrent file? A Bencoded metadata file containing

information about one or more filesName/size/date of each file# of “pieces” of each fileSHA1 hash of each pieceTrackers for the torrent

What is a “piece”? Torrents are made up of many small pieces You never download all the pieces from one

place (unless there’s only one seed) Pieces are usually less than a couple

megabytes each

Bencoding Dictionaries

Prefixed with “d”, end with “e” Lists

Prefixed with “l”, end with “e” Integers

Prefixed with “i”, end with “e” Strings

Prefixed with string length + “:”

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String• Integer

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String• Integer• List

Trackers

Trackers HTTP service that responds to GET requests Response is text/plain Bencoded dictionary Usually listens on port 6969

Tracker Announce Request Usually has a billion GET params:

info_hash peer_id port uploaded downloaded left compact no_peer_id event ip numwant key trackerid

Tracker Announce Request Important GET params:

info_hash○ the SHA1 hash of the piece to download

peer_id○ unique identifier of the person doing the

downloadingip/port○ the IP address and port of the peer

uploaded/downloaded/left○ used for calculating “share ratio”, i.e. how much the

tracker likes them

Tracker Announce Response A Bencoded dictionary with these keys:

completeincompletetracker id (yes, that is a space)peerswarning messagefailure reason (only if an error occurred)intervalmin interval

Tracker Announce Response The relevant parts:

peers○ Dictionary containing each peer’s IP address and

port○ Or, if the client wants it compact, a binary string of

each peer’s IP address and portinterval/min interval○ the interval in seconds which the client is allowed to

make a requestcomplete/incomplete○ number of seeders/leechers

Tracker Implementation PHP/MySQL/Apache Why not?

First, of course… We need a Bencoding library. Let’s roll our

own.

yay.

It’s tracker time We need a way to keep track of peers, and

who’s downloaded what and stuff Let’s use MySQL

It’s tracker time Request is pretty boring, as it’s just a

glorified array with error handling

It’s tracker time Response is a Bencoded dictionary, and is

also a glorified array

It’s tracker time Now we can set up an exception handler

that will send a proper response back in the event of a catastrophe

It’s tracker time Now we can make the tracker announce the

peers

Set up the torrent Now, stuff your tracker on a server

somewhere, listen on port 6969 Then add your tracker’s announce URL to a

torrent e.g. http://likeaboss.biz:6969/announce

Other stuff Trackers are also facilitators and the

supreme overlords over the torrents they track

The list of peers that gets sent back to the client determines who downloads from whom

They will use download/upload statistics to determine who isn’t uploading/seeding, and punish them by giving them crappy peers

Open source trackers OpenTracker (used by what’s left of The

Pirate Bay) XBT Tracker – implemented using UDP

instead of HTTP for ultimate efficiency MonoTorrent – cross-platform C#

implementation (client as well) PeerTracker – PHP 5 implementation

In short… Unless you want to implement some weird

rules for who gets to download stuff, don’t write your own. I only did it for fun.

Because it’s fun.

Questions?

Recommended