From ae3e9098e9396b4525ca3b51e0dd18821c1e64a0 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 16 Jan 2024 18:15:02 +0200 Subject: [PATCH 1/2] mdns: better error message on linux if avahi not installed --- pkg/vere/mdns.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index e62b7ea767..961dd54742 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -182,7 +182,15 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) NULL, NULL, htons(port), 0, NULL, register_cb, (void*)register_payload); if (err != kDNSServiceErr_NoError) { - u3l_log("mdns: service register error %i", err); + if (err == kDNSServiceErr_Unknown) { + #if defined(U3_OS_linux) + u3l_log("mdns: init failed, install avahi on this system for mdns support"); + #else + u3l_log("mdns: service register error %i", err); + # endif + } else { + u3l_log("mdns: service register error %i", err); + } DNSServiceRefDeallocate(sref); return; } From 1929904fd5386185b12aa89723c9948426e419b1 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 16 Jan 2024 18:15:24 +0200 Subject: [PATCH 2/2] mdns: distinguish fake ships in the mdns namespace --- pkg/vere/io/ames.c | 9 +++++++-- pkg/vere/mdns.c | 32 +++++++++++++++++++++++++------- pkg/vere/mdns.h | 5 +++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 9561d6d208..412a310e32 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2762,10 +2762,15 @@ _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) /* _ames_put_dear(): send lane to arvo after hearing mdns response */ static void -_ames_put_dear(c3_c* ship, c3_w s_addr, c3_s port, void* context) +_ames_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) { u3_ames* sam_u = (u3_ames*)context; + // one is loobean one is boolean + if (fake == sam_u->pir_u->fak_o) { + return; + } + u3_lane lan; lan.pip_w = ntohl(s_addr); lan.por_s = ntohs(port); @@ -2866,7 +2871,7 @@ _ames_io_start(u3_ames* sam_u) char* our_s = u3r_string(our); u3z(our); - mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); c3_free(our_s); } diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 961dd54742..0875489625 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -8,6 +8,7 @@ typedef struct _mdns_payload { mdns_cb* cb; DNSServiceRef sref; char who[58]; + bool fake; uint16_t port; uv_poll_t poll; void* context; @@ -26,7 +27,7 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* r u3l_log("mdns: getaddrinfo error: %s", uv_strerror(status)); } else { struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; - payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); + payload->cb(payload->who, payload->fake, addr->sin_addr.s_addr, payload->port, payload->context); } payload->poll.data = payload; @@ -60,11 +61,18 @@ static void resolve_cb(DNSServiceRef sref, payload->sref = sref; payload->port = port; - int i; + char *start = name; + if (strncmp(name, "fake-", 4) == 0) { + payload->fake = 1; + start = name + 5; + } else { + payload->fake = 0; + } + payload->who[0] = '~'; - for (i = 0; name[i] != '\0' && name[i] != '.' && i < 58; ++i) + for (int i = 0; start[i] != '\0' && start[i] != '.' && i < 58; ++i) { - payload->who[i+1] = name[i]; + payload->who[i+1] = start[i]; } struct addrinfo hints; @@ -166,7 +174,7 @@ static void register_cb(DNSServiceRef sref, uv_close((uv_handle_t*)&payload->poll, register_close_cb); } -void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) +void mdns_init(uint16_t port, bool fake, char* our, mdns_cb* cb, void* context) { #if defined(U3_OS_linux) setenv("AVAHI_COMPAT_NOWARN", "1", 0); @@ -177,8 +185,18 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) DNSServiceRef sref; DNSServiceErrorType err; - char* our_no_sig = our + 1; // certain url parsers don't like the ~ - err = DNSServiceRegister(&sref, 0, 0, our_no_sig, "_ames._udp", + + char* domain; + char s[63] = {0}; + if (fake) { + strcat(s, "fake-"); + strcat(s, our + 1); // certain url parsers don't like the ~ + domain = s; + } else { + domain = our + 1; + } + + err = DNSServiceRegister(&sref, 0, 0, domain, "_ames._udp", NULL, NULL, htons(port), 0, NULL, register_cb, (void*)register_payload); if (err != kDNSServiceErr_NoError) { diff --git a/pkg/vere/mdns.h b/pkg/vere/mdns.h index 66c3477179..8ddddd9392 100644 --- a/pkg/vere/mdns.h +++ b/pkg/vere/mdns.h @@ -1,5 +1,6 @@ #include "noun.h" +#include -typedef void mdns_cb(c3_c* ship, c3_w s_addr, c3_s port, void* context); +typedef void mdns_cb(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context); -void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); +void mdns_init(uint16_t port, bool fake, char* our, mdns_cb* cb, void* context);