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, !g || (g->state != AVAHI_ENTRY_GROUP_ESTABLISHED && g->state != AVAHI_ENTRY_GROUP_REGISTERING), AVAHI_ERR_BAD_STATE);
221 if (flags & AVAHI_PUBLISH_UPDATE) {
222 AvahiRecord *old_record;
225 /* Update and existing record */
227 /* Find the first matching entry */
228 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
229 if (!e->dead && e->group == g && e->interface == interface && e->protocol == protocol)
235 /* Hmm, nothing found? */
237 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
241 /* Update the entry */
242 old_record = e->record;
243 e->record = avahi_record_ref(r);
246 /* Announce our changes when needed */
247 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
249 /* Remove the old entry from all caches, if needed */
250 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
251 avahi_goodbye_entry(s, e, 1, 0);
253 /* Reannounce our updated entry */
254 avahi_reannounce_entry(s, e);
257 /* If we were the first entry in the list, we need to update the key */
259 avahi_hashmap_replace(s->entries_by_key, e->record->key, e);
261 avahi_record_unref(old_record);
266 /* Add a new record */
268 if (check_record_conflict(s, interface, protocol, r, flags) < 0) {
269 avahi_server_set_errno(s, AVAHI_ERR_COLLISION);
273 if (!(e = avahi_new(AvahiEntry, 1))) {
274 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
279 e->record = avahi_record_ref(r);
281 e->interface = interface;
282 e->protocol = protocol;
286 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, e->announcers);
288 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
290 /* Insert into hash table indexed by name */
291 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
292 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
293 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
295 /* Insert into group list */
297 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
299 avahi_announce_entry(s, e);
305 int avahi_server_add(
308 AvahiIfIndex interface,
309 AvahiProtocol protocol,
310 AvahiPublishFlags flags,
313 if (!server_add_internal(s, g, interface, protocol, flags, r))
314 return avahi_server_errno(s);
319 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
320 AvahiEntry **e = (AvahiEntry**) state;
325 *e = g ? g->entries : s->entries;
327 while (*e && (*e)->dead)
328 *e = g ? (*e)->by_group_next : (*e)->entries_next;
333 return avahi_record_ref((*e)->record);
336 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
342 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
344 for (e = s->entries; e; e = e->entries_next) {
351 if (!(t = avahi_record_to_string(e->record)))
352 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
354 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
357 callback(ln, userdata);
360 avahi_dump_caches(s->monitor, callback, userdata);
362 if (s->wide_area_lookup_engine)
363 avahi_wide_area_cache_dump(s->wide_area_lookup_engine, callback, userdata);
367 static AvahiEntry *server_add_ptr_internal(
370 AvahiIfIndex interface,
371 AvahiProtocol protocol,
372 AvahiPublishFlags flags,
383 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
384 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(dest), AVAHI_ERR_INVALID_HOST_NAME);
387 name = s->host_name_fqdn;
389 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl))) {
390 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
394 r->data.ptr.name = avahi_normalize_name_strdup(dest);
395 e = server_add_internal(s, g, interface, protocol, flags, r);
396 avahi_record_unref(r);
400 int avahi_server_add_ptr(
403 AvahiIfIndex interface,
404 AvahiProtocol protocol,
405 AvahiPublishFlags flags,
414 if (!(e = server_add_ptr_internal(s, g, interface, protocol, flags, ttl, name, dest)))
415 return avahi_server_errno(s);
420 int avahi_server_add_address(
423 AvahiIfIndex interface,
424 AvahiProtocol protocol,
425 AvahiPublishFlags flags,
429 char n[AVAHI_DOMAIN_NAME_MAX];
431 AvahiEntry *entry = NULL, *reverse = NULL;
437 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
438 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(a->proto), AVAHI_ERR_INVALID_PROTOCOL);
439 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags,
440 AVAHI_PUBLISH_NO_REVERSE|
441 AVAHI_PUBLISH_NO_ANNOUNCE|
442 AVAHI_PUBLISH_NO_PROBE|
443 AVAHI_PUBLISH_UPDATE|
444 AVAHI_PUBLISH_USE_WIDE_AREA|
445 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
446 AVAHI_CHECK_VALIDITY(s, !name || avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
448 /* Prepare the host naem */
451 name = s->host_name_fqdn;
453 AVAHI_ASSERT_TRUE(avahi_normalize_name(name, n, sizeof(n)));
457 transport_flags_from_domain(s, &flags, name);
458 AVAHI_CHECK_VALIDITY(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
460 /* Create the A/AAAA record */
462 if (a->proto == AVAHI_PROTO_INET) {
464 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
465 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
469 r->data.a.address = a->data.ipv4;
472 assert(a->proto == AVAHI_PROTO_INET6);
474 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
475 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
479 r->data.aaaa.address = a->data.ipv6;
482 entry = server_add_internal(s, g, interface, protocol, (flags & ~ AVAHI_PUBLISH_NO_REVERSE) | AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
483 avahi_record_unref(r);
486 ret = avahi_server_errno(s);
490 /* Create the reverse lookup entry */
492 if (!(flags & AVAHI_PUBLISH_NO_REVERSE)) {
493 char reverse_n[AVAHI_DOMAIN_NAME_MAX];
494 avahi_reverse_lookup_name(a, reverse_n, sizeof(reverse_n));
496 if (!(reverse = server_add_ptr_internal(s, g, interface, protocol, flags | AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse_n, name))) {
497 ret = avahi_server_errno(s);
504 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
506 avahi_entry_free(s, entry);
508 avahi_entry_free(s, reverse);
514 static AvahiEntry *server_add_txt_strlst_nocopy(
517 AvahiIfIndex interface,
518 AvahiProtocol protocol,
519 AvahiPublishFlags flags,
522 AvahiStringList *strlst) {
529 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl))) {
530 avahi_string_list_free(strlst);
531 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
535 r->data.txt.string_list = strlst;
536 e = server_add_internal(s, g, interface, protocol, flags, r);
537 avahi_record_unref(r);
542 static AvahiStringList *add_magic_cookie(
544 AvahiStringList *strlst) {
548 if (!s->config.add_service_cookie)
551 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
552 /* This string list already contains a magic cookie */
555 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
558 static int server_add_service_strlst_nocopy(
561 AvahiIfIndex interface,
562 AvahiProtocol protocol,
563 AvahiPublishFlags flags,
569 AvahiStringList *strlst) {
571 char ptr_name[AVAHI_DOMAIN_NAME_MAX], svc_name[AVAHI_DOMAIN_NAME_MAX], enum_ptr[AVAHI_DOMAIN_NAME_MAX], *h = NULL;
572 AvahiRecord *r = NULL;
574 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
580 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
581 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
582 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags,
583 AVAHI_PUBLISH_NO_COOKIE|
584 AVAHI_PUBLISH_UPDATE|
585 AVAHI_PUBLISH_USE_WIDE_AREA|
586 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
587 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
588 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
589 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
590 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !host || avahi_is_valid_domain_name(host), AVAHI_ERR_INVALID_HOST_NAME);
593 domain = s->domain_name;
596 host = s->host_name_fqdn;
598 transport_flags_from_domain(s, &flags, domain);
599 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
601 if (!(h = avahi_normalize_name_strdup(host))) {
602 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
606 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
607 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, type, domain)) < 0 ||
608 (ret = avahi_service_name_join(enum_ptr, sizeof(enum_ptr), NULL, "_services._dns-sd._udp", domain)) < 0) {
609 avahi_server_set_errno(s, ret);
613 /* Add service enumeration PTR record */
615 if (!(ptr_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name))) {
616 ret = avahi_server_errno(s);
622 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
623 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
627 r->data.srv.priority = 0;
628 r->data.srv.weight = 0;
629 r->data.srv.port = port;
630 r->data.srv.name = h;
632 srv_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, r);
633 avahi_record_unref(r);
636 ret = avahi_server_errno(s);
642 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
643 strlst = add_magic_cookie(s, strlst);
645 txt_entry = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
649 ret = avahi_server_errno(s);
653 /* Add service type enumeration record */
655 if (!(enum_entry = server_add_ptr_internal(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name))) {
656 ret = avahi_server_errno(s);
661 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
663 avahi_entry_free(s, srv_entry);
665 avahi_entry_free(s, txt_entry);
667 avahi_entry_free(s, ptr_entry);
669 avahi_entry_free(s, enum_entry);
672 avahi_string_list_free(strlst);
678 int avahi_server_add_service_strlst(
681 AvahiIfIndex interface,
682 AvahiProtocol protocol,
683 AvahiPublishFlags flags,
689 AvahiStringList *strlst) {
695 return server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_copy(strlst));
698 int avahi_server_add_service(
701 AvahiIfIndex interface,
702 AvahiProtocol protocol,
703 AvahiPublishFlags flags,
715 ret = server_add_service_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, host, port, avahi_string_list_new_va(va));
721 static int server_update_service_txt_strlst_nocopy(
724 AvahiIfIndex interface,
725 AvahiProtocol protocol,
726 AvahiPublishFlags flags,
730 AvahiStringList *strlst) {
732 char svc_name[AVAHI_DOMAIN_NAME_MAX];
740 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
741 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
742 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_FLAGS_VALID(flags,
743 AVAHI_PUBLISH_NO_COOKIE|
744 AVAHI_PUBLISH_USE_WIDE_AREA|
745 AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
746 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
747 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
748 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
751 domain = s->domain_name;
753 transport_flags_from_domain(s, &flags, domain);
754 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
756 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0) {
757 avahi_server_set_errno(s, ret);
762 if (!(flags & AVAHI_PUBLISH_NO_COOKIE))
763 strlst = add_magic_cookie(s, strlst);
765 e = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_UPDATE, AVAHI_DEFAULT_TTL, svc_name, strlst);
769 ret = avahi_server_errno(s);
773 avahi_string_list_free(strlst);
778 int avahi_server_update_service_txt_strlst(
781 AvahiIfIndex interface,
782 AvahiProtocol protocol,
783 AvahiPublishFlags flags,
787 AvahiStringList *strlst) {
789 return server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_copy(strlst));
792 /** Update the TXT record for a service with the NULL termonate list of strings */
793 int avahi_server_update_service_txt(
796 AvahiIfIndex interface,
797 AvahiProtocol protocol,
798 AvahiPublishFlags flags,
807 va_start(va, domain);
808 ret = server_update_service_txt_strlst_nocopy(s, g, interface, protocol, flags, name, type, domain, avahi_string_list_new_va(va));
814 int avahi_server_add_service_subtype(
817 AvahiIfIndex interface,
818 AvahiProtocol protocol,
819 AvahiPublishFlags flags,
823 const char *subtype) {
826 char svc_name[AVAHI_DOMAIN_NAME_MAX], ptr_name[AVAHI_DOMAIN_NAME_MAX];
832 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
833 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
834 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);
835 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
836 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
837 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
838 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, avahi_is_valid_service_subtype(subtype), AVAHI_ERR_INVALID_SERVICE_SUBTYPE);
841 domain = s->domain_name;
843 transport_flags_from_domain(s, &flags, domain);
844 AVAHI_CHECK_VALIDITY_SET_RET_GOTO_FAIL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
846 if ((ret = avahi_service_name_join(svc_name, sizeof(svc_name), name, type, domain)) < 0 ||
847 (ret = avahi_service_name_join(ptr_name, sizeof(ptr_name), NULL, subtype, domain)) < 0) {
848 avahi_server_set_errno(s, ret);
852 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, 0, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
860 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
861 static const char hex[] = "0123456789abcdef";
863 const uint8_t *k = p;
865 while (sl > 1 && pl > 0) {
866 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
882 static AvahiEntry *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[AVAHI_DOMAIN_NAME_MAX], normalized_d[AVAHI_DOMAIN_NAME_MAX], *n;
901 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_USE_WIDE_AREA|AVAHI_PUBLISH_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
902 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
903 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, port != 0, AVAHI_ERR_INVALID_PORT);
904 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, avahi_is_valid_domain_name(name), AVAHI_ERR_INVALID_HOST_NAME);
905 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
908 domain = s->domain_name;
910 transport_flags_from_domain(s, &flags, domain);
911 AVAHI_CHECK_VALIDITY_RETURN_NULL(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
913 if (!(n = avahi_normalize_name_strdup(name))) {
914 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
918 AVAHI_ASSERT_TRUE(avahi_normalize_name(domain, normalized_d, sizeof(normalized_d)));
920 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", normalized_d);
922 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
923 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
928 r->data.srv.priority = 0;
929 r->data.srv.weight = 0;
930 r->data.srv.port = port;
931 r->data.srv.name = n;
932 e = server_add_internal(s, g, interface, protocol, 0, r);
933 avahi_record_unref(r);
938 int avahi_server_add_dns_server_address(
941 AvahiIfIndex interface,
942 AvahiProtocol protocol,
943 AvahiPublishFlags flags,
945 AvahiDNSServerType type,
946 const AvahiAddress *address,
947 uint16_t port /** should be 53 */) {
951 AvahiEntry *a_entry, *s_entry;
956 AVAHI_CHECK_VALIDITY(s, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
957 AVAHI_CHECK_VALIDITY(s, AVAHI_PROTO_VALID(protocol) && AVAHI_PROTO_VALID(address->proto), AVAHI_ERR_INVALID_PROTOCOL);
958 AVAHI_CHECK_VALIDITY(s, AVAHI_FLAGS_VALID(flags, AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_USE_WIDE_AREA), AVAHI_ERR_INVALID_FLAGS);
959 AVAHI_CHECK_VALIDITY(s, type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE, AVAHI_ERR_INVALID_FLAGS);
960 AVAHI_CHECK_VALIDITY(s, port != 0, AVAHI_ERR_INVALID_PORT);
961 AVAHI_CHECK_VALIDITY(s, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
964 domain = s->domain_name;
966 transport_flags_from_domain(s, &flags, domain);
967 AVAHI_CHECK_VALIDITY(s, flags & AVAHI_PUBLISH_USE_MULTICAST, AVAHI_ERR_NOT_SUPPORTED);
969 if (address->proto == AVAHI_PROTO_INET) {
970 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv4Address));
971 snprintf(n, sizeof(n), "ip-%s.%s", h, domain);
972 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
973 r->data.a.address = address->data.ipv4;
975 hexstring(h, sizeof(h), &address->data, sizeof(AvahiIPv6Address));
976 snprintf(n, sizeof(n), "ip6-%s.%s", h, domain);
977 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
978 r->data.aaaa.address = address->data.ipv6;
982 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
984 a_entry = server_add_internal(s, g, interface, protocol, AVAHI_PUBLISH_UNIQUE | AVAHI_PUBLISH_ALLOW_MULTIPLE, r);
985 avahi_record_unref(r);
988 return avahi_server_errno(s);
990 if (!(s_entry = server_add_dns_server_name(s, g, interface, protocol, flags, domain, type, n, port))) {
991 if (!(flags & AVAHI_PUBLISH_UPDATE))
992 avahi_entry_free(s, a_entry);
993 return avahi_server_errno(s);
999 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
1002 if (g->state == state)
1005 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
1007 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
1009 /* If the entry group was established for a time longer then
1010 * 5s, reset the establishment trial counter */
1012 if (avahi_age(&g->established_at) > 5000000)
1013 g->n_register_try = 0;
1016 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
1018 /* If the entry group is now established, remember the time
1021 gettimeofday(&g->established_at, NULL);
1026 g->callback(g->server, g, state, g->userdata);
1029 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
1030 AvahiSEntryGroup *g;
1034 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
1035 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1040 g->callback = callback;
1041 g->userdata = userdata;
1043 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1045 g->n_register_try = 0;
1046 g->register_time_event = NULL;
1047 g->register_time.tv_sec = 0;
1048 g->register_time.tv_usec = 0;
1049 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1051 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
1055 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
1061 for (e = g->entries; e; e = e->by_group_next) {
1063 avahi_goodbye_entry(g->server, e, 1, 1);
1068 if (g->register_time_event) {
1069 avahi_time_event_free(g->register_time_event);
1070 g->register_time_event = NULL;
1075 g->server->need_group_cleanup = 1;
1076 g->server->need_entry_cleanup = 1;
1079 static void entry_group_commit_real(AvahiSEntryGroup *g) {
1082 gettimeofday(&g->register_time, NULL);
1084 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1087 avahi_announce_group(g->server, g);
1088 avahi_s_entry_group_check_probed(g, 0);
1092 static void entry_group_register_time_event_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* userdata) {
1093 AvahiSEntryGroup *g = userdata;
1096 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
1098 avahi_time_event_free(g->register_time_event);
1099 g->register_time_event = NULL;
1101 /* Holdoff time passed, so let's start probing */
1102 entry_group_commit_real(g);
1105 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
1111 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
1112 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
1114 g->n_register_try++;
1116 avahi_timeval_add(&g->register_time,
1117 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
1118 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
1119 AVAHI_RR_HOLDOFF_MSEC));
1121 gettimeofday(&now, NULL);
1123 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
1124 /* Holdoff time passed, so let's start probing */
1125 /* avahi_log_debug("Holdoff passed, directly going on."); */
1127 entry_group_commit_real(g);
1129 /* avahi_log_debug("Holdoff not passed, sleeping."); */
1131 /* Holdoff time has not yet passed, so let's wait */
1132 assert(!g->register_time_event);
1133 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
1135 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1141 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
1145 for (e = g->entries; e; e = e->by_group_next) {
1147 avahi_goodbye_entry(g->server, e, 1, 1);
1151 g->server->need_entry_cleanup = 1;
1153 if (g->register_time_event) {
1154 avahi_time_event_free(g->register_time_event);
1155 g->register_time_event = NULL;
1160 gettimeofday(&g->register_time, NULL);
1162 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
1165 int avahi_entry_is_commited(AvahiEntry *e) {
1170 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1171 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1174 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
1181 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
1184 g->userdata = userdata;
1187 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
1193 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
1197 /* Look for an entry that is not dead */
1198 for (e = g->entries; e; e = e->by_group_next)