Files
lotherk 38304c4378 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.
2026-11-16 19:50:16 +00:00

287 lines
5.7 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}