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