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 *txt_records;
60 AVAHI_LLIST_FIELDS(StaticService, services);
63 struct StaticServiceGroup {
67 char *name, *chosen_name;
68 int replace_wildcards;
70 AvahiSEntryGroup *entry_group;
71 AVAHI_LLIST_HEAD(StaticService, services);
72 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
75 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
77 static char *replacestr(const char *pattern, const char *a, const char *b) {
78 char *r = NULL, *e, *n;
80 while ((e = strstr(pattern, a))) {
83 k = avahi_strndup(pattern, e - pattern);
85 n = avahi_strdup_printf("%s%s%s", r, k, b);
87 n = avahi_strdup_printf("%s%s", k, b);
93 pattern = e + strlen(a);
97 return avahi_strdup(pattern);
99 n = avahi_strdup_printf("%s%s", r, pattern);
105 static void add_static_service_group_to_server(StaticServiceGroup *g);
106 static void remove_static_service_group_from_server(StaticServiceGroup *g);
108 static StaticService *static_service_new(StaticServiceGroup *group) {
112 s = avahi_new(StaticService, 1);
115 s->type = s->host_name = s->domain_name = NULL;
117 s->protocol = AVAHI_PROTO_UNSPEC;
119 s->txt_records = NULL;
121 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
126 static StaticServiceGroup *static_service_group_new(char *filename) {
127 StaticServiceGroup *g;
130 g = avahi_new(StaticServiceGroup, 1);
131 g->filename = avahi_strdup(filename);
133 g->name = g->chosen_name = NULL;
134 g->replace_wildcards = 0;
135 g->entry_group = NULL;
137 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
138 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
143 static void static_service_free(StaticService *s) {
146 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
149 avahi_free(s->host_name);
150 avahi_free(s->domain_name);
152 avahi_string_list_free(s->txt_records);
157 static void static_service_group_free(StaticServiceGroup *g) {
161 avahi_s_entry_group_free(g->entry_group);
164 static_service_free(g->services);
166 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
168 avahi_free(g->filename);
170 avahi_free(g->chosen_name);
174 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
175 StaticServiceGroup *g = userdata;
180 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
183 remove_static_service_group_from_server(g);
185 n = avahi_alternative_service_name(g->chosen_name);
186 avahi_free(g->chosen_name);
189 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
191 add_static_service_group_to_server(g);
192 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
193 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
196 static void add_static_service_group_to_server(StaticServiceGroup *g) {
201 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
202 /* This service group is already registered in the server */
205 if (!g->chosen_name) {
207 if (g->replace_wildcards)
208 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
210 g->chosen_name = avahi_strdup(g->name);
215 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
217 assert(avahi_s_entry_group_is_empty(g->entry_group));
219 for (s = g->services; s; s = s->services_next) {
221 if (avahi_server_add_service_strlst(
225 g->chosen_name, s->type,
226 s->domain_name, s->host_name, s->port,
227 s->txt_records) < 0) {
228 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
229 g->chosen_name, s->type, g->filename,
230 avahi_strerror(avahi_server_errno(avahi_server)));
231 remove_static_service_group_from_server(g);
236 avahi_s_entry_group_commit(g->entry_group);
239 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
243 avahi_s_entry_group_reset(g->entry_group);
248 XML_TAG_SERVICE_GROUP,
258 struct xml_userdata {
259 StaticServiceGroup *group;
260 StaticService *service;
261 xml_tag_name current_tag;
266 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
267 struct xml_userdata *u = data;
274 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
279 u->current_tag = XML_TAG_SERVICE_GROUP;
280 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
281 u->current_tag = XML_TAG_NAME;
284 if (strcmp(attr[0], "replace-wildcards") == 0)
285 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
293 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
294 u->current_tag = XML_TAG_SERVICE;
297 u->service = static_service_new(u->group);
300 if (strcmp(attr[0], "protocol") == 0) {
301 AvahiProtocol protocol;
303 if (strcmp(attr[1], "ipv4") == 0) {
304 protocol = AVAHI_PROTO_INET;
305 } else if (strcmp(attr[1], "ipv6") == 0) {
306 protocol = AVAHI_PROTO_INET6;
307 } else if (strcmp(attr[1], "any") == 0) {
308 protocol = AVAHI_PROTO_UNSPEC;
310 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
315 u->service->protocol = protocol;
323 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
327 u->current_tag = XML_TAG_TYPE;
328 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
332 u->current_tag = XML_TAG_DOMAIN_NAME;
333 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
337 u->current_tag = XML_TAG_HOST_NAME;
338 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
342 u->current_tag = XML_TAG_PORT;
343 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
347 u->current_tag = XML_TAG_TXT_RECORD;
349 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
356 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
361 static void XMLCALL xml_end(void *data, const char *el) {
362 struct xml_userdata *u = data;
368 switch (u->current_tag) {
369 case XML_TAG_SERVICE_GROUP:
371 if (!u->group->name || !u->group->services) {
372 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
377 u->current_tag = XML_TAG_INVALID;
380 case XML_TAG_SERVICE:
382 if (!u->service->type) {
383 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
389 u->current_tag = XML_TAG_SERVICE_GROUP;
393 u->current_tag = XML_TAG_SERVICE_GROUP;
400 p = u->buf ? atoi(u->buf) : 0;
402 if (p < 0 || p > 0xFFFF) {
403 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
408 u->service->port = (uint16_t) p;
410 u->current_tag = XML_TAG_SERVICE;
414 case XML_TAG_TXT_RECORD: {
417 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
418 u->current_tag = XML_TAG_SERVICE;
423 case XML_TAG_DOMAIN_NAME:
424 case XML_TAG_HOST_NAME:
425 u->current_tag = XML_TAG_SERVICE;
428 case XML_TAG_INVALID:
436 static char *append_cdata(char *t, const char *n, int length) {
443 k = avahi_strndup(n, length);
446 r = avahi_strdup_printf("%s%s", t, k);
455 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
456 struct xml_userdata *u = data;
462 switch (u->current_tag) {
464 u->group->name = append_cdata(u->group->name, s, len);
469 u->service->type = append_cdata(u->service->type, s, len);
472 case XML_TAG_DOMAIN_NAME:
474 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
477 case XML_TAG_HOST_NAME:
479 u->service->host_name = append_cdata(u->service->host_name, s, len);
483 case XML_TAG_TXT_RECORD:
485 u->buf = append_cdata(u->buf, s, len);
488 case XML_TAG_SERVICE_GROUP:
489 case XML_TAG_SERVICE:
490 case XML_TAG_INVALID:
495 static int static_service_group_load(StaticServiceGroup *g) {
496 XML_Parser parser = NULL;
498 struct xml_userdata u;
508 u.current_tag = XML_TAG_INVALID;
511 /* Cleanup old data in this service group, if available */
512 remove_static_service_group_from_server(g);
514 static_service_free(g->services);
517 avahi_free(g->chosen_name);
518 g->name = g->chosen_name = NULL;
519 g->replace_wildcards = 0;
521 if (!(parser = XML_ParserCreate(NULL))) {
522 avahi_log_error("XML_ParserCreate() failed.");
526 if ((fd = open(g->filename, O_RDONLY)) < 0) {
527 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
531 if (fstat(fd, &st) < 0) {
532 avahi_log_error("fstat(): %s", strerror(errno));
536 g->mtime = st.st_mtime;
538 XML_SetUserData(parser, &u);
540 XML_SetElementHandler(parser, xml_start, xml_end);
541 XML_SetCharacterDataHandler(parser, xml_cdata);
546 #define BUFSIZE (10*1024)
548 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
549 avahi_log_error("XML_GetBuffer() failed.");
553 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
554 avahi_log_error("read(): %s\n", strerror(errno));
558 if (!XML_ParseBuffer(parser, n, n == 0)) {
559 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
574 XML_ParserFree(parser);
581 static void load_file(char *n) {
582 StaticServiceGroup *g;
585 for (g = groups; g; g = g->groups_next)
586 if (strcmp(g->filename, n) == 0)
589 avahi_log_info("Loading service file %s.", n);
591 g = static_service_group_new(n);
592 if (static_service_group_load(g) < 0) {
593 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
594 static_service_group_free(g);
598 void static_service_load(void) {
599 StaticServiceGroup *g, *n;
603 for (g = groups; g; g = n) {
608 if (stat(g->filename, &st) < 0) {
611 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
613 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
615 static_service_group_free(g);
616 } else if (st.st_mtime != g->mtime) {
617 avahi_log_info("Service group file %s changed, reloading.", g->filename);
619 if (static_service_group_load(g) < 0) {
620 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
621 static_service_group_free(g);
626 memset(&globbuf, 0, sizeof(globbuf));
627 if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
628 avahi_log_error("Failed to read service directory.");
630 for (p = globbuf.gl_pathv; *p; p++)
637 void static_service_free_all(void) {
640 static_service_group_free(groups);
643 void static_service_add_to_server(void) {
644 StaticServiceGroup *g;
646 for (g = groups; g; g = g->groups_next)
647 add_static_service_group_to_server(g);
650 void static_service_remove_from_server(void) {
651 StaticServiceGroup *g;
653 for (g = groups; g; g = g->groups_next)
654 remove_static_service_group_from_server(g);