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, gboolean remove) {
45 if (avahi_interface_address_relevant(a) &&
47 m->server->config.register_addresses &&
48 (m->server->state == AVAHI_SERVER_RUNNING ||
49 m->server->state == AVAHI_SERVER_REGISTERING)) {
51 if (!a->entry_group) {
52 a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
53 avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address);
54 avahi_entry_group_commit(a->entry_group);
60 if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
61 avahi_server_decrease_host_rr_pending(m->server);
63 avahi_entry_group_free(a->entry_group);
64 a->entry_group = NULL;
69 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove) {
70 AvahiInterfaceAddress *a;
74 for (a = i->addresses; a; a = a->address_next)
75 update_address_rr(m, a, remove);
78 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove) {
84 for (i = hw->interfaces; i; i = i->by_hardware_next)
85 update_interface_rr(m, i, remove);
88 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
91 g_assert(a->interface);
93 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
96 avahi_entry_group_free(a->entry_group);
101 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
105 g_message("removing interface %s.%i", i->hardware->name, i->protocol);
106 avahi_goodbye_interface(m->server, i, send_goodbye);
107 g_message("forcing responses...");
108 avahi_response_scheduler_force(i->response_scheduler);
111 g_assert(!i->announcements);
114 free_address(m, i->addresses);
116 avahi_response_scheduler_free(i->response_scheduler);
117 avahi_query_scheduler_free(i->query_scheduler);
118 avahi_probe_scheduler_free(i->probe_scheduler);
119 avahi_cache_free(i->cache);
121 AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
122 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
127 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
131 while (hw->interfaces)
132 free_interface(m, hw->interfaces, send_goodbye);
134 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
135 g_hash_table_remove(m->hash_table, &hw->index);
141 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
142 AvahiInterfaceAddress *ia;
148 for (ia = i->addresses; ia; ia = ia->address_next)
149 if (avahi_address_cmp(&ia->address, raddr) == 0)
155 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
157 struct rtgenmsg *gen;
160 memset(&req, 0, sizeof(req));
161 n = (struct nlmsghdr*) req;
162 n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
163 n->nlmsg_type = type;
164 n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
168 memset(gen, 0, sizeof(struct rtgenmsg));
169 gen->rtgen_family = AF_UNSPEC;
171 return avahi_netlink_send(nl, n, ret_seq);
174 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
179 g_assert(protocol != AF_UNSPEC);
181 i = g_new(AvahiInterface, 1);
184 i->protocol = protocol;
185 i->announcing = FALSE;
187 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
188 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
190 i->cache = avahi_cache_new(m->server, i);
191 i->response_scheduler = avahi_response_scheduler_new(i);
192 i->query_scheduler = avahi_query_scheduler_new(i);
193 i->probe_scheduler = avahi_probe_scheduler_new(i);
195 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
196 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
199 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
205 b = avahi_interface_relevant(i);
207 if (b && !i->announcing) {
208 g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
210 if (i->protocol == AF_INET)
211 avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
212 if (i->protocol == AF_INET6)
213 avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
215 i->announcing = TRUE;
216 avahi_announce_interface(m->server, i);
217 } else if (!b && i->announcing) {
218 g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
220 if (i->protocol == AF_INET)
221 avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
222 if (i->protocol == AF_INET6)
223 avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
225 avahi_goodbye_interface(m->server, i, FALSE);
226 avahi_response_scheduler_clear(i->response_scheduler);
227 avahi_query_scheduler_clear(i->query_scheduler);
228 avahi_probe_scheduler_clear(i->probe_scheduler);
229 avahi_cache_flush(i->cache);
231 i->announcing = FALSE;
235 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
241 for (i = hw->interfaces; i; i = i->by_hardware_next)
242 check_interface_relevant(m, i);
245 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
246 AvahiInterfaceMonitor *m = userdata;
250 g_assert(m->netlink == nl);
252 if (n->nlmsg_type == RTM_NEWLINK) {
253 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
254 AvahiHwInterface *hw;
255 struct rtattr *a = NULL;
258 if (ifinfomsg->ifi_family != AF_UNSPEC)
261 if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
262 hw = g_new(AvahiHwInterface, 1);
267 hw->index = ifinfomsg->ifi_index;
269 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
270 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
272 g_hash_table_insert(m->hash_table, &hw->index, hw);
274 if (m->server->fd_ipv4 >= 0)
275 new_interface(m, hw, AF_INET);
276 if (m->server->fd_ipv6 >= 0)
277 new_interface(m, hw, AF_INET6);
280 hw->flags = ifinfomsg->ifi_flags;
282 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
283 a = IFLA_RTA(ifinfomsg);
285 while (RTA_OK(a, l)) {
286 switch(a->rta_type) {
289 hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
293 g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
294 hw->mtu = *((unsigned int*) RTA_DATA(a));
304 update_hw_interface_rr(m, hw, FALSE);
305 check_hw_interface_relevant(m, hw);
307 } else if (n->nlmsg_type == RTM_DELLINK) {
308 struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
309 AvahiHwInterface *hw;
311 if (ifinfomsg->ifi_family != AF_UNSPEC)
314 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
317 update_hw_interface_rr(m, hw, TRUE);
318 free_hw_interface(m, hw, FALSE);
320 } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
322 struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
324 struct rtattr *a = NULL;
329 if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
332 if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
335 raddr.family = ifaddrmsg->ifa_family;
337 l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
338 a = IFA_RTA(ifaddrmsg);
340 while (RTA_OK(a, l)) {
341 switch(a->rta_type) {
343 if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
344 (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
347 memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
363 if (n->nlmsg_type == RTM_NEWADDR) {
364 AvahiInterfaceAddress *addr;
366 if (!(addr = get_address(m, i, &raddr))) {
367 addr = g_new(AvahiInterfaceAddress, 1);
369 addr->address = raddr;
371 addr->entry_group = NULL;
373 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
376 addr->flags = ifaddrmsg->ifa_flags;
377 addr->scope = ifaddrmsg->ifa_scope;
379 update_address_rr(m, addr, FALSE);
380 check_interface_relevant(m, i);
382 AvahiInterfaceAddress *addr;
384 if (!(addr = get_address(m, i, &raddr)))
387 update_address_rr(m, addr, TRUE);
388 free_address(m, addr);
390 check_interface_relevant(m, i);
393 } else if (n->nlmsg_type == NLMSG_DONE) {
395 if (m->list == LIST_IFACE) {
398 if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
399 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
404 g_message("Enumeration complete");
407 } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
408 struct nlmsgerr *e = NLMSG_DATA (n);
411 g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
415 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
416 AvahiInterfaceMonitor *m = NULL;
418 m = g_new0(AvahiInterfaceMonitor, 1);
420 if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
423 m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
425 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
426 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
428 if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
431 m->list = LIST_IFACE;
436 avahi_interface_monitor_free(m);
440 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
443 while (m->list != LIST_DONE) {
444 if (!avahi_netlink_work(m->netlink, TRUE))
449 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
452 while (m->hw_interfaces)
453 free_hw_interface(m, m->hw_interfaces, TRUE);
455 g_assert(!m->interfaces);
459 avahi_netlink_free(m->netlink);
462 g_hash_table_destroy(m->hash_table);
468 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
469 AvahiHwInterface *hw;
474 g_assert(protocol != AF_UNSPEC);
476 if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
479 for (i = hw->interfaces; i; i = i->by_hardware_next)
480 if (i->protocol == protocol)
486 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
490 return g_hash_table_lookup(m->hash_table, &index);
494 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
499 if (!avahi_interface_relevant(i))
502 g_assert(!a || a->family == i->protocol);
505 /* g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
507 /* g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
509 if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
510 avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
511 else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
512 avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
515 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
519 avahi_interface_send_packet_unicast(i, p, NULL, 0);
522 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
526 if (avahi_interface_relevant(i))
527 return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
532 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
536 if (avahi_interface_relevant(i))
537 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
542 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
546 if (avahi_interface_relevant(i))
547 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
552 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
556 for (i = m->interfaces; i; i = i->interface_next) {
557 if (avahi_interface_relevant(i)) {
558 fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
559 avahi_cache_dump(i->cache, f);
565 gboolean avahi_interface_relevant(AvahiInterface *i) {
569 (i->hardware->flags & IFF_UP) &&
570 (i->hardware->flags & IFF_RUNNING) &&
571 !(i->hardware->flags & IFF_LOOPBACK) &&
572 (i->hardware->flags & IFF_MULTICAST) &&
576 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) {
579 return a->scope == RT_SCOPE_UNIVERSE;
583 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
586 if (index > 0 && index != i->hardware->index)
589 if (protocol != AF_UNSPEC && protocol != i->protocol)
595 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
600 if (protocol != AF_UNSPEC) {
603 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
604 callback(m, i, userdata);
607 AvahiHwInterface *hw;
610 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
611 for (i = hw->interfaces; i; i = i->by_hardware_next)
612 if (avahi_interface_match(i, interface, protocol))
613 callback(m, i, userdata);
619 for (i = m->interfaces; i; i = i->interface_next)
620 if (avahi_interface_match(i, interface, protocol))
621 callback(m, i, userdata);
625 void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove) {
630 for (i = m->interfaces; i; i = i->interface_next)
631 update_interface_rr(m, i, remove);