4 This file is part of avahi.
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.
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.
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
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
29 #include <sys/utsname.h>
36 #include <avahi-common/domain.h>
37 #include <avahi-common/timeval.h>
38 #include <avahi-common/malloc.h>
39 #include <avahi-common/error.h>
47 #include "dns-srv-rr.h"
50 void avahi_entry_free(AvahiServer*s, AvahiEntry *e) {
56 avahi_goodbye_entry(s, e, 1, 1);
58 /* Remove from linked list */
59 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
61 /* Remove from hash table indexed by name */
62 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
63 AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
65 avahi_hashmap_replace(s->entries_by_key, t->record->key, t);
67 avahi_hashmap_remove(s->entries_by_key, e->record->key);
69 /* Remove from associated group */
71 AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
73 avahi_record_unref(e->record);
77 void avahi_entry_group_free(AvahiServer *s, AvahiSEntryGroup *g) {
82 avahi_entry_free(s, g->entries);
84 if (g->register_time_event)
85 avahi_time_event_free(g->register_time_event);
87 AVAHI_LLIST_REMOVE(AvahiSEntryGroup, groups, s->groups, g);
91 void avahi_cleanup_dead_entries(AvahiServer *s) {
94 if (s->need_group_cleanup) {
95 AvahiSEntryGroup *g, *next;
97 for (g = s->groups; g; g = next) {
98 next = g->groups_next;
101 avahi_entry_group_free(s, g);
104 s->need_group_cleanup = 0;
107 if (s->need_entry_cleanup) {
108 AvahiEntry *e, *next;
110 for (e = s->entries; e; e = next) {
111 next = e->entries_next;
114 avahi_entry_free(s, e);
117 s->need_entry_cleanup = 0;
120 if (s->need_browser_cleanup)
121 avahi_browser_cleanup(s);
124 static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiPublishFlags flags) {
130 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
134 if (!(flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_UNIQUE))
137 if ((flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) && (e->flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) )
140 if ((interface <= 0 ||
142 e->interface == interface) &&
143 (protocol == AVAHI_PROTO_UNSPEC ||
144 e->protocol == AVAHI_PROTO_UNSPEC ||
145 e->protocol == protocol))
153 static AvahiEntry * server_add_internal(
156 AvahiIfIndex interface,
157 AvahiProtocol protocol,
158 AvahiPublishFlags flags,
166 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
167 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
168 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(
170 AVAHI_PUBLISH_NO_ANNOUNCE|
171 AVAHI_PUBLISH_NO_PROBE|
172 AVAHI_PUBLISH_UNIQUE|
173 AVAHI_PUBLISH_ALLOW_MULTIPLE|
174 AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
175 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(r->key->name), AVAHI_ERR_INVALID_HOST_NAME);
176 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, r->ttl != 0, AVAHI_ERR_INVALID_TTL);
177 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !avahi_key_is_pattern(r->key), AVAHI_ERR_IS_PATTERN);
178 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_record_is_valid(r), AVAHI_ERR_INVALID_RECORD);
179 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, r->key->clazz == AVAHI_DNS_CLASS_IN, AVAHI_ERR_INVALID_DNS_CLASS);
180 AVAHI_CHECK_VALIDITY_RETURN_NULL(s,
181 (r->key->type != 0) &&
182 (r->key->type != AVAHI_DNS_TYPE_ANY) &&
183 (r->key->type != AVAHI_DNS_TYPE_OPT) &&
184 (r->key->type != AVAHI_DNS_TYPE_TKEY) &&
185 (r->key->type != AVAHI_DNS_TYPE_TSIG) &&
186 (r->key->type != AVAHI_DNS_TYPE_IXFR) &&
187 (r->key->type != AVAHI_DNS_TYPE_AXFR), AVAHI_ERR_INVALID_DNS_TYPE);
189 if (flags & AVAHI_PUBLISH_UPDATE) {
190 AvahiRecord *old_record;
193 /* Update and existing record */
195 /* Find the first matching entry */
196 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
197 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
203 /* Hmm, nothing found? */
205 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
209 /* Update the entry */
210 old_record = e->record;
211 e->record = avahi_record_ref(r);
214 /* Announce our changes when needed */
215 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
217 /* Remove the old entry from all caches, if needed */
218 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
219 avahi_goodbye_entry(s, e, 1, 0);
221 /* Reannounce our updated entry */
222 avahi_reannounce_entry(s, e);
225 /* If we were the first entry in the list, we need to update the key */
227 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
229 avahi_record_unref(old_record);
234 /* Add a new record */
236 if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
237 avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
241 if (!(e = avahi_new(AvahiEntry, 1))) {
242 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
247 e->record = avahi_record_ref(r);
249 e->interface = interface;
250 e->protocol = protocol;
254 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
256 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
258 /* Insert into hash table indexed by name */
259 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
260 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
261 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
263 /* Insert into group list */
265 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
267 avahi_announce_entry(s, e);
273 int avahi_server_add(
276 AvahiIfIndex interface,
277 AvahiProtocol protocol,
278 AvahiPublishFlags flags,
281 if (!server_add_internal(s, g, interface, protocol, flags, r))
282 return avahi_server_errno(s);
287 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
288 AvahiEntry **e = (AvahiEntry**) state;
293 *e = g ? g->entries : s->entries;
295 while (*e && (*e)->dead)
296 *e = g ? (*e)->by_group_next : (*e)->entries_next;
301 return avahi_record_ref((*e)->record);
304 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
310 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
312 for (e = s->entries; e; e = e->entries_next) {
319 if (!(t = avahi_record_to_string(e->record)))
320 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
322 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
325 callback(ln, userdata);
328 avahi_dump_caches(s->monitor, callback, userdata);
330 if (s->wide_area_lookup_engine)
331 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
335 static AvahiEntry *server_add_ptr_internal(
338 AvahiIfIndex interface,
339 AvahiProtocol protocol,
340 AvahiPublishFlags flags,
351 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
352 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(dest), AVAHI_ERR_INVALID_HOST_NAME);
355 name = s->host_name_fqdn;
357 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) {
358 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
362 r->data.ptr.name = avahi_normalize_name_strdup(dest);
363 e = server_add_internal(s, g, interface, protocol, flags, r);
364 avahi_record_unref(r);
368 int avahi_server_add_ptr(
371 AvahiIfIndex interface,
372 AvahiProtocol protocol,
373 AvahiPublishFlags flags,
382 if (!(e = server_add_ptr_internal(s, g, interface, protocol, flags, ttl, name, dest)))
383 return avahi_server_errno(s);
388 int avahi_server_add_address(
391 AvahiIfIndex interface,
392 AvahiProtocol protocol,
393 AvahiPublishFlags flags,
397 char n[AVAHI_DOMAIN_NAME_MAX];
399 AvahiEntry *entry = NULL, *reverse = NULL;
405 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
406 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(a->proto), AVAHI_ERR_INVALID_PROTOCOL);
407 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_REVERSE|AVAHI_PUBLISH_NO_ANNOUNCE|AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
408 AVAHI_CHECK_VALIDITY(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
410 /* Prepare the host naem */
413 name = s->host_name_fqdn;
415 AVAHI_ASSERT_TRUE(avahi_normalize_name(name, n, sizeof(n)));
419 /* Create the A/AAAA record */
421 if (a->proto == AVAHI_PROTO_INET) {
423 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
424 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
428 r->data.a.address = a->data.ipv4;
431 assert(a->proto == AVAHI_PROTO_INET6);
433 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
434 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
438 r->data.aaaa.address = a->data.ipv6;
441 entry = server_add_internal(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
442 avahi_record_unref(r);
445 ret = avahi_server_errno(s);
449 /* Create the reverse lookup entry */
451 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
452 char reverse_n[AVAHI_DOMAIN_NAME_MAX];
453 avahi_reverse_lookup_name(a, reverse_n, sizeof(reverse_n));
455 if (!(reverse = server_add_ptr_internal(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
456 ret = avahi_server_errno(s);
463 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
465 avahi_entry_free(s, entry);
467 avahi_entry_free(s, reverse);
473 static AvahiEntry *server_add_txt_strlst_nocopy(
476 AvahiIfIndex interface,
477 AvahiProtocol protocol,
478 AvahiPublishFlags flags,
481 AvahiStringList *strlst) {
488 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
489 avahi_string_list_free(strlst);
490 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
494 r->data.txt.string_list = strlst;
495 e = server_add_internal(s, g, interface, protocol, flags, r);
496 avahi_record_unref(r);
501 static AvahiStringList *add_magic_cookie(
503 AvahiStringList *strlst) {
507 if (!s->config.add_service_cookie)
510 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
511 /* This string list already contains a magic cookie */
514 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
517 static int server_add_service_strlst_nocopy(
520 AvahiIfIndex interface,
521 AvahiProtocol protocol,
522 AvahiPublishFlags flags,
528 AvahiStringList *strlst) {
530 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
531 AvahiRecord *r = NULL;
533 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
539 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
540 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
541 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
542 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
543 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
544 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
545 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
548 domain = s->domain_name;
551 host = s->host_name_fqdn;
553 if (!(h = avahi_normalize_name_strdup(host))) {
554 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
558 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
559 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
560 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
561 avahi_server_set_errno(s, ret);
565 /* Add service enumeration PTR record */
567 if (!(ptr_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name))) {
568 ret = avahi_server_errno(s);
574 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
575 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
579 r->data.srv.priority = 0;
580 r->data.srv.weight = 0;
581 r->data.srv.port = port;
582 r->data.srv.name = h;
584 srv_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r);
585 avahi_record_unref(r);
588 ret = avahi_server_errno(s);
594 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
595 strlst = add_magic_cookie(s, strlst);
597 txt_entry = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
601 ret = avahi_server_errno(s);
605 /* Add service type enumeration record */
607 if (!(enum_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name))) {
608 ret = avahi_server_errno(s);
613 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
615 avahi_entry_free(s, srv_entry);
617 avahi_entry_free(s, txt_entry);
619 avahi_entry_free(s, ptr_entry);
621 avahi_entry_free(s, enum_entry);
624 avahi_string_list_free(strlst);
630 int avahi_server_add_service_strlst(
633 AvahiIfIndex interface,
634 AvahiProtocol protocol,
635 AvahiPublishFlags flags,
641 AvahiStringList *strlst) {
647 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
650 int avahi_server_add_service(
653 AvahiIfIndex interface,
654 AvahiProtocol protocol,
655 AvahiPublishFlags flags,
667 ret = server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
673 static int server_update_service_txt_strlst_nocopy(
676 AvahiIfIndex interface,
677 AvahiProtocol protocol,
678 AvahiPublishFlags flags,
682 AvahiStringList *strlst) {
684 char svc_name[AVAHI_DOMAIN_NAME_MAX];
692 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
693 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
694 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS);
695 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
696 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
697 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
700 domain = s->domain_name;
702 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
703 avahi_server_set_errno(s, ret);
708 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
709 strlst = add_magic_cookie(s, strlst);
711 e = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
715 ret = avahi_server_errno(s);
719 avahi_string_list_free(strlst);
724 int avahi_server_update_service_txt_strlst(
727 AvahiIfIndex interface,
728 AvahiProtocol protocol,
729 AvahiPublishFlags flags,
733 AvahiStringList *strlst) {
735 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
738 /** Update the TXT record for a service with the NULL termonate list of strings */
739 int avahi_server_update_service_txt(
742 AvahiIfIndex interface,
743 AvahiProtocol protocol,
744 AvahiPublishFlags flags,
753 va_start(va, domain);
754 ret = server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
760 int avahi_server_add_service_subtype(
763 AvahiIfIndex interface,
764 AvahiProtocol protocol,
765 AvahiPublishFlags flags,
769 const char *subtype) {
772 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
778 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
779 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
780 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
781 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
782 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
783 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
784 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
787 domain = s->domain_name;
789 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
790 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
791 avahi_server_set_errno(s, ret);
795 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
803 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
804 static const char hex[] = "0123456789abcdef";
806 const uint8_t *k = p;
808 while (sl > 1 && pl > 0) {
809 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
825 static AvahiEntry * server_add_dns_server_name(
828 AvahiIfIndex interface,
829 AvahiProtocol protocol,
830 AvahiPublishFlags flags,
832 AvahiDNSServerType type,
834 uint16_t port /** should be 53 */) {
837 char t[AVAHI_DOMAIN_NAME_MAX], normalized_d[AVAHI_DOMAIN_NAME_MAX], *n;
844 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
845 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
846 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, port != 0, AVAHI_ERR_INVALID_PORT);
847 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
848 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
851 domain = s->domain_name;
853 if (!(n = avahi_normalize_name_strdup(name))) {
854 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
858 AVAHI_ASSERT_TRUE(avahi_normalize_name(domain, normalized_d, sizeof(normalized_d)));
860 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
862 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
863 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
868 r->data.srv.priority = 0;
869 r->data.srv.weight = 0;
870 r->data.srv.port = port;
871 r->data.srv.name = n;
872 e = server_add_internal(s, g, interface, protocol, 0, r);
873 avahi_record_unref(r);
878 int avahi_server_add_dns_server_address(
881 AvahiIfIndex interface,
882 AvahiProtocol protocol,
883 AvahiPublishFlags flags,
885 AvahiDNSServerType type,
886 const AvahiAddress *address,
887 uint16_t port /** should be 53 */) {
891 AvahiEntry *a_entry, *s_entry;
896 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
897 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(address->proto), AVAHI_ERR_INVALID_PROTOCOL);
898 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
899 AVAHI_CHECK_VALIDITY(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
900 AVAHI_CHECK_VALIDITY(s, port != 0, AVAHI_ERR_INVALID_PORT);
901 AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
904 domain = s->domain_name;
906 if (address->proto == AVAHI_PROTO_INET) {
907 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
908 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
909 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
910 r->data.a.address = address->data.ipv4;
912 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
913 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
914 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
915 r->data.aaaa.address = address->data.ipv6;
919 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
921 a_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
922 avahi_record_unref(r);
925 return avahi_server_errno(s);
927 if (!(s_entry = server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port))) {
928 if (!(flags & AVAHI_PUBLISH_UPDATE))
929 avahi_entry_free(s, a_entry);
930 return avahi_server_errno(s);
936 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
939 if (g->state == state)
942 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
944 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
946 /* If the entry group was established for a time longer then
947 * 5s, reset the establishment trial counter */
949 if (avahi_age(&g->established_at) > 5000000)
950 g->n_register_try = 0;
953 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
955 /* If the entry group is now established, remember the time
958 gettimeofday(&g->established_at, NULL);
963 g->callback(g->server, g, state, g->userdata);
966 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
971 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
972 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
977 g->callback = callback;
978 g->userdata = userdata;
980 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
982 g->n_register_try = 0;
983 g->register_time_event = NULL;
984 g->register_time.tv_sec = 0;
985 g->register_time.tv_usec = 0;
986 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
988 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
992 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
998 for (e = g->entries; e; e = e->by_group_next) {
1000 avahi_goodbye_entry(g->server, e, 1, 1);
1005 if (g->register_time_event) {
1006 avahi_time_event_free(g->register_time_event);
1007 g->register_time_event = NULL;
1012 g->server->need_group_cleanup = 1;
1013 g->server->need_entry_cleanup = 1;
1016 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1019 gettimeofday(&g->register_time, NULL);
1021 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1024 avahi_announce_group(g->server, g);
1025 avahi_s_entry_group_check_probed(g, 0);
1029 static void entry_group_register_time_event_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* userdata) {
1030 AvahiSEntryGroup *g = userdata;
1033 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
1035 avahi_time_event_free(g->register_time_event);
1036 g->register_time_event = NULL;
1038 /* Holdoff time passed, so let's start probing */
1039 entry_group_commit_real(g);
1042 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1048 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1049 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1051 g->n_register_try++;
1053 avahi_timeval_add(&g->register_time,
1054 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1055 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1056 AVAHI_RR_HOLDOFF_MSEC));
1058 gettimeofday(&now, NULL);
1060 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1061 /* Holdoff time passed, so let's start probing */
1062 /* avahi_log_debug("Holdoff passed, directly going on."); */
1064 entry_group_commit_real(g);
1066 /* avahi_log_debug("Holdoff not passed, sleeping."); */
1068 /* Holdoff time has not yet passed, so let's wait */
1069 assert(!g->register_time_event);
1070 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1072 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1078 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1082 for (e = g->entries; e; e = e->by_group_next) {
1084 avahi_goodbye_entry(g->server, e, 1, 1);
1088 g->server->need_entry_cleanup = 1;
1090 if (g->register_time_event) {
1091 avahi_time_event_free(g->register_time_event);
1092 g->register_time_event = NULL;
1097 gettimeofday(&g->register_time, NULL);
1099 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1102 int avahi_entry_is_commited(AvahiEntry *e) {
1107 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1108 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1111 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1118 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1121 g->userdata = userdata;
1124 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1130 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1134 /* Look for an entry that is not dead */
1135 for (e = g->entries; e; e = e->by_group_next)