| 1 | <?xml version="1.0" encoding="utf-8" ?> |
|---|
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 4 | <head> |
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|---|
| 6 | <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> |
|---|
| 7 | <title></title> |
|---|
| 8 | <link rel="stylesheet" href="css/screen.css" type="text/css" /> |
|---|
| 9 | </head> |
|---|
| 10 | <body> |
|---|
| 11 | <div class="document"> |
|---|
| 12 | |
|---|
| 13 | <div id="upper" class="clear"> |
|---|
| 14 | <div id="wrap"> |
|---|
| 15 | <div id="header"> |
|---|
| 16 | <h1><a href="./index.html">BitTorrent<span>.org</span></a></h1> |
|---|
| 17 | </div> |
|---|
| 18 | <div id="nav"> |
|---|
| 19 | <ul> |
|---|
| 20 | <li><a href="./index.html">Home</a></li> |
|---|
| 21 | <li><a href="./introduction.html">For Users</a></li> |
|---|
| 22 | <li><span>For Developers</span></li> |
|---|
| 23 | <!-- <li><a href="./blog">Blog</a></li> --> |
|---|
| 24 | <li><a href="./donate.html">Donate!</a></li> |
|---|
| 25 | </ul> |
|---|
| 26 | </div> <!-- nav --> |
|---|
| 27 | <!-- ### Begin Content ### --> |
|---|
| 28 | <div id="second"> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | <div class="section" id="extension-protocol-for-bittorrent"> |
|---|
| 33 | <h1>extension protocol for bittorrent</h1> |
|---|
| 34 | <p>The intention of this protocol is to provide a simple and thin transport |
|---|
| 35 | for extensions to the bittorrent protocol. Supporting this protocol makes |
|---|
| 36 | it easy to add new extensions without interfering with the standard |
|---|
| 37 | bittorrent protocol or clients that don't support this extension or the |
|---|
| 38 | one you want to add.</p> |
|---|
| 39 | <p>To advertise to other clients that you support, one bit from the reserved |
|---|
| 40 | bytes is used.</p> |
|---|
| 41 | <p>The bit selected for the extension protocol is bit 20 from the right (counting |
|---|
| 42 | starts at 0). So (reserved_byte[5] & 0x10) is the expression to use for checking |
|---|
| 43 | if the client supports extended messaging.</p> |
|---|
| 44 | <p>Once support for the protocol is established, the client is supposed to |
|---|
| 45 | support 1 new message:</p> |
|---|
| 46 | <table border="1" class="docutils"> |
|---|
| 47 | <colgroup> |
|---|
| 48 | <col width="86%" /> |
|---|
| 49 | <col width="14%" /> |
|---|
| 50 | </colgroup> |
|---|
| 51 | <thead valign="bottom"> |
|---|
| 52 | <tr><th class="head">name</th> |
|---|
| 53 | <th class="head">id</th> |
|---|
| 54 | </tr> |
|---|
| 55 | </thead> |
|---|
| 56 | <tbody valign="top"> |
|---|
| 57 | <tr><td><tt class="docutils literal"><span class="pre">extended</span></tt></td> |
|---|
| 58 | <td>20</td> |
|---|
| 59 | </tr> |
|---|
| 60 | </tbody> |
|---|
| 61 | </table> |
|---|
| 62 | <p>This message is sent as any other bittorrent message, with a 4 byte length |
|---|
| 63 | prefix and a single byte identifying the message (the single byte being 20 |
|---|
| 64 | in this case). At the start of the payload of the message, is a single byte |
|---|
| 65 | message identifier. This identifier can refer to different extension messages |
|---|
| 66 | and only one ID is specified, 0. If the ID is 0, the message is a handshake |
|---|
| 67 | message which is described below. The layout of a general <tt class="docutils literal"><span class="pre">extended</span></tt> message |
|---|
| 68 | follows (including the message headers used by the bittorrent protocol):</p> |
|---|
| 69 | <table border="1" class="docutils"> |
|---|
| 70 | <colgroup> |
|---|
| 71 | <col width="15%" /> |
|---|
| 72 | <col width="85%" /> |
|---|
| 73 | </colgroup> |
|---|
| 74 | <thead valign="bottom"> |
|---|
| 75 | <tr><th class="head">size</th> |
|---|
| 76 | <th class="head">description</th> |
|---|
| 77 | </tr> |
|---|
| 78 | </thead> |
|---|
| 79 | <tbody valign="top"> |
|---|
| 80 | <tr><td>uint32_t</td> |
|---|
| 81 | <td>length prefix. Specifies the number of bytes for the |
|---|
| 82 | entire message. (Big endian)</td> |
|---|
| 83 | </tr> |
|---|
| 84 | <tr><td>uint8_t</td> |
|---|
| 85 | <td>bittorrent message ID, = 20</td> |
|---|
| 86 | </tr> |
|---|
| 87 | <tr><td>uint8_t</td> |
|---|
| 88 | <td>extended message ID. 0 = handshake, >0 = extended |
|---|
| 89 | message as specified by the handshake.</td> |
|---|
| 90 | </tr> |
|---|
| 91 | </tbody> |
|---|
| 92 | </table> |
|---|
| 93 | <div class="section" id="handshake-message"> |
|---|
| 94 | <h2>handshake message</h2> |
|---|
| 95 | <p>The payload of the handshake message is a bencoded dictionary. All items |
|---|
| 96 | in the dictionary are optional. Any unknown names should be ignored |
|---|
| 97 | by the client. All parts of the dictionary are case sensitive. |
|---|
| 98 | This is the defined item in the dictionary:</p> |
|---|
| 99 | <table border="1" class="docutils"> |
|---|
| 100 | <colgroup> |
|---|
| 101 | <col width="11%" /> |
|---|
| 102 | <col width="89%" /> |
|---|
| 103 | </colgroup> |
|---|
| 104 | <thead valign="bottom"> |
|---|
| 105 | <tr><th class="head">name</th> |
|---|
| 106 | <th class="head">description</th> |
|---|
| 107 | </tr> |
|---|
| 108 | </thead> |
|---|
| 109 | <tbody valign="top"> |
|---|
| 110 | <tr><td>m</td> |
|---|
| 111 | <td><p class="first">Dictionary of supported extension messages which maps |
|---|
| 112 | names of extensions to an extended message ID for each |
|---|
| 113 | extension message. The only requirement on these IDs |
|---|
| 114 | is that no extension message share the same one. Setting |
|---|
| 115 | an extension number to zero means that the extension is |
|---|
| 116 | not supported/disabled. The client should ignore any |
|---|
| 117 | extension names it doesn't recognize.</p> |
|---|
| 118 | <p class="last">The extension message IDs are the IDs used to send the |
|---|
| 119 | extension messages to the peer sending this handshake. |
|---|
| 120 | i.e. The IDs are local to this particular peer.</p> |
|---|
| 121 | </td> |
|---|
| 122 | </tr> |
|---|
| 123 | </tbody> |
|---|
| 124 | </table> |
|---|
| 125 | <p>Here are some other items that an implementation may choose to support:</p> |
|---|
| 126 | <table border="1" class="docutils"> |
|---|
| 127 | <colgroup> |
|---|
| 128 | <col width="12%" /> |
|---|
| 129 | <col width="88%" /> |
|---|
| 130 | </colgroup> |
|---|
| 131 | <thead valign="bottom"> |
|---|
| 132 | <tr><th class="head">name</th> |
|---|
| 133 | <th class="head">description</th> |
|---|
| 134 | </tr> |
|---|
| 135 | </thead> |
|---|
| 136 | <tbody valign="top"> |
|---|
| 137 | <tr><td>p</td> |
|---|
| 138 | <td>Local TCP listen port. Allows each side to learn about |
|---|
| 139 | the TCP port number of the other side. Note that there is |
|---|
| 140 | no need for the receiving side of the connection to send |
|---|
| 141 | this extension message, since its port number is already |
|---|
| 142 | known.</td> |
|---|
| 143 | </tr> |
|---|
| 144 | <tr><td>v</td> |
|---|
| 145 | <td>Client name and version (as a utf-8 string). |
|---|
| 146 | This is a much more reliable way of identifying the |
|---|
| 147 | client than relying on the peer id encoding.</td> |
|---|
| 148 | </tr> |
|---|
| 149 | <tr><td>yourip</td> |
|---|
| 150 | <td>A string containing the compact representation of the ip |
|---|
| 151 | address this peer sees you as. i.e. this is the |
|---|
| 152 | receiver's external ip address (no port is included). |
|---|
| 153 | This may be either an IPv4 (4 bytes) or an IPv6 |
|---|
| 154 | (16 bytes) address.</td> |
|---|
| 155 | </tr> |
|---|
| 156 | <tr><td>ipv6</td> |
|---|
| 157 | <td>If this peer has an IPv6 interface, this is the compact |
|---|
| 158 | representation of that address (16 bytes). The client may |
|---|
| 159 | prefer to connect back via the IPv6 address.</td> |
|---|
| 160 | </tr> |
|---|
| 161 | <tr><td>ipv4</td> |
|---|
| 162 | <td>If this peer has an IPv4 interface, this is the compact |
|---|
| 163 | representation of that address (4 bytes). The client may |
|---|
| 164 | prefer to connect back via this interface.</td> |
|---|
| 165 | </tr> |
|---|
| 166 | <tr><td>reqq</td> |
|---|
| 167 | <td>An integer, the number of outstanding request messages |
|---|
| 168 | this client supports without dropping any. The default in |
|---|
| 169 | in libtorrent is 250.</td> |
|---|
| 170 | </tr> |
|---|
| 171 | </tbody> |
|---|
| 172 | </table> |
|---|
| 173 | <p>The handshake dictionary could also include extended handshake |
|---|
| 174 | information, such as support for encrypted headers or anything |
|---|
| 175 | imaginable.</p> |
|---|
| 176 | <p>An example of what the payload of a handshake message could look like:</p> |
|---|
| 177 | <table border="1" class="docutils"> |
|---|
| 178 | <colgroup> |
|---|
| 179 | <col width="36%" /> |
|---|
| 180 | <col width="64%" /> |
|---|
| 181 | </colgroup> |
|---|
| 182 | <thead valign="bottom"> |
|---|
| 183 | <tr><th class="head" colspan="2">Dictionary</th> |
|---|
| 184 | </tr> |
|---|
| 185 | </thead> |
|---|
| 186 | <tbody valign="top"> |
|---|
| 187 | <tr><td><tt class="docutils literal"><span class="pre">m</span></tt></td> |
|---|
| 188 | <td><table border="1" class="first last docutils"> |
|---|
| 189 | <colgroup> |
|---|
| 190 | <col width="88%" /> |
|---|
| 191 | <col width="12%" /> |
|---|
| 192 | </colgroup> |
|---|
| 193 | <thead valign="bottom"> |
|---|
| 194 | <tr><th class="head" colspan="2">Dictionary</th> |
|---|
| 195 | </tr> |
|---|
| 196 | </thead> |
|---|
| 197 | <tbody valign="top"> |
|---|
| 198 | <tr><td><tt class="docutils literal"><span class="pre">LT_metadata</span></tt></td> |
|---|
| 199 | <td>1</td> |
|---|
| 200 | </tr> |
|---|
| 201 | <tr><td><tt class="docutils literal"><span class="pre">ut_pex</span></tt></td> |
|---|
| 202 | <td>2</td> |
|---|
| 203 | </tr> |
|---|
| 204 | </tbody> |
|---|
| 205 | </table> |
|---|
| 206 | </td> |
|---|
| 207 | </tr> |
|---|
| 208 | <tr><td><tt class="docutils literal"><span class="pre">p</span></tt></td> |
|---|
| 209 | <td>6881</td> |
|---|
| 210 | </tr> |
|---|
| 211 | <tr><td><tt class="docutils literal"><span class="pre">v</span></tt></td> |
|---|
| 212 | <td>"µTorrent 1.2"</td> |
|---|
| 213 | </tr> |
|---|
| 214 | </tbody> |
|---|
| 215 | </table> |
|---|
| 216 | <p>and in the encoded form:</p> |
|---|
| 217 | <p><tt class="docutils literal"><span class="pre">d1:md11:LT_metadatai1e6:µT_PEXi2ee1:pi6881e1:v13:\xc2\xb5Torrent</span> <span class="pre">1.2e</span></tt></p> |
|---|
| 218 | <p>To make sure the extension names do not collide by mistake, they should be |
|---|
| 219 | prefixed with the two (or one) character code that is used to identify the |
|---|
| 220 | client that introduced the extension. This applies for both the names of |
|---|
| 221 | extension messages, and for any additional information put inside the |
|---|
| 222 | top-level dictionary. All one and two byte identifiers are invalid to use |
|---|
| 223 | unless defined by this specification.</p> |
|---|
| 224 | <p>This message should be sent immediately after the standard bittorrent handshake |
|---|
| 225 | to any peer that supports this extension protocol. It is valid to send the |
|---|
| 226 | handshake message more than once during the lifetime of a connection, |
|---|
| 227 | the sending client should not be disconnected. An implementation may choose |
|---|
| 228 | to ignore the subsequent handshake messages (or parts of them).</p> |
|---|
| 229 | <p>Subsequent handshake messages can be used to enable/disable extensions |
|---|
| 230 | without restarting the connection. If a peer supports changing extensions |
|---|
| 231 | at run time, it should note that the <tt class="docutils literal"><span class="pre">m</span></tt> dictionary is additive. |
|---|
| 232 | It's enough that it contains the actual <em>CHANGES</em> to the extension list. |
|---|
| 233 | To disable the support for <tt class="docutils literal"><span class="pre">LT_metadata</span></tt> at run-time, without affecting |
|---|
| 234 | any other extensions, this message should be sent: |
|---|
| 235 | <tt class="docutils literal"><span class="pre">d11:LT_metadatai0ee</span></tt>. |
|---|
| 236 | As specified above, the value 0 is used to turn off an extension.</p> |
|---|
| 237 | <p>The extension IDs must be stored for every peer, becuase every peer may have |
|---|
| 238 | different IDs for the same extension.</p> |
|---|
| 239 | <p>This specification, deliberately, does not specify any extensions such as |
|---|
| 240 | peer-exchange or metadata exchange. This protocol is merely a transport |
|---|
| 241 | for the actual extensions to the bittorrent protocol and the extensions |
|---|
| 242 | named in the example above (such as <tt class="docutils literal"><span class="pre">p</span></tt>) are just examples of possible |
|---|
| 243 | extensions.</p> |
|---|
| 244 | </div> |
|---|
| 245 | <div class="section" id="rationale"> |
|---|
| 246 | <h2>rationale</h2> |
|---|
| 247 | <p>The reason why the extension messages' IDs would be defined in the handshake |
|---|
| 248 | is to avoid having a global registry of message IDs. Instead the names of the |
|---|
| 249 | extension messages requires unique names, which is much easier to do without |
|---|
| 250 | a global registry. The convention is to use a two letter prefix on the |
|---|
| 251 | extension message names, the prefix would identify the client first |
|---|
| 252 | implementing the extension message. e.g. <tt class="docutils literal"><span class="pre">LT_metadata</span></tt> is implemented by |
|---|
| 253 | libtorrent, and hence it has the <tt class="docutils literal"><span class="pre">LT</span></tt> prefix.</p> |
|---|
| 254 | <p>If the client supporting the extensions can decide which numbers the messages |
|---|
| 255 | it receives will have, it means they are constants within that client. i.e. |
|---|
| 256 | they can be used in <tt class="docutils literal"><span class="pre">switch</span></tt> statements. It's easy for the other end to |
|---|
| 257 | store an array with the ID's we expect for each message and use that for |
|---|
| 258 | lookups each time it sends an extension message.</p> |
|---|
| 259 | <p>The reason for having a dictionary instead of having an array (using |
|---|
| 260 | implicitly assigned index numbers to the extensions) is that if a client |
|---|
| 261 | want to disable some extensions, the ID numbers would change, and it wouldn't |
|---|
| 262 | be able to use constants (and hence, not use them in a <tt class="docutils literal"><span class="pre">switch</span></tt>). If the |
|---|
| 263 | messages IDs would map directly to bittorrent message IDs, It would also make |
|---|
| 264 | it possible to map extensions in the handshake to existing extensions with |
|---|
| 265 | fixed message IDs.</p> |
|---|
| 266 | <p>The reasoning behind having a single byte as extended message identifier is |
|---|
| 267 | to follow the the bittorrent spec. with its single byte message identifiers. |
|---|
| 268 | It is also considered to be enough. It won't limit the total number of |
|---|
| 269 | extensions, only the number of extensions used simultaneously.</p> |
|---|
| 270 | <p>The reason for using single byte identifiers for the standardized handshake |
|---|
| 271 | identifiers is 1) The mainline DHT uses single byte identifiers. 2) Saves |
|---|
| 272 | bandwidth. The only advantage of longer messages is that it makes the |
|---|
| 273 | protocol more readable for a human, but the BT protocol wasn't designed to |
|---|
| 274 | be a human readable protocol, so why bother.</p> |
|---|
| 275 | </div> |
|---|
| 276 | <div class="section" id="authors"> |
|---|
| 277 | <h2>authors</h2> |
|---|
| 278 | <div class="line-block"> |
|---|
| 279 | <div class="line"><a class="reference" href="mailto:arvid%40bittorrent.com">Arvid Norberg</a></div> |
|---|
| 280 | <div class="line"><a class="reference" href="ludde@utorrent.com">Ludvig Strigeus</a></div> |
|---|
| 281 | </div> |
|---|
| 282 | </div> |
|---|
| 283 | </div> |
|---|
| 284 | |
|---|
| 285 | </div> |
|---|
| 286 | <div id="footer"> |
|---|
| 287 | <hr/> |
|---|
| 288 | <p>Copyright 2006 BitTorrent.org</p> |
|---|
| 289 | </div> |
|---|
| 290 | |
|---|
| 291 | </div> |
|---|
| 292 | </body> |
|---|
| 293 | </html> |
|---|