Handshake

class Handshake(val infoHash: Sha1Hash, val peerId: Sha1Hash, val reserved: ByteArray)

The fixed BitTorrent peer-protocol handshake. This is the pure-Kotlin port of bt_peer_connection::write_handshake() (src/bt_peer_connection.cpp) and the receive-side parsing in bt_peer_connection::on_receive/recv_handshake.

Defined by BEP-3. The wire layout is exactly 68 bytes:

<pstrlen=19><pstr="BitTorrent protocol"><8 reserved bytes><20-byte info_hash><20-byte peer_id>
1 + 19 + 8 + 20 + 20 = 68

The 8 reserved bytes carry capability flags negotiated by ANDing the two peers' reserved blocks. libtorrent sets, in write_handshake():

  • reserved[7] |= 0x01: DHT support (BEP-5),

  • reserved[7] |= 0x04: Fast extension (BEP-6),

  • reserved[5] |= 0x10: Extension protocol (BEP-10),

  • reserved[7] |= 0x10: v2 upgrade (BEP-52), set only for a v1 peer in a hybrid torrent to advertise willingness to upgrade to the v2 protocol.

(reserved[7] is the last byte; reserved[5] is the 6th byte / index 5.)

Constructors

Link copied to clipboard
constructor(infoHash: Sha1Hash, peerId: Sha1Hash, reserved: ByteArray)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The 20-byte info-hash of the torrent both peers claim to be swarming.

Link copied to clipboard

The remote (or local) 20-byte peer id.

Link copied to clipboard

The raw 8 reserved bytes exactly as they appeared on the wire.

Link copied to clipboard

True if the peer set the DHT bit (reserved[7] & 0x01, BEP-5).

Link copied to clipboard

True if the peer set the extension-protocol bit (reserved[5] & 0x10, BEP-10).

Link copied to clipboard

True if the peer set the Fast-extension bit (reserved[7] & 0x04, BEP-6).

Link copied to clipboard

True if the peer set the v2-upgrade bit (reserved[7] & 0x10, BEP-52). For a v1 connection this signals the peer is willing to upgrade to the BitTorrent v2 protocol on a hybrid torrent.

Functions

Link copied to clipboard

Re-encode this handshake to its canonical 68-byte form.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
open override fun toString(): String