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