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, int remove) {
44 if (!avahi_interface_address_relevant(a) || remove) {
46 avahi_entry_group_free(a->entry_group);
47 a->entry_group = NULL;
50 if (!a->entry_group) {
51 a->entry_group = avahi_entry_group_new(m->server, NULL, NULL);
52 avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address);
53 avahi_entry_group_commit(a->entry_group);
58 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove) {
59 AvahiInterfaceAddress *a;
63 for (a = i->addresses; a; a = a->address_next)
64 update_address_rr(m, a, remove);
67 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove) {
73 for (i = hw->interfaces; i; i = i->by_hardware_next)
74 update_interface_rr(m, i, remove);
77 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
80 g_assert(a->interface);
82 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
85 avahi_entry_group_free(a->entry_group);
90 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
94 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
95 avahi_goodbye_interface(m->server, i, send_goodbye);
96 g_message("flushing...");
97 avahi_packet_scheduler_flush_responses(i->scheduler);
100 g_assert(!i->announcements);
103 free_address(m, i->addresses);
105 avahi_packet_scheduler_free(i->scheduler);
106 avahi_cache_free(i->cache);
108 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
109 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
114 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
118 while (hw->interfaces)
119 free_interface(m, hw->interfaces, send_goodbye);
121 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
122 g_hash_table_remove(m->hash_table, &hw->index);
128 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
129 AvahiInterfaceAddress *ia;
135 for (ia = i->addresses; ia; ia = ia->address_next)
136 if (avahi_address_cmp(&ia->address, raddr) == 0)
142 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
144 struct rtgenmsg *gen;
147 memset(&req, 0, sizeof(req));
148 n = (struct nlmsghdr*) req;
149 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
150 n->nlmsg_type = type;
151 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
155 memset(gen, 0, sizeof(struct rtgenmsg));
156 gen->rtgen_family = AF_UNSPEC;
158 return avahi_netlink_send(nl, n, ret_seq);
161 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
166 g_assert(protocol != AF_UNSPEC);
168 i = g_new(AvahiInterface, 1);
171 i->protocol = protocol;
172 i->announcing = FALSE;
174 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
175 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
177 i->cache = avahi_cache_new(m->server, i);
178 i->scheduler = avahi_packet_scheduler_new(m->server, i);
180 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
181 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
184 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
190 b = avahi_interface_relevant(i);
192 if (b && !i->announcing) {
193 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
195 if (i->protocol == AF_INET)
196 avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
197 if (i->protocol == AF_INET6)
198 avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
200 i->announcing = TRUE;
201 avahi_announce_interface(m->server, i);
202 } else if (!b && i->announcing) {
203 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
205 if (i->protocol == AF_INET)
206 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
207 if (i->protocol == AF_INET6)
208 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
210 avahi_goodbye_interface(m->server, i, FALSE);
211 avahi_cache_flush(i->cache);
213 i->announcing = FALSE;
217 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
223 for (i = hw->interfaces; i; i = i->by_hardware_next)
224 check_interface_relevant(m, i);
227 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
228 AvahiInterfaceMonitor *m = userdata;
232 g_assert(m->netlink == nl);
234 if (n->nlmsg_type == RTM_NEWLINK) {
235 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
236 AvahiHwInterface *hw;
237 struct rtattr *a = NULL;
240 if (ifinfomsg->ifi_family != AF_UNSPEC)
243 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
244 hw = g_new(AvahiHwInterface, 1);
249 hw->index = ifinfomsg->ifi_index;
251 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
252 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
254 g_hash_table_insert(m->hash_table, &hw->index, hw);
256 if (m->server->fd_ipv4 >= 0)
257 new_interface(m, hw, AF_INET);
258 if (m->server->fd_ipv6 >= 0)
259 new_interface(m, hw, AF_INET6);
262 hw->flags = ifinfomsg->ifi_flags;
264 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
265 a = IFLA_RTA(ifinfomsg);
267 while (RTA_OK(a, l)) {
268 switch(a->rta_type) {
271 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
275 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
276 hw->mtu = *((unsigned int*) RTA_DATA(a));
286 update_hw_interface_rr(m, hw, FALSE);
287 check_hw_interface_relevant(m, hw);
289 } else if (n->nlmsg_type == RTM_DELLINK) {
290 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
291 AvahiHwInterface *hw;
293 if (ifinfomsg->ifi_family != AF_UNSPEC)
296 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
299 update_hw_interface_rr(m, hw, TRUE);
300 free_hw_interface(m, hw, FALSE);
302 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
304 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
306 struct rtattr *a = NULL;
311 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
314 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
317 raddr.family = ifaddrmsg->ifa_family;
319 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
320 a = IFA_RTA(ifaddrmsg);
322 while (RTA_OK(a, l)) {
323 switch(a->rta_type) {
325 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
326 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
329 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
345 if (n->nlmsg_type == RTM_NEWADDR) {
346 AvahiInterfaceAddress *addr;
348 if (!(addr = get_address(m, i, &raddr))) {
349 addr = g_new(AvahiInterfaceAddress, 1);
351 addr->address = raddr;
353 addr->entry_group = NULL;
355 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
358 addr->flags = ifaddrmsg->ifa_flags;
359 addr->scope = ifaddrmsg->ifa_scope;
361 update_address_rr(m, addr, FALSE);
362 check_interface_relevant(m, i);
364 AvahiInterfaceAddress *addr;
366 if (!(addr = get_address(m, i, &raddr)))
369 update_address_rr(m, addr, TRUE);
370 free_address(m, addr);
372 check_interface_relevant(m, i);
375 } else if (n->nlmsg_type == NLMSG_DONE) {
377 if (m->list == LIST_IFACE) {
380 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
381 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
386 g_message("Enumeration complete");
389 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
390 struct nlmsgerr *e = NLMSG_DATA (n);
393 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
397 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
398 AvahiInterfaceMonitor *m = NULL;
400 m = g_new0(AvahiInterfaceMonitor, 1);
402 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
405 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
407 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
408 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
410 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
413 m->list = LIST_IFACE;
418 avahi_interface_monitor_free(m);
422 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
425 while (m->list != LIST_DONE) {
426 if (!avahi_netlink_work(m->netlink, TRUE))
431 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
434 while (m->hw_interfaces)
435 free_hw_interface(m, m->hw_interfaces, TRUE);
437 g_assert(!m->interfaces);
441 avahi_netlink_free(m->netlink);
444 g_hash_table_destroy(m->hash_table);
450 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
451 AvahiHwInterface *hw;
456 g_assert(protocol != AF_UNSPEC);
458 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
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, gint index) {
472 return g_hash_table_lookup(m->hash_table, &index);
476 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
481 if (!avahi_interface_relevant(i))
484 g_assert(!a || a->family == i->protocol);
487 g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port);
489 g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol);
491 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
492 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
493 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
494 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
497 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
501 avahi_interface_send_packet_unicast(i, p, NULL, 0);
504 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
508 if (avahi_interface_relevant(i))
509 return avahi_packet_scheduler_post_query(i->scheduler, key, immediately);
514 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, gboolean immediately, const AvahiAddress *querier) {
518 if (avahi_interface_relevant(i))
519 return avahi_packet_scheduler_post_response(i->scheduler, record, flush_cache, immediately, querier);
524 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
528 if (avahi_interface_relevant(i))
529 return avahi_packet_scheduler_post_probe(i->scheduler, record, immediately);
534 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
538 for (i = m->interfaces; i; i = i->interface_next) {
539 if (avahi_interface_relevant(i)) {
540 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
541 avahi_cache_dump(i->cache, f);
547 gboolean avahi_interface_relevant(AvahiInterface *i) {
551 (i->hardware->flags & IFF_UP) &&
552 (i->hardware->flags & IFF_RUNNING) &&
553 !(i->hardware->flags & IFF_LOOPBACK) &&
554 (i->hardware->flags & IFF_MULTICAST) &&
558 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
561 return a->scope == RT_SCOPE_UNIVERSE;
565 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
568 if (index > 0 && index != i->hardware->index)
571 if (protocol != AF_UNSPEC && protocol != i->protocol)
578 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
583 if (protocol != AF_UNSPEC) {
586 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
587 callback(m, i, userdata);
590 AvahiHwInterface *hw;
593 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
594 for (i = hw->interfaces; i; i = i->by_hardware_next)
595 if (avahi_interface_match(i, interface, protocol))
596 callback(m, i, userdata);
602 for (i = m->interfaces; i; i = i->interface_next)
603 if (avahi_interface_match(i, interface, protocol))
604 callback(m, i, userdata);