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