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

56
include/naut/uring.h Normal file
View file

@ -0,0 +1,56 @@
/* uring.h — io_uring lifecycle (the kernel-facing seam, part 2).
*
* Thin ownership wrapper over liburing so the rest of the engine never calls
* io_uring_* directly that keeps the "Linux-only now, portable later" seam
* intact (a future epoll/kqueue backend implements the same reactor contract).
* Each reactor thread owns exactly one naut_ring for both network and disk.
*/
#ifndef NAUT_URING_H
#define NAUT_URING_H
#include "naut/common.h"
#include "naut/buf.h"
#include <liburing.h>
typedef struct naut_ring {
struct io_uring ring;
bool sqpoll;
bool send_zc;
bool msg_ring;
bool buffers_registered;
bool recv_fixed;
} naut_ring;
/* entries: SQ depth (rounded up to a power of two by the kernel).
* sqpoll: dedicate a kernel thread to submission polling removes the
* io_uring_enter syscall from the hot path once the queue is warm (needs
* CAP_SYS_NICE or /proc/sys tuning on some setups; falls back if it can't). */
naut_err naut_ring_init(naut_ring *r, unsigned entries, bool sqpoll);
naut_err naut_ring_init_cpu(naut_ring *r, unsigned entries, bool sqpoll,
int sqpoll_cpu);
void naut_ring_close(naut_ring *r);
/* Detect kernel support for the features the data path relies on. Logs a
* summary; returns NAUT_ERR_NOSYS if a hard requirement is missing. */
naut_err naut_ring_probe(naut_ring *r);
/* Register the pool's contiguous slab as fixed buffer index 0. Registration
* may fail under a low RLIMIT_MEMLOCK; callers can continue in degraded mode. */
naut_err naut_ring_register_bufpool(naut_ring *r, const naut_bufpool *pool);
void naut_ring_unregister_buffers(naut_ring *r);
/* Prepare a send using SEND_ZC when supported and requested, otherwise normal
* SEND. Returns true when the caller must retain the buffer until a CQE with
* IORING_CQE_F_NOTIF arrives. */
bool naut_ring_prep_send(naut_ring *r, struct io_uring_sqe *sqe, int fd,
const void *buf, size_t len, int flags,
bool prefer_zero_copy);
/* Returns true when this recv was armed against the registered fixed buffer.
* Some kernels accept READ_FIXED but reject IORING_RECVSEND_FIXED_BUF on plain
* recv with -EINVAL; the caller should remember this per-operation so it can
* distinguish "fixed-buffer unsupported, retry unfixed" from a real error
* (rather than tearing the connection down). */
bool naut_ring_prep_recv(naut_ring *r, struct io_uring_sqe *sqe, int fd,
void *buf, size_t len, int flags);
#endif /* NAUT_URING_H */