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