Files
mcde/programs/dtchat/headless.c
T
lotherk dc44fc25a3 dtchat: add the headless peer mode (T5.7)
dtchat-headless starts the TLS listener (--listen [HOST:]PORT, port 0 prints the real port) and one reconnecting link per --peer, loads/creates the identity in --config, prints JSON events (ready, peer-online, peer-offline, message, typing, file-offer, error) and takes JSON commands on stdin (send by host:port or peer id, quit). dc_local_peer_conn() and the ack id let a link know its peer. tests/p2p.sh runs two instances over loopback: A listens, B connects, B sends, A answers by id, both quit; standalone run prints 'tests/p2p.sh: all ok'. Wired into noinst_PROGRAMS and mcde-tests.sh (expect 29 ok). make mcde-host still blocked by pid 2126511 (release/sourcesets/usb).
2026-11-16 20:43:06 +00:00

468 lines
11 KiB
C

/*
* headless.c -- dtchat without a display
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* usage: dtchat-headless [--config DIR] [--listen [HOST:]PORT]
* [--peer HOST:PORT ...]
*
* Starts the local TLS listener and one reconnecting link per --peer. Every
* event is one JSON object on stdout; commands are JSON lines on stdin:
*
* {"cmd":"send","peer":"HOST:PORT"|"ID","text":"..."}
* {"cmd":"quit"}
*
* Events: ready, peer-online, peer-offline, message, typing, file-offer,
* error. The test harness is tests/p2p.sh.
*/
#include "dc_internal.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct Link {
char target[256];
DcLocalPeer *peer;
int online;
char id[128];
struct Link *next;
};
struct Accepted {
DcLocalConn *conn;
char id[128];
char pin[128];
struct Accepted *next;
};
static DcLoop *loop;
static DcLocalServer *server;
static struct Link *links;
static struct Accepted *accepted;
static int quit_flag;
static int stdin_id;
static DcFrameBuf stdin_frame;
static void
emit(DcJson *json)
{
size_t len = 0;
char *text = dc_json_write(json, &len);
if (text != NULL) {
(void)write(STDOUT_FILENO, text, len);
(void)write(STDOUT_FILENO, "\n", 1);
free(text);
}
dc_json_free(json);
}
static void
emit_error(const char *text)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "event", dc_json_new_string("error"));
(void)dc_json_object_set(json, "text", dc_json_new_string(text));
emit(json);
}
static void
emit_peer(const char *event, const char *mode, const char *target,
const char *id, const char *pin)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "event", dc_json_new_string(event));
(void)dc_json_object_set(json, "mode", dc_json_new_string(mode));
if (target != NULL)
(void)dc_json_object_set(json, "target",
dc_json_new_string(target));
if (id != NULL)
(void)dc_json_object_set(json, "id", dc_json_new_string(id));
if (pin != NULL)
(void)dc_json_object_set(json, "pin",
dc_json_new_string(pin));
emit(json);
}
static struct Accepted *
accepted_find(DcLocalConn *conn)
{
struct Accepted *a;
for (a = accepted; a != NULL; a = a->next)
if (a->conn == conn)
return a;
return NULL;
}
static struct Accepted *
accepted_by_id(const char *id)
{
struct Accepted *a;
for (a = accepted; a != NULL; a = a->next)
if (strcmp(a->id, id) == 0)
return a;
return NULL;
}
static void
server_hello(DcLocalConn *conn, const DcJson *hello, void *arg)
{
struct Accepted *a = calloc(1, sizeof(*a));
const char *id = dc_json_string(dc_json_get(hello, "id"));
const char *pin = dc_json_string(dc_json_get(hello, "pin"));
(void)arg;
if (a == NULL)
return;
a->conn = conn;
snprintf(a->id, sizeof(a->id), "%s", id != NULL ? id : "");
snprintf(a->pin, sizeof(a->pin), "%s", pin != NULL ? pin : "");
a->next = accepted;
accepted = a;
emit_peer("peer-online", "server", NULL, a->id, a->pin);
}
static void
emit_message(const char *from, const DcJson *json)
{
DcJson *out = dc_json_new_object();
const char *text = dc_json_string(dc_json_get(json, "text"));
(void)dc_json_object_set(out, "event",
dc_json_new_string("message"));
(void)dc_json_object_set(out, "from",
dc_json_new_string(from != NULL ? from : ""));
(void)dc_json_object_set(out, "text",
dc_json_new_string(text != NULL ? text : ""));
emit(out);
}
static void
common_line(const char *from, DcLocalConn *conn, const DcJson *json)
{
const char *type = dc_json_string(dc_json_get(json, "type"));
if (type == NULL)
return;
if (strcmp(type, "msg") == 0) {
emit_message(from, json);
if (conn != NULL)
(void)dc_local_send_msg_ack(conn, dc_json_string(
dc_json_get(json, "client_id")));
} else if (strcmp(type, "typing") == 0) {
DcJson *out = dc_json_new_object();
(void)dc_json_object_set(out, "event",
dc_json_new_string("typing"));
(void)dc_json_object_set(out, "from",
dc_json_new_string(from));
(void)dc_json_object_set(out, "on", dc_json_new_bool(
dc_json_bool(dc_json_get(json, "on"))));
emit(out);
} else if (strcmp(type, "file-offer") == 0) {
DcJson *out = dc_json_new_object();
(void)dc_json_object_set(out, "event",
dc_json_new_string("file-offer"));
(void)dc_json_object_set(out, "from",
dc_json_new_string(from));
(void)dc_json_object_set(out, "name", dc_json_new_string(
dc_json_string(dc_json_get(json, "name"))));
(void)dc_json_object_set(out, "size",
dc_json_new_int(dc_json_get(json, "size") != NULL ?
dc_json_int(dc_json_get(json, "size")) : 0));
emit(out);
}
}
static void
server_line(DcLocalConn *conn, const DcJson *json, void *arg)
{
struct Accepted *a = accepted_find(conn);
(void)arg;
common_line(a != NULL ? a->id : "?", conn, json);
}
static void
server_closed(DcLocalConn *conn, void *arg)
{
struct Accepted **slot = &accepted;
(void)arg;
while (*slot != NULL) {
if ((*slot)->conn == conn) {
struct Accepted *dead = *slot;
emit_peer("peer-offline", "server", NULL, dead->id,
NULL);
*slot = dead->next;
free(dead);
return;
}
slot = &(*slot)->next;
}
}
static void
link_line(DcLocalPeer *peer, const DcJson *json, void *arg)
{
struct Link *link = arg;
(void)peer;
common_line(link->id, dc_local_peer_conn(peer), json);
}
static void
link_state(DcLocalPeer *peer, int online, void *arg)
{
struct Link *link = arg;
DcLocalConn *conn = dc_local_peer_conn(peer);
const char *id = conn != NULL ? dc_local_conn_peer_id(conn) : NULL;
const char *pin = conn != NULL ? dc_local_conn_peer_pin(conn) : NULL;
link->online = online;
if (online && id != NULL)
snprintf(link->id, sizeof(link->id), "%s", id);
emit_peer(online ? "peer-online" : "peer-offline", "client",
link->target, link->id, online ? pin : NULL);
}
static struct Link *
link_by_name(const char *name)
{
struct Link *link;
for (link = links; link != NULL; link = link->next)
if (strcmp(link->target, name) == 0 ||
(link->id[0] != '\0' && strcmp(link->id, name) == 0))
return link;
return NULL;
}
static void
stdin_readable(int fd, short revents, void *arg)
{
char buf[4096];
ssize_t n;
(void)revents;
(void)arg;
n = read(fd, buf, sizeof(buf));
if (n <= 0) {
quit_flag = 1;
return;
}
if (dc_frame_feed(&stdin_frame, buf, (size_t)n) != 0)
return;
for (;;) {
size_t len = 0;
char *line = dc_frame_next(&stdin_frame, &len);
DcJson *cmd;
const char *what;
if (line == NULL)
break;
cmd = dc_json_parse(line, len, NULL, 0);
free(line);
if (cmd == NULL) {
emit_error("invalid command");
continue;
}
what = dc_json_string(dc_json_get(cmd, "cmd"));
if (what == NULL) {
emit_error("missing cmd");
} else if (strcmp(what, "quit") == 0) {
quit_flag = 1;
} else if (strcmp(what, "send") == 0) {
const char *peer = dc_json_string(dc_json_get(cmd,
"peer"));
const char *text = dc_json_string(dc_json_get(cmd,
"text"));
struct Link *link = link_by_name(peer);
DcJson *msg;
if (link != NULL) {
msg = dc_json_new_object();
(void)dc_json_object_set(msg, "type",
dc_json_new_string("msg"));
(void)dc_json_object_set(msg, "text",
dc_json_new_string(text != NULL ?
text : ""));
if (dc_local_peer_send(link->peer, msg) != 0)
emit_error("peer is offline");
dc_json_free(msg);
} else {
struct Accepted *a = accepted_by_id(peer);
if (a != NULL) {
if (dc_local_send_msg(a->conn, text,
NULL) != 0)
emit_error("send failed");
} else {
emit_error("unknown peer");
}
}
} else {
emit_error("unknown cmd");
}
dc_json_free(cmd);
}
}
static int
split_host_port(const char *text, char *host, size_t hostlen, int *port)
{
const char *colon = strrchr(text, ':');
char *end = NULL;
long value;
if (colon == NULL) {
snprintf(host, hostlen, "127.0.0.1");
value = strtol(text, &end, 10);
} else {
size_t len = (size_t)(colon - text);
if (len >= hostlen)
return -1;
memcpy(host, text, len);
host[len] = '\0';
if (host[0] == '\0')
snprintf(host, hostlen, "127.0.0.1");
value = strtol(colon + 1, &end, 10);
}
if (end == NULL || *end != '\0' || value < 0 || value > 65535)
return -1;
*port = (int)value;
return 0;
}
int
main(int argc, char **argv)
{
const char *config = NULL;
char default_config[512];
const char *listen_text = NULL;
const char *peer_text[8];
int npeers = 0;
DcIdentity *identity;
int i;
int port = 0;
char listen_host[128] = "127.0.0.1";
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) {
config = argv[++i];
} else if (strcmp(argv[i], "--listen") == 0 &&
i + 1 < argc) {
listen_text = argv[++i];
} else if (strcmp(argv[i], "--peer") == 0 &&
i + 1 < argc && npeers < 8) {
peer_text[npeers++] = argv[++i];
} else {
fprintf(stderr, "usage: %s [--config DIR] "
"[--listen [HOST:]PORT] [--peer HOST:PORT ...]\n",
argv[0]);
return 2;
}
}
if (config == NULL) {
const char *home = getenv("HOME");
if (home == NULL)
return 1;
snprintf(default_config, sizeof(default_config),
"%s/.dt/dtchat", home);
config = default_config;
}
signal(SIGPIPE, SIG_IGN);
identity = dc_identity_load_or_create(config);
if (identity == NULL) {
fprintf(stderr, "dtchat-headless: cannot load the identity\n");
return 1;
}
loop = dc_loop_new();
if (loop == NULL)
return 1;
if (listen_text != NULL) {
if (split_host_port(listen_text, listen_host,
sizeof(listen_host), &port) != 0) {
fprintf(stderr, "dtchat-headless: bad --listen\n");
return 2;
}
server = dc_local_server_new(loop, identity, listen_host, port,
NULL, NULL, server_hello, server_line, server_closed, NULL);
if (server == NULL) {
fprintf(stderr, "dtchat-headless: cannot listen\n");
return 1;
}
port = dc_local_server_port(server);
}
for (i = 0; i < npeers; i++) {
char host[128];
int p;
struct Link *link;
if (split_host_port(peer_text[i], host, sizeof(host), &p) != 0 ||
p <= 0) {
fprintf(stderr, "dtchat-headless: bad --peer\n");
return 2;
}
link = calloc(1, sizeof(*link));
if (link == NULL)
return 1;
snprintf(link->target, sizeof(link->target), "%s", peer_text[i]);
link->next = links;
links = link;
link->peer = dc_local_peer_new(loop, identity, host, p, NULL,
link_line, link_state, link);
if (link->peer == NULL || dc_local_peer_start(link->peer) != 0)
emit_error("cannot start the peer link");
}
{
DcJson *ready = dc_json_new_object();
(void)dc_json_object_set(ready, "event",
dc_json_new_string("ready"));
(void)dc_json_object_set(ready, "listen", dc_json_new_int(port));
(void)dc_json_object_set(ready, "id", dc_json_new_string(
dc_identity_public_b64(identity)));
(void)dc_json_object_set(ready, "pin", dc_json_new_string(
dc_identity_pin(identity)));
emit(ready);
}
dc_frame_init(&stdin_frame);
stdin_id = dc_loop_add_fd(loop, STDIN_FILENO, POLLIN, stdin_readable,
NULL);
while (!quit_flag)
(void)dc_loop_run(loop, 200);
if (stdin_id > 0)
(void)dc_loop_del_fd(loop, stdin_id);
while (links != NULL) {
struct Link *next = links->next;
dc_local_peer_free(links->peer);
free(links);
links = next;
}
dc_local_server_free(server);
while (accepted != NULL) {
struct Accepted *next = accepted->next;
free(accepted);
accepted = next;
}
dc_frame_free(&stdin_frame);
dc_loop_free(loop);
dc_identity_free(identity);
return 0;
}