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
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
44 #include "multicast-lookup.h"
47 void avahi_interface_address_update_rrs(AvahiInterfaceAddress *a, int remove_rrs) {
48 AvahiInterfaceMonitor *m;
53 if (a->interface->announcing &&
55 avahi_interface_address_is_relevant(a) &&
57 m->server->config.publish_addresses &&
58 (m->server->state == AVAHI_SERVER_RUNNING ||
59 m->server->state == AVAHI_SERVER_REGISTERING)) {
61 /* Fill the entry group */
63 a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
65 if (!a->entry_group) /* OOM */
68 if (avahi_s_entry_group_is_empty(a->entry_group)) {
69 char t[AVAHI_ADDRESS_STR_MAX];
70 avahi_address_snprint(t, sizeof(t), &a->address);
72 avahi_log_info("Registering new address %s on %s.", t, a->interface->hardware->name);
74 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
75 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
76 avahi_s_entry_group_free(a->entry_group);
77 a->entry_group = NULL;
81 avahi_s_entry_group_commit(a->entry_group);
85 /* Clear the entry group */
87 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
88 char t[AVAHI_ADDRESS_STR_MAX];
89 avahi_address_snprint(t, sizeof(t), &a->address);
91 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
92 m->server->state == AVAHI_SERVER_REGISTERING)
93 avahi_server_decrease_host_rr_pending(m->server);
95 avahi_log_info("Withdrawing address %s on %s.", t, a->interface->hardware->name);
97 avahi_s_entry_group_reset(a->entry_group);
102 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
103 AvahiInterfaceAddress *a;
107 for (a = i->addresses; a; a = a->address_next)
108 avahi_interface_address_update_rrs(a, remove_rrs);
111 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
113 AvahiInterfaceMonitor *m;
118 for (i = hw->interfaces; i; i = i->by_hardware_next)
119 avahi_interface_update_rrs(i, remove_rrs);
121 if (m->list_complete &&
123 m->server->config.publish_workstation &&
124 (m->server->state == AVAHI_SERVER_RUNNING ||
125 m->server->state == AVAHI_SERVER_REGISTERING)) {
127 if (!hw->entry_group)
128 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
130 if (!hw->entry_group)
133 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
134 char name[AVAHI_LABEL_MAX];
137 if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size)))
140 snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, t);
146 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
147 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
148 avahi_s_entry_group_free(hw->entry_group);
149 hw->entry_group = NULL;
151 avahi_s_entry_group_commit(hw->entry_group);
156 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
158 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
159 avahi_server_decrease_host_rr_pending(m->server);
161 avahi_s_entry_group_reset(hw->entry_group);
166 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
167 AvahiHwInterface *hw;
171 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
172 avahi_hw_interface_update_rrs(hw, remove_rrs);
175 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
177 assert(a->interface);
179 avahi_interface_address_update_rrs(a, 1);
180 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
183 avahi_s_entry_group_free(a->entry_group);
188 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
191 /* Handle goodbyes and remove announcers */
192 avahi_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
193 avahi_response_scheduler_force(i->response_scheduler);
194 assert(!i->announcers);
196 /* Remove queriers */
197 avahi_querier_free_all(i);
198 avahi_hashmap_free(i->queriers_by_key);
200 /* Remove local RRs */
201 avahi_interface_update_rrs(i, 1);
204 avahi_interface_address_free(i->addresses);
206 avahi_response_scheduler_free(i->response_scheduler);
207 avahi_query_scheduler_free(i->query_scheduler);
208 avahi_probe_scheduler_free(i->probe_scheduler);
209 avahi_cache_free(i->cache);
211 AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
212 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
217 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
220 avahi_hw_interface_update_rrs(hw, 1);
222 while (hw->interfaces)
223 avahi_interface_free(hw->interfaces, send_goodbye);
226 avahi_s_entry_group_free(hw->entry_group);
228 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
229 avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
231 avahi_free(hw->name);
235 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
240 assert(AVAHI_PROTO_VALID(protocol));
242 if (!(i = avahi_new(AvahiInterface, 1)))
247 i->protocol = protocol;
250 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
251 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, i->announcers);
253 AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
254 i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
256 i->cache = avahi_cache_new(m->server, i);
257 i->response_scheduler = avahi_response_scheduler_new(i);
258 i->query_scheduler = avahi_query_scheduler_new(i);
259 i->probe_scheduler = avahi_probe_scheduler_new(i);
261 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
264 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
265 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
273 avahi_cache_free(i->cache);
274 if (i->response_scheduler)
275 avahi_response_scheduler_free(i->response_scheduler);
276 if (i->query_scheduler)
277 avahi_query_scheduler_free(i->query_scheduler);
278 if (i->probe_scheduler)
279 avahi_probe_scheduler_free(i->probe_scheduler);
285 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
286 AvahiHwInterface *hw;
289 assert(AVAHI_IF_VALID(idx));
291 if (!(hw = avahi_new(AvahiHwInterface, 1)))
299 hw->mac_address_size = 0;
300 hw->entry_group = NULL;
302 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
303 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
305 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
307 if (m->server->fd_ipv4 >= 0)
308 avahi_interface_new(m, hw, AVAHI_PROTO_INET);
309 if (m->server->fd_ipv6 >= 0)
310 avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
315 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
316 AvahiInterfaceAddress *a;
321 if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
327 a->prefix_len = prefix_len;
329 a->entry_group = NULL;
331 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
336 static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
338 if (i->protocol == AVAHI_PROTO_INET6)
339 return avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, join);
340 else if (i->protocol == AVAHI_PROTO_INET) {
342 #ifdef HAVE_STRUCT_IP_MREQN
343 return avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, join);
346 AvahiInterfaceAddress *ia;
349 for (ia = i->addresses; ia; ia = ia->address_next)
350 r |= avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &ia->address, join);
360 void avahi_interface_check_relevant(AvahiInterface *i) {
362 AvahiInterfaceMonitor *m;
367 b = avahi_interface_is_relevant(i);
369 if (m->list_complete && b && !i->announcing) {
370 avahi_log_info("New relevant interface %s.%s.", i->hardware->name, avahi_proto_to_string(i->protocol));
372 interface_mdns_mcast_join(i, 1);
375 avahi_announce_interface(m->server, i);
376 avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
377 } else if (!b && i->announcing) {
378 avahi_log_info("Interface %s.%s no longer relevant.", i->hardware->name, avahi_proto_to_string(i->protocol));
380 interface_mdns_mcast_join(i, 0);
382 avahi_goodbye_interface(m->server, i, 0, 1);
383 avahi_querier_free_all(i);
385 avahi_response_scheduler_clear(i->response_scheduler);
386 avahi_query_scheduler_clear(i->query_scheduler);
387 avahi_probe_scheduler_clear(i->probe_scheduler);
388 avahi_cache_flush(i->cache);
394 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
399 for (i = hw->interfaces; i; i = i->by_hardware_next)
400 avahi_interface_check_relevant(i);
403 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
408 for (i = m->interfaces; i; i = i->interface_next)
409 avahi_interface_check_relevant(i);
412 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
413 AvahiInterfaceMonitor *m = NULL;
415 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
416 return NULL; /* OOM */
419 m->list_complete = 0;
420 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
422 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
423 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
425 if (avahi_interface_monitor_init_osdep(m) < 0)
431 avahi_interface_monitor_free(m);
435 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
438 while (m->hw_interfaces)
439 avahi_hw_interface_free(m->hw_interfaces, 1);
441 assert(!m->interfaces);
443 avahi_interface_monitor_free_osdep(m);
446 avahi_hashmap_free(m->hashmap);
452 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
453 AvahiHwInterface *hw;
458 assert(protocol != AVAHI_PROTO_UNSPEC);
460 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
463 for (i = hw->interfaces; i; i = i->by_hardware_next)
464 if (i->protocol == protocol)
470 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
474 return avahi_hashmap_lookup(m->hashmap, &idx);
477 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
478 AvahiInterfaceAddress *ia;
484 for (ia = i->addresses; ia; ia = ia->address_next)
485 if (avahi_address_cmp(&ia->address, raddr) == 0)
492 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
495 /* char t[AVAHI_ADDRESS_STR_MAX]; */
497 if (!avahi_interface_is_relevant(i))
500 assert(!a || a->proto == i->protocol);
503 /* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
505 /* avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
507 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
508 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
509 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
510 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
513 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
517 avahi_interface_send_packet_unicast(i, p, NULL, 0);
520 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) {
524 if (avahi_interface_is_relevant(i))
525 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
530 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
534 if (avahi_interface_is_relevant(i))
535 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
540 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
544 if (avahi_interface_is_relevant(i))
545 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
550 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
554 for (i = m->interfaces; i; i = i->interface_next) {
555 if (avahi_interface_is_relevant(i)) {
557 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
558 callback(ln, userdata);
559 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
567 int avahi_interface_is_relevant(AvahiInterface *i) {
568 AvahiInterfaceAddress *a;
569 int relevant_address;
573 relevant_address = 0;
575 for (a = i->addresses; a; a = a->address_next)
576 if (avahi_interface_address_is_relevant(a)) {
577 relevant_address = 1;
581 return i->hardware->flags_ok && relevant_address;
584 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
585 AvahiInterfaceAddress *b;
588 /* Publish public IP addresses */
593 /* Publish link local IP addresses if they are the only ones on the link */
594 for (b = a->interface->addresses; b; b = b->address_next) {
608 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
611 if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
614 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
620 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
624 if (interface != AVAHI_IF_UNSPEC) {
625 if (protocol != AVAHI_PROTO_UNSPEC) {
628 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
629 callback(m, i, userdata);
632 AvahiHwInterface *hw;
635 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
636 for (i = hw->interfaces; i; i = i->by_hardware_next)
637 if (avahi_interface_match(i, interface, protocol))
638 callback(m, i, userdata);
644 for (i = m->interfaces; i; i = i->interface_next)
645 if (avahi_interface_match(i, interface, protocol))
646 callback(m, i, userdata);
651 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
653 AvahiInterfaceAddress *ia;
657 for (i = m->interfaces; i; i = i->interface_next)
658 for (ia = i->addresses; ia; ia = ia->address_next)
659 if (avahi_address_cmp(a, &ia->address) == 0)
665 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
666 AvahiInterfaceAddress *ia;
671 if (a->proto != i->protocol)
674 for (ia = i->addresses; ia; ia = ia->address_next) {
676 if (a->proto == AVAHI_PROTO_INET) {
679 m = ~(((uint32_t) -1) >> ia->prefix_len);
681 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
686 assert(a->proto == AVAHI_PROTO_INET6);
690 for (j = 0; j < 16; j++) {
704 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
713 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
715 AvahiInterfaceAddress *j;
718 assert(iface != AVAHI_IF_UNSPEC);
721 if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
724 for (j = i->addresses; j; j = j->address_next)
725 if (avahi_address_cmp(a, &j->address) == 0)