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 #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>
44 #include "static-services.h"
46 typedef struct StaticService StaticService;
47 typedef struct StaticServiceGroup StaticServiceGroup;
49 struct StaticService {
50 StaticServiceGroup *group;
58 AvahiStringList *subtypes;
60 AvahiStringList *txt_records;
62 AVAHI_LLIST_FIELDS(StaticService, services);
65 struct StaticServiceGroup {
69 char *name, *chosen_name;
70 int replace_wildcards;
72 AvahiSEntryGroup *entry_group;
73 AVAHI_LLIST_HEAD(StaticService, services);
74 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
77 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
79 static char *replacestr(const char *pattern, const char *a, const char *b) {
80 char *r = NULL, *e, *n;
82 while ((e = strstr(pattern, a))) {
85 k = avahi_strndup(pattern, e - pattern);
87 n = avahi_strdup_printf("%s%s%s", r, k, b);
89 n = avahi_strdup_printf("%s%s", k, b);
95 pattern = e + strlen(a);
99 return avahi_strdup(pattern);
101 n = avahi_strdup_printf("%s%s", r, pattern);
107 static void add_static_service_group_to_server(StaticServiceGroup *g);
108 static void remove_static_service_group_from_server(StaticServiceGroup *g);
110 static StaticService *static_service_new(StaticServiceGroup *group) {
114 s = avahi_new(StaticService, 1);
117 s->type = s->host_name = s->domain_name = NULL;
119 s->protocol = AVAHI_PROTO_UNSPEC;
121 s->txt_records = NULL;
124 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
129 static StaticServiceGroup *static_service_group_new(char *filename) {
130 StaticServiceGroup *g;
133 g = avahi_new(StaticServiceGroup, 1);
134 g->filename = avahi_strdup(filename);
136 g->name = g->chosen_name = NULL;
137 g->replace_wildcards = 0;
138 g->entry_group = NULL;
140 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
141 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
146 static void static_service_free(StaticService *s) {
149 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
152 avahi_free(s->host_name);
153 avahi_free(s->domain_name);
155 avahi_string_list_free(s->txt_records);
156 avahi_string_list_free(s->subtypes);
161 static void static_service_group_free(StaticServiceGroup *g) {
165 avahi_s_entry_group_free(g->entry_group);
168 static_service_free(g->services);
170 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
172 avahi_free(g->filename);
174 avahi_free(g->chosen_name);
178 static void entry_group_callback(AvahiServer *s, AVAHI_GCC_UNUSED AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
179 StaticServiceGroup *g = userdata;
186 case AVAHI_ENTRY_GROUP_COLLISION: {
189 remove_static_service_group_from_server(g);
191 n = avahi_alternative_service_name(g->chosen_name);
192 avahi_free(g->chosen_name);
195 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
197 add_static_service_group_to_server(g);
201 case AVAHI_ENTRY_GROUP_ESTABLISHED:
202 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
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);
210 case AVAHI_ENTRY_GROUP_UNCOMMITED:
211 case AVAHI_ENTRY_GROUP_REGISTERING:
216 static void add_static_service_group_to_server(StaticServiceGroup *g) {
221 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
222 /* This service group is already registered in the server */
225 if (!g->chosen_name) {
227 if (g->replace_wildcards)
228 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
230 g->chosen_name = avahi_strdup(g->name);
235 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
237 assert(avahi_s_entry_group_is_empty(g->entry_group));
239 for (s = g->services; s; s = s->services_next) {
242 if (avahi_server_add_service_strlst(
245 AVAHI_IF_UNSPEC, s->protocol,
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);
257 for (i = s->subtypes; i; i = i->next) {
259 if (avahi_server_add_service_subtype(
262 AVAHI_IF_UNSPEC, s->protocol,
264 g->chosen_name, s->type, s->domain_name,
265 (char*) i->text) < 0) {
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)));
274 avahi_s_entry_group_commit(g->entry_group);
277 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
281 avahi_s_entry_group_reset(g->entry_group);
286 XML_TAG_SERVICE_GROUP,
297 struct xml_userdata {
298 StaticServiceGroup *group;
299 StaticService *service;
300 xml_tag_name current_tag;
305 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
306 struct xml_userdata *u = data;
313 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
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;
323 if (strcmp(attr[0], "replace-wildcards") == 0)
324 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
332 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
333 u->current_tag = XML_TAG_SERVICE;
336 u->service = static_service_new(u->group);
339 if (strcmp(attr[0], "protocol") == 0) {
340 AvahiProtocol protocol;
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;
349 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
354 u->service->protocol = protocol;
362 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
366 u->current_tag = XML_TAG_TYPE;
367 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "subtype") == 0) {
371 u->current_tag = XML_TAG_SUBTYPE;
372 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
376 u->current_tag = XML_TAG_DOMAIN_NAME;
377 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
381 u->current_tag = XML_TAG_HOST_NAME;
382 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
386 u->current_tag = XML_TAG_PORT;
387 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
391 u->current_tag = XML_TAG_TXT_RECORD;
393 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
400 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
405 static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
406 struct xml_userdata *u = data;
412 switch (u->current_tag) {
413 case XML_TAG_SERVICE_GROUP:
415 if (!u->group->name || !u->group->services) {
416 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
421 u->current_tag = XML_TAG_INVALID;
424 case XML_TAG_SERVICE:
426 if (!u->service->type) {
427 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
433 u->current_tag = XML_TAG_SERVICE_GROUP;
437 u->current_tag = XML_TAG_SERVICE_GROUP;
444 p = u->buf ? atoi(u->buf) : 0;
446 if (p < 0 || p > 0xFFFF) {
447 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
452 u->service->port = (uint16_t) p;
454 u->current_tag = XML_TAG_SERVICE;
458 case XML_TAG_TXT_RECORD: {
461 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
462 u->current_tag = XML_TAG_SERVICE;
466 case XML_TAG_SUBTYPE: {
469 u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
470 u->current_tag = XML_TAG_SERVICE;
475 case XML_TAG_DOMAIN_NAME:
476 case XML_TAG_HOST_NAME:
477 u->current_tag = XML_TAG_SERVICE;
480 case XML_TAG_INVALID:
488 static char *append_cdata(char *t, const char *n, int length) {
495 k = avahi_strndup(n, length);
498 r = avahi_strdup_printf("%s%s", t, k);
507 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
508 struct xml_userdata *u = data;
514 switch (u->current_tag) {
516 u->group->name = append_cdata(u->group->name, s, len);
521 u->service->type = append_cdata(u->service->type, s, len);
524 case XML_TAG_DOMAIN_NAME:
526 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
529 case XML_TAG_HOST_NAME:
531 u->service->host_name = append_cdata(u->service->host_name, s, len);
535 case XML_TAG_TXT_RECORD:
536 case XML_TAG_SUBTYPE:
538 u->buf = append_cdata(u->buf, s, len);
541 case XML_TAG_SERVICE_GROUP:
542 case XML_TAG_SERVICE:
543 case XML_TAG_INVALID:
548 static int static_service_group_load(StaticServiceGroup *g) {
549 XML_Parser parser = NULL;
551 struct xml_userdata u;
561 u.current_tag = XML_TAG_INVALID;
564 /* Cleanup old data in this service group, if available */
565 remove_static_service_group_from_server(g);
567 static_service_free(g->services);
570 avahi_free(g->chosen_name);
571 g->name = g->chosen_name = NULL;
572 g->replace_wildcards = 0;
574 if (!(parser = XML_ParserCreate(NULL))) {
575 avahi_log_error("XML_ParserCreate() failed.");
579 if ((fd = open(g->filename, O_RDONLY)) < 0) {
580 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
584 if (fstat(fd, &st) < 0) {
585 avahi_log_error("fstat(): %s", strerror(errno));
589 g->mtime = st.st_mtime;
591 XML_SetUserData(parser, &u);
593 XML_SetElementHandler(parser, xml_start, xml_end);
594 XML_SetCharacterDataHandler(parser, xml_cdata);
599 #define BUFSIZE (10*1024)
601 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
602 avahi_log_error("XML_GetBuffer() failed.");
606 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
607 avahi_log_error("read(): %s\n", strerror(errno));
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)));
627 XML_ParserFree(parser);
634 static void load_file(char *n) {
635 StaticServiceGroup *g;
638 for (g = groups; g; g = g->groups_next)
639 if (strcmp(g->filename, n) == 0)
642 avahi_log_info("Loading service file %s.", n);
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);
651 void static_service_load(void) {
652 StaticServiceGroup *g, *n;
656 for (g = groups; g; g = n) {
661 if (stat(g->filename, &st) < 0) {
664 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
666 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
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);
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);
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.");
683 for (p = globbuf.gl_pathv; *p; p++)
690 void static_service_free_all(void) {
693 static_service_group_free(groups);
696 void static_service_add_to_server(void) {
697 StaticServiceGroup *g;
699 for (g = groups; g; g = g->groups_next)
700 add_static_service_group_to_server(g);
703 void static_service_remove_from_server(void) {
704 StaticServiceGroup *g;
706 for (g = groups; g; g = g->groups_next)
707 remove_static_service_group_from_server(g);