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