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

59
include/naut/common.h Normal file
View file

@ -0,0 +1,59 @@
/* common.h — project-wide types, attributes, and error codes.
*
* Pure C11. No allocation, no platform calls; safe to include everywhere.
*/
#ifndef NAUT_COMMON_H
#define NAUT_COMMON_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
/* --- sizes ------------------------------------------------------------- */
#define NAUT_CACHELINE 64u
#define NAUT_PAGE 4096u
/* BitTorrent wire block size (BEP-3). The fundamental transfer unit. */
#define NAUT_BLOCK (16u * 1024u)
/* --- compiler attributes ----------------------------------------------- */
#define NAUT_LIKELY(x) __builtin_expect(!!(x), 1)
#define NAUT_UNLIKELY(x) __builtin_expect(!!(x), 0)
#define NAUT_INLINE static inline __attribute__((always_inline))
#define NAUT_ALIGNED(n) __attribute__((aligned(n)))
#define NAUT_CACHE_ALIGNED __attribute__((aligned(NAUT_CACHELINE)))
#define NAUT_NORETURN __attribute__((noreturn))
#define NAUT_UNUSED __attribute__((unused))
#define NAUT_PACKED __attribute__((packed))
#define NAUT_MUST_USE __attribute__((warn_unused_result))
#define NAUT_PRINTF(fi, ai) __attribute__((format(printf, fi, ai)))
/* --- small helpers ----------------------------------------------------- */
#define NAUT_ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
#define NAUT_MIN(a, b) ((a) < (b) ? (a) : (b))
#define NAUT_MAX(a, b) ((a) > (b) ? (a) : (b))
#define NAUT_ALIGN_UP(x, a) (((uintptr_t)(x) + ((a) - 1)) & ~((uintptr_t)(a) - 1))
#define NAUT_ALIGN_DOWN(x, a) ((uintptr_t)(x) & ~((uintptr_t)(a) - 1))
#define NAUT_IS_POW2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
#define NAUT_CONTAINER_OF(ptr, type, member) \
((type *)((char *)(1 ? (ptr) : &((type *)0)->member) - offsetof(type, member)))
/* --- error codes ------------------------------------------------------- */
typedef int naut_err;
enum {
NAUT_OK = 0,
NAUT_ERR_NOMEM = -1,
NAUT_ERR_INVAL = -2,
NAUT_ERR_IO = -3,
NAUT_ERR_AGAIN = -4, /* would block / retry */
NAUT_ERR_PROTO = -5, /* protocol violation */
NAUT_ERR_RANGE = -6,
NAUT_ERR_NOSYS = -7, /* unsupported by kernel/build */
NAUT_ERR_FULL = -8,
NAUT_ERR_EMPTY = -9,
NAUT_ERR_NOTFOUND = -10,
};
const char *naut_strerror(naut_err e);
#endif /* NAUT_COMMON_H */