Naut/include/naut/dht.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

66 lines
2.4 KiB
C

/* dht.h - BEP-5 KRPC codec and bounded IPv4 get_peers traversal. */
#ifndef NAUT_DHT_H
#define NAUT_DHT_H
#include "naut/common.h"
#include "naut/tracker.h"
#define NAUT_DHT_ID_LEN 20
#define NAUT_DHT_MAX_NODES 256
#define NAUT_DHT_MAX_PEERS 256
typedef struct {
uint8_t id[NAUT_DHT_ID_LEN];
uint8_t ip[4];
uint16_t port;
} naut_dht_node;
typedef enum {
NAUT_DHT_RESPONSE,
NAUT_DHT_ERROR
} naut_dht_message_type;
typedef struct {
naut_dht_message_type type;
uint8_t transaction[8];
size_t transaction_len;
uint8_t id[NAUT_DHT_ID_LEN];
bool has_id;
uint8_t token[64];
size_t token_len;
naut_dht_node *nodes;
size_t num_nodes;
naut_peer_addr *peers;
size_t num_peers;
int error_code;
} naut_dht_response;
naut_err naut_dht_build_ping(const uint8_t *tx, size_t tx_len,
const uint8_t id[20],
uint8_t **out, size_t *out_len);
naut_err naut_dht_build_find_node(const uint8_t *tx, size_t tx_len,
const uint8_t id[20],
const uint8_t target[20],
uint8_t **out, size_t *out_len);
naut_err naut_dht_build_get_peers(const uint8_t *tx, size_t tx_len,
const uint8_t id[20],
const uint8_t info_hash[20],
uint8_t **out, size_t *out_len);
naut_err naut_dht_build_announce_peer(const uint8_t *tx, size_t tx_len,
const uint8_t id[20],
const uint8_t info_hash[20],
uint16_t port, bool implied_port,
const void *token, size_t token_len,
uint8_t **out, size_t *out_len);
naut_err naut_dht_parse_response(const uint8_t *data, size_t len,
naut_dht_response *out);
void naut_dht_response_free(naut_dht_response *response);
/* Query bootstrap endpoints ("host:port") and iteratively follow returned
* compact nodes until peers are found or the bounded traversal is exhausted. */
naut_err naut_dht_get_peers(const char *const *bootstrap, size_t num_bootstrap,
const uint8_t info_hash[20],
naut_peer_addr **peers, size_t *num_peers);
#endif /* NAUT_DHT_H */