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>
93 lines
2.6 KiB
C
93 lines
2.6 KiB
C
#include "naut/rpc.h"
|
|
|
|
#include <jansson.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define DEFAULT_SOCKET "/tmp/nautd.sock"
|
|
|
|
static void usage(const char *program) {
|
|
fprintf(stderr,
|
|
"usage: %s [--socket PATH] METHOD [PARAMS_JSON]\n"
|
|
" %s [--socket PATH] events\n", program, program);
|
|
}
|
|
|
|
static json_t *parse_params(const char *text) {
|
|
if (!text) return json_object();
|
|
json_error_t error;
|
|
return json_loads(text, JSON_REJECT_DUPLICATES | JSON_DECODE_ANY, &error);
|
|
}
|
|
|
|
static int print_json(json_t *json) {
|
|
if (!json) return 1;
|
|
if (json_dumpf(json, stdout, JSON_INDENT(2) | JSON_SORT_KEYS) != 0)
|
|
return 1;
|
|
putchar('\n');
|
|
return 0;
|
|
}
|
|
|
|
static int stream_events(const char *socket_path) {
|
|
int fd = naut_rpc_connect_unix(socket_path);
|
|
if (fd < 0) return 1;
|
|
json_t *request = json_pack("{s:s,s:o}", "method", "subscribe",
|
|
"params", json_object());
|
|
if (!request ||
|
|
naut_rpc_send_json(fd, NAUT_RPC_REQUEST, request) != NAUT_OK) {
|
|
json_decref(request);
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
json_decref(request);
|
|
for (;;) {
|
|
naut_rpc_frame_type type;
|
|
json_t *payload = NULL;
|
|
if (naut_rpc_recv_json(fd, &type, &payload) != NAUT_OK) {
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
print_json(payload);
|
|
fflush(stdout);
|
|
json_decref(payload);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *socket_path = getenv("NAUT_SOCKET");
|
|
if (!socket_path || !*socket_path) socket_path = DEFAULT_SOCKET;
|
|
int arg = 1;
|
|
if (arg < argc && strcmp(argv[arg], "--socket") == 0) {
|
|
if (arg + 1 >= argc) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
socket_path = argv[arg + 1];
|
|
arg += 2;
|
|
}
|
|
if (arg >= argc || arg + 2 < argc) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
signal(SIGPIPE, SIG_IGN);
|
|
const char *method = argv[arg++];
|
|
if (strcmp(method, "events") == 0)
|
|
return stream_events(socket_path);
|
|
json_t *params = parse_params(arg < argc ? argv[arg] : NULL);
|
|
if (!params) {
|
|
fprintf(stderr, "nautctl: invalid JSON parameters\n");
|
|
return 2;
|
|
}
|
|
json_t *reply = NULL;
|
|
naut_err error = naut_rpc_call(socket_path, method, params, &reply);
|
|
json_decref(params);
|
|
if (error != NAUT_OK) {
|
|
fprintf(stderr, "nautctl: RPC failed: %s\n", naut_strerror(error));
|
|
return 1;
|
|
}
|
|
int result = print_json(reply);
|
|
bool ok = json_is_true(json_object_get(reply, "ok"));
|
|
json_decref(reply);
|
|
return result || !ok;
|
|
}
|