2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <avahi-common/error.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/domain.h>
42 #include "multicast-lookup.h"
45 void avahi_interface_address_update_rrs(AvahiInterfaceAddress *a, int remove_rrs) {
46 AvahiInterfaceMonitor *m;
51 if (m->list_complete &&
52 avahi_interface_address_is_relevant(a) &&
53 avahi_interface_is_relevant(a->interface) &&
55 m->server->config.publish_addresses &&
56 (m->server->state == AVAHI_SERVER_RUNNING ||
57 m->server->state == AVAHI_SERVER_REGISTERING)) {
59 /* Fill the entry group */
61 a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
63 if (!a->entry_group) /* OOM */
66 if (avahi_s_entry_group_is_empty(a->entry_group)) {
67 char t[AVAHI_ADDRESS_STR_MAX];
70 p = (a->interface->protocol == AVAHI_PROTO_INET && m->server->config.publish_a_on_ipv6) ||
71 (a->interface->protocol == AVAHI_PROTO_INET6 && m->server->config.publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : a->interface->protocol;
73 avahi_address_snprint(t, sizeof(t), &a->address);
74 avahi_log_info("Registering new address record for %s on %s.%s.", t, a->interface->hardware->name, p == AVAHI_PROTO_UNSPEC ? "*" : avahi_proto_to_string(p));
76 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, p, 0, NULL, &a->address) < 0) {
77 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
78 avahi_s_entry_group_free(a->entry_group);
79 a->entry_group = NULL;
83 avahi_s_entry_group_commit(a->entry_group);
87 /* Clear the entry group */
89 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
90 char t[AVAHI_ADDRESS_STR_MAX];
91 avahi_address_snprint(t, sizeof(t), &a->address);
93 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
94 m->server->state == AVAHI_SERVER_REGISTERING)
95 avahi_server_decrease_host_rr_pending(m->server);
97 avahi_log_info("Withdrawing address record for %s on %s.", t, a->interface->hardware->name);
99 avahi_s_entry_group_reset(a->entry_group);
104 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
105 AvahiInterfaceAddress *a;
109 for (a = i->addresses; a; a = a->address_next)
110 avahi_interface_address_update_rrs(a, remove_rrs);
113 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
115 AvahiInterfaceMonitor *m;
120 for (i = hw->interfaces; i; i = i->by_hardware_next)
121 avahi_interface_update_rrs(i, remove_rrs);
123 if (m->list_complete &&
125 m->server->config.publish_workstation &&
126 (m->server->state == AVAHI_SERVER_RUNNING)) {
128 if (!hw->entry_group)
129 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
131 if (!hw->entry_group)
134 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
135 char name[AVAHI_LABEL_MAX], mac[256];
137 avahi_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size);
138 snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, mac);
140 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
141 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
142 avahi_s_entry_group_free(hw->entry_group);
143 hw->entry_group = NULL;
145 avahi_s_entry_group_commit(hw->entry_group);
150 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
152 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
153 avahi_server_decrease_host_rr_pending(m->server);
155 avahi_s_entry_group_reset(hw->entry_group);
160 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
161 AvahiHwInterface *hw;
165 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
166 avahi_hw_interface_update_rrs(hw, remove_rrs);
169 static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
170 char at[AVAHI_ADDRESS_STR_MAX];
174 if (!!join == !!i->mcast_joined)
177 if ((i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 < 0) ||
178 (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 < 0))
182 AvahiInterfaceAddress *a;
184 /* Look if there's an address with global scope */
185 for (a = i->addresses; a; a = a->address_next)
189 /* No address with a global scope has been found, so let's use
194 /* Hmm, there is no address available. */
198 i->local_mcast_address = a->address;
201 avahi_log_info("%s mDNS multicast group on interface %s.%s with address %s.",
202 join ? "Joining" : "Leaving",
204 avahi_proto_to_string(i->protocol),
205 avahi_address_snprint(at, sizeof(at), &i->local_mcast_address));
207 if (i->protocol == AVAHI_PROTO_INET6)
208 r = avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, &i->local_mcast_address.data.ipv6, i->hardware->index, join);
210 assert(i->protocol == AVAHI_PROTO_INET);
212 r = avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &i->local_mcast_address.data.ipv4, i->hardware->index, join);
218 i->mcast_joined = join;
223 static int interface_mdns_mcast_rejoin(AvahiInterface *i) {
224 AvahiInterfaceAddress *a, *usable = NULL, *found = NULL;
227 if (!i->mcast_joined)
230 /* Check whether old address we joined with is still available. If
231 * not, rejoin using an other address. */
233 for (a = i->addresses; a; a = a->address_next) {
234 if (a->global_scope && !usable)
237 if (avahi_address_cmp(&a->address, &i->local_mcast_address) == 0) {
240 /* No action necessary: the address still exists and
241 * has global scope. */
248 if (found && !usable)
249 /* No action necessary: the address still exists and no better one has been found */
252 interface_mdns_mcast_join(i, 0);
253 return interface_mdns_mcast_join(i, 1);
256 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
258 assert(a->interface);
260 avahi_interface_address_update_rrs(a, 1);
261 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
264 avahi_s_entry_group_free(a->entry_group);
266 interface_mdns_mcast_rejoin(a->interface);
271 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
274 /* Handle goodbyes and remove announcers */
275 avahi_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
276 avahi_response_scheduler_force(i->response_scheduler);
277 assert(!i->announcers);
280 interface_mdns_mcast_join(i, 0);
282 /* Remove queriers */
283 avahi_querier_free_all(i);
284 avahi_hashmap_free(i->queriers_by_key);
286 /* Remove local RRs */
287 avahi_interface_update_rrs(i, 1);
290 avahi_interface_address_free(i->addresses);
292 avahi_response_scheduler_free(i->response_scheduler);
293 avahi_query_scheduler_free(i->query_scheduler);
294 avahi_probe_scheduler_free(i->probe_scheduler);
295 avahi_cache_free(i->cache);
297 AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
298 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
303 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
306 avahi_hw_interface_update_rrs(hw, 1);
308 while (hw->interfaces)
309 avahi_interface_free(hw->interfaces, send_goodbye);
312 avahi_s_entry_group_free(hw->entry_group);
314 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
315 avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
317 avahi_free(hw->name);
321 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
326 assert(AVAHI_PROTO_VALID(protocol));
328 if (!(i = avahi_new(AvahiInterface, 1)))
333 i->protocol = protocol;
337 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
338 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, i->announcers);
340 AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
341 i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
343 i->cache = avahi_cache_new(m->server, i);
344 i->response_scheduler = avahi_response_scheduler_new(i);
345 i->query_scheduler = avahi_query_scheduler_new(i);
346 i->probe_scheduler = avahi_probe_scheduler_new(i);
348 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
351 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
352 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
360 avahi_cache_free(i->cache);
361 if (i->response_scheduler)
362 avahi_response_scheduler_free(i->response_scheduler);
363 if (i->query_scheduler)
364 avahi_query_scheduler_free(i->query_scheduler);
365 if (i->probe_scheduler)
366 avahi_probe_scheduler_free(i->probe_scheduler);
372 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
373 AvahiHwInterface *hw;
376 assert(AVAHI_IF_VALID(idx));
378 if (!(hw = avahi_new(AvahiHwInterface, 1)))
386 hw->mac_address_size = 0;
387 hw->entry_group = NULL;
389 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
390 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
392 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
394 if (m->server->fd_ipv4 >= 0 || m->server->config.publish_a_on_ipv6)
395 avahi_interface_new(m, hw, AVAHI_PROTO_INET);
396 if (m->server->fd_ipv6 >= 0 || m->server->config.publish_aaaa_on_ipv4)
397 avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
402 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
403 AvahiInterfaceAddress *a;
408 if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
414 a->prefix_len = prefix_len;
416 a->entry_group = NULL;
418 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
423 void avahi_interface_check_relevant(AvahiInterface *i) {
425 AvahiInterfaceMonitor *m;
430 b = avahi_interface_is_relevant(i);
432 if (m->list_complete && b && !i->announcing) {
433 interface_mdns_mcast_join(i, 1);
435 if (i->mcast_joined) {
436 avahi_log_info("New relevant interface %s.%s for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
439 avahi_announce_interface(m->server, i);
440 avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
443 } else if (!b && i->announcing) {
444 avahi_log_info("Interface %s.%s no longer relevant for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
446 interface_mdns_mcast_join(i, 0);
448 avahi_goodbye_interface(m->server, i, 0, 1);
449 avahi_querier_free_all(i);
451 avahi_response_scheduler_clear(i->response_scheduler);
452 avahi_query_scheduler_clear(i->query_scheduler);
453 avahi_probe_scheduler_clear(i->probe_scheduler);
454 avahi_cache_flush(i->cache);
459 interface_mdns_mcast_rejoin(i);
462 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
467 for (i = hw->interfaces; i; i = i->by_hardware_next)
468 avahi_interface_check_relevant(i);
471 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
476 for (i = m->interfaces; i; i = i->interface_next)
477 avahi_interface_check_relevant(i);
480 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
481 AvahiInterfaceMonitor *m = NULL;
483 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
484 return NULL; /* OOM */
487 m->list_complete = 0;
488 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
490 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
491 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
493 if (avahi_interface_monitor_init_osdep(m) < 0)
499 avahi_interface_monitor_free(m);
503 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
506 while (m->hw_interfaces)
507 avahi_hw_interface_free(m->hw_interfaces, 1);
509 assert(!m->interfaces);
511 avahi_interface_monitor_free_osdep(m);
514 avahi_hashmap_free(m->hashmap);
520 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
521 AvahiHwInterface *hw;
526 assert(protocol != AVAHI_PROTO_UNSPEC);
528 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
531 for (i = hw->interfaces; i; i = i->by_hardware_next)
532 if (i->protocol == protocol)
538 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
542 return avahi_hashmap_lookup(m->hashmap, &idx);
545 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
546 AvahiInterfaceAddress *ia;
552 for (ia = i->addresses; ia; ia = ia->address_next)
553 if (avahi_address_cmp(&ia->address, raddr) == 0)
559 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
566 assert(!a || a->proto == i->protocol);
568 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
569 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, i->mcast_joined ? &i->local_mcast_address.data.ipv4 : NULL, a ? &a->data.ipv4 : NULL, port);
570 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
571 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, i->mcast_joined ? &i->local_mcast_address.data.ipv6 : NULL, a ? &a->data.ipv6 : NULL, port);
574 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
578 avahi_interface_send_packet_unicast(i, p, NULL, 0);
581 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately, unsigned *ret_id) {
588 return avahi_query_scheduler_post(i->query_scheduler, key, immediately, ret_id);
591 int avahi_interface_withraw_query(AvahiInterface *i, unsigned id) {
593 return avahi_query_scheduler_withdraw_by_id(i->query_scheduler, id);
596 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
603 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
606 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
613 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
616 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
620 for (i = m->interfaces; i; i = i->interface_next) {
621 if (avahi_interface_is_relevant(i)) {
623 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
624 callback(ln, userdata);
625 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
633 static int avahi_interface_is_relevant_internal(AvahiInterface *i) {
634 AvahiInterfaceAddress *a;
638 if (!i->hardware->flags_ok)
641 for (a = i->addresses; a; a = a->address_next)
642 if (avahi_interface_address_is_relevant(a))
648 int avahi_interface_is_relevant(AvahiInterface *i) {
652 for (l = i->monitor->server->config.deny_interfaces; l; l = l->next)
653 if (strcasecmp((char*) l->text, i->hardware->name) == 0)
656 if (i->monitor->server->config.allow_interfaces) {
658 for (l = i->monitor->server->config.allow_interfaces; l; l = l->next)
659 if (strcasecmp((char*) l->text, i->hardware->name) == 0)
666 return avahi_interface_is_relevant_internal(i);
669 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
670 AvahiInterfaceAddress *b;
673 /* Publish public IP addresses */
678 /* Publish link local IP addresses if they are the only ones on the link */
679 for (b = a->interface->addresses; b; b = b->address_next) {
693 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
696 if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
699 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
705 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
709 if (interface != AVAHI_IF_UNSPEC) {
710 if (protocol != AVAHI_PROTO_UNSPEC) {
713 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
714 callback(m, i, userdata);
717 AvahiHwInterface *hw;
720 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
721 for (i = hw->interfaces; i; i = i->by_hardware_next)
722 if (avahi_interface_match(i, interface, protocol))
723 callback(m, i, userdata);
729 for (i = m->interfaces; i; i = i->interface_next)
730 if (avahi_interface_match(i, interface, protocol))
731 callback(m, i, userdata);
736 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
738 AvahiInterfaceAddress *ia;
742 for (i = m->interfaces; i; i = i->interface_next)
743 for (ia = i->addresses; ia; ia = ia->address_next)
744 if (avahi_address_cmp(a, &ia->address) == 0)
750 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
751 AvahiInterfaceAddress *ia;
756 if (a->proto != i->protocol)
759 for (ia = i->addresses; ia; ia = ia->address_next) {
761 if (a->proto == AVAHI_PROTO_INET) {
764 m = ~(((uint32_t) -1) >> ia->prefix_len);
766 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
771 assert(a->proto == AVAHI_PROTO_INET6);
775 for (j = 0; j < 16; j++) {
789 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
798 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
800 AvahiInterfaceAddress *j;
803 assert(iface != AVAHI_IF_UNSPEC);
806 if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
809 for (j = i->addresses; j; j = j->address_next)
810 if (avahi_address_cmp(a, &j->address) == 0)
816 AvahiIfIndex avahi_find_interface_for_address(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
820 /* Some stupid OS don't support passing the interface index when a
821 * packet is received. We have to work around that limitation by
822 * looking for an interface that has the incoming address
823 * attached. This is sometimes ambiguous, but we have to live with
826 for (i = m->interfaces; i; i = i->interface_next) {
827 AvahiInterfaceAddress *ai;
829 if (i->protocol != a->proto)
832 for (ai = i->addresses; ai; ai = ai->address_next)
833 if (avahi_address_cmp(a, &ai->address) == 0)
834 return i->hardware->index;
837 return AVAHI_IF_UNSPEC;