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