DhtMessage

sealed class DhtMessage

The KRPC message model and codec for the BitTorrent DHT: a pure-Kotlin port of the message-building and -parsing code spread across libtorrent's src/kademlia/node.cpp (incoming_request, the per-query response builders, and the single-bucket-refresh / ping query builders), find_data.cpp, get_peers.cpp, get_item.cpp, put_data.cpp and msg.cpp.

Every DHT packet is a bencoded dictionary with three or four top-level keys (BEP-5 "KRPC Protocol"):

  • t: the transaction id, an opaque byte string (libtorrent uses 2 random bytes, see rpc_manager::invoke). The responder echoes it verbatim.

  • y: the message type, either "q" query, "r" response or "e" error.

  • v: an optional client-version string (not modelled here; libtorrent adds it at the transport layer).

  • q + a: for a query, the query name and its argument dict.

  • r: for a response, the response dict.

  • e: for an error, the [code, message] list (incoming_error).

The argument/response dict for every query carries the sender's own node id under id (rpc_manager::add_our_id / node::incoming_request's add_our_id(reply)).

This file provides:

Reuses NodeId/Sha1Hash, DhtEndpoint/CompactNode (compact node info), ImmutableItem/MutableItem (BEP-44 values) and DhtKeySizes.

Inheritors

Types

Link copied to clipboard
sealed class Args

The typed contents of a query's a dictionary, minus the always-present id. Each subtype corresponds to one BEP-5/44/51 query.

Link copied to clipboard
object Companion
Link copied to clipboard
class ErrorMessage(val transactionId: ByteArray, val code: Int, val message: String) : DhtMessage

A KRPC error (y == "e"): {t, y:"e", e:[code, message]}, built by libtorrent's incoming_error.

Link copied to clipboard
class Query(val transactionId: ByteArray, val query: String, val nodeId: Sha1Hash, val args: DhtMessage.Args, val readOnly: Boolean = false) : DhtMessage

A KRPC query (y == "q"): {t, y:"q", q:<name>, a:{id, …}}. The concrete argument payload is one of the nested Args types.

Link copied to clipboard
sealed class Reply

The typed contents of a response's r dictionary, minus the always-present id.

Link copied to clipboard
class Response(val transactionId: ByteArray, val nodeId: Sha1Hash, val reply: DhtMessage.Reply, val raw: BdecodeNode? = null) : DhtMessage

A KRPC response (y == "r"): {t, y:"r", r:{id, …}}. The typed r payload is one of the Reply types. raw keeps the original response dict, so fields this model does not break out (p, ip, scrape bloom filters, …) remain accessible.

Properties

Link copied to clipboard
abstract val messageType: Char

The single character carried in y: 'q', 'r' or 'e'.

Link copied to clipboard
abstract val transactionId: ByteArray

The transaction id (t): opaque bytes the response echoes back from the query.