]> git.meshlink.io Git - catta/blob - avahi-daemon/static-services.c
1f9b765099f0dd34501bc35e0d680e57b9d67940
[catta] / avahi-daemon / static-services.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 <sys/stat.h>
27 #include <glob.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <glib.h>
35 #include <expat.h>
36
37 #include <avahi-core/llist.h>
38 #include <avahi-core/log.h>
39
40 #include "main.h"
41 #include "static-services.h"
42
43 typedef struct StaticService StaticService;
44 typedef struct StaticServiceGroup StaticServiceGroup;
45
46 struct StaticService {
47     StaticServiceGroup *group;
48     
49     gchar *type;
50     gchar *domain_name;
51     gchar *host_name;
52     guint16 port;
53
54     AvahiStringList *txt_records;
55     
56     AVAHI_LLIST_FIELDS(StaticService, services);
57 };
58
59 struct StaticServiceGroup {
60     gchar *filename;
61     time_t mtime;
62
63     gchar *name, *chosen_name;
64     gboolean replace_wildcards;
65
66     AvahiEntryGroup *entry_group;
67     AVAHI_LLIST_HEAD(StaticService, services);
68     AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
69 };
70
71 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
72
73 static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
74     gchar *r = NULL, *e, *n;
75
76     while ((e = strstr(pattern, a))) {
77         gchar *k;
78
79         k = g_strndup(pattern, e - pattern);
80         if (r)
81             n = g_strconcat(r, k, b, NULL);
82         else
83             n = g_strconcat(k, b, NULL);
84
85         g_free(k);
86         g_free(r);
87         r = n;
88
89         pattern = e + strlen(a);
90     }
91
92     if (!r)
93         return g_strdup(pattern);
94
95     n = g_strconcat(r, pattern, NULL);
96     g_free(r);
97
98     return n;
99 }
100
101 static void add_static_service_group_to_server(StaticServiceGroup *g);
102 static void remove_static_service_group_from_server(StaticServiceGroup *g);
103
104 static StaticService *static_service_new(StaticServiceGroup *group) {
105     StaticService *s;
106     
107     g_assert(group);
108     s = g_new(StaticService, 1);
109     s->group = group;
110
111     s->type = s->host_name = s->domain_name = NULL;
112     s->port = 0;
113
114     s->txt_records = NULL;
115
116     AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
117
118     return s;
119 }
120
121 static StaticServiceGroup *static_service_group_new(gchar *filename) {
122     StaticServiceGroup *g;
123     g_assert(filename);
124
125     g = g_new(StaticServiceGroup, 1);
126     g->filename = g_strdup(filename);
127     g->mtime = 0;
128     g->name = g->chosen_name = NULL;
129     g->replace_wildcards = FALSE;
130     g->entry_group = NULL;
131
132     AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
133     AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
134
135     return g;
136 }
137
138 static void static_service_free(StaticService *s) {
139     g_assert(s);
140     
141     AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
142
143     g_free(s->type);
144     g_free(s->host_name);
145     g_free(s->domain_name);
146
147     avahi_string_list_free(s->txt_records);
148     
149     g_free(s);
150 }
151
152 static void static_service_group_free(StaticServiceGroup *g) {
153     g_assert(g);
154
155     remove_static_service_group_from_server(g);
156
157     while (g->services)
158         static_service_free(g->services);
159
160     AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
161
162     g_free(g->filename);
163     g_free(g->name);
164     g_free(g->chosen_name);
165     g_free(g);
166 }
167
168 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
169     StaticServiceGroup *g = userdata;
170     
171     g_assert(s);
172     g_assert(g);
173     
174     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
175         gchar *n;
176
177         remove_static_service_group_from_server(g);
178
179         n = avahi_alternative_service_name(g->chosen_name);
180         g_free(g->chosen_name);
181         g->chosen_name = n;
182
183         avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
184
185         add_static_service_group_to_server(g);
186     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
187         avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
188 }
189
190 static void add_static_service_group_to_server(StaticServiceGroup *g) {
191     StaticService *s;
192
193     g_assert(g);
194     
195     if (g->entry_group)
196         return;
197     
198     if (g->chosen_name)
199         g_free(g->chosen_name);
200     
201     if (g->replace_wildcards)
202         g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
203     else
204         g->chosen_name = g_strdup(g->name);
205     
206     g->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, g);
207     
208     for (s = g->services; s; s = s->services_next) {
209
210         if (avahi_server_add_service_strlst(
211                 avahi_server,
212                 g->entry_group,
213                 -1, AF_UNSPEC,
214                 g->chosen_name, s->type, 
215                 s->domain_name, s->host_name, s->port,
216                 s->txt_records) < 0) {
217             avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
218             remove_static_service_group_from_server(g);
219             return;
220         }
221     }
222
223     avahi_entry_group_commit(g->entry_group);
224 }
225
226 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
227     g_assert(g);
228
229     if (g->entry_group) {
230         avahi_entry_group_free(g->entry_group);
231         g->entry_group = NULL;
232     }
233 }
234
235 typedef enum {
236     XML_TAG_INVALID,
237     XML_TAG_SERVICE_GROUP,
238     XML_TAG_NAME,
239     XML_TAG_SERVICE,
240     XML_TAG_TYPE,
241     XML_TAG_DOMAIN_NAME,
242     XML_TAG_HOST_NAME,
243     XML_TAG_PORT,
244     XML_TAG_TXT_RECORD
245 } xml_tag_name;
246
247 struct xml_userdata {
248     StaticServiceGroup *group;
249     StaticService *service;
250     xml_tag_name current_tag;
251     gboolean failed;
252     gchar *buf;
253 };
254
255 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
256     struct xml_userdata *u = data;
257     
258     g_assert(u);
259
260     if (u->failed)
261         return;
262
263     if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
264
265         if (attr[0])
266             goto invalid_attr;
267
268         u->current_tag = XML_TAG_SERVICE_GROUP;
269     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
270         u->current_tag = XML_TAG_NAME;
271
272         if (attr[0]) {
273             if (strcmp(attr[0], "replace-wildcards") == 0) 
274                 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
275             else
276                 goto invalid_attr;
277         }
278
279         if (attr[2])
280             goto invalid_attr;
281         
282             
283         
284     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
285         if (attr[0])
286             goto invalid_attr;
287
288         g_assert(!u->service);
289         u->service = static_service_new(u->group);
290
291         u->current_tag = XML_TAG_SERVICE;
292     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
293         if (attr[0])
294             goto invalid_attr;
295
296         u->current_tag = XML_TAG_TYPE;
297     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
298         if (attr[0])
299             goto invalid_attr;
300         
301         u->current_tag = XML_TAG_DOMAIN_NAME;
302     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
303         if (attr[0])
304             goto invalid_attr;
305         
306         u->current_tag = XML_TAG_HOST_NAME;
307     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
308         if (attr[0])
309             goto invalid_attr;
310         
311         u->current_tag = XML_TAG_PORT;
312     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
313         if (attr[0])
314             goto invalid_attr;
315         
316         u->current_tag = XML_TAG_TXT_RECORD;
317     } else {
318         avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
319         u->failed = TRUE;
320     }
321
322     return;
323
324 invalid_attr:
325     avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
326     u->failed = TRUE;
327     return;
328 }
329     
330 static void XMLCALL xml_end(void *data, const char *el) {
331     struct xml_userdata *u = data;
332     g_assert(u);
333
334     if (u->failed)
335         return;
336     
337     switch (u->current_tag) {
338         case XML_TAG_SERVICE_GROUP:
339
340             if (!u->group->name || !u->group->services) {
341                 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
342                 u->failed = TRUE;
343                 return;
344             }
345             
346             u->current_tag = XML_TAG_INVALID;
347             break;
348
349         case XML_TAG_SERVICE:
350
351             if (u->service->port == 0 || !u->service->type) {
352                 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
353                 u->failed = TRUE;
354                 return;
355             }
356             
357             u->service = NULL;
358             u->current_tag = XML_TAG_SERVICE_GROUP;
359             break;
360
361         case XML_TAG_NAME:
362             u->current_tag = XML_TAG_SERVICE_GROUP;
363             break;
364
365         case XML_TAG_PORT: {
366             int p;
367             g_assert(u->service);
368             
369             p = u->buf ? atoi(u->buf) : 0;
370
371             if (p <= 0 || p > 0xFFFF) {
372                 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
373                 u->failed = TRUE;
374                 return;
375             }
376
377             u->service->port = (guint16) p;
378
379             u->current_tag = XML_TAG_SERVICE;
380             break;
381         }
382
383         case XML_TAG_TXT_RECORD: {
384             g_assert(u->service);
385             
386             u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
387             u->current_tag = XML_TAG_SERVICE;
388             break;
389         }
390             
391         case XML_TAG_TYPE:
392         case XML_TAG_DOMAIN_NAME:
393         case XML_TAG_HOST_NAME:
394             u->current_tag = XML_TAG_SERVICE;
395             break;
396
397         case XML_TAG_INVALID:
398             ;
399     }
400
401     g_free(u->buf);
402     u->buf = NULL;
403 }
404
405 static gchar *append_cdata(gchar *t, const gchar *n, int length) {
406     gchar *r, *k;
407     
408     if (!length)
409         return t;
410
411     k = g_strndup(n, length);
412
413     if (t) {
414         r = g_strconcat(t, k, NULL);
415         g_free(k);
416         g_free(t);
417     } else
418         r = k;
419     
420     return r;
421 }
422
423 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
424     struct xml_userdata *u = data;
425     g_assert(u);
426
427     if (u->failed)
428         return;
429
430     switch (u->current_tag) {
431         case XML_TAG_NAME:
432             u->group->name = append_cdata(u->group->name, s, len);
433             break;
434             
435         case XML_TAG_TYPE:
436             g_assert(u->service);
437             u->service->type = append_cdata(u->service->type, s, len);
438             break;
439
440         case XML_TAG_DOMAIN_NAME:
441             g_assert(u->service);
442             u->service->domain_name = append_cdata(u->service->domain_name, s, len);
443             break;
444
445         case XML_TAG_HOST_NAME:
446             g_assert(u->service);
447             u->service->host_name = append_cdata(u->service->host_name, s, len);
448             break;
449
450         case XML_TAG_PORT:
451         case XML_TAG_TXT_RECORD:
452             u->buf = append_cdata(u->buf, s, len);
453             break;
454
455         case XML_TAG_SERVICE_GROUP:
456         case XML_TAG_SERVICE:
457         case XML_TAG_INVALID:
458             ;
459     }
460 }
461
462 static gint static_service_group_load(StaticServiceGroup *g) {
463     XML_Parser parser = NULL;
464     gint fd = -1;
465     struct xml_userdata u;
466     gint r = -1;
467     struct stat st;
468     ssize_t n;
469
470     g_assert(g);
471
472     u.buf = NULL;
473     u.group = g;
474     u.service = NULL;
475     u.current_tag = XML_TAG_INVALID;
476     u.failed = FALSE;
477
478     /* Cleanup old data in this service group, if available */
479     remove_static_service_group_from_server(g);
480     while (g->services)
481         static_service_free(g->services);
482
483     g_free(g->name);
484     g_free(g->chosen_name);
485     g->name = g->chosen_name = NULL;
486     g->replace_wildcards = FALSE;
487     
488     if (!(parser = XML_ParserCreate(NULL))) {
489         avahi_log_error("XML_ParserCreate() failed.");
490         goto finish;
491     }
492
493     if ((fd = open(g->filename, O_RDONLY)) < 0) {
494         avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
495         goto finish;
496     }
497
498     if (fstat(fd, &st) < 0) {
499         avahi_log_error("fstat(): %s", strerror(errno));
500         goto finish;
501     }
502
503     g->mtime = st.st_mtime;
504     
505     XML_SetUserData(parser, &u);
506
507     XML_SetElementHandler(parser, xml_start, xml_end);
508     XML_SetCharacterDataHandler(parser, xml_cdata);
509
510     do {
511         void *buffer;
512
513 #define BUFSIZE (10*1024)
514
515         if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
516             avahi_log_error("XML_GetBuffer() failed.");
517             goto finish;
518         }
519
520         if ((n = read(fd, buffer, BUFSIZE)) < 0) {
521             avahi_log_error("read(): %s\n", strerror(errno));
522             goto finish;
523         }
524
525         if (!XML_ParseBuffer(parser, n, n == 0)) {
526             avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
527             goto finish;
528         }
529
530     } while (n != 0);
531
532     if (!u.failed)
533         r = 0;
534
535 finish:
536
537     if (fd >= 0)
538         close(fd);
539
540     if (parser)
541         XML_ParserFree(parser);
542
543     g_free(u.buf);
544
545     return r;
546 }
547
548 static void load_file(gchar *n) {
549     StaticServiceGroup *g;
550     g_assert(n);
551
552     for (g = groups; g; g = g->groups_next)
553         if (strcmp(g->filename, n) == 0)
554             return;
555
556     avahi_log_info("Loading service file %s", n);
557     
558     g = static_service_group_new(n);
559     if (static_service_group_load(g) < 0) {
560         avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
561         static_service_group_free(g);
562     }
563 }
564
565 void static_service_load(void) {
566     StaticServiceGroup *g, *n;
567     glob_t globbuf;
568     gchar **p;
569
570     for (g = groups; g; g = n) {
571         struct stat st;
572
573         n = g->groups_next;
574
575         if (stat(g->filename, &st) < 0) {
576
577             if (errno == ENOENT)
578                 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
579             else
580                 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
581             
582             static_service_group_free(g);
583         } else if (st.st_mtime != g->mtime) {
584             avahi_log_info("Service group file %s changed, reloading.", g->filename);
585             
586             if (static_service_group_load(g) < 0) {
587                 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
588                 static_service_group_free(g);
589             }
590         }
591     }
592
593     memset(&globbuf, 0, sizeof(globbuf));
594     if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
595         avahi_log_error("Failed to read service directory.");
596     else {
597         for (p = globbuf.gl_pathv; *p; p++)
598             load_file(*p);
599         
600         globfree(&globbuf);
601     }
602 }
603
604 void static_service_free_all(void) {
605     
606     while (groups)
607         static_service_group_free(groups);
608 }
609
610 void static_service_add_to_server(void) {
611     StaticServiceGroup *g;
612
613     for (g = groups; g; g = g->groups_next)
614         add_static_service_group_to_server(g);
615 }
616
617 void static_service_remove_from_server(void) {
618     StaticServiceGroup *g;
619
620     for (g = groups; g; g = g->groups_next)
621         remove_static_service_group_from_server(g);
622 }