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
37 #endif /* USE_EXPAT_H */
41 #endif /* USE_BSDXML_H */
43 #include <avahi-common/llist.h>
44 #include <avahi-common/malloc.h>
45 #include <avahi-common/alternative.h>
46 #include <avahi-common/error.h>
47 #include <avahi-core/log.h>
48 #include <avahi-core/publish.h>
51 #include "static-services.h"
53 typedef struct StaticService StaticService;
54 typedef struct StaticServiceGroup StaticServiceGroup;
56 struct StaticService {
57 StaticServiceGroup *group;
65 AvahiStringList *subtypes;
67 AvahiStringList *txt_records;
69 AVAHI_LLIST_FIELDS(StaticService, services);
72 struct StaticServiceGroup {
76 char *name, *chosen_name;
77 int replace_wildcards;
79 AvahiSEntryGroup *entry_group;
80 AVAHI_LLIST_HEAD(StaticService, services);
81 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
84 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
86 static char *replacestr(const char *pattern, const char *a, const char *b) {
87 char *r = NULL, *e, *n;
89 while ((e = strstr(pattern, a))) {
92 k = avahi_strndup(pattern, e - pattern);
94 n = avahi_strdup_printf("%s%s%s", r, k, b);
96 n = avahi_strdup_printf("%s%s", k, b);
102 pattern = e + strlen(a);
106 return avahi_strdup(pattern);
108 n = avahi_strdup_printf("%s%s", r, pattern);
114 static void add_static_service_group_to_server(StaticServiceGroup *g);
115 static void remove_static_service_group_from_server(StaticServiceGroup *g);
117 static StaticService *static_service_new(StaticServiceGroup *group) {
121 s = avahi_new(StaticService, 1);
124 s->type = s->host_name = s->domain_name = NULL;
126 s->protocol = AVAHI_PROTO_UNSPEC;
128 s->txt_records = NULL;
131 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
136 static StaticServiceGroup *static_service_group_new(char *filename) {
137 StaticServiceGroup *g;
140 g = avahi_new(StaticServiceGroup, 1);
141 g->filename = avahi_strdup(filename);
143 g->name = g->chosen_name = NULL;
144 g->replace_wildcards = 0;
145 g->entry_group = NULL;
147 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
148 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
153 static void static_service_free(StaticService *s) {
156 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
159 avahi_free(s->host_name);
160 avahi_free(s->domain_name);
162 avahi_string_list_free(s->txt_records);
163 avahi_string_list_free(s->subtypes);
168 static void static_service_group_free(StaticServiceGroup *g) {
172 avahi_s_entry_group_free(g->entry_group);
175 static_service_free(g->services);
177 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
179 avahi_free(g->filename);
181 avahi_free(g->chosen_name);
185 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
186 StaticServiceGroup *g = userdata;
193 case AVAHI_ENTRY_GROUP_COLLISION: {
196 remove_static_service_group_from_server(g);
198 n = avahi_alternative_service_name(g->chosen_name);
199 avahi_free(g->chosen_name);
202 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
204 add_static_service_group_to_server(g);
208 case AVAHI_ENTRY_GROUP_ESTABLISHED:
209 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
212 case AVAHI_ENTRY_GROUP_FAILURE:
213 avahi_log_warn("Failed to publish service \"%s\" (%s): %s", g->chosen_name, g->filename, avahi_strerror(avahi_server_errno(s)));
214 remove_static_service_group_from_server(g);
217 case AVAHI_ENTRY_GROUP_UNCOMMITED:
218 case AVAHI_ENTRY_GROUP_REGISTERING:
223 static void add_static_service_group_to_server(StaticServiceGroup *g) {
228 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
229 /* This service group is already registered in the server */
232 if (!g->chosen_name || (g->replace_wildcards && strstr(g->name, "%h"))) {
234 avahi_free(g->chosen_name);
236 if (g->replace_wildcards)
237 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
239 g->chosen_name = avahi_strdup(g->name);
244 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
246 assert(avahi_s_entry_group_is_empty(g->entry_group));
248 for (s = g->services; s; s = s->services_next) {
251 if (avahi_server_add_service_strlst(
254 AVAHI_IF_UNSPEC, s->protocol,
256 g->chosen_name, s->type, s->domain_name,
257 s->host_name, s->port,
258 s->txt_records) < 0) {
259 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
260 g->chosen_name, s->type, g->filename,
261 avahi_strerror(avahi_server_errno(avahi_server)));
262 remove_static_service_group_from_server(g);
266 for (i = s->subtypes; i; i = i->next) {
268 if (avahi_server_add_service_subtype(
271 AVAHI_IF_UNSPEC, s->protocol,
273 g->chosen_name, s->type, s->domain_name,
274 (char*) i->text) < 0) {
276 avahi_log_error("Failed to add subtype '%s' for service '%s' of type '%s', ignoring subtype (%s): %s",
277 i->text, g->chosen_name, s->type, g->filename,
278 avahi_strerror(avahi_server_errno(avahi_server)));
283 avahi_s_entry_group_commit(g->entry_group);
286 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
290 avahi_s_entry_group_reset(g->entry_group);
295 XML_TAG_SERVICE_GROUP,
306 struct xml_userdata {
307 StaticServiceGroup *group;
308 StaticService *service;
309 xml_tag_name current_tag;
318 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
319 struct xml_userdata *u = data;
326 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
331 u->current_tag = XML_TAG_SERVICE_GROUP;
332 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
333 u->current_tag = XML_TAG_NAME;
336 if (strcmp(attr[0], "replace-wildcards") == 0)
337 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
345 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
346 u->current_tag = XML_TAG_SERVICE;
349 u->service = static_service_new(u->group);
352 if (strcmp(attr[0], "protocol") == 0) {
353 AvahiProtocol protocol;
355 if (strcmp(attr[1], "ipv4") == 0) {
356 protocol = AVAHI_PROTO_INET;
357 } else if (strcmp(attr[1], "ipv6") == 0) {
358 protocol = AVAHI_PROTO_INET6;
359 } else if (strcmp(attr[1], "any") == 0) {
360 protocol = AVAHI_PROTO_UNSPEC;
362 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
367 u->service->protocol = protocol;
375 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
379 u->current_tag = XML_TAG_TYPE;
380 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "subtype") == 0) {
384 u->current_tag = XML_TAG_SUBTYPE;
385 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
389 u->current_tag = XML_TAG_DOMAIN_NAME;
390 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
394 u->current_tag = XML_TAG_HOST_NAME;
395 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
399 u->current_tag = XML_TAG_PORT;
400 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
404 u->current_tag = XML_TAG_TXT_RECORD;
406 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
413 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
418 static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
419 struct xml_userdata *u = data;
425 switch (u->current_tag) {
426 case XML_TAG_SERVICE_GROUP:
428 if (!u->group->name || !u->group->services) {
429 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
434 u->current_tag = XML_TAG_INVALID;
437 case XML_TAG_SERVICE:
439 if (!u->service->type) {
440 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
446 u->current_tag = XML_TAG_SERVICE_GROUP;
450 u->current_tag = XML_TAG_SERVICE_GROUP;
457 p = u->buf ? atoi(u->buf) : 0;
459 if (p < 0 || p > 0xFFFF) {
460 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
465 u->service->port = (uint16_t) p;
467 u->current_tag = XML_TAG_SERVICE;
471 case XML_TAG_TXT_RECORD: {
474 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
475 u->current_tag = XML_TAG_SERVICE;
479 case XML_TAG_SUBTYPE: {
482 u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
483 u->current_tag = XML_TAG_SERVICE;
488 case XML_TAG_DOMAIN_NAME:
489 case XML_TAG_HOST_NAME:
490 u->current_tag = XML_TAG_SERVICE;
493 case XML_TAG_INVALID:
501 static char *append_cdata(char *t, const char *n, int length) {
508 k = avahi_strndup(n, length);
511 r = avahi_strdup_printf("%s%s", t, k);
520 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
521 struct xml_userdata *u = data;
527 switch (u->current_tag) {
529 u->group->name = append_cdata(u->group->name, s, len);
534 u->service->type = append_cdata(u->service->type, s, len);
537 case XML_TAG_DOMAIN_NAME:
539 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
542 case XML_TAG_HOST_NAME:
544 u->service->host_name = append_cdata(u->service->host_name, s, len);
548 case XML_TAG_TXT_RECORD:
549 case XML_TAG_SUBTYPE:
551 u->buf = append_cdata(u->buf, s, len);
554 case XML_TAG_SERVICE_GROUP:
555 case XML_TAG_SERVICE:
556 case XML_TAG_INVALID:
561 static int static_service_group_load(StaticServiceGroup *g) {
562 XML_Parser parser = NULL;
564 struct xml_userdata u;
574 u.current_tag = XML_TAG_INVALID;
577 /* Cleanup old data in this service group, if available */
578 remove_static_service_group_from_server(g);
580 static_service_free(g->services);
583 avahi_free(g->chosen_name);
584 g->name = g->chosen_name = NULL;
585 g->replace_wildcards = 0;
587 if (!(parser = XML_ParserCreate(NULL))) {
588 avahi_log_error("XML_ParserCreate() failed.");
592 if ((fd = open(g->filename, O_RDONLY)) < 0) {
593 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
597 if (fstat(fd, &st) < 0) {
598 avahi_log_error("fstat(): %s", strerror(errno));
602 g->mtime = st.st_mtime;
604 XML_SetUserData(parser, &u);
606 XML_SetElementHandler(parser, xml_start, xml_end);
607 XML_SetCharacterDataHandler(parser, xml_cdata);
612 #define BUFSIZE (10*1024)
614 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
615 avahi_log_error("XML_GetBuffer() failed.");
619 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
620 avahi_log_error("read(): %s\n", strerror(errno));
624 if (!XML_ParseBuffer(parser, n, n == 0)) {
625 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", (int) XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
640 XML_ParserFree(parser);
647 static void load_file(char *n) {
648 StaticServiceGroup *g;
651 for (g = groups; g; g = g->groups_next)
652 if (strcmp(g->filename, n) == 0)
655 avahi_log_info("Loading service file %s.", n);
657 g = static_service_group_new(n);
658 if (static_service_group_load(g) < 0) {
659 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
660 static_service_group_free(g);
664 void static_service_load(int in_chroot) {
665 StaticServiceGroup *g, *n;
670 for (g = groups; g; g = n) {
675 if (stat(g->filename, &st) < 0) {
678 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
680 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
682 static_service_group_free(g);
683 } else if (st.st_mtime != g->mtime) {
684 avahi_log_info("Service group file %s changed, reloading.", g->filename);
686 if (static_service_group_load(g) < 0) {
687 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
688 static_service_group_free(g);
693 memset(&globbuf, 0, sizeof(globbuf));
695 if ((globret = glob(in_chroot ? "/services/*.service" : AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf)) != 0)
700 avahi_log_error("Not enough memory to read service directory "AVAHI_SERVICE_DIR".");
705 avahi_log_info("No service file found in "AVAHI_SERVICE_DIR".");
709 avahi_log_error("Failed to read "AVAHI_SERVICE_DIR".");
714 for (p = globbuf.gl_pathv; *p; p++)
721 void static_service_free_all(void) {
724 static_service_group_free(groups);
727 void static_service_add_to_server(void) {
728 StaticServiceGroup *g;
730 for (g = groups; g; g = g->groups_next)
731 add_static_service_group_to_server(g);
734 void static_service_remove_from_server(void) {
735 StaticServiceGroup *g;
737 for (g = groups; g; g = g->groups_next)
738 remove_static_service_group_from_server(g);