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