From 2d283f5ada15618eb186ddb4ce9062c05645d2da Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 16 Nov 2026 19:50:16 +0000 Subject: [PATCH] dtchat: add the local peer model (T5.3) peers.c keeps one peer per identity id: the TXT record (v/id/name/pin/port, ';'-joined) merges by id and marks the peer online; a service that disappears is marked offline, a manual peer stays; blocking is a property that survives announcements. Strings are owned by the peer. test_peers covers announce/merge, port preference, offline per peer, block survival, manual peers and malformed records; built test all ok, make mcde-host exit=0, tools/mcde-tests.sh 26 ok/0 bad. --- ext/src/mcde/lib/libDtChat/Makefile.am | 9 +- ext/src/mcde/lib/libDtChat/dc_internal.h | 31 +++ ext/src/mcde/lib/libDtChat/peers.c | 286 +++++++++++++++++++++++ ext/src/mcde/lib/libDtChat/test_peers.c | 113 +++++++++ tools/mcde-tests.sh | 1 + 5 files changed, 437 insertions(+), 3 deletions(-) create mode 100644 ext/src/mcde/lib/libDtChat/peers.c create mode 100644 ext/src/mcde/lib/libDtChat/test_peers.c diff --git a/ext/src/mcde/lib/libDtChat/Makefile.am b/ext/src/mcde/lib/libDtChat/Makefile.am index ab3ef2ff39..7002d087ff 100644 --- a/ext/src/mcde/lib/libDtChat/Makefile.am +++ b/ext/src/mcde/lib/libDtChat/Makefile.am @@ -11,14 +11,14 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ - irc.c identity.c dc_internal.h + irc.c identity.c peers.c dc_internal.h libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) libDtChat_la_LDFLAGS = -version-info 1:0:0 # Built by make mcde-host as well, so tools/mcde-tests.sh finds them in # build/mcde-host; make check still builds and runs them. noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ - test_identity test_adapter test_instance fake_adapter + test_identity test_peers test_adapter test_instance fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -53,5 +53,8 @@ test_irc_LDADD = libDtChat.la libDtChatIpc.la test_identity_SOURCES = test_identity.c test_identity_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_identity_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) +test_peers_SOURCES = test_peers.c +test_peers_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_peers_LDADD = libDtChat.la libDtChatIpc.la TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ - test_identity test_adapter test_instance + test_identity test_peers test_adapter test_instance diff --git a/ext/src/mcde/lib/libDtChat/dc_internal.h b/ext/src/mcde/lib/libDtChat/dc_internal.h index 06e2d80787..41ed9f0628 100644 --- a/ext/src/mcde/lib/libDtChat/dc_internal.h +++ b/ext/src/mcde/lib/libDtChat/dc_internal.h @@ -177,6 +177,37 @@ const char *dc_identity_pin(const DcIdentity *id); const char *dc_identity_public_b64(const DcIdentity *id); const char *dc_identity_cert_pem(const DcIdentity *id); +/* + * Local peers. One peer per identity id; dc_peers_announce() takes the + * service's TXT record ("v=1;id=...;name=...;pin=...;port=...", keys joined + * with ';'), merges it by id and marks the peer online. A service that goes + * away is dc_peers_offline(); a manual peer is not touched by that. The + * block flag belongs to the peer and survives announcements. Strings belong + * to the peer. + */ +typedef struct DcPeer DcPeer; +typedef struct DcPeers DcPeers; + +DcPeers *dc_peers_new(void); +void dc_peers_free(DcPeers *peers); +size_t dc_peers_count(const DcPeers *peers); +DcPeer *dc_peers_at(const DcPeers *peers, size_t index); +DcPeer *dc_peers_find(const DcPeers *peers, const char *id); +int dc_peers_announce(DcPeers *peers, const char *txt, const char *host, + int port); +int dc_peers_offline(DcPeers *peers, const char *id); +DcPeer *dc_peers_add_manual(DcPeers *peers, const char *id, const char *name, + const char *host, int port, const char *pin); +void dc_peer_set_blocked(DcPeer *peer, int blocked); +const char *dc_peer_id(const DcPeer *peer); +const char *dc_peer_name(const DcPeer *peer); +const char *dc_peer_host(const DcPeer *peer); +const char *dc_peer_pin(const DcPeer *peer); +int dc_peer_port(const DcPeer *peer); +int dc_peer_online(const DcPeer *peer); +int dc_peer_blocked(const DcPeer *peer); +int dc_peer_manual(const DcPeer *peer); + /* * IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags, * the prefix, the command, up to 15 parameters and the trailing text; diff --git a/ext/src/mcde/lib/libDtChat/peers.c b/ext/src/mcde/lib/libDtChat/peers.c new file mode 100644 index 0000000000..0d95c65096 --- /dev/null +++ b/ext/src/mcde/lib/libDtChat/peers.c @@ -0,0 +1,286 @@ +/* + * peers.c -- the local peers dtchat knows about + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * One peer per identity id. A DNS-SD announcement (the TXT record of + * _dtchat._tcp) or a manual entry creates or updates it; a peer that + * disappears from mDNS is marked offline, a manual one stays. Blocking is + * a property of the peer and survives announcements. The mDNS and socket + * parts live in McdeSys and the listener (T5.2/T5.4); this module is the + * state and the TXT parser, so it is testable without a network. + */ +#include "dc_internal.h" + +#include +#include +#include + +struct DcPeer { + char *id; + char *name; + char *host; + char *pin; + int port; + int online; + int blocked; + int manual; +}; + +struct DcPeers { + DcPeer *peer; + size_t count; + size_t capacity; +}; + +DcPeers * +dc_peers_new(void) +{ + return calloc(1, sizeof(DcPeers)); +} + +void +dc_peers_free(DcPeers *peers) +{ + size_t i; + + if (peers == NULL) + return; + for (i = 0; i < peers->count; i++) { + free(peers->peer[i].id); + free(peers->peer[i].name); + free(peers->peer[i].host); + free(peers->peer[i].pin); + } + free(peers->peer); + free(peers); +} + +size_t +dc_peers_count(const DcPeers *peers) +{ + return peers != NULL ? peers->count : 0; +} + +DcPeer * +dc_peers_at(const DcPeers *peers, size_t index) +{ + if (peers == NULL || index >= peers->count) + return NULL; + return &peers->peer[index]; +} + +DcPeer * +dc_peers_find(const DcPeers *peers, const char *id) +{ + size_t i; + + if (peers == NULL || id == NULL) + return NULL; + for (i = 0; i < peers->count; i++) + if (strcmp(peers->peer[i].id, id) == 0) + return &peers->peer[i]; + return NULL; +} + +static DcPeer * +peer_create(DcPeers *peers, const char *id) +{ + DcPeer *peer; + + if (peers->count == peers->capacity) { + size_t cap = peers->capacity != 0 ? peers->capacity * 2 : 8; + DcPeer *next = realloc(peers->peer, cap * sizeof(*next)); + + if (next == NULL) + return NULL; + peers->peer = next; + peers->capacity = cap; + } + peer = &peers->peer[peers->count]; + memset(peer, 0, sizeof(*peer)); + peer->id = strdup(id); + peer->name = strdup(id); + peer->host = strdup(""); + peer->pin = strdup(""); + peer->port = 0; + if (peer->id == NULL || peer->name == NULL || peer->host == NULL || + peer->pin == NULL) { + free(peer->id); + free(peer->name); + free(peer->host); + free(peer->pin); + return NULL; + } + peers->count++; + return peer; +} + +static int +set_string(char **slot, const char *value) +{ + char *copy = strdup(value != NULL ? value : ""); + + if (copy == NULL) + return -1; + free(*slot); + *slot = copy; + return 0; +} + +/* + * TXT record of the service, e.g. + * "v=1;id=8f...;name=alice;pin=sha256:...;port=4711". Keys are separated by + * ';' (a DNS-SD TXT record is a sequence of key=value strings, which the + * watcher joins with ';'). Unknown keys are ignored; a record without an + * "id" is refused. + */ +int +dc_peers_announce(DcPeers *peers, const char *txt, const char *host, int port) +{ + char *copy; + char *save = NULL; + char *part; + char id[256] = ""; + char name[256] = ""; + char pin[256] = ""; + int txt_port = 0; + DcPeer *peer; + + if (peers == NULL || txt == NULL) + return -1; + copy = strdup(txt); + if (copy == NULL) + return -1; + for (part = strtok_r(copy, ";", &save); part != NULL; + part = strtok_r(NULL, ";", &save)) { + char *eq = strchr(part, '='); + + if (eq == NULL) + continue; + *eq++ = '\0'; + if (strcmp(part, "id") == 0) + snprintf(id, sizeof(id), "%s", eq); + else if (strcmp(part, "name") == 0) + snprintf(name, sizeof(name), "%s", eq); + else if (strcmp(part, "pin") == 0) + snprintf(pin, sizeof(pin), "%s", eq); + else if (strcmp(part, "port") == 0) + txt_port = atoi(eq); + } + free(copy); + if (id[0] == '\0') + return -1; + peer = dc_peers_find(peers, id); + if (peer == NULL) { + peer = peer_create(peers, id); + if (peer == NULL) + return -1; + peer->manual = 0; + } + if (name[0] != '\0' && set_string(&peer->name, name) != 0) + return -1; + if (pin[0] != '\0' && set_string(&peer->pin, pin) != 0) + return -1; + if (host != NULL && set_string(&peer->host, host) != 0) + return -1; + if (port > 0) + peer->port = port; + else if (txt_port > 0) + peer->port = txt_port; + peer->online = 1; + return 0; +} + +int +dc_peers_offline(DcPeers *peers, const char *id) +{ + DcPeer *peer = dc_peers_find(peers, id); + + if (peer == NULL) + return -1; + peer->online = 0; + return 0; +} + +DcPeer * +dc_peers_add_manual(DcPeers *peers, const char *id, const char *name, + const char *host, int port, const char *pin) +{ + DcPeer *peer; + + if (peers == NULL || id == NULL || *id == '\0') + return NULL; + peer = dc_peers_find(peers, id); + if (peer == NULL) { + peer = peer_create(peers, id); + if (peer == NULL) + return NULL; + } + if (name != NULL && *name != '\0' && + set_string(&peer->name, name) != 0) + return NULL; + if (host != NULL && set_string(&peer->host, host) != 0) + return NULL; + if (pin != NULL && set_string(&peer->pin, pin) != 0) + return NULL; + if (port > 0) + peer->port = port; + peer->manual = 1; + return peer; +} + +void +dc_peer_set_blocked(DcPeer *peer, int blocked) +{ + if (peer != NULL) + peer->blocked = blocked ? 1 : 0; +} + +const char * +dc_peer_id(const DcPeer *peer) +{ + return peer != NULL ? peer->id : NULL; +} + +const char * +dc_peer_name(const DcPeer *peer) +{ + return peer != NULL ? peer->name : NULL; +} + +const char * +dc_peer_host(const DcPeer *peer) +{ + return peer != NULL ? peer->host : NULL; +} + +const char * +dc_peer_pin(const DcPeer *peer) +{ + return peer != NULL ? peer->pin : NULL; +} + +int +dc_peer_port(const DcPeer *peer) +{ + return peer != NULL ? peer->port : 0; +} + +int +dc_peer_online(const DcPeer *peer) +{ + return peer != NULL ? peer->online : 0; +} + +int +dc_peer_blocked(const DcPeer *peer) +{ + return peer != NULL ? peer->blocked : 0; +} + +int +dc_peer_manual(const DcPeer *peer) +{ + return peer != NULL ? peer->manual : 0; +} diff --git a/ext/src/mcde/lib/libDtChat/test_peers.c b/ext/src/mcde/lib/libDtChat/test_peers.c new file mode 100644 index 0000000000..9a3f3d1945 --- /dev/null +++ b/ext/src/mcde/lib/libDtChat/test_peers.c @@ -0,0 +1,113 @@ +/* + * test_peers.c -- tests for the local peer model (T5.3) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#include "dc_internal.h" + +#include +#include + +static int fails; + +#define CHECK(cond) do {\ + if (!(cond)) {\ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\ + #cond);\ + fails++;\ + }\ +} while (0) + +int +main(void) +{ + DcPeers *peers = dc_peers_new(); + DcPeer *p; + + CHECK(peers != NULL); + if (peers == NULL) + return 1; + CHECK(dc_peers_count(peers) == 0); + + CHECK(dc_peers_announce(peers, + "v=1;id=abc;name=Alice;pin=sha256:aa;port=4711", + "192.168.1.5", 0) == 0); + CHECK(dc_peers_count(peers) == 1); + p = dc_peers_find(peers, "abc"); + CHECK(p != NULL); + if (p != NULL) { + CHECK(strcmp(dc_peer_id(p), "abc") == 0); + CHECK(strcmp(dc_peer_name(p), "Alice") == 0); + CHECK(strcmp(dc_peer_host(p), "192.168.1.5") == 0); + CHECK(strcmp(dc_peer_pin(p), "sha256:aa") == 0); + CHECK(dc_peer_port(p) == 4711); + CHECK(dc_peer_online(p) == 1); + CHECK(dc_peer_blocked(p) == 0); + CHECK(dc_peer_manual(p) == 0); + } + + /* the same id updates instead of duplicating; the explicit port wins */ + CHECK(dc_peers_announce(peers, "id=abc;name=Alice 2", "10.0.0.7", + 5000) == 0); + CHECK(dc_peers_count(peers) == 1); + p = dc_peers_find(peers, "abc"); + CHECK(p != NULL && strcmp(dc_peer_name(p), "Alice 2") == 0); + CHECK(dc_peer_port(p) == 5000); + CHECK(strcmp(dc_peer_host(p), "10.0.0.7") == 0); + CHECK(strcmp(dc_peer_pin(p), "sha256:aa") == 0); + + /* a second identity is a second peer */ + CHECK(dc_peers_announce(peers, "id=def;name=Bob", "10.0.0.8", 6000) == 0); + CHECK(dc_peers_count(peers) == 2); + + /* offline is per peer */ + CHECK(dc_peers_offline(peers, "abc") == 0); + p = dc_peers_find(peers, "abc"); + CHECK(p != NULL && dc_peer_online(p) == 0); + p = dc_peers_find(peers, "def"); + CHECK(p != NULL && dc_peer_online(p) == 1); + CHECK(dc_peers_offline(peers, "nope") == -1); + + /* blocking survives an announcement */ + p = dc_peers_find(peers, "abc"); + dc_peer_set_blocked(p, 1); + CHECK(dc_peers_announce(peers, "id=abc;name=Alice 3", "10.0.0.7", + 5000) == 0); + p = dc_peers_find(peers, "abc"); + CHECK(p != NULL && dc_peer_blocked(p) == 1); + CHECK(dc_peer_online(p) == 1); + dc_peer_set_blocked(p, 0); + CHECK(dc_peer_blocked(p) == 0); + + /* manual peer: no mDNS, stays after the service list changes */ + p = dc_peers_add_manual(peers, "manual1", "Carol", "192.168.1.9", + 7000, "sha256:bb"); + CHECK(p != NULL); + CHECK(dc_peers_count(peers) == 3); + if (p != NULL) { + CHECK(dc_peer_manual(p) == 1); + CHECK(dc_peer_online(p) == 0); + CHECK(strcmp(dc_peer_name(p), "Carol") == 0); + CHECK(dc_peer_port(p) == 7000); + } + CHECK(dc_peers_offline(peers, "manual1") == 0); + p = dc_peers_find(peers, "manual1"); + CHECK(p != NULL && dc_peer_online(p) == 0); + + /* a record without an id is refused and changes nothing */ + CHECK(dc_peers_announce(peers, "name=Nobody;port=1", NULL, 0) == -1); + CHECK(dc_peers_count(peers) == 3); + CHECK(dc_peers_announce(peers, "", NULL, 0) == -1); + + CHECK(dc_peers_at(peers, 0) != NULL); + CHECK(dc_peers_at(peers, 3) == NULL); + dc_peers_free(peers); + dc_peers_free(NULL); + + if (fails != 0) { + fprintf(stderr, "%d test(s) failed\n", fails); + return 1; + } + printf("test_peers: all ok\n"); + return 0; +} diff --git a/tools/mcde-tests.sh b/tools/mcde-tests.sh index f491747b15..b7e3536220 100755 --- a/tools/mcde-tests.sh +++ b/tools/mcde-tests.sh @@ -76,6 +76,7 @@ run_test "DtChat/test_conv" "$B/lib/libDtChat/test_conv" run_test "DtChat/test_text" "$B/lib/libDtChat/test_text" run_test "DtChat/test_irc" "$B/lib/libDtChat/test_irc" run_test "DtChat/test_identity" "$B/lib/libDtChat/test_identity" +run_test "DtChat/test_peers" "$B/lib/libDtChat/test_peers" run_test "DtChat/test_adapter" "$B/lib/libDtChat/test_adapter" run_test "DtChat/test_instance" "$B/lib/libDtChat/test_instance"