X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-daemon%2Fstatic-hosts.c;h=34f531f4998c25610de7c6954f679741dd42003a;hb=7a5b2f69af7d36d6cd4153142f125fa011784e03;hp=8e16d8428d1061d1feae89295bc9951cf7f03547;hpb=187fe65eead4e4afdfe64b1b1ee72d276ca10d51;p=catta diff --git a/avahi-daemon/static-hosts.c b/avahi-daemon/static-hosts.c index 8e16d84..34f531f 100644 --- a/avahi-daemon/static-hosts.c +++ b/avahi-daemon/static-hosts.c @@ -1,18 +1,16 @@ -/* $Id$ */ - /*** This file is part of avahi. - + avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + avahi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 @@ -40,13 +38,16 @@ typedef struct StaticHost StaticHost; struct StaticHost { AvahiSEntryGroup *group; + int iteration; - char *host, *ip; + char *host; + AvahiAddress address; AVAHI_LLIST_FIELDS(StaticHost, hosts); }; static AVAHI_LLIST_HEAD(StaticHost, hosts) = NULL; +static int current_iteration = 0; static void add_static_host_to_server(StaticHost *h); static void remove_static_host_from_server(StaticHost *h); @@ -66,13 +67,13 @@ static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGro break; case AVAHI_ENTRY_GROUP_ESTABLISHED: - avahi_log_notice ("Static Host \"%s\" successfully established.", h->host); + avahi_log_notice ("Static host name \"%s\" successfully established.", h->host); break; case AVAHI_ENTRY_GROUP_FAILURE: - avahi_log_notice ("Failed to establish Static Host \"%s\": %s.", h->host, avahi_strerror (avahi_server_errno (s))); + avahi_log_notice ("Failed to establish static host name \"%s\": %s.", h->host, avahi_strerror (avahi_server_errno (s))); break; - + case AVAHI_ENTRY_GROUP_UNCOMMITED: case AVAHI_ENTRY_GROUP_REGISTERING: ; @@ -81,12 +82,12 @@ static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGro static StaticHost *static_host_new(void) { StaticHost *s; - + s = avahi_new(StaticHost, 1); s->group = NULL; s->host = NULL; - s->ip = NULL; + s->iteration = current_iteration; AVAHI_LLIST_PREPEND(StaticHost, hosts, hosts, s); @@ -98,73 +99,93 @@ static void static_host_free(StaticHost *s) { AVAHI_LLIST_REMOVE(StaticHost, hosts, hosts, s); - avahi_s_entry_group_free (s->group); + if (s->group) + avahi_s_entry_group_free (s->group); avahi_free(s->host); - avahi_free(s->ip); - + avahi_free(s); } +static StaticHost *static_host_find(const char *host, const AvahiAddress *a) { + StaticHost *h; + + assert(host); + assert(a); + + for (h = hosts; h; h = h->hosts_next) + if (!strcmp(h->host, host) && !avahi_address_cmp(a, &h->address)) + return h; + + return NULL; +} + static void add_static_host_to_server(StaticHost *h) { - AvahiAddress a; - int err; if (!h->group) - h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h); + if (!(h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h))) { + avahi_log_error("avahi_s_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(avahi_server))); + return; + } - if (!avahi_address_parse (h->ip, AVAHI_PROTO_UNSPEC, &a)) { - avahi_log_error("Static host %s: avahi_address_parse failed", h->host); - return; - } + if (avahi_s_entry_group_is_empty(h->group)) { + AvahiProtocol p; + int err; + const AvahiServerConfig *config; + config = avahi_server_get_config(avahi_server); - if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, h->host, &a))) { - avahi_log_error ("Static host %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err)); - return; - } + p = (h->address.proto == AVAHI_PROTO_INET && config->publish_a_on_ipv6) || + (h->address.proto == AVAHI_PROTO_INET6 && config->publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : h->address.proto; - avahi_s_entry_group_commit (h->group); + if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, p, 0, h->host, &h->address)) < 0) { + avahi_log_error ("Static host name %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err)); + return; + } + + avahi_s_entry_group_commit (h->group); + } } static void remove_static_host_from_server(StaticHost *h) { - avahi_s_entry_group_reset (h->group); + if (h->group) + avahi_s_entry_group_reset (h->group); } - + void static_hosts_add_to_server(void) { StaticHost *h; - for (h = hosts; h; h = h->hosts_next) { + for (h = hosts; h; h = h->hosts_next) add_static_host_to_server(h); - } } void static_hosts_remove_from_server(void) { StaticHost *h; - for (h = hosts; h; h = h->hosts_next) { + for (h = hosts; h; h = h->hosts_next) remove_static_host_from_server(h); - } } void static_hosts_load(int in_chroot) { FILE *f; unsigned int line = 0; - const char *filename = (in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts"); + StaticHost *h, *next; + const char *filename = in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts"; - if (!(f = fopen(filename, "r"))) - { + if (!(f = fopen(filename, "r"))) { if (errno != ENOENT) avahi_log_error ("Failed to open static hosts file: %s", strerror (errno)); return; } + current_iteration++; + while (!feof(f)) { unsigned int len; char ln[256], *s; char *host, *ip; - StaticHost *h; + AvahiAddress a; if (!fgets(ln, sizeof (ln), f)) break; @@ -197,7 +218,7 @@ void static_hosts_load(int in_chroot) { avahi_log_error("%s:%d: Error, unexpected end of line!", filename, line); avahi_free(host); avahi_free(ip); - break; + goto fail; } /* Skip over the host */ @@ -205,20 +226,48 @@ void static_hosts_load(int in_chroot) { /* Skip past any more spaces */ s += strspn(s, " \t"); - + /* Anything left? */ if (*s != 0) { avahi_log_error ("%s:%d: Junk on the end of the line!", filename, line); avahi_free(host); avahi_free(ip); - break; + goto fail; + } + + if (!avahi_address_parse(ip, AVAHI_PROTO_UNSPEC, &a)) { + avahi_log_error("Static host name %s: failed to parse address %s", host, ip); + avahi_free(host); + avahi_free(ip); + goto fail; } - h = static_host_new(); - h->host = host; - h->ip = ip; + avahi_free(ip); + + if ((h = static_host_find(host, &a))) + avahi_free(host); + else { + h = static_host_new(); + h->host = host; + h->address = a; + + avahi_log_info("Loading new static hostname %s.", h->host); + } + + h->iteration = current_iteration; } + for (h = hosts; h; h = next) { + next = h->hosts_next; + + if (h->iteration != current_iteration) { + avahi_log_info("Static hostname %s vanished, removing.", h->host); + static_host_free(h); + } + } + +fail: + fclose(f); }