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