Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mdns: distinguish fake ships in the mdns namespace #584

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 34 additions & 8 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,12 +185,30 @@ 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) {
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;
}
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);