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 int avahi_server_add(
155 AvahiIfIndex interface,
156 AvahiProtocol protocol,
157 AvahiPublishFlags flags,
163 if (!AVAHI_IF_VALID(interface))
164 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE);
166 if (!AVAHI_PROTO_VALID(protocol))
167 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL);
169 if (!AVAHI_FLAGS_VALID(
171 AVAHI_PUBLISH_NO_ANNOUNCE|
172 AVAHI_PUBLISH_NO_PROBE|
173 AVAHI_PUBLISH_UNIQUE|
174 AVAHI_PUBLISH_ALLOW_MULTIPLE|
175 AVAHI_PUBLISH_IS_PROXY|
176 AVAHI_PUBLISH_UPDATE))
177 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS);
179 if (!avahi_is_valid_domain_name(r->key->name))
180 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
183 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_TTL);
185 if (avahi_key_is_pattern(r->key))
186 return avahi_server_set_errno(s, AVAHI_ERR_IS_PATTERN);
188 if (!avahi_record_is_valid(r))
189 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_RECORD);
191 if (flags & AVAHI_PUBLISH_UPDATE) {
193 AvahiRecord *old_record;
196 /* Update and existing record */
198 /* Find the first matching entry */
199 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
200 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
206 /* Hmm, nothing found? */
208 return avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
210 /* Update the entry */
211 old_record = e->record;
212 e->record = avahi_record_ref(r);
215 /* Announce our changes when needed */
216 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
218 /* Remove the old entry from all caches, if needed */
219 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
220 avahi_goodbye_entry(s, e, 1, 0);
222 /* Reannounce our updated entry */
223 avahi_reannounce_entry(s, e);
226 /* If we were the first entry in the list, we need to update the key */
228 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
230 avahi_record_unref(old_record);
235 /* Add a new record */
237 if (check_record_conflict(s, interface, protocol, r, flags) < 0)
238 return avahi_server_set_errno(s, AVAHI_ERR_LOCAL_COLLISION);
240 if (!(e = avahi_new(AvahiEntry, 1)))
241 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
244 e->record = avahi_record_ref(r);
246 e->interface = interface;
247 e->protocol = protocol;
251 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
253 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
255 /* Insert into hash table indexed by name */
256 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
257 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
258 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
260 /* Insert into group list */
262 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
264 avahi_announce_entry(s, e);
270 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
271 AvahiEntry **e = (AvahiEntry**) state;
276 *e = g ? g->entries : s->entries;
278 while (*e && (*e)->dead)
279 *e = g ? (*e)->by_group_next : (*e)->entries_next;
284 return avahi_record_ref((*e)->record);
287 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
293 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
295 for (e = s->entries; e; e = e->entries_next) {
302 if (!(t = avahi_record_to_string(e->record)))
303 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
305 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
308 callback(ln, userdata);
311 avahi_dump_caches(s->monitor, callback, userdata);
313 if (s->wide_area_lookup_engine)
314 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
318 int avahi_server_add_ptr(
321 AvahiIfIndex interface,
322 AvahiProtocol protocol,
323 AvahiPublishFlags flags,
334 if ((name && !avahi_is_valid_domain_name(name)) || !avahi_is_valid_domain_name(dest))
335 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
338 name = s->host_name_fqdn;
340 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl)))
341 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
343 r->data.ptr.name = avahi_normalize_name_strdup(dest);
344 ret = avahi_server_add(s, g, interface, protocol, flags, r);
345 avahi_record_unref(r);
349 int avahi_server_add_address(
352 AvahiIfIndex interface,
353 AvahiProtocol protocol,
354 AvahiPublishFlags flags,
364 if (!AVAHI_IF_VALID(interface))
365 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE);
367 if (!AVAHI_PROTO_VALID(protocol) || !AVAHI_PROTO_VALID(a->proto))
368 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL);
370 if (!AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_REVERSE|AVAHI_PUBLISH_NO_ANNOUNCE|AVAHI_PUBLISH_NO_PROBE|AVAHI_PUBLISH_UPDATE))
371 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS);
373 if (name && !avahi_is_valid_domain_name(name))
374 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
377 name = s->host_name_fqdn;
379 if (!(n = avahi_normalize_name_strdup(name)))
380 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
385 if (a->proto == AVAHI_PROTO_INET) {
388 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
389 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
393 r->data.a.address = a->data.ipv4;
394 ret = avahi_server_add(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
395 avahi_record_unref(r);
400 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
403 if (!(reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4))) {
404 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
408 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
415 assert(a->proto == AVAHI_PROTO_INET6);
417 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
418 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
422 r->data.aaaa.address = a->data.ipv6;
423 ret = avahi_server_add(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
424 avahi_record_unref(r);
429 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
432 if (!(reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6))) {
433 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
437 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
443 if (!(reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6))) {
444 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
448 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
460 static int server_add_txt_strlst_nocopy(
463 AvahiIfIndex interface,
464 AvahiProtocol protocol,
465 AvahiPublishFlags flags,
468 AvahiStringList *strlst) {
475 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
476 avahi_string_list_free(strlst);
477 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
480 r->data.txt.string_list = strlst;
481 ret = avahi_server_add(s, g, interface, protocol, flags, r);
482 avahi_record_unref(r);
487 static AvahiStringList *add_magic_cookie(
489 AvahiStringList *strlst) {
493 if (!s->config.add_service_cookie)
496 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
497 /* This string list already contains a magic cookie */
500 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
503 static int server_add_service_strlst_nocopy(
506 AvahiIfIndex interface,
507 AvahiProtocol protocol,
508 AvahiPublishFlags flags,
514 AvahiStringList *strlst) {
516 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
517 AvahiRecord *r = NULL;
524 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
525 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
526 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_NO_COOKIE|AVAHI_PUBLISH_IS_PROXY|AVAHI_PUBLISH_UPDATE), AVAHI_ERR_INVALID_FLAGS);
527 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
528 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
529 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
530 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
533 domain = s->domain_name;
536 host = s->host_name_fqdn;
538 if (!(h = avahi_normalize_name_strdup(host))) {
539 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
543 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
544 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
545 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
546 avahi_server_set_errno(s, ret);
550 /* Add service enumeration PTR record */
552 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
557 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
558 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
562 r->data.srv.priority = 0;
563 r->data.srv.weight = 0;
564 r->data.srv.port = port;
565 r->data.srv.name = h;
567 ret = avahi_server_add(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE, r);
568 avahi_record_unref(r);
575 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
576 strlst = add_magic_cookie(s, strlst);
578 ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
584 /* Add service type enumeration record */
586 ret = avahi_server_add_ptr(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY), AVAHI_DEFAULT_TTL, enum_ptr, ptr_name);
590 avahi_string_list_free(strlst);
596 int avahi_server_add_service_strlst(
599 AvahiIfIndex interface,
600 AvahiProtocol protocol,
601 AvahiPublishFlags flags,
607 AvahiStringList *strlst) {
613 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
616 int avahi_server_add_service_va(
619 AvahiIfIndex interface,
620 AvahiProtocol protocol,
621 AvahiPublishFlags flags,
633 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
636 int avahi_server_add_service(
639 AvahiIfIndex interface,
640 AvahiProtocol protocol,
641 AvahiPublishFlags flags,
653 ret = avahi_server_add_service_va(s, g, interface, protocol, flags, name, type, domain, host, port, va);
659 static int server_update_service_txt_strlst_nocopy(
662 AvahiIfIndex interface,
663 AvahiProtocol protocol,
664 AvahiPublishFlags flags,
668 AvahiStringList *strlst) {
670 char svc_name[AVAHI_DOMAIN_NAME_MAX];
677 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
678 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
679 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_IS_PROXY|AVAHI_PUBLISH_NO_COOKIE), AVAHI_ERR_INVALID_FLAGS);
680 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
681 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
682 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
685 domain = s->domain_name;
687 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
688 avahi_server_set_errno(s, ret);
693 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
694 strlst = add_magic_cookie(s, strlst);
696 ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, (flags & AVAHI_PUBLISH_IS_PROXY) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
701 avahi_string_list_free(strlst);
706 int avahi_server_update_service_txt_strlst(
709 AvahiIfIndex interface,
710 AvahiProtocol protocol,
711 AvahiPublishFlags flags,
715 AvahiStringList *strlst) {
717 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
720 /** Update the TXT record for a service with the NULL terminated list of strings of the va_list. */
721 int avahi_server_update_service_txt_va(
724 AvahiIfIndex interface,
725 AvahiProtocol protocol,
726 AvahiPublishFlags flags,
732 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
735 /** Update the TXT record for a service with the NULL termonate list of strings */
736 int avahi_server_update_service_txt(
739 AvahiIfIndex interface,
740 AvahiProtocol protocol,
741 AvahiPublishFlags flags,
750 va_start(va, domain);
751 ret = avahi_server_update_service_txt_va(s, g, interface, protocol, flags, name, type, domain, va);
757 int avahi_server_add_service_subtype(
760 AvahiIfIndex interface,
761 AvahiProtocol protocol,
762 AvahiPublishFlags flags,
766 const char *subtype) {
769 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
775 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
776 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
777 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, 0), AVAHI_ERR_INVALID_FLAGS);
778 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
779 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
780 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
781 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
784 domain = s->domain_name;
786 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
787 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
788 avahi_server_set_errno(s, ret);
792 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, flags & AVAHI_PUBLISH_IS_PROXY, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
800 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
801 static const char hex[] = "0123456789abcdef";
803 const uint8_t *k = p;
805 while (sl > 1 && pl > 0) {
806 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
822 int avahi_server_add_dns_server_address(
825 AvahiIfIndex interface,
826 AvahiProtocol protocol,
827 AvahiPublishFlags flags,
829 AvahiDNSServerType type,
830 const AvahiAddress *address,
831 uint16_t port /** should be 53 */) {
840 if (!AVAHI_IF_VALID(interface))
841 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE);
843 if (!AVAHI_PROTO_VALID(protocol) || !AVAHI_PROTO_VALID(address->proto))
844 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL);
846 if (!AVAHI_FLAGS_VALID(flags, 0) || (type != AVAHI_DNS_SERVER_UPDATE && type != AVAHI_DNS_SERVER_RESOLVE))
847 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS);
850 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
852 if (domain && !avahi_is_valid_domain_name(domain))
853 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
856 domain = s->domain_name;
858 if (address->proto == AVAHI_PROTO_INET) {
859 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
860 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
861 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
862 r->data.a.address = address->data.ipv4;
864 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
865 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
866 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
867 r->data.aaaa.address = address->data.ipv6;
871 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
873 ret = avahi_server_add(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
874 avahi_record_unref(r);
879 return avahi_server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port);
882 int avahi_server_add_dns_server_name(
885 AvahiIfIndex interface,
886 AvahiProtocol protocol,
887 AvahiPublishFlags flags,
889 AvahiDNSServerType type,
891 uint16_t port /** should be 53 */) {
894 char t[256], *d = NULL, *n = NULL;
900 if (!AVAHI_IF_VALID(interface))
901 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_INTERFACE);
903 if (!AVAHI_PROTO_VALID(protocol))
904 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PROTOCOL);
906 if (!AVAHI_FLAGS_VALID(flags, 0) || (type != AVAHI_DNS_SERVER_UPDATE && type != AVAHI_DNS_SERVER_RESOLVE))
907 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_FLAGS);
910 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
912 if (!avahi_is_valid_domain_name(name))
913 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
915 if (domain && !avahi_is_valid_domain_name(domain))
916 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
919 domain = s->domain_name;
921 if (!(n = avahi_normalize_name_strdup(name)) ||
922 !(d = avahi_normalize_name_strdup(domain))) {
925 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
928 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d);
931 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
933 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
936 r->data.srv.priority = 0;
937 r->data.srv.weight = 0;
938 r->data.srv.port = port;
939 r->data.srv.name = n;
940 ret = avahi_server_add(s, g, interface, protocol, AVAHI_PUBLISH_NULL, r);
941 avahi_record_unref(r);
946 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
949 if (g->state == state)
952 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
954 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
956 /* If the entry group was established for a time longer then
957 * 5s, reset the establishment trial counter */
959 if (avahi_age(&g->established_at) > 5000000)
960 g->n_register_try = 0;
963 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
965 /* If the entry group is now established, remember the time
968 gettimeofday(&g->established_at, NULL);
973 g->callback(g->server, g, state, g->userdata);
976 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
981 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
982 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
987 g->callback = callback;
988 g->userdata = userdata;
990 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
992 g->n_register_try = 0;
993 g->register_time_event = NULL;
994 g->register_time.tv_sec = 0;
995 g->register_time.tv_usec = 0;
996 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
998 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
1002 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
1008 for (e = g->entries; e; e = e->by_group_next) {
1010 avahi_goodbye_entry(g->server, e, 1, 1);
1015 if (g->register_time_event) {
1016 avahi_time_event_free(g->register_time_event);
1017 g->register_time_event = NULL;
1022 g->server->need_group_cleanup = 1;
1023 g->server->need_entry_cleanup = 1;
1026 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1029 gettimeofday(&g->register_time, NULL);
1031 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1034 avahi_announce_group(g->server, g);
1035 avahi_s_entry_group_check_probed(g, 0);
1039 static void entry_group_register_time_event_callback(AvahiTimeEvent *e, void* userdata) {
1040 AvahiSEntryGroup *g = userdata;
1043 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
1045 avahi_time_event_free(g->register_time_event);
1046 g->register_time_event = NULL;
1048 /* Holdoff time passed, so let's start probing */
1049 entry_group_commit_real(g);
1052 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1058 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1059 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1061 g->n_register_try++;
1063 avahi_timeval_add(&g->register_time,
1064 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1065 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1066 AVAHI_RR_HOLDOFF_MSEC));
1068 gettimeofday(&now, NULL);
1070 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1071 /* Holdoff time passed, so let's start probing */
1072 /* avahi_log_debug("Holdoff passed, directly going on."); */
1074 entry_group_commit_real(g);
1076 /* avahi_log_debug("Holdoff not passed, sleeping."); */
1078 /* Holdoff time has not yet passed, so let's wait */
1079 assert(!g->register_time_event);
1080 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1082 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1088 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1092 for (e = g->entries; e; e = e->by_group_next) {
1094 avahi_goodbye_entry(g->server, e, 1, 1);
1098 g->server->need_entry_cleanup = 1;
1100 if (g->register_time_event) {
1101 avahi_time_event_free(g->register_time_event);
1102 g->register_time_event = NULL;
1107 gettimeofday(&g->register_time, NULL);
1109 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1112 int avahi_entry_is_commited(AvahiEntry *e) {
1117 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1118 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1121 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1128 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1131 g->userdata = userdata;
1134 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1140 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1144 /* Look for an entry that is not dead */
1145 for (e = g->entries; e; e = e->by_group_next)