# dtchat local peer protocol (LAN) Status: implemented in `lib/libDtChat/local.c`, `filexfer.c`, `pair.c`; normative for the local mode (`dtchat-headless`, later the UI). This is not the adapter protocol (`dtchat-adapter-protocol.txt`): local peers are the core itself, two dtchat instances on the same LAN, not a protocol helper process. ## 1. Transport and identity - TLS over TCP, one connection per peer. The server certificate is the instance's self-signed Ed25519 X.509 certificate (`identity.c`, `identity.json`, CN `dtchat`, ten years). The client presents its own identity certificate as well: the server asks for it (`verify_client` optional) and compares the hash of the certificate that completed the handshake with the pin in `hello`, so the claimed identity is bound to a key. The chain is not checked (TOFU by pin), only the key's possession. - The pin is the SHA-256 of the DER certificate, written `sha256:<64 lowercase hex>`; it is the same value the certificate's `tls_peer_cert_hash()` produces (`SHA256:...` normalised). - TOFU: the first pin for an identity id is stored only after the person compared the 6-digit SAS (section 5). A known id with a different pin is blocked until re-paired. - Framing: one UTF-8 JSON object per line (`\n`), no NUL, at most one object per line. The connection buffers are 128 KiB, enough for one 64 KiB file chunk in base64. Default port: none; the listener takes `--listen [HOST:]PORT`, port 0 picks an ephemeral port. Discovery (mDNS) is T5.2 and not implemented yet; peers are configured by `--peer HOST:PORT` or the peer model. ## 2. Connection sequence 1. TCP connect, mutual TLS handshake (both sides present their identity certificate). The client does not verify the CA chain with libtls (`insecure_noverifycert/noverifyname`); it verifies the pin from the server's `hello-ack` against the certificate it actually saw (`tls_peer_cert_hash`), which is the TOFU decision. The server does the same with the client's pin in `hello`. 2. The client sends `hello`: ```json {"type":"hello","id":"","pin":"sha256:"} ``` 3. The server checks the id and pin against what it expects (for a first contact: nothing; the person pairs), answers `hello-ack`: ```json {"type":"hello-ack","ok":true,"pin":"sha256:","id":""} ``` `ok:false` carries a `reason` (`id`, `pin`, `protocol`) and the server closes the connection after the line. 4. The client checks that `ack.pin` is the certificate's pin and, when a pin is already known, that it matches; the hello-ack has a 5 s timeout. 5. The connection is *ready*; both sides exchange the messages of section 3. ## 3. Messages All are JSON lines. Unknown types are ignored (forward compatibility). | Type | Direction | Fields | |---|---|---| | `hello` | client → server | `id`, `pin` | | `hello-ack` | server → client | `ok`, `pin`, `id`, `reason` | | `presence` | both | `show` (`online`, `away`, ...), optional `status` | | `msg` | both | `text`, optional `client_id` | | `msg-ack` | both | `client_id` | | `typing` | both | `on` (bool) | | `media-key` | both | `pub` (base64 X25519) and `port` (the sender's UDP port) | | `file-offer` | sender → receiver | `id`, `name`, `size`, `sha256` | | `file-accept` / `file-reject` | receiver → sender | `id`, optional `reason` | | `file-data` | sender → receiver | `id`, `data` (base64, up to 64 KiB) | | `file-end` | sender → receiver | `id`, `sha256` | | `file-done` | receiver → sender | `id`, `ok` | | `file-cancel` | both | `id`, optional `reason` | File messages are only accepted from a peer whose pin is paired (known and equal); before the SAS confirmation an offer is answered with `file-reject` and reason `pairing required`. Text messages are allowed before pairing. A file transfer is one offer id. The receiver writes `NAME.part`, checks the size and the SHA-256, and renames to a sanitized basename (no `/`, `\`, `..`, control characters) without overwriting (suffix `-1`, `-2`, ...). The limit is 512 MiB. `file-cancel` removes the part on both sides. ## 3a. Calls A call starts with `media-key` over the ready text connection: each side sends its ephemeral X25519 public key (base64) and its media port (the same number as its TLS listener). Both derive the two direction keys with HKDF-SHA256; the side that asked is the initiator, the answerer mirrors send/recv. The initiator then rings with sealed `DC_MEDIA_RING` packets on UDP to the peer's port; the answerer replies ACCEPT (or BUSY), and both switch to `IN_CALL`. `DC_MEDIA_PTT` tells the peer whether the key is held; audio (Opus 20 ms frames) and video (VP8, fragmented) follow as sealed `DC_MEDIA_AUDIO`/`DC_MEDIA_VIDEO` packets. A video frame carries the send time in milliseconds as its timestamp (the receiver compares it with its audio clock and drops a frame more than 80 ms late); a receiver that lost fragments sends `DC_MEDIA_KFREQ`, and the sender forces a keyframe for its next frame. The camera is 640x360 at 15 fps, 500 kbit/s; other capture sizes are scaled before encoding. A call needs both sides to have a `--listen` port, because that port is the media socket as well. ## 4. Reconnect and timeouts - A dropped connection is reported as offline; the peer link reconnects after 2, 4, 8 ... 60 s (reset on a successful hello-ack). - The hello-ack timeout is 5 s. - Messages are not queued while offline (`send` fails; the UI says so). - The listener keeps accepting while links are down. ## 5. Pairing (SAS) `dc_pair_sas(pin_a, pin_b)` sorts the two normalised pins, concatenates them, hashes with SHA-256 and takes the first 4 bytes modulo 1,000,000, formatted as six digits. Both sides compute the same code. Known answers: - `sha256:111...1` + `sha256:222...2` → `448183` - `sha256:111...1` twice → `500498` Flow: first contact (`dc_pins_check` returns 0) → show the code, the person compares it on both machines and confirms → `dc_pins_confirm` stores the pin (0600 `peers.json`) → files and calls are allowed. A changed pin returns -1 and stays blocked; only a new SAS confirmation clears it. Before pairing only text requests are allowed. ## 6. Files in the tree - `lib/libDtChat/local.c` — listener, client, hello, message lines, peer link. - `lib/libDtChat/filexfer.c` — offer/chunks/hash/name/cancel. - `lib/libDtChat/pair.c` — SAS, `peers.json`. - `programs/dtchat/headless.c` — headless peer, events on stdout. - `tests/p2p.sh` — two instances over loopback. - `programs/dtchat/ui_peer.c`, `ui_main.c` — Nearby section and peer details (the file dialogs are still open in T5.8).