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: add mdns for local network routing #511

Merged
merged 14 commits into from
Dec 5, 2023
Merged

mdns: add mdns for local network routing #511

merged 14 commits into from
Dec 5, 2023

Conversation

pkova
Copy link
Collaborator

@pkova pkova commented Sep 4, 2023

This PR implements mDNS for vere, significantly improving Urbit routing inside local networks. On startup vere will now broadcast a service with the format dinleb-rambep._ames._udp.local. Vere will also listen for any services on the local network with the signature of _ames._udp. Whenever new services are found vere will send the lane to arvo.

On macOS we can use Bonjour and don't need to pull down any dependencies at all. On Linux we can use Avahi, specifically the bonjour compatibility layer. The header we're using is dns_sd.h, documented here.

Tested on macos aarch64 and linux x86_64 so far. To try it out, build vere from this PR, start a fakezod and observe:

# macos
dns-sd -B _ames._udp

# linux
avahi-browse -r _ames._udp

You can also start another fake ship on the same network and do |ames-verb %rcv %odd to see your fakeships find eachother. For this second part you need the companion arvo PR, here's a pill that has it already:

./urbit -F zod -u https://urbit-foundation.s3.us-east-2.amazonaws.com/dear.pill

pkg/vere/vere.h Outdated
/* mdns_init(): initialize mdns.
*/
void
mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should I put this signature? vere.h seems wrong but don't really know where it should go.

@pkova
Copy link
Collaborator Author

pkova commented Sep 5, 2023

I wonder what we should do in the following scenario:

Someone boots a fresh ship on your local network. You receive the mDNS notification and send a %dear to arvo, but since this is a fresh boot you won't have peer-state for the ship yet and the lane will get dropped on the ground. You won't receive a mDNS notification about this lane until either ship restarts vere.

*/
static void
_ames_put_dear(char* ship, unsigned long s_addr, uint16_t port, void* context)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use c3 types: _ames_put_dear(c3_c*, c3_w, c3_s, void*)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (our == u3t(shp_u)) {
u3z(our);
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early return leaks shp_u

u3_auto_peer(
u3_auto_plan(&sam_u->car_u,
u3_ovum_init(0, c3__a, wir, cad)),
0, 0, _mdns_dear_bail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to track or handle failures with these events.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get real ugly crashes on arvo versions that don't support %dear unless I handle the bail myself.

pkg/vere/mdns.c Outdated
uv_poll_stop(payload->poll);
c3_free(payload->poll);
c3_free(payload->our);
c3_free(payload);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave a comment explaining why we leak sref.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pkg/vere/mdns.c Outdated
register_payload->cb = cb;
register_payload->context = context;
register_payload->our = our;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you don't need to save our in payload. I would try removing that, and also just passing our + 1 to DNSServiceRegister() (maybe after checking that '~' = *our).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pkg/vere/mdns.c Outdated
char* who;
char* our;
uint16_t port;
uv_poll_t* poll;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like poll always has the same lifetime as payload, so it can just be uv_poll_t (no *).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pkg/vere/mdns.c Outdated
poll->data = (void *)sref;
payload->poll = poll;
uv_poll_init(loop, poll, fd);
uv_poll_start(poll, UV_READABLE, poll_cb);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If poll has the same lifetime as payload (see above), you can remove the malloc here and just uv_poll_init(loop, &(payload->poll), fd);`

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And done.

@pkova pkova force-pushed the pkova/mdns branch 2 times, most recently from 1a48dfa to de78b05 Compare September 8, 2023 12:36
@pkova pkova merged commit e7e06d0 into develop Dec 5, 2023
5 checks passed
@pkova pkova deleted the pkova/mdns branch December 5, 2023 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants