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-core/log.h>
41 #include "static-services.h"
43 typedef struct StaticService StaticService;
44 typedef struct StaticServiceGroup StaticServiceGroup;
46 struct StaticService {
47 StaticServiceGroup *group;
55 AvahiStringList *txt_records;
57 AVAHI_LLIST_FIELDS(StaticService, services);
60 struct StaticServiceGroup {
64 char *name, *chosen_name;
65 int replace_wildcards;
67 AvahiSEntryGroup *entry_group;
68 AVAHI_LLIST_HEAD(StaticService, services);
69 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
72 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
74 static char *replacestr(const char *pattern, const char *a, const char *b) {
75 char *r = NULL, *e, *n;
77 while ((e = strstr(pattern, a))) {
80 k = avahi_strndup(pattern, e - pattern);
82 n = avahi_strdup_printf("%s%s%s", r, k, b);
84 n = avahi_strdup_printf("%s%s", k, b);
90 pattern = e + strlen(a);
94 return avahi_strdup(pattern);
96 n = avahi_strdup_printf("%s%s", r, pattern);
102 static void add_static_service_group_to_server(StaticServiceGroup *g);
103 static void remove_static_service_group_from_server(StaticServiceGroup *g);
105 static StaticService *static_service_new(StaticServiceGroup *group) {
109 s = avahi_new(StaticService, 1);
112 s->type = s->host_name = s->domain_name = NULL;
114 s->protocol = AVAHI_PROTO_UNSPEC;
116 s->txt_records = NULL;
118 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
123 static StaticServiceGroup *static_service_group_new(char *filename) {
124 StaticServiceGroup *g;
127 g = avahi_new(StaticServiceGroup, 1);
128 g->filename = avahi_strdup(filename);
130 g->name = g->chosen_name = NULL;
131 g->replace_wildcards = 0;
132 g->entry_group = NULL;
134 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
135 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
140 static void static_service_free(StaticService *s) {
143 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
146 avahi_free(s->host_name);
147 avahi_free(s->domain_name);
149 avahi_string_list_free(s->txt_records);
154 static void static_service_group_free(StaticServiceGroup *g) {
158 avahi_s_entry_group_free(g->entry_group);
161 static_service_free(g->services);
163 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
165 avahi_free(g->filename);
167 avahi_free(g->chosen_name);
171 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
172 StaticServiceGroup *g = userdata;
177 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
180 remove_static_service_group_from_server(g);
182 n = avahi_alternative_service_name(g->chosen_name);
183 avahi_free(g->chosen_name);
186 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
188 add_static_service_group_to_server(g);
189 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
190 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
193 static void add_static_service_group_to_server(StaticServiceGroup *g) {
198 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
199 /* This service group is already registered in the server */
203 avahi_free(g->chosen_name);
205 if (g->replace_wildcards)
206 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
208 g->chosen_name = avahi_strdup(g->name);
211 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
213 assert(avahi_s_entry_group_is_empty(g->entry_group));
215 for (s = g->services; s; s = s->services_next) {
217 if (avahi_server_add_service_strlst(
221 g->chosen_name, s->type,
222 s->domain_name, s->host_name, s->port,
223 s->txt_records) < 0) {
224 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
225 g->chosen_name, s->type, g->filename,
226 avahi_strerror(avahi_server_errno(avahi_server)));
227 remove_static_service_group_from_server(g);
232 avahi_s_entry_group_commit(g->entry_group);
235 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
239 avahi_s_entry_group_reset(g->entry_group);
244 XML_TAG_SERVICE_GROUP,
255 struct xml_userdata {
256 StaticServiceGroup *group;
257 StaticService *service;
258 xml_tag_name current_tag;
263 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
264 struct xml_userdata *u = data;
271 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
276 u->current_tag = XML_TAG_SERVICE_GROUP;
277 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
278 u->current_tag = XML_TAG_NAME;
281 if (strcmp(attr[0], "replace-wildcards") == 0)
282 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
290 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
295 u->service = static_service_new(u->group);
297 u->current_tag = XML_TAG_SERVICE;
298 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
302 u->current_tag = XML_TAG_TYPE;
303 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
307 u->current_tag = XML_TAG_DOMAIN_NAME;
308 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
312 u->current_tag = XML_TAG_HOST_NAME;
313 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
317 u->current_tag = XML_TAG_PORT;
318 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "protocol") == 0) {
322 u->current_tag = XML_TAG_PROTOCOL;
323 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
327 u->current_tag = XML_TAG_TXT_RECORD;
329 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
336 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
341 static void XMLCALL xml_end(void *data, const char *el) {
342 struct xml_userdata *u = data;
348 switch (u->current_tag) {
349 case XML_TAG_SERVICE_GROUP:
351 if (!u->group->name || !u->group->services) {
352 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
357 u->current_tag = XML_TAG_INVALID;
360 case XML_TAG_SERVICE:
362 if (!u->service->type) {
363 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
369 u->current_tag = XML_TAG_SERVICE_GROUP;
373 u->current_tag = XML_TAG_SERVICE_GROUP;
380 p = u->buf ? atoi(u->buf) : 0;
382 if (p < 0 || p > 0xFFFF) {
383 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
388 u->service->port = (uint16_t) p;
390 u->current_tag = XML_TAG_SERVICE;
394 case XML_TAG_PROTOCOL: {
398 if (u->buf && strcasecmp (u->buf, "ipv4") == 0) {
399 protocol = AVAHI_PROTO_INET;
400 } else if (u->buf && strcasecmp (u->buf, "ipv6") == 0) {
401 protocol = AVAHI_PROTO_INET6;
402 } else if (u->buf && strcasecmp (u->buf, "any") == 0) {
403 protocol = AVAHI_PROTO_UNSPEC;
405 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, u->buf);
410 u->service->protocol = protocol;
411 u->current_tag = XML_TAG_SERVICE;
415 case XML_TAG_TXT_RECORD: {
418 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
419 u->current_tag = XML_TAG_SERVICE;
424 case XML_TAG_DOMAIN_NAME:
425 case XML_TAG_HOST_NAME:
426 u->current_tag = XML_TAG_SERVICE;
429 case XML_TAG_INVALID:
437 static char *append_cdata(char *t, const char *n, int length) {
444 k = avahi_strndup(n, length);
447 r = avahi_strdup_printf("%s%s", t, k);
456 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
457 struct xml_userdata *u = data;
463 switch (u->current_tag) {
465 u->group->name = append_cdata(u->group->name, s, len);
470 u->service->type = append_cdata(u->service->type, s, len);
473 case XML_TAG_DOMAIN_NAME:
475 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
478 case XML_TAG_HOST_NAME:
480 u->service->host_name = append_cdata(u->service->host_name, s, len);
484 case XML_TAG_PROTOCOL:
485 case XML_TAG_TXT_RECORD:
487 u->buf = append_cdata(u->buf, s, len);
490 case XML_TAG_SERVICE_GROUP:
491 case XML_TAG_SERVICE:
492 case XML_TAG_INVALID:
497 static int static_service_group_load(StaticServiceGroup *g) {
498 XML_Parser parser = NULL;
500 struct xml_userdata u;
510 u.current_tag = XML_TAG_INVALID;
513 /* Cleanup old data in this service group, if available */
514 remove_static_service_group_from_server(g);
516 static_service_free(g->services);
519 avahi_free(g->chosen_name);
520 g->name = g->chosen_name = NULL;
521 g->replace_wildcards = 0;
523 if (!(parser = XML_ParserCreate(NULL))) {
524 avahi_log_error("XML_ParserCreate() failed.");
528 if ((fd = open(g->filename, O_RDONLY)) < 0) {
529 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
533 if (fstat(fd, &st) < 0) {
534 avahi_log_error("fstat(): %s", strerror(errno));
538 g->mtime = st.st_mtime;
540 XML_SetUserData(parser, &u);
542 XML_SetElementHandler(parser, xml_start, xml_end);
543 XML_SetCharacterDataHandler(parser, xml_cdata);
548 #define BUFSIZE (10*1024)
550 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
551 avahi_log_error("XML_GetBuffer() failed.");
555 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
556 avahi_log_error("read(): %s\n", strerror(errno));
560 if (!XML_ParseBuffer(parser, n, n == 0)) {
561 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
576 XML_ParserFree(parser);
583 static void load_file(char *n) {
584 StaticServiceGroup *g;
587 for (g = groups; g; g = g->groups_next)
588 if (strcmp(g->filename, n) == 0)
591 avahi_log_info("Loading service file %s", n);
593 g = static_service_group_new(n);
594 if (static_service_group_load(g) < 0) {
595 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
596 static_service_group_free(g);
600 void static_service_load(void) {
601 StaticServiceGroup *g, *n;
605 for (g = groups; g; g = n) {
610 if (stat(g->filename, &st) < 0) {
613 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
615 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
617 static_service_group_free(g);
618 } else if (st.st_mtime != g->mtime) {
619 avahi_log_info("Service group file %s changed, reloading.", g->filename);
621 if (static_service_group_load(g) < 0) {
622 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
623 static_service_group_free(g);
628 memset(&globbuf, 0, sizeof(globbuf));
629 if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
630 avahi_log_error("Failed to read service directory.");
632 for (p = globbuf.gl_pathv; *p; p++)
639 void static_service_free_all(void) {
642 static_service_group_free(groups);
645 void static_service_add_to_server(void) {
646 StaticServiceGroup *g;
648 for (g = groups; g; g = g->groups_next)
649 add_static_service_group_to_server(g);
652 void static_service_remove_from_server(void) {
653 StaticServiceGroup *g;
655 for (g = groups; g; g = g->groups_next)
656 remove_static_service_group_from_server(g);