Introduction to the Bittorrent Protocol

8,343 views

Published on

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

Published in: Technology
3 Comments
8 Likes
Statistics
Notes
  • salox
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • @packata

    Yeah, that query was hideous. Ironically, i was just trying to make it simple.

    The IDE in the screenshots is notepad++ with a custom theme.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The query in createResponse is a killer :D
    There's really no need for that. You can store the seeders / leechers count in the row with the torrent info and update the statistic when the event changes.
    Non the less, really nice presentation. What' IDE are you using?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
8,343
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
366
Comments
3
Likes
8
Embeds 0
No embeds

No notes for slide

Introduction to the Bittorrent Protocol

  1. 1. Introduction to the BitTorrent Protocol<br />Tommy Montgomery // March 2, 2010<br />
  2. 2. What is BitTorrent?<br />
  3. 3. What is BitTorrent?<br />NOT illegal<br />
  4. 4. What is BitTorrent?<br />NOT illegal<br />Peer-to-peer (P2P) file transfer<br />
  5. 5. What is BitTorrent?<br />NOT illegal<br />Peer-to-peer (P2P) file transfer<br />Awesome<br />
  6. 6. What is BitTorrent?<br />NOT illegal<br />Peer-to-peer (P2P) file transfer<br />Awesome<br />
  7. 7. Poppycock!<br />BitTorrent historically has a bad reputation, due to its decentralized method of file transfer<br />This makes it difficult to “remove” a download, such as one that is illegal<br />BitTorrent is not inherently evil<br />People are inherently evil<br />I like bullet points<br />
  8. 8. How does it work?<br />
  9. 9. How does it work: Consumer<br />Download torrent: <br />lots-o-cool-stuff-n-things.lulz.torrent<br />Open it in your favorite client:<br />
  10. 10. How does it work<br />Files are downloaded piecemeal, from many different places instead of just one<br />Trackers are the central hub of control for each torrent<br />Each tracker manages your “peers”, which are other people who are trying to download the same file<br />
  11. 11. Glossary<br />
  12. 12. Glossary<br />Seeder<br />
  13. 13. Glossary<br />Seeder<br />Someone who has finished downloading and is only uploading<br />
  14. 14. Glossary<br />Seeder<br />Someone who has finished downloading and is only uploading<br />Leecher<br />
  15. 15. Glossary<br />Seeder<br />Someone who has finished downloading and is only uploading<br />Leecher<br />Someone who is downloading<br />
  16. 16. Glossary<br />Seeder<br />Someone who has finished downloading and is only uploading<br />Leecher<br />Someone who is downloading<br />Peer<br />
  17. 17. Glossary<br />Seeder<br />Someone who has finished downloading and is only uploading<br />Leecher<br />Someone who is downloading<br />Peer<br />The collective term for seeders and leechers<br />
  18. 18. What is a torrent file?<br />A Bencoded metadata file containing information about one or more files<br />Name/size/date of each file<br /># of “pieces” of each file<br />SHA1 hash of each piece<br />Trackers for the torrent<br />
  19. 19. What is a “piece”?<br />Torrents are made up of many small pieces<br />You never download all the pieces from one place (unless there’s only one seed)<br />Pieces are usually less than a couple megabytes each<br />
  20. 20. Bencoding<br />Dictionaries<br />Prefixed with “d”, end with “e”<br />Lists<br />Prefixed with “l”, end with “e”<br />Integers<br />Prefixed with “i”, end with “e”<br />Strings<br />Prefixed with string length + “:”<br />
  21. 21. Bencoding<br />d3:foo3:bar4:lulzi8el5:hello5:worldee<br />
  22. 22. Bencoding<br />d3:foo3:bar4:lulzi8el5:hello5:worldee<br /><ul><li>Dictionary</li></li></ul><li>Bencoding<br />d3:foo3:bar4:lulzi8el5:hello5:worldee<br /><ul><li>Dictionary
  23. 23. String</li></li></ul><li>Bencoding<br />d3:foo3:bar4:lulzi8el5:hello5:worldee<br /><ul><li>Dictionary
  24. 24. String
  25. 25. Integer</li></li></ul><li>Bencoding<br />d3:foo3:bar4:lulzi8el5:hello5:worldee<br /><ul><li>Dictionary
  26. 26. String
  27. 27. Integer
  28. 28. List</li></li></ul><li>Trackers<br />
  29. 29. Trackers<br />HTTP service that responds to GET requests<br />Response is text/plain Bencoded dictionary<br />Usually listens on port 6969<br />
  30. 30. Tracker Announce Request<br />Usually has a billion GET params:<br />info_hash<br />peer_id<br />port<br />uploaded<br />downloaded<br />left<br />compact<br />no_peer_id<br />event<br />ip<br />numwant<br />key<br />trackerid<br />
  31. 31. Tracker Announce Request<br />Important GET params:<br />info_hash<br />the SHA1 hash of the piece to download<br />peer_id<br />unique identifier of the person doing the downloading<br />ip/port<br />the IP address and port of the peer<br />uploaded/downloaded/left<br />used for calculating “share ratio”, i.e. how much the tracker likes them<br />
  32. 32. Tracker Announce Response<br />A Bencoded dictionary with these keys:<br />complete<br />incomplete<br />tracker id (yes, that is a space)<br />peers<br />warning message<br />failure reason (only if an error occurred)<br />interval<br />min interval<br />
  33. 33. Tracker Announce Response<br />The relevant parts:<br />peers<br />Dictionary containing each peer’s IP address and port<br />Or, if the client wants it compact, a binary string of each peer’s IP address and port<br />interval/min interval<br />the interval in seconds which the client is allowed to make a request<br />complete/incomplete<br />number of seeders/leechers<br />
  34. 34. Tracker Implementation<br />PHP/MySQL/Apache<br />Why not?<br />
  35. 35. First, of course…<br />We need a Bencoding library. Let’s roll our own.<br />
  36. 36.
  37. 37. yay.<br />
  38. 38. It’s tracker time<br />We need a way to keep track of peers, and who’s downloaded what and stuff<br />Let’s use MySQL<br />
  39. 39.
  40. 40. It’s tracker time<br />Request is pretty boring, as it’s just a glorified array with error handling<br />
  41. 41. It’s tracker time<br />Response is a Bencoded dictionary, and is also a glorified array<br />
  42. 42.
  43. 43. It’s tracker time<br />Now we can set up an exception handler that will send a proper response back in the event of a catastrophe<br />
  44. 44. It’s tracker time<br />Now we can make the tracker announce the peers<br />
  45. 45.
  46. 46.
  47. 47. Set up the torrent<br />Now, stuff your tracker on a server somewhere, listen on port 6969<br />Then add your tracker’s announce URL to a torrent<br />e.g. http://likeaboss.biz:6969/announce<br />
  48. 48. Other stuff<br />Trackers are also facilitators and the supreme overlords over the torrents they track<br />The list of peers that gets sent back to the client determines who downloads from whom<br />They will use download/upload statistics to determine who isn’t uploading/seeding, and punish them by giving them crappy peers<br />
  49. 49. Open source trackers<br />OpenTracker (used by what’s left of The Pirate Bay)<br />XBT Tracker – implemented using UDP instead of HTTP for ultimate efficiency<br />MonoTorrent – cross-platform C# implementation (client as well)<br />PeerTracker – PHP 5 implementation<br />
  50. 50. In short…<br />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.<br />Because it’s fun.<br />
  51. 51. Questions?<br />

×