root / dotorg / v3 / html / extension_protocol.rst

Revision 9921, 10.6 kB (checked in by arvid, 13 months ago)

added drafts for metadata extension and extension protocol

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