]> git.meshlink.io Git - catta/blob - libavahi-core/server.c
autotoolize
[catta] / libavahi-core / server.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 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <string.h>
25 #include <sys/utsname.h>
26 #include <unistd.h>
27
28 #include "server.h"
29 #include "util.h"
30 #include "iface.h"
31 #include "socket.h"
32 #include "subscribe.h"
33
34 static void free_entry(AvahiServer*s, AvahiEntry *e) {
35     AvahiEntry *t;
36
37     g_assert(s);
38     g_assert(e);
39
40     avahi_goodbye_entry(s, e, TRUE);
41
42     /* Remove from linked list */
43     AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
44
45     /* Remove from hash table indexed by name */
46     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
47     AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
48     if (t)
49         g_hash_table_replace(s->entries_by_key, t->record->key, t);
50     else
51         g_hash_table_remove(s->entries_by_key, e->record->key);
52
53     /* Remove from associated group */
54     if (e->group)
55         AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
56
57     avahi_record_unref(e->record);
58     g_free(e);
59 }
60
61 static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
62     g_assert(s);
63     g_assert(g);
64
65     while (g->entries)
66         free_entry(s, g->entries);
67
68     AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
69     g_free(g);
70 }
71
72 static void cleanup_dead(AvahiServer *s) {
73     AvahiEntryGroup *g, *ng;
74     AvahiEntry *e, *ne;
75     g_assert(s);
76
77
78     if (s->need_group_cleanup) {
79         for (g = s->groups; g; g = ng) {
80             ng = g->groups_next;
81             
82             if (g->dead)
83                 free_group(s, g);
84         }
85
86         s->need_group_cleanup = FALSE;
87     }
88
89     if (s->need_entry_cleanup) {
90         for (e = s->entries; e; e = ne) {
91             ne = e->entries_next;
92             
93             if (e->dead)
94                 free_entry(s, e);
95         }
96
97         s->need_entry_cleanup = FALSE;
98     }
99 }
100
101 static void handle_query_key(AvahiServer *s, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) {
102     AvahiEntry *e;
103     gchar *txt;
104     
105     g_assert(s);
106     g_assert(k);
107     g_assert(i);
108     g_assert(a);
109
110     g_message("Handling query: %s", txt = avahi_key_to_string(k));
111     g_free(txt);
112
113     avahi_packet_scheduler_incoming_query(i->scheduler, k);
114
115     if (k->type == AVAHI_DNS_TYPE_ANY) {
116
117         /* Handle ANY query */
118         
119         for (e = s->entries; e; e = e->entries_next)
120             if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i))
121                 avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE);
122     } else {
123
124         /* Handle all other queries */
125         
126         for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
127             if (!e->dead && avahi_entry_registered(s, e, i))
128                 avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE);
129     }
130 }
131
132 static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
133     g_assert(s);
134     g_assert(e);
135
136     
137     if (e->group) {
138         AvahiEntry *k;
139         
140         for (k = e->group->entries; k; k = k->by_group_next) {
141             avahi_goodbye_entry(s, k, FALSE);
142             k->dead = TRUE;
143         }
144         
145         avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
146     } else {
147         avahi_goodbye_entry(s, e, FALSE);
148         e->dead = TRUE;
149     }
150
151     s->need_entry_cleanup = TRUE;
152 }
153
154 static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
155     AvahiEntry *e, *n;
156     gchar *t;
157     
158     g_assert(s);
159     g_assert(record);
160     g_assert(i);
161
162     t = avahi_record_to_string(record);
163
164 /*     g_message("PROBE: [%s]", t); */
165     
166     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
167         n = e->by_key_next;
168
169         if (e->dead || avahi_record_equal_no_ttl(record, e->record))
170             continue;
171
172         if (avahi_entry_registering(s, e, i)) {
173             gint cmp;
174
175             if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
176                 withdraw_entry(s, e);
177                 g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
178             } else if (cmp < 0)
179                 g_message("Recieved conflicting probe [%s]. Local host won.", t);
180
181         }
182     }
183
184     g_free(t);
185 }
186
187 static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
188     guint n;
189     
190     g_assert(s);
191     g_assert(p);
192     g_assert(i);
193     g_assert(a);
194
195     /* Handle the questions */
196     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
197         AvahiKey *key;
198         gboolean unicast_response = FALSE;
199
200         if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
201             g_warning("Packet too short (1)");
202             return;
203         }
204
205         handle_query_key(s, key, i, a, port, legacy_unicast, unicast_response);
206         avahi_key_unref(key);
207     }
208
209     /* Known Answer Suppression */
210     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
211         AvahiRecord *record;
212         gboolean unique = FALSE;
213
214         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
215             g_warning("Packet too short (2)");
216             return;
217         }
218
219         avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
220         avahi_record_unref(record);
221     }
222
223     /* Probe record */
224     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
225         AvahiRecord *record;
226         gboolean unique = FALSE;
227
228         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
229             g_warning("Packet too short (3)");
230             return;
231         }
232
233         if (record->key->type != AVAHI_DNS_TYPE_ANY)
234             incoming_probe(s, record, i);
235         
236         avahi_record_unref(record);
237     }
238 }
239
240 static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
241     gboolean valid = TRUE;
242     AvahiEntry *e, *n;
243     gchar *t;
244     
245     g_assert(s);
246     g_assert(i);
247     g_assert(record);
248
249     t = avahi_record_to_string(record);
250
251 /*     g_message("CHECKING FOR CONFLICT: [%s]", t); */
252
253     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
254         n = e->by_key_next;
255
256         if (e->dead)
257             continue;
258         
259         if (avahi_entry_registered(s, e, i)) {
260
261             gboolean equal = avahi_record_equal_no_ttl(record, e->record);
262                 
263             /* Check whether there is a unique record conflict */
264             if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
265                 gint cmp;
266                 
267                 /* The lexicographically later data wins. */
268                 if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
269                     g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t);
270                     withdraw_entry(s, e);
271                 } else if (cmp < 0) {
272                     /* Tell the other host that our entry is lexicographically later */
273
274                     g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t);
275
276                     valid = FALSE;
277                     avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
278                 }
279                 
280                 /* Check wheter there is a TTL conflict */
281             } else if (equal && record->ttl <= e->record->ttl/2) {
282                 /* Correct the TTL */
283                 valid = FALSE;
284                 avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
285                 g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
286             }
287             
288         } else if (avahi_entry_registering(s, e, i)) {
289
290             if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
291
292                 /* We are currently registering a matching record, but
293                  * someone else already claimed it, so let's
294                  * withdraw */
295                 
296                 g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
297                 withdraw_entry(s, e);
298             }
299         }
300     }
301
302     g_free(t);
303
304     return valid;
305 }
306
307 static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
308     guint n;
309     
310     g_assert(s);
311     g_assert(p);
312     g_assert(i);
313     g_assert(a);
314     
315     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
316              avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
317         AvahiRecord *record;
318         gboolean cache_flush = FALSE;
319         gchar *txt;
320         
321         if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
322             g_warning("Packet too short (4)");
323             return;
324         }
325
326         if (record->key->type != AVAHI_DNS_TYPE_ANY) {
327
328             g_message("Handling response: %s", txt = avahi_record_to_string(record));
329             g_free(txt);
330             
331             if (handle_conflict(s, i, record, cache_flush, a)) {
332                 avahi_cache_update(i->cache, record, cache_flush, a);
333                 avahi_packet_scheduler_incoming_response(i->scheduler, record);
334             }
335         }
336             
337         avahi_record_unref(record);
338     }
339 }
340
341 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
342     AvahiInterface *i;
343     AvahiAddress a;
344     guint16 port;
345     
346     g_assert(s);
347     g_assert(p);
348     g_assert(sa);
349     g_assert(iface > 0);
350
351     if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
352         g_warning("Recieved packet from invalid interface.");
353         return;
354     }
355
356     g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol);
357
358     if (sa->sa_family == AF_INET6) {
359         static const guint8 ipv4_in_ipv6[] = {
360             0x00, 0x00, 0x00, 0x00,
361             0x00, 0x00, 0x00, 0x00,
362             0xFF, 0xFF, 0xFF, 0xFF };
363
364         /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
365
366         if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
367             return;
368     }
369
370     if (avahi_dns_packet_check_valid(p) < 0) {
371         g_warning("Recieved invalid packet.");
372         return;
373     }
374
375     port = avahi_port_from_sockaddr(sa);
376     avahi_address_from_sockaddr(sa, &a);
377
378     if (avahi_dns_packet_is_query(p)) {
379         gboolean legacy_unicast = FALSE;
380
381         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) == 0 ||
382             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
383             g_warning("Invalid query packet.");
384             return;
385         }
386
387         if (port != AVAHI_MDNS_PORT) {
388             /* Legacy Unicast */
389
390             if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
391                  avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
392                 g_warning("Invalid legacy unicast query packet.");
393                 return;
394             }
395         
396             legacy_unicast = TRUE;
397         }
398
399         handle_query(s, p, i, &a, port, legacy_unicast);
400         
401         g_message("Handled query");
402     } else {
403
404         if (port != AVAHI_MDNS_PORT) {
405             g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
406             return;
407         }
408
409         if (ttl != 255) {
410             g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
411             if (!s->ignore_bad_ttl)
412                 return;
413         }
414
415         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
416             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
417             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
418             g_warning("Invalid response packet.");
419             return;
420         }
421
422         handle_response(s, p, i, &a);
423         g_message("Handled response");
424     }
425 }
426
427 static void work(AvahiServer *s) {
428     struct sockaddr_in6 sa6;
429     struct sockaddr_in sa;
430     AvahiDnsPacket *p;
431     gint iface = -1;
432     guint8 ttl;
433         
434     g_assert(s);
435
436     if (s->pollfd_ipv4.revents & G_IO_IN) {
437         if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
438             dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
439             avahi_dns_packet_free(p);
440         }
441     }
442
443     if (s->pollfd_ipv6.revents & G_IO_IN) {
444         if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
445             dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
446             avahi_dns_packet_free(p);
447         }
448     }
449 }
450
451 static gboolean prepare_func(GSource *source, gint *timeout) {
452     g_assert(source);
453     g_assert(timeout);
454     
455     *timeout = -1;
456     return FALSE;
457 }
458
459 static gboolean check_func(GSource *source) {
460     AvahiServer* s;
461     g_assert(source);
462
463     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
464     g_assert(s);
465     
466     return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
467 }
468
469 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
470     AvahiServer* s;
471     g_assert(source);
472
473     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
474     g_assert(s);
475
476     work(s);
477     cleanup_dead(s);
478
479     return TRUE;
480 }
481
482 static void add_default_entries(AvahiServer *s) {
483     struct utsname utsname;
484     AvahiAddress a;
485     AvahiRecord *r;
486     
487     g_assert(s);
488     
489     /* Fill in HINFO rr */
490     r = avahi_record_new_full(s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
491     uname(&utsname);
492     r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
493     r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
494     avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
495     avahi_record_unref(r);
496
497     /* Add localhost entries */
498     avahi_address_parse("127.0.0.1", AF_INET, &a);
499     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
500
501     avahi_address_parse("::1", AF_INET6, &a);
502     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
503 }
504
505 AvahiServer *avahi_server_new(GMainContext *c) {
506     gchar *hn;
507     AvahiServer *s;
508     
509     static GSourceFuncs source_funcs = {
510         prepare_func,
511         check_func,
512         dispatch_func,
513         NULL,
514         NULL,
515         NULL
516     };
517
518     s = g_new(AvahiServer, 1);
519
520     s->ignore_bad_ttl = FALSE;
521     s->need_entry_cleanup = s->need_group_cleanup = FALSE;
522     
523     s->fd_ipv4 = avahi_open_socket_ipv4();
524     s->fd_ipv6 = avahi_open_socket_ipv6();
525     
526     if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
527         g_critical("Failed to create IP sockets.\n");
528         g_free(s);
529         return NULL;
530     }
531
532     if (s->fd_ipv4 < 0)
533         g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
534     else if (s->fd_ipv6 < 0)
535         g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
536     
537     if (c)
538         g_main_context_ref(s->context = c);
539     else
540         s->context = g_main_context_default();
541     
542     AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
543     s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
544     AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
545
546     AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions);
547     s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
548
549     /* Get host name */
550     hn = avahi_get_host_name();
551     hn[strcspn(hn, ".")] = 0;
552
553     s->hostname = g_strdup_printf("%s.local.", hn);
554     g_free(hn);
555
556     s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
557     s->monitor = avahi_interface_monitor_new(s);
558     avahi_interface_monitor_sync(s->monitor);
559     add_default_entries(s);
560     
561     /* Prepare IO source registration */
562     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
563     *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
564
565     memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
566     s->pollfd_ipv4.fd = s->fd_ipv4;
567     s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
568     g_source_add_poll(s->source, &s->pollfd_ipv4);
569     
570     memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
571     s->pollfd_ipv6.fd = s->fd_ipv6;
572     s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
573     g_source_add_poll(s->source, &s->pollfd_ipv6);
574
575     g_source_attach(s->source, s->context);
576
577     return s;
578 }
579
580 void avahi_server_free(AvahiServer* s) {
581     g_assert(s);
582
583     while(s->entries)
584         free_entry(s, s->entries);
585
586     avahi_interface_monitor_free(s->monitor);
587
588     while (s->groups)
589         free_group(s, s->groups);
590
591     while (s->subscriptions)
592         avahi_subscription_free(s->subscriptions);
593     g_hash_table_destroy(s->subscription_hashtable);
594
595     g_hash_table_destroy(s->entries_by_key);
596
597     avahi_time_event_queue_free(s->time_event_queue);
598
599     if (s->fd_ipv4 >= 0)
600         close(s->fd_ipv4);
601     if (s->fd_ipv6 >= 0)
602         close(s->fd_ipv6);
603     
604     g_free(s->hostname);
605
606     g_source_destroy(s->source);
607     g_source_unref(s->source);
608     g_main_context_unref(s->context);
609
610     g_free(s);
611 }
612
613 void avahi_server_add(
614     AvahiServer *s,
615     AvahiEntryGroup *g,
616     gint interface,
617     guchar protocol,
618     AvahiEntryFlags flags,
619     AvahiRecord *r) {
620     
621     AvahiEntry *e, *t;
622     g_assert(s);
623     g_assert(r);
624
625     g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
626
627     e = g_new(AvahiEntry, 1);
628     e->server = s;
629     e->record = avahi_record_ref(r);
630     e->group = g;
631     e->interface = interface;
632     e->protocol = protocol;
633     e->flags = flags;
634     e->dead = FALSE;
635
636     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
637
638     AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
639
640     /* Insert into hash table indexed by name */
641     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
642     AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
643     g_hash_table_replace(s->entries_by_key, e->record->key, t);
644
645     /* Insert into group list */
646     if (g)
647         AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); 
648
649     avahi_announce_entry(s, e);
650 }
651 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
652     AvahiEntry **e = (AvahiEntry**) state;
653     g_assert(s);
654     g_assert(e);
655
656     if (!*e)
657         *e = g ? g->entries : s->entries;
658     
659     while (*e && (*e)->dead)
660         *e = g ? (*e)->by_group_next : (*e)->entries_next;
661         
662     if (!*e)
663         return NULL;
664
665     return avahi_record_ref((*e)->record);
666 }
667
668 void avahi_server_dump(AvahiServer *s, FILE *f) {
669     AvahiEntry *e;
670     g_assert(s);
671     g_assert(f);
672
673     fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
674
675     for (e = s->entries; e; e = e->entries_next) {
676         gchar *t;
677
678         if (e->dead)
679             continue;
680         
681         t = avahi_record_to_string(e->record);
682         fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
683         g_free(t);
684     }
685
686     avahi_dump_caches(s->monitor, f);
687 }
688
689 void avahi_server_add_ptr(
690     AvahiServer *s,
691     AvahiEntryGroup *g,
692     gint interface,
693     guchar protocol,
694     AvahiEntryFlags flags,
695     const gchar *name,
696     const gchar *dest) {
697
698     AvahiRecord *r;
699
700     g_assert(dest);
701
702     r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
703     r->data.ptr.name = avahi_normalize_name(dest);
704     avahi_server_add(s, g, interface, protocol, flags, r);
705     avahi_record_unref(r);
706 }
707
708 void avahi_server_add_address(
709     AvahiServer *s,
710     AvahiEntryGroup *g,
711     gint interface,
712     guchar protocol,
713     AvahiEntryFlags flags,
714     const gchar *name,
715     AvahiAddress *a) {
716
717     gchar *n = NULL;
718     g_assert(s);
719     g_assert(a);
720
721     name = name ? (n = avahi_normalize_name(name)) : s->hostname;
722     
723     if (a->family == AF_INET) {
724         gchar *reverse;
725         AvahiRecord  *r;
726
727         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
728         r->data.a.address = a->data.ipv4;
729         avahi_server_add(s, g, interface, protocol, flags, r);
730         avahi_record_unref(r);
731         
732         reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
733         g_assert(reverse);
734         avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
735         g_free(reverse);
736         
737     } else {
738         gchar *reverse;
739         AvahiRecord *r;
740             
741         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
742         r->data.aaaa.address = a->data.ipv6;
743         avahi_server_add(s, g, interface, protocol, flags, r);
744         avahi_record_unref(r);
745
746         reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
747         g_assert(reverse);
748         avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
749         g_free(reverse);
750     
751         reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
752         g_assert(reverse);
753         avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
754         g_free(reverse);
755     }
756     
757     g_free(n);
758 }
759
760 void avahi_server_add_text_strlst(
761     AvahiServer *s,
762     AvahiEntryGroup *g,
763     gint interface,
764     guchar protocol,
765     AvahiEntryFlags flags,
766     const gchar *name,
767     AvahiStringList *strlst) {
768
769     AvahiRecord *r;
770     
771     g_assert(s);
772     
773     r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
774     r->data.txt.string_list = strlst;
775     avahi_server_add(s, g, interface, protocol, flags, r);
776     avahi_record_unref(r);
777 }
778
779 void avahi_server_add_text_va(
780     AvahiServer *s,
781     AvahiEntryGroup *g,
782     gint interface,
783     guchar protocol,
784     AvahiEntryFlags flags,
785     const gchar *name,
786     va_list va) {
787     
788     g_assert(s);
789
790     avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
791 }
792
793 void avahi_server_add_text(
794     AvahiServer *s,
795     AvahiEntryGroup *g,
796     gint interface,
797     guchar protocol,
798     AvahiEntryFlags flags,
799     const gchar *name,
800     ...) {
801
802     va_list va;
803     
804     g_assert(s);
805
806     va_start(va, name);
807     avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
808     va_end(va);
809 }
810
811 static void escape_service_name(gchar *d, guint size, const gchar *s) {
812     g_assert(d);
813     g_assert(size);
814     g_assert(s);
815
816     while (*s && size >= 2) {
817         if (*s == '.' || *s == '\\') {
818             if (size < 3)
819                 break;
820
821             *(d++) = '\\';
822             size--;
823         }
824             
825         *(d++) = *(s++);
826         size--;
827     }
828
829     g_assert(size > 0);
830     *(d++) = 0;
831 }
832
833 void avahi_server_add_service_strlst(
834     AvahiServer *s,
835     AvahiEntryGroup *g,
836     gint interface,
837     guchar protocol,
838     const gchar *type,
839     const gchar *name,
840     const gchar *domain,
841     const gchar *host,
842     guint16 port,
843     AvahiStringList *strlst) {
844
845     gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
846     AvahiRecord *r;
847     
848     g_assert(s);
849     g_assert(type);
850     g_assert(name);
851
852     escape_service_name(ename, sizeof(ename), name);
853
854     if (domain) {
855         while (domain[0] == '.')
856             domain++;
857     } else
858         domain = "local";
859
860     if (!host)
861         host = s->hostname;
862
863     snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
864     snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
865     
866     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
867
868     r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
869     r->data.srv.priority = 0;
870     r->data.srv.weight = 0;
871     r->data.srv.port = port;
872     r->data.srv.name = avahi_normalize_name(host);
873     avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
874     avahi_record_unref(r);
875
876     avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
877
878     snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
879     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
880 }
881
882 void avahi_server_add_service_va(
883     AvahiServer *s,
884     AvahiEntryGroup *g,
885     gint interface,
886     guchar protocol,
887     const gchar *type,
888     const gchar *name,
889     const gchar *domain,
890     const gchar *host,
891     guint16 port,
892     va_list va){
893
894     g_assert(s);
895     g_assert(type);
896     g_assert(name);
897
898     avahi_server_add_service(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
899 }
900
901 void avahi_server_add_service(
902     AvahiServer *s,
903     AvahiEntryGroup *g,
904     gint interface,
905     guchar protocol,
906     const gchar *type,
907     const gchar *name,
908     const gchar *domain,
909     const gchar *host,
910     guint16 port,
911     ... ){
912
913     va_list va;
914     
915     g_assert(s);
916     g_assert(type);
917     g_assert(name);
918
919     va_start(va, port);
920     avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
921     va_end(va);
922 }
923
924 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
925     AvahiKey *k = userdata;
926
927     g_assert(m);
928     g_assert(i);
929     g_assert(k);
930
931     avahi_interface_post_query(i, k, FALSE);
932 }
933
934 void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
935     g_assert(s);
936     g_assert(key);
937
938     avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
939 }
940
941 struct tmpdata {
942     AvahiRecord *record;
943     gboolean flush_cache;
944 };
945
946 static void post_response_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
947     struct tmpdata *tmpdata = userdata;
948
949     g_assert(m);
950     g_assert(i);
951     g_assert(tmpdata);
952
953     avahi_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE);
954 }
955
956 void avahi_server_post_response(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *record, gboolean flush_cache) {
957     struct tmpdata tmpdata;
958     
959     g_assert(s);
960     g_assert(record);
961
962     tmpdata.record = record;
963     tmpdata.flush_cache = flush_cache;
964
965     avahi_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata);
966 }
967
968 void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
969     g_assert(g);
970
971     g->state = state;
972     
973     if (g->callback) {
974         g->callback(g->server, g, state, g->userdata);
975         return;
976     }
977 }
978
979 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) {
980     AvahiEntryGroup *g;
981     
982     g_assert(s);
983
984     g = g_new(AvahiEntryGroup, 1);
985     g->server = s;
986     g->callback = callback;
987     g->userdata = userdata;
988     g->dead = FALSE;
989     g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
990     g->n_probing = 0;
991     AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
992
993     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
994     return g;
995 }
996
997 void avahi_entry_group_free(AvahiEntryGroup *g) {
998     g_assert(g);
999     g_assert(g->server);
1000
1001     g->dead = TRUE;
1002     g->server->need_group_cleanup = TRUE;
1003 }
1004
1005 void avahi_entry_group_commit(AvahiEntryGroup *g) {
1006     g_assert(g);
1007     g_assert(!g->dead);
1008
1009     if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
1010         return;
1011
1012     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1013     avahi_announce_group(g->server, g);
1014     avahi_entry_group_check_probed(g, FALSE);
1015 }
1016
1017 gboolean avahi_entry_commited(AvahiEntry *e) {
1018     g_assert(e);
1019     g_assert(!e->dead);
1020
1021     return !e->group ||
1022         e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1023         e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1024 }
1025
1026 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {
1027     g_assert(g);
1028     g_assert(!g->dead);
1029
1030     return g->state;
1031 }