]> git.meshlink.io Git - catta/blob - avahi-core/server.c
* rename subscribe.[ch] to resolve.[ch]
[catta] / avahi-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 "resolve.h"
37
38 #define AVAHI_HOST_RR_HOLDOFF_MSEC 1000
39
40 static void free_entry(AvahiServer*s, AvahiEntry *e) {
41     AvahiEntry *t;
42
43     g_assert(s);
44     g_assert(e);
45
46     avahi_goodbye_entry(s, e, TRUE);
47
48     /* Remove from linked list */
49     AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
50
51     /* Remove from hash table indexed by name */
52     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
53     AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
54     if (t)
55         g_hash_table_replace(s->entries_by_key, t->record->key, t);
56     else
57         g_hash_table_remove(s->entries_by_key, e->record->key);
58
59     /* Remove from associated group */
60     if (e->group)
61         AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
62
63     avahi_record_unref(e->record);
64     g_free(e);
65 }
66
67 static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
68     g_assert(s);
69     g_assert(g);
70
71     while (g->entries)
72         free_entry(s, g->entries);
73
74     AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
75     g_free(g);
76 }
77
78 static void cleanup_dead(AvahiServer *s) {
79     AvahiEntryGroup *g, *ng;
80     AvahiEntry *e, *ne;
81     g_assert(s);
82
83
84     if (s->need_group_cleanup) {
85         for (g = s->groups; g; g = ng) {
86             ng = g->groups_next;
87             
88             if (g->dead)
89                 free_group(s, g);
90         }
91
92         s->need_group_cleanup = FALSE;
93     }
94
95     if (s->need_entry_cleanup) {
96         for (e = s->entries; e; e = ne) {
97             ne = e->entries_next;
98             
99             if (e->dead)
100                 free_entry(s, e);
101         }
102
103         s->need_entry_cleanup = FALSE;
104     }
105 }
106
107 static void enum_aux_records(AvahiServer *s, AvahiInterface *i, const gchar *name, guint16 type, void (*callback)(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata), gpointer userdata) {
108     AvahiKey *k;
109     AvahiEntry *e;
110
111     g_assert(s);
112     g_assert(i);
113     g_assert(name);
114     g_assert(callback);
115
116     g_assert(type != AVAHI_DNS_TYPE_ANY);
117
118     k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type);
119
120     for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
121         if (!e->dead && avahi_entry_registered(s, e, i)) 
122             callback(s, e->record, e->flags & AVAHI_ENTRY_UNIQUE, userdata);
123
124     avahi_key_unref(k);
125 }
126
127 void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata), gpointer userdata) {
128     g_assert(s);
129     g_assert(i);
130     g_assert(r);
131     g_assert(callback);
132     
133     if (r->key->class == AVAHI_DNS_CLASS_IN) {
134         if (r->key->type == AVAHI_DNS_TYPE_PTR) {
135             enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_SRV, callback, userdata);
136             enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_TXT, callback, userdata);
137         } else if (r->key->type == AVAHI_DNS_TYPE_SRV) {
138             enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_A, callback, userdata);
139             enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_AAAA, callback, userdata);
140         }
141     }
142 }
143
144 void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, gboolean unicast_response, gboolean auxiliary) {
145     g_assert(s);
146     g_assert(i);
147     g_assert(e);
148
149     avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_ENTRY_UNIQUE, unicast_response, auxiliary);
150 }
151
152 void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, gboolean unicast_response) {
153     AvahiEntry *e;
154 /*     gchar *txt; */
155     
156     g_assert(s);
157     g_assert(i);
158     g_assert(k);
159
160 /*     g_message("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */
161 /*     g_free(txt); */
162
163     if (avahi_key_is_pattern(k)) {
164
165         /* Handle ANY query */
166         
167         for (e = s->entries; e; e = e->entries_next)
168             if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i))
169                 avahi_server_prepare_response(s, i, e, unicast_response, FALSE);
170
171     } else {
172
173         /* Handle all other queries */
174         
175         for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
176             if (!e->dead && avahi_entry_registered(s, e, i))
177                 avahi_server_prepare_response(s, i, e, unicast_response, FALSE);
178     }
179 }
180
181 static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
182     g_assert(s);
183     g_assert(e);
184     
185     if (e->group) {
186         AvahiEntry *k;
187         
188         for (k = e->group->entries; k; k = k->by_group_next) {
189             avahi_goodbye_entry(s, k, FALSE);
190             k->dead = TRUE;
191         }
192         
193         avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
194     } else {
195         avahi_goodbye_entry(s, e, FALSE);
196         e->dead = TRUE;
197     }
198
199     s->need_entry_cleanup = TRUE;
200 }
201
202 static void withdraw_rrset(AvahiServer *s, AvahiKey *key) {
203     AvahiEntry *e;
204     
205     g_assert(s);
206     g_assert(key);
207
208    for (e = g_hash_table_lookup(s->entries_by_key, key); e; e = e->by_key_next)
209         withdraw_entry(s, e);
210 }
211
212 static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
213     AvahiEntry *e, *n;
214     gchar *t;
215     gboolean ours = FALSE, won = FALSE, lost = FALSE;
216     
217     g_assert(s);
218     g_assert(record);
219     g_assert(i);
220
221     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
222         gint cmp;
223         n = e->by_key_next;
224
225         if (e->dead || !avahi_entry_probing(s, e, i))
226             continue;
227         
228         if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) {
229             ours = TRUE;
230             break;
231         } else if (cmp > 0)
232             won = TRUE;
233         else /* cmp < 0 */
234             lost = TRUE;
235     }
236
237     t = avahi_record_to_string(record);
238
239     if (!ours) {
240
241         if (won)
242             g_message("Recieved conflicting probe [%s]. Local host won.", t);
243         else if (lost) {
244             g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
245             withdraw_rrset(s, record->key);
246         }
247     }
248
249     g_free(t);
250 }
251
252 static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
253     gboolean valid = TRUE, ours = FALSE, conflict = FALSE, withdraw_immediately = FALSE;
254     AvahiEntry *e, *n, *conflicting_entry = NULL;
255     gchar *t;
256     
257     g_assert(s);
258     g_assert(i);
259     g_assert(record);
260
261     t = avahi_record_to_string(record);
262
263 /*     g_message("CHECKING FOR CONFLICT: [%s]", t);   */
264
265     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
266         n = e->by_key_next;
267
268         if (e->dead || (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique))
269             continue;
270
271         /* Either our entry or the other is intended to be unique, so let's check */
272         
273         if (avahi_entry_registered(s, e, i)) {
274
275             if (avahi_record_equal_no_ttl(e->record, record)) {
276                 ours = TRUE; /* We have an identical record, so this is no conflict */
277                 
278                 /* Check wheter there is a TTL conflict */
279                 if (record->ttl <= e->record->ttl/2) {
280                     /* Refresh */
281                     g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
282                     avahi_interface_post_response(i, e->record, FALSE, NULL, TRUE);
283                     valid = FALSE;
284                 }
285                 
286                 /* There's no need to check the other entries of this RRset */
287                 break;
288             } else {
289                 /* A conflict => we have to return to probe mode */
290                 conflict = TRUE;
291                 conflicting_entry = e;
292             }
293
294         } else if (avahi_entry_probing(s, e, i)) {
295
296             if (!avahi_record_equal_no_ttl(record, e->record)) {
297             
298                 /* We are currently registering a matching record, but
299                  * someone else already claimed it, so let's
300                  * withdraw */
301                 conflict = TRUE;
302                 withdraw_immediately = TRUE;
303             }
304         }
305     }
306
307 /*     g_message("ours=%i conflict=%i", ours, conflict); */
308
309     if (!ours && conflict) {
310         valid = FALSE;
311
312         if (withdraw_immediately) {
313             g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
314             withdraw_rrset(s, record->key);
315         } else {
316             g_assert(conflicting_entry);
317             g_message("Recieved conflicting record [%s]. Resetting our record.", t);
318             avahi_entry_return_to_initial_state(s, conflicting_entry, i);
319
320             /* Local unique records are returned to probin
321              * state. Local shared records are reannounced. */
322         }
323     }
324
325     g_free(t);
326
327     return valid;
328 }
329
330 static void append_aux_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) {
331     gboolean *unicast_response = userdata;
332
333     g_assert(s);
334     g_assert(r);
335     g_assert(unicast_response);
336     
337     avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, TRUE);
338 }
339
340 static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean unicast_response) {
341     g_assert(s);
342     g_assert(r);
343
344     avahi_server_enumerate_aux_records(s, i, r, append_aux_callback, &unicast_response);
345 }
346
347 void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
348
349     g_assert(s);
350     g_assert(i);
351     g_assert(!legacy_unicast || (a && port > 0 && p));
352
353     if (legacy_unicast) {
354         AvahiDnsPacket *reply;
355         AvahiRecord *r;
356
357         reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , TRUE, TRUE);
358         
359         while ((r = avahi_record_list_next(s->record_list, NULL, NULL, NULL))) {
360
361             append_aux_records_to_list(s, i, r, FALSE);
362             
363             if (avahi_dns_packet_append_record(reply, r, FALSE, 10))
364                 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
365             else {
366                 gchar *t = avahi_record_to_string(r);
367                 g_warning("Record [%s] not fitting in legacy unicast packet, dropping.", t);
368                 g_free(t);
369             }
370
371             avahi_record_unref(r);
372         }
373
374         if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0)
375             avahi_interface_send_packet_unicast(i, reply, a, port);
376
377         avahi_dns_packet_free(reply);
378
379     } else {
380         gboolean unicast_response, flush_cache, auxiliary;
381         AvahiDnsPacket *reply = NULL;
382         AvahiRecord *r;
383
384         /* In case the query packet was truncated never respond
385         immediately, because known answer suppression records might be
386         contained in later packets */
387         gboolean tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC);
388         
389         while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) {
390                         
391             if (!avahi_interface_post_response(i, r, flush_cache, a, !tc && flush_cache && !auxiliary) && unicast_response) {
392
393                 append_aux_records_to_list(s, i, r, unicast_response);
394                 
395                 /* Due to some reasons the record has not been scheduled.
396                  * The client requested an unicast response in that
397                  * case. Therefore we prepare such a response */
398
399                 for (;;) {
400                 
401                     if (!reply) {
402                         g_assert(p);
403                         reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE);
404                     }
405                 
406                     if (avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
407
408                         /* Appending this record succeeded, so incremeant
409                          * the specific header field, and return to the caller */
410                         
411                         avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
412
413                         break;
414                     }
415
416                     if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
417                         guint size;
418
419                         /* The record is too large for one packet, so create a larger packet */
420
421                         avahi_dns_packet_free(reply);
422                         size = avahi_record_get_estimate_size(r) + AVAHI_DNS_PACKET_HEADER_SIZE;
423                         if (size > AVAHI_DNS_PACKET_MAX_SIZE)
424                             size = AVAHI_DNS_PACKET_MAX_SIZE;
425                         reply = avahi_dns_packet_new_reply(p, size, FALSE, TRUE);
426
427                         if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
428                             avahi_dns_packet_free(reply);
429                             
430                             gchar *t = avahi_record_to_string(r);
431                             g_warning("Record [%s] too large, doesn't fit in any packet!", t);
432                             g_free(t);
433                             break;
434                         } else
435                             avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
436                     }
437
438                     /* Appending the record didn't succeeed, so let's send this packet, and create a new one */
439                     avahi_interface_send_packet_unicast(i, reply, a, port);
440                     avahi_dns_packet_free(reply);
441                     reply = NULL;
442                 }
443             }
444
445             avahi_record_unref(r);
446         }
447
448         if (reply) {
449             if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0) 
450                 avahi_interface_send_packet_unicast(i, reply, a, port);
451             avahi_dns_packet_free(reply);
452         }
453     }
454
455     avahi_record_list_flush(s->record_list);
456 }
457
458 static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
459     guint n;
460     
461     g_assert(s);
462     g_assert(p);
463     g_assert(i);
464     g_assert(a);
465
466 /*     g_message("query"); */
467
468     g_assert(avahi_record_list_empty(s->record_list));
469     
470     /* Handle the questions */
471     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
472         AvahiKey *key;
473         gboolean unicast_response = FALSE;
474
475         if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
476             g_warning("Packet too short (1)");
477             goto fail;
478         }
479
480         avahi_query_scheduler_incoming(i->query_scheduler, key);
481         avahi_server_prepare_matching_responses(s, i, key, unicast_response);
482         avahi_key_unref(key);
483     }
484
485     /* Known Answer Suppression */
486     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
487         AvahiRecord *record;
488         gboolean unique = FALSE;
489
490         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
491             g_warning("Packet too short (2)");
492             goto fail;
493         }
494
495         if (handle_conflict(s, i, record, unique, a)) {
496             avahi_response_scheduler_suppress(i->response_scheduler, record, a);
497             avahi_record_list_drop(s->record_list, record);
498         }
499         
500         avahi_record_unref(record);
501     }
502
503     /* Probe record */
504     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
505         AvahiRecord *record;
506         gboolean unique = FALSE;
507
508         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
509             g_warning("Packet too short (3)");
510             goto fail;
511         }
512
513         if (record->key->type != AVAHI_DNS_TYPE_ANY)
514             incoming_probe(s, record, i);
515         
516         avahi_record_unref(record);
517     }
518
519     if (!avahi_record_list_empty(s->record_list))
520         avahi_server_generate_response(s, i, p, a, port, legacy_unicast);
521
522     return;
523     
524 fail:
525     avahi_record_list_flush(s->record_list);
526
527 }
528
529 static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
530     guint n;
531     
532     g_assert(s);
533     g_assert(p);
534     g_assert(i);
535     g_assert(a);
536
537 /*     g_message("response"); */
538     
539     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
540              avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
541         AvahiRecord *record;
542         gboolean cache_flush = FALSE;
543 /*         gchar *txt; */
544         
545         if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
546             g_warning("Packet too short (4)");
547             break;
548         }
549
550         if (record->key->type != AVAHI_DNS_TYPE_ANY) {
551
552 /*             g_message("Handling response: %s", txt = avahi_record_to_string(record)); */
553 /*             g_free(txt); */
554             
555             if (handle_conflict(s, i, record, cache_flush, a)) {
556                 avahi_cache_update(i->cache, record, cache_flush, a);
557                 avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush);
558             }
559         }
560             
561         avahi_record_unref(record);
562     }
563 }
564
565 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
566     AvahiInterface *i;
567     AvahiAddress a;
568     guint16 port;
569     
570     g_assert(s);
571     g_assert(p);
572     g_assert(sa);
573     g_assert(iface > 0);
574
575     if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
576         !avahi_interface_relevant(i)) {
577         g_warning("Recieved packet from invalid interface.");
578         return;
579     }
580
581 /*     g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
582
583     if (sa->sa_family == AF_INET6) {
584         static const guint8 ipv4_in_ipv6[] = {
585             0x00, 0x00, 0x00, 0x00,
586             0x00, 0x00, 0x00, 0x00,
587             0xFF, 0xFF, 0xFF, 0xFF };
588
589         /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
590
591         if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
592             return;
593     }
594
595     if (avahi_dns_packet_check_valid(p) < 0) {
596         g_warning("Recieved invalid packet.");
597         return;
598     }
599
600     port = avahi_port_from_sockaddr(sa);
601     avahi_address_from_sockaddr(sa, &a);
602
603     if (avahi_dns_packet_is_query(p)) {
604         gboolean legacy_unicast = FALSE;
605
606         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
607             g_warning("Invalid query packet.");
608             return;
609         }
610
611         if (port != AVAHI_MDNS_PORT) {
612             /* Legacy Unicast */
613
614             if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
615                  avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
616                 g_warning("Invalid legacy unicast query packet.");
617                 return;
618             }
619         
620             legacy_unicast = TRUE;
621         }
622
623         handle_query(s, p, i, &a, port, legacy_unicast);
624         
625 /*         g_message("Handled query"); */
626     } else {
627
628         if (port != AVAHI_MDNS_PORT) {
629             g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
630             return;
631         }
632
633         if (ttl != 255) {
634             g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
635             if (s->config.check_response_ttl)
636                 return;
637         }
638
639         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
640             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
641             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
642             g_warning("Invalid response packet.");
643             return;
644         }
645
646         handle_response(s, p, i, &a);
647 /*         g_message("Handled response"); */
648     }
649 }
650
651 static void work(AvahiServer *s) {
652     struct sockaddr_in6 sa6;
653     struct sockaddr_in sa;
654     AvahiDnsPacket *p;
655     gint iface = 0;
656     guint8 ttl;
657         
658     g_assert(s);
659
660     if (s->pollfd_ipv4.revents & G_IO_IN) {
661         if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
662             dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
663             avahi_dns_packet_free(p);
664         }
665     }
666
667     if (s->pollfd_ipv6.revents & G_IO_IN) {
668         if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
669             dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
670             avahi_dns_packet_free(p);
671         }
672     }
673 }
674
675 static gboolean prepare_func(GSource *source, gint *timeout) {
676     g_assert(source);
677     g_assert(timeout);
678     
679     *timeout = -1;
680     return FALSE;
681 }
682
683 static gboolean check_func(GSource *source) {
684     AvahiServer* s;
685     g_assert(source);
686
687     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
688     g_assert(s);
689     
690     return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
691 }
692
693 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
694     AvahiServer* s;
695     g_assert(source);
696
697     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
698     g_assert(s);
699
700     work(s);
701     cleanup_dead(s);
702
703     return TRUE;
704 }
705
706 static void server_set_state(AvahiServer *s, AvahiServerState state) {
707     g_assert(s);
708
709     if (s->state == state)
710         return;
711     
712     s->state = state;
713
714     if (s->callback)
715         s->callback(s, state, s->userdata);
716 }
717
718 static void withdraw_host_rrs(AvahiServer *s) {
719     g_assert(s);
720
721     if (s->hinfo_entry_group) {
722         avahi_entry_group_free(s->hinfo_entry_group);
723         s->hinfo_entry_group = NULL;
724     }
725
726     if (s->browse_domain_entry_group) {
727         avahi_entry_group_free(s->browse_domain_entry_group);
728         s->browse_domain_entry_group = NULL;
729     }
730
731     avahi_update_host_rrs(s->monitor, TRUE);
732     s->n_host_rr_pending = 0;
733 }
734
735 void avahi_server_decrease_host_rr_pending(AvahiServer *s) {
736     g_assert(s);
737     
738     g_assert(s->n_host_rr_pending > 0);
739
740     if (--s->n_host_rr_pending == 0)
741         server_set_state(s, AVAHI_SERVER_RUNNING);
742 }
743
744 void avahi_server_increase_host_rr_pending(AvahiServer *s) {
745     g_assert(s);
746
747     s->n_host_rr_pending ++;
748 }
749
750 void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
751     g_assert(s);
752     g_assert(g);
753
754     if (state == AVAHI_ENTRY_GROUP_REGISTERING &&
755         s->state == AVAHI_SERVER_REGISTERING)
756         avahi_server_increase_host_rr_pending(s);
757     else if (state == AVAHI_ENTRY_GROUP_COLLISION &&
758         (s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) {
759         withdraw_host_rrs(s);
760         server_set_state(s, AVAHI_SERVER_COLLISION);
761     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED &&
762                s->state == AVAHI_SERVER_REGISTERING)
763         avahi_server_decrease_host_rr_pending(s);
764 }
765
766 static void register_hinfo(AvahiServer *s) {
767     struct utsname utsname;
768     AvahiRecord *r;
769     
770     g_assert(s);
771     
772     if (!s->config.register_hinfo || s->hinfo_entry_group)
773         return;
774     
775     s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
776     
777     /* Fill in HINFO rr */
778     r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
779     uname(&utsname);
780     r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
781     r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
782     avahi_server_add(s, s->hinfo_entry_group, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
783     avahi_record_unref(r);
784
785     avahi_entry_group_commit(s->hinfo_entry_group);
786 }
787
788 static void register_localhost(AvahiServer *s) {
789     AvahiAddress a;
790     g_assert(s);
791     
792     /* Add localhost entries */
793     avahi_address_parse("127.0.0.1", AF_INET, &a);
794     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
795
796     avahi_address_parse("::1", AF_INET6, &a);
797     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
798 }
799
800 static void register_browse_domain(AvahiServer *s) {
801     g_assert(s);
802
803     if (!s->config.announce_domain || s->browse_domain_entry_group)
804         return;
805
806     s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
807     avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "_browse._dns-sd._udp.local", s->domain_name);
808     avahi_entry_group_commit(s->browse_domain_entry_group);
809 }
810
811 static void register_stuff(AvahiServer *s) {
812     g_assert(s);
813
814     server_set_state(s, AVAHI_SERVER_REGISTERING);
815     register_hinfo(s);
816     register_browse_domain(s);
817     avahi_update_host_rrs(s->monitor, FALSE);
818
819     if (s->n_host_rr_pending == 0)
820         server_set_state(s, AVAHI_SERVER_RUNNING);
821 }
822
823 static void update_fqdn(AvahiServer *s) {
824     g_assert(s);
825     
826     g_assert(s->host_name);
827     g_assert(s->domain_name);
828
829     g_free(s->host_name_fqdn);
830     s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain_name);
831 }
832
833 static void register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) {
834     AvahiServer *s = userdata;
835     
836     g_assert(e);
837     g_assert(s);
838
839     g_assert(e == s->register_time_event);
840     avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
841     s->register_time_event = NULL;
842
843     if (s->state == AVAHI_SERVER_SLEEPING)
844         register_stuff(s);
845 }
846
847 static void delayed_register_stuff(AvahiServer *s) {
848     GTimeVal tv;
849     
850     g_assert(s);
851
852     avahi_elapse_time(&tv, AVAHI_HOST_RR_HOLDOFF_MSEC, 0);
853
854     if (s->register_time_event)
855         avahi_time_event_queue_update(s->time_event_queue, s->register_time_event, &tv);
856     else
857         s->register_time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, register_time_event_callback, s);
858 }
859
860 void avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) {
861     g_assert(s);
862     g_assert(host_name);
863
864     server_set_state(s, AVAHI_SERVER_SLEEPING);
865     withdraw_host_rrs(s);
866
867     g_free(s->host_name);
868     s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_host_name();
869     s->host_name[strcspn(s->host_name, ".")] = 0;
870     update_fqdn(s);
871
872     delayed_register_stuff(s);
873 }
874
875 void avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) {
876     g_assert(s);
877     g_assert(domain_name);
878
879     server_set_state(s, AVAHI_SERVER_SLEEPING);
880     withdraw_host_rrs(s);
881
882     g_free(s->domain_name);
883     s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local.");
884     update_fqdn(s);
885
886     delayed_register_stuff(s);
887 }
888
889 AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, AvahiServerCallback callback, gpointer userdata) {
890     AvahiServer *s;
891     
892     static GSourceFuncs source_funcs = {
893         prepare_func,
894         check_func,
895         dispatch_func,
896         NULL,
897         NULL,
898         NULL
899     };
900
901     s = g_new(AvahiServer, 1);
902     s->n_host_rr_pending = 0;
903     s->need_entry_cleanup = s->need_group_cleanup = FALSE;
904
905     if (sc)
906         avahi_server_config_copy(&s->config, sc);
907     else
908         avahi_server_config_init(&s->config);
909     
910     s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1;
911     s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1;
912     
913     if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
914         g_critical("Selected neither IPv6 nor IPv4 support, aborting.\n");
915         avahi_server_config_free(&s->config);
916         g_free(s);
917         return NULL;
918     }
919
920     if (s->fd_ipv4 < 0 && s->config.use_ipv4)
921         g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
922     else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
923         g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
924     
925     if (c)
926         g_main_context_ref(s->context = c);
927     else
928         s->context = g_main_context_default();
929
930     /* Prepare IO source registration */
931     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
932     *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
933
934     memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
935     s->pollfd_ipv4.fd = s->fd_ipv4;
936     s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
937     g_source_add_poll(s->source, &s->pollfd_ipv4);
938     
939     memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
940     s->pollfd_ipv6.fd = s->fd_ipv6;
941     s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
942     g_source_add_poll(s->source, &s->pollfd_ipv6);
943
944     g_source_attach(s->source, s->context);
945     
946     s->callback = callback;
947     s->userdata = userdata;
948     
949     AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
950     s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
951     AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
952
953     AVAHI_LLIST_HEAD_INIT(AvahiRecordResolver, s->record_resolvers);
954     s->record_resolver_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
955     AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers);
956
957     /* Get host name */
958     s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
959     s->host_name[strcspn(s->host_name, ".")] = 0;
960     s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : g_strdup("local.");
961     s->host_name_fqdn = NULL;
962     update_fqdn(s);
963
964     s->record_list = avahi_record_list_new();
965
966     s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
967     s->register_time_event = NULL;
968     
969     s->state = AVAHI_SERVER_INVALID;
970
971     s->monitor = avahi_interface_monitor_new(s);
972     avahi_interface_monitor_sync(s->monitor);
973
974     register_localhost(s);
975
976     s->hinfo_entry_group = NULL;
977     s->browse_domain_entry_group = NULL;
978     register_stuff(s);
979     
980     return s;
981 }
982
983 void avahi_server_free(AvahiServer* s) {
984     g_assert(s);
985
986     while(s->entries)
987         free_entry(s, s->entries);
988
989     avahi_interface_monitor_free(s->monitor);
990
991     while (s->groups)
992         free_group(s, s->groups);
993
994     while (s->host_name_resolvers)
995         avahi_host_name_resolver_free(s->host_name_resolvers);
996     
997     while (s->record_resolvers)
998         avahi_record_resolver_free(s->record_resolvers);
999     g_hash_table_destroy(s->record_resolver_hashtable);
1000
1001     g_hash_table_destroy(s->entries_by_key);
1002
1003     if (s->register_time_event)
1004         avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
1005     avahi_time_event_queue_free(s->time_event_queue);
1006
1007     avahi_record_list_free(s->record_list);
1008     
1009     if (s->fd_ipv4 >= 0)
1010         close(s->fd_ipv4);
1011     if (s->fd_ipv6 >= 0)
1012         close(s->fd_ipv6);
1013
1014     g_free(s->host_name);
1015     g_free(s->domain_name);
1016     g_free(s->host_name_fqdn);
1017
1018     g_source_destroy(s->source);
1019     g_source_unref(s->source);
1020     g_main_context_unref(s->context);
1021
1022     avahi_server_config_free(&s->config);
1023
1024     g_free(s);
1025 }
1026
1027 void avahi_server_add(
1028     AvahiServer *s,
1029     AvahiEntryGroup *g,
1030     gint interface,
1031     guchar protocol,
1032     AvahiEntryFlags flags,
1033     AvahiRecord *r) {
1034     
1035     AvahiEntry *e, *t;
1036     g_assert(s);
1037     g_assert(r);
1038
1039     g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
1040
1041     e = g_new(AvahiEntry, 1);
1042     e->server = s;
1043     e->record = avahi_record_ref(r);
1044     e->group = g;
1045     e->interface = interface;
1046     e->protocol = protocol;
1047     e->flags = flags;
1048     e->dead = FALSE;
1049
1050     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
1051
1052     AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
1053
1054     /* Insert into hash table indexed by name */
1055     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
1056     AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
1057     g_hash_table_replace(s->entries_by_key, e->record->key, t);
1058
1059     /* Insert into group list */
1060     if (g)
1061         AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); 
1062
1063     avahi_announce_entry(s, e);
1064 }
1065 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
1066     AvahiEntry **e = (AvahiEntry**) state;
1067     g_assert(s);
1068     g_assert(e);
1069
1070     if (!*e)
1071         *e = g ? g->entries : s->entries;
1072     
1073     while (*e && (*e)->dead)
1074         *e = g ? (*e)->by_group_next : (*e)->entries_next;
1075         
1076     if (!*e)
1077         return NULL;
1078
1079     return avahi_record_ref((*e)->record);
1080 }
1081
1082 void avahi_server_dump(AvahiServer *s, FILE *f) {
1083     AvahiEntry *e;
1084     g_assert(s);
1085     g_assert(f);
1086
1087     fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
1088
1089     for (e = s->entries; e; e = e->entries_next) {
1090         gchar *t;
1091
1092         if (e->dead)
1093             continue;
1094         
1095         t = avahi_record_to_string(e->record);
1096         fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
1097         g_free(t);
1098     }
1099
1100     avahi_dump_caches(s->monitor, f);
1101 }
1102
1103 void avahi_server_add_ptr(
1104     AvahiServer *s,
1105     AvahiEntryGroup *g,
1106     gint interface,
1107     guchar protocol,
1108     AvahiEntryFlags flags,
1109     const gchar *name,
1110     const gchar *dest) {
1111
1112     AvahiRecord *r;
1113
1114     g_assert(dest);
1115
1116     r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
1117     r->data.ptr.name = avahi_normalize_name(dest);
1118     avahi_server_add(s, g, interface, protocol, flags, r);
1119     avahi_record_unref(r);
1120 }
1121
1122 void avahi_server_add_address(
1123     AvahiServer *s,
1124     AvahiEntryGroup *g,
1125     gint interface,
1126     guchar protocol,
1127     AvahiEntryFlags flags,
1128     const gchar *name,
1129     AvahiAddress *a) {
1130
1131     gchar *n = NULL;
1132     g_assert(s);
1133     g_assert(a);
1134
1135     name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn;
1136     
1137     if (a->family == AF_INET) {
1138         gchar *reverse;
1139         AvahiRecord  *r;
1140
1141         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
1142         r->data.a.address = a->data.ipv4;
1143         avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
1144         avahi_record_unref(r);
1145         
1146         reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
1147         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1148         g_free(reverse);
1149         
1150     } else {
1151         gchar *reverse;
1152         AvahiRecord *r;
1153             
1154         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
1155         r->data.aaaa.address = a->data.ipv6;
1156         avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
1157         avahi_record_unref(r);
1158
1159         reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
1160         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1161         g_free(reverse);
1162     
1163         reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
1164         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1165         g_free(reverse);
1166     }
1167     
1168     g_free(n);
1169 }
1170
1171 void avahi_server_add_text_strlst(
1172     AvahiServer *s,
1173     AvahiEntryGroup *g,
1174     gint interface,
1175     guchar protocol,
1176     AvahiEntryFlags flags,
1177     const gchar *name,
1178     AvahiStringList *strlst) {
1179
1180     AvahiRecord *r;
1181     
1182     g_assert(s);
1183     
1184     r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
1185     r->data.txt.string_list = strlst;
1186     avahi_server_add(s, g, interface, protocol, flags, r);
1187     avahi_record_unref(r);
1188 }
1189
1190 void avahi_server_add_text_va(
1191     AvahiServer *s,
1192     AvahiEntryGroup *g,
1193     gint interface,
1194     guchar protocol,
1195     AvahiEntryFlags flags,
1196     const gchar *name,
1197     va_list va) {
1198     
1199     g_assert(s);
1200
1201     avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
1202 }
1203
1204 void avahi_server_add_text(
1205     AvahiServer *s,
1206     AvahiEntryGroup *g,
1207     gint interface,
1208     guchar protocol,
1209     AvahiEntryFlags flags,
1210     const gchar *name,
1211     ...) {
1212
1213     va_list va;
1214     
1215     g_assert(s);
1216
1217     va_start(va, name);
1218     avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
1219     va_end(va);
1220 }
1221
1222 static void escape_service_name(gchar *d, guint size, const gchar *s) {
1223     g_assert(d);
1224     g_assert(size);
1225     g_assert(s);
1226
1227     while (*s && size >= 2) {
1228         if (*s == '.' || *s == '\\') {
1229             if (size < 3)
1230                 break;
1231
1232             *(d++) = '\\';
1233             size--;
1234         }
1235             
1236         *(d++) = *(s++);
1237         size--;
1238     }
1239
1240     g_assert(size > 0);
1241     *(d++) = 0;
1242 }
1243
1244 void avahi_server_add_service_strlst(
1245     AvahiServer *s,
1246     AvahiEntryGroup *g,
1247     gint interface,
1248     guchar protocol,
1249     const gchar *type,
1250     const gchar *name,
1251     const gchar *domain,
1252     const gchar *host,
1253     guint16 port,
1254     AvahiStringList *strlst) {
1255
1256     gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
1257     AvahiRecord *r;
1258     
1259     g_assert(s);
1260     g_assert(type);
1261     g_assert(name);
1262
1263     escape_service_name(ename, sizeof(ename), name);
1264
1265     if (domain) {
1266         while (domain[0] == '.')
1267             domain++;
1268     } else
1269         domain = s->domain_name;
1270
1271     if (!host)
1272         host = s->host_name_fqdn;
1273
1274     snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
1275     snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
1276     
1277     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
1278
1279     r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
1280     r->data.srv.priority = 0;
1281     r->data.srv.weight = 0;
1282     r->data.srv.port = port;
1283     r->data.srv.name = avahi_normalize_name(host);
1284     avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
1285     avahi_record_unref(r);
1286
1287     avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
1288
1289     snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
1290     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
1291 }
1292
1293 void avahi_server_add_service_va(
1294     AvahiServer *s,
1295     AvahiEntryGroup *g,
1296     gint interface,
1297     guchar protocol,
1298     const gchar *type,
1299     const gchar *name,
1300     const gchar *domain,
1301     const gchar *host,
1302     guint16 port,
1303     va_list va){
1304
1305     g_assert(s);
1306     g_assert(type);
1307     g_assert(name);
1308
1309     avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
1310 }
1311
1312 void avahi_server_add_service(
1313     AvahiServer *s,
1314     AvahiEntryGroup *g,
1315     gint interface,
1316     guchar protocol,
1317     const gchar *type,
1318     const gchar *name,
1319     const gchar *domain,
1320     const gchar *host,
1321     guint16 port,
1322     ... ){
1323
1324     va_list va;
1325     
1326     g_assert(s);
1327     g_assert(type);
1328     g_assert(name);
1329
1330     va_start(va, port);
1331     avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
1332     va_end(va);
1333 }
1334
1335 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
1336     AvahiKey *k = userdata;
1337
1338     g_assert(m);
1339     g_assert(i);
1340     g_assert(k);
1341
1342     avahi_interface_post_query(i, k, FALSE);
1343 }
1344
1345 void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
1346     g_assert(s);
1347     g_assert(key);
1348
1349     avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
1350 }
1351
1352 void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
1353     g_assert(g);
1354
1355     if (g->state == state)
1356         return;
1357
1358     g->state = state;
1359     
1360     if (g->callback) {
1361         g->callback(g->server, g, state, g->userdata);
1362         return;
1363     }
1364 }
1365
1366 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) {
1367     AvahiEntryGroup *g;
1368     
1369     g_assert(s);
1370
1371     g = g_new(AvahiEntryGroup, 1);
1372     g->server = s;
1373     g->callback = callback;
1374     g->userdata = userdata;
1375     g->dead = FALSE;
1376     g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1377     g->n_probing = 0;
1378     AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1379
1380     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
1381     return g;
1382 }
1383
1384 void avahi_entry_group_free(AvahiEntryGroup *g) {
1385     AvahiEntry *e;
1386     
1387     g_assert(g);
1388     g_assert(g->server);
1389
1390     for (e = g->entries; e; e = e->by_group_next) {
1391         avahi_goodbye_entry(g->server, e, TRUE);
1392         e->dead = TRUE;
1393     }
1394
1395     g->dead = TRUE;
1396     
1397     g->server->need_group_cleanup = TRUE;
1398     g->server->need_entry_cleanup = TRUE;
1399 }
1400
1401 void avahi_entry_group_commit(AvahiEntryGroup *g) {
1402     g_assert(g);
1403     g_assert(!g->dead);
1404
1405     if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
1406         return;
1407
1408     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1409     avahi_announce_group(g->server, g);
1410     avahi_entry_group_check_probed(g, FALSE);
1411 }
1412
1413 gboolean avahi_entry_commited(AvahiEntry *e) {
1414     g_assert(e);
1415     g_assert(!e->dead);
1416
1417     return !e->group ||
1418         e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1419         e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1420 }
1421
1422 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {
1423     g_assert(g);
1424     g_assert(!g->dead);
1425
1426     return g->state;
1427 }
1428
1429 void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata) {
1430     g_assert(g);
1431
1432     g->userdata = userdata;
1433 }
1434
1435 gpointer avahi_entry_group_get_data(AvahiEntryGroup *g) {
1436     g_assert(g);
1437
1438     return g->userdata;
1439 }
1440
1441 const gchar* avahi_server_get_domain_name(AvahiServer *s) {
1442     g_assert(s);
1443
1444     return s->domain_name;
1445 }
1446
1447 const gchar* avahi_server_get_host_name(AvahiServer *s) {
1448     g_assert(s);
1449
1450     return s->host_name;
1451 }
1452
1453 const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s) {
1454     g_assert(s);
1455
1456     return s->host_name_fqdn;
1457 }
1458
1459 gpointer avahi_server_get_data(AvahiServer *s) {
1460     g_assert(s);
1461
1462     return s->userdata;
1463 }
1464
1465 void avahi_server_set_data(AvahiServer *s, gpointer userdata) {
1466     g_assert(s);
1467
1468     s->userdata = userdata;
1469 }
1470
1471 AvahiServerState avhai_server_get_state(AvahiServer *s) {
1472     g_assert(s);
1473
1474     return s->state;
1475 }
1476
1477 AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) {
1478     g_assert(c);
1479
1480     memset(c, 0, sizeof(AvahiServerConfig));
1481     c->register_hinfo = TRUE;
1482     c->register_addresses = TRUE;
1483     c->use_ipv6 = TRUE;
1484     c->use_ipv4 = TRUE;
1485     c->host_name = NULL;
1486     c->domain_name = NULL;
1487     c->check_response_ttl = TRUE;
1488     c->announce_domain = TRUE;
1489     c->use_iff_running = FALSE;
1490     
1491     return c;
1492 }
1493
1494 void avahi_server_config_free(AvahiServerConfig *c) {
1495     g_assert(c);
1496
1497     g_free(c->host_name);
1498     g_free(c->domain_name);
1499 }
1500
1501 AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) {
1502     g_assert(ret);
1503     g_assert(c);
1504
1505     *ret = *c;
1506
1507     ret->host_name = g_strdup(c->host_name);
1508     ret->domain_name = g_strdup(c->domain_name);
1509
1510     return ret;
1511 }