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("forcing responses...");
97 avahi_response_scheduler_force(i->response_scheduler);
100 g_assert(!i->announcements);
103 free_address(m, i->addresses);
105 avahi_response_scheduler_free(i->response_scheduler);
106 avahi_query_scheduler_free(i->query_scheduler);
107 avahi_probe_scheduler_free(i->probe_scheduler);
108 avahi_cache_free(i->cache);
110 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
111 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
116 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
120 while (hw->interfaces)
121 free_interface(m, hw->interfaces, send_goodbye);
123 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
124 g_hash_table_remove(m->hash_table, &hw->index);
130 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
131 AvahiInterfaceAddress *ia;
137 for (ia = i->addresses; ia; ia = ia->address_next)
138 if (avahi_address_cmp(&ia->address, raddr) == 0)
144 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
146 struct rtgenmsg *gen;
149 memset(&req, 0, sizeof(req));
150 n = (struct nlmsghdr*) req;
151 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
152 n->nlmsg_type = type;
153 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
157 memset(gen, 0, sizeof(struct rtgenmsg));
158 gen->rtgen_family = AF_UNSPEC;
160 return avahi_netlink_send(nl, n, ret_seq);
163 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
168 g_assert(protocol != AF_UNSPEC);
170 i = g_new(AvahiInterface, 1);
173 i->protocol = protocol;
174 i->announcing = FALSE;
176 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
177 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
179 i->cache = avahi_cache_new(m->server, i);
180 i->response_scheduler = avahi_response_scheduler_new(i);
181 i->query_scheduler = avahi_query_scheduler_new(i);
182 i->probe_scheduler = avahi_probe_scheduler_new(i);
184 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
185 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
188 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
194 b = avahi_interface_relevant(i);
196 if (b && !i->announcing) {
197 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
199 if (i->protocol == AF_INET)
200 avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
201 if (i->protocol == AF_INET6)
202 avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
204 i->announcing = TRUE;
205 avahi_announce_interface(m->server, i);
206 } else if (!b && i->announcing) {
207 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
209 if (i->protocol == AF_INET)
210 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
211 if (i->protocol == AF_INET6)
212 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
214 avahi_goodbye_interface(m->server, i, FALSE);
215 avahi_response_scheduler_clear(i->response_scheduler);
216 avahi_query_scheduler_clear(i->query_scheduler);
217 avahi_probe_scheduler_clear(i->probe_scheduler);
218 avahi_cache_flush(i->cache);
220 i->announcing = FALSE;
224 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
230 for (i = hw->interfaces; i; i = i->by_hardware_next)
231 check_interface_relevant(m, i);
234 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
235 AvahiInterfaceMonitor *m = userdata;
239 g_assert(m->netlink == nl);
241 if (n->nlmsg_type == RTM_NEWLINK) {
242 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
243 AvahiHwInterface *hw;
244 struct rtattr *a = NULL;
247 if (ifinfomsg->ifi_family != AF_UNSPEC)
250 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
251 hw = g_new(AvahiHwInterface, 1);
256 hw->index = ifinfomsg->ifi_index;
258 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
259 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
261 g_hash_table_insert(m->hash_table, &hw->index, hw);
263 if (m->server->fd_ipv4 >= 0)
264 new_interface(m, hw, AF_INET);
265 if (m->server->fd_ipv6 >= 0)
266 new_interface(m, hw, AF_INET6);
269 hw->flags = ifinfomsg->ifi_flags;
271 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
272 a = IFLA_RTA(ifinfomsg);
274 while (RTA_OK(a, l)) {
275 switch(a->rta_type) {
278 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
282 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
283 hw->mtu = *((unsigned int*) RTA_DATA(a));
293 update_hw_interface_rr(m, hw, FALSE);
294 check_hw_interface_relevant(m, hw);
296 } else if (n->nlmsg_type == RTM_DELLINK) {
297 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
298 AvahiHwInterface *hw;
300 if (ifinfomsg->ifi_family != AF_UNSPEC)
303 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
306 update_hw_interface_rr(m, hw, TRUE);
307 free_hw_interface(m, hw, FALSE);
309 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
311 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
313 struct rtattr *a = NULL;
318 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
321 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
324 raddr.family = ifaddrmsg->ifa_family;
326 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
327 a = IFA_RTA(ifaddrmsg);
329 while (RTA_OK(a, l)) {
330 switch(a->rta_type) {
332 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
333 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
336 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
352 if (n->nlmsg_type == RTM_NEWADDR) {
353 AvahiInterfaceAddress *addr;
355 if (!(addr = get_address(m, i, &raddr))) {
356 addr = g_new(AvahiInterfaceAddress, 1);
358 addr->address = raddr;
360 addr->entry_group = NULL;
362 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
365 addr->flags = ifaddrmsg->ifa_flags;
366 addr->scope = ifaddrmsg->ifa_scope;
368 update_address_rr(m, addr, FALSE);
369 check_interface_relevant(m, i);
371 AvahiInterfaceAddress *addr;
373 if (!(addr = get_address(m, i, &raddr)))
376 update_address_rr(m, addr, TRUE);
377 free_address(m, addr);
379 check_interface_relevant(m, i);
382 } else if (n->nlmsg_type == NLMSG_DONE) {
384 if (m->list == LIST_IFACE) {
387 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
388 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
393 g_message("Enumeration complete");
396 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
397 struct nlmsgerr *e = NLMSG_DATA (n);
400 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
404 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
405 AvahiInterfaceMonitor *m = NULL;
407 m = g_new0(AvahiInterfaceMonitor, 1);
409 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
412 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
414 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
415 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
417 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
420 m->list = LIST_IFACE;
425 avahi_interface_monitor_free(m);
429 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
432 while (m->list != LIST_DONE) {
433 if (!avahi_netlink_work(m->netlink, TRUE))
438 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
441 while (m->hw_interfaces)
442 free_hw_interface(m, m->hw_interfaces, TRUE);
444 g_assert(!m->interfaces);
448 avahi_netlink_free(m->netlink);
451 g_hash_table_destroy(m->hash_table);
457 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
458 AvahiHwInterface *hw;
463 g_assert(protocol != AF_UNSPEC);
465 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
468 for (i = hw->interfaces; i; i = i->by_hardware_next)
469 if (i->protocol == protocol)
475 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
479 return g_hash_table_lookup(m->hash_table, &index);
483 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
488 if (!avahi_interface_relevant(i))
491 g_assert(!a || a->family == i->protocol);
494 g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port);
496 g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol);
498 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
499 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
500 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
501 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
504 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
508 avahi_interface_send_packet_unicast(i, p, NULL, 0);
511 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
515 if (avahi_interface_relevant(i))
516 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
521 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
525 if (avahi_interface_relevant(i))
526 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
531 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
535 if (avahi_interface_relevant(i))
536 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
541 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
545 for (i = m->interfaces; i; i = i->interface_next) {
546 if (avahi_interface_relevant(i)) {
547 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
548 avahi_cache_dump(i->cache, f);
554 gboolean avahi_interface_relevant(AvahiInterface *i) {
558 (i->hardware->flags & IFF_UP) &&
559 (i->hardware->flags & IFF_RUNNING) &&
560 !(i->hardware->flags & IFF_LOOPBACK) &&
561 (i->hardware->flags & IFF_MULTICAST) &&
565 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
568 return a->scope == RT_SCOPE_UNIVERSE;
572 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
575 if (index > 0 && index != i->hardware->index)
578 if (protocol != AF_UNSPEC && protocol != i->protocol)
585 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
590 if (protocol != AF_UNSPEC) {
593 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
594 callback(m, i, userdata);
597 AvahiHwInterface *hw;
600 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
601 for (i = hw->interfaces; i; i = i->by_hardware_next)
602 if (avahi_interface_match(i, interface, protocol))
603 callback(m, i, userdata);
609 for (i = m->interfaces; i; i = i->interface_next)
610 if (avahi_interface_match(i, interface, protocol))
611 callback(m, i, userdata);