BitTorrent Bitfield Exchange Explained
The bitfield exchange is an essential communication phase in the BitTorrent protocol where two newly connected peers inform each other which specific pieces of a torrent file they possess. Occurring immediately after the initial connection handshake, this exchange allows both clients to quickly establish a baseline of available data, calculate piece availability across the swarm, and determine whether they should express interest in downloading from one another.
Timing and Role in the Handshake
Once two peers complete their TCP and protocol-level handshakes, they
enter the messaging phase. If a peer possesses at least one piece of the
torrent, the very first message it typically transmits is the
bitfield message. Sending a bitfield is optional; clients
that possess zero pieces (complete downloaders starting from scratch)
usually omit it entirely rather than sending an all-zero payload.
Structure of the Bitfield Payload
The bitfield message consists of a standard BitTorrent
message length prefix, a single-byte message identifier
(ID 5), and a payload formatted as a packed byte array:
- Bit Mapping: Each bit in the payload corresponds
directly to a piece index in the torrent, starting with the most
significant bit of the first byte representing piece index
0. - Values: A bit set to
1indicates the peer has verified and completed that piece, while a0indicates the piece is missing. - Padding: Because piece counts rarely divide evenly
into full 8-bit bytes, any unused bits at the end of the final byte are
set to
0.
Validation and Error Handling
Upon receiving a bitfield, the client validates the payload against
the metadata provided by the torrent’s .torrent file:
- Length Check: The receiving peer calculates the expected byte length based on the total piece count. If the bitfield length is incorrect, the connection is considered corrupt and terminated immediately.
- Spare Bit Verification: The recipient verifies that
any trailing padding bits are strictly set to
0. If any trailing bits are set to1, the client drops the connection to prevent protocol errors or malicious activity.
State Updating and Interest Determination
Once validated, the receiving peer updates its internal map of what the remote peer owns. It then compares this map against its own missing pieces:
- Expressing Interest: If the remote peer has at
least one piece that the local peer needs, the local peer marks the
connection state as “interested” and sends an
interestedmessage (ID 2). - Lack of Interest: If the remote peer possesses no
unique pieces needed by the local client, the local peer sets its state
to “not interested” and sends a
not interestedmessage (ID 3).
Transition to Real-Time Updates
The bitfield message is only sent once per peer
connection. Any pieces that a peer completes after this initial exchange
are not communicated via a new bitfield; instead, they are announced
individually in real time using lightweight have messages
(ID 4), which contain only the integer index of the newly
completed piece.