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
37 #include <avahi-core/llist.h>
38 #include <avahi-core/log.h>
42 typedef struct StaticService StaticService;
43 typedef struct StaticServiceGroup StaticServiceGroup;
45 struct StaticService {
46 StaticServiceGroup *group;
53 AvahiStringList *txt_records;
55 AVAHI_LLIST_FIELDS(StaticService, services);
58 struct StaticServiceGroup {
62 gchar *name, *chosen_name;
63 gboolean replace_wildcards;
65 AvahiEntryGroup *entry_group;
66 AVAHI_LLIST_HEAD(StaticService, services);
67 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
70 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
72 static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
73 gchar *r = NULL, *e, *n;
75 while ((e = strstr(pattern, a))) {
78 k = g_strndup(pattern, e - pattern);
80 n = g_strconcat(r, k, b, NULL);
82 n = g_strconcat(k, b, NULL);
88 pattern = e + strlen(a);
92 return g_strdup(pattern);
94 n = g_strconcat(r, pattern, NULL);
100 static void add_static_service_group_to_server(StaticServiceGroup *g);
101 static void remove_static_service_group_from_server(StaticServiceGroup *g);
103 static StaticService *static_service_new(StaticServiceGroup *group) {
107 s = g_new(StaticService, 1);
110 s->type = s->host_name = s->domain_name = NULL;
113 s->txt_records = NULL;
115 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
120 static StaticServiceGroup *static_service_group_new(gchar *filename) {
121 StaticServiceGroup *g;
124 g = g_new(StaticServiceGroup, 1);
125 g->filename = g_strdup(filename);
127 g->name = g->chosen_name = NULL;
128 g->replace_wildcards = FALSE;
129 g->entry_group = NULL;
131 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
132 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
137 static void static_service_free(StaticService *s) {
140 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
143 g_free(s->host_name);
144 g_free(s->domain_name);
146 avahi_string_list_free(s->txt_records);
151 static void static_service_group_free(StaticServiceGroup *g) {
154 remove_static_service_group_from_server(g);
157 static_service_free(g->services);
159 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
163 g_free(g->chosen_name);
167 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
168 StaticServiceGroup *g = userdata;
173 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
176 remove_static_service_group_from_server(g);
178 n = avahi_alternative_service_name(g->chosen_name);
179 g_free(g->chosen_name);
182 avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
184 add_static_service_group_to_server(g);
185 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
186 avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
189 static void add_static_service_group_to_server(StaticServiceGroup *g) {
198 g_free(g->chosen_name);
200 if (g->replace_wildcards)
201 g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
203 g->chosen_name = g_strdup(g->name);
205 g->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, g);
207 for (s = g->services; s; s = s->services_next) {
209 if (avahi_server_add_service_strlst(
213 s->type, g->chosen_name,
214 s->domain_name, s->host_name, s->port,
215 avahi_string_list_copy(s->txt_records)) < 0) {
216 avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
217 remove_static_service_group_from_server(g);
222 avahi_entry_group_commit(g->entry_group);
225 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
228 if (g->entry_group) {
229 avahi_entry_group_free(g->entry_group);
230 g->entry_group = NULL;
236 XML_TAG_SERVICE_GROUP,
246 struct xml_userdata {
247 StaticServiceGroup *group;
248 StaticService *service;
249 xml_tag_name current_tag;
254 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
255 struct xml_userdata *u = data;
262 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
267 u->current_tag = XML_TAG_SERVICE_GROUP;
268 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
269 u->current_tag = XML_TAG_NAME;
272 if (strcmp(attr[0], "replace-wildcards") == 0)
273 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
283 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
287 g_assert(!u->service);
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->port == 0 || !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;
366 g_assert(u->service);
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 = (guint16) p;
378 u->current_tag = XML_TAG_SERVICE;
382 case XML_TAG_TXT_RECORD: {
383 g_assert(u->service);
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 gchar *append_cdata(gchar *t, const gchar *n, int length) {
410 k = g_strndup(n, length);
413 r = g_strconcat(t, k, NULL);
422 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
423 struct xml_userdata *u = data;
429 switch (u->current_tag) {
431 u->group->name = append_cdata(u->group->name, s, len);
435 g_assert(u->service);
436 u->service->type = append_cdata(u->service->type, s, len);
439 case XML_TAG_DOMAIN_NAME:
440 g_assert(u->service);
441 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
444 case XML_TAG_HOST_NAME:
445 g_assert(u->service);
446 u->service->host_name = append_cdata(u->service->host_name, s, len);
450 case XML_TAG_TXT_RECORD:
451 u->buf = append_cdata(u->buf, s, len);
454 case XML_TAG_SERVICE_GROUP:
455 case XML_TAG_SERVICE:
456 case XML_TAG_INVALID:
461 static gint static_service_group_load(StaticServiceGroup *g) {
462 XML_Parser parser = NULL;
464 struct xml_userdata u;
474 u.current_tag = XML_TAG_INVALID;
477 /* Cleanup old data in this service group, if available */
478 remove_static_service_group_from_server(g);
480 static_service_free(g->services);
483 g_free(g->chosen_name);
484 g->name = g->chosen_name = NULL;
485 g->replace_wildcards = FALSE;
487 if (!(parser = XML_ParserCreate(NULL))) {
488 avahi_log_error("XML_ParserCreate() failed.");
492 if ((fd = open(g->filename, O_RDONLY)) < 0) {
493 avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
497 if (fstat(fd, &st) < 0) {
498 avahi_log_error("fstat(): %s", strerror(errno));
502 g->mtime = st.st_mtime;
504 XML_SetUserData(parser, &u);
506 XML_SetElementHandler(parser, xml_start, xml_end);
507 XML_SetCharacterDataHandler(parser, xml_cdata);
512 #define BUFSIZE (10*1024)
514 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
515 avahi_log_error("XML_GetBuffer() failed.");
519 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
520 avahi_log_error("read(): %s\n", strerror(errno));
524 if (!XML_ParseBuffer(parser, n, n == 0)) {
525 avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
540 XML_ParserFree(parser);
547 static void load_file(gchar *n) {
548 StaticServiceGroup *g;
551 for (g = groups; g; g = g->groups_next)
552 if (strcmp(g->filename, n) == 0)
555 avahi_log_info("Loading service file %s", n);
557 g = static_service_group_new(n);
558 if (static_service_group_load(g) < 0) {
559 avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
560 static_service_group_free(g);
564 void static_service_load(void) {
565 StaticServiceGroup *g, *n;
569 for (g = groups; g; g = n) {
574 if (stat(g->filename, &st) < 0) {
577 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
579 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
581 static_service_group_free(g);
582 } else if (st.st_mtime != g->mtime) {
583 avahi_log_info("Service group file %s changed, reloading.", g->filename);
585 if (static_service_group_load(g) < 0) {
586 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
587 static_service_group_free(g);
592 memset(&globbuf, 0, sizeof(globbuf));
593 if (glob(AVAHI_SERVICE_DIRECTORY "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
594 avahi_log_error("Failed to read service directory.");
596 for (p = globbuf.gl_pathv; *p; p++)
603 void static_service_free_all(void) {
606 static_service_group_free(groups);
609 void static_service_add_to_server(void) {
610 StaticServiceGroup *g;
612 for (g = groups; g; g = g->groups_next)
613 add_static_service_group_to_server(g);
616 void static_service_remove_from_server(void) {
617 StaticServiceGroup *g;
619 for (g = groups; g; g = g->groups_next)
620 remove_static_service_group_from_server(g);