diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 249fc42e42..7e772ae61c 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -119,6 +119,7 @@ vere_library( hdrs = [ "db/lmdb.h", "vere.h", + "mdns.h", ] + select({ "@platforms//os:macos": [], "@platforms//os:linux": [ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 6dd6cff67d..cba1dbf48d 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1,6 +1,7 @@ /// @file #include "vere.h" +#include "mdns.h" #include "noun.h" #include "ur.h" @@ -2063,7 +2064,6 @@ _ames_recv_cb(uv_udp_t* wax_u, static void _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) { - u3l_log("mdns: %%dear failure;"); u3z(lud); u3_ovum_free(egg_u); } @@ -2071,7 +2071,7 @@ _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(char* ship, unsigned long s_addr, uint16_t port, void* context) +_ames_put_dear(c3_c* ship, c3_w s_addr, c3_s port, void* context) { u3_ames* sam_u = (u3_ames*)context; @@ -2088,6 +2088,7 @@ _ames_put_dear(char* ship, unsigned long s_addr, uint16_t port, void* context) u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); if (our == u3t(shp_u)) { + u3z(shp_u); u3z(our); return; } @@ -2168,11 +2169,14 @@ _ames_io_start(u3_ames* sam_u) u3l_log("ames: live on %d (localhost only)", sam_u->pir_u->por_s); } - u3_noun our = u3dc("scot", 'p', who); - char* our_s = u3r_string(our); - u3z(our); + { + u3_noun our = u3dc("scot", 'p', u3k(who)); + char* our_s = u3r_string(our); + u3z(our); - mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + c3_free(our_s); + } uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index b38e5ea737..9489ad4841 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -1,16 +1,15 @@ /// @file #include "vere.h" - +#include "mdns.h" #include "dns_sd.h" typedef struct _mdns_payload { mdns_cb* cb; DNSServiceRef sref; - char* who; - char* our; + char who[58]; uint16_t port; - uv_poll_t* poll; + uv_poll_t poll; void* context; } mdns_payload; @@ -30,7 +29,6 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* r struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); - c3_free(payload->who); uv_freeaddrinfo(res); c3_free(req); DNSServiceRefDeallocate(payload->sref); @@ -51,8 +49,7 @@ static void resolve_cb(DNSServiceRef sref, { mdns_payload* payload = (mdns_payload*)context; - uv_poll_stop(payload->poll); - c3_free(payload->poll); + uv_poll_stop(&payload->poll); if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns resolve error %d", err); @@ -64,19 +61,13 @@ static void resolve_cb(DNSServiceRef sref, payload->sref = sref; payload->port = port; - char* who = strtok(name, "."); - - if (who == NULL) { - u3l_log("mdns: invalid domain name"); - c3_free(payload); - DNSServiceRefDeallocate(sref); - return; + int i; + payload->who[0] = '~'; + for (i = 0; name[i] != '\0' && name[i] != '.' && i < 58; ++i) + { + payload->who[i+1] = name[i]; } - char* who2 = c3_malloc(strlen(who) + 1); - snprintf(who2, sizeof who2, "~%s", who); - payload->who = who2; - struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; // Request only IPv4 addresses @@ -96,19 +87,17 @@ static void resolve_cb(DNSServiceRef sref, } } -static void poll_cb(uv_poll_t *handle, int status, int events) { +static void poll_cb(uv_poll_t* handle, int status, int events) { DNSServiceRef sref = (DNSServiceRef) handle->data; int err = DNSServiceProcessResult(sref); } -static void dns_sd_cb(DNSServiceRef sref, mdns_payload* payload) { +static void init_sref_poll(DNSServiceRef sref, mdns_payload* payload) { int fd = DNSServiceRefSockFD(sref); uv_loop_t* loop = uv_default_loop(); - uv_poll_t* poll = (uv_poll_t *)c3_malloc(sizeof(uv_poll_t)); - poll->data = (void *)sref; - payload->poll = poll; - uv_poll_init(loop, poll, fd); - uv_poll_start(poll, UV_READABLE, poll_cb); + payload->poll.data = (void*)sref; + uv_poll_init(loop, &payload->poll, fd); + uv_poll_start(&payload->poll, UV_READABLE, poll_cb); } static void browse_cb(DNSServiceRef s, @@ -145,7 +134,7 @@ static void browse_cb(DNSServiceRef s, DNSServiceRefDeallocate(sr); return; } - dns_sd_cb(sr, payload_copy); + init_sref_poll(sr, payload_copy); } } @@ -164,9 +153,9 @@ static void register_cb(DNSServiceRef sref, } else { u3l_log("mdns: %s registered on all interfaces", name); } - uv_poll_stop(payload->poll); - c3_free(payload->poll); - c3_free(payload->our); + // not freeing sref with DNSServiceRefDeallocate since that + // deregisters us from mdns + uv_poll_stop(&payload->poll); c3_free(payload); } @@ -181,10 +170,6 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) mdns_payload* register_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); - register_payload->cb = cb; - register_payload->context = context; - register_payload->our = our; - DNSServiceRef sref; DNSServiceErrorType err; err = DNSServiceRegister(&sref, 0, 0, our, "_ames._udp", @@ -192,12 +177,11 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) if (err != kDNSServiceErr_NoError) { u3l_log("mdns: service register error %i", err); - c3_free(our); DNSServiceRefDeallocate(sref); return; } - dns_sd_cb(sref, register_payload); + init_sref_poll(sref, register_payload); mdns_payload* browse_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); @@ -215,6 +199,6 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) return; } - dns_sd_cb(sref2, browse_payload); + init_sref_poll(sref2, browse_payload); } diff --git a/pkg/vere/mdns.h b/pkg/vere/mdns.h new file mode 100644 index 0000000000..66c3477179 --- /dev/null +++ b/pkg/vere/mdns.h @@ -0,0 +1,5 @@ +#include "noun.h" + +typedef void mdns_cb(c3_c* ship, c3_w s_addr, c3_s port, void* context); + +void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 3154432fc5..cef16810b6 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -674,12 +674,6 @@ uv_timer_t tim_u; // gc timer } u3_king; - /* mdns_cb: callback when mdns finds a new ship - */ - typedef void mdns_cb(char* ship, - unsigned long s_addr, - uint16_t port, - void* context); /* u3_pier_spin(): (re-)activate idle handler */ void @@ -1244,12 +1238,6 @@ u3_auto* u3_lick_io_init(u3_pier* pir_u); - /** mdns, routing for the local network - **/ - /* mdns_init(): initialize mdns. - */ - void - mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); /** HTTP server. **/ /* u3_http_io_init(): initialize http I/O.