protocol.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. BitTorrent <index.html> download <download.html> FAQ <FAQ.html>
  2. documentation <doc.html> press <press.html> Donate! <donate.html>
  3. BitTorrent is a protocol for distributing files. It identifies content
  4. by url and is designed to integrate seamlessly with the web. Its
  5. advantage over plain http is that when multiple downloads of the same
  6. file happen concurrently, the downloaders upload to each other, making
  7. it possible for the file source to support very large numbers of
  8. downloaders with only a modest increase in its load.
  9. The life cycle of a BitTorrent file distribution.
  10. A BitTorrent file distribution consists of these entities -
  11. * An ordinary web server
  12. * A static 'metainfo' file
  13. * A BitTorrent tracker
  14. * An 'origin' downloader
  15. * The end user web browsers
  16. * The end user downloaders
  17. There are ideally many end users for a single file.
  18. To start serving, a host goes through the following steps -
  19. 1. Start running a tracker (or, more likely, have one running already).
  20. 2. Start running an ordinary web server, such as apache, or have one
  21. already.
  22. 3. Associate the extension .torrent with mimetype
  23. application/x-bittorrent on their web server (or have done so
  24. already).
  25. 4. Generate a metainfo file using the complete file to be served and
  26. the url of the tracker.
  27. 5. Put the metainfo file on the web server.
  28. 6. Link to the metainfo file from some other web page.
  29. 7. Start a downloader which already has the complete file (the
  30. 'origin').
  31. To start downloading, a user does the following -
  32. 1. Run a BitTorrent installer (or have done so already).
  33. 2. Web surf.
  34. 3. Click on a link to a .torrent file.
  35. 4. Select where to save the file locally, or select a partial
  36. download to resume.
  37. 5. Wait for download to complete.
  38. 6. Tell downloader to exit (it keeps uploading until this happens).
  39. The connectivity is as follows -
  40. * The web site is serving up static files as normal, but kicking off
  41. the BitTorrent helper app on the clients.
  42. * The tracker is receiving information from all downloaders and
  43. giving them random lists of peers. This is done over http or https.
  44. * Downloaders are periodically checking in with the tracker to keep
  45. it informed of their progress, and are uploading to and
  46. downloading from each other via direct connections. These
  47. connections use the BitTorrent peer protocol, which operates over
  48. TCP.
  49. * The origin is uploading but not downloading at all, since it has
  50. the entire file. The origin is necessary to get the entire file
  51. into the network. Often for popular downloads the origin can be
  52. taken down after a while since several downloads may have
  53. completed and been left running indefinitely.
  54. Metainfo file and tracker responses are both sent in a simple,
  55. efficient, and extensible format called bencoding (pronounced 'bee
  56. encoding'). Bencoded messages are nested dictionaries and lists, which
  57. can contain strings and integers. Extensibility is supported by ignoring
  58. unexpected dictionary keys, so additional optional ones can be added later.
  59. Bencoding is done as follows -
  60. * Strings are length-prefixed base ten followed by a colon and the
  61. string. For example '4:spam' corresponds to 'spam'.
  62. * Integers are represented by an 'i' followed by the number in base
  63. 10 followed by an 'e'. For example 'i3e' corresponds to 3 and
  64. 'i-3e' corresponds to -3. Integers have no size limitation. 'i-0e'
  65. is invalid. All encodings with a leading zero, such as 'i03e', are
  66. invalid, other than 'i0e', which of course corresponds to 0.
  67. * Lists are encoded as an 'l' followed by their elements (also
  68. bencoded) followed by an 'e'. For example 'l4:spam4:eggse'
  69. corresponds to ['spam', 'eggs'].
  70. * Dictionaries are encoded as a 'd' followed by a list of
  71. alternating keys and their corresponding values followed by an
  72. 'e'. For example, 'd3:cow3:moo4:spam4:eggse' corresponds to
  73. {'cow': 'moo', 'spam': 'eggs'} and 'd4:spaml1:a1:bee' corresponds
  74. to {'spam': ['a', 'b']} . Keys must be strings and appear in
  75. sorted order (sorted as raw strings, not alphanumerics).
  76. * Metainfo files are bencoded dictionaries with the following keys -
  77. *
  78. * md={
  79. * announce=>'url',
  80. * info=>{
  81. * name=>'top-level-file-or-directory-name',
  82. * piece length=>12345,
  83. * pieces=>'md5sums',
  84. *
  85. * length=>12345,
  86. * -or-
  87. * files=>[
  88. * {
  89. * length=>12345,
  90. * path=>['sub','directory','path','and','filename']
  91. *
  92. * }, ... {}
  93. * ]
  94. * }
  95. Metainfo files are bencoded dictionaries with the following keys -
  96. 'announce'
  97. The url of the tracker.
  98. 'info'
  99. This maps to a dictionary, with keys described below.
  100. The 'name' key maps to a string which is the suggested name to save
  101. the file (or directory) as. It is purely advisory.
  102. 'piece length' maps to the number of bytes in each piece the file is
  103. split into. For the purposes of transfer, files are split into
  104. fixed-size pieces which are all the same length except for possibly
  105. the last one which may be truncated. Piece length is almost always a
  106. power of two, most commonly 2^20 .
  107. 'pieces' maps to a string whose length is a multiple of 20. It is to
  108. be subdivided into strings of length 20, each of which is the sha1
  109. hash of the piece at the corresponding index.
  110. There is also a key 'length' or a key 'files', but not both or
  111. neither. If 'length' is present then the download represents a
  112. single file, otherwise it represents a set of files which go in a
  113. directory structure.
  114. In the single file case, 'length' maps to the length of the file in
  115. bytes.
  116. For the purposes of the other keys, the multi-file case is treated
  117. as only having a single file by concatenating the files in the order
  118. they appear in the files list. The files list is the value 'files'
  119. maps to, and is a list of dictionaries containing the following keys -
  120. 'length'
  121. The length of the file, in bytes. 'path'
  122. A list of strings corresponding to subdirectory names, the last of
  123. which is the actual file name (a zero length list is an error case).
  124. In the single file case, the 'name' key is the name of a file, in the
  125. muliple file case, it's the name of a directory.
  126. Tracker queries are two way. The tracker receives information via GET
  127. parameters and returns a bencoded message. Note that although the
  128. current tracker implementation has its own web server, the tracker could
  129. run very nicely as, for example, an apache module.
  130. * Tracker GET requests have the following keys urlencoded -
  131. * req = {
  132. * info_hash => 'hash'
  133. * peer_id => 'random-20-character-name'
  134. * ip => 'ip-address' -or- 'dns-name'
  135. * port => '12345'
  136. * uploaded => '12345'
  137. * downloaded => '12345'
  138. * left => '12345'
  139. * event => 'started', 'completed' -or- 'stopped'
  140. * }
  141. Tracker GET requests have the following keys -
  142. 'info_hash'
  143. The 20 byte sha1 hash of the bencoded form of the 'info' value from
  144. the metainfo file. Note that this is a substring of the metainfo
  145. file. This value will almost certainly have to be escaped.
  146. 'peer_id'
  147. A string of length 20 which this downloader uses as its id. Each
  148. downloader generates its own id at random at the start of a new
  149. download. This value will also almost certainly have to be escaped.
  150. 'ip'
  151. An optional parameter giving the ip (or dns name) which this peer is
  152. at. Generally used for the origin if it's on the same machine as the
  153. tracker.
  154. 'port'
  155. The port number this peer is listening on. Common behavior is for a
  156. downloader to try to listen on port 6881 and if that port is taken
  157. try 6882, then 6883, etc. and give up after 6889.
  158. 'uploaded'
  159. The total amount uploaded so far, encoded in base ten ascii.
  160. 'downloaded'
  161. The total amount downloaded so far, encoded in base ten ascii.
  162. 'left'
  163. The number of bytes this peer still has to download, encoded in base
  164. ten ascii. Note that this can't be computed from downloaded and the
  165. file length since it might be a resume, and there's a chance that
  166. some of the downloaded data failed an integrity check and had to be
  167. re-downloaded.
  168. 'event'
  169. This is an optional key which maps to 'started', 'completed', or
  170. 'stopped' (or '', which is the same as not being present). If not
  171. present, this is one of the announcements done at regular intervals.
  172. An announcement using 'started' is sent when a download first
  173. begins, and one using 'completed' is sent when the download is
  174. complete. No 'completed' is sent if the file was complete when
  175. started. Downloaders send an announcement using 'stopped' when they
  176. cease downloading.
  177. * Tracker responses are bencoded dictionaries.
  178. *
  179. * resp = {
  180. * failure reason => 'error text'
  181. * - or -
  182. * interval => 12345
  183. * peers => {
  184. * peer id => 'identifier'
  185. * ip => 'ip-address' -or- 'hostname'
  186. * port => 12345
  187. * }
  188. * }
  189. Tracker responses are bencoded dictionaries. If a tracker response has a
  190. key 'failure reason', then that maps to a human readable string which
  191. explains why the query failed, and no other keys are required.
  192. Otherwise, it must have two keys - 'interval', which maps to the number
  193. of seconds the downloader should wait between regular rerequests, and
  194. 'peers'. 'peers' maps to a list of dictionaries corresponding to peers,
  195. each of which contains the keys 'peer id', 'ip', and 'port', which map
  196. to the peer's self-selected id, ip address or dns name as a string, and
  197. port number, respectively. Note that downloaders may rerequest on
  198. nonscheduled times if an event happens or they need more peers.
  199. If you want to make any extensions to metainfo files or tracker queries,
  200. please coordinate with Bram Cohen <mailto:bram@bitconjurer.org> to make
  201. sure that all extensions are done compatibly.
  202. * BitTorrent's peer protocol operates over TCP
  203. *
  204. * peer = {
  205. * }
  206. *
  207. BitTorrent's peer protocol operates over TCP. It performs efficiently
  208. without setting any socket options.
  209. Peer connections are symmetrical. Messages sent in both directions look
  210. the same, and data can flow in either direction.
  211. The peer protocol refers to pieces of the file by index as described in
  212. the metainfo file, starting at zero. When a peer finishes downloading a
  213. piece and checks that the hash matches, it announces that it has that
  214. piece to all of its peers.
  215. Connections contain two bits of state on either end - choked or not, and
  216. interested or not. Choking is a notification that no data will be sent
  217. until unchoking happens. The reasoning and common techniques behind
  218. choking are explained later in this document.
  219. Data transfer takes place whenever one side is interested and the other
  220. side is not choking. Interest state must be kept up to date at all times
  221. - whenever a downloader doesn't have something they currently would ask
  222. a peer for in unchoked, they must express lack of interest, despite
  223. being choked. Implementing this properly is tricky, but makes it
  224. possible for downloaders to know which peers will start downloading
  225. immediately if unchoked.
  226. Connections start out choked and not interested.
  227. When data is being transferred, downloaders should keep several piece
  228. requests queued up at once in order to get good TCP performance (this is
  229. called 'pipelining'.) On the other side, requests which can't be written
  230. out to the TCP buffer immediately should be queued up in memory rather
  231. than kept in an application-level network buffer, so they can all be
  232. thrown out when a choke happens.
  233. * Handshake:
  234. * \x13BitTorrent protocol\0\0\0\0\0\0\0\0<sha1 info hash><20byte peerid>
  235. *
  236. * Keep alive:
  237. * \x0000000
  238. *
  239. * 0 - choke:
  240. * \x0000001\00
  241. *
  242. * 1 - unchoke:
  243. * \x0000001\01
  244. *
  245. * 2 - interested:
  246. * \x0000001\02
  247. *
  248. * 3 - not interested:
  249. * \x0000001\03
  250. *
  251. * 4 - have:
  252. * \x0000005\04<index>
  253. *
  254. * 5 - bitfield:
  255. * \x00000??\05<bitfield-data>
  256. *
  257. * 6 - request:
  258. * \x000000d\06<index><begin><length>
  259. * length must be less than 2^17, recommended 2^15, regardless of pieces size
  260. *
  261. * 7 - piece:
  262. * \x???????\07<index><begin><data>
  263. *
  264. * 8 - cancel:
  265. * \x000000d\08<index><begin><length>
  266. The peer wire protocol consists of a handshake followed by a
  267. never-ending stream of length-prefixed messages. The handshake starts
  268. with character ninteen followed by the string 'BitTorrent protocol'. The
  269. leading character is a length prefix, put there in the hope that other
  270. new protocols may do the same and thus be trivially distinguishable from
  271. each other.
  272. All later integers sent in the protocol are encoded as four bytes
  273. big-endian.
  274. After the fixed headers come eight reserved bytes, which are all zero in
  275. all current implementations. If you wish to extend the protocol using
  276. these bytes, please coordinate with Bram Cohen
  277. <mailto:bram@bitconjurer.org> to make sure all extensions are done
  278. compatibly.
  279. Next comes the 20 byte sha1 hash of the bencoded form of the 'info'
  280. value from the metainfo file. (This is the same value which is announced
  281. as info_hash to the tracker, only here it's raw instead of quoted here).
  282. If both sides don't send the same value, they sever the connection. The
  283. one possible exception is if a downloader wants to do multiple downloads
  284. over a single port, they may wait for incoming connections to give a
  285. download hash first, and respond with the same one if it's in their list.
  286. After the download hash comes the 20-byte peer id which is reported in
  287. tracker requests and contained in peer lists in tracker responses. If
  288. the receiving side's peer id doesn't match the one the initiating side
  289. expects, it severs the connection.
  290. That's it for handshaking, next comes an alternating stream of length
  291. prefixes and messages. Messages of length zero are keepalives, and
  292. ignored. Keepalives are generally sent once every two minutes, but note
  293. that timeouts can be done much more quickly when data is expected.
  294. All non-keepalive messages start with a single byte which gives their
  295. type. The possible values are -
  296. * 0 - choke
  297. * 1 - unchoke
  298. * 2 - interested
  299. * 3 - not interested
  300. * 4 - have
  301. * 5 - bitfield
  302. * 6 - request
  303. * 7 - piece
  304. * 8 - cancel
  305. Choke, unchoke, interested, and not interested have no payload.
  306. Bitfield is only ever sent as the first message. Its payload is a
  307. bitfield with each index that downloader has sent set to one and the
  308. rest set to zero. Downloaders which don't have anything yet may skip the
  309. bitfield message. The first byte of the bitfield corresponds to indices
  310. 0-7 from high bit to low bit, respectively. The next one 8-15, etc.
  311. Spare bits at the end are set to zero.
  312. The have message's payload is a single number, the index which that
  313. downloader just completed and checked the hash of.
  314. Request messages contain an index, begin, and length. The last two are
  315. byte offsets. Length is generally a power of two unless it gets
  316. truncated by the end of the file. All current implementations use 2^15 ,
  317. and close connections which request an amount greater than 2^17 .
  318. Cancel messages have the same payload as request messages. They are
  319. generally only sent towards the end of a download, during what's called
  320. 'endgame mode'. When a download is almost complete, there's a tendency
  321. for the last few pieces to all be downloaded off a single hosed modem
  322. line, taking a very long time. To make sure the last few pieces come in
  323. quickly, once requests for all pieces a given downloader doesn't have
  324. yet are currently pending, it sends requests for everything to everyone
  325. it's downloading from. To keep this from becoming horribly inefficient,
  326. it sends cancels to everyone else every time a piece arrives.
  327. Piece messages contain an index, begin, and piece. Note that they are
  328. correlated with request messages implicitly. It's possible for an
  329. unexpected piece to arrive if choke and unchoke messages are sent in
  330. quick succession and/or transfer is going very slowly.
  331. Downloaders generally download pieces in random order, which does a
  332. reasonably good job of keeping them from having a strict subset or
  333. superset of the pieces of any of their peers.
  334. Choking is done for several reasons. TCP congestion control behaves very
  335. poorly when sending over many connections at once. Also, choking lets
  336. each peer use a tit-for-tat-ish algorithm to ensure that they get a
  337. consistent download rate.
  338. The choking algorithm described below is the currently deployed one. It
  339. is very important that all new algorithms work well both in a network
  340. consisting entirely of themselves and in a network consisting mostly of
  341. this one.
  342. There are several criteria a good choking algorithm should meet. It
  343. should cap the number of simultaneous uploads for good TCP performance.
  344. It should avoid choking and unchoking quickly, known as fibrillation. It
  345. should reciprocate to peers who let it download. Finally, it should try
  346. out unused connections once in a while to find out if they might be
  347. better than the currently used ones, known as optimistic unchoking.
  348. The currently deployed choking algorithm avoids fibrillation by only
  349. changing who's choked once every ten seconds. It does reciprocation and
  350. number of uploads capping by unchoking the four peers which it has the
  351. best download rates from and are interested. Peers which have a better
  352. upload rate but aren't interested get unchoked and if they become
  353. interested the worst uploader gets choked. If a downloader has a
  354. complete file, it uses its upload rate rather than its download rate to
  355. decide who to unchoke.
  356. For optimistic unchoking, at any one time there is a single peer which
  357. is unchoked regardless of it's upload rate (if interested, it counts as
  358. one of the four allowed downloaders.) Which peer is optimistically
  359. unchoked rotates every 30 seconds. To give them a decent chance of
  360. getting a complete piece to upload, new connections are three times as
  361. likely to start as the current optimistic unchoke as anywhere else in
  362. the rotation.