root/dotorg/trunk/html/beps/bep_0010.rst

Revision 11031, 10.9 KB (checked in by dave, 2 years ago)

Specify that BEP documents are in the public domain.

Line 
1BEP: 10
2Title: Extension Protocol
3Version: $Revision$
4Last-Modified: $Date$
5Author:  Arvid Norberg <arvid@bittorrent.com>, Ludvig Strigeus <bittorrent@strigeus.com>, Greg Hazel <greg@bittorrent.com>
6Status:  Draft
7Type:    Standards Track
8Created: 31-Jan-2008
9Post-History:
10
11
12The intention of this protocol is to provide a simple and thin transport
13for extensions to the bittorrent protocol. Supporting this protocol makes
14it easy to add new extensions without interfering with the standard
15bittorrent protocol or clients that don't support this extension or the
16one you want to add.
17
18To advertise to other clients that you support, one bit from the reserved
19bytes is used.
20
21The bit selected for the extension protocol is bit 20 from the right (counting
22starts at 0). So (reserved_byte[5] & 0x10) is the expression to use for checking
23if the client supports extended messaging.
24
25Once support for the protocol is established, the client is supposed to
26support 1 new message:
27
28+------------------------+----+
29|name                    | id |
30+========================+====+
31|``extended``            | 20 |
32+------------------------+----+
33
34This message is sent as any other bittorrent message, with a 4 byte length
35prefix and a single byte identifying the message (the single byte being 20
36in this case). At the start of the payload of the message, is a single byte
37message identifier. This identifier can refer to different extension messages
38and only one ID is specified, 0. If the ID is 0, the message is a handshake
39message which is described below. The layout of a general ``extended`` message
40follows (including the message headers used by the bittorrent protocol):
41
42+----------+---------------------------------------------------------+
43| size     | description                                             |
44+==========+=========================================================+
45| uint32_t | length prefix. Specifies the number of bytes for the    |
46|          | entire message. (Big endian)                            |
47+----------+---------------------------------------------------------+
48| uint8_t  | bittorrent message ID, = 20                             |
49+----------+---------------------------------------------------------+
50| uint8_t  | extended message ID. 0 = handshake, >0 = extended       |
51|          | message as specified by the handshake.                  |
52+----------+---------------------------------------------------------+
53
54
55handshake message
56=================
57
58The payload of the handshake message is a bencoded dictionary. All items
59in the dictionary are optional. Any unknown names should be ignored
60by the client. All parts of the dictionary are case sensitive.
61This is the defined item in the dictionary:
62
63+-------+-----------------------------------------------------------+
64| name  | description                                               |
65+=======+===========================================================+
66| m     | Dictionary of supported extension messages which maps     |
67|       | names of extensions to an extended message ID for each    |
68|       | extension message. The only requirement on these IDs      |
69|       | is that no extension message share the same one. Setting  |
70|       | an extension number to zero means that the extension is   |
71|       | not supported/disabled. The client should ignore any      |
72|       | extension names it doesn't recognize.                     |
73|       |                                                           |
74|       | The extension message IDs are the IDs used to send the    |
75|       | extension messages to the peer sending this handshake.    |
76|       | i.e. The IDs are local to this particular peer.           |
77+-------+-----------------------------------------------------------+
78
79
80Here are some other items that an implementation may choose to support:
81
82+--------+-----------------------------------------------------------+
83| name   | description                                               |
84+========+===========================================================+
85| p      | Local TCP listen port. Allows each side to learn about    |
86|        | the TCP port number of the other side. Note that there is |
87|        | no need for the receiving side of the connection to send  |
88|        | this extension message, since its port number is already  |
89|        | known.                                                    |
90+--------+-----------------------------------------------------------+
91| v      | Client name and version (as a utf-8 string).              |
92|        | This is a much more reliable way of identifying the       |
93|        | client than relying on the peer id encoding.              |
94+--------+-----------------------------------------------------------+
95| yourip | A string containing the compact representation of the ip  |
96|        | address this peer sees you as. i.e. this is the           |
97|        | receiver's external ip address (no port is included).     |
98|        | This may be either an IPv4 (4 bytes) or an IPv6           |
99|        | (16 bytes) address.                                       |
100+--------+-----------------------------------------------------------+
101| ipv6   | If this peer has an IPv6 interface, this is the compact   |
102|        | representation of that address (16 bytes). The client may |
103|        | prefer to connect back via the IPv6 address.              |
104+--------+-----------------------------------------------------------+
105| ipv4   | If this peer has an IPv4 interface, this is the compact   |
106|        | representation of that address (4 bytes). The client may  |
107|        | prefer to connect back via this interface.                |
108+--------+-----------------------------------------------------------+
109| reqq   | An integer, the number of outstanding request messages    |
110|        | this client supports without dropping any. The default in |
111|        | in libtorrent is 250.                                     |
112+--------+-----------------------------------------------------------+
113
114The handshake dictionary could also include extended handshake
115information, such as support for encrypted headers or anything
116imaginable.
117
118An example of what the payload of a handshake message could look like:
119
120+------------------------------------------------------+
121| Dictionary                                           |
122+===================+==================================+
123| ``m``             |  +--------------------------+    |
124|                   |  | Dictionary               |    |
125|                   |  +======================+===+    |
126|                   |  | ``LT_metadata``      | 1 |    |
127|                   |  +----------------------+---+    |
128|                   |  | ``ut_pex``           | 2 |    |
129|                   |  +----------------------+---+    |
130|                   |                                  |
131+-------------------+----------------------------------+
132| ``p``             | 6881                             |
133+-------------------+----------------------------------+
134| ``v``             | "µTorrent 1.2"                   |
135+-------------------+----------------------------------+
136
137and in the encoded form:
138
139``d1:md11:LT_metadatai1e6:µT_PEXi2ee1:pi6881e1:v13:\xc2\xb5Torrent 1.2e``
140
141To make sure the extension names do not collide by mistake, they should be
142prefixed with the two (or one) character code that is used to identify the
143client that introduced the extension. This applies for both the names of
144extension messages, and for any additional information put inside the
145top-level dictionary. All one and two byte identifiers are invalid to use
146unless defined by this specification.
147
148This message should be sent immediately after the standard bittorrent handshake
149to any peer that supports this extension protocol. It is valid to send the
150handshake message more than once during the lifetime of a connection,
151the sending client should not be disconnected. An implementation may choose
152to ignore the subsequent handshake messages (or parts of them).
153
154Subsequent handshake messages can be used to enable/disable extensions
155without restarting the connection. If a peer supports changing extensions
156at run time, it should note that the ``m`` dictionary is additive.
157It's enough that it contains the actual *CHANGES* to the extension list.
158To disable the support for ``LT_metadata`` at run-time, without affecting
159any other extensions, this message should be sent:
160``d11:LT_metadatai0ee``.
161As specified above, the value 0 is used to turn off an extension.
162
163The extension IDs must be stored for every peer, becuase every peer may have
164different IDs for the same extension.
165
166This specification, deliberately, does not specify any extensions such as
167peer-exchange or metadata exchange. This protocol is merely a transport
168for the actual extensions to the bittorrent protocol and the extensions
169named in the example above (such as ``p``) are just examples of possible
170extensions.
171
172rationale
173=========
174
175The reason why the extension messages' IDs would be defined in the handshake
176is to avoid having a global registry of message IDs. Instead the names of the
177extension messages requires unique names, which is much easier to do without
178a global registry. The convention is to use a two letter prefix on the
179extension message names, the prefix would identify the client first
180implementing the extension message. e.g. ``LT_metadata`` is implemented by
181libtorrent, and hence it has the ``LT`` prefix.
182
183If the client supporting the extensions can decide which numbers the messages
184it receives will have, it means they are constants within that client. i.e.
185they can be used in ``switch`` statements. It's easy for the other end to
186store an array with the ID's we expect for each message and use that for
187lookups each time it sends an extension message.
188
189The reason for having a dictionary instead of having an array (using
190implicitly assigned index numbers to the extensions) is that if a client
191want to disable some extensions, the ID numbers would change, and it wouldn't
192be able to use constants (and hence, not use them in a ``switch``). If the
193messages IDs would map directly to bittorrent message IDs, It would also make
194it possible to map extensions in the handshake to existing extensions with
195fixed message IDs.
196
197The reasoning behind having a single byte as extended message identifier is
198to follow the the bittorrent spec. with its single byte message identifiers.
199It is also considered to be enough. It won't limit the total number of
200extensions, only the number of extensions used simultaneously.
201
202The reason for using single byte identifiers for the standardized handshake
203identifiers is 1) The mainline DHT uses single byte identifiers. 2) Saves
204bandwidth. The only advantage of longer messages is that it makes the
205protocol more readable for a human, but the BT protocol wasn't designed to
206be a human readable protocol, so why bother.
207
208
209Copyright
210=========
211
212This document has been placed in the public domain.
213
214
215
216..
217   Local Variables:
218   mode: indented-text
219   indent-tabs-mode: nil
220   sentence-end-double-space: t
221   fill-column: 70
222   coding: utf-8
223   End:
Note: See TracBrowser for help on using the browser.