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
23 #include <sys/socket.h>
24 #include <asm/types.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
36 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove) {
40 if (!avahi_interface_address_relevant(a) || remove) {
42 avahi_entry_group_free(a->entry_group);
43 a->entry_group = NULL;
46 if (!a->entry_group) {
47 a->entry_group = avahi_entry_group_new(m->server, NULL, NULL);
48 avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address);
49 avahi_entry_group_commit(a->entry_group);
54 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove) {
55 AvahiInterfaceAddress *a;
59 for (a = i->addresses; a; a = a->address_next)
60 update_address_rr(m, a, remove);
63 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove) {
69 for (i = hw->interfaces; i; i = i->by_hardware_next)
70 update_interface_rr(m, i, remove);
73 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
76 g_assert(a->interface);
78 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
81 avahi_entry_group_free(a->entry_group);
86 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
90 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
91 avahi_goodbye_interface(m->server, i, send_goodbye);
92 g_message("flushing...");
93 avahi_packet_scheduler_flush_responses(i->scheduler);
96 g_assert(!i->announcements);
99 free_address(m, i->addresses);
101 avahi_packet_scheduler_free(i->scheduler);
102 avahi_cache_free(i->cache);
104 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
105 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
110 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
114 while (hw->interfaces)
115 free_interface(m, hw->interfaces, send_goodbye);
117 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
118 g_hash_table_remove(m->hash_table, &hw->index);
124 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
125 AvahiInterfaceAddress *ia;
131 for (ia = i->addresses; ia; ia = ia->address_next)
132 if (avahi_address_cmp(&ia->address, raddr) == 0)
138 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
140 struct rtgenmsg *gen;
143 memset(&req, 0, sizeof(req));
144 n = (struct nlmsghdr*) req;
145 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
146 n->nlmsg_type = type;
147 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
151 memset(gen, 0, sizeof(struct rtgenmsg));
152 gen->rtgen_family = AF_UNSPEC;
154 return avahi_netlink_send(nl, n, ret_seq);
157 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
162 g_assert(protocol != AF_UNSPEC);
164 i = g_new(AvahiInterface, 1);
167 i->protocol = protocol;
168 i->announcing = FALSE;
170 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
171 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
173 i->cache = avahi_cache_new(m->server, i);
174 i->scheduler = avahi_packet_scheduler_new(m->server, i);
176 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
177 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
180 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
186 b = avahi_interface_relevant(i);
188 if (b && !i->announcing) {
189 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
191 if (i->protocol == AF_INET)
192 avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
193 if (i->protocol == AF_INET6)
194 avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
196 i->announcing = TRUE;
197 avahi_announce_interface(m->server, i);
198 } else if (!b && i->announcing) {
199 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
201 avahi_goodbye_interface(m->server, i, FALSE);
203 if (i->protocol == AF_INET)
204 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
205 if (i->protocol == AF_INET6)
206 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
208 i->announcing = FALSE;
212 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
218 for (i = hw->interfaces; i; i = i->by_hardware_next)
219 check_interface_relevant(m, i);
222 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
223 AvahiInterfaceMonitor *m = userdata;
227 g_assert(m->netlink == nl);
229 if (n->nlmsg_type == RTM_NEWLINK) {
230 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
231 AvahiHwInterface *hw;
232 struct rtattr *a = NULL;
235 if (ifinfomsg->ifi_family != AF_UNSPEC)
238 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
239 hw = g_new(AvahiHwInterface, 1);
244 hw->index = ifinfomsg->ifi_index;
246 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
247 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
249 g_hash_table_insert(m->hash_table, &hw->index, hw);
251 if (m->server->fd_ipv4 >= 0)
252 new_interface(m, hw, AF_INET);
253 if (m->server->fd_ipv6 >= 0)
254 new_interface(m, hw, AF_INET6);
257 hw->flags = ifinfomsg->ifi_flags;
259 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
260 a = IFLA_RTA(ifinfomsg);
262 while (RTA_OK(a, l)) {
263 switch(a->rta_type) {
266 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
270 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
271 hw->mtu = *((unsigned int*) RTA_DATA(a));
281 update_hw_interface_rr(m, hw, FALSE);
282 check_hw_interface_relevant(m, hw);
284 } else if (n->nlmsg_type == RTM_DELLINK) {
285 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
286 AvahiHwInterface *hw;
288 if (ifinfomsg->ifi_family != AF_UNSPEC)
291 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
294 update_hw_interface_rr(m, hw, TRUE);
295 free_hw_interface(m, hw, FALSE);
297 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
299 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
301 struct rtattr *a = NULL;
306 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
309 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
312 raddr.family = ifaddrmsg->ifa_family;
314 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
315 a = IFA_RTA(ifaddrmsg);
317 while (RTA_OK(a, l)) {
318 switch(a->rta_type) {
320 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
321 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
324 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
340 if (n->nlmsg_type == RTM_NEWADDR) {
341 AvahiInterfaceAddress *addr;
343 if (!(addr = get_address(m, i, &raddr))) {
344 addr = g_new(AvahiInterfaceAddress, 1);
346 addr->address = raddr;
348 addr->entry_group = NULL;
350 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
353 addr->flags = ifaddrmsg->ifa_flags;
354 addr->scope = ifaddrmsg->ifa_scope;
356 update_address_rr(m, addr, FALSE);
357 check_interface_relevant(m, i);
359 AvahiInterfaceAddress *addr;
361 if (!(addr = get_address(m, i, &raddr)))
364 update_address_rr(m, addr, TRUE);
365 free_address(m, addr);
367 check_interface_relevant(m, i);
370 } else if (n->nlmsg_type == NLMSG_DONE) {
372 if (m->list == LIST_IFACE) {
375 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
376 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
381 g_message("Enumeration complete");
384 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
385 struct nlmsgerr *e = NLMSG_DATA (n);
388 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
392 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
393 AvahiInterfaceMonitor *m = NULL;
395 m = g_new0(AvahiInterfaceMonitor, 1);
397 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
400 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
402 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
403 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
405 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
408 m->list = LIST_IFACE;
413 avahi_interface_monitor_free(m);
417 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
420 while (m->list != LIST_DONE) {
421 if (!avahi_netlink_work(m->netlink, TRUE))
426 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
429 while (m->hw_interfaces)
430 free_hw_interface(m, m->hw_interfaces, TRUE);
432 g_assert(!m->interfaces);
436 avahi_netlink_free(m->netlink);
439 g_hash_table_destroy(m->hash_table);
445 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
446 AvahiHwInterface *hw;
451 g_assert(protocol != AF_UNSPEC);
453 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
456 for (i = hw->interfaces; i; i = i->by_hardware_next)
457 if (i->protocol == protocol)
463 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
467 return g_hash_table_lookup(m->hash_table, &index);
471 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
475 if (avahi_interface_relevant(i)) {
476 g_message("sending on '%s.%i'", i->hardware->name, i->protocol);
478 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
479 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p);
480 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
481 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p);
485 void avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
489 if (avahi_interface_relevant(i))
490 avahi_packet_scheduler_post_query(i->scheduler, key, immediately);
494 void avahi_interface_post_response(AvahiInterface *i, const AvahiAddress *a, AvahiRecord *record, gboolean flush_cache, gboolean immediately) {
498 if (avahi_interface_relevant(i))
499 avahi_packet_scheduler_post_response(i->scheduler, a, record, flush_cache, immediately);
502 void avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
506 if (avahi_interface_relevant(i))
507 avahi_packet_scheduler_post_probe(i->scheduler, record, immediately);
510 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
514 for (i = m->interfaces; i; i = i->interface_next) {
515 if (avahi_interface_relevant(i)) {
516 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
517 avahi_cache_dump(i->cache, f);
523 gboolean avahi_interface_relevant(AvahiInterface *i) {
527 (i->hardware->flags & IFF_UP) &&
528 (i->hardware->flags & IFF_RUNNING) &&
529 !(i->hardware->flags & IFF_LOOPBACK) &&
530 (i->hardware->flags & IFF_MULTICAST) &&
534 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
537 return a->scope == RT_SCOPE_UNIVERSE;
541 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
544 if (index > 0 && index != i->hardware->index)
547 if (protocol != AF_UNSPEC && protocol != i->protocol)
554 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
559 if (protocol != AF_UNSPEC) {
562 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
563 callback(m, i, userdata);
566 AvahiHwInterface *hw;
569 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
570 for (i = hw->interfaces; i; i = i->by_hardware_next)
571 if (avahi_interface_match(i, interface, protocol))
572 callback(m, i, userdata);
578 for (i = m->interfaces; i; i = i->interface_next)
579 if (avahi_interface_match(i, interface, protocol))
580 callback(m, i, userdata);