Files
lotherk 8ee96c2063 mcde: add DNS-SD announce and watch for dtchat's local mode (T5.2)
McdeSys.h: mcde_sys_net_announce()/poll/ready/stop and mcde_sys_net_watch()/poll/next/close with McdeSysService (name, type, host, TXT, port, gone). mdns_avahi.c uses avahi-client with an own AvahiSimplePoll per handle (no thread; the poll call runs one iteration), publishes an entry group while the daemon runs, frees it for the goodbye, browses and resolves NEW services, queues REMOVE, and survives a missing daemon (AVAHI_CLIENT_NO_FAIL, CONNECTING). mdns_netbsd.c does the same with libdns_sd (DNSServiceRegister with a TXTRecordRef, DNSServiceBrowse, DNSServiceResolve per NEW service, poll on DNSServiceRefSockFD then DNSServiceProcessResult); it compiles against the sysroot with -ldns_sd and has not run on NetBSD. mdns_stub.c answers ENOTSUP when the build has no client. configure checks avahi-client/avahi-common on non-NetBSD with AM_CONDITIONAL MCDE_AVAHI. test_netwatch announces _mcde-test._tcp on 4711 with a TXT record, sees it through its own watch (name, TXT, port, address) and stops the announce; against the running avahi-daemon it reports 'announced and seen' and 'goodbye seen', all ok. test_netwatch is a noinst program and a harness entry. In-tree build pending: a foreign release chain holds the lock (pid 141722).
2026-11-17 07:36:03 +00:00

113 lines
2.7 KiB
C

/*
* test_netwatch.c -- McdeSys: DNS-SD announce and watch (T5.2)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Announces a test service, watches its type and expects the service with its
* TXT record, then stops the announce and expects the goodbye. A system
* without a running mDNS daemon is no failure: the test says so and exits 0.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <Mcde/McdeSys.h>
#define TYPE "_mcde-test._tcp"
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static long long
now_ms(void)
{
struct timespec ts;
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
int
main(void)
{
McdeSysAnnounce *announce = NULL;
McdeSysWatch *watch = NULL;
char name[128];
char txt[256];
McdeSysService svc;
int seen = 0;
int gone = 0;
long long deadline;
snprintf(name, sizeof(name), "MCDE watch %ld", (long)getpid());
snprintf(txt, sizeof(txt), "v=1;id=watch-%ld;name=test", (long)getpid());
if (mcde_sys_net_announce(&announce, TYPE, name, 4711, txt) !=
MCDE_SYS_OK) {
printf("netwatch: no announce (%s), skipped\n",
mcde_sys_error());
return 0;
}
if (mcde_sys_net_watch(&watch, TYPE) != MCDE_SYS_OK) {
printf("netwatch: no watch (%s), skipped\n", mcde_sys_error());
mcde_sys_net_announce_stop(announce);
return 0;
}
deadline = now_ms() + 8000;
while (!seen && now_ms() < deadline) {
int rc;
(void)mcde_sys_net_announce_poll(announce, 100);
rc = mcde_sys_net_watch_poll(watch, 100);
if (rc < 0)
break;
while (mcde_sys_net_watch_next(watch, &svc) == 1) {
if (strcmp(svc.name, name) == 0 && !svc.gone) {
seen = 1;
CHECK(strstr(svc.txt, "id=watch-") != NULL);
CHECK(svc.port == 4711);
CHECK(svc.host[0] != '\0');
}
}
}
CHECK(seen);
printf("netwatch: announced and seen (%s)\n", name);
if (seen) {
mcde_sys_net_announce_stop(announce);
announce = NULL;
deadline = now_ms() + 5000;
while (!gone && now_ms() < deadline) {
if (mcde_sys_net_watch_poll(watch, 100) < 0)
break;
while (mcde_sys_net_watch_next(watch, &svc) == 1)
if (strcmp(svc.name, name) == 0 && svc.gone)
gone = 1;
}
/* the goodbye is not guaranteed on a busy network */
printf("netwatch: %s\n", gone ? "goodbye seen" :
"goodbye not seen (mDNS may hold the cache)");
}
if (announce != NULL)
mcde_sys_net_announce_stop(announce);
mcde_sys_net_watch_close(watch);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_netwatch: all ok\n");
return 0;
}