C
The C library of Atyha: join a host by a short room code (HK4M-7TRW) over WebRTC, signalled through public Nostr relays, with an optional password (CPace). It reproduces ../vectors/atyha-v1.json byte for byte and interoperates with the TypeScript reference over the real relays.
Atyha is signalling only: it carries one complete SDP offer and one complete SDP answer. Your program makes the peer connection with its own WebRTC stack (libdatachannel, the browser's, Godot's…).
- C99; macOS, Linux, Windows (MSVC), iOS, Android, Emscripten.
- Poll-driven and single-threaded: no thread is started, nothing calls you behind your back. Call
atyha_*_poll(obj, now_ms)every frame and drain the events. - No global state of its own (libsodium's
sodium_initand libcurl'scurl_global_initare the libraries' own, called for you). - MIT. Depends on libsodium and bitcoin-core/libsecp256k1 (MIT, fetched by CMake).
Layers
| layer | files | I/O |
|---|---|---|
codes, topic, CPace, keys, sealing (atyha_crypto.h) | src/crypto.c, src/util.c | none |
| messages, host and joiner state machines, in-memory bus | src/message.c, src/session.c, src/membus.c | none (through an atyha_bus) |
| Nostr relay pool: NIP-01 events, BIP-340 signing, REQ/EVENT/CLOSE/EOSE/OK/CLOSED, retirement and backoff | src/nostr.c | none (through an atyha_ws_backend) |
| WebSocket backends | src/ws_curl.c (target atyha_curl), src/ws_libdatachannel.c (target atyha_libdatachannel), src/ws_emscripten.c (in atyha under Emscripten), or your own | yes |
Build
brew install libsodium curl # or apt install libsodium-dev libcurl4-openssl-dev
cmake -S . -B build -DCMAKE_PREFIX_PATH="$(brew --prefix curl);$(brew --prefix)"
cmake --build build
ctest --test-dir build # vectors, session, nostrTargets: atyha (static: everything but the native backends), atyha_curl (static, the libcurl backend), atyha_libdatachannel (static, libdatachannel's WebSocket). Options: ATYHA_TESTS (on when top level), ATYHA_CURL (on), ATYHA_WITH_LIBDATACHANNEL (on with the examples; links the parent project's datachannel (or datachannel-static) target, which must be built with NO_WEBSOCKET OFF, else the fetched one), ATYHA_EXAMPLES (off; fetches libdatachannel v0.24.6 with NO_MEDIA ON, NO_WEBSOCKET OFF, and needs OpenSSL: -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3)), SODIUM_ROOT (a libsodium install to use, e.g. one built with emcc), ATYHA_VECTORS (path). A parent project may define atyha::sodium / atyha::secp256k1 targets itself.
libsecp256k1 is fetched at v0.8.0 with the schnorrsig and extrakeys modules.
libcurl version
The WebSocket API (curl_ws_send/curl_ws_recv, CURLOPT_CONNECT_ONLY = 2) is stable since libcurl 8.11.0 and must be compiled in (ws/wss among the protocols of curl -V). macOS's own libcurl (8.7.1 on this machine) has no WebSocket support; Homebrew's curl (8.16.0 here) has it. The backend refuses to start (returns NULL) when libcurl lacks wss. On Windows/Android/iOS build libcurl with WebSockets enabled (-DCURL_DISABLE_WEBSOCKETS=OFF, the default since 8.11) and give it CA certificates (atyha_curl_options.ca_file) where the platform store is not used.
libdatachannel's WebSocket
For programs that already link libdatachannel and OpenSSL (the game and its server) and whose libcurl has no WebSocket (macOS's system libcurl 8.7.1):
#include "atyha_libdatachannel.h"
atyha_ws_backend* ws = atyha_libdatachannel_backend_create(NULL); /* verifies TLS */
/* ... tr.ws = ws ... */
atyha_libdatachannel_backend_destroy(ws); /* before rtcCleanup() */libdatachannel calls back on its own threads; the backend only queues there, under a per-connection lock (pthread mutex, CRITICAL_SECTION on Windows), and Atyha drains the queue from its poll. Closing deletes the rtc WebSocket first (rtcDeleteWebSocket resets the callbacks) and frees the queue after. disable_tls_verification (default 0) exists for tests only.
Known limit: libdatachannel's TLS client offers only the P-256 key-exchange group (SSL_CTX_set1_groups_list(ctx, "P-256") in its tlstransport.cpp), and a relay that refuses P-256 fails the handshake: of the default relays, wss://nostr.data.haus does (checked with openssl s_client -groups P-256). The other eight work, and the pool goes on without it.
Emscripten
Under emcmake cmake, src/ws_emscripten.c is compiled into atyha and the curl backend is skipped; link the program with -lwebsocket.js and give a libsodium built with emcc via -DSODIUM_ROOT=....
API
Everything is in include/atyha.h. Strings are UTF-8; a string an event points to stays valid until the next poll of the same object.
Transport
#include "atyha.h"
#include "atyha_curl.h" /* native */
atyha_ws_backend* ws = atyha_curl_backend_create(NULL); /* one for the program */
atyha_transport tr = {0};
tr.ws = ws; /* Nostr, default relays of spec section 8 */
/* tr.relays = my_list; tr.relay_count = n; a configured relay list */
/* tr.bus_open = atyha_memory_bus_open; tr.bus_user = hub; tests, no network */Host (fits an asynchronous "offer in, answer later" server)
atyha_host_config hc = {0};
hc.app = "ngspades"; /* 1..64 bytes */
hc.password = "123"; /* NULL or "": none */
hc.meta = "{\"name\":\"my server\"}"; /* a JSON object sent to joiners, or NULL */
hc.ice = ice; hc.ice_count = n; /* optional: STUN/TURN for the joiners */
hc.transport = tr;
atyha_host* host = atyha_host_create(&hc);
/* atyha_host_code(host) -> "HK4M7TRW"; atyha_code_display() -> "HK4M-7TRW" */
for (;;) { /* every tick */
atyha_event ev;
while (atyha_host_poll(host, now_ms, &ev)) {
if (ev.type == ATYHA_EV_OPEN) { /* the room is reachable: show the code */ }
if (ev.type == ATYHA_EV_OFFER) {
/* ev.sdp, ev.meta (joiner's JSON or NULL), ev.joiner, ev.attempt:
start the peer connection; reply whenever ready, from any tick: */
remember(ev.attempt);
}
}
/* later: */
atyha_host_answer(host, attempt, answer_sdp); /* or */
atyha_host_reject(host, attempt, "server full");
}
atyha_host_destroy(host); /* closes the room */For ngnet: attempt plays the part of offer_id; ATYHA_EV_OFFER is where ngnet_rtc_offer(...) would be called, and the answer hook calls atyha_host_answer (or atyha_host_reject in place of ngnet_master_reject). A reply is kept 60 s and re-sent to every repeat of the same offer; attempts waiting for the program are kept 5 minutes.
Joiner
atyha_join_config jc = {0};
jc.app = "ngspades"; jc.code = typed_text; jc.password = pw; jc.meta = "{\"player\":\"p1\"}";
jc.transport = tr;
atyha_joiner* j = atyha_joiner_create(&jc);
while (atyha_joiner_poll(j, now_ms, &ev)) switch (ev.type) {
case ATYHA_EV_NEED_OFFER: /* password right. ev.ice/ev.ice_count (host's servers, use
them when > 0; atyha_ice_url_libdatachannel formats them),
ev.meta (host's). Make the peer connection, both channels,
wait for gathering complete (<= 5 s), then: */
atyha_joiner_set_offer(j, offer_sdp); break;
case ATYHA_EV_ANSWER: /* ev.sdp: set it as the remote description; j is done */ break;
case ATYHA_EV_ERROR: /* ev.error: ATYHA_ERR_BAD_CODE, _NO_RELAY, _NO_ROOM,
_WRONG_PASSWORD, _REJECTED (ev.reason = host's text),
_NO_ANSWER, _CANCELLED; atyha_error_name() gives the
TS JoinError code ("wrong_password"...) */ break;
case ATYHA_EV_LOG: /* ev.text */ break;
}
atyha_joiner_cancel(j); /* any time: the next poll reports cancelled */
atyha_joiner_destroy(j);Timings follow the spec and the TS reference: hello every 2 s from the first EOSE, no_relay when no relay confirmed the subscription in 10 s, no_room 25 s after the first hello, wrong_password 4 s after a pake that does not open (unless a good one comes), offer every 2 s, no_answer 25 s after the first offer (timeout_ms changes the 25 s). One addition: if the program never calls atyha_joiner_set_offer, the joiner reports no_answer 25 s after ATYHA_EV_NEED_OFFER.
Your own WebSocket
Fill an atyha_ws_backend (connect, send, poll returning ATYHA_WS_OPEN / ATYHA_WS_TEXT / ATYHA_WS_CLOSED, close, optional update), or a whole atyha_bus (publish, poll, destroy) for another transport. Both are plain structs of function pointers, embedded as the first member of your own struct.
Building blocks
atyha_crypto.h exposes lv_cat, PRS, CI, the CPace generator string and generator, shares, scalar_mult_vfy, ISK, the HKDF keys, seal/open and base64url, as the vectors name them.
Example: echo
cmake -S . -B build -DATYHA_EXAMPLES=ON -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3) \
-DCMAKE_PREFIX_PATH="$(brew --prefix curl);$(brew --prefix)"
cmake --build build
build/echo host [--app A] [--password P] [--ws libdatachannel|curl] # CODE XXXX-XXXX, then PEER <meta>
build/echo join <code> [--app A] [--password P] [--ws libdatachannel|curl]The echo contract of every Atyha example (the same as ts/tools/peer.ts): the host answers a text "x" on reliable with "echo x" and sends binary back on its channel; the joiner sends "hi" and the bytes 1,2,3 and prints CONNECTED <host meta>, RELIABLE echo hi, UNRELIABLE 1,2,3 (exit 0), or FAILED <code> <why>. ATYHA_LOG=1 prints the relay log on stderr. --ws picks the WebSocket backend for the relays (default libdatachannel; curl when atyha_curl was built).
Notes
- Relays: an
OK falseorCLOSEDsayingrate-limited:closes that relay for 60 s;blocked:,restricted:,auth-required:orpow:drops it for the session; a closed connection reconnects after 2 s, doubling to 30 s. Events are deduplicated by id (the last 1024), own events and events more than 60 s off the clock are ignored. Received signatures are not checked (spec section 8 allows it). - The Nostr
created_atuses the wall clock (time()); the polls'now_msmay be any monotonic millisecond clock (atyha_now_ms()is one).