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 #include <sys/sysctl.h>
37 #include <net/route.h>
39 #include <net/if_dl.h>
40 #include <netinet/in.h>
44 #include "iface-pfroute.h"
47 static int bitcount (unsigned int n)
58 static void rtm_info(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
61 struct if_msghdr *ifm = (struct if_msghdr *)rtm;
62 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
64 if (sdl->sdl_family != AF_LINK)
67 if (ifm->ifm_addrs == 0 && ifm->ifm_index > 0) {
68 if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifm->ifm_index)))
70 avahi_hw_interface_free(hw, 0);
74 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifm->ifm_index)))
75 if (!(hw = avahi_hw_interface_new(m, (AvahiIfIndex) ifm->ifm_index)))
79 (ifm->ifm_flags & IFF_UP) &&
80 (!m->server->config.use_iff_running || (ifm->ifm_flags & IFF_RUNNING)) &&
81 !(ifm->ifm_flags & IFF_LOOPBACK) &&
82 (ifm->ifm_flags & IFF_MULTICAST) &&
83 (m->server->config.allow_point_to_point || !(ifinfomsg->ifi_flags & IFF_POINTOPOINT));
86 hw->name = avahi_strndup(sdl->sdl_data, sdl->sdl_nlen);
88 hw->mtu = ifm->ifm_data.ifi_mtu;
90 hw->mac_address_size = sdl->sdl_alen;
91 if (hw->mac_address_size > AVAHI_MAC_ADDRESS_MAX)
92 hw->mac_address_size = AVAHI_MAC_ADDRESS_MAX;
94 memcpy(hw->mac_address, sdl->sdl_data + sdl->sdl_nlen, hw->mac_address_size);
98 /* avahi_log_debug("======\n name: %s\n index:%d\n mtu:%d\n mac:%s\n flags_ok:%d\n======", */
99 /* hw->name, hw->index, */
101 /* avahi_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size), */
105 avahi_hw_interface_check_relevant(hw);
106 avahi_hw_interface_update_rrs(hw, 0);
110 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
111 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
113 static void rtm_addr(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
115 AvahiInterface *iface;
118 struct ifa_msghdr *ifam = (struct ifa_msghdr *) rtm;
119 char *cp = (char *)(ifam + 1);
120 int addrs = ifam->ifam_addrs;
123 struct sockaddr *sa =NULL;
125 #if defined(__NetBSD__) || defined(__OpenBSD__)
126 if(((struct sockaddr *)cp)->sa_family == AF_UNSPEC)
127 ((struct sockaddr *)cp)->sa_family = AF_INET;
130 if(((struct sockaddr *)cp)->sa_family != AF_INET && ((struct sockaddr *)cp)->sa_family != AF_INET6)
133 if (!(iface = avahi_interface_monitor_get_interface(m, (AvahiIfIndex) ifam->ifam_index, avahi_af_to_proto(((struct sockaddr *)cp)->sa_family))))
136 raddr.proto = avahi_af_to_proto(((struct sockaddr *)cp)->sa_family);
138 for(i = 0; addrs != 0 && i < RTAX_MAX; addrs &= ~(1<<i), i++)
142 sa = (struct sockaddr *)cp;
145 switch(sa->sa_family) {
149 prefixlen = bitcount((unsigned int)((struct sockaddr_in *)sa)->sin_addr.s_addr);
152 memcpy(raddr.data.data, &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr));
161 prefixlen = bitcount((unsigned int)((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr);
164 memcpy(raddr.data.data, &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr));
183 if(rtm->rtm_type == RTM_NEWADDR)
185 AvahiInterfaceAddress *addriface;
186 if (!(addriface = avahi_interface_monitor_get_address(m, iface, &raddr)))
187 if (!(addriface = avahi_interface_address_new(m, iface, &raddr, prefixlen)))
190 /* addriface->global_scope = ifaddrmsg->ifa_scope == RT_SCOPE_UNIVERSE || ifaddrmsg->ifa_scope == RT_SCOPE_SITE; */
191 addriface->global_scope = 1;
195 AvahiInterfaceAddress *addriface;
196 assert(rtm->rtm_type == RTM_DELADDR);
197 if (!(addriface = avahi_interface_monitor_get_address(m, iface, &raddr)))
199 avahi_interface_address_free(addriface);
202 avahi_interface_check_relevant(iface);
203 avahi_interface_update_rrs(iface, 0);
206 static void parse_rtmsg(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
211 if (rtm->rtm_version != RTM_VERSION) {
212 avahi_log_warn("routing message version %d not understood",
217 switch (rtm->rtm_type) {
230 static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event,void *userdata) {
231 AvahiInterfaceMonitor *m = (AvahiInterfaceMonitor *)userdata;
232 AvahiPfRoute *nl = m->osdep.pfroute;
239 assert(fd == nl->fd);
242 if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
243 if (errno == EAGAIN || errno == EINTR)
245 avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
248 parse_rtmsg((struct rt_msghdr *)msg, m);
253 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
255 m->osdep.pfroute = NULL;
259 if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
260 avahi_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
264 if (!(m->osdep.pfroute = avahi_new(AvahiPfRoute , 1))) {
265 avahi_log_error(__FILE__": avahi_new() failed.");
268 m->osdep.pfroute->fd = fd;
270 if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api,
271 m->osdep.pfroute->fd,
275 avahi_log_error(__FILE__": Failed to create watch.");
283 if (m->osdep.pfroute) {
284 if (m->osdep.pfroute->watch)
285 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
290 m->osdep.pfroute = NULL;
296 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
299 if (m->osdep.pfroute) {
300 if (m->osdep.pfroute->watch)
301 m->server->poll_api->watch_free(m->osdep.pfroute->watch);
303 if (m->osdep.pfroute->fd >= 0)
304 close(m->osdep.pfroute->fd);
306 avahi_free(m->osdep.pfroute);
307 m->osdep.pfroute = NULL;
311 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
314 char *buf, *lim, *next, count = 0;
315 struct rt_msghdr *rtm;
322 mib[2] = 0; /* protocol */
323 mib[3] = 0; /* wildcard address family */
324 mib[4] = NET_RT_IFLIST;
325 mib[5] = 0; /* no flags */
326 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
328 avahi_log_error("sysctl failed: %s", strerror(errno));
329 avahi_log_error("route-sysctl-estimate");
332 if ((buf = avahi_malloc(needed)) == NULL)
334 avahi_log_error("malloc failed in avahi_interface_monitor_sync");
337 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
338 avahi_log_warn("sysctl failed: %s", strerror(errno));
339 if (errno == ENOMEM && count++ < 10) {
340 avahi_log_warn("Routing table grew, retrying");
347 for (next = buf; next < lim; next += rtm->rtm_msglen) {
348 rtm = (struct rt_msghdr *)next;
352 m->list_complete = 1;
353 avahi_interface_monitor_check_relevant(m);
354 avahi_interface_monitor_update_rrs(m, 0);
355 avahi_log_info("Network interface enumeration completed.");