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"
49 void avahi_entry_free(AvahiServer*s, AvahiEntry *e) {
55 avahi_goodbye_entry(s, e, 1, 1);
57 /* Remove from linked list */
58 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
60 /* Remove from hash table indexed by name */
61 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
62 AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
64 avahi_hashmap_replace(s->entries_by_key, t->record->key, t);
66 avahi_hashmap_remove(s->entries_by_key, e->record->key);
68 /* Remove from associated group */
70 AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
72 avahi_record_unref(e->record);
76 void avahi_entry_group_free(AvahiServer *s, AvahiSEntryGroup *g) {
81 avahi_entry_free(s, g->entries);
83 if (g->register_time_event)
84 avahi_time_event_free(g->register_time_event);
86 AVAHI_LLIST_REMOVE(AvahiSEntryGroup, groups, s->groups, g);
90 void avahi_cleanup_dead_entries(AvahiServer *s) {
93 if (s->need_group_cleanup) {
94 AvahiSEntryGroup *g, *next;
96 for (g = s->groups; g; g = next) {
97 next = g->groups_next;
100 avahi_entry_group_free(s, g);
103 s->need_group_cleanup = 0;
106 if (s->need_entry_cleanup) {
107 AvahiEntry *e, *next;
109 for (e = s->entries; e; e = next) {
110 next = e->entries_next;
113 avahi_entry_free(s, e);
116 s->need_entry_cleanup = 0;
119 if (s->need_browser_cleanup)
120 avahi_browser_cleanup(s);
123 static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiPublishFlags flags) {
129 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
133 if (!(flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_UNIQUE))
136 if ((flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) && (e->flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) )
139 if ((interface <= 0 ||
141 e->interface == interface) &&
142 (protocol == AVAHI_PROTO_UNSPEC ||
143 e->protocol == AVAHI_PROTO_UNSPEC ||
144 e->protocol == protocol))
152 static AvahiEntry * server_add_internal(
155 AvahiIfIndex interface,
156 AvahiProtocol protocol,
157 AvahiPublishFlags flags,
165 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
166 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
167 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(
169 AVAHI_PUBLISH_NO_ANNOUNCE|
170 AVAHI_PUBLISH_NO_PROBE|
171 AVAHI_PUBLISH_UNIQUE|
172 AVAHI_PUBLISH_ALLOW_MULTIPLE|
173 AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
174 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(r->key->name), AVAHI_ERR_INVALID_HOST_NAME);
175 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, r->ttl != 0, AVAHI_ERR_INVALID_TTL);
176 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !avahi_key_is_pattern(r->key), AVAHI_ERR_IS_PATTERN);
177 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_record_is_valid(r), AVAHI_ERR_INVALID_RECORD);
179 if (flags & AVAHI_PUBLISH_UPDATE) {
180 AvahiRecord *old_record;
183 /* Update and existing record */
185 /* Find the first matching entry */
186 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
187 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
193 /* Hmm, nothing found? */
195 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
199 /* Update the entry */
200 old_record = e->record;
201 e->record = avahi_record_ref(r);
204 /* Announce our changes when needed */
205 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
207 /* Remove the old entry from all caches, if needed */
208 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
209 avahi_goodbye_entry(s, e, 1, 0);
211 /* Reannounce our updated entry */
212 avahi_reannounce_entry(s, e);
215 /* If we were the first entry in the list, we need to update the key */
217 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
219 avahi_record_unref(old_record);
224 /* Add a new record */
226 if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
227 avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
231 if (!(e = avahi_new(AvahiEntry, 1))) {
232 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
237 e->record = avahi_record_ref(r);
239 e->interface = interface;
240 e->protocol = protocol;
244 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
246 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
248 /* Insert into hash table indexed by name */
249 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
250 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
251 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
253 /* Insert into group list */
255 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
257 avahi_announce_entry(s, e);
263 int avahi_server_add(
266 AvahiIfIndex interface,
267 AvahiProtocol protocol,
268 AvahiPublishFlags flags,
271 if (!server_add_internal(s, g, interface, protocol, flags, r))
272 return avahi_server_errno(s);
277 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
278 AvahiEntry **e = (AvahiEntry**) state;
283 *e = g ? g->entries : s->entries;
285 while (*e && (*e)->dead)
286 *e = g ? (*e)->by_group_next : (*e)->entries_next;
291 return avahi_record_ref((*e)->record);
294 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
300 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
302 for (e = s->entries; e; e = e->entries_next) {
309 if (!(t = avahi_record_to_string(e->record)))
310 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
312 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
315 callback(ln, userdata);
318 avahi_dump_caches(s->monitor, callback, userdata);
320 if (s->wide_area_lookup_engine)
321 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
325 static AvahiEntry *server_add_ptr_internal(
328 AvahiIfIndex interface,
329 AvahiProtocol protocol,
330 AvahiPublishFlags flags,
341 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
342 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(dest), AVAHI_ERR_INVALID_HOST_NAME);
345 name = s->host_name_fqdn;
347 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) {
348 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
352 r->data.ptr.name = avahi_normalize_name_strdup(dest);
353 e = server_add_internal(s, g, interface, protocol, flags, r);
354 avahi_record_unref(r);
358 int avahi_server_add_ptr(
361 AvahiIfIndex interface,
362 AvahiProtocol protocol,
363 AvahiPublishFlags flags,
372 if (!(e = server_add_ptr_internal(s, g, interface, protocol, flags, ttl, name, dest)))
373 return avahi_server_errno(s);
378 int avahi_server_add_address(
381 AvahiIfIndex interface,
382 AvahiProtocol protocol,
383 AvahiPublishFlags flags,
387 char n[AVAHI_DOMAIN_NAME_MAX];
389 AvahiEntry *entry = NULL, *reverse = NULL;
395 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
396 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(a->proto), AVAHI_ERR_INVALID_PROTOCOL);
397 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);
398 AVAHI_CHECK_VALIDITY(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
400 /* Prepare the host naem */
403 name = s->host_name_fqdn;
405 AVAHI_ASSERT_TRUE(avahi_normalize_name(name, n, sizeof(n)));
409 /* Create the A/AAAA record */
411 if (a->proto == AVAHI_PROTO_INET) {
413 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
414 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
418 r->data.a.address = a->data.ipv4;
421 assert(a->proto == AVAHI_PROTO_INET6);
423 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
424 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
428 r->data.aaaa.address = a->data.ipv6;
431 entry = server_add_internal(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
432 avahi_record_unref(r);
435 ret = avahi_server_errno(s);
439 /* Create the reverse lookup entry */
441 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
442 char reverse_n[AVAHI_DOMAIN_NAME_MAX];
443 avahi_reverse_lookup_name(reverse_n, sizeof(reverse_n), a);
445 if (!(reverse = server_add_ptr_internal(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
446 ret = avahi_server_errno(s);
453 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
455 avahi_entry_free(s, entry);
457 avahi_entry_free(s, reverse);
463 static AvahiEntry *server_add_txt_strlst_nocopy(
466 AvahiIfIndex interface,
467 AvahiProtocol protocol,
468 AvahiPublishFlags flags,
471 AvahiStringList *strlst) {
478 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
479 avahi_string_list_free(strlst);
480 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
484 r->data.txt.string_list = strlst;
485 e = server_add_internal(s, g, interface, protocol, flags, r);
486 avahi_record_unref(r);
491 static AvahiStringList *add_magic_cookie(
493 AvahiStringList *strlst) {
497 if (!s->config.add_service_cookie)
500 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
501 /* This string list already contains a magic cookie */
504 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
507 static int server_add_service_strlst_nocopy(
510 AvahiIfIndex interface,
511 AvahiProtocol protocol,
512 AvahiPublishFlags flags,
518 AvahiStringList *strlst) {
520 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
521 AvahiRecord *r = NULL;
523 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
529 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
530 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
531 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
532 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
533 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
534 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
535 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
538 domain = s->domain_name;
541 host = s->host_name_fqdn;
543 if (!(h = avahi_normalize_name_strdup(host))) {
544 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
548 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
549 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
550 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
551 avahi_server_set_errno(s, ret);
555 /* Add service enumeration PTR record */
557 if (!(ptr_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name))) {
558 ret = avahi_server_errno(s);
564 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
565 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
569 r->data.srv.priority = 0;
570 r->data.srv.weight = 0;
571 r->data.srv.port = port;
572 r->data.srv.name = h;
574 srv_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r);
575 avahi_record_unref(r);
578 ret = avahi_server_errno(s);
584 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
585 strlst = add_magic_cookie(s, strlst);
587 txt_entry = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
591 ret = avahi_server_errno(s);
595 /* Add service type enumeration record */
597 if (!(enum_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name))) {
598 ret = avahi_server_errno(s);
603 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
605 avahi_entry_free(s, srv_entry);
607 avahi_entry_free(s, txt_entry);
609 avahi_entry_free(s, ptr_entry);
611 avahi_entry_free(s, enum_entry);
614 avahi_string_list_free(strlst);
620 int avahi_server_add_service_strlst(
623 AvahiIfIndex interface,
624 AvahiProtocol protocol,
625 AvahiPublishFlags flags,
631 AvahiStringList *strlst) {
637 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
640 int avahi_server_add_service(
643 AvahiIfIndex interface,
644 AvahiProtocol protocol,
645 AvahiPublishFlags flags,
657 ret = server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
663 static int server_update_service_txt_strlst_nocopy(
666 AvahiIfIndex interface,
667 AvahiProtocol protocol,
668 AvahiPublishFlags flags,
672 AvahiStringList *strlst) {
674 char svc_name[AVAHI_DOMAIN_NAME_MAX];
682 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
683 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
684 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS);
685 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
686 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
687 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
690 domain = s->domain_name;
692 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
693 avahi_server_set_errno(s, ret);
698 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
699 strlst = add_magic_cookie(s, strlst);
701 e = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
705 ret = avahi_server_errno(s);
709 avahi_string_list_free(strlst);
714 int avahi_server_update_service_txt_strlst(
717 AvahiIfIndex interface,
718 AvahiProtocol protocol,
719 AvahiPublishFlags flags,
723 AvahiStringList *strlst) {
725 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
728 /** Update the TXT record for a service with the NULL termonate list of strings */
729 int avahi_server_update_service_txt(
732 AvahiIfIndex interface,
733 AvahiProtocol protocol,
734 AvahiPublishFlags flags,
743 va_start(va, domain);
744 ret = server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
750 int avahi_server_add_service_subtype(
753 AvahiIfIndex interface,
754 AvahiProtocol protocol,
755 AvahiPublishFlags flags,
759 const char *subtype) {
762 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
768 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
769 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
770 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
771 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
772 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
773 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
774 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
777 domain = s->domain_name;
779 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
780 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
781 avahi_server_set_errno(s, ret);
785 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
793 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
794 static const char hex[] = "0123456789abcdef";
796 const uint8_t *k = p;
798 while (sl > 1 && pl > 0) {
799 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
815 static AvahiEntry * server_add_dns_server_name(
818 AvahiIfIndex interface,
819 AvahiProtocol protocol,
820 AvahiPublishFlags flags,
822 AvahiDNSServerType type,
824 uint16_t port /** should be 53 */) {
827 char t[AVAHI_DOMAIN_NAME_MAX], normalized_d[AVAHI_DOMAIN_NAME_MAX], *n;
834 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
835 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
836 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, port != 0, AVAHI_ERR_INVALID_PORT);
837 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
838 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
841 domain = s->domain_name;
843 if (!(n = avahi_normalize_name_strdup(name))) {
844 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
848 AVAHI_ASSERT_TRUE(avahi_normalize_name(domain, normalized_d, sizeof(normalized_d)));
850 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
852 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
853 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
858 r->data.srv.priority = 0;
859 r->data.srv.weight = 0;
860 r->data.srv.port = port;
861 r->data.srv.name = n;
862 e = server_add_internal(s, g, interface, protocol, 0, r);
863 avahi_record_unref(r);
868 int avahi_server_add_dns_server_address(
871 AvahiIfIndex interface,
872 AvahiProtocol protocol,
873 AvahiPublishFlags flags,
875 AvahiDNSServerType type,
876 const AvahiAddress *address,
877 uint16_t port /** should be 53 */) {
881 AvahiEntry *a_entry, *s_entry;
886 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
887 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(address->proto), AVAHI_ERR_INVALID_PROTOCOL);
888 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
889 AVAHI_CHECK_VALIDITY(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
890 AVAHI_CHECK_VALIDITY(s, port != 0, AVAHI_ERR_INVALID_PORT);
891 AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
894 domain = s->domain_name;
896 if (address->proto == AVAHI_PROTO_INET) {
897 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
898 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
899 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
900 r->data.a.address = address->data.ipv4;
902 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
903 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
904 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
905 r->data.aaaa.address = address->data.ipv6;
909 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
911 a_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
912 avahi_record_unref(r);
915 return avahi_server_errno(s);
917 if (!(s_entry = server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port))) {
918 if (!(flags & AVAHI_PUBLISH_UPDATE))
919 avahi_entry_free(s, a_entry);
920 return avahi_server_errno(s);
926 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
929 if (g->state == state)
932 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
934 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
936 /* If the entry group was established for a time longer then
937 * 5s, reset the establishment trial counter */
939 if (avahi_age(&g->established_at) > 5000000)
940 g->n_register_try = 0;
943 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
945 /* If the entry group is now established, remember the time
948 gettimeofday(&g->established_at, NULL);
953 g->callback(g->server, g, state, g->userdata);
956 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
961 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
962 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
967 g->callback = callback;
968 g->userdata = userdata;
970 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
972 g->n_register_try = 0;
973 g->register_time_event = NULL;
974 g->register_time.tv_sec = 0;
975 g->register_time.tv_usec = 0;
976 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
978 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
982 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
988 for (e = g->entries; e; e = e->by_group_next) {
990 avahi_goodbye_entry(g->server, e, 1, 1);
995 if (g->register_time_event) {
996 avahi_time_event_free(g->register_time_event);
997 g->register_time_event = NULL;
1002 g->server->need_group_cleanup = 1;
1003 g->server->need_entry_cleanup = 1;
1006 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1009 gettimeofday(&g->register_time, NULL);
1011 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1014 avahi_announce_group(g->server, g);
1015 avahi_s_entry_group_check_probed(g, 0);
1019 static void entry_group_register_time_event_callback(AvahiTimeEvent *e, void* userdata) {
1020 AvahiSEntryGroup *g = userdata;
1023 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
1025 avahi_time_event_free(g->register_time_event);
1026 g->register_time_event = NULL;
1028 /* Holdoff time passed, so let's start probing */
1029 entry_group_commit_real(g);
1032 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1038 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1039 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1041 g->n_register_try++;
1043 avahi_timeval_add(&g->register_time,
1044 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1045 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1046 AVAHI_RR_HOLDOFF_MSEC));
1048 gettimeofday(&now, NULL);
1050 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1051 /* Holdoff time passed, so let's start probing */
1052 /* avahi_log_debug("Holdoff passed, directly going on."); */
1054 entry_group_commit_real(g);
1056 /* avahi_log_debug("Holdoff not passed, sleeping."); */
1058 /* Holdoff time has not yet passed, so let's wait */
1059 assert(!g->register_time_event);
1060 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1062 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1068 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1072 for (e = g->entries; e; e = e->by_group_next) {
1074 avahi_goodbye_entry(g->server, e, 1, 1);
1078 g->server->need_entry_cleanup = 1;
1080 if (g->register_time_event) {
1081 avahi_time_event_free(g->register_time_event);
1082 g->register_time_event = NULL;
1087 gettimeofday(&g->register_time, NULL);
1089 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1092 int avahi_entry_is_commited(AvahiEntry *e) {
1097 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1098 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1101 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1108 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1111 g->userdata = userdata;
1114 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1120 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1124 /* Look for an entry that is not dead */
1125 for (e = g->entries; e; e = e->by_group_next)