Naut/include/naut/extension.h
ookami125 2178d6a70c 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>
2026-06-15 12:12:00 -04:00

70 lines
2.4 KiB
C

/* extension.h - BEP-10 transport, BEP-9 metadata, and BEP-11 PEX codecs. */
#ifndef NAUT_EXTENSION_H
#define NAUT_EXTENSION_H
#include "naut/common.h"
#include "naut/tracker.h"
#define NAUT_EXT_UT_METADATA 1
#define NAUT_EXT_UT_PEX 2
#define NAUT_METADATA_BLOCK (16u * 1024u)
#define NAUT_METADATA_MAX (4u * 1024u * 1024u)
typedef struct {
uint8_t ut_metadata;
uint8_t ut_pex;
uint32_t metadata_size;
uint32_t reqq;
uint16_t port;
} naut_ext_handshake;
/* Build a complete peer-wire extended message (length, message ID 20,
* extension ID, payload). The returned buffer is malloc-owned. */
naut_err naut_ext_build_handshake(uint8_t ut_metadata_id, uint8_t ut_pex_id,
uint32_t metadata_size, uint16_t port,
uint8_t **out, size_t *out_len);
naut_err naut_ext_parse_handshake(const uint8_t *payload, size_t len,
naut_ext_handshake *out);
typedef enum {
NAUT_METADATA_REQUEST = 0,
NAUT_METADATA_DATA = 1,
NAUT_METADATA_REJECT = 2
} naut_metadata_type;
typedef struct {
naut_metadata_type type;
uint32_t piece;
uint32_t total_size;
const uint8_t *data;
size_t data_len;
} naut_metadata_msg;
naut_err naut_metadata_build(uint8_t ext_id, naut_metadata_type type,
uint32_t piece, uint32_t total_size,
const void *data, size_t data_len,
uint8_t **out, size_t *out_len);
naut_err naut_metadata_parse(const uint8_t *payload, size_t len,
naut_metadata_msg *out);
typedef struct {
naut_peer_addr *added;
uint8_t *added_flags;
size_t num_added;
naut_peer_addr *dropped;
size_t num_dropped;
} naut_pex_msg;
naut_err naut_pex_build(uint8_t ext_id, const naut_pex_msg *msg,
uint8_t **out, size_t *out_len);
naut_err naut_pex_parse(const uint8_t *payload, size_t len, naut_pex_msg *out);
void naut_pex_free(naut_pex_msg *msg);
/* Fetch and SHA-1 verify a v1 torrent's raw info dictionary from one peer.
* The returned buffer is malloc-owned. */
naut_err naut_metadata_fetch(const naut_peer_addr *peer,
const uint8_t info_hash[20],
const uint8_t peer_id[20],
uint8_t **info, size_t *info_len);
#endif /* NAUT_EXTENSION_H */