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);
180 if (flags & AVAHI_PUBLISH_UPDATE) {
181 AvahiRecord *old_record;
184 /* Update and existing record */
186 /* Find the first matching entry */
187 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
188 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
194 /* Hmm, nothing found? */
196 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
200 /* Update the entry */
201 old_record = e->record;
202 e->record = avahi_record_ref(r);
205 /* Announce our changes when needed */
206 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
208 /* Remove the old entry from all caches, if needed */
209 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
210 avahi_goodbye_entry(s, e, 1, 0);
212 /* Reannounce our updated entry */
213 avahi_reannounce_entry(s, e);
216 /* If we were the first entry in the list, we need to update the key */
218 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
220 avahi_record_unref(old_record);
225 /* Add a new record */
227 if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
228 avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
232 if (!(e = avahi_new(AvahiEntry, 1))) {
233 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
238 e->record = avahi_record_ref(r);
240 e->interface = interface;
241 e->protocol = protocol;
245 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
247 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
249 /* Insert into hash table indexed by name */
250 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
251 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
252 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
254 /* Insert into group list */
256 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
258 avahi_announce_entry(s, e);
264 int avahi_server_add(
267 AvahiIfIndex interface,
268 AvahiProtocol protocol,
269 AvahiPublishFlags flags,
272 if (!server_add_internal(s, g, interface, protocol, flags, r))
273 return avahi_server_errno(s);
278 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
279 AvahiEntry **e = (AvahiEntry**) state;
284 *e = g ? g->entries : s->entries;
286 while (*e && (*e)->dead)
287 *e = g ? (*e)->by_group_next : (*e)->entries_next;
292 return avahi_record_ref((*e)->record);
295 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
301 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
303 for (e = s->entries; e; e = e->entries_next) {
310 if (!(t = avahi_record_to_string(e->record)))
311 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
313 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
316 callback(ln, userdata);
319 avahi_dump_caches(s->monitor, callback, userdata);
321 if (s->wide_area_lookup_engine)
322 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
326 static AvahiEntry *server_add_ptr_internal(
329 AvahiIfIndex interface,
330 AvahiProtocol protocol,
331 AvahiPublishFlags flags,
342 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
343 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(dest), AVAHI_ERR_INVALID_HOST_NAME);
346 name = s->host_name_fqdn;
348 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) {
349 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
353 r->data.ptr.name = avahi_normalize_name_strdup(dest);
354 e = server_add_internal(s, g, interface, protocol, flags, r);
355 avahi_record_unref(r);
359 int avahi_server_add_ptr(
362 AvahiIfIndex interface,
363 AvahiProtocol protocol,
364 AvahiPublishFlags flags,
373 if (!(e = server_add_ptr_internal(s, g, interface, protocol, flags, ttl, name, dest)))
374 return avahi_server_errno(s);
379 int avahi_server_add_address(
382 AvahiIfIndex interface,
383 AvahiProtocol protocol,
384 AvahiPublishFlags flags,
388 char n[AVAHI_DOMAIN_NAME_MAX];
390 AvahiEntry *entry = NULL, *reverse = NULL;
396 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
397 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(a->proto), AVAHI_ERR_INVALID_PROTOCOL);
398 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);
399 AVAHI_CHECK_VALIDITY(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
401 /* Prepare the host naem */
404 name = s->host_name_fqdn;
406 AVAHI_ASSERT_TRUE(avahi_normalize_name(name, n, sizeof(n)));
410 /* Create the A/AAAA record */
412 if (a->proto == AVAHI_PROTO_INET) {
414 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
415 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
419 r->data.a.address = a->data.ipv4;
422 assert(a->proto == AVAHI_PROTO_INET6);
424 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
425 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
429 r->data.aaaa.address = a->data.ipv6;
432 entry = server_add_internal(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
433 avahi_record_unref(r);
436 ret = avahi_server_errno(s);
440 /* Create the reverse lookup entry */
442 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
443 char reverse_n[AVAHI_DOMAIN_NAME_MAX];
444 avahi_reverse_lookup_name(a, reverse_n, sizeof(reverse_n));
446 if (!(reverse = server_add_ptr_internal(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
447 ret = avahi_server_errno(s);
454 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
456 avahi_entry_free(s, entry);
458 avahi_entry_free(s, reverse);
464 static AvahiEntry *server_add_txt_strlst_nocopy(
467 AvahiIfIndex interface,
468 AvahiProtocol protocol,
469 AvahiPublishFlags flags,
472 AvahiStringList *strlst) {
479 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
480 avahi_string_list_free(strlst);
481 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
485 r->data.txt.string_list = strlst;
486 e = server_add_internal(s, g, interface, protocol, flags, r);
487 avahi_record_unref(r);
492 static AvahiStringList *add_magic_cookie(
494 AvahiStringList *strlst) {
498 if (!s->config.add_service_cookie)
501 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
502 /* This string list already contains a magic cookie */
505 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
508 static int server_add_service_strlst_nocopy(
511 AvahiIfIndex interface,
512 AvahiProtocol protocol,
513 AvahiPublishFlags flags,
519 AvahiStringList *strlst) {
521 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
522 AvahiRecord *r = NULL;
524 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
530 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
531 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
532 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
533 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
534 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
535 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
536 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
539 domain = s->domain_name;
542 host = s->host_name_fqdn;
544 if (!(h = avahi_normalize_name_strdup(host))) {
545 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
549 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
550 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
551 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
552 avahi_server_set_errno(s, ret);
556 /* Add service enumeration PTR record */
558 if (!(ptr_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name))) {
559 ret = avahi_server_errno(s);
565 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
566 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
570 r->data.srv.priority = 0;
571 r->data.srv.weight = 0;
572 r->data.srv.port = port;
573 r->data.srv.name = h;
575 srv_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r);
576 avahi_record_unref(r);
579 ret = avahi_server_errno(s);
585 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
586 strlst = add_magic_cookie(s, strlst);
588 txt_entry = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
592 ret = avahi_server_errno(s);
596 /* Add service type enumeration record */
598 if (!(enum_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name))) {
599 ret = avahi_server_errno(s);
604 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
606 avahi_entry_free(s, srv_entry);
608 avahi_entry_free(s, txt_entry);
610 avahi_entry_free(s, ptr_entry);
612 avahi_entry_free(s, enum_entry);
615 avahi_string_list_free(strlst);
621 int avahi_server_add_service_strlst(
624 AvahiIfIndex interface,
625 AvahiProtocol protocol,
626 AvahiPublishFlags flags,
632 AvahiStringList *strlst) {
638 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
641 int avahi_server_add_service(
644 AvahiIfIndex interface,
645 AvahiProtocol protocol,
646 AvahiPublishFlags flags,
658 ret = server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
664 static int server_update_service_txt_strlst_nocopy(
667 AvahiIfIndex interface,
668 AvahiProtocol protocol,
669 AvahiPublishFlags flags,
673 AvahiStringList *strlst) {
675 char svc_name[AVAHI_DOMAIN_NAME_MAX];
683 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
684 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
685 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS);
686 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
687 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
688 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
691 domain = s->domain_name;
693 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
694 avahi_server_set_errno(s, ret);
699 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
700 strlst = add_magic_cookie(s, strlst);
702 e = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
706 ret = avahi_server_errno(s);
710 avahi_string_list_free(strlst);
715 int avahi_server_update_service_txt_strlst(
718 AvahiIfIndex interface,
719 AvahiProtocol protocol,
720 AvahiPublishFlags flags,
724 AvahiStringList *strlst) {
726 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
729 /** Update the TXT record for a service with the NULL termonate list of strings */
730 int avahi_server_update_service_txt(
733 AvahiIfIndex interface,
734 AvahiProtocol protocol,
735 AvahiPublishFlags flags,
744 va_start(va, domain);
745 ret = server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
751 int avahi_server_add_service_subtype(
754 AvahiIfIndex interface,
755 AvahiProtocol protocol,
756 AvahiPublishFlags flags,
760 const char *subtype) {
763 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
769 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
770 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
771 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
772 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
773 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
774 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
775 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
778 domain = s->domain_name;
780 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
781 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
782 avahi_server_set_errno(s, ret);
786 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
794 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
795 static const char hex[] = "0123456789abcdef";
797 const uint8_t *k = p;
799 while (sl > 1 && pl > 0) {
800 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
816 static AvahiEntry * server_add_dns_server_name(
819 AvahiIfIndex interface,
820 AvahiProtocol protocol,
821 AvahiPublishFlags flags,
823 AvahiDNSServerType type,
825 uint16_t port /** should be 53 */) {
828 char t[AVAHI_DOMAIN_NAME_MAX], normalized_d[AVAHI_DOMAIN_NAME_MAX], *n;
835 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
836 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
837 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, port != 0, AVAHI_ERR_INVALID_PORT);
838 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
839 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
842 domain = s->domain_name;
844 if (!(n = avahi_normalize_name_strdup(name))) {
845 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
849 AVAHI_ASSERT_TRUE(avahi_normalize_name(domain, normalized_d, sizeof(normalized_d)));
851 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
853 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
854 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
859 r->data.srv.priority = 0;
860 r->data.srv.weight = 0;
861 r->data.srv.port = port;
862 r->data.srv.name = n;
863 e = server_add_internal(s, g, interface, protocol, 0, r);
864 avahi_record_unref(r);
869 int avahi_server_add_dns_server_address(
872 AvahiIfIndex interface,
873 AvahiProtocol protocol,
874 AvahiPublishFlags flags,
876 AvahiDNSServerType type,
877 const AvahiAddress *address,
878 uint16_t port /** should be 53 */) {
882 AvahiEntry *a_entry, *s_entry;
887 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
888 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(address->proto), AVAHI_ERR_INVALID_PROTOCOL);
889 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
890 AVAHI_CHECK_VALIDITY(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
891 AVAHI_CHECK_VALIDITY(s, port != 0, AVAHI_ERR_INVALID_PORT);
892 AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
895 domain = s->domain_name;
897 if (address->proto == AVAHI_PROTO_INET) {
898 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
899 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
900 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
901 r->data.a.address = address->data.ipv4;
903 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
904 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
905 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
906 r->data.aaaa.address = address->data.ipv6;
910 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
912 a_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
913 avahi_record_unref(r);
916 return avahi_server_errno(s);
918 if (!(s_entry = server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port))) {
919 if (!(flags & AVAHI_PUBLISH_UPDATE))
920 avahi_entry_free(s, a_entry);
921 return avahi_server_errno(s);
927 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
930 if (g->state == state)
933 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
935 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
937 /* If the entry group was established for a time longer then
938 * 5s, reset the establishment trial counter */
940 if (avahi_age(&g->established_at) > 5000000)
941 g->n_register_try = 0;
944 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
946 /* If the entry group is now established, remember the time
949 gettimeofday(&g->established_at, NULL);
954 g->callback(g->server, g, state, g->userdata);
957 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
962 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
963 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
968 g->callback = callback;
969 g->userdata = userdata;
971 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
973 g->n_register_try = 0;
974 g->register_time_event = NULL;
975 g->register_time.tv_sec = 0;
976 g->register_time.tv_usec = 0;
977 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
979 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
983 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
989 for (e = g->entries; e; e = e->by_group_next) {
991 avahi_goodbye_entry(g->server, e, 1, 1);
996 if (g->register_time_event) {
997 avahi_time_event_free(g->register_time_event);
998 g->register_time_event = NULL;
1003 g->server->need_group_cleanup = 1;
1004 g->server->need_entry_cleanup = 1;
1007 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1010 gettimeofday(&g->register_time, NULL);
1012 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1015 avahi_announce_group(g->server, g);
1016 avahi_s_entry_group_check_probed(g, 0);
1020 static void entry_group_register_time_event_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* userdata) {
1021 AvahiSEntryGroup *g = userdata;
1024 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
1026 avahi_time_event_free(g->register_time_event);
1027 g->register_time_event = NULL;
1029 /* Holdoff time passed, so let's start probing */
1030 entry_group_commit_real(g);
1033 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1039 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1040 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1042 g->n_register_try++;
1044 avahi_timeval_add(&g->register_time,
1045 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1046 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1047 AVAHI_RR_HOLDOFF_MSEC));
1049 gettimeofday(&now, NULL);
1051 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1052 /* Holdoff time passed, so let's start probing */
1053 /* avahi_log_debug("Holdoff passed, directly going on."); */
1055 entry_group_commit_real(g);
1057 /* avahi_log_debug("Holdoff not passed, sleeping."); */
1059 /* Holdoff time has not yet passed, so let's wait */
1060 assert(!g->register_time_event);
1061 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1063 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1069 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1073 for (e = g->entries; e; e = e->by_group_next) {
1075 avahi_goodbye_entry(g->server, e, 1, 1);
1079 g->server->need_entry_cleanup = 1;
1081 if (g->register_time_event) {
1082 avahi_time_event_free(g->register_time_event);
1083 g->register_time_event = NULL;
1088 gettimeofday(&g->register_time, NULL);
1090 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1093 int avahi_entry_is_commited(AvahiEntry *e) {
1098 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1099 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1102 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1109 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1112 g->userdata = userdata;
1115 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1121 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1125 /* Look for an entry that is not dead */
1126 for (e = g->entries; e; e = e->by_group_next)