root / dotorg / v3 / html / extension_protocol.html

Revision 9922, 11.7 kB (checked in by arvid, 13 months ago)

added the generated html as well

Line 
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
35for extensions to the bittorrent protocol. Supporting this protocol makes
36it easy to add new extensions without interfering with the standard
37bittorrent protocol or clients that don't support this extension or the
38one you want to add.</p>
39<p>To advertise to other clients that you support, one bit from the reserved
40bytes is used.</p>
41<p>The bit selected for the extension protocol is bit 20 from the right (counting
42starts at 0). So (reserved_byte[5] &amp; 0x10) is the expression to use for checking
43if the client supports extended messaging.</p>
44<p>Once support for the protocol is established, the client is supposed to
45support 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
63prefix and a single byte identifying the message (the single byte being 20
64in this case). At the start of the payload of the message, is a single byte
65message identifier. This identifier can refer to different extension messages
66and only one ID is specified, 0. If the ID is 0, the message is a handshake
67message which is described below. The layout of a general <tt class="docutils literal"><span class="pre">extended</span></tt> message
68follows (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
82entire 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, &gt;0 = extended
89message 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
96in the dictionary are optional. Any unknown names should be ignored
97by the client. All parts of the dictionary are case sensitive.
98This 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
112names of extensions to an extended message ID for each
113extension message. The only requirement on these IDs
114is that no extension message share the same one. Setting
115an extension number to zero means that the extension is
116not supported/disabled. The client should ignore any
117extension names it doesn't recognize.</p>
118<p class="last">The extension message IDs are the IDs used to send the
119extension messages to the peer sending this handshake.
120i.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
139the TCP port number of the other side. Note that there is
140no need for the receiving side of the connection to send
141this extension message, since its port number is already
142known.</td>
143</tr>
144<tr><td>v</td>
145<td>Client name and version (as a utf-8 string).
146This is a much more reliable way of identifying the
147client 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
151address this peer sees you as. i.e. this is the
152receiver's external ip address (no port is included).
153This 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
158representation of that address (16 bytes). The client may
159prefer 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
163representation of that address (4 bytes). The client may
164prefer to connect back via this interface.</td>
165</tr>
166<tr><td>reqq</td>
167<td>An integer, the number of outstanding request messages
168this client supports without dropping any. The default in
169in libtorrent is 250.</td>
170</tr>
171</tbody>
172</table>
173<p>The handshake dictionary could also include extended handshake
174information, such as support for encrypted headers or anything
175imaginable.</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>&quot;µTorrent 1.2&quot;</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
219prefixed with the two (or one) character code that is used to identify the
220client that introduced the extension. This applies for both the names of
221extension messages, and for any additional information put inside the
222top-level dictionary. All one and two byte identifiers are invalid to use
223unless defined by this specification.</p>
224<p>This message should be sent immediately after the standard bittorrent handshake
225to any peer that supports this extension protocol. It is valid to send the
226handshake message more than once during the lifetime of a connection,
227the sending client should not be disconnected. An implementation may choose
228to ignore the subsequent handshake messages (or parts of them).</p>
229<p>Subsequent handshake messages can be used to enable/disable extensions
230without restarting the connection. If a peer supports changing extensions
231at run time, it should note that the <tt class="docutils literal"><span class="pre">m</span></tt> dictionary is additive.
232It's enough that it contains the actual <em>CHANGES</em> to the extension list.
233To disable the support for <tt class="docutils literal"><span class="pre">LT_metadata</span></tt> at run-time, without affecting
234any other extensions, this message should be sent:
235<tt class="docutils literal"><span class="pre">d11:LT_metadatai0ee</span></tt>.
236As 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
238different IDs for the same extension.</p>
239<p>This specification, deliberately, does not specify any extensions such as
240peer-exchange or metadata exchange. This protocol is merely a transport
241for the actual extensions to the bittorrent protocol and the extensions
242named in the example above (such as <tt class="docutils literal"><span class="pre">p</span></tt>) are just examples of possible
243extensions.</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
248is to avoid having a global registry of message IDs. Instead the names of the
249extension messages requires unique names, which is much easier to do without
250a global registry. The convention is to use a two letter prefix on the
251extension message names, the prefix would identify the client first
252implementing the extension message. e.g. <tt class="docutils literal"><span class="pre">LT_metadata</span></tt> is implemented by
253libtorrent, 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
255it receives will have, it means they are constants within that client. i.e.
256they can be used in <tt class="docutils literal"><span class="pre">switch</span></tt> statements. It's easy for the other end to
257store an array with the ID's we expect for each message and use that for
258lookups each time it sends an extension message.</p>
259<p>The reason for having a dictionary instead of having an array (using
260implicitly assigned index numbers to the extensions) is that if a client
261want to disable some extensions, the ID numbers would change, and it wouldn't
262be able to use constants (and hence, not use them in a <tt class="docutils literal"><span class="pre">switch</span></tt>). If the
263messages IDs would map directly to bittorrent message IDs, It would also make
264it possible to map extensions in the handshake to existing extensions with
265fixed message IDs.</p>
266<p>The reasoning behind having a single byte as extended message identifier is
267to follow the the bittorrent spec. with its single byte message identifiers.
268It is also considered to be enough. It won't limit the total number of
269extensions, only the number of extensions used simultaneously.</p>
270<p>The reason for using single byte identifiers for the standardized handshake
271identifiers is 1) The mainline DHT uses single byte identifiers. 2) Saves
272bandwidth. The only advantage of longer messages is that it makes the
273protocol more readable for a human, but the BT protocol wasn't designed to
274be 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&#37;&#52;&#48;bittorrent&#46;com">Arvid Norberg</a></div>
280<div class="line"><a class="reference" href="ludde&#64;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>
Note: See TracBrowser for help on using the browser.