| 1 | BEP: 10 |
|---|
| 2 | Title: Extension Protocol |
|---|
| 3 | Version: $Revision$ |
|---|
| 4 | Last-Modified: $Date$ |
|---|
| 5 | Author: Arvid Norberg <arvid@bittorrent.com>, Ludvig Strigeus <bittorrent@strigeus.com>, Greg Hazel <greg@bittorrent.com> |
|---|
| 6 | Status: Draft |
|---|
| 7 | Type: Standards Track |
|---|
| 8 | Created: 31-Jan-2008 |
|---|
| 9 | Post-History: |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | The intention of this protocol is to provide a simple and thin transport |
|---|
| 13 | for extensions to the bittorrent protocol. Supporting this protocol makes |
|---|
| 14 | it easy to add new extensions without interfering with the standard |
|---|
| 15 | bittorrent protocol or clients that don't support this extension or the |
|---|
| 16 | one you want to add. |
|---|
| 17 | |
|---|
| 18 | To advertise to other clients that you support, one bit from the reserved |
|---|
| 19 | bytes is used. |
|---|
| 20 | |
|---|
| 21 | The bit selected for the extension protocol is bit 20 from the right (counting |
|---|
| 22 | starts at 0). So (reserved_byte[5] & 0x10) is the expression to use for checking |
|---|
| 23 | if the client supports extended messaging. |
|---|
| 24 | |
|---|
| 25 | Once support for the protocol is established, the client is supposed to |
|---|
| 26 | support 1 new message: |
|---|
| 27 | |
|---|
| 28 | +------------------------+----+ |
|---|
| 29 | |name | id | |
|---|
| 30 | +========================+====+ |
|---|
| 31 | |``extended`` | 20 | |
|---|
| 32 | +------------------------+----+ |
|---|
| 33 | |
|---|
| 34 | This message is sent as any other bittorrent message, with a 4 byte length |
|---|
| 35 | prefix and a single byte identifying the message (the single byte being 20 |
|---|
| 36 | in this case). At the start of the payload of the message, is a single byte |
|---|
| 37 | message identifier. This identifier can refer to different extension messages |
|---|
| 38 | and only one ID is specified, 0. If the ID is 0, the message is a handshake |
|---|
| 39 | message which is described below. The layout of a general ``extended`` message |
|---|
| 40 | follows (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 | |
|---|
| 55 | handshake message |
|---|
| 56 | ================= |
|---|
| 57 | |
|---|
| 58 | The payload of the handshake message is a bencoded dictionary. All items |
|---|
| 59 | in the dictionary are optional. Any unknown names should be ignored |
|---|
| 60 | by the client. All parts of the dictionary are case sensitive. |
|---|
| 61 | This 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 | |
|---|
| 80 | Here 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 | |
|---|
| 114 | The handshake dictionary could also include extended handshake |
|---|
| 115 | information, such as support for encrypted headers or anything |
|---|
| 116 | imaginable. |
|---|
| 117 | |
|---|
| 118 | An 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 | |
|---|
| 137 | and in the encoded form: |
|---|
| 138 | |
|---|
| 139 | ``d1:md11:LT_metadatai1e6:µT_PEXi2ee1:pi6881e1:v13:\xc2\xb5Torrent 1.2e`` |
|---|
| 140 | |
|---|
| 141 | To make sure the extension names do not collide by mistake, they should be |
|---|
| 142 | prefixed with the two (or one) character code that is used to identify the |
|---|
| 143 | client that introduced the extension. This applies for both the names of |
|---|
| 144 | extension messages, and for any additional information put inside the |
|---|
| 145 | top-level dictionary. All one and two byte identifiers are invalid to use |
|---|
| 146 | unless defined by this specification. |
|---|
| 147 | |
|---|
| 148 | This message should be sent immediately after the standard bittorrent handshake |
|---|
| 149 | to any peer that supports this extension protocol. It is valid to send the |
|---|
| 150 | handshake message more than once during the lifetime of a connection, |
|---|
| 151 | the sending client should not be disconnected. An implementation may choose |
|---|
| 152 | to ignore the subsequent handshake messages (or parts of them). |
|---|
| 153 | |
|---|
| 154 | Subsequent handshake messages can be used to enable/disable extensions |
|---|
| 155 | without restarting the connection. If a peer supports changing extensions |
|---|
| 156 | at run time, it should note that the ``m`` dictionary is additive. |
|---|
| 157 | It's enough that it contains the actual *CHANGES* to the extension list. |
|---|
| 158 | To disable the support for ``LT_metadata`` at run-time, without affecting |
|---|
| 159 | any other extensions, this message should be sent: |
|---|
| 160 | ``d11:LT_metadatai0ee``. |
|---|
| 161 | As specified above, the value 0 is used to turn off an extension. |
|---|
| 162 | |
|---|
| 163 | The extension IDs must be stored for every peer, becuase every peer may have |
|---|
| 164 | different IDs for the same extension. |
|---|
| 165 | |
|---|
| 166 | This specification, deliberately, does not specify any extensions such as |
|---|
| 167 | peer-exchange or metadata exchange. This protocol is merely a transport |
|---|
| 168 | for the actual extensions to the bittorrent protocol and the extensions |
|---|
| 169 | named in the example above (such as ``p``) are just examples of possible |
|---|
| 170 | extensions. |
|---|
| 171 | |
|---|
| 172 | rationale |
|---|
| 173 | ========= |
|---|
| 174 | |
|---|
| 175 | The reason why the extension messages' IDs would be defined in the handshake |
|---|
| 176 | is to avoid having a global registry of message IDs. Instead the names of the |
|---|
| 177 | extension messages requires unique names, which is much easier to do without |
|---|
| 178 | a global registry. The convention is to use a two letter prefix on the |
|---|
| 179 | extension message names, the prefix would identify the client first |
|---|
| 180 | implementing the extension message. e.g. ``LT_metadata`` is implemented by |
|---|
| 181 | libtorrent, and hence it has the ``LT`` prefix. |
|---|
| 182 | |
|---|
| 183 | If the client supporting the extensions can decide which numbers the messages |
|---|
| 184 | it receives will have, it means they are constants within that client. i.e. |
|---|
| 185 | they can be used in ``switch`` statements. It's easy for the other end to |
|---|
| 186 | store an array with the ID's we expect for each message and use that for |
|---|
| 187 | lookups each time it sends an extension message. |
|---|
| 188 | |
|---|
| 189 | The reason for having a dictionary instead of having an array (using |
|---|
| 190 | implicitly assigned index numbers to the extensions) is that if a client |
|---|
| 191 | want to disable some extensions, the ID numbers would change, and it wouldn't |
|---|
| 192 | be able to use constants (and hence, not use them in a ``switch``). If the |
|---|
| 193 | messages IDs would map directly to bittorrent message IDs, It would also make |
|---|
| 194 | it possible to map extensions in the handshake to existing extensions with |
|---|
| 195 | fixed message IDs. |
|---|
| 196 | |
|---|
| 197 | The reasoning behind having a single byte as extended message identifier is |
|---|
| 198 | to follow the the bittorrent spec. with its single byte message identifiers. |
|---|
| 199 | It is also considered to be enough. It won't limit the total number of |
|---|
| 200 | extensions, only the number of extensions used simultaneously. |
|---|
| 201 | |
|---|
| 202 | The reason for using single byte identifiers for the standardized handshake |
|---|
| 203 | identifiers is 1) The mainline DHT uses single byte identifiers. 2) Saves |
|---|
| 204 | bandwidth. The only advantage of longer messages is that it makes the |
|---|
| 205 | protocol more readable for a human, but the BT protocol wasn't designed to |
|---|
| 206 | be a human readable protocol, so why bother. |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | Copyright |
|---|
| 210 | ========= |
|---|
| 211 | |
|---|
| 212 | This 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: |
|---|