Home > Specs > BitTorrent > Peer Connections
Bitfield Management
The bitfield is a series of bits, often represented as a string, that indicates whether the piece at a particular index is available on the peer.
After a peer connection is made, and the initial handshake is complete, each peer should send a bitfield message.
If the remote peer supports the fast extension, as indicated in the initial connection handshake flags, it may be sent a HAVEALL or HAVENONE message instead of a completely full or completely blank bitfield.
The bitfield message is the message type 5 (one byte), followed directly by the bitfield. The lowest bit of the first character represents piece index 0.
The HAVEALL message is just the message type 14 (one byte). The HAVENONE message is just the message type 15 (one byte).
After the initial bitfield message, during regular operation, peers must send a HAVE message when new pieces are made available, and an lt_donthave
extension message if a piece is lost (which should be rare). If the lt_donthave extension message isn't supported by the remote peer, there is no way to indicate to them that a piece is no longer available, aside from closing the connection and re-connecting.
The HAVE message is the message type 4 (one byte), followed by the piece index as a 32-bit big-endian integer.
The lt_donthave
extension message is the message type 20 (one byte), followed by the message sub-type number (one byte) for lt_donthave that was indicated in the remote's initial
extended handshake, and then the piece index as a 32-bit big-endian integer.