0

I am attempting to setup CNAMEs in Avahi in order to broadcast multiple hostnames. I've found a variety of examples online that all work but they all require long lived processes:

https://github.com/Dalee/avahi-cname-aliases/blob/master/avahi_cname_aliases/init.py#L83

    # run and stay foreground
    def run(self):
        self.set_handlers()
        self.load_aliases()
        self.publish_aliases()

        # keep aliases published
        while self.running:
            time.sleep(TTL)

https://github.com/george-hawkins/avahi-aliases-notes/blob/master/avahi-alias#L48

    for name in sys.argv[1:]:
        publish_cname(name)
    try:
        # Just loop forever
        while 1: time.sleep(60)
    except KeyboardInterrupt:
        print("Exiting")

https://public.msli.com/lcs/jaf/publish_cnames.c

/** cnames should be a NULL-terminated array of alias hostnames for this host.
  * Example invocation:  const char * cnames = {"foo.local", "bar.local", NULL}; PublishAvahiCNames(cnames);  
  * Note that this function normally does not ever return!
  */
void PublishAvahiCNames(const char ** cnames)

I've confirmed that if the process that sets up the CNAME ends then the CNAME disappears but I can't find any documentation on avahi or dbus that indicates why these tools need to live forever. My use of dbus-monitor hasn't shown anything obvious that the scripts are doing to keep the CNAMEs alive.

Does anyone know what these scripts are doing to keep the published CNAMEs active in avahi?

Stephen L
  • 105
  • 3

1 Answers1

1

The clients do nothing, just stay connected to the D-Bus. If they vanish the server cleans up their entries, cf. dbus-protocol.c:

} else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
    char *name, *old, *new;

    if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
        avahi_log_warn("Error parsing NameOwnerChanged message");
        goto fail;
    }

    if (!*new) {
        Client *client;

        if ((client = client_get(name, FALSE))) {
            avahi_log_debug(__FILE__": client %s vanished.", name);
            client_free(client);
        }
    }
}
Stephen L
  • 105
  • 3
Piotr P. Karwasz
  • 5,292
  • 2
  • 9
  • 20
  • Do you know why this is the design of the Avahi dbus interface? Is it inherent to how dbus works or specific to Avahi? – Stephen L Nov 22 '19 at 23:27
  • 1
    I believe they don't want to have stale entries from dead processes. You might solve your problem copying what's done in [static-hosts.c](https://github.com/lathiat/avahi/blob/master/avahi-daemon/static-hosts.c). Those entries are administered by the server and don't vanish. E.g. add a config file called something like `local.db`, which contains a classical DNS zone. – Piotr P. Karwasz Nov 22 '19 at 23:30