Initial commit: Naut-Torrent — from-scratch 10 GbE BitTorrent client

A maintainable, extensible BitTorrent client (C11, Linux/io_uring) targeting
10 GbE saturation. All torrent functionality is built from scratch; liburing
is the only linked third-party dependency on the data path.

Implements Phases 1-7 of the roadmap:
- core: page-aligned buffer pool, MPMC/Treiber queues, bitfields, worker pool
- crypto: SHA-1/256 (SHA-NI + scalar), Merkle (BEP-52), RC4 (MSE)
- bencode/metainfo: zero-copy parser, v1/v2/hybrid .torrent + magnet
- peer: sans-IO wire codec, MSE/PE handshake state machine, BEP-10, ut_metadata, PEX
- piece/storage: block-level multi-peer engine, rarest-first + endgame,
  per-file completion events + single-file relocate (move-as-you-finish)
- tracker/dht: HTTP + UDP (BEP-15) trackers, BEP-5 KRPC iterative lookup
- platform: io_uring reactor (SQPOLL, registered buffers, SEND_ZC)
- surface: versioned RPC, native plugin ABI, sandboxed Lua scripting, nautd/nautctl

Verified against libtorrent (single/multi/hybrid, MSE, magnet-via-DHT, swarm);
unit + interop tests green; ASan/UBSan/TSan clean. Scripting reference in
docs/scripting.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ookami125 2026-06-15 12:12:00 -04:00
commit 2178d6a70c
121 changed files with 12644 additions and 0 deletions

1
tests/fixtures/data/multi/a.txt vendored Normal file

File diff suppressed because one or more lines are too long

BIN
tests/fixtures/data/multi/sub/b.dat vendored Normal file

Binary file not shown.

BIN
tests/fixtures/data/single.bin vendored Normal file

Binary file not shown.

40
tests/fixtures/generate.py vendored Normal file
View file

@ -0,0 +1,40 @@
import libtorrent as lt, os, hashlib, shutil
root = "tests/fixtures"
data = os.path.join(root, "data")
shutil.rmtree(data, ignore_errors=True)
os.makedirs(os.path.join(data, "multi", "sub"), exist_ok=True)
# deterministic content
with open(os.path.join(data, "single.bin"), "wb") as f:
f.write(bytes((i*131+7) & 0xff for i in range(200000)))
with open(os.path.join(data, "multi", "a.txt"), "wb") as f:
f.write(b"hello naut " * 5000)
with open(os.path.join(data, "multi", "sub", "b.dat"), "wb") as f:
f.write(bytes((i*7) & 0xff for i in range(90000)))
def make(name, src, flags):
fs = lt.file_storage()
lt.add_files(fs, src)
t = lt.create_torrent(fs, piece_size=16384, flags=flags)
parent = os.path.dirname(src) if os.path.isfile(src) else os.path.dirname(src.rstrip("/"))
t.add_tracker("http://tracker.example.com:8080/announce", 0)
t.add_tracker("udp://tracker.example.com:8080", 1)
lt.set_piece_hashes(t, parent)
ent = t.generate()
blob = lt.bencode(ent)
path = os.path.join(root, name)
with open(path, "wb") as f: f.write(blob)
ti = lt.torrent_info(ent)
ih = ti.info_hashes()
v1 = str(ih.v1) if ih.has_v1() else "-"
v2 = str(ih.v2) if ih.has_v2() else "-"
print(f"{name}\tv1={v1}\tv2={v2}\tpieces={ti.num_pieces()}\tsize={ti.total_size()}")
return path
V1 = lt.create_torrent.v1_only
V2 = lt.create_torrent.v2_only
make("single_v1.torrent", os.path.join(data, "single.bin"), V1)
make("multi_v1.torrent", os.path.join(data, "multi"), V1)
make("hybrid.torrent", os.path.join(data, "multi"), 0)
make("v2.torrent", os.path.join(data, "multi"), V2)

BIN
tests/fixtures/hybrid.torrent vendored Normal file

Binary file not shown.

1
tests/fixtures/multi_v1.torrent vendored Normal file
View file

@ -0,0 +1 @@
d8:announce40:http://tracker.example.com:8080/announce13:announce-listll40:http://tracker.example.com:8080/announceel30:udp://tracker.example.com:8080ee13:creation datei1781498759e4:infod5:filesld6:lengthi90000e4:pathl3:sub5:b.dateed6:lengthi55000e4:pathl5:a.txteee4:name5:multi12:piece lengthi16384e6:pieces180:¸2¾HGLµóžU÷JéJ(~5Ṹ2¾HGLµóžU÷JéJ(~5Ṹ2¾HGLµóžU÷JéJ(~5Ṹ2¾HGLµóžU÷JéJ(~5Ṹ2¾HGLµóžU÷JéJ(~5á¹_ålqy€ß5ØÜуþDæø+¯~^ÖPøLRD$¼•¨ŸÚ„Q«ûç-üº¢¸™3<E284A2>BøcŒ¯½í-R~,{ŽäúÊÙl1ÊQL!XñN*ee

7
tests/fixtures/phase7.lua vendored Normal file
View file

@ -0,0 +1,7 @@
function on_torrent_finished(event)
naut.move_file(event.torrent_id, 0, "/tmp/naut-phase7-finished")
end
function on_file_complete(event)
naut.move_file(event.torrent_id, event.index, event.path .. ".moved")
end

1
tests/fixtures/single_v1.torrent vendored Normal file
View file

@ -0,0 +1 @@
d8:announce40:http://tracker.example.com:8080/announce13:announce-listll40:http://tracker.example.com:8080/announceel30:udp://tracker.example.com:8080ee13:creation datei1781498759e4:infod6:lengthi200000e4:name10:single.bin12:piece lengthi16384e6:pieces260:í °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓåí °±Ê%‰Ìß!Ÿ£°8·HÓå<>L<EFBFBD>ï/éø®ÀÅÛ(vùlÝoGee

BIN
tests/fixtures/v2.torrent vendored Normal file

Binary file not shown.