4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <avahi-common/llist.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/error.h>
33 #include <avahi-core/log.h>
34 #include <avahi-core/publish.h>
37 #include "static-hosts.h"
39 typedef struct StaticHost StaticHost;
42 AvahiSEntryGroup *group;
46 AVAHI_LLIST_FIELDS(StaticHost, hosts);
49 static AVAHI_LLIST_HEAD(StaticHost, hosts) = NULL;
51 static void add_static_host_to_server(StaticHost *h);
52 static void remove_static_host_from_server(StaticHost *h);
54 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
64 case AVAHI_ENTRY_GROUP_COLLISION:
65 avahi_log_error("Host name conflict for \"%s\", not established.", h->host);
68 case AVAHI_ENTRY_GROUP_ESTABLISHED:
69 avahi_log_notice ("Static host name \"%s\" successfully established.", h->host);
72 case AVAHI_ENTRY_GROUP_FAILURE:
73 avahi_log_notice ("Failed to establish static host name \"%s\": %s.", h->host, avahi_strerror (avahi_server_errno (s)));
76 case AVAHI_ENTRY_GROUP_UNCOMMITED:
77 case AVAHI_ENTRY_GROUP_REGISTERING:
82 static StaticHost *static_host_new(void) {
85 s = avahi_new(StaticHost, 1);
91 AVAHI_LLIST_PREPEND(StaticHost, hosts, hosts, s);
96 static void static_host_free(StaticHost *s) {
99 AVAHI_LLIST_REMOVE(StaticHost, hosts, hosts, s);
102 avahi_s_entry_group_free (s->group);
110 static void add_static_host_to_server(StaticHost *h)
115 const AvahiServerConfig *config;
118 if (!(h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h))) {
119 avahi_log_error("avahi_s_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(avahi_server)));
123 if (!avahi_address_parse (h->ip, AVAHI_PROTO_UNSPEC, &a)) {
124 avahi_log_error("Static host name %s: avahi_address_parse failed", h->host);
128 config = avahi_server_get_config(avahi_server);
130 p = (a.proto == AVAHI_PROTO_INET && config->publish_a_on_ipv6) ||
131 (a.proto == AVAHI_PROTO_INET6 && config->publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : a.proto;
133 if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, p, 0, h->host, &a)) < 0) {
134 avahi_log_error ("Static host name %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err));
138 avahi_s_entry_group_commit (h->group);
141 static void remove_static_host_from_server(StaticHost *h)
144 avahi_s_entry_group_reset (h->group);
147 void static_hosts_add_to_server(void) {
150 for (h = hosts; h; h = h->hosts_next) {
151 add_static_host_to_server(h);
155 void static_hosts_remove_from_server(void) {
158 for (h = hosts; h; h = h->hosts_next) {
159 remove_static_host_from_server(h);
163 void static_hosts_load(int in_chroot) {
165 unsigned int line = 0;
166 const char *filename = (in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts");
168 if (!(f = fopen(filename, "r")))
171 avahi_log_error ("Failed to open static hosts file: %s", strerror (errno));
181 if (!fgets(ln, sizeof (ln), f))
186 /* Find the start of the line, ignore whitespace */
187 s = ln + strspn(ln, " \t");
188 /* Set the end of the string to NULL */
189 s[strcspn(s, "#\r\n")] = 0;
191 /* Ignore blank lines */
195 /* Read the first string (ip) up to the next whitespace */
196 len = strcspn(s, " \t");
197 ip = avahi_strndup(s, len);
202 /* Find the next token */
203 s += strspn(s, " \t");
204 len = strcspn(s, " \t");
205 host = avahi_strndup(s, len);
209 avahi_log_error("%s:%d: Error, unexpected end of line!", filename, line);
215 /* Skip over the host */
218 /* Skip past any more spaces */
219 s += strspn(s, " \t");
223 avahi_log_error ("%s:%d: Junk on the end of the line!", filename, line);
229 h = static_host_new();
237 void static_hosts_free_all (void)
240 static_host_free(hosts);