BitTorrent Extension Handshake Message ID

This article provides a concise overview of the message identifiers used in the BitTorrent Extension Protocol (BEP 10). It details the primary message ID assigned to extended messaging in the core peer-to-peer wire protocol, the specific extended message ID used for the extension handshake, and how these IDs facilitate communication between BitTorrent clients.

Core Message ID for Extended Messages

In the standard BitTorrent peer wire protocol, standard messages use single-byte identifiers ranging from 0 to 9 (such as choke, unchoke, interested, and piece). Under the Extension Protocol specified in BEP 10, the message ID assigned to all extended messages is 20.

When a peer sends an extended message, the payload structure begins with the standard BitTorrent framing: * Length prefix: 4-byte integer specifying the total length of the message. * Message ID: 1-byte value set to 20 (denoting an extended message).

Extended Message ID for the Handshake

Within the payload of message 20, the first byte represents the extended message ID, which specifies the sub-type of the extension being used.

For the extension handshake, the assigned extended message ID is 0.

Therefore, the full packet for an extension handshake is structured as follows: 1. <Length Prefix> (4 bytes) 2. <Standard Message ID> = 20 (1 byte) 3. <Extended Message ID> = 0 (1 byte) 4. <Bencoded Dictionary> (variable length)

Purpose of the Handshake

The extension handshake (message ID 20, sub-ID 0) is used immediately after the standard BitTorrent connection is established. It contains a bencoded dictionary where peers advertise the extensions they support (such as ut_pex for Peer Exchange or ut_metadata for magnet links) and dynamically map those extensions to custom integer IDs (typically 1 through 255) for subsequent communication.