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>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <net/route.h>
42 #include <netinet/in.h>
43 #include <net/if_dl.h>
45 #include <arpa/inet.h>
50 #include "iface-pfroute.h"
53 static void rtm_info(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
56 struct if_msghdr *ifm = (struct if_msghdr *)rtm;
57 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
59 if (sdl->sdl_family != AF_LINK)
62 if (ifm->ifm_addrs == 0 && ifm->ifm_index > 0) {
63 if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifm->ifm_index)))
65 avahi_hw_interface_free(hw, 0);
69 if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifm->ifm_index)))
70 if (!(hw = avahi_hw_interface_new(m, (AvahiIfIndex) ifm->ifm_index)))
74 (ifm->ifm_flags & IFF_UP) &&
75 (!m->server->config.use_iff_running || (ifm->ifm_flags & IFF_RUNNING)) &&
76 !(ifm->ifm_flags & IFF_LOOPBACK) &&
77 (ifm->ifm_flags & IFF_MULTICAST) &&
78 !(ifm->ifm_flags & IFF_POINTOPOINT);
81 hw->name = avahi_strndup(sdl->sdl_data, sdl->sdl_nlen);
83 hw->mtu = ifm->ifm_data.ifi_mtu;
85 hw->mac_address_size = sdl->sdl_alen;
86 if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS)
87 hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS;
89 memcpy(hw->mac_address, sdl->sdl_data + sdl->sdl_nlen, hw->mac_address_size);
91 printf("======\n name: %s\n index:%d\n mtu:%d\n mac:%s\n flags_ok:%d\n======",
94 avahi_format_mac_address(hw->mac_address, hw->mac_address_size),
97 avahi_hw_interface_check_relevant(hw);
98 avahi_hw_interface_update_rrs(hw, 0);
102 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
103 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
105 static void rtm_addr(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
107 AvahiInterface *iface;
110 struct ifa_msghdr *ifam = (struct ifa_msghdr *) rtm;
111 char *cp = (char *)(ifam + 1);
112 int addrs = ifam->ifam_addrs;
114 struct sockaddr *sa =NULL;
115 struct sockaddr *addr =NULL;
116 struct sockaddr *mask =NULL;
118 for(i = 0; addrs != 0 && i < RTAX_MAX; addrs &= ~(1<<i), i++)
122 sa = (struct sockaddr *)cp;
125 switch(sa->sa_family) {
150 struct in_addr in = ((struct sockaddr_in *)addr)->sin_addr;
151 printf("\nRTA_IFA %s\n", inet_ntoa(in));
155 struct in_addr in = ((struct sockaddr_in *)mask)->sin_addr;
156 printf("\nRTA_NETMASK %s\n", inet_ntoa(in));
160 /* if(rtm->rtm_type == RTM_NEWADDR) */
162 /* AvahiInterfaceAddress *addr; */
163 /* if (!(addr = avahi_interface_monitor_get_address(m, i, &raddr))) */
164 /* if (!(addr = avahi_interface_address_new(m, i, &raddr, ifaddrmsg->ifa_prefixlen))) */
165 /* return; /\* OOM *\/ */
167 /* addr->global_scope = ifaddrmsg->ifa_scope == RT_SCOPE_UNIVERSE || ifaddrmsg->ifa_scope == RT_SCOPE_SITE; */
171 /* AvahiInterfaceAddress *addr; */
172 /* assert(rtm->rtm_type == RTM_DELADDR); */
173 /* if (!(addr = avahi_interface_monitor_get_address(m, i, &raddr))) */
175 /* avahi_interface_address_free(addr); */
178 /* avahi_interface_check_relevant(iface); */
179 /* avahi_interface_update_rrs(iface, 0); */
182 static void parse_rtmsg(struct rt_msghdr *rtm, int msglen, AvahiInterfaceMonitor *m)
187 if (rtm->rtm_version != RTM_VERSION) {
188 avahi_log_warn("routing message version %d not understood\n",
193 switch (rtm->rtm_type) {
208 static int wild_dump_interfaces(AvahiInterfaceMonitor *m)
212 char *buf, *lim, *next, count = 0;
213 struct rt_msghdr *rtm;
218 mib[2] = 0; /* protocol */
219 mib[3] = 0; /* wildcard address family */
220 mib[4] = NET_RT_IFLIST;
221 mib[5] = 0; /* no flags */
222 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
223 err(EX_OSERR, "route-sysctl-estimate");
224 if ((buf = malloc(needed)) == NULL)
225 errx(EX_OSERR, "malloc failed");
226 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
227 if (errno == ENOMEM && count++ < 10) {
228 warnx("Routing table grew, retrying");
233 err(EX_OSERR, "actual retrieval of interface table");
236 for (next = buf; next < lim; next += rtm->rtm_msglen) {
237 rtm = (struct rt_msghdr *)next;
238 parse_rtmsg(rtm, rtm->rtm_msglen, m);
243 static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event,void *userdata) {
244 AvahiInterfaceMonitor *m = (AvahiInterfaceMonitor *)userdata;
245 AvahiPfRoute *nl = m->osdep.pfroute;
252 assert(fd == nl->fd);
255 time_t now = time(NULL);
256 if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
257 if (errno == EAGAIN || errno == EINTR)
259 avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
263 (void) printf("\ngot message of size %d on %s", (int)bytes, ctime(&now));
264 printf("parse_rtmsg\n");
265 parse_rtmsg((struct rt_msghdr *)msg, bytes ,m);
270 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
272 m->osdep.pfroute = NULL;
276 if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
277 avahi_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
281 if (!(m->osdep.pfroute = avahi_new(AvahiPfRoute , 1))) {
282 avahi_log_error(__FILE__": avahi_new() failed.");
285 m->osdep.pfroute->fd = fd;
287 if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api,
288 m->osdep.pfroute->fd,
292 avahi_log_error(__FILE__": Failed to create watch.");
296 printf("avahi_interface_monitor_init_osdep\n");
301 if (m->osdep.pfroute) {
302 if (m->osdep.pfroute->watch)
303 m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
308 m->osdep.pfroute = NULL;
314 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
317 if (m->osdep.pfroute) {
318 if (m->osdep.pfroute->watch)
319 m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
321 if (m->osdep.pfroute->fd >= 0)
322 close(m->osdep.pfroute->fd);
324 avahi_free(m->osdep.pfroute);
325 m->osdep.pfroute = NULL;
329 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
332 wild_dump_interfaces(m);
333 m->list_complete = 1;
334 avahi_interface_monitor_check_relevant(m);
335 avahi_interface_monitor_update_rrs(m, 0);
336 avahi_log_info("Network interface enumeration completed.");