Skip to content

Commit

Permalink
mdns: address joes feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova committed Sep 8, 2023
1 parent 390735c commit de78b05
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 53 deletions.
1 change: 1 addition & 0 deletions pkg/vere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ vere_library(
hdrs = [
"db/lmdb.h",
"vere.h",
"mdns.h",
] + select({
"@platforms//os:macos": [],
"@platforms//os:linux": [
Expand Down
16 changes: 10 additions & 6 deletions pkg/vere/io/ames.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// @file

#include "vere.h"
#include "mdns.h"

#include "noun.h"
#include "ur.h"
Expand Down Expand Up @@ -2063,15 +2064,14 @@ _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);
}

/* _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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
55 changes: 20 additions & 35 deletions pkg/vere/mdns.c
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}

Expand All @@ -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",
Expand All @@ -197,7 +182,7 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context)
return;
}

dns_sd_cb(sref, register_payload);
init_sref_poll(sref, register_payload);

mdns_payload* browse_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload));

Expand All @@ -215,6 +200,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);

}
5 changes: 5 additions & 0 deletions pkg/vere/mdns.h
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 0 additions & 12 deletions pkg/vere/vere.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit de78b05

Please sign in to comment.