]> git.meshlink.io Git - catta/blob - avahi-daemon/static-hosts.c
8b1eb58e281533050c06fa9d784c4f138d95fc22
[catta] / avahi-daemon / static-hosts.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29
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>
35
36 #include "main.h"
37 #include "static-hosts.h"
38
39 typedef struct StaticHost StaticHost;
40
41 struct StaticHost {
42     AvahiSEntryGroup *group;
43
44     char *host, *ip;
45
46     AVAHI_LLIST_FIELDS(StaticHost, hosts);
47 };
48
49 static AVAHI_LLIST_HEAD(StaticHost, hosts) = NULL;
50
51 static void add_static_host_to_server(StaticHost *h);
52 static void remove_static_host_from_server(StaticHost *h);
53
54 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
55     StaticHost *h;
56
57     assert(s);
58     assert(eg);
59
60     h = userdata;
61
62     switch (state) {
63
64         case AVAHI_ENTRY_GROUP_COLLISION:
65             avahi_log_error("Host name conflict for \"%s\", not established.", h->host);
66             break;
67
68         case AVAHI_ENTRY_GROUP_ESTABLISHED:
69             avahi_log_notice ("Static Host \"%s\" successfully established.", h->host);
70             break;
71
72         case AVAHI_ENTRY_GROUP_FAILURE:
73             avahi_log_notice ("Failed to establish Static Host \"%s\": %s.", h->host, avahi_strerror (avahi_server_errno (s)));
74             break;
75         
76         case AVAHI_ENTRY_GROUP_UNCOMMITED:
77         case AVAHI_ENTRY_GROUP_REGISTERING:
78             ;
79     }
80 }
81
82 static StaticHost *static_host_new(void) {
83     StaticHost *s;
84     
85     s = avahi_new(StaticHost, 1);
86
87     s->group = NULL;
88     s->host = NULL;
89     s->ip = NULL;
90
91     AVAHI_LLIST_PREPEND(StaticHost, hosts, hosts, s);
92
93     return s;
94 }
95
96 static void static_host_free(StaticHost *s) {
97     assert(s);
98
99     AVAHI_LLIST_REMOVE(StaticHost, hosts, hosts, s);
100
101     avahi_s_entry_group_free (s->group);
102
103     avahi_free(s->host);
104     avahi_free(s->ip);
105     
106     avahi_free(s);
107 }
108
109 static void add_static_host_to_server(StaticHost *h)
110 {
111     AvahiAddress a;
112     int err;
113
114     if (!h->group)
115         h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h);
116
117     if (!avahi_address_parse (h->ip, AVAHI_PROTO_UNSPEC, &a)) {
118         avahi_log_error("Static host %s: avahi_address_parse failed", h->host);
119         return;
120     }
121
122     if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, h->host, &a))) {
123         avahi_log_error ("Static host %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err));
124         return;
125     }
126
127     avahi_s_entry_group_commit (h->group);
128 }
129
130 static void remove_static_host_from_server(StaticHost *h)
131 {
132     avahi_s_entry_group_reset (h->group);
133 }
134  
135 void static_hosts_add_to_server(void) {
136     StaticHost *h;
137
138     for (h = hosts; h; h = h->hosts_next) {
139         add_static_host_to_server(h);
140     }
141 }
142
143 void static_hosts_remove_from_server(void) {
144     StaticHost *h;
145
146     for (h = hosts; h; h = h->hosts_next) {
147         remove_static_host_from_server(h);
148     }
149 }
150
151 void static_hosts_load(int in_chroot) {
152     FILE *f;
153     unsigned int line = 0;
154     const char *filename = (in_chroot ? "/hosts" : AVAHI_CONFIG_DIR "/hosts");
155
156     if (!(f = fopen(filename, "r")))
157     {
158         avahi_log_error ("Failed to open static hosts file: %s", strerror (errno));
159         return;
160     }
161
162     while (!feof(f)) {
163         unsigned int len;
164         char ln[256], *s;
165         char *host, *ip;
166         StaticHost *h;
167
168         if (!fgets(ln, sizeof (ln), f))
169             break;
170
171         line++;
172
173                 /* Find the start of the line, ignore whitespace */
174                 s = ln + strspn(ln, " \t");
175                 /* Set the end of the string to NULL */
176                 s[strcspn(s, "#\r\n")] = 0;
177
178                 /* Ignore comment (#) and blank lines (*/
179                 if (*s == '#' || *s == 0)
180                         continue;
181
182                 /* Read the first string (ip) up to the next whitespace */
183                 len = strcspn(s, " \t");
184                 ip = avahi_strndup(s, len);
185
186                 /* Skip past it */
187                 s += len;
188
189                 /* Find the next token */
190                 s += strspn(s, " \t");
191                 len = strcspn(s, " \t");
192                 host = avahi_strndup(s, len);
193
194                 if (*host == 0)
195                 {
196                         avahi_log_error ("%s:%d: Error, unexpected end of line!", filename, line);
197             break;
198                 }
199
200         /* Skip past any more spaces */
201                 s += strspn(s+len, " \t");
202         
203         /* Anything left? */
204                 if (*(s+len) != 0) {
205                         avahi_log_error ("%s:%d: Junk on the end of the line!", filename, line);
206             break;
207                 }
208
209         h = static_host_new();
210         h->host = host;
211         h->ip = ip;
212     }
213 }
214
215 void static_hosts_free_all (void)
216 {
217     StaticHost *h;
218
219     for (h = hosts; h; h = hosts->hosts_next)
220     {
221         static_host_free (h);
222     }
223 }