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>
41 typedef struct StaticService StaticService;
42 typedef struct StaticServiceGroup StaticServiceGroup;
44 struct StaticService {
45 StaticServiceGroup *group;
52 AvahiStringList *txt_records;
54 AVAHI_LLIST_FIELDS(StaticService, services);
57 struct StaticServiceGroup {
61 gchar *name, *chosen_name;
62 gboolean replace_wildcards;
64 AvahiEntryGroup *entry_group;
65 AVAHI_LLIST_HEAD(StaticService, services);
66 AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
69 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
71 static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
72 gchar *r = NULL, *e, *n;
74 while ((e = strstr(pattern, a))) {
77 k = g_strndup(pattern, e - pattern);
79 n = g_strconcat(r, k, b, NULL);
81 n = g_strconcat(k, b, NULL);
87 pattern = e + strlen(a);
91 return g_strdup(pattern);
93 n = g_strconcat(r, pattern, NULL);
99 static void add_static_service_group_to_server(StaticServiceGroup *g);
100 static void remove_static_service_group_from_server(StaticServiceGroup *g);
102 static StaticService *static_service_new(StaticServiceGroup *group) {
106 s = g_new(StaticService, 1);
109 s->type = s->host_name = s->domain_name = NULL;
112 s->txt_records = NULL;
114 AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
119 static StaticServiceGroup *static_service_group_new(gchar *filename) {
120 StaticServiceGroup *g;
123 g = g_new(StaticServiceGroup, 1);
124 g->filename = g_strdup(filename);
126 g->name = g->chosen_name = NULL;
127 g->replace_wildcards = FALSE;
128 g->entry_group = NULL;
130 AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
131 AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
136 static void static_service_free(StaticService *s) {
139 AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
142 g_free(s->host_name);
143 g_free(s->domain_name);
145 avahi_string_list_free(s->txt_records);
150 static void static_service_group_free(StaticServiceGroup *g) {
153 remove_static_service_group_from_server(g);
156 static_service_free(g->services);
158 AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
162 g_free(g->chosen_name);
166 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
167 StaticServiceGroup *g = userdata;
172 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
175 remove_static_service_group_from_server(g);
177 n = avahi_alternative_service_name(g->chosen_name);
178 g_free(g->chosen_name);
181 g_message("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
183 add_static_service_group_to_server(g);
185 g_message("Service \"%s\" (%s) successfully establised.", g->chosen_name, g->filename);
188 static void add_static_service_group_to_server(StaticServiceGroup *g) {
197 g_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 = g_strdup(g->name);
204 g->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, g);
206 for (s = g->services; s; s = s->services_next) {
208 if (avahi_server_add_service_strlst(
212 s->type, g->chosen_name,
213 s->domain_name, s->host_name, s->port,
214 avahi_string_list_copy(s->txt_records)) < 0) {
215 g_message("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
216 remove_static_service_group_from_server(g);
221 avahi_entry_group_commit(g->entry_group);
224 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
227 if (g->entry_group) {
228 avahi_entry_group_free(g->entry_group);
229 g->entry_group = NULL;
235 XML_TAG_SERVICE_GROUP,
245 struct xml_userdata {
246 StaticServiceGroup *group;
247 StaticService *service;
248 xml_tag_name current_tag;
253 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
254 struct xml_userdata *u = data;
261 if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
266 u->current_tag = XML_TAG_SERVICE_GROUP;
267 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
268 u->current_tag = XML_TAG_NAME;
271 if (strcmp(attr[0], "replace-wildcards") == 0)
272 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
282 } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
286 g_assert(!u->service);
287 u->service = static_service_new(u->group);
289 u->current_tag = XML_TAG_SERVICE;
290 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
294 u->current_tag = XML_TAG_TYPE;
295 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
299 u->current_tag = XML_TAG_DOMAIN_NAME;
300 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
304 u->current_tag = XML_TAG_HOST_NAME;
305 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
309 u->current_tag = XML_TAG_PORT;
310 } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
314 u->current_tag = XML_TAG_TXT_RECORD;
316 g_message("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
323 g_message("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
328 static void XMLCALL xml_end(void *data, const char *el) {
329 struct xml_userdata *u = data;
335 switch (u->current_tag) {
336 case XML_TAG_SERVICE_GROUP:
338 if (!u->group->name || !u->group->services) {
339 g_message("%s: parse failure: service group incomplete.", u->group->filename);
344 u->current_tag = XML_TAG_INVALID;
347 case XML_TAG_SERVICE:
349 if (u->service->port == 0 || !u->service->type) {
350 g_message("%s: parse failure: service incomplete.", u->group->filename);
356 u->current_tag = XML_TAG_SERVICE_GROUP;
360 u->current_tag = XML_TAG_SERVICE_GROUP;
365 g_assert(u->service);
367 p = u->buf ? atoi(u->buf) : 0;
369 if (p <= 0 || p > 0xFFFF) {
370 g_message("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
375 u->service->port = (guint16) p;
377 u->current_tag = XML_TAG_SERVICE;
381 case XML_TAG_TXT_RECORD: {
382 g_assert(u->service);
384 u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
385 u->current_tag = XML_TAG_SERVICE;
390 case XML_TAG_DOMAIN_NAME:
391 case XML_TAG_HOST_NAME:
392 u->current_tag = XML_TAG_SERVICE;
395 case XML_TAG_INVALID:
403 static gchar *append_cdata(gchar *t, const gchar *n, int length) {
409 k = g_strndup(n, length);
412 r = g_strconcat(t, k, NULL);
421 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
422 struct xml_userdata *u = data;
428 switch (u->current_tag) {
430 u->group->name = append_cdata(u->group->name, s, len);
434 g_assert(u->service);
435 u->service->type = append_cdata(u->service->type, s, len);
438 case XML_TAG_DOMAIN_NAME:
439 g_assert(u->service);
440 u->service->domain_name = append_cdata(u->service->domain_name, s, len);
443 case XML_TAG_HOST_NAME:
444 g_assert(u->service);
445 u->service->host_name = append_cdata(u->service->host_name, s, len);
449 case XML_TAG_TXT_RECORD:
450 u->buf = append_cdata(u->buf, s, len);
453 case XML_TAG_SERVICE_GROUP:
454 case XML_TAG_SERVICE:
455 case XML_TAG_INVALID:
460 static gint static_service_group_load(StaticServiceGroup *g) {
461 XML_Parser parser = NULL;
463 struct xml_userdata u;
473 u.current_tag = XML_TAG_INVALID;
476 /* Cleanup old data in this service group, if available */
477 remove_static_service_group_from_server(g);
479 static_service_free(g->services);
482 g_free(g->chosen_name);
483 g->name = g->chosen_name = NULL;
484 g->replace_wildcards = FALSE;
486 if (!(parser = XML_ParserCreate(NULL))) {
487 g_warning("XML_ParserCreate() failed.");
491 if ((fd = open(g->filename, O_RDONLY)) < 0) {
492 g_warning("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
496 if (fstat(fd, &st) < 0) {
497 g_warning("fstat(): %s", strerror(errno));
501 g->mtime = st.st_mtime;
503 XML_SetUserData(parser, &u);
505 XML_SetElementHandler(parser, xml_start, xml_end);
506 XML_SetCharacterDataHandler(parser, xml_cdata);
511 #define BUFSIZE (10*1024)
513 if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
514 g_warning("XML_GetBuffer() failed.");
518 if ((n = read(fd, buffer, BUFSIZE)) < 0) {
519 g_warning("read(): %s\n", strerror(errno));
523 if (!XML_ParseBuffer(parser, n, n == 0)) {
524 g_warning("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
539 XML_ParserFree(parser);
546 static void load_file(gchar *n) {
547 StaticServiceGroup *g;
550 for (g = groups; g; g = g->groups_next)
551 if (strcmp(g->filename, n) == 0)
554 g = static_service_group_new(n);
555 if (static_service_group_load(g) < 0) {
556 g_warning("Failed to load service group file %s, ignoring.", g->filename);
557 static_service_group_free(g);
561 void static_service_load(void) {
562 StaticServiceGroup *g, *n;
566 for (g = groups; g; g = n) {
571 if (stat(g->filename, &st) < 0) {
574 g_message("Service group file %s vanished, removing seervices.", g->filename);
576 g_warning("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
578 static_service_group_free(g);
579 } else if (st.st_mtime != g->mtime) {
580 g_message("Service group file %s changed, reloading.", g->filename);
582 if (static_service_group_load(g) < 0) {
583 g_warning("Failed to load service group file %s, removing service.", g->filename);
584 static_service_group_free(g);
589 memset(&globbuf, 0, sizeof(globbuf));
590 if (glob(AVAHI_SERVICE_DIRECTORY "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
591 g_warning("glob() failed.\n");
593 for (p = globbuf.gl_pathv; *p; p++)
600 void static_service_free_all(void) {
603 static_service_group_free(groups);
606 void static_service_add_to_server(void) {
607 StaticServiceGroup *g;
609 for (g = groups; g; g = g->groups_next)
610 add_static_service_group_to_server(g);
613 void static_service_remove_from_server(void) {
614 StaticServiceGroup *g;
616 for (g = groups; g; g = g->groups_next)
617 remove_static_service_group_from_server(g);