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;
54 AvahiStringList *txt_records;
56 AVAHI_LLIST_FIELDS(StaticService, services);
59 struct StaticServiceGroup {
63 char *name, *chosen_name;
64 int replace_wildcards;
66 AvahiSEntryGroup *entry_group;
67 AVAHI_LLIST_HEAD(StaticService, services);
68 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
71 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
73 static char *replacestr(const char *pattern, const char *a, const char *b) {
74 char *r = NULL, *e, *n;
76 while ((e = strstr(pattern, a))) {
79 k = avahi_strndup(pattern, e - pattern);
81 n = avahi_strdup_printf("%s%s%s", r, k, b);
83 n = avahi_strdup_printf("%s%s", k, b);
89 pattern = e + strlen(a);
93 return avahi_strdup(pattern);
95 n = avahi_strdup_printf("%s%s", r, pattern);
101 static void add_static_service_group_to_server(StaticServiceGroup *g);
102 static void remove_static_service_group_from_server(StaticServiceGroup *g);
104 static StaticService *static_service_new(StaticServiceGroup *group) {
108 s = avahi_new(StaticService, 1);
111 s->type = s->host_name = s->domain_name = NULL;
114 s->txt_records = NULL;
116 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
121 static StaticServiceGroup *static_service_group_new(char *filename) {
122 StaticServiceGroup *g;
125 g = avahi_new(StaticServiceGroup, 1);
126 g->filename = avahi_strdup(filename);
128 g->name = g->chosen_name = NULL;
129 g->replace_wildcards = 0;
130 g->entry_group = NULL;
132 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
133 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
138 static void static_service_free(StaticService *s) {
141 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
144 avahi_free(s->host_name);
145 avahi_free(s->domain_name);
147 avahi_string_list_free(s->txt_records);
152 static void static_service_group_free(StaticServiceGroup *g) {
156 avahi_s_entry_group_free(g->entry_group);
159 static_service_free(g->services);
161 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
163 avahi_free(g->filename);
165 avahi_free(g->chosen_name);
169 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
170 StaticServiceGroup *g = userdata;
175 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
178 remove_static_service_group_from_server(g);
180 n = avahi_alternative_service_name(g->chosen_name);
181 avahi_free(g->chosen_name);
184 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
186 add_static_service_group_to_server(g);
187 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
188 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
191 static void add_static_service_group_to_server(StaticServiceGroup *g) {
197 avahi_free(g->chosen_name);
199 if (g->replace_wildcards)
200 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
202 g->chosen_name = avahi_strdup(g->name);
205 g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
207 assert(avahi_s_entry_group_is_empty(g->entry_group));
209 for (s = g->services; s; s = s->services_next) {
211 if (avahi_server_add_service_strlst(
215 g->chosen_name, s->type,
216 s->domain_name, s->host_name, s->port,
217 s->txt_records) < 0) {
218 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s",
219 g->chosen_name, s->type, g->filename,
220 avahi_strerror(avahi_server_errno(avahi_server)));
221 remove_static_service_group_from_server(g);
226 avahi_s_entry_group_commit(g->entry_group);
229 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
233 avahi_s_entry_group_reset(g->entry_group);
238 XML_TAG_SERVICE_GROUP,
248 struct xml_userdata {
249 StaticServiceGroup *group;
250 StaticService *service;
251 xml_tag_name current_tag;
256 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
257 struct xml_userdata *u = data;
264 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
269 u->current_tag = XML_TAG_SERVICE_GROUP;
270 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
271 u->current_tag = XML_TAG_NAME;
274 if (strcmp(attr[0], "replace-wildcards") == 0)
275 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
283 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
288 u->service = static_service_new(u->group);
290 u->current_tag = XML_TAG_SERVICE;
291 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
295 u->current_tag = XML_TAG_TYPE;
296 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
300 u->current_tag = XML_TAG_DOMAIN_NAME;
301 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
305 u->current_tag = XML_TAG_HOST_NAME;
306 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
310 u->current_tag = XML_TAG_PORT;
311 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
315 u->current_tag = XML_TAG_TXT_RECORD;
317 avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
324 avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
329 static void XMLCALL xml_end(void *data, const char *el) {
330 struct xml_userdata *u = data;
336 switch (u->current_tag) {
337 case XML_TAG_SERVICE_GROUP:
339 if (!u->group->name || !u->group->services) {
340 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
345 u->current_tag = XML_TAG_INVALID;
348 case XML_TAG_SERVICE:
350 if (!u->service->type) {
351 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
357 u->current_tag = XML_TAG_SERVICE_GROUP;
361 u->current_tag = XML_TAG_SERVICE_GROUP;
368 p = u->buf ? atoi(u->buf) : 0;
370 if (p < 0 || p > 0xFFFF) {
371 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
376 u->service->port = (uint16_t) p;
378 u->current_tag = XML_TAG_SERVICE;
382 case XML_TAG_TXT_RECORD: {
385 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
386 u->current_tag = XML_TAG_SERVICE;
391 case XML_TAG_DOMAIN_NAME:
392 case XML_TAG_HOST_NAME:
393 u->current_tag = XML_TAG_SERVICE;
396 case XML_TAG_INVALID:
404 static char *append_cdata(char *t, const char *n, int length) {
411 k = avahi_strndup(n, length);
414 r = avahi_strdup_printf("%s%s", t, k);
423 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
424 struct xml_userdata *u = data;
430 switch (u->current_tag) {
432 u->group->name = append_cdata(u->group->name, s, len);
437 u->service->type = append_cdata(u->service->type, s, len);
440 case XML_TAG_DOMAIN_NAME:
442 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
445 case XML_TAG_HOST_NAME:
447 u->service->host_name = append_cdata(u->service->host_name, s, len);
451 case XML_TAG_TXT_RECORD:
452 u->buf = append_cdata(u->buf, s, len);
455 case XML_TAG_SERVICE_GROUP:
456 case XML_TAG_SERVICE:
457 case XML_TAG_INVALID:
462 static int static_service_group_load(StaticServiceGroup *g) {
463 XML_Parser parser = NULL;
465 struct xml_userdata u;
475 u.current_tag = XML_TAG_INVALID;
478 /* Cleanup old data in this service group, if available */
479 remove_static_service_group_from_server(g);
481 static_service_free(g->services);
484 avahi_free(g->chosen_name);
485 g->name = g->chosen_name = NULL;
486 g->replace_wildcards = 0;
488 if (!(parser = XML_ParserCreate(NULL))) {
489 avahi_log_error("XML_ParserCreate() failed.");
493 if ((fd = open(g->filename, O_RDONLY)) < 0) {
494 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
498 if (fstat(fd, &st) < 0) {
499 avahi_log_error("fstat(): %s", strerror(errno));
503 g->mtime = st.st_mtime;
505 XML_SetUserData(parser, &u);
507 XML_SetElementHandler(parser, xml_start, xml_end);
508 XML_SetCharacterDataHandler(parser, xml_cdata);
513 #define BUFSIZE (10*1024)
515 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
516 avahi_log_error("XML_GetBuffer() failed.");
520 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
521 avahi_log_error("read(): %s\n", strerror(errno));
525 if (!XML_ParseBuffer(parser, n, n == 0)) {
526 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
541 XML_ParserFree(parser);
548 static void load_file(char *n) {
549 StaticServiceGroup *g;
552 for (g = groups; g; g = g->groups_next)
553 if (strcmp(g->filename, n) == 0)
556 avahi_log_info("Loading service file %s", n);
558 g = static_service_group_new(n);
559 if (static_service_group_load(g) < 0) {
560 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
561 static_service_group_free(g);
565 void static_service_load(void) {
566 StaticServiceGroup *g, *n;
570 for (g = groups; g; g = n) {
575 if (stat(g->filename, &st) < 0) {
578 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
580 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
582 static_service_group_free(g);
583 } else if (st.st_mtime != g->mtime) {
584 avahi_log_info("Service group file %s changed, reloading.", g->filename);
586 if (static_service_group_load(g) < 0) {
587 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
588 static_service_group_free(g);
593 memset(&globbuf, 0, sizeof(globbuf));
594 if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
595 avahi_log_error("Failed to read service directory.");
597 for (p = globbuf.gl_pathv; *p; p++)
604 void static_service_free_all(void) {
607 static_service_group_free(groups);
610 void static_service_add_to_server(void) {
611 StaticServiceGroup *g;
613 for (g = groups; g; g = g->groups_next)
614 add_static_service_group_to_server(g);
617 void static_service_remove_from_server(void) {
618 StaticServiceGroup *g;
620 for (g = groups; g; g = g->groups_next)
621 remove_static_service_group_from_server(g);