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