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>
31 #include <avahi-common/error.h>
32 #include <avahi-common/malloc.h>
33 #include <avahi-common/domain.h>
41 #include "multicast-lookup.h"
44 void avahi_interface_address_update_rrs(AvahiInterfaceAddress *a, int remove_rrs) {
45 AvahiInterfaceMonitor *m;
50 if (a->interface->announcing &&
52 avahi_interface_address_is_relevant(a) &&
54 m->server->config.publish_addresses &&
55 (m->server->state == AVAHI_SERVER_RUNNING ||
56 m->server->state == AVAHI_SERVER_REGISTERING)) {
58 /* Fill the entry group */
60 a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
62 if (!a->entry_group) /* OOM */
65 if (avahi_s_entry_group_is_empty(a->entry_group)) {
67 avahi_address_snprint(t, sizeof(t), &a->address);
69 avahi_log_info("Registering new address %s on %s.", t, a->interface->hardware->name);
71 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
72 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
73 avahi_s_entry_group_free(a->entry_group);
74 a->entry_group = NULL;
78 avahi_s_entry_group_commit(a->entry_group);
82 /* Clear the entry group */
84 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
86 avahi_address_snprint(t, sizeof(t), &a->address);
88 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
89 m->server->state == AVAHI_SERVER_REGISTERING)
90 avahi_server_decrease_host_rr_pending(m->server);
92 avahi_log_info("Withdrawing address %s on %s.", t, a->interface->hardware->name);
94 avahi_s_entry_group_reset(a->entry_group);
99 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
100 AvahiInterfaceAddress *a;
104 for (a = i->addresses; a; a = a->address_next)
105 avahi_interface_address_update_rrs(a, remove_rrs);
108 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
110 AvahiInterfaceMonitor *m;
115 for (i = hw->interfaces; i; i = i->by_hardware_next)
116 avahi_interface_update_rrs(i, remove_rrs);
118 if (m->list_complete &&
120 m->server->config.publish_workstation &&
121 (m->server->state == AVAHI_SERVER_RUNNING ||
122 m->server->state == AVAHI_SERVER_REGISTERING)) {
124 if (!hw->entry_group)
125 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
127 if (!hw->entry_group)
130 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
131 char name[AVAHI_LABEL_MAX];
134 if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size)))
137 snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, t);
143 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
144 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
145 avahi_s_entry_group_free(hw->entry_group);
146 hw->entry_group = NULL;
148 avahi_s_entry_group_commit(hw->entry_group);
153 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
155 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
156 avahi_server_decrease_host_rr_pending(m->server);
158 avahi_s_entry_group_reset(hw->entry_group);
163 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
164 AvahiHwInterface *hw;
168 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
169 avahi_hw_interface_update_rrs(hw, remove_rrs);
172 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
174 assert(a->interface);
176 avahi_interface_address_update_rrs(a, 1);
177 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
180 avahi_s_entry_group_free(a->entry_group);
185 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
188 /* Handle goodbyes and remove announcers */
189 avahi_goodbye_interface(i->monitor->server, i, send_goodbye);
190 avahi_response_scheduler_force(i->response_scheduler);
191 assert(!i->announcements);
193 /* Remove queriers */
194 avahi_querier_free_all(i);
195 avahi_hashmap_free(i->queriers_by_key);
197 /* Remove local RRs */
198 avahi_interface_update_rrs(i, 1);
201 avahi_interface_address_free(i->addresses);
203 avahi_response_scheduler_free(i->response_scheduler);
204 avahi_query_scheduler_free(i->query_scheduler);
205 avahi_probe_scheduler_free(i->probe_scheduler);
206 avahi_cache_free(i->cache);
208 AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
209 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
214 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
217 avahi_hw_interface_update_rrs(hw, 1);
219 while (hw->interfaces)
220 avahi_interface_free(hw->interfaces, send_goodbye);
223 avahi_s_entry_group_free(hw->entry_group);
225 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
226 avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
228 avahi_free(hw->name);
232 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
237 assert(AVAHI_PROTO_VALID(protocol));
239 if (!(i = avahi_new(AvahiInterface, 1)))
244 i->protocol = protocol;
247 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
248 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
250 AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
251 i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
253 i->cache = avahi_cache_new(m->server, i);
254 i->response_scheduler = avahi_response_scheduler_new(i);
255 i->query_scheduler = avahi_query_scheduler_new(i);
256 i->probe_scheduler = avahi_probe_scheduler_new(i);
258 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
261 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
262 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
270 avahi_cache_free(i->cache);
271 if (i->response_scheduler)
272 avahi_response_scheduler_free(i->response_scheduler);
273 if (i->query_scheduler)
274 avahi_query_scheduler_free(i->query_scheduler);
275 if (i->probe_scheduler)
276 avahi_probe_scheduler_free(i->probe_scheduler);
282 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
283 AvahiHwInterface *hw;
286 assert(AVAHI_IF_VALID(idx));
288 if (!(hw = avahi_new(AvahiHwInterface, 1)))
296 hw->mac_address_size = 0;
297 hw->entry_group = NULL;
299 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
300 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
302 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
304 if (m->server->fd_ipv4 >= 0)
305 avahi_interface_new(m, hw, AVAHI_PROTO_INET);
306 if (m->server->fd_ipv6 >= 0)
307 avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
312 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
313 AvahiInterfaceAddress *a;
318 if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
324 a->prefix_len = prefix_len;
326 a->entry_group = NULL;
328 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
333 void avahi_interface_check_relevant(AvahiInterface *i) {
335 AvahiInterfaceMonitor *m;
340 b = avahi_interface_is_relevant(i);
342 if (m->list_complete && b && !i->announcing) {
343 avahi_log_info("New relevant interface %s.%s.", i->hardware->name, avahi_proto_to_string(i->protocol));
345 if (i->protocol == AVAHI_PROTO_INET)
346 avahi_mdns_mcast_join_ipv4(m->server->fd_ipv4, i->hardware->index);
347 if (i->protocol == AVAHI_PROTO_INET6)
348 avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index);
351 avahi_announce_interface(m->server, i);
352 avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
353 } else if (!b && i->announcing) {
354 avahi_log_info("Interface %s.%s no longer relevant.", i->hardware->name, avahi_proto_to_string(i->protocol));
356 if (i->protocol == AVAHI_PROTO_INET)
357 avahi_mdns_mcast_leave_ipv4(m->server->fd_ipv4, i->hardware->index);
358 if (i->protocol == AVAHI_PROTO_INET6)
359 avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index);
361 avahi_goodbye_interface(m->server, i, 0);
362 avahi_querier_free_all(i);
364 avahi_response_scheduler_clear(i->response_scheduler);
365 avahi_query_scheduler_clear(i->query_scheduler);
366 avahi_probe_scheduler_clear(i->probe_scheduler);
367 avahi_cache_flush(i->cache);
373 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
378 for (i = hw->interfaces; i; i = i->by_hardware_next)
379 avahi_interface_check_relevant(i);
382 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
387 for (i = m->interfaces; i; i = i->interface_next)
388 avahi_interface_check_relevant(i);
391 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
392 AvahiInterfaceMonitor *m = NULL;
394 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
395 return NULL; /* OOM */
398 m->list_complete = 0;
399 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
401 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
402 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
404 if (avahi_interface_monitor_init_osdep(m) < 0)
410 avahi_interface_monitor_free(m);
414 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
417 while (m->hw_interfaces)
418 avahi_hw_interface_free(m->hw_interfaces, 1);
420 assert(!m->interfaces);
422 avahi_interface_monitor_free_osdep(m);
425 avahi_hashmap_free(m->hashmap);
431 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
432 AvahiHwInterface *hw;
437 assert(protocol != AVAHI_PROTO_UNSPEC);
439 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
442 for (i = hw->interfaces; i; i = i->by_hardware_next)
443 if (i->protocol == protocol)
449 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
453 return avahi_hashmap_lookup(m->hashmap, &idx);
456 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
457 AvahiInterfaceAddress *ia;
463 for (ia = i->addresses; ia; ia = ia->address_next)
464 if (avahi_address_cmp(&ia->address, raddr) == 0)
471 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
476 if (!avahi_interface_is_relevant(i))
479 assert(!a || a->proto == i->protocol);
482 /* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
484 /* avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
486 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
487 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
488 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
489 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
492 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
496 avahi_interface_send_packet_unicast(i, p, NULL, 0);
499 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) {
503 if (avahi_interface_is_relevant(i))
504 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
509 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
513 if (avahi_interface_is_relevant(i))
514 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
519 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
523 if (avahi_interface_is_relevant(i))
524 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
529 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
533 for (i = m->interfaces; i; i = i->interface_next) {
534 if (avahi_interface_is_relevant(i)) {
536 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
537 callback(ln, userdata);
538 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
546 int avahi_interface_is_relevant(AvahiInterface *i) {
547 AvahiInterfaceAddress *a;
548 int relevant_address;
552 relevant_address = 0;
554 for (a = i->addresses; a; a = a->address_next)
555 if (avahi_interface_address_is_relevant(a)) {
556 relevant_address = 1;
560 return i->hardware->flags_ok && relevant_address;
563 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
564 AvahiInterfaceAddress *b;
567 /* Publish public IP addresses */
572 /* Publish link local IP addresses if they are the only ones on the link */
573 for (b = a->interface->addresses; b; b = b->address_next) {
587 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
590 if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
593 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
599 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
603 if (interface != AVAHI_IF_UNSPEC) {
604 if (protocol != AVAHI_PROTO_UNSPEC) {
607 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
608 callback(m, i, userdata);
611 AvahiHwInterface *hw;
614 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
615 for (i = hw->interfaces; i; i = i->by_hardware_next)
616 if (avahi_interface_match(i, interface, protocol))
617 callback(m, i, userdata);
623 for (i = m->interfaces; i; i = i->interface_next)
624 if (avahi_interface_match(i, interface, protocol))
625 callback(m, i, userdata);
630 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
632 AvahiInterfaceAddress *ia;
636 for (i = m->interfaces; i; i = i->interface_next)
637 for (ia = i->addresses; ia; ia = ia->address_next)
638 if (avahi_address_cmp(a, &ia->address) == 0)
644 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
645 AvahiInterfaceAddress *ia;
650 if (a->proto != i->protocol)
653 for (ia = i->addresses; ia; ia = ia->address_next) {
655 if (a->proto == AVAHI_PROTO_INET) {
658 m = ~(((uint32_t) -1) >> ia->prefix_len);
660 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
665 assert(a->proto == AVAHI_PROTO_INET6);
669 for (j = 0; j < 16; j++) {
683 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
692 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
694 AvahiInterfaceAddress *j;
697 assert(iface != AVAHI_IF_UNSPEC);
700 if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
703 for (j = i->addresses; j; j = j->address_next)
704 if (avahi_address_cmp(a, &j->address) == 0)