4 This file is part of avahi.
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.
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.
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
36 #endif /* USE_EXPAT_H */
40 #endif /* USE_BSDXML_H */
42 #include <avahi-common/llist.h>
43 #include <avahi-common/malloc.h>
44 #include <avahi-common/alternative.h>
45 #include <avahi-common/error.h>
46 #include <avahi-core/log.h>
47 #include <avahi-core/publish.h>
50 #include "static-services.h"
52 typedef struct StaticService StaticService;
53 typedef struct StaticServiceGroup StaticServiceGroup;
55 struct StaticService {
56 StaticServiceGroup *group;
64 AvahiStringList *subtypes;
66 AvahiStringList *txt_records;
68 AVAHI_LLIST_FIELDS(StaticService, services);
71 struct StaticServiceGroup {
75 char *name, *chosen_name;
76 int replace_wildcards;
78 AvahiSEntryGroup *entry_group;
79 AVAHI_LLIST_HEAD(StaticService, services);
80 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
83 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
85 static char *replacestr(const char *pattern, const char *a, const char *b) {
86 char *r = NULL, *e, *n;
88 while ((e = strstr(pattern, a))) {
91 k = avahi_strndup(pattern, e - pattern);
93 n = avahi_strdup_printf("%s%s%s", r, k, b);
95 n = avahi_strdup_printf("%s%s", k, b);
101 pattern = e + strlen(a);
105 return avahi_strdup(pattern);
107 n = avahi_strdup_printf("%s%s", r, pattern);
113 static void add_static_service_group_to_server(StaticServiceGroup *g);
114 static void remove_static_service_group_from_server(StaticServiceGroup *g);
116 static StaticService *static_service_new(StaticServiceGroup *group) {
120 s = avahi_new(StaticService, 1);
123 s->type = s->host_name = s->domain_name = NULL;
125 s->protocol = AVAHI_PROTO_UNSPEC;
127 s->txt_records = NULL;
130 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
135 static StaticServiceGroup *static_service_group_new(char *filename) {
136 StaticServiceGroup *g;
139 g = avahi_new(StaticServiceGroup, 1);
140 g->filename = avahi_strdup(filename);
142 g->name = g->chosen_name = NULL;
143 g->replace_wildcards = 0;
144 g->entry_group = NULL;
146 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
147 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
152 static void static_service_free(StaticService *s) {
155 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
158 avahi_free(s->host_name);
159 avahi_free(s->domain_name);
161 avahi_string_list_free(s->txt_records);
162 avahi_string_list_free(s->subtypes);
167 static void static_service_group_free(StaticServiceGroup *g) {
171 avahi_s_entry_group_free(g->entry_group);
174 static_service_free(g->services);
176 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
178 avahi_free(g->filename);
180 avahi_free(g->chosen_name);
184 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
185 StaticServiceGroup *g = userdata;
192 case AVAHI_ENTRY_GROUP_COLLISION: {
195 remove_static_service_group_from_server(g);
197 n = avahi_alternative_service_name(g->chosen_name);
198 avahi_free(g->chosen_name);
201 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
203 add_static_service_group_to_server(g);
207 case AVAHI_ENTRY_GROUP_ESTABLISHED:
208 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
211 case AVAHI_ENTRY_GROUP_FAILURE:
212 avahi_log_warn("Failed to publish service \"%s\" (%s): %s", g->chosen_name, g->filename, avahi_strerror(avahi_server_errno(s)));
213 remove_static_service_group_from_server(g);
216 case AVAHI_ENTRY_GROUP_UNCOMMITED:
217 case AVAHI_ENTRY_GROUP_REGISTERING:
222 static void add_static_service_group_to_server(StaticServiceGroup *g) {
227 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
228 /* This service group is already registered in the server */
231 if (!g->chosen_name || (g->replace_wildcards && strstr(g->name, "%h"))) {
233 avahi_free(g->chosen_name);
235 if (g->replace_wildcards)
236 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
238 g->chosen_name = avahi_strdup(g->name);
243 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
245 assert(avahi_s_entry_group_is_empty(g->entry_group));
247 for (s = g->services; s; s = s->services_next) {
250 if (avahi_server_add_service_strlst(
253 AVAHI_IF_UNSPEC, s->protocol,
255 g->chosen_name, s->type, s->domain_name,
256 s->host_name, s->port,
257 s->txt_records) < 0) {
258 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
259 g->chosen_name, s->type, g->filename,
260 avahi_strerror(avahi_server_errno(avahi_server)));
261 remove_static_service_group_from_server(g);
265 for (i = s->subtypes; i; i = i->next) {
267 if (avahi_server_add_service_subtype(
270 AVAHI_IF_UNSPEC, s->protocol,
272 g->chosen_name, s->type, s->domain_name,
273 (char*) i->text) < 0) {
275 avahi_log_error("Failed to add subtype '%s' for service '%s' of type '%s', ignoring subtype (%s): %s",
276 i->text, g->chosen_name, s->type, g->filename,
277 avahi_strerror(avahi_server_errno(avahi_server)));
282 avahi_s_entry_group_commit(g->entry_group);
285 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
289 avahi_s_entry_group_reset(g->entry_group);
294 XML_TAG_SERVICE_GROUP,
305 struct xml_userdata {
306 StaticServiceGroup *group;
307 StaticService *service;
308 xml_tag_name current_tag;
317 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
318 struct xml_userdata *u = data;
325 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
330 u->current_tag = XML_TAG_SERVICE_GROUP;
331 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
332 u->current_tag = XML_TAG_NAME;
335 if (strcmp(attr[0], "replace-wildcards") == 0)
336 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
344 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
345 u->current_tag = XML_TAG_SERVICE;
348 u->service = static_service_new(u->group);
351 if (strcmp(attr[0], "protocol") == 0) {
352 AvahiProtocol protocol;
354 if (strcmp(attr[1], "ipv4") == 0) {
355 protocol = AVAHI_PROTO_INET;
356 } else if (strcmp(attr[1], "ipv6") == 0) {
357 protocol = AVAHI_PROTO_INET6;
358 } else if (strcmp(attr[1], "any") == 0) {
359 protocol = AVAHI_PROTO_UNSPEC;
361 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
366 u->service->protocol = protocol;
374 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
378 u->current_tag = XML_TAG_TYPE;
379 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "subtype") == 0) {
383 u->current_tag = XML_TAG_SUBTYPE;
384 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
388 u->current_tag = XML_TAG_DOMAIN_NAME;
389 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
393 u->current_tag = XML_TAG_HOST_NAME;
394 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
398 u->current_tag = XML_TAG_PORT;
399 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
403 u->current_tag = XML_TAG_TXT_RECORD;
405 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
412 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
417 static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
418 struct xml_userdata *u = data;
424 switch (u->current_tag) {
425 case XML_TAG_SERVICE_GROUP:
427 if (!u->group->name || !u->group->services) {
428 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
433 u->current_tag = XML_TAG_INVALID;
436 case XML_TAG_SERVICE:
438 if (!u->service->type) {
439 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
445 u->current_tag = XML_TAG_SERVICE_GROUP;
449 u->current_tag = XML_TAG_SERVICE_GROUP;
456 p = u->buf ? atoi(u->buf) : 0;
458 if (p < 0 || p > 0xFFFF) {
459 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
464 u->service->port = (uint16_t) p;
466 u->current_tag = XML_TAG_SERVICE;
470 case XML_TAG_TXT_RECORD: {
473 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
474 u->current_tag = XML_TAG_SERVICE;
478 case XML_TAG_SUBTYPE: {
481 u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
482 u->current_tag = XML_TAG_SERVICE;
487 case XML_TAG_DOMAIN_NAME:
488 case XML_TAG_HOST_NAME:
489 u->current_tag = XML_TAG_SERVICE;
492 case XML_TAG_INVALID:
500 static char *append_cdata(char *t, const char *n, int length) {
507 k = avahi_strndup(n, length);
510 r = avahi_strdup_printf("%s%s", t, k);
519 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
520 struct xml_userdata *u = data;
526 switch (u->current_tag) {
528 u->group->name = append_cdata(u->group->name, s, len);
533 u->service->type = append_cdata(u->service->type, s, len);
536 case XML_TAG_DOMAIN_NAME:
538 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
541 case XML_TAG_HOST_NAME:
543 u->service->host_name = append_cdata(u->service->host_name, s, len);
547 case XML_TAG_TXT_RECORD:
548 case XML_TAG_SUBTYPE:
550 u->buf = append_cdata(u->buf, s, len);
553 case XML_TAG_SERVICE_GROUP:
554 case XML_TAG_SERVICE:
555 case XML_TAG_INVALID:
560 static int static_service_group_load(StaticServiceGroup *g) {
561 XML_Parser parser = NULL;
563 struct xml_userdata u;
573 u.current_tag = XML_TAG_INVALID;
576 /* Cleanup old data in this service group, if available */
577 remove_static_service_group_from_server(g);
579 static_service_free(g->services);
582 avahi_free(g->chosen_name);
583 g->name = g->chosen_name = NULL;
584 g->replace_wildcards = 0;
586 if (!(parser = XML_ParserCreate(NULL))) {
587 avahi_log_error("XML_ParserCreate() failed.");
591 if ((fd = open(g->filename, O_RDONLY)) < 0) {
592 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
596 if (fstat(fd, &st) < 0) {
597 avahi_log_error("fstat(): %s", strerror(errno));
601 g->mtime = st.st_mtime;
603 XML_SetUserData(parser, &u);
605 XML_SetElementHandler(parser, xml_start, xml_end);
606 XML_SetCharacterDataHandler(parser, xml_cdata);
611 #define BUFSIZE (10*1024)
613 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
614 avahi_log_error("XML_GetBuffer() failed.");
618 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
619 avahi_log_error("read(): %s\n", strerror(errno));
623 if (!XML_ParseBuffer(parser, n, n == 0)) {
624 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", (int) XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
639 XML_ParserFree(parser);
646 static void load_file(char *n) {
647 StaticServiceGroup *g;
650 for (g = groups; g; g = g->groups_next)
651 if (strcmp(g->filename, n) == 0)
654 avahi_log_info("Loading service file %s.", n);
656 g = static_service_group_new(n);
657 if (static_service_group_load(g) < 0) {
658 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
659 static_service_group_free(g);
663 void static_service_load(int in_chroot) {
664 StaticServiceGroup *g, *n;
669 for (g = groups; g; g = n) {
674 if (stat(g->filename, &st) < 0) {
677 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
679 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
681 static_service_group_free(g);
682 } else if (st.st_mtime != g->mtime) {
683 avahi_log_info("Service group file %s changed, reloading.", g->filename);
685 if (static_service_group_load(g) < 0) {
686 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
687 static_service_group_free(g);
692 memset(&globbuf, 0, sizeof(globbuf));
694 if ((globret = glob(in_chroot ? "/services/*.service" : AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf)) != 0)
699 avahi_log_error("Not enough memory to read service directory "AVAHI_SERVICE_DIR".");
704 avahi_log_info("No service file found in "AVAHI_SERVICE_DIR".");
708 avahi_log_error("Failed to read "AVAHI_SERVICE_DIR".");
713 for (p = globbuf.gl_pathv; *p; p++)
720 void static_service_free_all(void) {
723 static_service_group_free(groups);
726 void static_service_add_to_server(void) {
727 StaticServiceGroup *g;
729 for (g = groups; g; g = g->groups_next)
730 add_static_service_group_to_server(g);
733 void static_service_remove_from_server(void) {
734 StaticServiceGroup *g;
736 for (g = groups; g; g = g->groups_next)
737 remove_static_service_group_from_server(g);