]> git.meshlink.io Git - catta/blob - src/entry.c
Fix varargs warning for catta_server_add_service().
[catta] / src / entry.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include <arpa/inet.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35
36 #include <catta/domain.h>
37 #include <catta/timeval.h>
38 #include <catta/malloc.h>
39 #include <catta/error.h>
40 #include <catta/domain.h>
41 #include <catta/log.h>
42
43 #include "internal.h"
44 #include "iface.h"
45 #include "socket.h"
46 #include "browse.h"
47 #include "util.h"
48 #include "dns-srv-rr.h"
49 #include "rr-util.h"
50 #include "domain-util.h"
51
52 static void transport_flags_from_domain(CattaServer *s, CattaPublishFlags *flags, const char *domain) {
53     assert(flags);
54     assert(domain);
55
56     assert(!((*flags & CATTA_PUBLISH_USE_MULTICAST) && (*flags & CATTA_PUBLISH_USE_WIDE_AREA)));
57
58     if (*flags & (CATTA_PUBLISH_USE_MULTICAST|CATTA_PUBLISH_USE_WIDE_AREA))
59         return;
60
61     if (!s->wide_area_lookup_engine ||
62         !catta_wide_area_has_servers(s->wide_area_lookup_engine) ||
63         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_LOCAL) ||
64         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_ADDR_IPV4) ||
65         catta_domain_ends_with(domain, CATTA_MDNS_SUFFIX_ADDR_IPV6))
66         *flags |= CATTA_PUBLISH_USE_MULTICAST;
67     else
68         *flags |= CATTA_PUBLISH_USE_WIDE_AREA;
69 }
70
71 void catta_entry_free(CattaServer*s, CattaEntry *e) {
72     CattaEntry *t;
73
74     assert(s);
75     assert(e);
76
77     catta_goodbye_entry(s, e, 1, 1);
78
79     /* Remove from linked list */
80     CATTA_LLIST_REMOVE(CattaEntry, entries, s->entries, e);
81
82     /* Remove from hash table indexed by name */
83     t = catta_hashmap_lookup(s->entries_by_key, e->record->key);
84     CATTA_LLIST_REMOVE(CattaEntry, by_key, t, e);
85     if (t)
86         catta_hashmap_replace(s->entries_by_key, t->record->key, t);
87     else
88         catta_hashmap_remove(s->entries_by_key, e->record->key);
89
90     /* Remove from associated group */
91     if (e->group)
92         CATTA_LLIST_REMOVE(CattaEntry, by_group, e->group->entries, e);
93
94     catta_record_unref(e->record);
95     catta_free(e);
96 }
97
98 void catta_entry_group_free(CattaServer *s, CattaSEntryGroup *g) {
99     assert(s);
100     assert(g);
101
102     while (g->entries)
103         catta_entry_free(s, g->entries);
104
105     if (g->register_time_event)
106         catta_time_event_free(g->register_time_event);
107
108     CATTA_LLIST_REMOVE(CattaSEntryGroup, groups, s->groups, g);
109     catta_free(g);
110 }
111
112 void catta_cleanup_dead_entries(CattaServer *s) {
113     assert(s);
114
115     if (s->need_group_cleanup) {
116         CattaSEntryGroup *g, *next;
117
118         for (g = s->groups; g; g = next) {
119             next = g->groups_next;
120
121             if (g->dead)
122                 catta_entry_group_free(s, g);
123         }
124
125         s->need_group_cleanup = 0;
126     }
127
128     if (s->need_entry_cleanup) {
129         CattaEntry *e, *next;
130
131         for (e = s->entries; e; e = next) {
132             next = e->entries_next;
133
134             if (e->dead)
135                 catta_entry_free(s, e);
136         }
137
138         s->need_entry_cleanup = 0;
139     }
140
141     if (s->need_browser_cleanup)
142         catta_browser_cleanup(s);
143
144     if (s->cleanup_time_event) {
145         catta_time_event_free(s->cleanup_time_event);
146         s->cleanup_time_event = NULL;
147     }
148 }
149
150 static int check_record_conflict(CattaServer *s, CattaIfIndex iface, CattaProtocol protocol, CattaRecord *r, CattaPublishFlags flags) {
151     CattaEntry *e;
152
153     assert(s);
154     assert(r);
155
156     for (e = catta_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
157         if (e->dead)
158             continue;
159
160         if (!(flags & CATTA_PUBLISH_UNIQUE) && !(e->flags & CATTA_PUBLISH_UNIQUE))
161             continue;
162
163         if ((flags & CATTA_PUBLISH_ALLOW_MULTIPLE) && (e->flags & CATTA_PUBLISH_ALLOW_MULTIPLE) )
164             continue;
165
166         if (catta_record_equal_no_ttl(r, e->record)) {
167             /* The records are the same, not a conflict in any case */
168             continue;
169         }
170
171         if ((iface <= 0 ||
172              e->iface <= 0 ||
173              e->iface == iface) &&
174             (protocol == CATTA_PROTO_UNSPEC ||
175              e->protocol == CATTA_PROTO_UNSPEC ||
176              e->protocol == protocol))
177
178             return -1;
179     }
180
181     return 0;
182 }
183
184 static CattaEntry * server_add_internal(
185     CattaServer *s,
186     CattaSEntryGroup *g,
187     CattaIfIndex iface,
188     CattaProtocol protocol,
189     CattaPublishFlags flags,
190     CattaRecord *r) {
191
192     CattaEntry *e;
193
194     assert(s);
195     assert(r);
196
197     CATTA_CHECK_VALIDITY_RETURN_NULL(s, s->state != CATTA_SERVER_FAILURE && s->state != CATTA_SERVER_INVALID, CATTA_ERR_BAD_STATE);
198     CATTA_CHECK_VALIDITY_RETURN_NULL(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
199     CATTA_CHECK_VALIDITY_RETURN_NULL(s, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
200     CATTA_CHECK_VALIDITY_RETURN_NULL(s, CATTA_FLAGS_VALID(
201                                          flags,
202                                          CATTA_PUBLISH_NO_ANNOUNCE|
203                                          CATTA_PUBLISH_NO_PROBE|
204                                          CATTA_PUBLISH_UNIQUE|
205                                          CATTA_PUBLISH_ALLOW_MULTIPLE|
206                                          CATTA_PUBLISH_UPDATE|
207                                          CATTA_PUBLISH_USE_WIDE_AREA|
208                                          CATTA_PUBLISH_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
209     CATTA_CHECK_VALIDITY_RETURN_NULL(s, catta_is_valid_domain_name(r->key->name), CATTA_ERR_INVALID_HOST_NAME);
210     CATTA_CHECK_VALIDITY_RETURN_NULL(s, r->ttl != 0, CATTA_ERR_INVALID_TTL);
211     CATTA_CHECK_VALIDITY_RETURN_NULL(s, !catta_key_is_pattern(r->key), CATTA_ERR_IS_PATTERN);
212     CATTA_CHECK_VALIDITY_RETURN_NULL(s, catta_record_is_valid(r), CATTA_ERR_INVALID_RECORD);
213     CATTA_CHECK_VALIDITY_RETURN_NULL(s, r->key->clazz == CATTA_DNS_CLASS_IN, CATTA_ERR_INVALID_DNS_CLASS);
214     CATTA_CHECK_VALIDITY_RETURN_NULL(s,
215                                      (r->key->type != 0) &&
216                                      (r->key->type != CATTA_DNS_TYPE_ANY) &&
217                                      (r->key->type != CATTA_DNS_TYPE_OPT) &&
218                                      (r->key->type != CATTA_DNS_TYPE_TKEY) &&
219                                      (r->key->type != CATTA_DNS_TYPE_TSIG) &&
220                                      (r->key->type != CATTA_DNS_TYPE_IXFR) &&
221                                      (r->key->type != CATTA_DNS_TYPE_AXFR), CATTA_ERR_INVALID_DNS_TYPE);
222
223     transport_flags_from_domain(s, &flags, r->key->name);
224     CATTA_CHECK_VALIDITY_RETURN_NULL(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
225     CATTA_CHECK_VALIDITY_RETURN_NULL(s, !s->config.disable_publishing, CATTA_ERR_NOT_PERMITTED);
226     CATTA_CHECK_VALIDITY_RETURN_NULL(s,
227                                      !g ||
228                                      (g->state != CATTA_ENTRY_GROUP_ESTABLISHED && g->state != CATTA_ENTRY_GROUP_REGISTERING) ||
229                                      (flags & CATTA_PUBLISH_UPDATE), CATTA_ERR_BAD_STATE);
230
231     if (flags & CATTA_PUBLISH_UPDATE) {
232         CattaRecord *old_record;
233         int is_first = 1;
234
235         /* Update and existing record */
236
237         /* Find the first matching entry */
238         for (e = catta_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
239             if (!e->dead && e->group == g && e->iface == iface && e->protocol == protocol)
240                 break;
241
242             is_first = 0;
243         }
244
245         /* Hmm, nothing found? */
246         if (!e) {
247             catta_server_set_errno(s, CATTA_ERR_NOT_FOUND);
248             return NULL;
249         }
250
251         /* Update the entry */
252         old_record = e->record;
253         e->record = catta_record_ref(r);
254         e->flags = flags;
255
256         /* Announce our changes when needed */
257         if (!catta_record_equal_no_ttl(old_record, r) && (!g || g->state != CATTA_ENTRY_GROUP_UNCOMMITED)) {
258
259             /* Remove the old entry from all caches, if needed */
260             if (!(e->flags & CATTA_PUBLISH_UNIQUE))
261                 catta_goodbye_entry(s, e, 1, 0);
262
263             /* Reannounce our updated entry */
264             catta_reannounce_entry(s, e);
265         }
266
267         /* If we were the first entry in the list, we need to update the key */
268         if (is_first)
269             catta_hashmap_replace(s->entries_by_key, e->record->key, e);
270
271         catta_record_unref(old_record);
272
273     } else {
274         CattaEntry *t;
275
276         /* Add a new record */
277
278         if (check_record_conflict(s, iface, protocol, r, flags) < 0) {
279             catta_server_set_errno(s, CATTA_ERR_COLLISION);
280             return NULL;
281         }
282
283         if (!(e = catta_new(CattaEntry, 1))) {
284             catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
285             return NULL;
286         }
287
288         e->server = s;
289         e->record = catta_record_ref(r);
290         e->group = g;
291         e->iface = iface;
292         e->protocol = protocol;
293         e->flags = flags;
294         e->dead = 0;
295
296         CATTA_LLIST_HEAD_INIT(CattaAnnouncer, e->announcers);
297
298         CATTA_LLIST_PREPEND(CattaEntry, entries, s->entries, e);
299
300         /* Insert into hash table indexed by name */
301         t = catta_hashmap_lookup(s->entries_by_key, e->record->key);
302         CATTA_LLIST_PREPEND(CattaEntry, by_key, t, e);
303         catta_hashmap_replace(s->entries_by_key, e->record->key, t);
304
305         /* Insert into group list */
306         if (g)
307             CATTA_LLIST_PREPEND(CattaEntry, by_group, g->entries, e);
308
309         catta_announce_entry(s, e);
310     }
311
312     return e;
313 }
314
315 int catta_server_add(
316     CattaServer *s,
317     CattaSEntryGroup *g,
318     CattaIfIndex iface,
319     CattaProtocol protocol,
320     CattaPublishFlags flags,
321     CattaRecord *r) {
322
323     if (!server_add_internal(s, g, iface, protocol, flags, r))
324         return catta_server_errno(s);
325
326     return CATTA_OK;
327 }
328
329 const CattaRecord *catta_server_iterate(CattaServer *s, CattaSEntryGroup *g, void **state) {
330     CattaEntry **e = (CattaEntry**) state;
331     assert(s);
332     assert(e);
333
334     if (!*e)
335         *e = g ? g->entries : s->entries;
336
337     while (*e && (*e)->dead)
338         *e = g ? (*e)->by_group_next : (*e)->entries_next;
339
340     if (!*e)
341         return NULL;
342
343     return catta_record_ref((*e)->record);
344 }
345
346 int catta_server_dump(CattaServer *s, CattaDumpCallback callback, void* userdata) {
347     CattaEntry *e;
348
349     assert(s);
350     assert(callback);
351
352     callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
353
354     for (e = s->entries; e; e = e->entries_next) {
355         char *t;
356         char ln[256];
357
358         if (e->dead)
359             continue;
360
361         if (!(t = catta_record_to_string(e->record)))
362             return catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
363
364         snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->iface, e->protocol);
365         catta_free(t);
366
367         callback(ln, userdata);
368     }
369
370     catta_dump_caches(s->monitor, callback, userdata);
371
372     if (s->wide_area_lookup_engine)
373         catta_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
374     return CATTA_OK;
375 }
376
377 static CattaEntry *server_add_ptr_internal(
378     CattaServer *s,
379     CattaSEntryGroup *g,
380     CattaIfIndex iface,
381     CattaProtocol protocol,
382     CattaPublishFlags flags,
383     uint32_t ttl,
384     const char *name,
385     const char *dest) {
386
387     CattaRecord *r;
388     CattaEntry *e;
389
390     assert(s);
391     assert(dest);
392
393     CATTA_CHECK_VALIDITY_RETURN_NULL(s, !name || catta_is_valid_domain_name(name), CATTA_ERR_INVALID_HOST_NAME);
394     CATTA_CHECK_VALIDITY_RETURN_NULL(s, catta_is_valid_domain_name(dest), CATTA_ERR_INVALID_HOST_NAME);
395
396     if (!name)
397         name = s->host_name_fqdn;
398
399     if (!(r = catta_record_new_full(name, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_PTR, ttl))) {
400         catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
401         return NULL;
402     }
403
404     r->data.ptr.name = catta_normalize_name_strdup(dest);
405     e = server_add_internal(s, g, iface, protocol, flags, r);
406     catta_record_unref(r);
407     return e;
408 }
409
410 int catta_server_add_ptr(
411     CattaServer *s,
412     CattaSEntryGroup *g,
413     CattaIfIndex iface,
414     CattaProtocol protocol,
415     CattaPublishFlags flags,
416     uint32_t ttl,
417     const char *name,
418     const char *dest) {
419
420     CattaEntry *e;
421
422     assert(s);
423
424     if (!(e = server_add_ptr_internal(s, g, iface, protocol, flags, ttl, name, dest)))
425         return catta_server_errno(s);
426
427     return CATTA_OK;
428 }
429
430 int catta_server_add_address(
431     CattaServer *s,
432     CattaSEntryGroup *g,
433     CattaIfIndex iface,
434     CattaProtocol protocol,
435     CattaPublishFlags flags,
436     const char *name,
437     CattaAddress *a) {
438
439     char n[CATTA_DOMAIN_NAME_MAX];
440     int ret = CATTA_OK;
441     CattaEntry *entry = NULL, *reverse = NULL;
442     CattaRecord  *r;
443
444     assert(s);
445     assert(a);
446
447     CATTA_CHECK_VALIDITY(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
448     CATTA_CHECK_VALIDITY(s, CATTA_PROTO_VALID(protocol) && CATTA_PROTO_VALID(a->proto), CATTA_ERR_INVALID_PROTOCOL);
449     CATTA_CHECK_VALIDITY(s, CATTA_FLAGS_VALID(flags,
450                                               CATTA_PUBLISH_NO_REVERSE|
451                                               CATTA_PUBLISH_NO_ANNOUNCE|
452                                               CATTA_PUBLISH_NO_PROBE|
453                                               CATTA_PUBLISH_UPDATE|
454                                               CATTA_PUBLISH_USE_WIDE_AREA|
455                                               CATTA_PUBLISH_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
456     CATTA_CHECK_VALIDITY(s, !name || catta_is_valid_fqdn(name), CATTA_ERR_INVALID_HOST_NAME);
457
458     /* Prepare the host naem */
459
460     if (!name)
461         name = s->host_name_fqdn;
462     else {
463         CATTA_ASSERT_TRUE(catta_normalize_name(name, n, sizeof(n)));
464         name = n;
465     }
466
467     transport_flags_from_domain(s, &flags, name);
468     CATTA_CHECK_VALIDITY(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
469
470     /* Create the A/AAAA record */
471
472     if (a->proto == CATTA_PROTO_INET) {
473
474         if (!(r = catta_record_new_full(name, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_A, CATTA_DEFAULT_TTL_HOST_NAME))) {
475             ret = catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
476             goto finish;
477         }
478
479         r->data.a.address = a->data.ipv4;
480
481     } else {
482         assert(a->proto == CATTA_PROTO_INET6);
483
484         if (!(r = catta_record_new_full(name, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_AAAA, CATTA_DEFAULT_TTL_HOST_NAME))) {
485             ret = catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
486             goto finish;
487         }
488
489         r->data.aaaa.address = a->data.ipv6;
490     }
491
492     entry = server_add_internal(s, g, iface, protocol, (flags & ~ CATTA_PUBLISH_NO_REVERSE) | CATTA_PUBLISH_UNIQUE | CATTA_PUBLISH_ALLOW_MULTIPLE, r);
493     catta_record_unref(r);
494
495     if (!entry) {
496         ret = catta_server_errno(s);
497         goto finish;
498     }
499
500     /* Create the reverse lookup entry */
501
502     if (!(flags & CATTA_PUBLISH_NO_REVERSE)) {
503         char reverse_n[CATTA_DOMAIN_NAME_MAX];
504         catta_reverse_lookup_name(a, reverse_n, sizeof(reverse_n));
505
506         if (!(reverse = server_add_ptr_internal(s, g, iface, protocol, flags | CATTA_PUBLISH_UNIQUE, CATTA_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
507             ret = catta_server_errno(s);
508             goto finish;
509         }
510     }
511
512 finish:
513
514     if (ret != CATTA_OK && !(flags & CATTA_PUBLISH_UPDATE)) {
515         if (entry)
516             catta_entry_free(s, entry);
517         if (reverse)
518             catta_entry_free(s, reverse);
519     }
520
521     return ret;
522 }
523
524 static CattaEntry *server_add_txt_strlst_nocopy(
525     CattaServer *s,
526     CattaSEntryGroup *g,
527     CattaIfIndex iface,
528     CattaProtocol protocol,
529     CattaPublishFlags flags,
530     uint32_t ttl,
531     const char *name,
532     CattaStringList *strlst) {
533
534     CattaRecord *r;
535     CattaEntry *e;
536
537     assert(s);
538
539     if (!(r = catta_record_new_full(name ? name : s->host_name_fqdn, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_TXT, ttl))) {
540         catta_string_list_free(strlst);
541         catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
542         return NULL;
543     }
544
545     r->data.txt.string_list = strlst;
546     e = server_add_internal(s, g, iface, protocol, flags, r);
547     catta_record_unref(r);
548
549     return e;
550 }
551
552 static CattaStringList *add_magic_cookie(
553     CattaServer *s,
554     CattaStringList *strlst) {
555
556     assert(s);
557
558     if (!s->config.add_service_cookie)
559         return strlst;
560
561     if (catta_string_list_find(strlst, CATTA_SERVICE_COOKIE))
562         /* This string list already contains a magic cookie */
563         return strlst;
564
565     return catta_string_list_add_printf(strlst, CATTA_SERVICE_COOKIE"=%u", s->local_service_cookie);
566 }
567
568 static int server_add_service_strlst_nocopy(
569     CattaServer *s,
570     CattaSEntryGroup *g,
571     CattaIfIndex iface,
572     CattaProtocol protocol,
573     CattaPublishFlags flags,
574     const char *name,
575     const char *type,
576     const char *domain,
577     const char *host,
578     uint16_t port,
579     CattaStringList *strlst) {
580
581     char ptr_name[CATTA_DOMAIN_NAME_MAX], svc_name[CATTA_DOMAIN_NAME_MAX], enum_ptr[CATTA_DOMAIN_NAME_MAX], *h = NULL;
582     CattaRecord *r = NULL;
583     int ret = CATTA_OK;
584     CattaEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
585
586     assert(s);
587     assert(type);
588     assert(name);
589
590     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
591     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
592     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_FLAGS_VALID(flags,
593                                                                 CATTA_PUBLISH_NO_COOKIE|
594                                                                 CATTA_PUBLISH_UPDATE|
595                                                                 CATTA_PUBLISH_USE_WIDE_AREA|
596                                                                 CATTA_PUBLISH_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
597     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_name(name), CATTA_ERR_INVALID_SERVICE_NAME);
598     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_type_strict(type), CATTA_ERR_INVALID_SERVICE_TYPE);
599     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
600     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || catta_is_valid_fqdn(host), CATTA_ERR_INVALID_HOST_NAME);
601
602     if (!domain)
603         domain = s->domain_name;
604
605     if (!host)
606         host = s->host_name_fqdn;
607
608     transport_flags_from_domain(s, &flags, domain);
609     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
610
611     if (!(h = catta_normalize_name_strdup(host))) {
612         ret = catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
613         goto fail;
614     }
615
616     if ((ret = catta_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
617         (ret = catta_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
618         (ret = catta_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
619         catta_server_set_errno(s, ret);
620         goto fail;
621     }
622
623     /* Add service enumeration PTR record */
624
625     if (!(ptr_entry = server_add_ptr_internal(s, g, iface, protocol, 0, CATTA_DEFAULT_TTL, ptr_name, svc_name))) {
626         ret = catta_server_errno(s);
627         goto fail;
628     }
629
630     /* Add SRV record */
631
632     if (!(r = catta_record_new_full(svc_name, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_SRV, CATTA_DEFAULT_TTL_HOST_NAME))) {
633         ret = catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
634         goto fail;
635     }
636
637     r->data.srv.priority = 0;
638     r->data.srv.weight = 0;
639     r->data.srv.port = port;
640     r->data.srv.name = h;
641     h = NULL;
642     srv_entry = server_add_internal(s, g, iface, protocol, CATTA_PUBLISH_UNIQUE, r);
643     catta_record_unref(r);
644
645     if (!srv_entry) {
646         ret = catta_server_errno(s);
647         goto fail;
648     }
649
650     /* Add TXT record */
651
652     if (!(flags & CATTA_PUBLISH_NO_COOKIE))
653         strlst = add_magic_cookie(s, strlst);
654
655     txt_entry = server_add_txt_strlst_nocopy(s, g, iface, protocol, CATTA_PUBLISH_UNIQUE, CATTA_DEFAULT_TTL, svc_name, strlst);
656     strlst = NULL;
657
658     if (!txt_entry) {
659         ret = catta_server_errno(s);
660         goto fail;
661     }
662
663     /* Add service type enumeration record */
664
665     if (!(enum_entry = server_add_ptr_internal(s, g, iface, protocol, 0, CATTA_DEFAULT_TTL, enum_ptr, ptr_name))) {
666         ret = catta_server_errno(s);
667         goto fail;
668     }
669
670 fail:
671     if (ret != CATTA_OK && !(flags & CATTA_PUBLISH_UPDATE)) {
672         if (srv_entry)
673             catta_entry_free(s, srv_entry);
674         if (txt_entry)
675             catta_entry_free(s, txt_entry);
676         if (ptr_entry)
677             catta_entry_free(s, ptr_entry);
678         if (enum_entry)
679             catta_entry_free(s, enum_entry);
680     }
681
682     catta_string_list_free(strlst);
683     catta_free(h);
684
685     return ret;
686 }
687
688 int catta_server_add_service_strlst(
689     CattaServer *s,
690     CattaSEntryGroup *g,
691     CattaIfIndex iface,
692     CattaProtocol protocol,
693     CattaPublishFlags flags,
694     const char *name,
695     const char *type,
696     const char *domain,
697     const char *host,
698     uint16_t port,
699     CattaStringList *strlst) {
700
701     assert(s);
702     assert(type);
703     assert(name);
704
705     return server_add_service_strlst_nocopy(s, g, iface, protocol, flags, name, type, domain, host, port, catta_string_list_copy(strlst));
706 }
707
708 int catta_server_add_service(
709     CattaServer *s,
710     CattaSEntryGroup *g,
711     CattaIfIndex iface,
712     CattaProtocol protocol,
713     CattaPublishFlags flags,
714     const char *name,
715     const char *type,
716     const char *domain,
717     const char *host,
718     unsigned int port,
719     ... ){
720
721     va_list va;
722     int ret;
723
724     va_start(va, port);
725     ret = server_add_service_strlst_nocopy(s, g, iface, protocol, flags, name, type, domain, host, port, catta_string_list_new_va(va));
726     va_end(va);
727
728     return ret;
729 }
730
731 static int server_update_service_txt_strlst_nocopy(
732     CattaServer *s,
733     CattaSEntryGroup *g,
734     CattaIfIndex iface,
735     CattaProtocol protocol,
736     CattaPublishFlags flags,
737     const char *name,
738     const char *type,
739     const char *domain,
740     CattaStringList *strlst) {
741
742     char svc_name[CATTA_DOMAIN_NAME_MAX];
743     int ret = CATTA_OK;
744     CattaEntry *e;
745
746     assert(s);
747     assert(type);
748     assert(name);
749
750     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
751     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
752     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_FLAGS_VALID(flags,
753                                                                 CATTA_PUBLISH_NO_COOKIE|
754                                                                 CATTA_PUBLISH_USE_WIDE_AREA|
755                                                                 CATTA_PUBLISH_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
756     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_name(name), CATTA_ERR_INVALID_SERVICE_NAME);
757     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_type_strict(type), CATTA_ERR_INVALID_SERVICE_TYPE);
758     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
759
760     if (!domain)
761         domain = s->domain_name;
762
763     transport_flags_from_domain(s, &flags, domain);
764     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
765
766     if ((ret = catta_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
767         catta_server_set_errno(s, ret);
768         goto fail;
769     }
770
771     /* Add TXT record */
772     if (!(flags & CATTA_PUBLISH_NO_COOKIE))
773         strlst = add_magic_cookie(s, strlst);
774
775     e = server_add_txt_strlst_nocopy(s, g, iface, protocol, CATTA_PUBLISH_UNIQUE | CATTA_PUBLISH_UPDATE, CATTA_DEFAULT_TTL, svc_name, strlst);
776     strlst = NULL;
777
778     if (!e)
779         ret = catta_server_errno(s);
780
781 fail:
782
783     catta_string_list_free(strlst);
784
785     return ret;
786 }
787
788 int catta_server_update_service_txt_strlst(
789     CattaServer *s,
790     CattaSEntryGroup *g,
791     CattaIfIndex iface,
792     CattaProtocol protocol,
793     CattaPublishFlags flags,
794     const char *name,
795     const char *type,
796     const char *domain,
797     CattaStringList *strlst) {
798
799     return server_update_service_txt_strlst_nocopy(s, g, iface, protocol, flags, name, type, domain, catta_string_list_copy(strlst));
800 }
801
802 /** Update the TXT record for a service with the NULL termonate list of strings */
803 int catta_server_update_service_txt(
804     CattaServer *s,
805     CattaSEntryGroup *g,
806     CattaIfIndex iface,
807     CattaProtocol protocol,
808     CattaPublishFlags flags,
809     const char *name,
810     const char *type,
811     const char *domain,
812     ...) {
813
814     va_list va;
815     int ret;
816
817     va_start(va, domain);
818     ret = server_update_service_txt_strlst_nocopy(s, g, iface, protocol, flags, name, type, domain, catta_string_list_new_va(va));
819     va_end(va);
820
821     return ret;
822 }
823
824 int catta_server_add_service_subtype(
825     CattaServer *s,
826     CattaSEntryGroup *g,
827     CattaIfIndex iface,
828     CattaProtocol protocol,
829     CattaPublishFlags flags,
830     const char *name,
831     const char *type,
832     const char *domain,
833     const char *subtype) {
834
835     int ret = CATTA_OK;
836     char svc_name[CATTA_DOMAIN_NAME_MAX], ptr_name[CATTA_DOMAIN_NAME_MAX];
837
838     assert(name);
839     assert(type);
840     assert(subtype);
841
842     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
843     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_PROTO_VALID(protocol), CATTA_ERR_INVALID_PROTOCOL);
844     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, CATTA_FLAGS_VALID(flags, CATTA_PUBLISH_USE_MULTICAST|CATTA_PUBLISH_USE_WIDE_AREA), CATTA_ERR_INVALID_FLAGS);
845     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_name(name), CATTA_ERR_INVALID_SERVICE_NAME);
846     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_type_strict(type), CATTA_ERR_INVALID_SERVICE_TYPE);
847     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
848     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, catta_is_valid_service_subtype(subtype), CATTA_ERR_INVALID_SERVICE_SUBTYPE);
849
850     if (!domain)
851         domain = s->domain_name;
852
853     transport_flags_from_domain(s, &flags, domain);
854     CATTA_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
855
856     if ((ret = catta_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
857         (ret = catta_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
858         catta_server_set_errno(s, ret);
859         goto fail;
860     }
861
862     if ((ret = catta_server_add_ptr(s, g, iface, protocol, 0, CATTA_DEFAULT_TTL, ptr_name, svc_name)) < 0)
863         goto fail;
864
865 fail:
866
867     return ret;
868 }
869
870 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
871     static const char hex[] = "0123456789abcdef";
872     int b = 0;
873     const uint8_t *k = p;
874
875     while (sl > 1 && pl > 0) {
876         *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
877
878         if (b) {
879             k++;
880             pl--;
881         }
882
883         b = !b;
884
885         sl--;
886     }
887
888     if (sl > 0)
889         *s = 0;
890 }
891
892 static CattaEntry *server_add_dns_server_name(
893     CattaServer *s,
894     CattaSEntryGroup *g,
895     CattaIfIndex iface,
896     CattaProtocol protocol,
897     CattaPublishFlags flags,
898     const char *domain,
899     CattaDNSServerType type,
900     const char *name,
901     uint16_t port /** should be 53 */) {
902
903     CattaEntry *e;
904     char t[CATTA_DOMAIN_NAME_MAX], normalized_d[CATTA_DOMAIN_NAME_MAX], *n;
905
906     CattaRecord *r;
907
908     assert(s);
909     assert(name);
910
911     CATTA_CHECK_VALIDITY_RETURN_NULL(s, CATTA_FLAGS_VALID(flags, CATTA_PUBLISH_USE_WIDE_AREA|CATTA_PUBLISH_USE_MULTICAST), CATTA_ERR_INVALID_FLAGS);
912     CATTA_CHECK_VALIDITY_RETURN_NULL(s, type == CATTA_DNS_SERVER_UPDATE || type == CATTA_DNS_SERVER_RESOLVE, CATTA_ERR_INVALID_FLAGS);
913     CATTA_CHECK_VALIDITY_RETURN_NULL(s, port != 0, CATTA_ERR_INVALID_PORT);
914     CATTA_CHECK_VALIDITY_RETURN_NULL(s, catta_is_valid_fqdn(name), CATTA_ERR_INVALID_HOST_NAME);
915     CATTA_CHECK_VALIDITY_RETURN_NULL(s, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
916
917     if (!domain)
918         domain = s->domain_name;
919
920     transport_flags_from_domain(s, &flags, domain);
921     CATTA_CHECK_VALIDITY_RETURN_NULL(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
922
923     if (!(n = catta_normalize_name_strdup(name))) {
924         catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
925         return NULL;
926     }
927
928     CATTA_ASSERT_TRUE(catta_normalize_name(domain, normalized_d, sizeof(normalized_d)));
929
930     snprintf(t, sizeof(t), "%s.%s", type == CATTA_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
931
932     if (!(r = catta_record_new_full(t, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_SRV, CATTA_DEFAULT_TTL_HOST_NAME))) {
933         catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
934         catta_free(n);
935         return NULL;
936     }
937
938     r->data.srv.priority = 0;
939     r->data.srv.weight = 0;
940     r->data.srv.port = port;
941     r->data.srv.name = n;
942     e = server_add_internal(s, g, iface, protocol, 0, r);
943     catta_record_unref(r);
944
945     return e;
946 }
947
948 int catta_server_add_dns_server_address(
949     CattaServer *s,
950     CattaSEntryGroup *g,
951     CattaIfIndex iface,
952     CattaProtocol protocol,
953     CattaPublishFlags flags,
954     const char *domain,
955     CattaDNSServerType type,
956     const CattaAddress *address,
957     uint16_t port /** should be 53 */) {
958
959     CattaRecord *r;
960     char n[64], h[64];
961     CattaEntry *a_entry, *s_entry;
962
963     assert(s);
964     assert(address);
965
966     CATTA_CHECK_VALIDITY(s, CATTA_IF_VALID(iface), CATTA_ERR_INVALID_INTERFACE);
967     CATTA_CHECK_VALIDITY(s, CATTA_PROTO_VALID(protocol) && CATTA_PROTO_VALID(address->proto), CATTA_ERR_INVALID_PROTOCOL);
968     CATTA_CHECK_VALIDITY(s, CATTA_FLAGS_VALID(flags, CATTA_PUBLISH_USE_MULTICAST|CATTA_PUBLISH_USE_WIDE_AREA), CATTA_ERR_INVALID_FLAGS);
969     CATTA_CHECK_VALIDITY(s, type == CATTA_DNS_SERVER_UPDATE || type == CATTA_DNS_SERVER_RESOLVE, CATTA_ERR_INVALID_FLAGS);
970     CATTA_CHECK_VALIDITY(s, port != 0, CATTA_ERR_INVALID_PORT);
971     CATTA_CHECK_VALIDITY(s, !domain || catta_is_valid_domain_name(domain), CATTA_ERR_INVALID_DOMAIN_NAME);
972
973     if (!domain)
974         domain = s->domain_name;
975
976     transport_flags_from_domain(s, &flags, domain);
977     CATTA_CHECK_VALIDITY(s, flags & CATTA_PUBLISH_USE_MULTICAST, CATTA_ERR_NOT_SUPPORTED);
978
979     if (address->proto == CATTA_PROTO_INET) {
980         hexstring(h, sizeof(h), &address->data, sizeof(CattaIPv4Address));
981         snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
982         r = catta_record_new_full(n, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_A, CATTA_DEFAULT_TTL_HOST_NAME);
983         r->data.a.address = address->data.ipv4;
984     } else {
985         hexstring(h, sizeof(h), &address->data, sizeof(CattaIPv6Address));
986         snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
987         r = catta_record_new_full(n, CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_AAAA, CATTA_DEFAULT_TTL_HOST_NAME);
988         r->data.aaaa.address = address->data.ipv6;
989     }
990
991     if (!r)
992         return catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
993
994     a_entry = server_add_internal(s, g, iface, protocol, CATTA_PUBLISH_UNIQUE | CATTA_PUBLISH_ALLOW_MULTIPLE, r);
995     catta_record_unref(r);
996
997     if (!a_entry)
998         return catta_server_errno(s);
999
1000     if (!(s_entry = server_add_dns_server_name(s, g, iface, protocol, flags, domain, type, n, port))) {
1001         if (!(flags & CATTA_PUBLISH_UPDATE))
1002             catta_entry_free(s, a_entry);
1003         return catta_server_errno(s);
1004     }
1005
1006     return CATTA_OK;
1007 }
1008
1009 void catta_s_entry_group_change_state(CattaSEntryGroup *g, CattaEntryGroupState state) {
1010     assert(g);
1011
1012     if (g->state == state)
1013         return;
1014
1015     assert(state <= CATTA_ENTRY_GROUP_COLLISION);
1016
1017     if (g->state == CATTA_ENTRY_GROUP_ESTABLISHED) {
1018
1019         /* If the entry group was established for a time longer then
1020          * 5s, reset the establishment trial counter */
1021
1022         if (catta_age(&g->established_at) > 5000000)
1023             g->n_register_try = 0;
1024     } else if (g->state == CATTA_ENTRY_GROUP_REGISTERING) {
1025         if (g->register_time_event) {
1026             catta_time_event_free(g->register_time_event);
1027             g->register_time_event = NULL;
1028         }
1029     }
1030
1031     if (state == CATTA_ENTRY_GROUP_ESTABLISHED)
1032
1033         /* If the entry group is now established, remember the time
1034          * this happened */
1035
1036         gettimeofday(&g->established_at, NULL);
1037
1038     g->state = state;
1039
1040     if (g->callback)
1041         g->callback(g->server, g, state, g->userdata);
1042 }
1043
1044 CattaSEntryGroup *catta_s_entry_group_new(CattaServer *s, CattaSEntryGroupCallback callback, void* userdata) {
1045     CattaSEntryGroup *g;
1046
1047     assert(s);
1048
1049     if (!(g = catta_new(CattaSEntryGroup, 1))) {
1050         catta_server_set_errno(s, CATTA_ERR_NO_MEMORY);
1051         return NULL;
1052     }
1053
1054     g->server = s;
1055     g->callback = callback;
1056     g->userdata = userdata;
1057     g->dead = 0;
1058     g->state = CATTA_ENTRY_GROUP_UNCOMMITED;
1059     g->n_probing = 0;
1060     g->n_register_try = 0;
1061     g->register_time_event = NULL;
1062     g->register_time.tv_sec = 0;
1063     g->register_time.tv_usec = 0;
1064     CATTA_LLIST_HEAD_INIT(CattaEntry, g->entries);
1065
1066     CATTA_LLIST_PREPEND(CattaSEntryGroup, groups, s->groups, g);
1067     return g;
1068 }
1069
1070 static void cleanup_time_event_callback(CATTA_GCC_UNUSED CattaTimeEvent *e, void* userdata) {
1071     CattaServer *s = userdata;
1072
1073     assert(s);
1074
1075     catta_cleanup_dead_entries(s);
1076 }
1077
1078 static void schedule_cleanup(CattaServer *s) {
1079     struct timeval tv;
1080
1081     assert(s);
1082
1083     if (!s->cleanup_time_event)
1084         s->cleanup_time_event = catta_time_event_new(s->time_event_queue, catta_elapse_time(&tv, 1000, 0), &cleanup_time_event_callback, s);
1085 }
1086
1087 void catta_s_entry_group_free(CattaSEntryGroup *g) {
1088     CattaEntry *e;
1089
1090     assert(g);
1091     assert(g->server);
1092
1093     for (e = g->entries; e; e = e->by_group_next) {
1094         if (!e->dead) {
1095             catta_goodbye_entry(g->server, e, 1, 1);
1096             e->dead = 1;
1097         }
1098     }
1099
1100     if (g->register_time_event) {
1101         catta_time_event_free(g->register_time_event);
1102         g->register_time_event = NULL;
1103     }
1104
1105     g->dead = 1;
1106
1107     g->server->need_group_cleanup = 1;
1108     g->server->need_entry_cleanup = 1;
1109
1110     schedule_cleanup(g->server);
1111 }
1112
1113 static void entry_group_commit_real(CattaSEntryGroup *g) {
1114     assert(g);
1115
1116     gettimeofday(&g->register_time, NULL);
1117
1118     catta_s_entry_group_change_state(g, CATTA_ENTRY_GROUP_REGISTERING);
1119
1120     if (g->dead)
1121         return;
1122
1123     catta_announce_group(g->server, g);
1124     catta_s_entry_group_check_probed(g, 0);
1125 }
1126
1127 static void entry_group_register_time_event_callback(CATTA_GCC_UNUSED CattaTimeEvent *e, void* userdata) {
1128     CattaSEntryGroup *g = userdata;
1129     assert(g);
1130
1131     catta_time_event_free(g->register_time_event);
1132     g->register_time_event = NULL;
1133
1134     /* Holdoff time passed, so let's start probing */
1135     entry_group_commit_real(g);
1136 }
1137
1138 int catta_s_entry_group_commit(CattaSEntryGroup *g) {
1139     struct timeval now;
1140
1141     assert(g);
1142     assert(!g->dead);
1143
1144     if (g->state != CATTA_ENTRY_GROUP_UNCOMMITED && g->state != CATTA_ENTRY_GROUP_COLLISION)
1145         return catta_server_set_errno(g->server, CATTA_ERR_BAD_STATE);
1146
1147     if (catta_s_entry_group_is_empty(g))
1148         return catta_server_set_errno(g->server, CATTA_ERR_IS_EMPTY);
1149
1150     g->n_register_try++;
1151
1152     catta_timeval_add(&g->register_time,
1153                       1000*(g->n_register_try >= CATTA_RR_RATE_LIMIT_COUNT ?
1154                             CATTA_RR_HOLDOFF_MSEC_RATE_LIMIT :
1155                             CATTA_RR_HOLDOFF_MSEC));
1156
1157     gettimeofday(&now, NULL);
1158
1159     if (catta_timeval_compare(&g->register_time, &now) <= 0) {
1160
1161         /* Holdoff time passed, so let's start probing */
1162         entry_group_commit_real(g);
1163     } else {
1164
1165          /* Holdoff time has not yet passed, so let's wait */
1166         assert(!g->register_time_event);
1167         g->register_time_event = catta_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1168
1169         catta_s_entry_group_change_state(g, CATTA_ENTRY_GROUP_REGISTERING);
1170     }
1171
1172     return CATTA_OK;
1173 }
1174
1175 void catta_s_entry_group_reset(CattaSEntryGroup *g) {
1176     CattaEntry *e;
1177     assert(g);
1178
1179     for (e = g->entries; e; e = e->by_group_next) {
1180         if (!e->dead) {
1181             catta_goodbye_entry(g->server, e, 1, 1);
1182             e->dead = 1;
1183         }
1184     }
1185     g->server->need_entry_cleanup = 1;
1186
1187     g->n_probing = 0;
1188
1189     catta_s_entry_group_change_state(g, CATTA_ENTRY_GROUP_UNCOMMITED);
1190
1191     schedule_cleanup(g->server);
1192 }
1193
1194 int catta_entry_is_commited(CattaEntry *e) {
1195     assert(e);
1196     assert(!e->dead);
1197
1198     return !e->group ||
1199         e->group->state == CATTA_ENTRY_GROUP_REGISTERING ||
1200         e->group->state == CATTA_ENTRY_GROUP_ESTABLISHED;
1201 }
1202
1203 CattaEntryGroupState catta_s_entry_group_get_state(CattaSEntryGroup *g) {
1204     assert(g);
1205     assert(!g->dead);
1206
1207     return g->state;
1208 }
1209
1210 void catta_s_entry_group_set_data(CattaSEntryGroup *g, void* userdata) {
1211     assert(g);
1212
1213     g->userdata = userdata;
1214 }
1215
1216 void* catta_s_entry_group_get_data(CattaSEntryGroup *g) {
1217     assert(g);
1218
1219     return g->userdata;
1220 }
1221
1222 int catta_s_entry_group_is_empty(CattaSEntryGroup *g) {
1223     CattaEntry *e;
1224     assert(g);
1225
1226     /* Look for an entry that is not dead */
1227     for (e = g->entries; e; e = e->by_group_next)
1228         if (!e->dead)
1229             return 0;
1230
1231     return 1;
1232 }