Companion

object Companion

Properties

Link copied to clipboard
const val DHT_FLAG: Int = 1

DHT-support flag, OR'd into reserved byte 7 (BEP-5).

Link copied to clipboard
const val EXTENSION_FLAG: Int = 16

Extension-protocol flag, OR'd into reserved byte 5 (BEP-10).

Link copied to clipboard
const val FAST_FLAG: Int = 4

Fast-extension flag, OR'd into reserved byte 7 (BEP-6).

Link copied to clipboard
const val LENGTH: Int

Total on-the-wire size of a handshake: 1 + 19 + 8 + 20 + 20.

Link copied to clipboard

The BitTorrent v1 protocol identifier string ("pstr").

Link copied to clipboard
const val PSTRLEN: Int = 19

The single length-prefix byte: the length of PROTOCOL_STRING.

Link copied to clipboard
const val V2_FLAG: Int = 16

v2-upgrade flag, OR'd into reserved byte 7 (BEP-52).

Functions

Link copied to clipboard
fun buildReserved(dht: Boolean = true, fast: Boolean = true, extended: Boolean = true, v2: Boolean = false): ByteArray

Build an 8-byte reserved block advertising exactly the chosen capabilities. Mirrors the OR-into-reserved logic of write_handshake():

Link copied to clipboard
fun decode(bytes: ByteArray): Handshake?

Parse a 68-byte handshake. Returns null if bytes is shorter than LENGTH, if the length prefix is not 19, or if the protocol string does not match, which means the peer is not speaking BitTorrent v1. Bytes beyond LENGTH are ignored (they belong to subsequent messages on the stream).

Link copied to clipboard

Build the 8-byte reserved block libtorrent emits by default: DHT + Fast + Extension-protocol bits set (matching write_handshake() with all features compiled in, minus the v2-upgrade bit which depends on torrent state).

Link copied to clipboard
fun encode(infoHash: Sha1Hash, peerId: Sha1Hash, reserved: ByteArray = defaultReserved()): ByteArray

Encode a handshake. reserved must be exactly 8 bytes; it defaults to defaultReserved. Mirrors the byte order written by write_handshake().