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
27 #include <sys/socket.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 (a->interface->announcing &&
53 avahi_interface_address_is_relevant(a) &&
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];
68 avahi_address_snprint(t, sizeof(t), &a->address);
70 avahi_log_info("Registering new address %s on %s.", t, a->interface->hardware->name);
72 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
73 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
74 avahi_s_entry_group_free(a->entry_group);
75 a->entry_group = NULL;
79 avahi_s_entry_group_commit(a->entry_group);
83 /* Clear the entry group */
85 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
86 char t[AVAHI_ADDRESS_STR_MAX];
87 avahi_address_snprint(t, sizeof(t), &a->address);
89 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
90 m->server->state == AVAHI_SERVER_REGISTERING)
91 avahi_server_decrease_host_rr_pending(m->server);
93 avahi_log_info("Withdrawing address %s on %s.", t, a->interface->hardware->name);
95 avahi_s_entry_group_reset(a->entry_group);
100 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
101 AvahiInterfaceAddress *a;
105 for (a = i->addresses; a; a = a->address_next)
106 avahi_interface_address_update_rrs(a, remove_rrs);
109 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
111 AvahiInterfaceMonitor *m;
116 for (i = hw->interfaces; i; i = i->by_hardware_next)
117 avahi_interface_update_rrs(i, remove_rrs);
119 if (m->list_complete &&
121 m->server->config.publish_workstation &&
122 (m->server->state == AVAHI_SERVER_RUNNING ||
123 m->server->state == AVAHI_SERVER_REGISTERING)) {
125 if (!hw->entry_group)
126 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
128 if (!hw->entry_group)
131 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
132 char name[AVAHI_LABEL_MAX];
135 if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size)))
138 snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, t);
144 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
145 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
146 avahi_s_entry_group_free(hw->entry_group);
147 hw->entry_group = NULL;
149 avahi_s_entry_group_commit(hw->entry_group);
154 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
156 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
157 avahi_server_decrease_host_rr_pending(m->server);
159 avahi_s_entry_group_reset(hw->entry_group);
164 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
165 AvahiHwInterface *hw;
169 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
170 avahi_hw_interface_update_rrs(hw, remove_rrs);
173 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
175 assert(a->interface);
177 avahi_interface_address_update_rrs(a, 1);
178 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
181 avahi_s_entry_group_free(a->entry_group);
186 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
189 /* Handle goodbyes and remove announcers */
190 avahi_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
191 avahi_response_scheduler_force(i->response_scheduler);
192 assert(!i->announcers);
194 /* Remove queriers */
195 avahi_querier_free_all(i);
196 avahi_hashmap_free(i->queriers_by_key);
198 /* Remove local RRs */
199 avahi_interface_update_rrs(i, 1);
202 avahi_interface_address_free(i->addresses);
204 avahi_response_scheduler_free(i->response_scheduler);
205 avahi_query_scheduler_free(i->query_scheduler);
206 avahi_probe_scheduler_free(i->probe_scheduler);
207 avahi_cache_free(i->cache);
209 AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
210 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
215 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
218 avahi_hw_interface_update_rrs(hw, 1);
220 while (hw->interfaces)
221 avahi_interface_free(hw->interfaces, send_goodbye);
224 avahi_s_entry_group_free(hw->entry_group);
226 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
227 avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
229 avahi_free(hw->name);
233 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
238 assert(AVAHI_PROTO_VALID(protocol));
240 if (!(i = avahi_new(AvahiInterface, 1)))
245 i->protocol = protocol;
248 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
249 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, i->announcers);
251 AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
252 i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
254 i->cache = avahi_cache_new(m->server, i);
255 i->response_scheduler = avahi_response_scheduler_new(i);
256 i->query_scheduler = avahi_query_scheduler_new(i);
257 i->probe_scheduler = avahi_probe_scheduler_new(i);
259 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
262 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
263 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
271 avahi_cache_free(i->cache);
272 if (i->response_scheduler)
273 avahi_response_scheduler_free(i->response_scheduler);
274 if (i->query_scheduler)
275 avahi_query_scheduler_free(i->query_scheduler);
276 if (i->probe_scheduler)
277 avahi_probe_scheduler_free(i->probe_scheduler);
283 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
284 AvahiHwInterface *hw;
287 assert(AVAHI_IF_VALID(idx));
289 if (!(hw = avahi_new(AvahiHwInterface, 1)))
297 hw->mac_address_size = 0;
298 hw->entry_group = NULL;
300 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
301 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
303 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
305 if (m->server->fd_ipv4 >= 0)
306 avahi_interface_new(m, hw, AVAHI_PROTO_INET);
307 if (m->server->fd_ipv6 >= 0)
308 avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
313 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
314 AvahiInterfaceAddress *a;
319 if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
325 a->prefix_len = prefix_len;
327 a->entry_group = NULL;
329 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
334 static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
336 if (i->protocol == AVAHI_PROTO_INET6)
337 return avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, join);
338 else if (i->protocol == AVAHI_PROTO_INET) {
340 #ifdef HAVE_STRUCT_IP_MREQN
341 return avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, join);
344 AvahiInterfaceAddress *ia;
347 for (ia = i->addresses; ia; ia = ia->address_next)
348 r |= avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &ia->address, join);
358 void avahi_interface_check_relevant(AvahiInterface *i) {
360 AvahiInterfaceMonitor *m;
365 b = avahi_interface_is_relevant(i);
367 if (m->list_complete && b && !i->announcing) {
368 avahi_log_info("New relevant interface %s.%s.", i->hardware->name, avahi_proto_to_string(i->protocol));
370 interface_mdns_mcast_join(i, 1);
373 avahi_announce_interface(m->server, i);
374 avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
375 } else if (!b && i->announcing) {
376 avahi_log_info("Interface %s.%s no longer relevant.", i->hardware->name, avahi_proto_to_string(i->protocol));
378 interface_mdns_mcast_join(i, 0);
380 avahi_goodbye_interface(m->server, i, 0, 1);
381 avahi_querier_free_all(i);
383 avahi_response_scheduler_clear(i->response_scheduler);
384 avahi_query_scheduler_clear(i->query_scheduler);
385 avahi_probe_scheduler_clear(i->probe_scheduler);
386 avahi_cache_flush(i->cache);
392 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
397 for (i = hw->interfaces; i; i = i->by_hardware_next)
398 avahi_interface_check_relevant(i);
401 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
406 for (i = m->interfaces; i; i = i->interface_next)
407 avahi_interface_check_relevant(i);
410 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
411 AvahiInterfaceMonitor *m = NULL;
413 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
414 return NULL; /* OOM */
417 m->list_complete = 0;
418 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
420 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
421 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
423 if (avahi_interface_monitor_init_osdep(m) < 0)
429 avahi_interface_monitor_free(m);
433 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
436 while (m->hw_interfaces)
437 avahi_hw_interface_free(m->hw_interfaces, 1);
439 assert(!m->interfaces);
441 avahi_interface_monitor_free_osdep(m);
444 avahi_hashmap_free(m->hashmap);
450 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
451 AvahiHwInterface *hw;
456 assert(protocol != AVAHI_PROTO_UNSPEC);
458 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
461 for (i = hw->interfaces; i; i = i->by_hardware_next)
462 if (i->protocol == protocol)
468 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
472 return avahi_hashmap_lookup(m->hashmap, &idx);
475 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
476 AvahiInterfaceAddress *ia;
482 for (ia = i->addresses; ia; ia = ia->address_next)
483 if (avahi_address_cmp(&ia->address, raddr) == 0)
490 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
493 /* char t[AVAHI_ADDRESS_STR_MAX]; */
495 if (!avahi_interface_is_relevant(i))
498 assert(!a || a->proto == i->protocol);
501 /* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
503 /* avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
505 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
506 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
507 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
508 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
511 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
515 avahi_interface_send_packet_unicast(i, p, NULL, 0);
518 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) {
522 if (avahi_interface_is_relevant(i))
523 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
528 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
532 if (avahi_interface_is_relevant(i))
533 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
538 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
542 if (avahi_interface_is_relevant(i))
543 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
548 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
552 for (i = m->interfaces; i; i = i->interface_next) {
553 if (avahi_interface_is_relevant(i)) {
555 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
556 callback(ln, userdata);
557 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
565 int avahi_interface_is_relevant(AvahiInterface *i) {
566 AvahiInterfaceAddress *a;
567 int relevant_address;
571 relevant_address = 0;
573 for (a = i->addresses; a; a = a->address_next)
574 if (avahi_interface_address_is_relevant(a)) {
575 relevant_address = 1;
579 return i->hardware->flags_ok && relevant_address;
582 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
583 AvahiInterfaceAddress *b;
586 /* Publish public IP addresses */
591 /* Publish link local IP addresses if they are the only ones on the link */
592 for (b = a->interface->addresses; b; b = b->address_next) {
606 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
609 if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
612 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
618 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
622 if (interface != AVAHI_IF_UNSPEC) {
623 if (protocol != AVAHI_PROTO_UNSPEC) {
626 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
627 callback(m, i, userdata);
630 AvahiHwInterface *hw;
633 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
634 for (i = hw->interfaces; i; i = i->by_hardware_next)
635 if (avahi_interface_match(i, interface, protocol))
636 callback(m, i, userdata);
642 for (i = m->interfaces; i; i = i->interface_next)
643 if (avahi_interface_match(i, interface, protocol))
644 callback(m, i, userdata);
649 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
651 AvahiInterfaceAddress *ia;
655 for (i = m->interfaces; i; i = i->interface_next)
656 for (ia = i->addresses; ia; ia = ia->address_next)
657 if (avahi_address_cmp(a, &ia->address) == 0)
663 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
664 AvahiInterfaceAddress *ia;
669 if (a->proto != i->protocol)
672 for (ia = i->addresses; ia; ia = ia->address_next) {
674 if (a->proto == AVAHI_PROTO_INET) {
677 m = ~(((uint32_t) -1) >> ia->prefix_len);
679 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
684 assert(a->proto == AVAHI_PROTO_INET6);
688 for (j = 0; j < 16; j++) {
702 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
711 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
713 AvahiInterfaceAddress *j;
716 assert(iface != AVAHI_IF_UNSPEC);
719 if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
722 for (j = i->addresses; j; j = j->address_next)
723 if (avahi_address_cmp(a, &j->address) == 0)