From 823be7f47b1e2299fe2b0b5bc4fedb4993a1e09a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 22 Oct 2023 07:30:57 +0200 Subject: [PATCH] Instance: implement Zeroconf publisher --- src/Instance.cxx | 18 +++++++++++++++++- src/Main.cxx | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Instance.cxx b/src/Instance.cxx index 028c06c..c13a1cc 100644 --- a/src/Instance.cxx +++ b/src/Instance.cxx @@ -78,7 +78,23 @@ Instance::AddListener(const ListenerConfig &config) listeners.emplace_front(*this, config.Create(SOCK_STREAM)); #ifdef HAVE_AVAHI - // TODO + auto &listener = listeners.front(); + + if (!config.zeroconf_service.empty()) { + /* ask the kernel for the effective address via + getsockname(), because it may have changed, e.g. if + the kernel has selected a port for us */ + const auto local_address = listener.GetLocalAddress(); + if (local_address.IsDefined()) { + const char *const interface = config.interface.empty() + ? nullptr + : config.interface.c_str(); + + avahi_services.emplace_front(config.zeroconf_service.c_str(), + interface, local_address, + config.v6only); + } + } #endif // HAVE_AVAHI } diff --git a/src/Main.cxx b/src/Main.cxx index 6ae620b..f8c50c9 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -60,6 +60,10 @@ try { for (const auto &i : config.listeners) instance.AddListener(i); +#ifdef HAVE_AVAHI + instance.EnableZeroconf(); +#endif // HAVE_AVAHI + #ifdef HAVE_LIBSYSTEMD /* tell systemd we're ready */ sd_notify(0, "READY=1");