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-common/domain.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 char label[AVAHI_LABEL_MAX];
239 p = avahi_server_get_host_name(avahi_server);
240 avahi_unescape_label(&p, label, sizeof(label));
242 g->chosen_name = replacestr(g->name, "%h", label);
244 g->chosen_name = avahi_strdup(g->name);
249 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
251 assert(avahi_s_entry_group_is_empty(g->entry_group));
253 for (s = g->services; s; s = s->services_next) {
256 if (avahi_server_add_service_strlst(
259 AVAHI_IF_UNSPEC, s->protocol,
261 g->chosen_name, s->type, s->domain_name,
262 s->host_name, s->port,
263 s->txt_records) < 0) {
264 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
265 g->chosen_name, s->type, g->filename,
266 avahi_strerror(avahi_server_errno(avahi_server)));
267 remove_static_service_group_from_server(g);
271 for (i = s->subtypes; i; i = i->next) {
273 if (avahi_server_add_service_subtype(
276 AVAHI_IF_UNSPEC, s->protocol,
278 g->chosen_name, s->type, s->domain_name,
279 (char*) i->text) < 0) {
281 avahi_log_error("Failed to add subtype '%s' for service '%s' of type '%s', ignoring subtype (%s): %s",
282 i->text, g->chosen_name, s->type, g->filename,
283 avahi_strerror(avahi_server_errno(avahi_server)));
288 avahi_s_entry_group_commit(g->entry_group);
291 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
295 avahi_s_entry_group_reset(g->entry_group);
300 XML_TAG_SERVICE_GROUP,
311 struct xml_userdata {
312 StaticServiceGroup *group;
313 StaticService *service;
314 xml_tag_name current_tag;
323 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
324 struct xml_userdata *u = data;
331 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
336 u->current_tag = XML_TAG_SERVICE_GROUP;
337 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
338 u->current_tag = XML_TAG_NAME;
341 if (strcmp(attr[0], "replace-wildcards") == 0)
342 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
350 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
351 u->current_tag = XML_TAG_SERVICE;
354 u->service = static_service_new(u->group);
357 if (strcmp(attr[0], "protocol") == 0) {
358 AvahiProtocol protocol;
360 if (strcmp(attr[1], "ipv4") == 0) {
361 protocol = AVAHI_PROTO_INET;
362 } else if (strcmp(attr[1], "ipv6") == 0) {
363 protocol = AVAHI_PROTO_INET6;
364 } else if (strcmp(attr[1], "any") == 0) {
365 protocol = AVAHI_PROTO_UNSPEC;
367 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
372 u->service->protocol = protocol;
380 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
384 u->current_tag = XML_TAG_TYPE;
385 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "subtype") == 0) {
389 u->current_tag = XML_TAG_SUBTYPE;
390 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
394 u->current_tag = XML_TAG_DOMAIN_NAME;
395 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
399 u->current_tag = XML_TAG_HOST_NAME;
400 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
404 u->current_tag = XML_TAG_PORT;
405 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
409 u->current_tag = XML_TAG_TXT_RECORD;
411 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
418 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
423 static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
424 struct xml_userdata *u = data;
430 switch (u->current_tag) {
431 case XML_TAG_SERVICE_GROUP:
433 if (!u->group->name || !u->group->services) {
434 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
439 u->current_tag = XML_TAG_INVALID;
442 case XML_TAG_SERVICE:
444 if (!u->service->type) {
445 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
451 u->current_tag = XML_TAG_SERVICE_GROUP;
455 u->current_tag = XML_TAG_SERVICE_GROUP;
462 p = u->buf ? atoi(u->buf) : 0;
464 if (p < 0 || p > 0xFFFF) {
465 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
470 u->service->port = (uint16_t) p;
472 u->current_tag = XML_TAG_SERVICE;
476 case XML_TAG_TXT_RECORD: {
479 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
480 u->current_tag = XML_TAG_SERVICE;
484 case XML_TAG_SUBTYPE: {
487 u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
488 u->current_tag = XML_TAG_SERVICE;
493 case XML_TAG_DOMAIN_NAME:
494 case XML_TAG_HOST_NAME:
495 u->current_tag = XML_TAG_SERVICE;
498 case XML_TAG_INVALID:
506 static char *append_cdata(char *t, const char *n, int length) {
513 k = avahi_strndup(n, length);
516 r = avahi_strdup_printf("%s%s", t, k);
525 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
526 struct xml_userdata *u = data;
532 switch (u->current_tag) {
534 u->group->name = append_cdata(u->group->name, s, len);
539 u->service->type = append_cdata(u->service->type, s, len);
542 case XML_TAG_DOMAIN_NAME:
544 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
547 case XML_TAG_HOST_NAME:
549 u->service->host_name = append_cdata(u->service->host_name, s, len);
553 case XML_TAG_TXT_RECORD:
554 case XML_TAG_SUBTYPE:
556 u->buf = append_cdata(u->buf, s, len);
559 case XML_TAG_SERVICE_GROUP:
560 case XML_TAG_SERVICE:
561 case XML_TAG_INVALID:
566 static int static_service_group_load(StaticServiceGroup *g) {
567 XML_Parser parser = NULL;
569 struct xml_userdata u;
579 u.current_tag = XML_TAG_INVALID;
582 /* Cleanup old data in this service group, if available */
583 remove_static_service_group_from_server(g);
585 static_service_free(g->services);
588 avahi_free(g->chosen_name);
589 g->name = g->chosen_name = NULL;
590 g->replace_wildcards = 0;
592 if (!(parser = XML_ParserCreate(NULL))) {
593 avahi_log_error("XML_ParserCreate() failed.");
597 if ((fd = open(g->filename, O_RDONLY)) < 0) {
598 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
602 if (fstat(fd, &st) < 0) {
603 avahi_log_error("fstat(): %s", strerror(errno));
607 g->mtime = st.st_mtime;
609 XML_SetUserData(parser, &u);
611 XML_SetElementHandler(parser, xml_start, xml_end);
612 XML_SetCharacterDataHandler(parser, xml_cdata);
617 #define BUFSIZE (10*1024)
619 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
620 avahi_log_error("XML_GetBuffer() failed.");
624 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
625 avahi_log_error("read(): %s\n", strerror(errno));
629 if (!XML_ParseBuffer(parser, n, n == 0)) {
630 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", (int) XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
645 XML_ParserFree(parser);
652 static void load_file(char *n) {
653 StaticServiceGroup *g;
656 for (g = groups; g; g = g->groups_next)
657 if (strcmp(g->filename, n) == 0)
660 avahi_log_info("Loading service file %s.", n);
662 g = static_service_group_new(n);
663 if (static_service_group_load(g) < 0) {
664 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
665 static_service_group_free(g);
669 void static_service_load(int in_chroot) {
670 StaticServiceGroup *g, *n;
675 for (g = groups; g; g = n) {
680 if (stat(g->filename, &st) < 0) {
683 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
685 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
687 static_service_group_free(g);
688 } else if (st.st_mtime != g->mtime) {
689 avahi_log_info("Service group file %s changed, reloading.", g->filename);
691 if (static_service_group_load(g) < 0) {
692 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
693 static_service_group_free(g);
698 memset(&globbuf, 0, sizeof(globbuf));
700 if ((globret = glob(in_chroot ? "/services/*.service" : AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf)) != 0)
705 avahi_log_error("Not enough memory to read service directory "AVAHI_SERVICE_DIR".");
710 avahi_log_info("No service file found in "AVAHI_SERVICE_DIR".");
714 avahi_log_error("Failed to read "AVAHI_SERVICE_DIR".");
719 for (p = globbuf.gl_pathv; *p; p++)
726 void static_service_free_all(void) {
729 static_service_group_free(groups);
732 void static_service_add_to_server(void) {
733 StaticServiceGroup *g;
735 for (g = groups; g; g = g->groups_next)
736 add_static_service_group_to_server(g);
739 void static_service_remove_from_server(void) {
740 StaticServiceGroup *g;
742 for (g = groups; g; g = g->groups_next)
743 remove_static_service_group_from_server(g);