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>
60 lines
2 KiB
C
60 lines
2 KiB
C
#include "naut/bitfield.h"
|
|
#include "test.h"
|
|
|
|
int main(void) {
|
|
naut_bitfield bf;
|
|
/* deliberately not a multiple of 64 to exercise tail masking */
|
|
CHECK(naut_bitfield_init(&bf, 1000) == NAUT_OK);
|
|
CHECK_EQ(bf.nwords, 16);
|
|
CHECK_EQ(naut_bitfield_count(&bf), 0);
|
|
CHECK(!naut_bitfield_all_set(&bf));
|
|
|
|
naut_bitfield_set(&bf, 0);
|
|
naut_bitfield_set(&bf, 63);
|
|
naut_bitfield_set(&bf, 64);
|
|
naut_bitfield_set(&bf, 999);
|
|
CHECK(naut_bitfield_test(&bf, 0));
|
|
CHECK(naut_bitfield_test(&bf, 999));
|
|
CHECK(!naut_bitfield_test(&bf, 1));
|
|
CHECK_EQ(naut_bitfield_count(&bf), 4);
|
|
|
|
CHECK_EQ(naut_bitfield_find_set(&bf, 0), 0);
|
|
CHECK_EQ(naut_bitfield_find_set(&bf, 1), 63);
|
|
CHECK_EQ(naut_bitfield_find_set(&bf, 65), 999);
|
|
CHECK_EQ(naut_bitfield_find_set(&bf, 1000), SIZE_MAX);
|
|
|
|
CHECK_EQ(naut_bitfield_find_zero(&bf, 0), 1);
|
|
CHECK_EQ(naut_bitfield_find_zero(&bf, 63), 65);
|
|
|
|
naut_bitfield_clear(&bf, 63);
|
|
CHECK(!naut_bitfield_test(&bf, 63));
|
|
CHECK_EQ(naut_bitfield_count(&bf), 3);
|
|
|
|
/* all-set respects nbits, not nwords*64 */
|
|
naut_bitfield_set_all(&bf);
|
|
CHECK_EQ(naut_bitfield_count(&bf), 1000);
|
|
CHECK(naut_bitfield_all_set(&bf));
|
|
CHECK_EQ(naut_bitfield_find_zero(&bf, 0), SIZE_MAX);
|
|
|
|
/* wire round-trip (BEP-3 MSB-first) */
|
|
naut_bitfield_clear_all(&bf);
|
|
naut_bitfield_set(&bf, 0); /* -> byte 0, bit 0x80 */
|
|
naut_bitfield_set(&bf, 7); /* -> byte 0, bit 0x01 */
|
|
naut_bitfield_set(&bf, 8); /* -> byte 1, bit 0x80 */
|
|
uint8_t wire[125]; /* ceil(1000/8) */
|
|
naut_bitfield_to_wire(&bf, wire, sizeof(wire));
|
|
CHECK_EQ(wire[0], 0x81);
|
|
CHECK_EQ(wire[1], 0x80);
|
|
|
|
naut_bitfield bf2;
|
|
naut_bitfield_init(&bf2, 1000);
|
|
naut_bitfield_from_wire(&bf2, wire, sizeof(wire));
|
|
CHECK(naut_bitfield_test(&bf2, 0));
|
|
CHECK(naut_bitfield_test(&bf2, 7));
|
|
CHECK(naut_bitfield_test(&bf2, 8));
|
|
CHECK_EQ(naut_bitfield_count(&bf2), 3);
|
|
|
|
naut_bitfield_free(&bf);
|
|
naut_bitfield_free(&bf2);
|
|
TEST_MAIN_END();
|
|
}
|