]> git.meshlink.io Git - catta/blob - server.c
* Complete conflict detection stuff (including probing et al)
[catta] / server.c
1 #include <sys/socket.h>
2 #include <arpa/inet.h>
3 #include <string.h>
4 #include <sys/utsname.h>
5 #include <unistd.h>
6
7 #include "server.h"
8 #include "util.h"
9 #include "iface.h"
10 #include "socket.h"
11 #include "subscribe.h"
12
13 static void free_entry(flxServer*s, flxEntry *e) {
14     flxEntry *t;
15
16     g_assert(s);
17     g_assert(e);
18
19     flx_goodbye_entry(s, e, TRUE);
20
21     /* Remove from linked list */
22     FLX_LLIST_REMOVE(flxEntry, entries, s->entries, e);
23
24     /* Remove from hash table indexed by name */
25     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
26     FLX_LLIST_REMOVE(flxEntry, by_key, t, e);
27     if (t)
28         g_hash_table_replace(s->entries_by_key, t->record->key, t);
29     else
30         g_hash_table_remove(s->entries_by_key, e->record->key);
31
32     /* Remove from associated group */
33     if (e->group)
34         FLX_LLIST_REMOVE(flxEntry, by_group, e->group->entries, e);
35
36     flx_record_unref(e->record);
37     g_free(e);
38 }
39
40 static void free_group(flxServer *s, flxEntryGroup *g) {
41     g_assert(s);
42     g_assert(g);
43
44     while (g->entries)
45         free_entry(s, g->entries);
46
47     FLX_LLIST_REMOVE(flxEntryGroup, groups, s->groups, g);
48     g_free(g);
49 }
50
51 static void cleanup_dead(flxServer *s) {
52     flxEntryGroup *g, *ng;
53     flxEntry *e, *ne;
54     g_assert(s);
55
56
57     if (s->need_group_cleanup) {
58         for (g = s->groups; g; g = ng) {
59             ng = g->groups_next;
60             
61             if (g->dead)
62                 free_group(s, g);
63         }
64
65         s->need_group_cleanup = FALSE;
66     }
67
68     if (s->need_entry_cleanup) {
69         for (e = s->entries; e; e = ne) {
70             ne = e->entries_next;
71             
72             if (e->dead)
73                 free_entry(s, e);
74         }
75
76         s->need_entry_cleanup = FALSE;
77     }
78 }
79
80 static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) {
81     flxEntry *e;
82     gchar *txt;
83     
84     g_assert(s);
85     g_assert(k);
86     g_assert(i);
87     g_assert(a);
88
89     g_message("Handling query: %s", txt = flx_key_to_string(k));
90     g_free(txt);
91
92     flx_packet_scheduler_incoming_query(i->scheduler, k);
93
94     if (k->type == FLX_DNS_TYPE_ANY) {
95
96         /* Handle ANY query */
97         
98         for (e = s->entries; e; e = e->entries_next)
99             if (!e->dead && flx_key_pattern_match(k, e->record->key) && flx_entry_registered(s, e, i))
100                 flx_interface_post_response(i, a, e->record, e->flags & FLX_ENTRY_UNIQUE, FALSE);
101     } else {
102
103         /* Handle all other queries */
104         
105         for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
106             if (!e->dead && flx_entry_registered(s, e, i))
107                 flx_interface_post_response(i, a, e->record, e->flags & FLX_ENTRY_UNIQUE, FALSE);
108     }
109 }
110
111 static void withdraw_entry(flxServer *s, flxEntry *e) {
112     g_assert(s);
113     g_assert(e);
114
115     e->dead = TRUE;
116     s->need_entry_cleanup = TRUE;
117
118     flx_goodbye_entry(s, e, FALSE);
119     
120     if (e->group)
121         flx_entry_group_run_callback(e->group, FLX_ENTRY_GROUP_COLLISION);
122 }
123
124 static void incoming_probe(flxServer *s, flxRecord *record, flxInterface *i) {
125     flxEntry *e, *n;
126     gchar *t;
127     
128     g_assert(s);
129     g_assert(record);
130     g_assert(i);
131
132     t = flx_record_to_string(record);
133
134     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
135         n = e->by_key_next;
136         
137         if (e->dead || !flx_record_equal_no_ttl(record, e->record))
138             continue;
139
140         if (flx_entry_registering(s, e, i)) {
141             
142             if (flx_record_lexicographical_compare(record, e->record) > 0) {
143                 withdraw_entry(s, e);
144                 g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
145             } else
146                 g_message("Recieved conflicting probe [%s]. Local host won.", t);
147         }
148     }
149
150     g_free(t);
151 }
152
153 static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
154     guint n;
155     
156     g_assert(s);
157     g_assert(p);
158     g_assert(i);
159     g_assert(a);
160
161     /* Handle the questions */
162     for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT); n > 0; n --) {
163         flxKey *key;
164
165         if (!(key = flx_dns_packet_consume_key(p))) {
166             g_warning("Packet too short (1)");
167             return;
168         }
169
170         handle_query_key(s, key, i, a);
171         flx_key_unref(key);
172     }
173
174     /* Known Answer Suppresion */
175     for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT); n > 0; n --) {
176         flxRecord *record;
177         gboolean unique = FALSE;
178
179         if (!(record = flx_dns_packet_consume_record(p, &unique))) {
180             g_warning("Packet too short (2)");
181             return;
182         }
183
184         flx_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
185         flx_record_unref(record);
186     }
187
188     /* Probe record */
189     for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_NSCOUNT); n > 0; n --) {
190         flxRecord *record;
191         gboolean unique = FALSE;
192
193         if (!(record = flx_dns_packet_consume_record(p, &unique))) {
194             g_warning("Packet too short (3)");
195             return;
196         }
197
198         if (record->key->type != FLX_DNS_TYPE_ANY)
199             incoming_probe(s, record, i);
200         
201         flx_record_unref(record);
202     }
203 }
204
205 static gboolean handle_conflict(flxServer *s, flxInterface *i, flxRecord *record, const flxAddress *a) {
206     gboolean valid = TRUE;
207     flxEntry *e, *n;
208     gchar *t;
209     
210     g_assert(s);
211     g_assert(i);
212     g_assert(record);
213
214     t = flx_record_to_string(record);
215
216     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
217         n = e->by_key_next;
218
219         if (e->dead)
220             continue;
221         
222         if (flx_entry_registered(s, e, i)) {
223
224             gboolean equal = flx_record_equal_no_ttl(record, e->record);
225                 
226             /* Check whether there is a unique record conflict */
227             if (!equal && (e->flags & FLX_ENTRY_UNIQUE)) {
228                 
229                 /* The lexicographically later data wins. */
230                 if (flx_record_lexicographical_compare(record, e->record) > 0) {
231                     withdraw_entry(s, e);
232                     g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t);
233                 } else {
234                     /* Tell the other host that our entry is lexicographically later */
235                     valid = FALSE;
236                     flx_interface_post_response(i, a, e->record, e->flags & FLX_ENTRY_UNIQUE, TRUE);
237                     g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t);
238                 }
239                 
240                 /* Check wheter there is a TTL conflict */
241             } else if (equal && record->ttl <= e->record->ttl/2) {
242                 /* Correct the TTL */
243                 valid = FALSE;
244                 flx_interface_post_response(i, a, e->record, e->flags & FLX_ENTRY_UNIQUE, TRUE);
245                 g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
246             }
247         }
248     }
249
250     g_free(t);
251
252     return valid;
253 }
254
255 static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) {
256     guint n;
257     
258     g_assert(s);
259     g_assert(p);
260     g_assert(i);
261     g_assert(a);
262     
263     for (n = flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT) +
264              flx_dns_packet_get_field(p, FLX_DNS_FIELD_ARCOUNT); n > 0; n--) {
265         flxRecord *record;
266         gboolean cache_flush = FALSE;
267         gchar *txt;
268         
269         if (!(record = flx_dns_packet_consume_record(p, &cache_flush))) {
270             g_warning("Packet too short (4)");
271             return;
272         }
273
274         if (record->key->type != FLX_DNS_TYPE_ANY) {
275             continue;
276         
277             g_message("Handling response: %s", txt = flx_record_to_string(record));
278             g_free(txt);
279             
280             if (handle_conflict(s, i, record, a)) {
281                 flx_cache_update(i->cache, record, cache_flush, a);
282                 flx_packet_scheduler_incoming_response(i->scheduler, record);
283             }
284         }
285             
286         flx_record_unref(record);
287     }
288 }
289
290 static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
291     flxInterface *i;
292     flxAddress a;
293     
294     g_assert(s);
295     g_assert(p);
296     g_assert(sa);
297     g_assert(iface > 0);
298
299     g_message("new packet recieved.");
300
301     if (!(i = flx_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
302         g_warning("Recieved packet from invalid interface.");
303         return;
304     }
305
306     if (ttl != 255) {
307         g_warning("Recieved packet with invalid TTL on interface '%s.%i'.", i->hardware->name, i->protocol);
308         if (!s->ignore_bad_ttl)
309             return;
310     }
311
312     if (sa->sa_family == AF_INET6) {
313         static const unsigned char ipv4_in_ipv6[] = {
314             0x00, 0x00, 0x00, 0x00,
315             0x00, 0x00, 0x00, 0x00,
316             0xFF, 0xFF, 0xFF, 0xFF };
317
318         if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0) {
319
320             /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
321             return;
322         }
323     }
324
325     if (flx_dns_packet_check_valid(p) < 0) {
326         g_warning("Recieved invalid packet.");
327         return;
328     }
329
330     flx_address_from_sockaddr(sa, &a);
331
332     if (flx_dns_packet_is_query(p)) {
333
334         if (flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT) == 0 ||
335             flx_dns_packet_get_field(p, FLX_DNS_FIELD_ARCOUNT) != 0) {
336             g_warning("Invalid query packet.");
337             return;
338         }
339                 
340         handle_query(s, p, i, &a);    
341         g_message("Handled query");
342     } else {
343         if (flx_dns_packet_get_field(p, FLX_DNS_FIELD_QDCOUNT) != 0 ||
344             flx_dns_packet_get_field(p, FLX_DNS_FIELD_ANCOUNT) == 0 ||
345             flx_dns_packet_get_field(p, FLX_DNS_FIELD_NSCOUNT) != 0) {
346             g_warning("Invalid response packet.");
347             return;
348         }
349
350         handle_response(s, p, i, &a);
351         g_message("Handled response");
352     }
353 }
354
355 static void work(flxServer *s) {
356     struct sockaddr_in6 sa6;
357     struct sockaddr_in sa;
358     flxDnsPacket *p;
359     gint iface = -1;
360     guint8 ttl;
361         
362     g_assert(s);
363
364     if (s->pollfd_ipv4.revents & G_IO_IN) {
365         if ((p = flx_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
366             dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
367             flx_dns_packet_free(p);
368         }
369     }
370
371     if (s->pollfd_ipv6.revents & G_IO_IN) {
372         if ((p = flx_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
373             dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
374             flx_dns_packet_free(p);
375         }
376     }
377 }
378
379 static gboolean prepare_func(GSource *source, gint *timeout) {
380     g_assert(source);
381     g_assert(timeout);
382     
383     *timeout = -1;
384     return FALSE;
385 }
386
387 static gboolean check_func(GSource *source) {
388     flxServer* s;
389     g_assert(source);
390
391     s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
392     g_assert(s);
393     
394     return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
395 }
396
397 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
398     flxServer* s;
399     g_assert(source);
400
401     s = *((flxServer**) (((guint8*) source) + sizeof(GSource)));
402     g_assert(s);
403
404     work(s);
405     cleanup_dead(s);
406
407     return TRUE;
408 }
409
410 static void add_default_entries(flxServer *s) {
411     gint length = 0;
412     struct utsname utsname;
413     gchar *hinfo;
414     flxAddress a;
415     flxRecord *r;
416     
417     g_assert(s);
418     
419     /* Fill in HINFO rr */
420     r = flx_record_new_full(s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_HINFO);
421     uname(&utsname);
422     r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
423     r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
424     flx_server_add(s, NULL, 0, AF_UNSPEC, FLX_ENTRY_UNIQUE | FLX_ENTRY_NOANNOUNCE | FLX_ENTRY_NOPROBE, r);
425     flx_record_unref(r);
426
427     /* Add localhost entries */
428     flx_address_parse("127.0.0.1", AF_INET, &a);
429     flx_server_add_address(s, NULL, 0, AF_UNSPEC, FLX_ENTRY_UNIQUE|FLX_ENTRY_NOPROBE|FLX_ENTRY_NOANNOUNCE, "localhost", &a);
430
431     flx_address_parse("::1", AF_INET6, &a);
432     flx_server_add_address(s, NULL, 0, AF_UNSPEC, FLX_ENTRY_UNIQUE|FLX_ENTRY_NOPROBE|FLX_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
433 }
434
435 flxServer *flx_server_new(GMainContext *c) {
436     gchar *hn, *e;
437     flxServer *s;
438     
439     static GSourceFuncs source_funcs = {
440         prepare_func,
441         check_func,
442         dispatch_func,
443         NULL,
444         NULL,
445         NULL
446     };
447
448     s = g_new(flxServer, 1);
449
450     s->ignore_bad_ttl = FALSE;
451     s->need_entry_cleanup = s->need_group_cleanup = FALSE;
452     
453     s->fd_ipv4 = flx_open_socket_ipv4();
454     s->fd_ipv6 = -1 /*flx_open_socket_ipv6() */; 
455     
456     if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
457         g_critical("Failed to create IP sockets.\n");
458         g_free(s);
459         return NULL;
460     }
461
462     if (s->fd_ipv4 < 0)
463         g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
464     else if (s->fd_ipv6 < 0)
465         g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
466     
467     if (c)
468         g_main_context_ref(s->context = c);
469     else
470         s->context = g_main_context_default();
471     
472     FLX_LLIST_HEAD_INIT(flxEntry, s->entries);
473     s->entries_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
474     FLX_LLIST_HEAD_INIT(flxGroup, s->groups);
475
476     FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions);
477     s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
478
479     /* Get host name */
480     hn = flx_get_host_name();
481     hn[strcspn(hn, ".")] = 0;
482
483     s->hostname = g_strdup_printf("%s.local.", hn);
484     g_free(hn);
485
486     s->time_event_queue = flx_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
487     s->monitor = flx_interface_monitor_new(s);
488     flx_interface_monitor_sync(s->monitor);
489     add_default_entries(s);
490     
491     /* Prepare IO source registration */
492     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(flxServer*));
493     *((flxServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
494
495     memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
496     s->pollfd_ipv4.fd = s->fd_ipv4;
497     s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
498     g_source_add_poll(s->source, &s->pollfd_ipv4);
499     
500     memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
501     s->pollfd_ipv6.fd = s->fd_ipv6;
502     s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
503     g_source_add_poll(s->source, &s->pollfd_ipv6);
504
505     g_source_attach(s->source, s->context);
506
507     return s;
508 }
509
510 void flx_server_free(flxServer* s) {
511     g_assert(s);
512
513     while(s->entries)
514         free_entry(s, s->entries);
515
516     flx_interface_monitor_free(s->monitor);
517
518     while (s->groups)
519         free_group(s, s->groups);
520
521     while (s->subscriptions)
522         flx_subscription_free(s->subscriptions);
523     g_hash_table_destroy(s->subscription_hashtable);
524
525     g_hash_table_destroy(s->entries_by_key);
526
527     flx_time_event_queue_free(s->time_event_queue);
528
529     if (s->fd_ipv4 >= 0)
530         close(s->fd_ipv4);
531     if (s->fd_ipv6 >= 0)
532         close(s->fd_ipv6);
533     
534     g_free(s->hostname);
535
536     g_source_destroy(s->source);
537     g_source_unref(s->source);
538     g_main_context_unref(s->context);
539
540     g_free(s);
541 }
542
543 void flx_server_add(
544     flxServer *s,
545     flxEntryGroup *g,
546     gint interface,
547     guchar protocol,
548     flxEntryFlags flags,
549     flxRecord *r) {
550     
551     flxEntry *e, *t;
552     g_assert(s);
553     g_assert(r);
554
555     g_assert(r->key->type != FLX_DNS_TYPE_ANY);
556
557     e = g_new(flxEntry, 1);
558     e->server = s;
559     e->record = flx_record_ref(r);
560     e->group = g;
561     e->interface = interface;
562     e->protocol = protocol;
563     e->flags = flags;
564     e->dead = FALSE;
565
566     FLX_LLIST_HEAD_INIT(flxAnnouncement, e->announcements);
567
568     FLX_LLIST_PREPEND(flxEntry, entries, s->entries, e);
569
570     /* Insert into hash table indexed by name */
571     t = g_hash_table_lookup(s->entries_by_key, e->record->key);
572     FLX_LLIST_PREPEND(flxEntry, by_key, t, e);
573     g_hash_table_replace(s->entries_by_key, e->record->key, t);
574
575     /* Insert into group list */
576     if (g)
577         FLX_LLIST_PREPEND(flxEntry, by_group, g->entries, e); 
578
579     flx_announce_entry(s, e);
580 }
581 const flxRecord *flx_server_iterate(flxServer *s, flxEntryGroup *g, void **state) {
582     flxEntry **e = (flxEntry**) state;
583     g_assert(s);
584     g_assert(e);
585
586     if (!*e)
587         *e = g ? g->entries : s->entries;
588     
589     while (*e && (*e)->dead)
590         *e = g ? (*e)->by_group_next : (*e)->entries_next;
591         
592     if (!*e)
593         return NULL;
594
595     return flx_record_ref((*e)->record);
596 }
597
598 void flx_server_dump(flxServer *s, FILE *f) {
599     flxEntry *e;
600     g_assert(s);
601     g_assert(f);
602
603     fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
604
605     for (e = s->entries; e; e = e->entries_next) {
606         gchar *t;
607
608         if (e->dead)
609             continue;
610         
611         t = flx_record_to_string(e->record);
612         fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
613         g_free(t);
614     }
615
616     flx_dump_caches(s->monitor, f);
617 }
618
619 void flx_server_add_ptr(
620     flxServer *s,
621     flxEntryGroup *g,
622     gint interface,
623     guchar protocol,
624     flxEntryFlags flags,
625     const gchar *name,
626     const gchar *dest) {
627
628     flxRecord *r;
629
630     g_assert(dest);
631
632     r = flx_record_new_full(name ? name : s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_PTR);
633     r->data.ptr.name = flx_normalize_name(dest);
634     flx_server_add(s, g, interface, protocol, flags, r);
635     flx_record_unref(r);
636 }
637
638 void flx_server_add_address(
639     flxServer *s,
640     flxEntryGroup *g,
641     gint interface,
642     guchar protocol,
643     flxEntryFlags flags,
644     const gchar *name,
645     flxAddress *a) {
646
647     gchar *n = NULL;
648     g_assert(s);
649     g_assert(a);
650
651     name = name ? (n = flx_normalize_name(name)) : s->hostname;
652     
653     if (a->family == AF_INET) {
654         gchar *reverse;
655         flxRecord  *r;
656
657         r = flx_record_new_full(name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_A);
658         r->data.a.address = a->data.ipv4;
659         flx_server_add(s, g, interface, protocol, flags, r);
660         flx_record_unref(r);
661         
662         reverse = flx_reverse_lookup_name_ipv4(&a->data.ipv4);
663         g_assert(reverse);
664         flx_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
665         g_free(reverse);
666         
667     } else {
668         gchar *reverse;
669         flxRecord *r;
670             
671         r = flx_record_new_full(name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_AAAA);
672         r->data.aaaa.address = a->data.ipv6;
673         flx_server_add(s, g, interface, protocol, flags, r);
674         flx_record_unref(r);
675
676         reverse = flx_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
677         g_assert(reverse);
678         flx_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
679         g_free(reverse);
680     
681         reverse = flx_reverse_lookup_name_ipv6_int(&a->data.ipv6);
682         g_assert(reverse);
683         flx_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
684         g_free(reverse);
685     }
686     
687     g_free(n);
688 }
689
690 void flx_server_add_text_va(
691     flxServer *s,
692     flxEntryGroup *g,
693     gint interface,
694     guchar protocol,
695     flxEntryFlags flags,
696     const gchar *name,
697     va_list va) {
698
699     flxRecord *r;
700     
701     g_assert(s);
702     
703     r = flx_record_new_full(name ? name : s->hostname, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT);
704     r->data.txt.string_list = flx_string_list_new_va(va);
705     flx_server_add(s, g, interface, protocol, flags, r);
706     flx_record_unref(r);
707 }
708
709 void flx_server_add_text(
710     flxServer *s,
711     flxEntryGroup *g,
712     gint interface,
713     guchar protocol,
714     flxEntryFlags flags,
715     const gchar *name,
716     ...) {
717
718     va_list va;
719     
720     g_assert(s);
721
722     va_start(va, name);
723     flx_server_add_text_va(s, g, interface, protocol, flags, name, va);
724     va_end(va);
725 }
726
727 static void escape_service_name(gchar *d, guint size, const gchar *s) {
728     g_assert(d);
729     g_assert(size);
730     g_assert(s);
731
732     while (*s && size >= 2) {
733         if (*s == '.' || *s == '\\') {
734             if (size < 3)
735                 break;
736
737             *(d++) = '\\';
738             size--;
739         }
740             
741         *(d++) = *(s++);
742         size--;
743     }
744
745     g_assert(size > 0);
746     *(d++) = 0;
747 }
748
749
750 void flx_server_add_service_va(
751     flxServer *s,
752     flxEntryGroup *g,
753     gint interface,
754     guchar protocol,
755     const gchar *type,
756     const gchar *name,
757     const gchar *domain,
758     const gchar *host,
759     guint16 port,
760     va_list va) {
761
762     gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
763     flxRecord *r;
764     
765     g_assert(s);
766     g_assert(type);
767     g_assert(name);
768
769     escape_service_name(ename, sizeof(ename), name);
770
771     if (domain) {
772         while (domain[0] == '.')
773             domain++;
774     } else
775         domain = "local";
776
777     if (!host)
778         host = s->hostname;
779
780     snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
781     snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
782     
783     flx_server_add_ptr(s, g, interface, protocol, FALSE, ptr_name, svc_name);
784
785     r = flx_record_new_full(svc_name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_SRV);
786     r->data.srv.priority = 0;
787     r->data.srv.weight = 0;
788     r->data.srv.port = port;
789     r->data.srv.name = flx_normalize_name(host);
790     flx_server_add(s, g, interface, protocol, TRUE, r);
791     flx_record_unref(r);
792
793     flx_server_add_text_va(s, g, interface, protocol, FALSE, svc_name, va);
794
795     snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
796     flx_server_add_ptr(s, g, interface, protocol, FALSE, enum_ptr, ptr_name);
797 }
798
799 void flx_server_add_service(
800     flxServer *s,
801     flxEntryGroup *g,
802     gint interface,
803     guchar protocol,
804     const gchar *type,
805     const gchar *name,
806     const gchar *domain,
807     const gchar *host,
808     guint16 port,
809     ... ){
810
811     va_list va;
812     
813     g_assert(s);
814     g_assert(type);
815     g_assert(name);
816
817     va_start(va, port);
818     flx_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
819     va_end(va);
820 }
821
822 static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
823     flxKey *k = userdata;
824
825     g_assert(m);
826     g_assert(i);
827     g_assert(k);
828
829     flx_interface_post_query(i, k, FALSE);
830 }
831
832 void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) {
833     g_assert(s);
834     g_assert(key);
835
836     flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
837 }
838
839 struct tmpdata {
840     flxRecord *record;
841     gboolean flush_cache;
842 };
843
844 static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
845     struct tmpdata *tmpdata = userdata;
846
847     g_assert(m);
848     g_assert(i);
849     g_assert(tmpdata);
850
851     flx_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE);
852 }
853
854 void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record, gboolean flush_cache) {
855     struct tmpdata tmpdata;
856     
857     g_assert(s);
858     g_assert(record);
859
860     tmpdata.record = record;
861     tmpdata.flush_cache = flush_cache;
862
863     flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata);
864 }
865
866 void flx_entry_group_run_callback(flxEntryGroup *g, flxEntryGroupStatus status) {
867     g_assert(g);
868
869     if (g->callback) {
870         g->callback(g->server, g, status, g->userdata);
871         return;
872     }
873
874     if (status == FLX_ENTRY_GROUP_COLLISION)
875         flx_entry_group_free(g);
876
877     /* Ignore the rest */
878 }
879
880 flxEntryGroup *flx_entry_group_new(flxServer *s, flxEntryGroupCallback callback, gpointer userdata) {
881     flxEntryGroup *g;
882     
883     g_assert(s);
884
885     g = g_new(flxEntryGroup, 1);
886     g->server = s;
887     g->callback = callback;
888     g->userdata = userdata;
889     g->dead = FALSE;
890     g->status = FLX_ENTRY_GROUP_UNCOMMITED;
891     g->n_probing = 0;
892     FLX_LLIST_HEAD_INIT(flxEntry, g->entries);
893
894     FLX_LLIST_PREPEND(flxEntryGroup, groups, s->groups, g);
895     return g;
896 }
897
898 void flx_entry_group_free(flxEntryGroup *g) {
899     g_assert(g);
900     g_assert(g->server);
901
902     g->dead = TRUE;
903     g->server->need_group_cleanup = TRUE;
904 }
905
906 void flx_entry_group_commit(flxEntryGroup *g) {
907     flxEntry *e;
908     
909     g_assert(g);
910     g_assert(!g->dead);
911
912     if (g->status != FLX_ENTRY_GROUP_UNCOMMITED)
913         return;
914
915     flx_entry_group_run_callback(g, g->status = FLX_ENTRY_GROUP_REGISTERING);
916     flx_announce_group(g->server, g);
917     flx_entry_group_check_probed(g, FALSE);
918 }
919
920 gboolean flx_entry_commited(flxEntry *e) {
921     g_assert(e);
922     g_assert(!e->dead);
923
924     return !e->group ||
925         e->group->status == FLX_ENTRY_GROUP_REGISTERING ||
926         e->group->status == FLX_ENTRY_GROUP_ESTABLISHED;
927 }
928
929 flxEntryGroupStatus flx_entry_group_get_status(flxEntryGroup *g) {
930     g_assert(g);
931     g_assert(!g->dead);
932
933     return g->status;
934 }