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>
43 #include "static-services.h"
45 typedef struct StaticService StaticService;
46 typedef struct StaticServiceGroup StaticServiceGroup;
48 struct StaticService {
49 StaticServiceGroup *group;
57 AvahiStringList *txt_records;
59 AVAHI_LLIST_FIELDS(StaticService, services);
62 struct StaticServiceGroup {
66 char *name, *chosen_name;
67 int replace_wildcards;
69 AvahiSEntryGroup *entry_group;
70 AVAHI_LLIST_HEAD(StaticService, services);
71 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
74 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
76 static char *replacestr(const char *pattern, const char *a, const char *b) {
77 char *r = NULL, *e, *n;
79 while ((e = strstr(pattern, a))) {
82 k = avahi_strndup(pattern, e - pattern);
84 n = avahi_strdup_printf("%s%s%s", r, k, b);
86 n = avahi_strdup_printf("%s%s", k, b);
92 pattern = e + strlen(a);
96 return avahi_strdup(pattern);
98 n = avahi_strdup_printf("%s%s", r, pattern);
104 static void add_static_service_group_to_server(StaticServiceGroup *g);
105 static void remove_static_service_group_from_server(StaticServiceGroup *g);
107 static StaticService *static_service_new(StaticServiceGroup *group) {
111 s = avahi_new(StaticService, 1);
114 s->type = s->host_name = s->domain_name = NULL;
116 s->protocol = AVAHI_PROTO_UNSPEC;
118 s->txt_records = NULL;
120 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
125 static StaticServiceGroup *static_service_group_new(char *filename) {
126 StaticServiceGroup *g;
129 g = avahi_new(StaticServiceGroup, 1);
130 g->filename = avahi_strdup(filename);
132 g->name = g->chosen_name = NULL;
133 g->replace_wildcards = 0;
134 g->entry_group = NULL;
136 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
137 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
142 static void static_service_free(StaticService *s) {
145 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
148 avahi_free(s->host_name);
149 avahi_free(s->domain_name);
151 avahi_string_list_free(s->txt_records);
156 static void static_service_group_free(StaticServiceGroup *g) {
160 avahi_s_entry_group_free(g->entry_group);
163 static_service_free(g->services);
165 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
167 avahi_free(g->filename);
169 avahi_free(g->chosen_name);
173 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
174 StaticServiceGroup *g = userdata;
179 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
182 remove_static_service_group_from_server(g);
184 n = avahi_alternative_service_name(g->chosen_name);
185 avahi_free(g->chosen_name);
188 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
190 add_static_service_group_to_server(g);
191 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
192 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
195 static void add_static_service_group_to_server(StaticServiceGroup *g) {
200 if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group))
201 /* This service group is already registered in the server */
204 if (!g->chosen_name) {
206 if (g->replace_wildcards)
207 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
209 g->chosen_name = avahi_strdup(g->name);
214 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
216 assert(avahi_s_entry_group_is_empty(g->entry_group));
218 for (s = g->services; s; s = s->services_next) {
220 if (avahi_server_add_service_strlst(
224 g->chosen_name, s->type,
225 s->domain_name, s->host_name, s->port,
226 s->txt_records) < 0) {
227 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
228 g->chosen_name, s->type, g->filename,
229 avahi_strerror(avahi_server_errno(avahi_server)));
230 remove_static_service_group_from_server(g);
235 avahi_s_entry_group_commit(g->entry_group);
238 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
242 avahi_s_entry_group_reset(g->entry_group);
247 XML_TAG_SERVICE_GROUP,
257 struct xml_userdata {
258 StaticServiceGroup *group;
259 StaticService *service;
260 xml_tag_name current_tag;
265 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
266 struct xml_userdata *u = data;
273 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
278 u->current_tag = XML_TAG_SERVICE_GROUP;
279 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
280 u->current_tag = XML_TAG_NAME;
283 if (strcmp(attr[0], "replace-wildcards") == 0)
284 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
292 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
293 u->current_tag = XML_TAG_SERVICE;
296 u->service = static_service_new(u->group);
299 if (strcmp(attr[0], "protocol") == 0) {
300 AvahiProtocol protocol;
302 if (strcmp(attr[1], "ipv4") == 0) {
303 protocol = AVAHI_PROTO_INET;
304 } else if (strcmp(attr[1], "ipv6") == 0) {
305 protocol = AVAHI_PROTO_INET6;
306 } else if (strcmp(attr[1], "any") == 0) {
307 protocol = AVAHI_PROTO_UNSPEC;
309 avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]);
314 u->service->protocol = protocol;
322 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
326 u->current_tag = XML_TAG_TYPE;
327 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
331 u->current_tag = XML_TAG_DOMAIN_NAME;
332 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
336 u->current_tag = XML_TAG_HOST_NAME;
337 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
341 u->current_tag = XML_TAG_PORT;
342 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
346 u->current_tag = XML_TAG_TXT_RECORD;
348 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
355 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
360 static void XMLCALL xml_end(void *data, const char *el) {
361 struct xml_userdata *u = data;
367 switch (u->current_tag) {
368 case XML_TAG_SERVICE_GROUP:
370 if (!u->group->name || !u->group->services) {
371 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
376 u->current_tag = XML_TAG_INVALID;
379 case XML_TAG_SERVICE:
381 if (!u->service->type) {
382 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
388 u->current_tag = XML_TAG_SERVICE_GROUP;
392 u->current_tag = XML_TAG_SERVICE_GROUP;
399 p = u->buf ? atoi(u->buf) : 0;
401 if (p < 0 || p > 0xFFFF) {
402 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
407 u->service->port = (uint16_t) p;
409 u->current_tag = XML_TAG_SERVICE;
413 case XML_TAG_TXT_RECORD: {
416 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
417 u->current_tag = XML_TAG_SERVICE;
422 case XML_TAG_DOMAIN_NAME:
423 case XML_TAG_HOST_NAME:
424 u->current_tag = XML_TAG_SERVICE;
427 case XML_TAG_INVALID:
435 static char *append_cdata(char *t, const char *n, int length) {
442 k = avahi_strndup(n, length);
445 r = avahi_strdup_printf("%s%s", t, k);
454 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
455 struct xml_userdata *u = data;
461 switch (u->current_tag) {
463 u->group->name = append_cdata(u->group->name, s, len);
468 u->service->type = append_cdata(u->service->type, s, len);
471 case XML_TAG_DOMAIN_NAME:
473 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
476 case XML_TAG_HOST_NAME:
478 u->service->host_name = append_cdata(u->service->host_name, s, len);
482 case XML_TAG_TXT_RECORD:
484 u->buf = append_cdata(u->buf, s, len);
487 case XML_TAG_SERVICE_GROUP:
488 case XML_TAG_SERVICE:
489 case XML_TAG_INVALID:
494 static int static_service_group_load(StaticServiceGroup *g) {
495 XML_Parser parser = NULL;
497 struct xml_userdata u;
507 u.current_tag = XML_TAG_INVALID;
510 /* Cleanup old data in this service group, if available */
511 remove_static_service_group_from_server(g);
513 static_service_free(g->services);
516 avahi_free(g->chosen_name);
517 g->name = g->chosen_name = NULL;
518 g->replace_wildcards = 0;
520 if (!(parser = XML_ParserCreate(NULL))) {
521 avahi_log_error("XML_ParserCreate() failed.");
525 if ((fd = open(g->filename, O_RDONLY)) < 0) {
526 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
530 if (fstat(fd, &st) < 0) {
531 avahi_log_error("fstat(): %s", strerror(errno));
535 g->mtime = st.st_mtime;
537 XML_SetUserData(parser, &u);
539 XML_SetElementHandler(parser, xml_start, xml_end);
540 XML_SetCharacterDataHandler(parser, xml_cdata);
545 #define BUFSIZE (10*1024)
547 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
548 avahi_log_error("XML_GetBuffer() failed.");
552 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
553 avahi_log_error("read(): %s\n", strerror(errno));
557 if (!XML_ParseBuffer(parser, n, n == 0)) {
558 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
573 XML_ParserFree(parser);
580 static void load_file(char *n) {
581 StaticServiceGroup *g;
584 for (g = groups; g; g = g->groups_next)
585 if (strcmp(g->filename, n) == 0)
588 avahi_log_info("Loading service file %s.", n);
590 g = static_service_group_new(n);
591 if (static_service_group_load(g) < 0) {
592 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
593 static_service_group_free(g);
597 void static_service_load(void) {
598 StaticServiceGroup *g, *n;
602 for (g = groups; g; g = n) {
607 if (stat(g->filename, &st) < 0) {
610 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
612 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
614 static_service_group_free(g);
615 } else if (st.st_mtime != g->mtime) {
616 avahi_log_info("Service group file %s changed, reloading.", g->filename);
618 if (static_service_group_load(g) < 0) {
619 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
620 static_service_group_free(g);
625 memset(&globbuf, 0, sizeof(globbuf));
626 if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
627 avahi_log_error("Failed to read service directory.");
629 for (p = globbuf.gl_pathv; *p; p++)
636 void static_service_free_all(void) {
639 static_service_group_free(groups);
642 void static_service_add_to_server(void) {
643 StaticServiceGroup *g;
645 for (g = groups; g; g = g->groups_next)
646 add_static_service_group_to_server(g);
649 void static_service_remove_from_server(void) {
650 StaticServiceGroup *g;
652 for (g = groups; g; g = g->groups_next)
653 remove_static_service_group_from_server(g);