2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #endif /* USE_EXPAT_H */
39 #endif /* USE_BSDXML_H */
41 #include <avahi-common/llist.h>
42 #include <avahi-common/malloc.h>
43 #include <avahi-common/alternative.h>
44 #include <avahi-common/error.h>
45 #include <avahi-core/log.h>
46 #include <avahi-core/publish.h>
49 #include "static-services.h"
51 typedef struct StaticService StaticService;
52 typedef struct StaticServiceGroup StaticServiceGroup;
54 struct StaticService {
55 StaticServiceGroup *group;
63 AvahiStringList *subtypes;
65 AvahiStringList *txt_records;
67 AVAHI_LLIST_FIELDS(StaticService, services);
70 struct StaticServiceGroup {
74 char *name, *chosen_name;
75 int replace_wildcards;
77 AvahiSEntryGroup *entry_group;
78 AVAHI_LLIST_HEAD(StaticService, services);
79 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
82 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
84 static char *replacestr(const char *pattern, const char *a, const char *b) {
85 char *r = NULL, *e, *n;
87 while ((e = strstr(pattern, a))) {
90 k = avahi_strndup(pattern, e - pattern);
92 n = avahi_strdup_printf("%s%s%s", r, k, b);
94 n = avahi_strdup_printf("%s%s", k, b);
100 pattern = e + strlen(a);
104 return avahi_strdup(pattern);
106 n = avahi_strdup_printf("%s%s", r, pattern);
112 static void add_static_service_group_to_server(StaticServiceGroup *g);
113 static void remove_static_service_group_from_server(StaticServiceGroup *g);
115 static StaticService *static_service_new(StaticServiceGroup *group) {
119 s = avahi_new(StaticService, 1);
122 s->type = s->host_name = s->domain_name = NULL;
124 s->protocol = AVAHI_PROTO_UNSPEC;
126 s->txt_records = NULL;
129 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
134 static StaticServiceGroup *static_service_group_new(char *filename) {
135 StaticServiceGroup *g;
138 g = avahi_new(StaticServiceGroup, 1);
139 g->filename = avahi_strdup(filename);
141 g->name = g->chosen_name = NULL;
142 g->replace_wildcards = 0;
143 g->entry_group = NULL;
145 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
146 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
151 static void static_service_free(StaticService *s) {
154 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
157 avahi_free(s->host_name);
158 avahi_free(s->domain_name);
160 avahi_string_list_free(s->txt_records);
161 avahi_string_list_free(s->subtypes);
166 static void static_service_group_free(StaticServiceGroup *g) {
170 avahi_s_entry_group_free(g->entry_group);
173 static_service_free(g->services);
175 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
177 avahi_free(g->filename);
179 avahi_free(g->chosen_name);
183 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
184 StaticServiceGroup *g = userdata;
191 case AVAHI_ENTRY_GROUP_COLLISION: {
194 remove_static_service_group_from_server(g);
196 n = avahi_alternative_service_name(g->chosen_name);
197 avahi_free(g->chosen_name);
200 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
202 add_static_service_group_to_server(g);
206 case AVAHI_ENTRY_GROUP_ESTABLISHED:
207 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
210 case AVAHI_ENTRY_GROUP_FAILURE:
211 avahi_log_warn("Failed to publish service \"%s\" (%s): %s", g->chosen_name, g->filename, avahi_strerror(avahi_server_errno(s)));
212 remove_static_service_group_from_server(g);
215 case AVAHI_ENTRY_GROUP_UNCOMMITED:
216 case AVAHI_ENTRY_GROUP_REGISTERING:
221 static void add_static_service_group_to_server(StaticServiceGroup *g) {
226 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
227 /* This service group is already registered in the server */
230 if (!g->chosen_name || (g->replace_wildcards && strstr(g->name, "%h"))) {
232 avahi_free(g->chosen_name);
234 if (g->replace_wildcards)
235 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
237 g->chosen_name = avahi_strdup(g->name);
242 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
244 assert(avahi_s_entry_group_is_empty(g->entry_group));
246 for (s = g->services; s; s = s->services_next) {
249 if (avahi_server_add_service_strlst(
252 AVAHI_IF_UNSPEC, s->protocol,
254 g->chosen_name, s->type, s->domain_name,
255 s->host_name, s->port,
256 s->txt_records) < 0) {
257 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
258 g->chosen_name, s->type, g->filename,
259 avahi_strerror(avahi_server_errno(avahi_server)));
260 remove_static_service_group_from_server(g);
264 for (i = s->subtypes; i; i = i->next) {
266 if (avahi_server_add_service_subtype(
269 AVAHI_IF_UNSPEC, s->protocol,
271 g->chosen_name, s->type, s->domain_name,
272 (char*) i->text) < 0) {
274 avahi_log_error("Failed to add subtype '%s' for service '%s' of type '%s', ignoring subtype (%s): %s",
275 i->text, g->chosen_name, s->type, g->filename,
276 avahi_strerror(avahi_server_errno(avahi_server)));
281 avahi_s_entry_group_commit(g->entry_group);
284 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
288 avahi_s_entry_group_reset(g->entry_group);
293 XML_TAG_SERVICE_GROUP,
304 struct xml_userdata {
305 StaticServiceGroup *group;
306 StaticService *service;
307 xml_tag_name current_tag;
316 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
317 struct xml_userdata *u = data;
324 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
329 u->current_tag = XML_TAG_SERVICE_GROUP;
330 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
331 u->current_tag = XML_TAG_NAME;
334 if (strcmp(attr[0], "replace-wildcards") == 0)
335 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
343 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
344 u->current_tag = XML_TAG_SERVICE;
347 u->service = static_service_new(u->group);
350 if (strcmp(attr[0], "protocol") == 0) {
351 AvahiProtocol protocol;
353 if (strcmp(attr[1], "ipv4") == 0) {
354 protocol = AVAHI_PROTO_INET;
355 } else if (strcmp(attr[1], "ipv6") == 0) {
356 protocol = AVAHI_PROTO_INET6;
357 } else if (strcmp(attr[1], "any") == 0) {
358 protocol = AVAHI_PROTO_UNSPEC;
360 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
365 u->service->protocol = protocol;
373 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
377 u->current_tag = XML_TAG_TYPE;
378 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "subtype") == 0) {
382 u->current_tag = XML_TAG_SUBTYPE;
383 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
387 u->current_tag = XML_TAG_DOMAIN_NAME;
388 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
392 u->current_tag = XML_TAG_HOST_NAME;
393 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
397 u->current_tag = XML_TAG_PORT;
398 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
402 u->current_tag = XML_TAG_TXT_RECORD;
404 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
411 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
416 static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
417 struct xml_userdata *u = data;
423 switch (u->current_tag) {
424 case XML_TAG_SERVICE_GROUP:
426 if (!u->group->name || !u->group->services) {
427 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
432 u->current_tag = XML_TAG_INVALID;
435 case XML_TAG_SERVICE:
437 if (!u->service->type) {
438 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
444 u->current_tag = XML_TAG_SERVICE_GROUP;
448 u->current_tag = XML_TAG_SERVICE_GROUP;
455 p = u->buf ? atoi(u->buf) : 0;
457 if (p < 0 || p > 0xFFFF) {
458 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
463 u->service->port = (uint16_t) p;
465 u->current_tag = XML_TAG_SERVICE;
469 case XML_TAG_TXT_RECORD: {
472 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
473 u->current_tag = XML_TAG_SERVICE;
477 case XML_TAG_SUBTYPE: {
480 u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
481 u->current_tag = XML_TAG_SERVICE;
486 case XML_TAG_DOMAIN_NAME:
487 case XML_TAG_HOST_NAME:
488 u->current_tag = XML_TAG_SERVICE;
491 case XML_TAG_INVALID:
499 static char *append_cdata(char *t, const char *n, int length) {
506 k = avahi_strndup(n, length);
509 r = avahi_strdup_printf("%s%s", t, k);
518 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
519 struct xml_userdata *u = data;
525 switch (u->current_tag) {
527 u->group->name = append_cdata(u->group->name, s, len);
532 u->service->type = append_cdata(u->service->type, s, len);
535 case XML_TAG_DOMAIN_NAME:
537 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
540 case XML_TAG_HOST_NAME:
542 u->service->host_name = append_cdata(u->service->host_name, s, len);
546 case XML_TAG_TXT_RECORD:
547 case XML_TAG_SUBTYPE:
549 u->buf = append_cdata(u->buf, s, len);
552 case XML_TAG_SERVICE_GROUP:
553 case XML_TAG_SERVICE:
554 case XML_TAG_INVALID:
559 static int static_service_group_load(StaticServiceGroup *g) {
560 XML_Parser parser = NULL;
562 struct xml_userdata u;
572 u.current_tag = XML_TAG_INVALID;
575 /* Cleanup old data in this service group, if available */
576 remove_static_service_group_from_server(g);
578 static_service_free(g->services);
581 avahi_free(g->chosen_name);
582 g->name = g->chosen_name = NULL;
583 g->replace_wildcards = 0;
585 if (!(parser = XML_ParserCreate(NULL))) {
586 avahi_log_error("XML_ParserCreate() failed.");
590 if ((fd = open(g->filename, O_RDONLY)) < 0) {
591 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
595 if (fstat(fd, &st) < 0) {
596 avahi_log_error("fstat(): %s", strerror(errno));
600 g->mtime = st.st_mtime;
602 XML_SetUserData(parser, &u);
604 XML_SetElementHandler(parser, xml_start, xml_end);
605 XML_SetCharacterDataHandler(parser, xml_cdata);
610 #define BUFSIZE (10*1024)
612 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
613 avahi_log_error("XML_GetBuffer() failed.");
617 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
618 avahi_log_error("read(): %s\n", strerror(errno));
622 if (!XML_ParseBuffer(parser, n, n == 0)) {
623 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", (int) XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
638 XML_ParserFree(parser);
645 static void load_file(char *n) {
646 StaticServiceGroup *g;
649 for (g = groups; g; g = g->groups_next)
650 if (strcmp(g->filename, n) == 0)
653 avahi_log_info("Loading service file %s.", n);
655 g = static_service_group_new(n);
656 if (static_service_group_load(g) < 0) {
657 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
658 static_service_group_free(g);
662 void static_service_load(int in_chroot) {
663 StaticServiceGroup *g, *n;
668 for (g = groups; g; g = n) {
673 if (stat(g->filename, &st) < 0) {
676 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
678 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
680 static_service_group_free(g);
681 } else if (st.st_mtime != g->mtime) {
682 avahi_log_info("Service group file %s changed, reloading.", g->filename);
684 if (static_service_group_load(g) < 0) {
685 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
686 static_service_group_free(g);
691 memset(&globbuf, 0, sizeof(globbuf));
693 if ((globret = glob(in_chroot ? "/services/*.service" : AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf)) != 0)
698 avahi_log_error("Not enough memory to read service directory "AVAHI_SERVICE_DIR".");
703 avahi_log_info("No service file found in "AVAHI_SERVICE_DIR".");
707 avahi_log_error("Failed to read "AVAHI_SERVICE_DIR".");
712 for (p = globbuf.gl_pathv; *p; p++)
719 void static_service_free_all(void) {
722 static_service_group_free(groups);
725 void static_service_add_to_server(void) {
726 StaticServiceGroup *g;
728 for (g = groups; g; g = g->groups_next)
729 add_static_service_group_to_server(g);
732 void static_service_remove_from_server(void) {
733 StaticServiceGroup *g;
735 for (g = groups; g; g = g->groups_next)
736 remove_static_service_group_from_server(g);