]> git.meshlink.io Git - catta/blob - avahi-core/iface-pfroute.c
* added some fixes mentioned by mezcalero
[catta] / avahi-core / iface-pfroute.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-common/malloc.h>
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <err.h>
34 #include <sysexits.h>
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <sys/sysctl.h>
40 #include <net/route.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <net/if_dl.h>
44
45 #include <arpa/inet.h>
46
47
48 #include "log.h"
49 #include "iface.h"
50 #include "iface-pfroute.h"
51 #include "util.h"
52
53 static void rtm_info(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
54 {
55   AvahiHwInterface *hw;
56   struct if_msghdr *ifm = (struct if_msghdr *)rtm;
57   struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
58   
59   if (sdl->sdl_family != AF_LINK)
60     return;
61   
62   if (ifm->ifm_addrs == 0 && ifm->ifm_index > 0) {
63     if (!(hw = avahi_interface_monitor_get_hw_interface(m, (AvahiIfIndex) ifm->ifm_index)))
64       return;
65     avahi_hw_interface_free(hw, 0);
66     return;
67   }
68   
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)))
71       return; /* OOM */
72   
73   hw->flags_ok =
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);
79   
80   avahi_free(hw->name);
81   hw->name = avahi_strndup(sdl->sdl_data, sdl->sdl_nlen);
82       
83   hw->mtu = ifm->ifm_data.ifi_mtu;
84   
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;
88   
89   memcpy(hw->mac_address, sdl->sdl_data + sdl->sdl_nlen, hw->mac_address_size);
90   
91   printf("======\n name: %s\n index:%d\n mtu:%d\n mac:%s\n flags_ok:%d\n======", 
92          hw->name, hw->index, 
93          hw->mtu, 
94          avahi_format_mac_address(hw->mac_address, hw->mac_address_size),
95          hw->flags_ok);
96   
97   avahi_hw_interface_check_relevant(hw);
98   avahi_hw_interface_update_rrs(hw, 0);
99 }
100
101 #define ROUNDUP(a) \
102      ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
103 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
104
105 static void rtm_addr(struct rt_msghdr *rtm, AvahiInterfaceMonitor *m)
106 {
107   AvahiInterface *iface;
108   AvahiAddress raddr;
109   int raddr_valid = 0;
110   struct ifa_msghdr *ifam = (struct ifa_msghdr *) rtm;
111   char *cp = (char *)(ifam + 1);
112   int addrs = ifam->ifam_addrs;
113   int i;
114   struct sockaddr *sa  =NULL;
115   struct sockaddr *addr  =NULL;
116   struct sockaddr *mask  =NULL;
117
118   for(i = 0; addrs != 0 && i < RTAX_MAX; addrs &= ~(1<<i), i++)
119     {
120       if (!(addrs & 1<<i))
121         continue;
122       sa = (struct sockaddr *)cp;
123       if (sa->sa_len == 0) 
124         continue;
125       switch(sa->sa_family) {
126       case AF_INET:
127         switch (1<<i) {
128         case RTA_NETMASK:
129           mask = sa;
130           break;
131         case RTA_IFA:
132           addr = sa;
133         default:
134           break;
135         }
136       case AF_INET6:
137         break;
138       default:
139         break;
140       }
141 #ifdef SA_SIZE
142       cp += SA_SIZE(sa);
143 #else
144       ADVANCE(cp, sa);
145 #endif
146     }
147
148   if(addr)
149     {
150       struct in_addr in = ((struct sockaddr_in *)addr)->sin_addr;
151       printf("\nRTA_IFA %s\n", inet_ntoa(in)); 
152     }
153   if(mask)
154     {
155       struct in_addr in = ((struct sockaddr_in *)mask)->sin_addr;
156       printf("\nRTA_NETMASK %s\n", inet_ntoa(in)); 
157     }
158
159
160 /*   if(rtm->rtm_type == RTM_NEWADDR) */
161 /*     { */
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 *\/ */
166       
167 /*       addr->global_scope = ifaddrmsg->ifa_scope == RT_SCOPE_UNIVERSE || ifaddrmsg->ifa_scope == RT_SCOPE_SITE; */
168 /*     } */
169 /*   else */
170 /*     { */
171 /*       AvahiInterfaceAddress *addr; */
172 /*       assert(rtm->rtm_type == RTM_DELADDR); */
173 /*       if (!(addr = avahi_interface_monitor_get_address(m, i, &raddr))) */
174 /*      return; */
175 /*       avahi_interface_address_free(addr); */
176 /*     } */
177   
178 /*   avahi_interface_check_relevant(iface); */
179 /*   avahi_interface_update_rrs(iface, 0); */
180 }
181
182 static void parse_rtmsg(struct rt_msghdr *rtm, int msglen, AvahiInterfaceMonitor *m)
183 {
184   assert(m);
185   assert(rtm);
186   
187   if (rtm->rtm_version != RTM_VERSION) {
188     avahi_log_warn("routing message version %d not understood\n",
189                       rtm->rtm_version);
190     return;
191   }
192
193   switch (rtm->rtm_type) {
194   case RTM_IFINFO:
195     rtm_info(rtm,m);
196     break;
197   case RTM_NEWADDR:
198   case RTM_DELADDR:
199     rtm_addr(rtm,m);
200     break;
201     
202   default:
203     break;
204   }
205 }
206
207
208 static int wild_dump_interfaces(AvahiInterfaceMonitor *m)
209 {
210   size_t needed;
211   int mib[6];
212   char *buf, *lim, *next, count = 0;
213   struct rt_msghdr *rtm;
214
215  retry2:
216   mib[0] = CTL_NET;
217   mib[1] = PF_ROUTE;
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");
229       sleep(1);
230       free(buf);
231       goto retry2;
232     }
233     err(EX_OSERR, "actual retrieval of interface table");
234   }
235   lim = buf + needed;
236   for (next = buf; next < lim; next += rtm->rtm_msglen) {
237     rtm = (struct rt_msghdr *)next;
238     parse_rtmsg(rtm, rtm->rtm_msglen, m);
239   }
240   return 0;
241 }
242
243 static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event,void *userdata) {
244   AvahiInterfaceMonitor *m = (AvahiInterfaceMonitor *)userdata;
245   AvahiPfRoute *nl = m->osdep.pfroute;
246     ssize_t bytes;
247     char msg[2048];
248
249     assert(m);
250     assert(w);
251     assert(nl);
252     assert(fd == nl->fd);
253
254     do {
255       time_t now = time(NULL);
256       if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
257         if (errno == EAGAIN || errno == EINTR)
258           return;
259         avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
260         return;
261       }
262
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);
266     }
267     while (bytes > 0);
268 }
269
270 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
271     int fd = -1;
272     m->osdep.pfroute = NULL;
273
274     assert(m);
275
276     if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
277         avahi_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
278         goto fail;
279     }
280
281     if (!(m->osdep.pfroute = avahi_new(AvahiPfRoute , 1))) {
282         avahi_log_error(__FILE__": avahi_new() failed.");
283         goto fail;
284     }
285     m->osdep.pfroute->fd = fd;
286
287     if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api, 
288                                                                    m->osdep.pfroute->fd, 
289                                                                    AVAHI_WATCH_IN, 
290                                                                    socket_event, 
291                                                                    m))) {
292       avahi_log_error(__FILE__": Failed to create watch.");
293       goto fail;
294     }
295     
296     printf("avahi_interface_monitor_init_osdep\n");
297     return 0;
298
299 fail:
300
301     if (m->osdep.pfroute) {
302       if (m->osdep.pfroute->watch)
303         m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
304       
305       if (fd >= 0)
306         close(fd);
307       
308       m->osdep.pfroute = NULL;
309     }
310
311     return -1;
312 }
313
314 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
315     assert(m);
316
317     if (m->osdep.pfroute) {
318       if (m->osdep.pfroute->watch)
319         m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
320       
321       if (m->osdep.pfroute->fd >= 0)
322         close(m->osdep.pfroute->fd);
323
324       avahi_free(m->osdep.pfroute);
325       m->osdep.pfroute = NULL;
326     }
327 }
328
329 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
330     assert(m);
331
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.");
337 }
338