Introduction to the Bittorrent Protocol

49
Introduction to the BitTorrent Protocol Tommy Montgomery // March 2, 2010

description

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

Transcript of Introduction to the Bittorrent Protocol

Page 1: Introduction to the Bittorrent Protocol

Introduction to the BitTorrent Protocol

Tommy Montgomery // March 2, 2010

Page 2: Introduction to the Bittorrent Protocol

What is BitTorrent?

Page 3: Introduction to the Bittorrent Protocol

What is BitTorrent? NOT illegal

Page 4: Introduction to the Bittorrent Protocol

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

Page 5: Introduction to the Bittorrent Protocol

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

Page 6: Introduction to the Bittorrent Protocol

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

Page 7: Introduction to the Bittorrent Protocol

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

Page 8: Introduction to the Bittorrent Protocol

How does it work?

Page 9: Introduction to the Bittorrent Protocol

How does it work: Consumer Download torrent:

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

Open it in your favorite client:

Page 10: Introduction to the Bittorrent Protocol

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

Page 11: Introduction to the Bittorrent Protocol

Glossary

Page 12: Introduction to the Bittorrent Protocol

Glossary Seeder

Page 13: Introduction to the Bittorrent Protocol

Glossary Seeder

Someone who has finished downloading and is only uploading

Page 14: Introduction to the Bittorrent Protocol

Glossary Seeder

Someone who has finished downloading and is only uploading

Leecher

Page 15: Introduction to the Bittorrent Protocol

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

Page 16: Introduction to the Bittorrent Protocol

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

Peer

Page 17: Introduction to the Bittorrent Protocol

Glossary Seeder

Someone who has finished downloading and is only uploading

LeecherSomeone who is downloading

PeerThe collective term for seeders and leechers

Page 18: Introduction to the Bittorrent Protocol

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

Page 19: Introduction to the Bittorrent Protocol

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

Page 20: Introduction to the Bittorrent Protocol

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 + “:”

Page 21: Introduction to the Bittorrent Protocol

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

Page 22: Introduction to the Bittorrent Protocol

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary

Page 23: Introduction to the Bittorrent Protocol

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String

Page 24: Introduction to the Bittorrent Protocol

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String• Integer

Page 25: Introduction to the Bittorrent Protocol

Bencodingd3:foo3:bar4:lulzi8el5:hello5:worldee

• Dictionary• String• Integer• List

Page 26: Introduction to the Bittorrent Protocol

Trackers

Page 27: Introduction to the Bittorrent Protocol

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

Page 28: Introduction to the Bittorrent Protocol

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

Page 29: Introduction to the Bittorrent Protocol

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

Page 30: Introduction to the Bittorrent Protocol

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

Page 31: Introduction to the Bittorrent Protocol

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

Page 32: Introduction to the Bittorrent Protocol

Tracker Implementation PHP/MySQL/Apache Why not?

Page 33: Introduction to the Bittorrent Protocol

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

own.

Page 34: Introduction to the Bittorrent Protocol
Page 35: Introduction to the Bittorrent Protocol

yay.

Page 36: Introduction to the Bittorrent Protocol

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

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

Page 37: Introduction to the Bittorrent Protocol
Page 38: Introduction to the Bittorrent Protocol

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

glorified array with error handling

Page 39: Introduction to the Bittorrent Protocol

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

also a glorified array

Page 40: Introduction to the Bittorrent Protocol
Page 41: Introduction to the Bittorrent Protocol

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

Page 42: Introduction to the Bittorrent Protocol

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

peers

Page 43: Introduction to the Bittorrent Protocol
Page 44: Introduction to the Bittorrent Protocol
Page 45: Introduction to the Bittorrent Protocol

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

Page 46: Introduction to the Bittorrent Protocol

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

Page 47: Introduction to the Bittorrent Protocol

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

Page 48: Introduction to the Bittorrent Protocol

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.

Page 49: Introduction to the Bittorrent Protocol

Questions?