]> git.meshlink.io Git - catta/blob - avahi-core/server.c
* add the first bits of a mDNS reflector
[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 "browse.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
469 static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean flush_cache) {
470     AvahiInterface *j;
471     
472     g_assert(s);
473     g_assert(i);
474     g_assert(r);
475
476     if (!s->config.enable_reflector)
477         return;
478
479     for (j = s->monitor->interfaces; j; j = j->interface_next)
480         if (j != i)
481             avahi_interface_post_response(j, r, flush_cache, NULL, TRUE);
482 }
483
484 static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) {
485     AvahiInterface *j;
486     
487     g_assert(s);
488     g_assert(i);
489     g_assert(k);
490
491     if (!s->config.enable_reflector)
492         return;
493
494     for (j = s->monitor->interfaces; j; j = j->interface_next)
495         if (j != i)
496             avahi_interface_post_query(j, k, TRUE);
497 }
498
499 static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) {
500     AvahiInterface *j;
501     
502     g_assert(s);
503     g_assert(i);
504     g_assert(r);
505
506     if (!s->config.enable_reflector)
507         return;
508
509     for (j = s->monitor->interfaces; j; j = j->interface_next)
510         if (j != i)
511             avahi_interface_post_probe(j, r, TRUE);
512 }
513
514 static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
515     guint n;
516     
517     g_assert(s);
518     g_assert(p);
519     g_assert(i);
520     g_assert(a);
521
522 /*     g_message("query"); */
523
524     g_assert(avahi_record_list_empty(s->record_list));
525     
526     /* Handle the questions */
527     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
528         AvahiKey *key;
529         gboolean unicast_response = FALSE;
530
531         if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
532             g_warning("Packet too short (1)");
533             goto fail;
534         }
535
536         reflect_query(s, i, key);
537         avahi_query_scheduler_incoming(i->query_scheduler, key);
538         avahi_server_prepare_matching_responses(s, i, key, unicast_response);
539         avahi_key_unref(key);
540     }
541
542     /* Known Answer Suppression */
543     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
544         AvahiRecord *record;
545         gboolean unique = FALSE;
546
547         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
548             g_warning("Packet too short (2)");
549             goto fail;
550         }
551
552         if (handle_conflict(s, i, record, unique, a)) {
553             avahi_response_scheduler_suppress(i->response_scheduler, record, a);
554             avahi_record_list_drop(s->record_list, record);
555         }
556         
557         avahi_record_unref(record);
558     }
559
560     /* Probe record */
561     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
562         AvahiRecord *record;
563         gboolean unique = FALSE;
564
565         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
566             g_warning("Packet too short (3)");
567             goto fail;
568         }
569
570         if (record->key->type != AVAHI_DNS_TYPE_ANY) {
571             reflect_probe(s, i, record);
572             incoming_probe(s, record, i);
573         }
574         
575         avahi_record_unref(record);
576     }
577
578     if (!avahi_record_list_empty(s->record_list))
579         avahi_server_generate_response(s, i, p, a, port, legacy_unicast);
580
581     return;
582     
583 fail:
584     avahi_record_list_flush(s->record_list);
585
586 }
587
588 static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
589     guint n;
590     
591     g_assert(s);
592     g_assert(p);
593     g_assert(i);
594     g_assert(a);
595
596 /*     g_message("response"); */
597     
598     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
599              avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
600         AvahiRecord *record;
601         gboolean cache_flush = FALSE;
602 /*         gchar *txt; */
603         
604         if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
605             g_warning("Packet too short (4)");
606             break;
607         }
608
609         if (record->key->type != AVAHI_DNS_TYPE_ANY) {
610
611 /*             g_message("Handling response: %s", txt = avahi_record_to_string(record)); */
612 /*             g_free(txt); */
613             
614             if (handle_conflict(s, i, record, cache_flush, a)) {
615                 reflect_response(s, i, record, cache_flush);
616                 avahi_cache_update(i->cache, record, cache_flush, a);
617                 avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush);
618             }
619         }
620             
621         avahi_record_unref(record);
622     }
623 }
624
625 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
626     AvahiInterface *i;
627     AvahiAddress a;
628     guint16 port;
629     
630     g_assert(s);
631     g_assert(p);
632     g_assert(sa);
633     g_assert(iface > 0);
634
635     if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
636         !avahi_interface_relevant(i)) {
637         g_warning("Recieved packet from invalid interface.");
638         return;
639     }
640
641 /*     g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
642
643     if (sa->sa_family == AF_INET6) {
644         static const guint8 ipv4_in_ipv6[] = {
645             0x00, 0x00, 0x00, 0x00,
646             0x00, 0x00, 0x00, 0x00,
647             0xFF, 0xFF, 0xFF, 0xFF };
648
649         /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
650
651         if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
652             return;
653     }
654
655     if (avahi_dns_packet_check_valid(p) < 0) {
656         g_warning("Recieved invalid packet.");
657         return;
658     }
659
660     port = avahi_port_from_sockaddr(sa);
661     avahi_address_from_sockaddr(sa, &a);
662
663     if (avahi_dns_packet_is_query(p)) {
664         gboolean legacy_unicast = FALSE;
665
666         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
667             g_warning("Invalid query packet.");
668             return;
669         }
670
671         if (port != AVAHI_MDNS_PORT) {
672             /* Legacy Unicast */
673
674             if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
675                  avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
676                 g_warning("Invalid legacy unicast query packet.");
677                 return;
678             }
679         
680             legacy_unicast = TRUE;
681         }
682
683         handle_query(s, p, i, &a, port, legacy_unicast);
684         
685 /*         g_message("Handled query"); */
686     } else {
687
688         if (port != AVAHI_MDNS_PORT) {
689             g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
690             return;
691         }
692
693         if (ttl != 255) {
694             g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
695             if (s->config.check_response_ttl)
696                 return;
697         }
698
699         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
700             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
701             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
702             g_warning("Invalid response packet.");
703             return;
704         }
705
706         handle_response(s, p, i, &a);
707 /*         g_message("Handled response"); */
708     }
709 }
710
711 static void work(AvahiServer *s) {
712     struct sockaddr_in6 sa6;
713     struct sockaddr_in sa;
714     AvahiDnsPacket *p;
715     gint iface = 0;
716     guint8 ttl;
717         
718     g_assert(s);
719
720     if (s->pollfd_ipv4.revents & G_IO_IN) {
721         if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
722             dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
723             avahi_dns_packet_free(p);
724         }
725     }
726
727     if (s->pollfd_ipv6.revents & G_IO_IN) {
728         if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
729             dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
730             avahi_dns_packet_free(p);
731         }
732     }
733 }
734
735 static gboolean prepare_func(GSource *source, gint *timeout) {
736     g_assert(source);
737     g_assert(timeout);
738     
739     *timeout = -1;
740     return FALSE;
741 }
742
743 static gboolean check_func(GSource *source) {
744     AvahiServer* s;
745     g_assert(source);
746
747     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
748     g_assert(s);
749     
750     return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
751 }
752
753 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
754     AvahiServer* s;
755     g_assert(source);
756
757     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
758     g_assert(s);
759
760     work(s);
761     cleanup_dead(s);
762
763     return TRUE;
764 }
765
766 static void server_set_state(AvahiServer *s, AvahiServerState state) {
767     g_assert(s);
768
769     if (s->state == state)
770         return;
771     
772     s->state = state;
773
774     if (s->callback)
775         s->callback(s, state, s->userdata);
776 }
777
778 static void withdraw_host_rrs(AvahiServer *s) {
779     g_assert(s);
780
781     if (s->hinfo_entry_group) {
782         avahi_entry_group_free(s->hinfo_entry_group);
783         s->hinfo_entry_group = NULL;
784     }
785
786     if (s->browse_domain_entry_group) {
787         avahi_entry_group_free(s->browse_domain_entry_group);
788         s->browse_domain_entry_group = NULL;
789     }
790
791     avahi_update_host_rrs(s->monitor, TRUE);
792     s->n_host_rr_pending = 0;
793 }
794
795 void avahi_server_decrease_host_rr_pending(AvahiServer *s) {
796     g_assert(s);
797     
798     g_assert(s->n_host_rr_pending > 0);
799
800     if (--s->n_host_rr_pending == 0)
801         server_set_state(s, AVAHI_SERVER_RUNNING);
802 }
803
804 void avahi_server_increase_host_rr_pending(AvahiServer *s) {
805     g_assert(s);
806
807     s->n_host_rr_pending ++;
808 }
809
810 void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
811     g_assert(s);
812     g_assert(g);
813
814     if (state == AVAHI_ENTRY_GROUP_REGISTERING &&
815         s->state == AVAHI_SERVER_REGISTERING)
816         avahi_server_increase_host_rr_pending(s);
817     else if (state == AVAHI_ENTRY_GROUP_COLLISION &&
818         (s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) {
819         withdraw_host_rrs(s);
820         server_set_state(s, AVAHI_SERVER_COLLISION);
821     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED &&
822                s->state == AVAHI_SERVER_REGISTERING)
823         avahi_server_decrease_host_rr_pending(s);
824 }
825
826 static void register_hinfo(AvahiServer *s) {
827     struct utsname utsname;
828     AvahiRecord *r;
829     
830     g_assert(s);
831     
832     if (!s->config.register_hinfo || s->hinfo_entry_group)
833         return;
834     
835     s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
836     
837     /* Fill in HINFO rr */
838     r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
839     uname(&utsname);
840     r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
841     r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
842     avahi_server_add(s, s->hinfo_entry_group, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
843     avahi_record_unref(r);
844
845     avahi_entry_group_commit(s->hinfo_entry_group);
846 }
847
848 static void register_localhost(AvahiServer *s) {
849     AvahiAddress a;
850     g_assert(s);
851     
852     /* Add localhost entries */
853     avahi_address_parse("127.0.0.1", AF_INET, &a);
854     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
855
856     avahi_address_parse("::1", AF_INET6, &a);
857     avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
858 }
859
860 static void register_browse_domain(AvahiServer *s) {
861     g_assert(s);
862
863     if (!s->config.announce_domain || s->browse_domain_entry_group)
864         return;
865
866     s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
867     avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "_browse._dns-sd._udp.local", s->domain_name);
868     avahi_entry_group_commit(s->browse_domain_entry_group);
869 }
870
871 static void register_stuff(AvahiServer *s) {
872     g_assert(s);
873
874     server_set_state(s, AVAHI_SERVER_REGISTERING);
875     register_hinfo(s);
876     register_browse_domain(s);
877     avahi_update_host_rrs(s->monitor, FALSE);
878
879     if (s->n_host_rr_pending == 0)
880         server_set_state(s, AVAHI_SERVER_RUNNING);
881 }
882
883 static void update_fqdn(AvahiServer *s) {
884     g_assert(s);
885     
886     g_assert(s->host_name);
887     g_assert(s->domain_name);
888
889     g_free(s->host_name_fqdn);
890     s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain_name);
891 }
892
893 static void register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) {
894     AvahiServer *s = userdata;
895     
896     g_assert(e);
897     g_assert(s);
898
899     g_assert(e == s->register_time_event);
900     avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
901     s->register_time_event = NULL;
902
903     if (s->state == AVAHI_SERVER_SLEEPING)
904         register_stuff(s);
905 }
906
907 static void delayed_register_stuff(AvahiServer *s) {
908     GTimeVal tv;
909     
910     g_assert(s);
911
912     avahi_elapse_time(&tv, AVAHI_HOST_RR_HOLDOFF_MSEC, 0);
913
914     if (s->register_time_event)
915         avahi_time_event_queue_update(s->time_event_queue, s->register_time_event, &tv);
916     else
917         s->register_time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, register_time_event_callback, s);
918 }
919
920 void avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) {
921     g_assert(s);
922     g_assert(host_name);
923
924     server_set_state(s, AVAHI_SERVER_SLEEPING);
925     withdraw_host_rrs(s);
926
927     g_free(s->host_name);
928     s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_host_name();
929     s->host_name[strcspn(s->host_name, ".")] = 0;
930     update_fqdn(s);
931
932     delayed_register_stuff(s);
933 }
934
935 void avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) {
936     g_assert(s);
937     g_assert(domain_name);
938
939     server_set_state(s, AVAHI_SERVER_SLEEPING);
940     withdraw_host_rrs(s);
941
942     g_free(s->domain_name);
943     s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local.");
944     update_fqdn(s);
945
946     delayed_register_stuff(s);
947 }
948
949 AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, AvahiServerCallback callback, gpointer userdata) {
950     AvahiServer *s;
951     
952     static GSourceFuncs source_funcs = {
953         prepare_func,
954         check_func,
955         dispatch_func,
956         NULL,
957         NULL,
958         NULL
959     };
960
961     s = g_new(AvahiServer, 1);
962     s->n_host_rr_pending = 0;
963     s->need_entry_cleanup = s->need_group_cleanup = s->need_browser_cleanup = FALSE;
964
965     if (sc)
966         avahi_server_config_copy(&s->config, sc);
967     else
968         avahi_server_config_init(&s->config);
969     
970     s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1;
971     s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1;
972     
973     if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
974         g_critical("Selected neither IPv6 nor IPv4 support, aborting.\n");
975         avahi_server_config_free(&s->config);
976         g_free(s);
977         return NULL;
978     }
979
980     if (s->fd_ipv4 < 0 && s->config.use_ipv4)
981         g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
982     else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
983         g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
984     
985     if (c)
986         g_main_context_ref(s->context = c);
987     else
988         s->context = g_main_context_default();
989
990     /* Prepare IO source registration */
991     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
992     *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
993
994     memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
995     s->pollfd_ipv4.fd = s->fd_ipv4;
996     s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
997     g_source_add_poll(s->source, &s->pollfd_ipv4);
998     
999     memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
1000     s->pollfd_ipv6.fd = s->fd_ipv6;
1001     s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
1002     g_source_add_poll(s->source, &s->pollfd_ipv6);
1003
1004     g_source_attach(s->source, s->context);
1005     
1006     s->callback = callback;
1007     s->userdata = userdata;
1008     
1009     AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
1010     s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
1011     AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
1012
1013     AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, s->record_browsers);
1014     s->record_browser_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
1015     AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers);
1016     AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers);
1017     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers);
1018     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers);
1019     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers);
1020     AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, s->service_resolvers);
1021
1022     /* Get host name */
1023     s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
1024     s->host_name[strcspn(s->host_name, ".")] = 0;
1025     s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : g_strdup("local.");
1026     s->host_name_fqdn = NULL;
1027     update_fqdn(s);
1028
1029     s->record_list = avahi_record_list_new();
1030
1031     s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
1032     s->register_time_event = NULL;
1033     
1034     s->state = AVAHI_SERVER_INVALID;
1035
1036     s->monitor = avahi_interface_monitor_new(s);
1037     avahi_interface_monitor_sync(s->monitor);
1038
1039     register_localhost(s);
1040
1041     s->hinfo_entry_group = NULL;
1042     s->browse_domain_entry_group = NULL;
1043     register_stuff(s);
1044     
1045     return s;
1046 }
1047
1048 void avahi_server_free(AvahiServer* s) {
1049     g_assert(s);
1050
1051     while(s->entries)
1052         free_entry(s, s->entries);
1053
1054     avahi_interface_monitor_free(s->monitor);
1055
1056     while (s->groups)
1057         free_group(s, s->groups);
1058
1059     while (s->host_name_resolvers)
1060         avahi_host_name_resolver_free(s->host_name_resolvers);
1061     while (s->address_resolvers)
1062         avahi_address_resolver_free(s->address_resolvers);
1063     while (s->domain_browsers)
1064         avahi_domain_browser_free(s->domain_browsers);
1065     while (s->service_type_browsers)
1066         avahi_service_type_browser_free(s->service_type_browsers);
1067     while (s->service_browsers)
1068         avahi_service_browser_free(s->service_browsers);
1069     while (s->service_resolvers)
1070         avahi_service_resolver_free(s->service_resolvers);
1071     while (s->record_browsers)
1072         avahi_record_browser_destroy(s->record_browsers);
1073     g_hash_table_destroy(s->record_browser_hashtable);
1074
1075     g_hash_table_destroy(s->entries_by_key);
1076
1077     if (s->register_time_event)
1078         avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
1079     avahi_time_event_queue_free(s->time_event_queue);
1080
1081     avahi_record_list_free(s->record_list);
1082     
1083     if (s->fd_ipv4 >= 0)
1084         close(s->fd_ipv4);
1085     if (s->fd_ipv6 >= 0)
1086         close(s->fd_ipv6);
1087
1088     g_free(s->host_name);
1089     g_free(s->domain_name);
1090     g_free(s->host_name_fqdn);
1091
1092     g_source_destroy(s->source);
1093     g_source_unref(s->source);
1094     g_main_context_unref(s->context);
1095
1096     avahi_server_config_free(&s->config);
1097
1098     g_free(s);
1099 }
1100
1101 void avahi_server_add(
1102     AvahiServer *s,
1103     AvahiEntryGroup *g,
1104     gint interface,
1105     guchar protocol,
1106     AvahiEntryFlags flags,
1107     AvahiRecord *r) {
1108     
1109     AvahiEntry *e, *t;
1110     g_assert(s);
1111     g_assert(r);
1112
1113     g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
1114
1115     e = g_new(AvahiEntry, 1);
1116     e->server = s;
1117     e->record = avahi_record_ref(r);
1118     e->group = g;
1119     e->interface = interface;
1120     e->protocol = protocol;
1121     e->flags = flags;
1122     e->dead = FALSE;
1123
1124     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
1125
1126     AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
1127
1128     /* Insert into hash table indexed by name */
1129     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
1130     AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
1131     g_hash_table_replace(s->entries_by_key, e->record->key, t);
1132
1133     /* Insert into group list */
1134     if (g)
1135         AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); 
1136
1137     avahi_announce_entry(s, e);
1138 }
1139 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
1140     AvahiEntry **e = (AvahiEntry**) state;
1141     g_assert(s);
1142     g_assert(e);
1143
1144     if (!*e)
1145         *e = g ? g->entries : s->entries;
1146     
1147     while (*e && (*e)->dead)
1148         *e = g ? (*e)->by_group_next : (*e)->entries_next;
1149         
1150     if (!*e)
1151         return NULL;
1152
1153     return avahi_record_ref((*e)->record);
1154 }
1155
1156 void avahi_server_dump(AvahiServer *s, FILE *f) {
1157     AvahiEntry *e;
1158     g_assert(s);
1159     g_assert(f);
1160
1161     fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
1162
1163     for (e = s->entries; e; e = e->entries_next) {
1164         gchar *t;
1165
1166         if (e->dead)
1167             continue;
1168         
1169         t = avahi_record_to_string(e->record);
1170         fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
1171         g_free(t);
1172     }
1173
1174     avahi_dump_caches(s->monitor, f);
1175 }
1176
1177 void avahi_server_add_ptr(
1178     AvahiServer *s,
1179     AvahiEntryGroup *g,
1180     gint interface,
1181     guchar protocol,
1182     AvahiEntryFlags flags,
1183     const gchar *name,
1184     const gchar *dest) {
1185
1186     AvahiRecord *r;
1187
1188     g_assert(dest);
1189
1190     r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
1191     r->data.ptr.name = avahi_normalize_name(dest);
1192     avahi_server_add(s, g, interface, protocol, flags, r);
1193     avahi_record_unref(r);
1194 }
1195
1196 void avahi_server_add_address(
1197     AvahiServer *s,
1198     AvahiEntryGroup *g,
1199     gint interface,
1200     guchar protocol,
1201     AvahiEntryFlags flags,
1202     const gchar *name,
1203     AvahiAddress *a) {
1204
1205     gchar *n = NULL;
1206     g_assert(s);
1207     g_assert(a);
1208
1209     name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn;
1210     
1211     if (a->family == AF_INET) {
1212         gchar *reverse;
1213         AvahiRecord  *r;
1214
1215         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
1216         r->data.a.address = a->data.ipv4;
1217         avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
1218         avahi_record_unref(r);
1219         
1220         reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
1221         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1222         g_free(reverse);
1223         
1224     } else {
1225         gchar *reverse;
1226         AvahiRecord *r;
1227             
1228         r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
1229         r->data.aaaa.address = a->data.ipv6;
1230         avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
1231         avahi_record_unref(r);
1232
1233         reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
1234         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1235         g_free(reverse);
1236     
1237         reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
1238         avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
1239         g_free(reverse);
1240     }
1241     
1242     g_free(n);
1243 }
1244
1245 void avahi_server_add_text_strlst(
1246     AvahiServer *s,
1247     AvahiEntryGroup *g,
1248     gint interface,
1249     guchar protocol,
1250     AvahiEntryFlags flags,
1251     const gchar *name,
1252     AvahiStringList *strlst) {
1253
1254     AvahiRecord *r;
1255     
1256     g_assert(s);
1257     
1258     r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
1259     r->data.txt.string_list = strlst;
1260     avahi_server_add(s, g, interface, protocol, flags, r);
1261     avahi_record_unref(r);
1262 }
1263
1264 void avahi_server_add_text_va(
1265     AvahiServer *s,
1266     AvahiEntryGroup *g,
1267     gint interface,
1268     guchar protocol,
1269     AvahiEntryFlags flags,
1270     const gchar *name,
1271     va_list va) {
1272     
1273     g_assert(s);
1274
1275     avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
1276 }
1277
1278 void avahi_server_add_text(
1279     AvahiServer *s,
1280     AvahiEntryGroup *g,
1281     gint interface,
1282     guchar protocol,
1283     AvahiEntryFlags flags,
1284     const gchar *name,
1285     ...) {
1286
1287     va_list va;
1288     
1289     g_assert(s);
1290
1291     va_start(va, name);
1292     avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
1293     va_end(va);
1294 }
1295
1296 static void escape_service_name(gchar *d, guint size, const gchar *s) {
1297     g_assert(d);
1298     g_assert(size);
1299     g_assert(s);
1300
1301     while (*s && size >= 2) {
1302         if (*s == '.' || *s == '\\') {
1303             if (size < 3)
1304                 break;
1305
1306             *(d++) = '\\';
1307             size--;
1308         }
1309             
1310         *(d++) = *(s++);
1311         size--;
1312     }
1313
1314     g_assert(size > 0);
1315     *(d++) = 0;
1316 }
1317
1318 void avahi_server_add_service_strlst(
1319     AvahiServer *s,
1320     AvahiEntryGroup *g,
1321     gint interface,
1322     guchar protocol,
1323     const gchar *type,
1324     const gchar *name,
1325     const gchar *domain,
1326     const gchar *host,
1327     guint16 port,
1328     AvahiStringList *strlst) {
1329
1330     gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
1331     AvahiRecord *r;
1332     
1333     g_assert(s);
1334     g_assert(type);
1335     g_assert(name);
1336
1337     escape_service_name(ename, sizeof(ename), name);
1338
1339     if (domain) {
1340         while (domain[0] == '.')
1341             domain++;
1342     } else
1343         domain = s->domain_name;
1344
1345     if (!host)
1346         host = s->host_name_fqdn;
1347
1348     snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
1349     snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
1350     
1351     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
1352
1353     r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
1354     r->data.srv.priority = 0;
1355     r->data.srv.weight = 0;
1356     r->data.srv.port = port;
1357     r->data.srv.name = avahi_normalize_name(host);
1358     avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
1359     avahi_record_unref(r);
1360
1361     avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
1362
1363     snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
1364     avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
1365 }
1366
1367 void avahi_server_add_service_va(
1368     AvahiServer *s,
1369     AvahiEntryGroup *g,
1370     gint interface,
1371     guchar protocol,
1372     const gchar *type,
1373     const gchar *name,
1374     const gchar *domain,
1375     const gchar *host,
1376     guint16 port,
1377     va_list va){
1378
1379     g_assert(s);
1380     g_assert(type);
1381     g_assert(name);
1382
1383     avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
1384 }
1385
1386 void avahi_server_add_service(
1387     AvahiServer *s,
1388     AvahiEntryGroup *g,
1389     gint interface,
1390     guchar protocol,
1391     const gchar *type,
1392     const gchar *name,
1393     const gchar *domain,
1394     const gchar *host,
1395     guint16 port,
1396     ... ){
1397
1398     va_list va;
1399     
1400     g_assert(s);
1401     g_assert(type);
1402     g_assert(name);
1403
1404     va_start(va, port);
1405     avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
1406     va_end(va);
1407 }
1408
1409 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
1410     AvahiKey *k = userdata;
1411
1412     g_assert(m);
1413     g_assert(i);
1414     g_assert(k);
1415
1416     avahi_interface_post_query(i, k, FALSE);
1417 }
1418
1419 void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
1420     g_assert(s);
1421     g_assert(key);
1422
1423     avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
1424 }
1425
1426 void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
1427     g_assert(g);
1428
1429     if (g->state == state)
1430         return;
1431
1432     g->state = state;
1433     
1434     if (g->callback) {
1435         g->callback(g->server, g, state, g->userdata);
1436         return;
1437     }
1438 }
1439
1440 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) {
1441     AvahiEntryGroup *g;
1442     
1443     g_assert(s);
1444
1445     g = g_new(AvahiEntryGroup, 1);
1446     g->server = s;
1447     g->callback = callback;
1448     g->userdata = userdata;
1449     g->dead = FALSE;
1450     g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1451     g->n_probing = 0;
1452     AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1453
1454     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
1455     return g;
1456 }
1457
1458 void avahi_entry_group_free(AvahiEntryGroup *g) {
1459     AvahiEntry *e;
1460     
1461     g_assert(g);
1462     g_assert(g->server);
1463
1464     for (e = g->entries; e; e = e->by_group_next) {
1465         avahi_goodbye_entry(g->server, e, TRUE);
1466         e->dead = TRUE;
1467     }
1468
1469     g->dead = TRUE;
1470     
1471     g->server->need_group_cleanup = TRUE;
1472     g->server->need_entry_cleanup = TRUE;
1473 }
1474
1475 void avahi_entry_group_commit(AvahiEntryGroup *g) {
1476     g_assert(g);
1477     g_assert(!g->dead);
1478
1479     if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
1480         return;
1481
1482     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1483     avahi_announce_group(g->server, g);
1484     avahi_entry_group_check_probed(g, FALSE);
1485 }
1486
1487 gboolean avahi_entry_commited(AvahiEntry *e) {
1488     g_assert(e);
1489     g_assert(!e->dead);
1490
1491     return !e->group ||
1492         e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1493         e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1494 }
1495
1496 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {
1497     g_assert(g);
1498     g_assert(!g->dead);
1499
1500     return g->state;
1501 }
1502
1503 void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata) {
1504     g_assert(g);
1505
1506     g->userdata = userdata;
1507 }
1508
1509 gpointer avahi_entry_group_get_data(AvahiEntryGroup *g) {
1510     g_assert(g);
1511
1512     return g->userdata;
1513 }
1514
1515 const gchar* avahi_server_get_domain_name(AvahiServer *s) {
1516     g_assert(s);
1517
1518     return s->domain_name;
1519 }
1520
1521 const gchar* avahi_server_get_host_name(AvahiServer *s) {
1522     g_assert(s);
1523
1524     return s->host_name;
1525 }
1526
1527 const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s) {
1528     g_assert(s);
1529
1530     return s->host_name_fqdn;
1531 }
1532
1533 gpointer avahi_server_get_data(AvahiServer *s) {
1534     g_assert(s);
1535
1536     return s->userdata;
1537 }
1538
1539 void avahi_server_set_data(AvahiServer *s, gpointer userdata) {
1540     g_assert(s);
1541
1542     s->userdata = userdata;
1543 }
1544
1545 AvahiServerState avahi_server_get_state(AvahiServer *s) {
1546     g_assert(s);
1547
1548     return s->state;
1549 }
1550
1551 AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) {
1552     g_assert(c);
1553
1554     memset(c, 0, sizeof(AvahiServerConfig));
1555     c->register_hinfo = TRUE;
1556     c->register_addresses = TRUE;
1557     c->use_ipv6 = TRUE;
1558     c->use_ipv4 = TRUE;
1559     c->host_name = NULL;
1560     c->domain_name = NULL;
1561     c->check_response_ttl = TRUE;
1562     c->announce_domain = TRUE;
1563     c->use_iff_running = FALSE;
1564     c->enable_reflector = FALSE;
1565     
1566     return c;
1567 }
1568
1569 void avahi_server_config_free(AvahiServerConfig *c) {
1570     g_assert(c);
1571
1572     g_free(c->host_name);
1573     g_free(c->domain_name);
1574 }
1575
1576 AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) {
1577     g_assert(ret);
1578     g_assert(c);
1579
1580     *ret = *c;
1581
1582     ret->host_name = g_strdup(c->host_name);
1583     ret->domain_name = g_strdup(c->domain_name);
1584
1585     return ret;
1586 }