torrent-core
Low-level core abstractions for the BitTorrent protocol. Zero async runtime dependency — all types are fully synchronous.
This crate provides the fundamental data types and algorithms needed for
BitTorrent communication. It is a dependency of torrent and can
also be used standalone when only low-level parsing or encoding is needed.
Modules
| Module | Description | BEP |
|---|---|---|
bencode |
Bencode encoding/decoding with strict validation | BEP 3 |
error |
Error + ErrorKind (kind + source pattern) | — |
metainfo |
.torrent file parsing, info_hash() |
BEP 3, 12, 52 |
magnet |
Magnet URI parsing (hex + base32) | BEP 9 |
peer |
Handshake, 11 wire message types, PeerId | BEP 3 |
dht |
KRPC message format, Kademlia RoutingTable | BEP 5 |
tracker |
Announce request/response data types | BEP 3, 15, 23 |
piece |
PieceManager (bitfield), PieceSelector + 4 strategies | BEP 3 |
storage |
Storage trait for piece read/write | BEP 3 |
Quick Start
use ;
let = decode.unwrap;
assert!;
let encoded = encode;
assert_eq!;
Examples
Bencode
use ;
use Bytes;
// Decode a bencoded dictionary
let = decode.unwrap;
let encoded = encode;
// Keys are sorted lexicographically
assert_eq!;
Metainfo
use from_bytes;
let meta = from_bytes.unwrap;
println!;
println!;
Magnet
use FromStr;
use MagnetUri;
let uri = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567\
&dn=ubuntu-24.04&tr=http://tracker.example.com/announce";
let magnet = from_str.unwrap;
assert_eq!;
assert_eq!;
Peer
use ;
// PeerId generation
let peer_id = random;
assert_eq!;
// Handshake round-trip
let hs = new;
let bytes = hs.to_bytes;
let parsed = from_bytes.unwrap;
assert_eq!;
// Message round-trip
let msg = Request ;
let encoded = encode;
let decoded = decode.unwrap;
assert_eq!;
DHT / KRPC
use ;
let mut rt = new;
rt.insert;
assert_eq!;
Storage
use PieceManager;
let mut pm = new;
pm.set_piece;
assert_eq!;
assert_eq!;
Tracker data types
use parse_compact_peers_ipv4;
let data = ; // 127.0.0.1:6881
let peers = parse_compact_peers_ipv4.unwrap;
assert_eq!;
No async runtime required — this crate has zero dependency on tokio or any other async runtime.
Testing
All tests are synchronous — no #[tokio::test] is used anywhere.
Features Not Here
The following require async I/O and live in the torrent crate:
- Peer stream: async TCP
PeerConnection - Tracker: HTTP and UDP announce clients
- DHT RPC: async UDP send/receive with transaction matching
- DHT queries:
find_node,get_peers,announce_peer - File storage:
FileStorage(tokiofs) - Session: high-level download/upload orchestration
License
MIT — see LICENSE.