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>
28 #include <asm/types.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
40 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove) {
44 if (avahi_interface_address_relevant(a) &&
46 m->server->config.register_addresses &&
47 (m->server->state == AVAHI_SERVER_RUNNING ||
48 m->server->state == AVAHI_SERVER_REGISTERING)) {
50 if (!a->entry_group) {
51 a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
52 avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address);
53 avahi_entry_group_commit(a->entry_group);
59 if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
60 avahi_server_decrease_host_rr_pending(m->server);
62 avahi_entry_group_free(a->entry_group);
63 a->entry_group = NULL;
68 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove) {
69 AvahiInterfaceAddress *a;
74 for (a = i->addresses; a; a = a->address_next)
75 update_address_rr(m, a, remove);
78 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove) {
84 for (i = hw->interfaces; i; i = i->by_hardware_next)
85 update_interface_rr(m, i, remove);
88 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
91 g_assert(a->interface);
93 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
96 avahi_entry_group_free(a->entry_group);
101 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
105 avahi_goodbye_interface(m->server, i, send_goodbye);
106 avahi_response_scheduler_force(i->response_scheduler);
108 g_assert(!i->announcements);
111 free_address(m, i->addresses);
113 avahi_response_scheduler_free(i->response_scheduler);
114 avahi_query_scheduler_free(i->query_scheduler);
115 avahi_probe_scheduler_free(i->probe_scheduler);
116 avahi_cache_free(i->cache);
118 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
119 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
124 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
128 while (hw->interfaces)
129 free_interface(m, hw->interfaces, send_goodbye);
131 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
132 g_hash_table_remove(m->hash_table, &hw->index);
138 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
139 AvahiInterfaceAddress *ia;
145 for (ia = i->addresses; ia; ia = ia->address_next)
146 if (avahi_address_cmp(&ia->address, raddr) == 0)
152 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
154 struct rtgenmsg *gen;
157 memset(&req, 0, sizeof(req));
158 n = (struct nlmsghdr*) req;
159 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
160 n->nlmsg_type = type;
161 n->nlmsg_flags = NLM_F_ROOT/*|NLM_F_MATCH*/|NLM_F_REQUEST;
165 memset(gen, 0, sizeof(struct rtgenmsg));
166 gen->rtgen_family = AF_UNSPEC;
168 return avahi_netlink_send(nl, n, ret_seq);
171 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
176 g_assert(protocol != AF_UNSPEC);
178 i = g_new(AvahiInterface, 1);
181 i->protocol = protocol;
182 i->announcing = FALSE;
184 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
185 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
187 i->cache = avahi_cache_new(m->server, i);
188 i->response_scheduler = avahi_response_scheduler_new(i);
189 i->query_scheduler = avahi_query_scheduler_new(i);
190 i->probe_scheduler = avahi_probe_scheduler_new(i);
192 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
193 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
196 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
202 b = avahi_interface_relevant(i);
204 if (b && !i->announcing) {
205 g_message("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index);
207 if (i->protocol == AF_INET)
208 avahi_mdns_mcast_join_ipv4(i->hardware->index, m->server->fd_ipv4);
209 if (i->protocol == AF_INET6)
210 avahi_mdns_mcast_join_ipv6(i->hardware->index, m->server->fd_ipv6);
212 i->announcing = TRUE;
213 avahi_announce_interface(m->server, i);
214 } else if (!b && i->announcing) {
215 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
217 if (i->protocol == AF_INET)
218 avahi_mdns_mcast_leave_ipv4(i->hardware->index, m->server->fd_ipv4);
219 if (i->protocol == AF_INET6)
220 avahi_mdns_mcast_leave_ipv6(i->hardware->index, m->server->fd_ipv6);
222 avahi_goodbye_interface(m->server, i, FALSE);
223 avahi_response_scheduler_clear(i->response_scheduler);
224 avahi_query_scheduler_clear(i->query_scheduler);
225 avahi_probe_scheduler_clear(i->probe_scheduler);
226 avahi_cache_flush(i->cache);
228 i->announcing = FALSE;
232 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
238 for (i = hw->interfaces; i; i = i->by_hardware_next)
239 check_interface_relevant(m, i);
242 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
243 AvahiInterfaceMonitor *m = userdata;
247 g_assert(m->netlink == nl);
249 if (n->nlmsg_type == RTM_NEWLINK) {
250 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
251 AvahiHwInterface *hw;
252 struct rtattr *a = NULL;
255 if (ifinfomsg->ifi_family != AF_UNSPEC)
258 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
259 hw = g_new(AvahiHwInterface, 1);
264 hw->index = ifinfomsg->ifi_index;
266 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
267 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
269 g_hash_table_insert(m->hash_table, &hw->index, hw);
271 if (m->server->fd_ipv4 >= 0)
272 new_interface(m, hw, AF_INET);
273 if (m->server->fd_ipv6 >= 0)
274 new_interface(m, hw, AF_INET6);
277 hw->flags = ifinfomsg->ifi_flags;
279 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
280 a = IFLA_RTA(ifinfomsg);
282 while (RTA_OK(a, l)) {
283 switch(a->rta_type) {
286 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
290 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
291 hw->mtu = *((unsigned int*) RTA_DATA(a));
301 update_hw_interface_rr(m, hw, FALSE);
302 check_hw_interface_relevant(m, hw);
304 } else if (n->nlmsg_type == RTM_DELLINK) {
305 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
306 AvahiHwInterface *hw;
308 if (ifinfomsg->ifi_family != AF_UNSPEC)
311 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
314 update_hw_interface_rr(m, hw, TRUE);
315 free_hw_interface(m, hw, FALSE);
317 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
319 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
321 struct rtattr *a = NULL;
324 gboolean raddr_valid = FALSE;
326 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
329 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
332 raddr.family = ifaddrmsg->ifa_family;
334 l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg));
335 a = IFA_RTA(ifaddrmsg);
337 while (RTA_OK(a, l)) {
339 switch(a->rta_type) {
341 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
342 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
345 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
360 if (n->nlmsg_type == RTM_NEWADDR) {
361 AvahiInterfaceAddress *addr;
363 if (!(addr = get_address(m, i, &raddr))) {
364 addr = g_new(AvahiInterfaceAddress, 1);
366 addr->address = raddr;
368 addr->entry_group = NULL;
370 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
373 addr->flags = ifaddrmsg->ifa_flags;
374 addr->scope = ifaddrmsg->ifa_scope;
376 update_address_rr(m, addr, FALSE);
377 check_interface_relevant(m, i);
379 AvahiInterfaceAddress *addr;
381 if (!(addr = get_address(m, i, &raddr)))
384 update_address_rr(m, addr, TRUE);
385 free_address(m, addr);
387 check_interface_relevant(m, i);
390 } else if (n->nlmsg_type == NLMSG_DONE) {
392 if (m->list == LIST_IFACE) {
395 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
396 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
401 g_message("Enumeration complete");
404 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
405 struct nlmsgerr *e = NLMSG_DATA (n);
408 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
412 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
413 AvahiInterfaceMonitor *m = NULL;
415 m = g_new0(AvahiInterfaceMonitor, 1);
417 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
420 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
422 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
423 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
425 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
428 m->list = LIST_IFACE;
433 avahi_interface_monitor_free(m);
437 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
440 while (m->list != LIST_DONE) {
441 if (!avahi_netlink_work(m->netlink, TRUE))
446 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
449 while (m->hw_interfaces)
450 free_hw_interface(m, m->hw_interfaces, TRUE);
452 g_assert(!m->interfaces);
456 avahi_netlink_free(m->netlink);
459 g_hash_table_destroy(m->hash_table);
465 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
466 AvahiHwInterface *hw;
471 g_assert(protocol != AF_UNSPEC);
473 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
476 for (i = hw->interfaces; i; i = i->by_hardware_next)
477 if (i->protocol == protocol)
483 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
487 return g_hash_table_lookup(m->hash_table, &index);
491 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
496 if (!avahi_interface_relevant(i))
499 g_assert(!a || a->family == i->protocol);
502 /* g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
504 /* g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
506 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
507 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
508 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
509 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
512 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
516 avahi_interface_send_packet_unicast(i, p, NULL, 0);
519 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
523 if (avahi_interface_relevant(i))
524 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
529 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
533 if (avahi_interface_relevant(i))
534 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
539 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
543 if (avahi_interface_relevant(i))
544 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
549 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
553 for (i = m->interfaces; i; i = i->interface_next) {
554 if (avahi_interface_relevant(i)) {
555 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
556 avahi_cache_dump(i->cache, f);
562 gboolean avahi_interface_relevant(AvahiInterface *i) {
563 AvahiInterfaceAddress *a;
564 gboolean relevant_address;
568 relevant_address = FALSE;
570 for (a = i->addresses; a; a = a->address_next)
571 if (avahi_interface_address_relevant(a)) {
572 relevant_address = TRUE;
576 /* g_message("%p. iface-relevant: %i %i %i %i %i %i", i, relevant_address, */
577 /* (i->hardware->flags & IFF_UP), */
578 /* (i->hardware->flags & IFF_RUNNING), */
579 /* !(i->hardware->flags & IFF_LOOPBACK), */
580 /* (i->hardware->flags & IFF_MULTICAST), */
581 /* !(i->hardware->flags & IFF_POINTOPOINT)); */
584 (i->hardware->flags & IFF_UP) &&
585 (!i->monitor->server->config.use_iff_running || (i->hardware->flags & IFF_RUNNING)) &&
586 !(i->hardware->flags & IFF_LOOPBACK) &&
587 (i->hardware->flags & IFF_MULTICAST) &&
588 !(i->hardware->flags & IFF_POINTOPOINT) &&
592 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
595 return a->scope == RT_SCOPE_UNIVERSE;
599 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
602 if (index > 0 && index != i->hardware->index)
605 if (protocol != AF_UNSPEC && protocol != i->protocol)
611 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
616 if (protocol != AF_UNSPEC) {
619 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
620 callback(m, i, userdata);
623 AvahiHwInterface *hw;
626 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
627 for (i = hw->interfaces; i; i = i->by_hardware_next)
628 if (avahi_interface_match(i, interface, protocol))
629 callback(m, i, userdata);
635 for (i = m->interfaces; i; i = i->interface_next)
636 if (avahi_interface_match(i, interface, protocol))
637 callback(m, i, userdata);
641 void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove) {
646 for (i = m->interfaces; i; i = i->interface_next)
647 update_interface_rr(m, i, remove);