]> git.meshlink.io Git - catta/blob - avahi-daemon/static-services.c
03824fde497b04511de758302a7ed47a3ee0bb07
[catta] / avahi-daemon / static-services.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/stat.h>
27 #include <glob.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <glib.h>
35 #include <expat.h>
36
37 #include <avahi-common/llist.h>
38 #include <avahi-core/log.h>
39
40 #include "main.h"
41 #include "static-services.h"
42
43 typedef struct StaticService StaticService;
44 typedef struct StaticServiceGroup StaticServiceGroup;
45
46 struct StaticService {
47     StaticServiceGroup *group;
48     
49     gchar *type;
50     gchar *domain_name;
51     gchar *host_name;
52     guint16 port;
53
54     AvahiStringList *txt_records;
55     
56     AVAHI_LLIST_FIELDS(StaticService, services);
57 };
58
59 struct StaticServiceGroup {
60     gchar *filename;
61     time_t mtime;
62
63     gchar *name, *chosen_name;
64     gboolean replace_wildcards;
65
66     AvahiSEntryGroup *entry_group;
67     AVAHI_LLIST_HEAD(StaticService, services);
68     AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
69 };
70
71 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
72
73 static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
74     gchar *r = NULL, *e, *n;
75
76     while ((e = strstr(pattern, a))) {
77         gchar *k;
78
79         k = g_strndup(pattern, e - pattern);
80         if (r)
81             n = g_strconcat(r, k, b, NULL);
82         else
83             n = g_strconcat(k, b, NULL);
84
85         g_free(k);
86         g_free(r);
87         r = n;
88
89         pattern = e + strlen(a);
90     }
91
92     if (!r)
93         return g_strdup(pattern);
94
95     n = g_strconcat(r, pattern, NULL);
96     g_free(r);
97
98     return n;
99 }
100
101 static void add_static_service_group_to_server(StaticServiceGroup *g);
102 static void remove_static_service_group_from_server(StaticServiceGroup *g);
103
104 static StaticService *static_service_new(StaticServiceGroup *group) {
105     StaticService *s;
106     
107     g_assert(group);
108     s = g_new(StaticService, 1);
109     s->group = group;
110
111     s->type = s->host_name = s->domain_name = NULL;
112     s->port = 0;
113
114     s->txt_records = NULL;
115
116     AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
117
118     return s;
119 }
120
121 static StaticServiceGroup *static_service_group_new(gchar *filename) {
122     StaticServiceGroup *g;
123     g_assert(filename);
124
125     g = g_new(StaticServiceGroup, 1);
126     g->filename = g_strdup(filename);
127     g->mtime = 0;
128     g->name = g->chosen_name = NULL;
129     g->replace_wildcards = FALSE;
130     g->entry_group = NULL;
131
132     AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
133     AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
134
135     return g;
136 }
137
138 static void static_service_free(StaticService *s) {
139     g_assert(s);
140     
141     AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
142
143     g_free(s->type);
144     g_free(s->host_name);
145     g_free(s->domain_name);
146
147     avahi_string_list_free(s->txt_records);
148     
149     g_free(s);
150 }
151
152 static void static_service_group_free(StaticServiceGroup *g) {
153     g_assert(g);
154
155     if (g->entry_group)
156         avahi_s_entry_group_free(g->entry_group);
157
158     while (g->services)
159         static_service_free(g->services);
160
161     AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
162
163     g_free(g->filename);
164     g_free(g->name);
165     g_free(g->chosen_name);
166     g_free(g);
167 }
168
169 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
170     StaticServiceGroup *g = userdata;
171     
172     g_assert(s);
173     g_assert(g);
174     
175     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
176         gchar *n;
177
178         remove_static_service_group_from_server(g);
179
180         n = avahi_alternative_service_name(g->chosen_name);
181         g_free(g->chosen_name);
182         g->chosen_name = n;
183
184         avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
185
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);
189 }
190
191 static void add_static_service_group_to_server(StaticServiceGroup *g) {
192     StaticService *s;
193
194     g_assert(g);
195     
196     if (g->chosen_name)
197         g_free(g->chosen_name);
198     
199     if (g->replace_wildcards)
200         g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
201     else
202         g->chosen_name = g_strdup(g->name);
203
204     if (!g->entry_group)
205         g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
206
207     g_assert(avahi_s_entry_group_is_empty(g->entry_group));
208     
209     for (s = g->services; s; s = s->services_next) {
210
211         if (avahi_server_add_service_strlst(
212                 avahi_server,
213                 g->entry_group,
214                 -1, AF_UNSPEC,
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);
222             return;
223         }
224     }
225
226     avahi_s_entry_group_commit(g->entry_group);
227 }
228
229 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
230     g_assert(g);
231
232     if (g->entry_group)
233         avahi_s_entry_group_reset(g->entry_group);
234 }
235
236 typedef enum {
237     XML_TAG_INVALID,
238     XML_TAG_SERVICE_GROUP,
239     XML_TAG_NAME,
240     XML_TAG_SERVICE,
241     XML_TAG_TYPE,
242     XML_TAG_DOMAIN_NAME,
243     XML_TAG_HOST_NAME,
244     XML_TAG_PORT,
245     XML_TAG_TXT_RECORD
246 } xml_tag_name;
247
248 struct xml_userdata {
249     StaticServiceGroup *group;
250     StaticService *service;
251     xml_tag_name current_tag;
252     gboolean failed;
253     gchar *buf;
254 };
255
256 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
257     struct xml_userdata *u = data;
258     
259     g_assert(u);
260
261     if (u->failed)
262         return;
263
264     if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
265
266         if (attr[0])
267             goto invalid_attr;
268
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;
272
273         if (attr[0]) {
274             if (strcmp(attr[0], "replace-wildcards") == 0) 
275                 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
276             else
277                 goto invalid_attr;
278         }
279
280         if (attr[2])
281             goto invalid_attr;
282         
283             
284         
285     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
286         if (attr[0])
287             goto invalid_attr;
288
289         g_assert(!u->service);
290         u->service = static_service_new(u->group);
291
292         u->current_tag = XML_TAG_SERVICE;
293     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
294         if (attr[0])
295             goto invalid_attr;
296
297         u->current_tag = XML_TAG_TYPE;
298     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
299         if (attr[0])
300             goto invalid_attr;
301         
302         u->current_tag = XML_TAG_DOMAIN_NAME;
303     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
304         if (attr[0])
305             goto invalid_attr;
306         
307         u->current_tag = XML_TAG_HOST_NAME;
308     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
309         if (attr[0])
310             goto invalid_attr;
311         
312         u->current_tag = XML_TAG_PORT;
313     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
314         if (attr[0])
315             goto invalid_attr;
316         
317         u->current_tag = XML_TAG_TXT_RECORD;
318     } else {
319         avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
320         u->failed = TRUE;
321     }
322
323     return;
324
325 invalid_attr:
326     avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
327     u->failed = TRUE;
328     return;
329 }
330     
331 static void XMLCALL xml_end(void *data, const char *el) {
332     struct xml_userdata *u = data;
333     g_assert(u);
334
335     if (u->failed)
336         return;
337     
338     switch (u->current_tag) {
339         case XML_TAG_SERVICE_GROUP:
340
341             if (!u->group->name || !u->group->services) {
342                 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
343                 u->failed = TRUE;
344                 return;
345             }
346             
347             u->current_tag = XML_TAG_INVALID;
348             break;
349
350         case XML_TAG_SERVICE:
351
352             if (!u->service->type) {
353                 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
354                 u->failed = TRUE;
355                 return;
356             }
357             
358             u->service = NULL;
359             u->current_tag = XML_TAG_SERVICE_GROUP;
360             break;
361
362         case XML_TAG_NAME:
363             u->current_tag = XML_TAG_SERVICE_GROUP;
364             break;
365
366         case XML_TAG_PORT: {
367             int p;
368             g_assert(u->service);
369             
370             p = u->buf ? atoi(u->buf) : 0;
371
372             if (p < 0 || p > 0xFFFF) {
373                 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
374                 u->failed = TRUE;
375                 return;
376             }
377
378             u->service->port = (guint16) p;
379
380             u->current_tag = XML_TAG_SERVICE;
381             break;
382         }
383
384         case XML_TAG_TXT_RECORD: {
385             g_assert(u->service);
386             
387             u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
388             u->current_tag = XML_TAG_SERVICE;
389             break;
390         }
391             
392         case XML_TAG_TYPE:
393         case XML_TAG_DOMAIN_NAME:
394         case XML_TAG_HOST_NAME:
395             u->current_tag = XML_TAG_SERVICE;
396             break;
397
398         case XML_TAG_INVALID:
399             ;
400     }
401
402     g_free(u->buf);
403     u->buf = NULL;
404 }
405
406 static gchar *append_cdata(gchar *t, const gchar *n, int length) {
407     gchar *r, *k;
408     
409     if (!length)
410         return t;
411
412     k = g_strndup(n, length);
413
414     if (t) {
415         r = g_strconcat(t, k, NULL);
416         g_free(k);
417         g_free(t);
418     } else
419         r = k;
420     
421     return r;
422 }
423
424 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
425     struct xml_userdata *u = data;
426     g_assert(u);
427
428     if (u->failed)
429         return;
430
431     switch (u->current_tag) {
432         case XML_TAG_NAME:
433             u->group->name = append_cdata(u->group->name, s, len);
434             break;
435             
436         case XML_TAG_TYPE:
437             g_assert(u->service);
438             u->service->type = append_cdata(u->service->type, s, len);
439             break;
440
441         case XML_TAG_DOMAIN_NAME:
442             g_assert(u->service);
443             u->service->domain_name = append_cdata(u->service->domain_name, s, len);
444             break;
445
446         case XML_TAG_HOST_NAME:
447             g_assert(u->service);
448             u->service->host_name = append_cdata(u->service->host_name, s, len);
449             break;
450
451         case XML_TAG_PORT:
452         case XML_TAG_TXT_RECORD:
453             u->buf = append_cdata(u->buf, s, len);
454             break;
455
456         case XML_TAG_SERVICE_GROUP:
457         case XML_TAG_SERVICE:
458         case XML_TAG_INVALID:
459             ;
460     }
461 }
462
463 static gint static_service_group_load(StaticServiceGroup *g) {
464     XML_Parser parser = NULL;
465     gint fd = -1;
466     struct xml_userdata u;
467     gint r = -1;
468     struct stat st;
469     ssize_t n;
470
471     g_assert(g);
472
473     u.buf = NULL;
474     u.group = g;
475     u.service = NULL;
476     u.current_tag = XML_TAG_INVALID;
477     u.failed = FALSE;
478
479     /* Cleanup old data in this service group, if available */
480     remove_static_service_group_from_server(g);
481     while (g->services)
482         static_service_free(g->services);
483
484     g_free(g->name);
485     g_free(g->chosen_name);
486     g->name = g->chosen_name = NULL;
487     g->replace_wildcards = FALSE;
488     
489     if (!(parser = XML_ParserCreate(NULL))) {
490         avahi_log_error("XML_ParserCreate() failed.");
491         goto finish;
492     }
493
494     if ((fd = open(g->filename, O_RDONLY)) < 0) {
495         avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
496         goto finish;
497     }
498
499     if (fstat(fd, &st) < 0) {
500         avahi_log_error("fstat(): %s", strerror(errno));
501         goto finish;
502     }
503
504     g->mtime = st.st_mtime;
505     
506     XML_SetUserData(parser, &u);
507
508     XML_SetElementHandler(parser, xml_start, xml_end);
509     XML_SetCharacterDataHandler(parser, xml_cdata);
510
511     do {
512         void *buffer;
513
514 #define BUFSIZE (10*1024)
515
516         if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
517             avahi_log_error("XML_GetBuffer() failed.");
518             goto finish;
519         }
520
521         if ((n = read(fd, buffer, BUFSIZE)) < 0) {
522             avahi_log_error("read(): %s\n", strerror(errno));
523             goto finish;
524         }
525
526         if (!XML_ParseBuffer(parser, n, n == 0)) {
527             avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
528             goto finish;
529         }
530
531     } while (n != 0);
532
533     if (!u.failed)
534         r = 0;
535
536 finish:
537
538     if (fd >= 0)
539         close(fd);
540
541     if (parser)
542         XML_ParserFree(parser);
543
544     g_free(u.buf);
545
546     return r;
547 }
548
549 static void load_file(gchar *n) {
550     StaticServiceGroup *g;
551     g_assert(n);
552
553     for (g = groups; g; g = g->groups_next)
554         if (strcmp(g->filename, n) == 0)
555             return;
556
557     avahi_log_info("Loading service file %s", n);
558     
559     g = static_service_group_new(n);
560     if (static_service_group_load(g) < 0) {
561         avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
562         static_service_group_free(g);
563     }
564 }
565
566 void static_service_load(void) {
567     StaticServiceGroup *g, *n;
568     glob_t globbuf;
569     gchar **p;
570
571     for (g = groups; g; g = n) {
572         struct stat st;
573
574         n = g->groups_next;
575
576         if (stat(g->filename, &st) < 0) {
577
578             if (errno == ENOENT)
579                 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
580             else
581                 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
582             
583             static_service_group_free(g);
584         } else if (st.st_mtime != g->mtime) {
585             avahi_log_info("Service group file %s changed, reloading.", g->filename);
586             
587             if (static_service_group_load(g) < 0) {
588                 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
589                 static_service_group_free(g);
590             }
591         }
592     }
593
594     memset(&globbuf, 0, sizeof(globbuf));
595     if (glob(AVAHI_SERVICE_DIR "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
596         avahi_log_error("Failed to read service directory.");
597     else {
598         for (p = globbuf.gl_pathv; *p; p++)
599             load_file(*p);
600         
601         globfree(&globbuf);
602     }
603 }
604
605 void static_service_free_all(void) {
606     
607     while (groups)
608         static_service_group_free(groups);
609 }
610
611 void static_service_add_to_server(void) {
612     StaticServiceGroup *g;
613
614     for (g = groups; g; g = g->groups_next)
615         add_static_service_group_to_server(g);
616 }
617
618 void static_service_remove_from_server(void) {
619     StaticServiceGroup *g;
620
621     for (g = groups; g; g = g->groups_next)
622         remove_static_service_group_from_server(g);
623 }