2 #include <sys/socket.h>
4 #include <linux/netlink.h>
5 #include <linux/rtnetlink.h>
15 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove) {
19 if (!avahi_interface_address_relevant(a) || remove) {
21 avahi_entry_group_free(a->entry_group);
22 a->entry_group = NULL;
25 if (!a->entry_group) {
26 a->entry_group = avahi_entry_group_new(m->server, NULL, NULL);
27 avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address);
28 avahi_entry_group_commit(a->entry_group);
33 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove) {
34 AvahiInterfaceAddress *a;
38 for (a = i->addresses; a; a = a->address_next)
39 update_address_rr(m, a, remove);
42 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove) {
48 for (i = hw->interfaces; i; i = i->by_hardware_next)
49 update_interface_rr(m, i, remove);
52 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
55 g_assert(a->interface);
57 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
60 avahi_entry_group_free(a->entry_group);
65 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
69 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
70 avahi_goodbye_interface(m->server, i, send_goodbye);
71 g_message("flushing...");
72 avahi_packet_scheduler_flush_responses(i->scheduler);
75 g_assert(!i->announcements);
78 free_address(m, i->addresses);
80 avahi_packet_scheduler_free(i->scheduler);
81 avahi_cache_free(i->cache);
83 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
84 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
89 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
93 while (hw->interfaces)
94 free_interface(m, hw->interfaces, send_goodbye);
96 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
97 g_hash_table_remove(m->hash_table, &hw->index);
103 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
104 AvahiInterfaceAddress *ia;
110 for (ia = i->addresses; ia; ia = ia->address_next)
111 if (avahi_address_cmp(&ia->address, raddr) == 0)
117 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
119 struct rtgenmsg *gen;
122 memset(&req, 0, sizeof(req));
123 n = (struct nlmsghdr*) req;
124 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
125 n->nlmsg_type = type;
126 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
130 memset(gen, 0, sizeof(struct rtgenmsg));
131 gen->rtgen_family = AF_UNSPEC;
133 return avahi_netlink_send(nl, n, ret_seq);
136 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
141 g_assert(protocol != AF_UNSPEC);
143 i = g_new(AvahiInterface, 1);
146 i->protocol = protocol;
147 i->announcing = FALSE;
149 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
150 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
152 i->cache = avahi_cache_new(m->server, i);
153 i->scheduler = avahi_packet_scheduler_new(m->server, i);
155 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
156 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
159 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
165 b = avahi_interface_relevant(i);
167 if (b && !i->announcing) {
168 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
170 if (i->protocol == AF_INET)
171 avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
172 if (i->protocol == AF_INET6)
173 avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
175 i->announcing = TRUE;
176 avahi_announce_interface(m->server, i);
177 } else if (!b && i->announcing) {
178 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
180 avahi_goodbye_interface(m->server, i, FALSE);
182 if (i->protocol == AF_INET)
183 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
184 if (i->protocol == AF_INET6)
185 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
187 i->announcing = FALSE;
191 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
197 for (i = hw->interfaces; i; i = i->by_hardware_next)
198 check_interface_relevant(m, i);
201 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
202 AvahiInterfaceMonitor *m = userdata;
206 g_assert(m->netlink == nl);
208 if (n->nlmsg_type == RTM_NEWLINK) {
209 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
210 AvahiHwInterface *hw;
211 struct rtattr *a = NULL;
214 if (ifinfomsg->ifi_family != AF_UNSPEC)
217 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
218 hw = g_new(AvahiHwInterface, 1);
223 hw->index = ifinfomsg->ifi_index;
225 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
226 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
228 g_hash_table_insert(m->hash_table, &hw->index, hw);
230 if (m->server->fd_ipv4 >= 0)
231 new_interface(m, hw, AF_INET);
232 if (m->server->fd_ipv6 >= 0)
233 new_interface(m, hw, AF_INET6);
236 hw->flags = ifinfomsg->ifi_flags;
238 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
239 a = IFLA_RTA(ifinfomsg);
241 while (RTA_OK(a, l)) {
242 switch(a->rta_type) {
245 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
249 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
250 hw->mtu = *((unsigned int*) RTA_DATA(a));
260 update_hw_interface_rr(m, hw, FALSE);
261 check_hw_interface_relevant(m, hw);
263 } else if (n->nlmsg_type == RTM_DELLINK) {
264 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
265 AvahiHwInterface *hw;
268 if (ifinfomsg->ifi_family != AF_UNSPEC)
271 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
274 update_hw_interface_rr(m, hw, TRUE);
275 free_hw_interface(m, hw, FALSE);
277 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
279 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
281 struct rtattr *a = NULL;
286 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
289 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
292 raddr.family = ifaddrmsg->ifa_family;
294 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
295 a = IFA_RTA(ifaddrmsg);
297 while (RTA_OK(a, l)) {
298 switch(a->rta_type) {
300 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
301 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
304 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
320 if (n->nlmsg_type == RTM_NEWADDR) {
321 AvahiInterfaceAddress *addr;
323 if (!(addr = get_address(m, i, &raddr))) {
324 addr = g_new(AvahiInterfaceAddress, 1);
326 addr->address = raddr;
328 addr->entry_group = NULL;
330 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
333 addr->flags = ifaddrmsg->ifa_flags;
334 addr->scope = ifaddrmsg->ifa_scope;
336 update_address_rr(m, addr, FALSE);
337 check_interface_relevant(m, i);
339 AvahiInterfaceAddress *addr;
341 if (!(addr = get_address(m, i, &raddr)))
344 update_address_rr(m, addr, TRUE);
345 free_address(m, addr);
347 check_interface_relevant(m, i);
350 } else if (n->nlmsg_type == NLMSG_DONE) {
352 if (m->list == LIST_IFACE) {
355 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
356 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
361 g_message("Enumeration complete");
364 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
365 struct nlmsgerr *e = NLMSG_DATA (n);
368 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
372 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
373 AvahiInterfaceMonitor *m = NULL;
375 m = g_new0(AvahiInterfaceMonitor, 1);
377 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
380 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
382 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
383 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
385 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
388 m->list = LIST_IFACE;
393 avahi_interface_monitor_free(m);
397 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
400 while (m->list != LIST_DONE) {
401 if (!avahi_netlink_work(m->netlink, TRUE))
406 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
409 while (m->hw_interfaces)
410 free_hw_interface(m, m->hw_interfaces, TRUE);
412 g_assert(!m->interfaces);
416 avahi_netlink_free(m->netlink);
419 g_hash_table_destroy(m->hash_table);
425 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
426 AvahiHwInterface *hw;
431 g_assert(protocol != AF_UNSPEC);
433 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
436 for (i = hw->interfaces; i; i = i->by_hardware_next)
437 if (i->protocol == protocol)
443 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
447 return g_hash_table_lookup(m->hash_table, &index);
451 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
455 if (avahi_interface_relevant(i)) {
456 g_message("sending on '%s.%i'", i->hardware->name, i->protocol);
458 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
459 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p);
460 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
461 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p);
465 void avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
469 if (avahi_interface_relevant(i))
470 avahi_packet_scheduler_post_query(i->scheduler, key, immediately);
474 void avahi_interface_post_response(AvahiInterface *i, const AvahiAddress *a, AvahiRecord *record, gboolean flush_cache, gboolean immediately) {
478 if (avahi_interface_relevant(i))
479 avahi_packet_scheduler_post_response(i->scheduler, a, record, flush_cache, immediately);
482 void avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
486 if (avahi_interface_relevant(i))
487 avahi_packet_scheduler_post_probe(i->scheduler, record, immediately);
490 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
494 for (i = m->interfaces; i; i = i->interface_next) {
495 if (avahi_interface_relevant(i)) {
496 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
497 avahi_cache_dump(i->cache, f);
503 gboolean avahi_interface_relevant(AvahiInterface *i) {
507 (i->hardware->flags & IFF_UP) &&
508 (i->hardware->flags & IFF_RUNNING) &&
509 !(i->hardware->flags & IFF_LOOPBACK) &&
510 (i->hardware->flags & IFF_MULTICAST) &&
514 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
517 return a->scope == RT_SCOPE_UNIVERSE;
521 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
524 if (index > 0 && index != i->hardware->index)
527 if (protocol != AF_UNSPEC && protocol != i->protocol)
534 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
539 if (protocol != AF_UNSPEC) {
542 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
543 callback(m, i, userdata);
546 AvahiHwInterface *hw;
549 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
550 for (i = hw->interfaces; i; i = i->by_hardware_next)
551 if (avahi_interface_match(i, interface, protocol))
552 callback(m, i, userdata);
558 for (i = m->interfaces; i; i = i->interface_next)
559 if (avahi_interface_match(i, interface, protocol))
560 callback(m, i, userdata);