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 avahi_goodbye_interface(m->server, i, FALSE);
207 if (i->protocol == AF_INET)
208 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
209 if (i->protocol == AF_INET6)
210 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
212 i->announcing = FALSE;
216 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
222 for (i = hw->interfaces; i; i = i->by_hardware_next)
223 check_interface_relevant(m, i);
226 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
227 AvahiInterfaceMonitor *m = userdata;
231 g_assert(m->netlink == nl);
233 if (n->nlmsg_type == RTM_NEWLINK) {
234 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
235 AvahiHwInterface *hw;
236 struct rtattr *a = NULL;
239 if (ifinfomsg->ifi_family != AF_UNSPEC)
242 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
243 hw = g_new(AvahiHwInterface, 1);
248 hw->index = ifinfomsg->ifi_index;
250 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
251 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
253 g_hash_table_insert(m->hash_table, &hw->index, hw);
255 if (m->server->fd_ipv4 >= 0)
256 new_interface(m, hw, AF_INET);
257 if (m->server->fd_ipv6 >= 0)
258 new_interface(m, hw, AF_INET6);
261 hw->flags = ifinfomsg->ifi_flags;
263 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
264 a = IFLA_RTA(ifinfomsg);
266 while (RTA_OK(a, l)) {
267 switch(a->rta_type) {
270 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
274 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
275 hw->mtu = *((unsigned int*) RTA_DATA(a));
285 update_hw_interface_rr(m, hw, FALSE);
286 check_hw_interface_relevant(m, hw);
288 } else if (n->nlmsg_type == RTM_DELLINK) {
289 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
290 AvahiHwInterface *hw;
292 if (ifinfomsg->ifi_family != AF_UNSPEC)
295 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
298 update_hw_interface_rr(m, hw, TRUE);
299 free_hw_interface(m, hw, FALSE);
301 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
303 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
305 struct rtattr *a = NULL;
310 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
313 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
316 raddr.family = ifaddrmsg->ifa_family;
318 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
319 a = IFA_RTA(ifaddrmsg);
321 while (RTA_OK(a, l)) {
322 switch(a->rta_type) {
324 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
325 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
328 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
344 if (n->nlmsg_type == RTM_NEWADDR) {
345 AvahiInterfaceAddress *addr;
347 if (!(addr = get_address(m, i, &raddr))) {
348 addr = g_new(AvahiInterfaceAddress, 1);
350 addr->address = raddr;
352 addr->entry_group = NULL;
354 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
357 addr->flags = ifaddrmsg->ifa_flags;
358 addr->scope = ifaddrmsg->ifa_scope;
360 update_address_rr(m, addr, FALSE);
361 check_interface_relevant(m, i);
363 AvahiInterfaceAddress *addr;
365 if (!(addr = get_address(m, i, &raddr)))
368 update_address_rr(m, addr, TRUE);
369 free_address(m, addr);
371 check_interface_relevant(m, i);
374 } else if (n->nlmsg_type == NLMSG_DONE) {
376 if (m->list == LIST_IFACE) {
379 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
380 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
385 g_message("Enumeration complete");
388 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
389 struct nlmsgerr *e = NLMSG_DATA (n);
392 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
396 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
397 AvahiInterfaceMonitor *m = NULL;
399 m = g_new0(AvahiInterfaceMonitor, 1);
401 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
404 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
406 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
407 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
409 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
412 m->list = LIST_IFACE;
417 avahi_interface_monitor_free(m);
421 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
424 while (m->list != LIST_DONE) {
425 if (!avahi_netlink_work(m->netlink, TRUE))
430 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
433 while (m->hw_interfaces)
434 free_hw_interface(m, m->hw_interfaces, TRUE);
436 g_assert(!m->interfaces);
440 avahi_netlink_free(m->netlink);
443 g_hash_table_destroy(m->hash_table);
449 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
450 AvahiHwInterface *hw;
455 g_assert(protocol != AF_UNSPEC);
457 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
460 for (i = hw->interfaces; i; i = i->by_hardware_next)
461 if (i->protocol == protocol)
467 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
471 return g_hash_table_lookup(m->hash_table, &index);
475 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
480 if (!avahi_interface_relevant(i))
483 g_assert(!a || a->family == i->protocol);
486 g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port);
488 g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol);
490 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
491 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
492 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
493 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
496 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
500 avahi_interface_send_packet_unicast(i, p, NULL, 0);
503 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
507 if (avahi_interface_relevant(i))
508 return avahi_packet_scheduler_post_query(i->scheduler, key, immediately);
513 gboolean avahi_interface_post_response(AvahiInterface *i, const AvahiAddress *a, AvahiRecord *record, gboolean flush_cache, gboolean immediately) {
517 if (avahi_interface_relevant(i))
518 return avahi_packet_scheduler_post_response(i->scheduler, a, record, flush_cache, immediately);
523 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
527 if (avahi_interface_relevant(i))
528 return avahi_packet_scheduler_post_probe(i->scheduler, record, immediately);
533 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
537 for (i = m->interfaces; i; i = i->interface_next) {
538 if (avahi_interface_relevant(i)) {
539 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
540 avahi_cache_dump(i->cache, f);
546 gboolean avahi_interface_relevant(AvahiInterface *i) {
550 (i->hardware->flags & IFF_UP) &&
551 (i->hardware->flags & IFF_RUNNING) &&
552 !(i->hardware->flags & IFF_LOOPBACK) &&
553 (i->hardware->flags & IFF_MULTICAST) &&
557 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
560 return a->scope == RT_SCOPE_UNIVERSE;
564 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
567 if (index > 0 && index != i->hardware->index)
570 if (protocol != AF_UNSPEC && protocol != i->protocol)
577 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
582 if (protocol != AF_UNSPEC) {
585 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
586 callback(m, i, userdata);
589 AvahiHwInterface *hw;
592 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
593 for (i = hw->interfaces; i; i = i->by_hardware_next)
594 if (avahi_interface_match(i, interface, protocol))
595 callback(m, i, userdata);
601 for (i = m->interfaces; i; i = i->interface_next)
602 if (avahi_interface_match(i, interface, protocol))
603 callback(m, i, userdata);