Skip to content

Commit

Permalink
mdns: distinguish fake ships in the mdns namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova committed Jan 16, 2024
1 parent ae3e909 commit 1929904
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
9 changes: 7 additions & 2 deletions pkg/vere/io/ames.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
32 changes: 25 additions & 7 deletions pkg/vere/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/vere/mdns.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "noun.h"
#include <stdbool.h>

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);

0 comments on commit 1929904

Please sign in to comment.