Skip to content

thrum-clients/c: the C client, with its primitives imported - #88

Merged
adiled merged 2 commits into
mainfrom
thrum/c-client
Oct 24, 2026
Merged

adiled merged 2 commits into
mainfrom
thrum/c-client

Conversation

@adiled

@adiled adiled commented Oct 24, 2026

Copy link
Copy Markdown
Owner

What this adds

The fifth reference client for the thrum wire, in C, and nothing else.
No hive rides along: this is what a C bee needs, so that dwarfstar (#85)
can shrink to the hive itself on top of it.

C has no JSON and no crypto in its standard library, so the client
imports both instead of carrying them:

  • libsodium for Ed25519, SHA-256 and randomness
  • cJSON for reading and writing tones

Nothing is vendored and no primitive is hand-rolled. #85 carried a
vendored Ed25519 (5,019 lines), a vendored jsmn, a hand-written JSON tree
and a SHA-256 emitted by codegen; this replaces all of it with two
pkg-config dependencies.

What it touches in the core

  • codegen gains a C target that emits only what is read from the Rust
    source: the registry (thrum/chi.h) and the views (thrum/protocol.h).
    The helpers are library calls and live with the client, so the
    generator carries no C logic. main.rs now lets a target own the files
    it owns rather than always three.
  • thrum-core/build.rs refreshes the two headers; --check gates them.
  • CI gets a c job that installs libsodium-dev and libcjson-dev
    and runs make test.
  • WIRE.md and the codegen README name the C client.

The client

thrum/client.c the socket, sid dispatch, one locked write per line, the hello verdict
thrum/identity.c enters through hum bee enter --hive --as, loads the filed key, signs the hello with one timestamp
thrum/helpers.c sigil, rid, dusk, wane, socket path
thrum/spec.c thrum_tone_undeclared, the declared-key check humd runs
thrum.mk what a hive's Makefile includes for the flags and the archive
examples/echo_bee.c a whole worker: enters, nestles, echoes every prompt

Entering goes through the hum CLI, as the TS bees do, so there is one
implementation of it.

The hello's verdict

humd acks every tone that has a rid with echo ok:true as soon as it
is delivered. The hello is admitted by the breath after its own ack and
refused by an echo ok:false under its rid. thrum_client_hello waits
for that (ten seconds by default), and thrum_client_run returns -1 if a
refusal arrives after admission.

Reading it this way was not optional. The first version took the first
breath as admission, and a live humd admitted a forged key, because:

  1. humd sends a breath on connect, before it has seen any hello.
    WIRE.md says the breath is the reply to the hello.
  2. A refused socket is not closed: thrum_close only drops the client
    from the registry. WIRE.md says the connection is closed. The Rust and
    TS bees ignore breath and echo, so a refused one sits connected
    and never knows.
  3. The revocation check runs after the admission breath is sent, so a
    revoked bee is told it is in, then refused.

This PR does not change humd; those three are worth their own fix.

Proof

  • make test: 423 checks. The bee id and the hello signature match
    hum_identity::BeeKey byte for byte over the RFC 8032 test seed; the
    sigils are thrum_core::sigil's. A thread plays humd over a real Unix
    socket: the golden fixture in seven-byte pieces among junk lines, the
    admit, refuse, revoke, hang-up and silent hellos, eight threads sending
    at once.
  • Clean under ASan, UBSan and TSan.
  • The CI job run in Ubuntu 24.04, x86-64 and arm64, gcc 13: no warnings,
    423 of 423.
  • Live, against a humd built from main in isolated XDG dirs: the echo bee
    entered and nestled, a C asker's prompt came back as chunk and
    finish through humd, a hello signed by a key never entered was
    refused, and the C sigil matched the one humd put on the wire.

Building it

brew install pkgconf libsodium cjson                         # macOS
sudo apt-get install pkg-config libsodium-dev libcjson-dev   # Debian, Ubuntu
make -C thrum-clients/c test

C has no JSON and no crypto in its standard library, so the C client
imports both instead of carrying them: libsodium for Ed25519, SHA-256
and randomness, cJSON for tones. Nothing is vendored and no primitive is
hand-rolled.

codegen emits only what is read from the Rust source for C: the
registry (thrum/chi.h) and the views (thrum/protocol.h). The helpers
are library calls and live with the client, so the generator carries no
C logic. build.rs refreshes the pair and --check gates them.

A bee enters through the hum CLI and signs its hello with one
timestamp. Sends are one locked write per line. The hello's verdict is
read the way humd gives it: the breath after the hello's own delivery
ack admits, an echo with ok false under its rid refuses, and a refusal
after admission ends the run loop. thrum_tone_undeclared names stray
keys the way thrum_core::undeclared does. thrum.mk gives a hive the
flags and the archive.

The tests check the bee id and the hello signature byte for byte
against hum_identity over the RFC 8032 seed, read the golden fixture
through a real socket among junk lines, replay humd's admit, refuse,
revoke, hang-up and silent hellos, and send from eight threads at once.
They pass under ASan, UBSan and TSan, and on Ubuntu 24.04 with gcc.
CI builds and tests the client against the distribution's packages.
…ncluder's goal

main made a sigil the session's own, sha256("sigil:<sid>"), the same on
every humd, so thrum_sigil takes the sid alone. The vectors are
thrum_core::sigil's, and the C sigil matches the one humd put on the
wire in a live run.

The regenerated views carry headroom on hello and pulse.

thrum.mk defined the archive before the includer defined anything, so
the archive became the default goal of every Makefile that included it,
the client's own among them, and a bare make built only libthrum.a. It
now puts the includer's default goal back.
@adiled
adiled merged commit b30c162 into main Oct 24, 2026
5 checks passed
@adiled
adiled deleted the thrum/c-client branch October 31, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant