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
26 #include <avahi-common/malloc.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/param.h>
35 #ifdef HAVE_SYS_SYSCTL_H
36 #include <sys/sysctl.h>
39 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
46 #include "iface-pfroute.h"
49 static int bitcount (unsigned int n)
60 static void rtm_info(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
63 struct if_msghdr *ifm = (struct if_msghdr *)rtm;
64 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
66 if (sdl->sdl_family != AF_LINK)
69 if (ifm->ifm_addrs == 0 && ifm->ifm_index > 0) {
70 if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifm->ifm_index)))
72 avahi_hw_interface_free(hw, 0);
76 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifm->ifm_index)))
77 if (!(hw = avahi_hw_interface_new(m, (AvahiIfIndex) ifm->ifm_index)))
81 (ifm->ifm_flags & IFF_UP) &&
82 (!m->server->config.use_iff_running || (ifm->ifm_flags & IFF_RUNNING)) &&
83 !(ifm->ifm_flags & IFF_LOOPBACK) &&
84 (ifm->ifm_flags & IFF_MULTICAST) &&
85 (m->server->config.allow_point_to_point || !(ifm->ifm_flags & IFF_POINTOPOINT));
88 hw->name = avahi_strndup(sdl->sdl_data, sdl->sdl_nlen);
90 hw->mtu = ifm->ifm_data.ifi_mtu;
92 hw->mac_address_size = sdl->sdl_alen;
93 if (hw->mac_address_size > AVAHI_MAC_ADDRESS_MAX)
94 hw->mac_address_size = AVAHI_MAC_ADDRESS_MAX;
96 memcpy(hw->mac_address, sdl->sdl_data + sdl->sdl_nlen, hw->mac_address_size);
100 /* avahi_log_debug("======\n name: %s\n index:%d\n mtu:%d\n mac:%s\n flags_ok:%d\n======", */
101 /* hw->name, hw->index, */
103 /* avahi_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size), */
107 avahi_hw_interface_check_relevant(hw);
108 avahi_hw_interface_update_rrs(hw, 0);
112 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113 #ifdef HAVE_SYS_SYSCTL_H
114 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
116 #define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr)))
119 static void rtm_addr(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
121 AvahiInterface *iface;
124 struct ifa_msghdr *ifam = (struct ifa_msghdr *) rtm;
125 char *cp = (char *)(ifam + 1);
126 int addrs = ifam->ifam_addrs;
129 struct sockaddr *sa =NULL;
131 #if defined(__NetBSD__) || defined(__OpenBSD__)
132 if(((struct sockaddr *)cp)->sa_family == AF_UNSPEC)
133 ((struct sockaddr *)cp)->sa_family = AF_INET;
136 if(((struct sockaddr *)cp)->sa_family != AF_INET && ((struct sockaddr *)cp)->sa_family != AF_INET6)
139 if (!(iface = avahi_interface_monitor_get_interface(m, (AvahiIfIndex) ifam->ifam_index, avahi_af_to_proto(((struct sockaddr *)cp)->sa_family))))
142 raddr.proto = avahi_af_to_proto(((struct sockaddr *)cp)->sa_family);
144 for(i = 0; addrs != 0 && i < RTAX_MAX; addrs &= ~(1<<i), i++)
148 sa = (struct sockaddr *)cp;
149 #ifdef HAVE_SYS_SYSCTL_H
153 switch(sa->sa_family) {
157 prefixlen = bitcount((unsigned int)((struct sockaddr_in *)sa)->sin_addr.s_addr);
160 memcpy(raddr.data.data, &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr));
169 prefixlen = bitcount((unsigned int)((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr);
172 memcpy(raddr.data.data, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr));
191 if(rtm->rtm_type == RTM_NEWADDR)
193 AvahiInterfaceAddress *addriface;
194 if (!(addriface = avahi_interface_monitor_get_address(m, iface, &raddr)))
195 if (!(addriface = avahi_interface_address_new(m, iface, &raddr, prefixlen)))
198 /* addriface->global_scope = ifaddrmsg->ifa_scope == RT_SCOPE_UNIVERSE || ifaddrmsg->ifa_scope == RT_SCOPE_SITE; */
199 addriface->global_scope = 1;
203 AvahiInterfaceAddress *addriface;
204 assert(rtm->rtm_type == RTM_DELADDR);
205 if (!(addriface = avahi_interface_monitor_get_address(m, iface, &raddr)))
207 avahi_interface_address_free(addriface);
210 avahi_interface_check_relevant(iface);
211 avahi_interface_update_rrs(iface, 0);
214 static void parse_rtmsg(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
219 if (rtm->rtm_version != RTM_VERSION) {
220 avahi_log_warn("routing message version %d not understood",
225 switch (rtm->rtm_type) {
238 static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event,void *userdata) {
239 AvahiInterfaceMonitor *m = (AvahiInterfaceMonitor *)userdata;
240 AvahiPfRoute *nl = m->osdep.pfroute;
247 assert(fd == nl->fd);
250 if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
251 if (errno == EAGAIN || errno == EINTR)
253 avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
256 parse_rtmsg((struct rt_msghdr *)msg, m);
261 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
263 m->osdep.pfroute = NULL;
267 if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
268 avahi_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
272 if (!(m->osdep.pfroute = avahi_new(AvahiPfRoute , 1))) {
273 avahi_log_error(__FILE__": avahi_new() failed.");
276 m->osdep.pfroute->fd = fd;
278 if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api,
279 m->osdep.pfroute->fd,
283 avahi_log_error(__FILE__": Failed to create watch.");
291 if (m->osdep.pfroute) {
292 if (m->osdep.pfroute->watch)
293 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
298 m->osdep.pfroute = NULL;
304 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
307 if (m->osdep.pfroute) {
308 if (m->osdep.pfroute->watch)
309 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
311 if (m->osdep.pfroute->fd >= 0)
312 close(m->osdep.pfroute->fd);
314 avahi_free(m->osdep.pfroute);
315 m->osdep.pfroute = NULL;
319 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
322 char *buf, *lim, *next, count = 0;
323 struct rt_msghdr *rtm;
328 #ifdef HAVE_SYS_SYSCTL_H
331 mib[2] = 0; /* protocol */
332 mib[3] = 0; /* wildcard address family */
333 mib[4] = NET_RT_IFLIST;
334 mib[5] = 0; /* no flags */
335 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
337 avahi_log_error("sysctl failed: %s", strerror(errno));
338 avahi_log_error("route-sysctl-estimate");
342 if ((buf = avahi_malloc(needed)) == NULL)
344 avahi_log_error("malloc failed in avahi_interface_monitor_sync");
347 #ifdef HAVE_SYS_SYSCTL_H
348 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
349 avahi_log_warn("sysctl failed: %s", strerror(errno));
350 if (errno == ENOMEM && count++ < 10) {
351 avahi_log_warn("Routing table grew, retrying");
359 for (next = buf; next < lim; next += rtm->rtm_msglen) {
360 rtm = (struct rt_msghdr *)next;
364 m->list_complete = 1;
365 avahi_interface_monitor_check_relevant(m);
366 avahi_interface_monitor_update_rrs(m, 0);
367 avahi_log_info("Network interface enumeration completed.");