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
33 #include <arpa/inet.h>
35 #include <sys/utsname.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <avahi-common/domain.h>
40 #include <avahi-common/timeval.h>
41 #include <avahi-common/malloc.h>
42 #include <avahi-common/error.h>
43 #include <avahi-common/domain.h>
51 #include "dns-srv-rr.h"
53 #include "domain-util.h"
55 static void transport_flags_from_domain(AvahiServer *s, AvahiPublishFlags *flags, const char *domain) {
59 assert(!((*flags & AVAHI_PUBLISH_USE_MULTICAST) && (*flags & AVAHI_PUBLISH_USE_WIDE_AREA)));
61 if (*flags & (AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_USE_WIDE_AREA))
64 if (!s->wide_area_lookup_engine ||
65 !avahi_wide_area_has_servers(s->wide_area_lookup_engine) ||
66 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_LOCAL) ||
67 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV4) ||
68 avahi_domain_ends_with(domain, AVAHI_MDNS_SUFFIX_ADDR_IPV6))
69 *flags |= AVAHI_PUBLISH_USE_MULTICAST;
71 *flags |= AVAHI_PUBLISH_USE_WIDE_AREA;
74 void avahi_entry_free(AvahiServer*s, AvahiEntry *e) {
80 avahi_goodbye_entry(s, e, 1, 1);
82 /* Remove from linked list */
83 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
85 /* Remove from hash table indexed by name */
86 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
87 AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
89 avahi_hashmap_replace(s->entries_by_key, t->record->key, t);
91 avahi_hashmap_remove(s->entries_by_key, e->record->key);
93 /* Remove from associated group */
95 AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
97 avahi_record_unref(e->record);
101 void avahi_entry_group_free(AvahiServer *s, AvahiSEntryGroup *g) {
106 avahi_entry_free(s, g->entries);
108 if (g->register_time_event)
109 avahi_time_event_free(g->register_time_event);
111 AVAHI_LLIST_REMOVE(AvahiSEntryGroup, groups, s->groups, g);
115 void avahi_cleanup_dead_entries(AvahiServer *s) {
118 if (s->need_group_cleanup) {
119 AvahiSEntryGroup *g, *next;
121 for (g = s->groups; g; g = next) {
122 next = g->groups_next;
125 avahi_entry_group_free(s, g);
128 s->need_group_cleanup = 0;
131 if (s->need_entry_cleanup) {
132 AvahiEntry *e, *next;
134 for (e = s->entries; e; e = next) {
135 next = e->entries_next;
138 avahi_entry_free(s, e);
141 s->need_entry_cleanup = 0;
144 if (s->need_browser_cleanup)
145 avahi_browser_cleanup(s);
148 static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiPublishFlags flags) {
154 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
158 if (!(flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_UNIQUE))
161 if ((flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) && (e->flags & AVAHI_PUBLISH_ALLOW_MULTIPLE) )
164 if ((interface <= 0 ||
166 e->interface == interface) &&
167 (protocol == AVAHI_PROTO_UNSPEC ||
168 e->protocol == AVAHI_PROTO_UNSPEC ||
169 e->protocol == protocol))
177 static AvahiEntry * server_add_internal(
180 AvahiIfIndex interface,
181 AvahiProtocol protocol,
182 AvahiPublishFlags flags,
190 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, s->state != AVAHI_SERVER_FAILURE && s->state != AVAHI_SERVER_INVALID, AVAHI_ERR_BAD_STATE);
191 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
192 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
193 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(
195 AVAHI_PUBLISH_NO_ANNOUNCE|
196 AVAHI_PUBLISH_NO_PROBE|
197 AVAHI_PUBLISH_UNIQUE|
198 AVAHI_PUBLISH_ALLOW_MULTIPLE|
199 AVAHI_PUBLISH_UPDATE|
200 AVAHI_PUBLISH_USE_WIDE_AREA|
201 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
202 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(r->key->name), AVAHI_ERR_INVALID_HOST_NAME);
203 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, r->ttl != 0, AVAHI_ERR_INVALID_TTL);
204 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !avahi_key_is_pattern(r->key), AVAHI_ERR_IS_PATTERN);
205 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_record_is_valid(r), AVAHI_ERR_INVALID_RECORD);
206 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, r->key->clazz == AVAHI_DNS_CLASS_IN, AVAHI_ERR_INVALID_DNS_CLASS);
207 AVAHI_CHECK_VALIDITY_RETURN_NULL(s,
208 (r->key->type != 0) &&
209 (r->key->type != AVAHI_DNS_TYPE_ANY) &&
210 (r->key->type != AVAHI_DNS_TYPE_OPT) &&
211 (r->key->type != AVAHI_DNS_TYPE_TKEY) &&
212 (r->key->type != AVAHI_DNS_TYPE_TSIG) &&
213 (r->key->type != AVAHI_DNS_TYPE_IXFR) &&
214 (r->key->type != AVAHI_DNS_TYPE_AXFR), AVAHI_ERR_INVALID_DNS_TYPE);
216 transport_flags_from_domain(s, &flags, r->key->name);
217 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
218 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !s->config.disable_publishing, AVAHI_ERR_NOT_PERMITTED);
219 AVAHI_CHECK_VALIDITY_RETURN_NULL(s,
221 (g->state != AVAHI_ENTRY_GROUP_ESTABLISHED && g->state != AVAHI_ENTRY_GROUP_REGISTERING) ||
222 (flags & AVAHI_PUBLISH_UPDATE), AVAHI_ERR_BAD_STATE);
224 if (flags & AVAHI_PUBLISH_UPDATE) {
225 AvahiRecord *old_record;
228 /* Update and existing record */
230 /* Find the first matching entry */
231 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
232 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
238 /* Hmm, nothing found? */
240 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
244 /* Update the entry */
245 old_record = e->record;
246 e->record = avahi_record_ref(r);
249 /* Announce our changes when needed */
250 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
252 /* Remove the old entry from all caches, if needed */
253 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
254 avahi_goodbye_entry(s, e, 1, 0);
256 /* Reannounce our updated entry */
257 avahi_reannounce_entry(s, e);
260 /* If we were the first entry in the list, we need to update the key */
262 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
264 avahi_record_unref(old_record);
269 /* Add a new record */
271 if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
272 avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
276 if (!(e = avahi_new(AvahiEntry, 1))) {
277 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
282 e->record = avahi_record_ref(r);
284 e->interface = interface;
285 e->protocol = protocol;
289 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
291 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
293 /* Insert into hash table indexed by name */
294 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
295 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
296 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
298 /* Insert into group list */
300 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
302 avahi_announce_entry(s, e);
308 int avahi_server_add(
311 AvahiIfIndex interface,
312 AvahiProtocol protocol,
313 AvahiPublishFlags flags,
316 if (!server_add_internal(s, g, interface, protocol, flags, r))
317 return avahi_server_errno(s);
322 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
323 AvahiEntry **e = (AvahiEntry**) state;
328 *e = g ? g->entries : s->entries;
330 while (*e && (*e)->dead)
331 *e = g ? (*e)->by_group_next : (*e)->entries_next;
336 return avahi_record_ref((*e)->record);
339 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
345 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
347 for (e = s->entries; e; e = e->entries_next) {
354 if (!(t = avahi_record_to_string(e->record)))
355 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
357 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
360 callback(ln, userdata);
363 avahi_dump_caches(s->monitor, callback, userdata);
365 if (s->wide_area_lookup_engine)
366 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
370 static AvahiEntry *server_add_ptr_internal(
373 AvahiIfIndex interface,
374 AvahiProtocol protocol,
375 AvahiPublishFlags flags,
386 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
387 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(dest), AVAHI_ERR_INVALID_HOST_NAME);
390 name = s->host_name_fqdn;
392 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) {
393 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
397 r->data.ptr.name = avahi_normalize_name_strdup(dest);
398 e = server_add_internal(s, g, interface, protocol, flags, r);
399 avahi_record_unref(r);
403 int avahi_server_add_ptr(
406 AvahiIfIndex interface,
407 AvahiProtocol protocol,
408 AvahiPublishFlags flags,
417 if (!(e = server_add_ptr_internal(s, g, interface, protocol, flags, ttl, name, dest)))
418 return avahi_server_errno(s);
423 int avahi_server_add_address(
426 AvahiIfIndex interface,
427 AvahiProtocol protocol,
428 AvahiPublishFlags flags,
432 char n[AVAHI_DOMAIN_NAME_MAX];
434 AvahiEntry *entry = NULL, *reverse = NULL;
440 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
441 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(a->proto), AVAHI_ERR_INVALID_PROTOCOL);
442 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags,
443 AVAHI_PUBLISH_NO_REVERSE|
444 AVAHI_PUBLISH_NO_ANNOUNCE|
445 AVAHI_PUBLISH_NO_PROBE|
446 AVAHI_PUBLISH_UPDATE|
447 AVAHI_PUBLISH_USE_WIDE_AREA|
448 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
449 AVAHI_CHECK_VALIDITY(s, !name || avahi_is_valid_fqdn(name), AVAHI_ERR_INVALID_HOST_NAME);
451 /* Prepare the host naem */
454 name = s->host_name_fqdn;
456 AVAHI_ASSERT_TRUE(avahi_normalize_name(name, n, sizeof(n)));
460 transport_flags_from_domain(s, &flags, name);
461 AVAHI_CHECK_VALIDITY(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
463 /* Create the A/AAAA record */
465 if (a->proto == AVAHI_PROTO_INET) {
467 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
468 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
472 r->data.a.address = a->data.ipv4;
475 assert(a->proto == AVAHI_PROTO_INET6);
477 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
478 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
482 r->data.aaaa.address = a->data.ipv6;
485 entry = server_add_internal(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
486 avahi_record_unref(r);
489 ret = avahi_server_errno(s);
493 /* Create the reverse lookup entry */
495 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
496 char reverse_n[AVAHI_DOMAIN_NAME_MAX];
497 avahi_reverse_lookup_name(a, reverse_n, sizeof(reverse_n));
499 if (!(reverse = server_add_ptr_internal(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
500 ret = avahi_server_errno(s);
507 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
509 avahi_entry_free(s, entry);
511 avahi_entry_free(s, reverse);
517 static AvahiEntry *server_add_txt_strlst_nocopy(
520 AvahiIfIndex interface,
521 AvahiProtocol protocol,
522 AvahiPublishFlags flags,
525 AvahiStringList *strlst) {
532 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
533 avahi_string_list_free(strlst);
534 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
538 r->data.txt.string_list = strlst;
539 e = server_add_internal(s, g, interface, protocol, flags, r);
540 avahi_record_unref(r);
545 static AvahiStringList *add_magic_cookie(
547 AvahiStringList *strlst) {
551 if (!s->config.add_service_cookie)
554 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
555 /* This string list already contains a magic cookie */
558 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
561 static int server_add_service_strlst_nocopy(
564 AvahiIfIndex interface,
565 AvahiProtocol protocol,
566 AvahiPublishFlags flags,
572 AvahiStringList *strlst) {
574 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
575 AvahiRecord *r = NULL;
577 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
583 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
584 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
585 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags,
586 AVAHI_PUBLISH_NO_COOKIE|
587 AVAHI_PUBLISH_UPDATE|
588 AVAHI_PUBLISH_USE_WIDE_AREA|
589 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
590 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
591 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
592 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
593 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_fqdn(host), AVAHI_ERR_INVALID_HOST_NAME);
596 domain = s->domain_name;
599 host = s->host_name_fqdn;
601 transport_flags_from_domain(s, &flags, domain);
602 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
604 if (!(h = avahi_normalize_name_strdup(host))) {
605 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
609 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
610 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
611 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
612 avahi_server_set_errno(s, ret);
616 /* Add service enumeration PTR record */
618 if (!(ptr_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name))) {
619 ret = avahi_server_errno(s);
625 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
626 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
630 r->data.srv.priority = 0;
631 r->data.srv.weight = 0;
632 r->data.srv.port = port;
633 r->data.srv.name = h;
635 srv_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r);
636 avahi_record_unref(r);
639 ret = avahi_server_errno(s);
645 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
646 strlst = add_magic_cookie(s, strlst);
648 txt_entry = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
652 ret = avahi_server_errno(s);
656 /* Add service type enumeration record */
658 if (!(enum_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name))) {
659 ret = avahi_server_errno(s);
664 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
666 avahi_entry_free(s, srv_entry);
668 avahi_entry_free(s, txt_entry);
670 avahi_entry_free(s, ptr_entry);
672 avahi_entry_free(s, enum_entry);
675 avahi_string_list_free(strlst);
681 int avahi_server_add_service_strlst(
684 AvahiIfIndex interface,
685 AvahiProtocol protocol,
686 AvahiPublishFlags flags,
692 AvahiStringList *strlst) {
698 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
701 int avahi_server_add_service(
704 AvahiIfIndex interface,
705 AvahiProtocol protocol,
706 AvahiPublishFlags flags,
718 ret = server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
724 static int server_update_service_txt_strlst_nocopy(
727 AvahiIfIndex interface,
728 AvahiProtocol protocol,
729 AvahiPublishFlags flags,
733 AvahiStringList *strlst) {
735 char svc_name[AVAHI_DOMAIN_NAME_MAX];
743 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
744 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
745 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags,
746 AVAHI_PUBLISH_NO_COOKIE|
747 AVAHI_PUBLISH_USE_WIDE_AREA|
748 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
749 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
750 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
751 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
754 domain = s->domain_name;
756 transport_flags_from_domain(s, &flags, domain);
757 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
759 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
760 avahi_server_set_errno(s, ret);
765 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
766 strlst = add_magic_cookie(s, strlst);
768 e = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
772 ret = avahi_server_errno(s);
776 avahi_string_list_free(strlst);
781 int avahi_server_update_service_txt_strlst(
784 AvahiIfIndex interface,
785 AvahiProtocol protocol,
786 AvahiPublishFlags flags,
790 AvahiStringList *strlst) {
792 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
795 /** Update the TXT record for a service with the NULL termonate list of strings */
796 int avahi_server_update_service_txt(
799 AvahiIfIndex interface,
800 AvahiProtocol protocol,
801 AvahiPublishFlags flags,
810 va_start(va, domain);
811 ret = server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
817 int avahi_server_add_service_subtype(
820 AvahiIfIndex interface,
821 AvahiProtocol protocol,
822 AvahiPublishFlags flags,
826 const char *subtype) {
829 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
835 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
836 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
837 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_USE_WIDE_AREA), AVAHI_ERR_INVALID_FLAGS);
838 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
839 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
840 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
841 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
844 domain = s->domain_name;
846 transport_flags_from_domain(s, &flags, domain);
847 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
849 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
850 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
851 avahi_server_set_errno(s, ret);
855 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
863 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
864 static const char hex[] = "0123456789abcdef";
866 const uint8_t *k = p;
868 while (sl > 1 && pl > 0) {
869 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
885 static AvahiEntry *server_add_dns_server_name(
888 AvahiIfIndex interface,
889 AvahiProtocol protocol,
890 AvahiPublishFlags flags,
892 AvahiDNSServerType type,
894 uint16_t port /** should be 53 */) {
897 char t[AVAHI_DOMAIN_NAME_MAX], normalized_d[AVAHI_DOMAIN_NAME_MAX], *n;
904 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_USE_WIDE_AREA|AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
905 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
906 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, port != 0, AVAHI_ERR_INVALID_PORT);
907 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_fqdn(name), AVAHI_ERR_INVALID_HOST_NAME);
908 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
911 domain = s->domain_name;
913 transport_flags_from_domain(s, &flags, domain);
914 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
916 if (!(n = avahi_normalize_name_strdup(name))) {
917 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
921 AVAHI_ASSERT_TRUE(avahi_normalize_name(domain, normalized_d, sizeof(normalized_d)));
923 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
925 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
926 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
931 r->data.srv.priority = 0;
932 r->data.srv.weight = 0;
933 r->data.srv.port = port;
934 r->data.srv.name = n;
935 e = server_add_internal(s, g, interface, protocol, 0, r);
936 avahi_record_unref(r);
941 int avahi_server_add_dns_server_address(
944 AvahiIfIndex interface,
945 AvahiProtocol protocol,
946 AvahiPublishFlags flags,
948 AvahiDNSServerType type,
949 const AvahiAddress *address,
950 uint16_t port /** should be 53 */) {
954 AvahiEntry *a_entry, *s_entry;
959 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
960 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(address->proto), AVAHI_ERR_INVALID_PROTOCOL);
961 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_USE_WIDE_AREA), AVAHI_ERR_INVALID_FLAGS);
962 AVAHI_CHECK_VALIDITY(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
963 AVAHI_CHECK_VALIDITY(s, port != 0, AVAHI_ERR_INVALID_PORT);
964 AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
967 domain = s->domain_name;
969 transport_flags_from_domain(s, &flags, domain);
970 AVAHI_CHECK_VALIDITY(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
972 if (address->proto == AVAHI_PROTO_INET) {
973 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
974 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
975 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
976 r->data.a.address = address->data.ipv4;
978 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
979 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
980 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
981 r->data.aaaa.address = address->data.ipv6;
985 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
987 a_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
988 avahi_record_unref(r);
991 return avahi_server_errno(s);
993 if (!(s_entry = server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port))) {
994 if (!(flags & AVAHI_PUBLISH_UPDATE))
995 avahi_entry_free(s, a_entry);
996 return avahi_server_errno(s);
1002 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
1005 if (g->state == state)
1008 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
1010 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
1012 /* If the entry group was established for a time longer then
1013 * 5s, reset the establishment trial counter */
1015 if (avahi_age(&g->established_at) > 5000000)
1016 g->n_register_try = 0;
1017 } else if (g->state == AVAHI_ENTRY_GROUP_REGISTERING) {
1018 if (g->register_time_event) {
1019 avahi_time_event_free(g->register_time_event);
1020 g->register_time_event = NULL;
1024 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
1026 /* If the entry group is now established, remember the time
1029 gettimeofday(&g->established_at, NULL);
1034 g->callback(g->server, g, state, g->userdata);
1037 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
1038 AvahiSEntryGroup *g;
1042 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
1043 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1048 g->callback = callback;
1049 g->userdata = userdata;
1051 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1053 g->n_register_try = 0;
1054 g->register_time_event = NULL;
1055 g->register_time.tv_sec = 0;
1056 g->register_time.tv_usec = 0;
1057 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1059 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
1063 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
1069 for (e = g->entries; e; e = e->by_group_next) {
1071 avahi_goodbye_entry(g->server, e, 1, 1);
1076 if (g->register_time_event) {
1077 avahi_time_event_free(g->register_time_event);
1078 g->register_time_event = NULL;
1083 g->server->need_group_cleanup = 1;
1084 g->server->need_entry_cleanup = 1;
1087 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1090 gettimeofday(&g->register_time, NULL);
1092 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1097 avahi_announce_group(g->server, g);
1098 avahi_s_entry_group_check_probed(g, 0);
1101 static void entry_group_register_time_event_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* userdata) {
1102 AvahiSEntryGroup *g = userdata;
1105 avahi_time_event_free(g->register_time_event);
1106 g->register_time_event = NULL;
1108 /* Holdoff time passed, so let's start probing */
1109 entry_group_commit_real(g);
1112 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1118 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1119 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1121 if (avahi_s_entry_group_is_empty(g))
1122 return avahi_server_set_errno(g->server, AVAHI_ERR_IS_EMPTY);
1124 g->n_register_try++;
1126 avahi_timeval_add(&g->register_time,
1127 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1128 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1129 AVAHI_RR_HOLDOFF_MSEC));
1131 gettimeofday(&now, NULL);
1133 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1135 /* Holdoff time passed, so let's start probing */
1136 entry_group_commit_real(g);
1139 /* Holdoff time has not yet passed, so let's wait */
1140 assert(!g->register_time_event);
1141 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1143 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1149 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1153 for (e = g->entries; e; e = e->by_group_next) {
1155 avahi_goodbye_entry(g->server, e, 1, 1);
1159 g->server->need_entry_cleanup = 1;
1163 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1166 int avahi_entry_is_commited(AvahiEntry *e) {
1171 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1172 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1175 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1182 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1185 g->userdata = userdata;
1188 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1194 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1198 /* Look for an entry that is not dead */
1199 for (e = g->entries; e; e = e->by_group_next)