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>
48 #define AVAHI_RR_HOLDOFF_MSEC 1000
49 #define AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT 60000
50 #define AVAHI_RR_RATE_LIMIT_COUNT 15
52 static void free_entry(AvahiServer*s, AvahiEntry *e) {
58 avahi_goodbye_entry(s, e, 1);
60 /* Remove from linked list */
61 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
63 /* Remove from hash table indexed by name */
64 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
65 AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
67 avahi_hashmap_replace(s->entries_by_key, t->record->key, t);
69 avahi_hashmap_remove(s->entries_by_key, e->record->key);
71 /* Remove from associated group */
73 AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
75 avahi_record_unref(e->record);
79 static void free_group(AvahiServer *s, AvahiSEntryGroup *g) {
84 free_entry(s, g->entries);
86 if (g->register_time_event)
87 avahi_time_event_free(g->register_time_event);
89 AVAHI_LLIST_REMOVE(AvahiSEntryGroup, groups, s->groups, g);
93 static void cleanup_dead(AvahiServer *s) {
96 if (s->need_group_cleanup) {
97 AvahiSEntryGroup *g, *next;
99 for (g = s->groups; g; g = next) {
100 next = g->groups_next;
106 s->need_group_cleanup = 0;
109 if (s->need_entry_cleanup) {
110 AvahiEntry *e, *next;
112 for (e = s->entries; e; e = next) {
113 next = e->entries_next;
119 s->need_entry_cleanup = 0;
122 if (s->need_browser_cleanup)
123 avahi_browser_cleanup(s);
126 static void enum_aux_records(AvahiServer *s, AvahiInterface *i, const char *name, uint16_t type, void (*callback)(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata), void* userdata) {
135 assert(type != AVAHI_DNS_TYPE_ANY);
137 if (!(k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type)))
140 for (e = avahi_hashmap_lookup(s->entries_by_key, k); e; e = e->by_key_next)
141 if (!e->dead && avahi_entry_is_registered(s, e, i))
142 callback(s, e->record, e->flags & AVAHI_ENTRY_UNIQUE, userdata);
147 void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, void (*callback)(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata), void* userdata) {
153 if (r->key->clazz == AVAHI_DNS_CLASS_IN) {
154 if (r->key->type == AVAHI_DNS_TYPE_PTR) {
155 enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_SRV, callback, userdata);
156 enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_TXT, callback, userdata);
157 } else if (r->key->type == AVAHI_DNS_TYPE_SRV) {
158 enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_A, callback, userdata);
159 enum_aux_records(s, i, r->data.srv.name, AVAHI_DNS_TYPE_AAAA, callback, userdata);
164 void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, int unicast_response, int auxiliary) {
169 avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_ENTRY_UNIQUE, unicast_response, auxiliary);
172 void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, int unicast_response) {
180 /* avahi_log_debug("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */
181 /* avahi_free(txt); */
183 if (avahi_key_is_pattern(k)) {
185 /* Handle ANY query */
187 for (e = s->entries; e; e = e->entries_next)
188 if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_is_registered(s, e, i))
189 avahi_server_prepare_response(s, i, e, unicast_response, 0);
193 /* Handle all other queries */
195 for (e = avahi_hashmap_lookup(s->entries_by_key, k); e; e = e->by_key_next)
196 if (!e->dead && avahi_entry_is_registered(s, e, i))
197 avahi_server_prepare_response(s, i, e, unicast_response, 0);
201 static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
208 for (k = e->group->entries; k; k = k->by_group_next) {
210 avahi_goodbye_entry(s, k, 0);
215 e->group->n_probing = 0;
217 avahi_s_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
219 avahi_goodbye_entry(s, e, 0);
223 s->need_entry_cleanup = 1;
226 static void withdraw_rrset(AvahiServer *s, AvahiKey *key) {
232 for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next)
234 withdraw_entry(s, e);
237 static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
240 int ours = 0, won = 0, lost = 0;
246 t = avahi_record_to_string(record);
248 /* avahi_log_debug("incoming_probe()"); */
250 for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) {
257 if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) {
262 if (avahi_entry_is_probing(s, e, i)) {
274 avahi_log_debug("Recieved conflicting probe [%s]. Local host won.", t);
276 avahi_log_debug("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
277 withdraw_rrset(s, record->key);
279 /* avahi_log_debug("Not conflicting probe"); */
285 static int handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, int unique, const AvahiAddress *a) {
286 int valid = 1, ours = 0, conflict = 0, withdraw_immediately = 0;
287 AvahiEntry *e, *n, *conflicting_entry = NULL;
294 /* avahi_log_debug("CHECKING FOR CONFLICT: [%s]", t); */
296 for (e = avahi_hashmap_lookup(s->entries_by_key, record->key); e; e = n) {
302 /* Check if the incoming is a goodbye record */
303 if (avahi_record_is_goodbye(record)) {
305 if (avahi_record_equal_no_ttl(e->record, record)) {
309 t = avahi_record_to_string(record);
310 avahi_log_debug("Recieved goodbye record for one of our records [%s]. Refreshing.", t);
311 avahi_server_prepare_matching_responses(s, i, e->record->key, 0);
318 /* If the goodybe packet doesn't match one of our own RRs, we simply ignore it. */
322 if (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique)
325 /* Either our entry or the other is intended to be unique, so let's check */
327 if (avahi_record_equal_no_ttl(e->record, record)) {
328 ours = 1; /* We have an identical record, so this is no conflict */
330 /* Check wheter there is a TTL conflict */
331 if (record->ttl <= e->record->ttl/2 &&
332 avahi_entry_is_registered(s, e, i)) {
335 t = avahi_record_to_string(record);
337 avahi_log_debug("Recieved record with bad TTL [%s]. Refreshing.", t);
338 avahi_server_prepare_matching_responses(s, i, e->record->key, 0);
344 /* There's no need to check the other entries of this RRset */
349 if (avahi_entry_is_registered(s, e, i)) {
351 /* A conflict => we have to return to probe mode */
353 conflicting_entry = e;
355 } else if (avahi_entry_is_probing(s, e, i)) {
357 /* We are currently registering a matching record, but
358 * someone else already claimed it, so let's
361 withdraw_immediately = 1;
366 /* avahi_log_debug("ours=%i conflict=%i", ours, conflict); */
368 if (!ours && conflict) {
373 t = avahi_record_to_string(record);
375 if (withdraw_immediately) {
376 avahi_log_debug("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
377 withdraw_rrset(s, record->key);
379 assert(conflicting_entry);
380 avahi_log_debug("Recieved conflicting record [%s]. Resetting our record.", t);
381 avahi_entry_return_to_initial_state(s, conflicting_entry, i);
383 /* Local unique records are returned to probing
384 * state. Local shared records are reannounced. */
393 static void append_aux_callback(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) {
394 int *unicast_response = userdata;
398 assert(unicast_response);
400 avahi_record_list_push(s->record_list, r, flush_cache, *unicast_response, 1);
403 static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int unicast_response) {
407 avahi_server_enumerate_aux_records(s, i, r, append_aux_callback, &unicast_response);
410 void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port, int legacy_unicast, int immediately) {
414 assert(!legacy_unicast || (a && port > 0 && p));
416 if (legacy_unicast) {
417 AvahiDnsPacket *reply;
420 if (!(reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , 1, 1)))
423 while ((r = avahi_record_list_next(s->record_list, NULL, NULL, NULL))) {
425 append_aux_records_to_list(s, i, r, 0);
427 if (avahi_dns_packet_append_record(reply, r, 0, 10))
428 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
430 char *t = avahi_record_to_string(r);
431 avahi_log_warn("Record [%s] not fitting in legacy unicast packet, dropping.", t);
435 avahi_record_unref(r);
438 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0)
439 avahi_interface_send_packet_unicast(i, reply, a, port);
441 avahi_dns_packet_free(reply);
444 int unicast_response, flush_cache, auxiliary;
445 AvahiDnsPacket *reply = NULL;
448 /* In case the query packet was truncated never respond
449 immediately, because known answer suppression records might be
450 contained in later packets */
451 int tc = p && !!(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC);
453 while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) {
455 if (!avahi_interface_post_response(i, r, flush_cache, a, immediately || (flush_cache && !tc && !auxiliary)) && unicast_response) {
457 append_aux_records_to_list(s, i, r, unicast_response);
459 /* Due to some reasons the record has not been scheduled.
460 * The client requested an unicast response in that
461 * case. Therefore we prepare such a response */
468 if (!(reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, 0, 0)))
472 if (avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
474 /* Appending this record succeeded, so incremeant
475 * the specific header field, and return to the caller */
477 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
482 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
485 /* The record is too large for one packet, so create a larger packet */
487 avahi_dns_packet_free(reply);
488 size = avahi_record_get_estimate_size(r) + AVAHI_DNS_PACKET_HEADER_SIZE;
489 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
490 size = AVAHI_DNS_PACKET_MAX_SIZE;
492 if (!(reply = avahi_dns_packet_new_reply(p, size, 0, 1)))
495 if (!avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
497 avahi_dns_packet_free(reply);
498 t = avahi_record_to_string(r);
499 avahi_log_warn("Record [%s] too large, doesn't fit in any packet!", t);
503 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
506 /* Appending the record didn't succeeed, so let's send this packet, and create a new one */
507 avahi_interface_send_packet_unicast(i, reply, a, port);
508 avahi_dns_packet_free(reply);
513 avahi_record_unref(r);
517 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0)
518 avahi_interface_send_packet_unicast(i, reply, a, port);
519 avahi_dns_packet_free(reply);
523 avahi_record_list_flush(s->record_list);
527 static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, int flush_cache) {
534 if (!s->config.enable_reflector)
537 for (j = s->monitor->interfaces; j; j = j->interface_next)
538 if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol))
539 avahi_interface_post_response(j, r, flush_cache, NULL, 1);
542 static void* reflect_cache_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
543 AvahiServer *s = userdata;
550 avahi_record_list_push(s->record_list, e->record, e->cache_flush, 0, 0);
554 static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) {
561 if (!s->config.enable_reflector)
564 for (j = s->monitor->interfaces; j; j = j->interface_next)
565 if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) {
566 /* Post the query to other networks */
567 avahi_interface_post_query(j, k, 1);
569 /* Reply from caches of other network. This is needed to
570 * "work around" known answer suppression. */
572 avahi_cache_walk(j->cache, k, reflect_cache_walk_callback, s);
576 static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) {
583 if (!s->config.enable_reflector)
586 for (j = s->monitor->interfaces; j; j = j->interface_next)
587 if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol))
588 avahi_interface_post_probe(j, r, 1);
591 static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port, int legacy_unicast, int from_local_iface) {
600 /* avahi_log_debug("query"); */
602 assert(avahi_record_list_is_empty(s->record_list));
604 is_probe = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) > 0;
606 /* Handle the questions */
607 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
609 int unicast_response = 0;
611 if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
612 avahi_log_warn("Packet too short (1)");
616 if (!legacy_unicast && !from_local_iface)
617 reflect_query(s, i, key);
619 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 &&
620 !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC))
621 /* Allow our own queries to be suppressed by incoming
622 * queries only when they do not include known answers */
623 avahi_query_scheduler_incoming(i->query_scheduler, key);
625 avahi_server_prepare_matching_responses(s, i, key, unicast_response);
626 avahi_key_unref(key);
629 if (!legacy_unicast) {
631 /* Known Answer Suppression */
632 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
636 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
637 avahi_log_warn("Packet too short (2)");
641 if (handle_conflict(s, i, record, unique, a)) {
642 avahi_response_scheduler_suppress(i->response_scheduler, record, a);
643 avahi_record_list_drop(s->record_list, record);
646 avahi_record_unref(record);
650 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
654 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
655 avahi_log_warn("Packet too short (3)");
659 if (!avahi_key_is_pattern(record->key)) {
660 if (!from_local_iface)
661 reflect_probe(s, i, record);
662 incoming_probe(s, record, i);
665 avahi_record_unref(record);
669 if (!avahi_record_list_is_empty(s->record_list))
670 avahi_server_generate_response(s, i, p, a, port, legacy_unicast, is_probe);
675 avahi_record_list_flush(s->record_list);
678 static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, int from_local_iface) {
686 /* avahi_log_debug("response"); */
688 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
689 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
694 if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
695 avahi_log_warn("Packet too short (4)");
699 if (!avahi_key_is_pattern(record->key)) {
701 /* avahi_log_debug("Handling response: %s", txt = avahi_record_to_string(record)); */
702 /* avahi_free(txt); */
704 if (handle_conflict(s, i, record, cache_flush, a)) {
705 if (!from_local_iface)
706 reflect_response(s, i, record, cache_flush);
707 avahi_cache_update(i->cache, record, cache_flush, a);
708 avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush);
712 avahi_record_unref(record);
715 /* If the incoming response contained a conflicting record, some
716 records have been scheduling for sending. We need to flush them
718 if (!avahi_record_list_is_empty(s->record_list))
719 avahi_server_generate_response(s, i, NULL, NULL, 0, 0, 1);
722 static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) {
723 unsigned n, idx = (unsigned) -1;
724 AvahiLegacyUnicastReflectSlot *slot;
728 if (!s->legacy_unicast_reflect_slots)
729 s->legacy_unicast_reflect_slots = avahi_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS);
731 for (n = 0; n < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; n++, s->legacy_unicast_reflect_id++) {
732 idx = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
734 if (!s->legacy_unicast_reflect_slots[idx])
738 if (idx == (unsigned) -1 || s->legacy_unicast_reflect_slots[idx])
741 if (!(slot = avahi_new(AvahiLegacyUnicastReflectSlot, 1)))
742 return NULL; /* OOM */
744 s->legacy_unicast_reflect_slots[idx] = slot;
745 slot->id = s->legacy_unicast_reflect_id++;
751 static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) {
757 idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
759 assert(s->legacy_unicast_reflect_slots[idx] == slot);
761 avahi_time_event_free(slot->time_event);
764 s->legacy_unicast_reflect_slots[idx] = NULL;
767 static void free_slots(AvahiServer *s) {
771 if (!s->legacy_unicast_reflect_slots)
774 for (idx = 0; idx < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; idx ++)
775 if (s->legacy_unicast_reflect_slots[idx])
776 deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]);
778 avahi_free(s->legacy_unicast_reflect_slots);
779 s->legacy_unicast_reflect_slots = NULL;
782 static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, uint16_t id) {
787 if (!s->legacy_unicast_reflect_slots)
790 idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
792 if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id)
795 return s->legacy_unicast_reflect_slots[idx];
798 static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) {
799 AvahiLegacyUnicastReflectSlot *slot = userdata;
803 assert(slot->time_event == e);
805 deallocate_slot(slot->server, slot);
808 static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, uint16_t port) {
809 AvahiLegacyUnicastReflectSlot *slot;
817 assert(i->protocol == a->proto);
819 if (!s->config.enable_reflector)
822 /* avahi_log_debug("legacy unicast reflector"); */
824 /* Reflecting legacy unicast queries is a little more complicated
825 than reflecting normal queries, since we must route the
826 responses back to the right client. Therefore we must store
827 some information for finding the right client contact data for
828 response packets. In contrast to normal queries legacy
829 unicast query and response packets are reflected untouched and
830 are not reassembled into larger packets */
832 if (!(slot = allocate_slot(s))) {
833 /* No slot available, we drop this legacy unicast query */
834 avahi_log_warn("No slot available for legacy unicast reflection, dropping query packet.");
838 slot->original_id = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID);
841 slot->interface = i->hardware->index;
843 avahi_elapse_time(&slot->elapse_time, 2000, 0);
844 slot->time_event = avahi_time_event_new(s->time_event_queue, &slot->elapse_time, legacy_unicast_reflect_slot_timeout, slot);
846 /* Patch the packet with our new locally generatedt id */
847 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
849 for (j = s->monitor->interfaces; j; j = j->interface_next)
850 if (avahi_interface_relevant(j) &&
852 (s->config.reflect_ipv || j->protocol == i->protocol)) {
854 if (j->protocol == AVAHI_PROTO_INET && s->fd_legacy_unicast_ipv4 >= 0) {
855 avahi_send_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, j->hardware->index, p, NULL, 0);
856 } else if (j->protocol == AVAHI_PROTO_INET6 && s->fd_legacy_unicast_ipv6 >= 0)
857 avahi_send_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, j->hardware->index, p, NULL, 0);
861 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);
864 static int originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) {
869 if (!s->config.enable_reflector)
872 avahi_address_from_sockaddr(sa, &a);
874 if (!avahi_address_is_local(s->monitor, &a))
877 if (sa->sa_family == AF_INET && s->fd_legacy_unicast_ipv4 >= 0) {
878 struct sockaddr_in lsa;
879 socklen_t l = sizeof(lsa);
881 if (getsockname(s->fd_legacy_unicast_ipv4, &lsa, &l) != 0)
882 avahi_log_warn("getsockname(): %s", strerror(errno));
884 return lsa.sin_port == ((const struct sockaddr_in*) sa)->sin_port;
888 if (sa->sa_family == AF_INET6 && s->fd_legacy_unicast_ipv6 >= 0) {
889 struct sockaddr_in6 lsa;
890 socklen_t l = sizeof(lsa);
892 if (getsockname(s->fd_legacy_unicast_ipv6, &lsa, &l) != 0)
893 avahi_log_warn("getsockname(): %s", strerror(errno));
895 return lsa.sin6_port == ((const struct sockaddr_in6*) sa)->sin6_port;
901 static int is_mdns_mcast_address(const AvahiAddress *a) {
905 avahi_address_parse(a->proto == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->proto, &b);
906 return avahi_address_cmp(a, &b) == 0;
909 static int originates_from_local_iface(AvahiServer *s, AvahiIfIndex iface, const AvahiAddress *a, uint16_t port) {
911 assert(iface != AVAHI_IF_UNSPEC);
914 /* If it isn't the MDNS port it can't be generated by us */
915 if (port != AVAHI_MDNS_PORT)
918 return avahi_interface_has_address(s->monitor, iface, a);
921 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, int ttl) {
925 int from_local_iface = 0;
933 if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) ||
934 !avahi_interface_relevant(i)) {
935 avahi_log_warn("Recieved packet from invalid interface.");
939 /* avahi_log_debug("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
941 port = avahi_port_from_sockaddr(sa);
942 avahi_address_from_sockaddr(sa, &a);
944 if (avahi_address_is_ipv4_in_ipv6(&a))
945 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
948 if (originates_from_local_legacy_unicast_socket(s, sa))
949 /* This originates from our local reflector, so let's ignore it */
952 /* We don't want to reflect local traffic, so we check if this packet is generated locally. */
953 if (s->config.enable_reflector)
954 from_local_iface = originates_from_local_iface(s, iface, &a, port);
956 if (avahi_dns_packet_is_valid(p) < 0) {
957 avahi_log_warn("Recieved invalid packet.");
961 if (avahi_dns_packet_is_query(p)) {
962 int legacy_unicast = 0;
964 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
965 avahi_log_warn("Invalid query packet.");
969 if (port != AVAHI_MDNS_PORT) {
972 if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
973 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
974 avahi_log_warn("Invalid legacy unicast query packet.");
982 reflect_legacy_unicast_query_packet(s, p, i, &a, port);
984 handle_query_packet(s, p, i, &a, port, legacy_unicast, from_local_iface);
986 /* avahi_log_debug("Handled query"); */
988 if (port != AVAHI_MDNS_PORT) {
989 avahi_log_warn("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
993 if (ttl != 255 && s->config.check_response_ttl) {
994 avahi_log_warn("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
998 if (!is_mdns_mcast_address(dest) &&
999 !avahi_interface_address_on_link(i, &a)) {
1000 avahi_log_warn("Recivied non-local response on interface '%s.%i'.", i->hardware->name, i->protocol);
1004 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
1005 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
1006 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
1007 avahi_log_warn("Invalid response packet.");
1011 handle_response_packet(s, p, i, &a, from_local_iface);
1012 /* avahi_log_debug("Handled response"); */
1016 static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface, int ttl) {
1017 AvahiInterface *i, *j;
1020 AvahiLegacyUnicastReflectSlot *slot;
1027 if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) ||
1028 !avahi_interface_relevant(i)) {
1029 avahi_log_warn("Recieved packet from invalid interface.");
1033 /* avahi_log_debug("new legacy unicast packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
1035 port = avahi_port_from_sockaddr(sa);
1036 avahi_address_from_sockaddr(sa, &a);
1038 if (avahi_address_is_ipv4_in_ipv6(&a))
1039 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
1042 if (avahi_dns_packet_is_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
1043 avahi_log_warn("Recieved invalid packet.");
1047 if (!(slot = find_slot(s, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)))) {
1048 avahi_log_warn("Recieved legacy unicast response with unknown id");
1052 if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.proto)) ||
1053 !avahi_interface_relevant(j))
1056 /* Patch the original ID into this response */
1057 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);
1059 /* Forward the response to the correct client */
1060 avahi_interface_send_packet_unicast(j, p, &slot->address, slot->port);
1062 /* Undo changes to packet */
1063 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
1066 static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
1067 AvahiServer *s = userdata;
1072 struct sockaddr_in sa;
1073 struct sockaddr_in6 sa6;
1078 if (events & AVAHI_WATCH_IN) {
1080 if (fd == s->fd_ipv4) {
1081 dest.proto = AVAHI_PROTO_INET;
1082 if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
1083 dispatch_packet(s, p, (struct sockaddr*) &sa, &dest, iface, ttl);
1084 avahi_dns_packet_free(p);
1086 } else if (fd == s->fd_ipv6) {
1087 dest.proto = AVAHI_PROTO_INET6;
1089 if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
1090 dispatch_packet(s, p, (struct sockaddr*) &sa6, &dest, iface, ttl);
1091 avahi_dns_packet_free(p);
1093 } else if (fd == s->fd_legacy_unicast_ipv4) {
1094 dest.proto = AVAHI_PROTO_INET;
1096 if ((p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
1097 dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
1098 avahi_dns_packet_free(p);
1100 } else if (fd == s->fd_legacy_unicast_ipv6) {
1101 dest.proto = AVAHI_PROTO_INET6;
1103 if ((p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
1104 dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
1105 avahi_dns_packet_free(p);
1115 static void server_set_state(AvahiServer *s, AvahiServerState state) {
1118 if (s->state == state)
1124 s->callback(s, state, s->userdata);
1127 static void withdraw_host_rrs(AvahiServer *s) {
1130 if (s->hinfo_entry_group)
1131 avahi_s_entry_group_reset(s->hinfo_entry_group);
1133 if (s->browse_domain_entry_group)
1134 avahi_s_entry_group_reset(s->browse_domain_entry_group);
1136 avahi_update_host_rrs(s->monitor, 1);
1137 s->n_host_rr_pending = 0;
1140 void avahi_server_decrease_host_rr_pending(AvahiServer *s) {
1143 assert(s->n_host_rr_pending > 0);
1145 if (--s->n_host_rr_pending == 0)
1146 server_set_state(s, AVAHI_SERVER_RUNNING);
1149 void avahi_server_increase_host_rr_pending(AvahiServer *s) {
1152 s->n_host_rr_pending ++;
1155 void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
1159 if (state == AVAHI_ENTRY_GROUP_REGISTERING &&
1160 s->state == AVAHI_SERVER_REGISTERING)
1161 avahi_server_increase_host_rr_pending(s);
1163 else if (state == AVAHI_ENTRY_GROUP_COLLISION &&
1164 (s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) {
1165 withdraw_host_rrs(s);
1166 server_set_state(s, AVAHI_SERVER_COLLISION);
1168 } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED &&
1169 s->state == AVAHI_SERVER_REGISTERING)
1170 avahi_server_decrease_host_rr_pending(s);
1173 static void register_hinfo(AvahiServer *s) {
1174 struct utsname utsname;
1179 if (!s->config.publish_hinfo)
1182 if (s->hinfo_entry_group)
1183 assert(avahi_s_entry_group_is_empty(s->hinfo_entry_group));
1185 s->hinfo_entry_group = avahi_s_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
1187 if (!s->hinfo_entry_group) {
1188 avahi_log_warn("Failed to create HINFO entry group: %s", avahi_strerror(s->error));
1192 /* Fill in HINFO rr */
1193 if ((r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME))) {
1195 r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine));
1196 r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname));
1198 if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_UNIQUE, r) < 0) {
1199 avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error));
1203 avahi_record_unref(r);
1206 if (avahi_s_entry_group_commit(s->hinfo_entry_group) < 0)
1207 avahi_log_warn("Failed to commit HINFO entry group: %s", avahi_strerror(s->error));
1211 static void register_localhost(AvahiServer *s) {
1215 /* Add localhost entries */
1216 avahi_address_parse("127.0.0.1", AVAHI_PROTO_INET, &a);
1217 avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
1219 avahi_address_parse("::1", AVAHI_PROTO_INET6, &a);
1220 avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
1223 static void register_browse_domain(AvahiServer *s) {
1226 if (!s->config.publish_domain)
1229 if (s->browse_domain_entry_group)
1230 assert(avahi_s_entry_group_is_empty(s->browse_domain_entry_group));
1232 s->browse_domain_entry_group = avahi_s_entry_group_new(s, NULL, NULL);
1234 if (!s->browse_domain_entry_group) {
1235 avahi_log_warn("Failed to create browse domain entry group: %s", avahi_strerror(s->error));
1239 if (avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, AVAHI_DEFAULT_TTL, "b._dns-sd._udp.local", s->domain_name) < 0) {
1240 avahi_log_warn("Failed to add browse domain RR: %s", avahi_strerror(s->error));
1244 if (avahi_s_entry_group_commit(s->browse_domain_entry_group) < 0)
1245 avahi_log_warn("Failed to commit browse domain entry group: %s", avahi_strerror(s->error));
1248 static void register_stuff(AvahiServer *s) {
1251 server_set_state(s, AVAHI_SERVER_REGISTERING);
1252 s->n_host_rr_pending ++; /** Make sure that the state isn't changed tp AVAHI_SERVER_RUNNING too early */
1255 register_browse_domain(s);
1256 avahi_update_host_rrs(s->monitor, 0);
1258 s->n_host_rr_pending --;
1260 if (s->n_host_rr_pending == 0)
1261 server_set_state(s, AVAHI_SERVER_RUNNING);
1264 static void update_fqdn(AvahiServer *s) {
1268 assert(s->host_name);
1269 assert(s->domain_name);
1271 if (!(n = avahi_strdup_printf("%s.%s", s->host_name, s->domain_name)))
1274 avahi_free(s->host_name_fqdn);
1275 s->host_name_fqdn = n;
1278 int avahi_server_set_host_name(AvahiServer *s, const char *host_name) {
1282 if (host_name && !avahi_is_valid_host_name(host_name))
1283 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
1285 withdraw_host_rrs(s);
1287 avahi_free(s->host_name);
1288 s->host_name = host_name ? avahi_normalize_name(host_name) : avahi_get_host_name();
1289 s->host_name[strcspn(s->host_name, ".")] = 0;
1296 int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name) {
1298 assert(domain_name);
1300 if (domain_name && !avahi_is_valid_domain_name(domain_name))
1301 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
1303 withdraw_host_rrs(s);
1305 avahi_free(s->domain_name);
1306 s->domain_name = domain_name ? avahi_normalize_name(domain_name) : avahi_strdup("local");
1313 static int valid_server_config(const AvahiServerConfig *sc) {
1315 if (sc->host_name && !avahi_is_valid_host_name(sc->host_name))
1316 return AVAHI_ERR_INVALID_HOST_NAME;
1318 if (sc->domain_name && !avahi_is_valid_domain_name(sc->domain_name))
1319 return AVAHI_ERR_INVALID_DOMAIN_NAME;
1324 static int setup_sockets(AvahiServer *s) {
1327 s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1;
1328 s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1;
1330 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0)
1331 return AVAHI_ERR_NO_NETWORK;
1333 if (s->fd_ipv4 < 0 && s->config.use_ipv4)
1334 avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode");
1335 else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
1336 avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode");
1338 s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1;
1339 s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1;
1341 s->watch_ipv4 = s->watch_ipv6 = s->watch_legacy_unicast_ipv4 = s->watch_legacy_unicast_ipv6 = NULL;
1343 if (s->fd_ipv4 >= 0)
1344 s->watch_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_ipv4, AVAHI_WATCH_IN, socket_event, s);
1345 if (s->fd_ipv6 >= 0)
1346 s->watch_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_ipv6, AVAHI_WATCH_IN, socket_event, s);
1347 if (s->fd_legacy_unicast_ipv4 >= 0)
1348 s->watch_legacy_unicast_ipv4 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv4, AVAHI_WATCH_IN, socket_event, s);
1349 if (s->fd_legacy_unicast_ipv6 >= 0)
1350 s->watch_legacy_unicast_ipv6 = s->poll_api->watch_new(s->poll_api, s->fd_legacy_unicast_ipv6, AVAHI_WATCH_IN, socket_event, s);
1355 AvahiServer *avahi_server_new(const AvahiPoll *poll_api, const AvahiServerConfig *sc, AvahiServerCallback callback, void* userdata, int *error) {
1359 if ((e = valid_server_config(sc)) < 0) {
1365 if (!(s = avahi_new(AvahiServer, 1))) {
1367 *error = AVAHI_ERR_NO_MEMORY;
1372 s->poll_api = poll_api;
1375 avahi_server_config_copy(&s->config, sc);
1377 avahi_server_config_init(&s->config);
1379 if ((e = setup_sockets(s)) < 0) {
1383 avahi_server_config_free(&s->config);
1389 s->n_host_rr_pending = 0;
1390 s->need_entry_cleanup = 0;
1391 s->need_group_cleanup = 0;
1392 s->need_browser_cleanup = 0;
1394 s->time_event_queue = avahi_time_event_queue_new(poll_api);
1396 s->callback = callback;
1397 s->userdata = userdata;
1399 s->entries_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
1400 AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
1401 AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
1403 s->record_browser_hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
1404 AVAHI_LLIST_HEAD_INIT(AvahiSRecordBrowser, s->record_browsers);
1405 AVAHI_LLIST_HEAD_INIT(AvahiSHostNameResolver, s->host_name_resolvers);
1406 AVAHI_LLIST_HEAD_INIT(AvahiSAddressResolver, s->address_resolvers);
1407 AVAHI_LLIST_HEAD_INIT(AvahiSDomainBrowser, s->domain_browsers);
1408 AVAHI_LLIST_HEAD_INIT(AvahiSServiceTypeBrowser, s->service_type_browsers);
1409 AVAHI_LLIST_HEAD_INIT(AvahiSServiceBrowser, s->service_browsers);
1410 AVAHI_LLIST_HEAD_INIT(AvahiSServiceResolver, s->service_resolvers);
1411 AVAHI_LLIST_HEAD_INIT(AvahiSDNSServerBrowser, s->dns_server_browsers);
1413 s->legacy_unicast_reflect_slots = NULL;
1414 s->legacy_unicast_reflect_id = 0;
1417 s->local_service_cookie = (uint32_t) rand() * (uint32_t) rand();
1418 } while (s->local_service_cookie == AVAHI_SERVICE_COOKIE_INVALID);
1421 s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
1422 s->host_name[strcspn(s->host_name, ".")] = 0;
1423 s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : avahi_strdup("local");
1424 s->host_name_fqdn = NULL;
1427 s->record_list = avahi_record_list_new();
1429 s->state = AVAHI_SERVER_INVALID;
1431 s->monitor = avahi_interface_monitor_new(s);
1432 avahi_interface_monitor_sync(s->monitor);
1434 register_localhost(s);
1436 s->hinfo_entry_group = NULL;
1437 s->browse_domain_entry_group = NULL;
1440 s->error = AVAHI_OK;
1445 void avahi_server_free(AvahiServer* s) {
1449 free_entry(s, s->entries);
1451 avahi_interface_monitor_free(s->monitor);
1454 free_group(s, s->groups);
1458 while (s->dns_server_browsers)
1459 avahi_s_dns_server_browser_free(s->dns_server_browsers);
1460 while (s->host_name_resolvers)
1461 avahi_s_host_name_resolver_free(s->host_name_resolvers);
1462 while (s->address_resolvers)
1463 avahi_s_address_resolver_free(s->address_resolvers);
1464 while (s->domain_browsers)
1465 avahi_s_domain_browser_free(s->domain_browsers);
1466 while (s->service_type_browsers)
1467 avahi_s_service_type_browser_free(s->service_type_browsers);
1468 while (s->service_browsers)
1469 avahi_s_service_browser_free(s->service_browsers);
1470 while (s->service_resolvers)
1471 avahi_s_service_resolver_free(s->service_resolvers);
1472 while (s->record_browsers)
1473 avahi_s_record_browser_destroy(s->record_browsers);
1475 avahi_hashmap_free(s->record_browser_hashmap);
1476 avahi_hashmap_free(s->entries_by_key);
1478 avahi_time_event_queue_free(s->time_event_queue);
1480 avahi_record_list_free(s->record_list);
1483 s->poll_api->watch_free(s->watch_ipv4);
1485 s->poll_api->watch_free(s->watch_ipv6);
1486 if (s->watch_legacy_unicast_ipv4)
1487 s->poll_api->watch_free(s->watch_legacy_unicast_ipv4);
1488 if (s->watch_legacy_unicast_ipv6)
1489 s->poll_api->watch_free(s->watch_legacy_unicast_ipv6);
1491 if (s->fd_ipv4 >= 0)
1493 if (s->fd_ipv6 >= 0)
1495 if (s->fd_legacy_unicast_ipv4 >= 0)
1496 close(s->fd_legacy_unicast_ipv4);
1497 if (s->fd_legacy_unicast_ipv6 >= 0)
1498 close(s->fd_legacy_unicast_ipv6);
1500 avahi_free(s->host_name);
1501 avahi_free(s->domain_name);
1502 avahi_free(s->host_name_fqdn);
1504 avahi_server_config_free(&s->config);
1509 static int check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) {
1515 for (e = avahi_hashmap_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
1519 if (!(flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_UNIQUE))
1522 if ((flags & AVAHI_ENTRY_ALLOWMUTIPLE) && (e->flags & AVAHI_ENTRY_ALLOWMUTIPLE) )
1525 if ((interface <= 0 ||
1526 e->interface <= 0 ||
1527 e->interface == interface) &&
1528 (protocol == AVAHI_PROTO_UNSPEC ||
1529 e->protocol == AVAHI_PROTO_UNSPEC ||
1530 e->protocol == protocol))
1538 int avahi_server_add(
1540 AvahiSEntryGroup *g,
1541 AvahiIfIndex interface,
1542 AvahiProtocol protocol,
1543 AvahiEntryFlags flags,
1552 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_TTL);
1554 if (avahi_key_is_pattern(r->key))
1555 return avahi_server_set_errno(s, AVAHI_ERR_IS_PATTERN);
1557 if (!avahi_record_is_valid(r))
1558 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_RECORD);
1560 if (check_record_conflict(s, interface, protocol, r, flags) < 0)
1561 return avahi_server_set_errno(s, AVAHI_ERR_LOCAL_COLLISION);
1563 if (!(e = avahi_new(AvahiEntry, 1)))
1564 return avahi_server_set_errno(s, AVAHI_ERR_NO_NETWORK);
1567 e->record = avahi_record_ref(r);
1569 e->interface = interface;
1570 e->protocol = protocol;
1574 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
1576 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
1578 /* Insert into hash table indexed by name */
1579 t = avahi_hashmap_lookup(s->entries_by_key, e->record->key);
1580 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
1581 avahi_hashmap_replace(s->entries_by_key, e->record->key, t);
1583 /* Insert into group list */
1585 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
1587 avahi_announce_entry(s, e);
1592 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state) {
1593 AvahiEntry **e = (AvahiEntry**) state;
1598 *e = g ? g->entries : s->entries;
1600 while (*e && (*e)->dead)
1601 *e = g ? (*e)->by_group_next : (*e)->entries_next;
1606 return avahi_record_ref((*e)->record);
1609 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata) {
1615 callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
1617 for (e = s->entries; e; e = e->entries_next) {
1624 if (!(t = avahi_record_to_string(e->record)))
1625 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1627 snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
1630 callback(ln, userdata);
1633 avahi_dump_caches(s->monitor, callback, userdata);
1637 int avahi_server_add_ptr(
1639 AvahiSEntryGroup *g,
1640 AvahiIfIndex interface,
1641 AvahiProtocol protocol,
1642 AvahiEntryFlags flags,
1653 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl)))
1654 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1656 r->data.ptr.name = avahi_normalize_name(dest);
1657 ret = avahi_server_add(s, g, interface, protocol, flags, r);
1658 avahi_record_unref(r);
1662 int avahi_server_add_address(
1664 AvahiSEntryGroup *g,
1665 AvahiIfIndex interface,
1666 AvahiProtocol protocol,
1667 AvahiEntryFlags flags,
1677 if (!(n = avahi_normalize_name(name)))
1678 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1682 name = s->host_name_fqdn;
1684 if (!avahi_is_valid_domain_name(name)) {
1685 ret = avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
1689 if (a->proto == AVAHI_PROTO_INET) {
1693 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME))) {
1694 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1698 r->data.a.address = a->data.ipv4;
1699 ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
1700 avahi_record_unref(r);
1705 if (!(reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4))) {
1706 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1710 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
1711 avahi_free(reverse);
1717 assert(a->proto == AVAHI_PROTO_INET6);
1719 if (!(r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME))) {
1720 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1724 r->data.aaaa.address = a->data.ipv6;
1725 ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
1726 avahi_record_unref(r);
1731 if (!(reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6))) {
1732 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1736 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
1737 avahi_free(reverse);
1742 if (!(reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6))) {
1743 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1747 ret = avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
1748 avahi_free(reverse);
1758 static int server_add_txt_strlst_nocopy(
1760 AvahiSEntryGroup *g,
1761 AvahiIfIndex interface,
1762 AvahiProtocol protocol,
1763 AvahiEntryFlags flags,
1766 AvahiStringList *strlst) {
1773 if (!(r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl)))
1774 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1776 r->data.txt.string_list = strlst;
1777 ret = avahi_server_add(s, g, interface, protocol, flags, r);
1778 avahi_record_unref(r);
1783 int avahi_server_add_txt_strlst(
1785 AvahiSEntryGroup *g,
1786 AvahiIfIndex interface,
1787 AvahiProtocol protocol,
1788 AvahiEntryFlags flags,
1791 AvahiStringList *strlst) {
1795 return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_copy(strlst));
1798 int avahi_server_add_txt_va(
1800 AvahiSEntryGroup *g,
1801 AvahiIfIndex interface,
1802 AvahiProtocol protocol,
1803 AvahiEntryFlags flags,
1810 return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_new_va(va));
1813 int avahi_server_add_txt(
1815 AvahiSEntryGroup *g,
1816 AvahiIfIndex interface,
1817 AvahiProtocol protocol,
1818 AvahiEntryFlags flags,
1829 ret = avahi_server_add_txt_va(s, g, interface, protocol, flags, ttl, name, va);
1835 static void escape_service_name(char *d, size_t size, const char *s) {
1840 while (*s && size >= 2) {
1841 if (*s == '.' || *s == '\\') {
1857 static AvahiStringList *add_magic_cookie(
1859 AvahiStringList *strlst) {
1863 if (!s->config.add_service_cookie)
1866 if (avahi_string_list_find(strlst, AVAHI_SERVICE_COOKIE))
1867 /* This string list already contains a magic cookie */
1870 return avahi_string_list_add_printf(strlst, AVAHI_SERVICE_COOKIE"=%u", s->local_service_cookie);
1873 static int server_add_service_strlst_nocopy(
1875 AvahiSEntryGroup *g,
1876 AvahiIfIndex interface,
1877 AvahiProtocol protocol,
1883 AvahiStringList *strlst) {
1885 char ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
1886 char *t = NULL, *d = NULL, *h = NULL;
1887 AvahiRecord *r = NULL;
1894 if (!avahi_is_valid_service_name(name))
1895 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME);
1897 if (!avahi_is_valid_service_type(type))
1898 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_TYPE);
1900 if (domain && !avahi_is_valid_domain_name(domain))
1901 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
1903 if (host && !avahi_is_valid_domain_name(host))
1904 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
1906 escape_service_name(ename, sizeof(ename), name);
1909 domain = s->domain_name;
1912 host = s->host_name_fqdn;
1914 if (!(d = avahi_normalize_name(domain)) ||
1915 !(t = avahi_normalize_name(type)) ||
1916 !(h = avahi_normalize_name(host))) {
1917 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1921 snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d);
1922 snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d);
1924 if ((ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, ptr_name, svc_name)) < 0)
1927 if (!(r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
1928 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
1932 r->data.srv.priority = 0;
1933 r->data.srv.weight = 0;
1934 r->data.srv.port = port;
1935 r->data.srv.name = h;
1937 ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
1938 avahi_record_unref(r);
1943 strlst = add_magic_cookie(s, strlst);
1945 ret = server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
1951 snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d);
1952 ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name);
1960 avahi_string_list_free(strlst);
1965 int avahi_server_add_service_strlst(
1967 AvahiSEntryGroup *g,
1968 AvahiIfIndex interface,
1969 AvahiProtocol protocol,
1975 AvahiStringList *strlst) {
1981 return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_copy(strlst));
1984 int avahi_server_add_service_va(
1986 AvahiSEntryGroup *g,
1987 AvahiIfIndex interface,
1988 AvahiProtocol protocol,
2000 return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_new_va(va));
2003 int avahi_server_add_service(
2005 AvahiSEntryGroup *g,
2006 AvahiIfIndex interface,
2007 AvahiProtocol protocol,
2023 ret = avahi_server_add_service_va(s, g, interface, protocol, name, type, domain, host, port, va);
2028 static void hexstring(char *s, size_t sl, const void *p, size_t pl) {
2029 static const char hex[] = "0123456789abcdef";
2031 const uint8_t *k = p;
2033 while (sl > 1 && pl > 0) {
2034 *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
2050 int avahi_server_add_dns_server_address(
2052 AvahiSEntryGroup *g,
2053 AvahiIfIndex interface,
2054 AvahiProtocol protocol,
2056 AvahiDNSServerType type,
2057 const AvahiAddress *address,
2058 uint16_t port /** should be 53 */) {
2066 assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
2067 assert(address->proto == AVAHI_PROTO_INET || address->proto == AVAHI_PROTO_INET6);
2070 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
2072 if (domain && !avahi_is_valid_domain_name(domain))
2073 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
2075 if (address->proto == AVAHI_PROTO_INET) {
2076 hexstring(n+3, sizeof(n)-3, &address->data, 4);
2077 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
2078 r->data.a.address = address->data.ipv4;
2080 hexstring(n+3, sizeof(n)-3, &address->data, 6);
2081 r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
2082 r->data.aaaa.address = address->data.ipv6;
2086 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2088 ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
2089 avahi_record_unref(r);
2094 return avahi_server_add_dns_server_name(s, g, interface, protocol, domain, type, n, port);
2097 int avahi_server_add_dns_server_name(
2099 AvahiSEntryGroup *g,
2100 AvahiIfIndex interface,
2101 AvahiProtocol protocol,
2103 AvahiDNSServerType type,
2105 uint16_t port /** should be 53 */) {
2108 char t[256], *d = NULL, *n = NULL;
2113 assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
2116 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_PORT);
2118 if (!avahi_is_valid_domain_name(name))
2119 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_HOST_NAME);
2121 if (domain && !avahi_is_valid_domain_name(domain))
2122 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
2126 domain = s->domain_name;
2128 if (!(n = avahi_normalize_name(name)) ||
2129 !(d = avahi_normalize_name(domain))) {
2132 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2135 snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d);
2138 if (!(r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME))) {
2140 return avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2143 r->data.srv.priority = 0;
2144 r->data.srv.weight = 0;
2145 r->data.srv.port = port;
2146 r->data.srv.name = n;
2147 ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_NULL, r);
2148 avahi_record_unref(r);
2153 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
2154 AvahiKey *k = userdata;
2160 avahi_interface_post_query(i, k, 0);
2163 void avahi_server_post_query(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) {
2167 avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
2170 void avahi_s_entry_group_change_state(AvahiSEntryGroup *g, AvahiEntryGroupState state) {
2173 if (g->state == state)
2176 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
2181 g->callback(g->server, g, state, g->userdata);
2184 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata) {
2185 AvahiSEntryGroup *g;
2189 if (!(g = avahi_new(AvahiSEntryGroup, 1))) {
2190 avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2195 g->callback = callback;
2196 g->userdata = userdata;
2198 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
2200 g->n_register_try = 0;
2201 g->register_time_event = NULL;
2202 g->register_time.tv_sec = 0;
2203 g->register_time.tv_usec = 0;
2204 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
2206 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
2210 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
2216 for (e = g->entries; e; e = e->by_group_next) {
2218 avahi_goodbye_entry(g->server, e, 1);
2223 if (g->register_time_event) {
2224 avahi_time_event_free(g->register_time_event);
2225 g->register_time_event = NULL;
2230 g->server->need_group_cleanup = 1;
2231 g->server->need_entry_cleanup = 1;
2234 static void entry_group_commit_real(AvahiSEntryGroup *g) {
2237 gettimeofday(&g->register_time, NULL);
2239 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
2242 avahi_announce_group(g->server, g);
2243 avahi_s_entry_group_check_probed(g, 0);
2247 static void entry_group_register_time_event_callback(AvahiTimeEvent *e, void* userdata) {
2248 AvahiSEntryGroup *g = userdata;
2251 /* avahi_log_debug("Holdoff passed, waking up and going on."); */
2253 avahi_time_event_free(g->register_time_event);
2254 g->register_time_event = NULL;
2256 /* Holdoff time passed, so let's start probing */
2257 entry_group_commit_real(g);
2260 int avahi_s_entry_group_commit(AvahiSEntryGroup *g) {
2266 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
2267 return avahi_server_set_errno(g->server, AVAHI_ERR_BAD_STATE);
2269 g->n_register_try++;
2271 avahi_timeval_add(&g->register_time,
2272 1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
2273 AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
2274 AVAHI_RR_HOLDOFF_MSEC));
2276 gettimeofday(&now, NULL);
2278 if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
2279 /* Holdoff time passed, so let's start probing */
2280 /* avahi_log_debug("Holdoff passed, directly going on."); */
2282 entry_group_commit_real(g);
2284 /* avahi_log_debug("Holdoff not passed, sleeping."); */
2286 /* Holdoff time has not yet passed, so let's wait */
2287 assert(!g->register_time_event);
2288 g->register_time_event = avahi_time_event_new(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
2290 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
2296 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
2300 if (g->register_time_event) {
2301 avahi_time_event_free(g->register_time_event);
2302 g->register_time_event = NULL;
2305 for (e = g->entries; e; e = e->by_group_next) {
2307 avahi_goodbye_entry(g->server, e, 1);
2312 if (g->register_time_event) {
2313 avahi_time_event_free(g->register_time_event);
2314 g->register_time_event = NULL;
2317 g->server->need_entry_cleanup = 1;
2320 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
2323 int avahi_entry_is_commited(AvahiEntry *e) {
2328 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
2329 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
2332 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g) {
2339 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata) {
2342 g->userdata = userdata;
2345 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g) {
2351 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
2355 /* Look for an entry that is not dead */
2356 for (e = g->entries; e; e = e->by_group_next)
2363 const char* avahi_server_get_domain_name(AvahiServer *s) {
2366 return s->domain_name;
2369 const char* avahi_server_get_host_name(AvahiServer *s) {
2372 return s->host_name;
2375 const char* avahi_server_get_host_name_fqdn(AvahiServer *s) {
2378 return s->host_name_fqdn;
2381 void* avahi_server_get_data(AvahiServer *s) {
2387 void avahi_server_set_data(AvahiServer *s, void* userdata) {
2390 s->userdata = userdata;
2393 AvahiServerState avahi_server_get_state(AvahiServer *s) {
2399 AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) {
2402 memset(c, 0, sizeof(AvahiServerConfig));
2405 c->host_name = NULL;
2406 c->domain_name = NULL;
2407 c->check_response_ttl = 0;
2408 c->publish_hinfo = 1;
2409 c->publish_addresses = 1;
2410 c->publish_workstation = 1;
2411 c->publish_domain = 1;
2412 c->use_iff_running = 0;
2413 c->enable_reflector = 0;
2415 c->add_service_cookie = 1;
2420 void avahi_server_config_free(AvahiServerConfig *c) {
2423 avahi_free(c->host_name);
2424 avahi_free(c->domain_name);
2427 AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) {
2428 char *d = NULL, *h = NULL;
2433 if (!(h = avahi_strdup(c->host_name)))
2437 if (!(d = avahi_strdup(c->domain_name))) {
2444 ret->domain_name = d;
2449 int avahi_server_errno(AvahiServer *s) {
2455 /* Just for internal use */
2456 int avahi_server_set_errno(AvahiServer *s, int error) {
2459 return s->error = error;
2462 uint32_t avahi_server_get_local_service_cookie(AvahiServer *s) {
2465 return s->local_service_cookie;
2468 int avahi_server_is_service_local(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char*domain) {
2469 AvahiKey *key = NULL;
2471 char ename[64], n[256];
2480 if (!avahi_is_valid_service_name(name))
2481 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_NAME);
2483 if (!avahi_is_valid_service_type(type))
2484 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_SERVICE_TYPE);
2486 if (domain && !avahi_is_valid_domain_name(domain))
2487 return avahi_server_set_errno(s, AVAHI_ERR_INVALID_DOMAIN_NAME);
2489 escape_service_name(ename, sizeof(ename), name);
2491 if (!(d = avahi_normalize_name(domain)) ||
2492 !(t = avahi_normalize_name(type))) {
2493 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2496 snprintf(n, sizeof(n), "%s.%s.%s", ename, t, d);
2498 if (!(key = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) {
2499 ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
2505 for (e = avahi_hashmap_lookup(s->entries_by_key, key); e; e = e->by_key_next) {
2507 if ((e->interface == interface || e->interface <= 0 || interface <= 0) &&
2508 (e->protocol == protocol || e->protocol == AVAHI_PROTO_UNSPEC || protocol == AVAHI_PROTO_UNSPEC)) {
2514 avahi_key_unref(key);
2529 avahi_key_unref(key);