MetadataTransfer

class MetadataTransfer(val totalSize: Int)

Reassembles the metadata (info) blob from incoming UtMetadata data pieces and verifies it against the expected info-hash. This is the codec-side counterpart of ut_metadata_plugin::received_metadata / metadata_request in ut_metadata.cpp.

Construct it once the total size is known (from the first data message's total_size, or from the peer's metadata_size handshake field), feed pieces with addPiece, then once isComplete call verify to check the assembled bytes against the torrent's info-hash. libtorrent does the equivalent check via torrent::set_metadata (which hashes and compares); the expensive-invariant block at the top of ut_metadata_plugin::metadata() shows the exact hashes used: hasher(info) == v1 and hasher256(info) == v2.

This helper is deliberately self-contained and does no networking; it just owns the buffer and the per-piece "have" bitfield.

Parameters

totalSize

the full length of the metadata blob, in bytes (must be > 0).

Constructors

Link copied to clipboard
constructor(totalSize: Int)

Properties

Link copied to clipboard

True once every piece has been received.

Link copied to clipboard

The number of 16 KiB pieces this metadata spans.

Link copied to clipboard

Functions

Link copied to clipboard

Convenience: accept a parsed UtMetadata.Message. Only UtMetadata.Type.DATA messages contribute; anything else returns false.

Link copied to clipboard
fun addPiece(piece: Int, data: ByteArray): Boolean

Store the bytes of one metadata piece. Mirrors the validity checks in received_metadata:

Link copied to clipboard

The assembled metadata bytes. Only meaningful once isComplete; before then it still returns the (partially-filled) buffer so callers can hash-check incrementally if they wish.

Link copied to clipboard
fun hasPiece(piece: Int): Boolean

True if piece piece has already been stored.

Link copied to clipboard

The list of piece indices still missing. Use it to drive UtMetadata.encodeRequest.

Link copied to clipboard
fun verify(expected: Digest32): Boolean

Verify against whichever info-hash width expected is: a 20-byte digest is treated as a v1 (SHA-1) hash, a 32-byte digest as a v2 (SHA-256) hash. Any other width returns false.

Link copied to clipboard
fun verifySha1(expected: Sha1Hash): Boolean

Verify the assembled metadata against an expected v1 (SHA-1) info-hash, that is hasher(info).final() == info_hashes().v1. Returns false if not yet isComplete.

Link copied to clipboard

Verify the assembled metadata against an expected v2 (SHA-256) info-hash, that is hasher256(info).final() == info_hashes().v2. Returns false if not yet isComplete.