]> git.meshlink.io Git - catta/blob - avahi-core/iface-pfroute.c
* more portability work
[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
115   if(addrs == 0)
116     return;
117
118   for(i = 1; i; i <<=1)
119     {
120       struct sockaddr *sa;
121       sa = (struct sockaddr *)cp;
122
123       /* interface addr sockaddr present */
124       if (i & addrs && i & RTA_BRD)
125         {
126           if(sa->sa_family == AF_INET)
127             {
128               struct in_addr in = ((struct sockaddr_in *)sa)->sin_addr;
129               printf("\n%s\n",
130                      inet_ntoa(in)); 
131             }
132           else
133             if(sa->sa_family == AF_INET6)
134               {
135                 /*          struct sockaddr_in6 sin6; */
136                 
137               }
138 #ifdef SA_SIZE
139           cp += SA_SIZE(sa);
140 #else
141           ADVANCE(cp, sa);
142 #endif
143
144         }
145     }
146 }
147
148 static void parse_rtmsg(struct rt_msghdr *rtm, int msglen, AvahiInterfaceMonitor *m)
149 {
150   assert(m);
151   assert(rtm);
152   
153   if (rtm->rtm_version != RTM_VERSION) {
154     avahi_log_warn("routing message version %d not understood\n",
155                       rtm->rtm_version);
156     return;
157   }
158
159   switch (rtm->rtm_type) {
160   case RTM_IFINFO:
161     rtm_info(rtm,m);
162     break;
163   case RTM_NEWADDR:
164   case RTM_DELADDR:
165     rtm_addr(rtm,m);
166     break;
167     
168   default:
169     break;
170   }
171 }
172
173
174 static int wild_dump_interfaces(AvahiInterfaceMonitor *m)
175 {
176   size_t needed;
177   int mib[6];
178   char *buf, *lim, *next, count = 0;
179   struct rt_msghdr *rtm;
180
181  retry2:
182   mib[0] = CTL_NET;
183   mib[1] = PF_ROUTE;
184   mib[2] = 0;             /* protocol */
185   mib[3] = 0;             /* wildcard address family */
186   mib[4] = NET_RT_IFLIST;
187   mib[5] = 0;             /* no flags */
188   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
189     err(EX_OSERR, "route-sysctl-estimate");
190   if ((buf = malloc(needed)) == NULL)
191     errx(EX_OSERR, "malloc failed");
192   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
193     if (errno == ENOMEM && count++ < 10) {
194       warnx("Routing table grew, retrying");
195       sleep(1);
196       free(buf);
197       goto retry2;
198     }
199     err(EX_OSERR, "actual retrieval of interface table");
200   }
201   lim = buf + needed;
202   for (next = buf; next < lim; next += rtm->rtm_msglen) {
203     rtm = (struct rt_msghdr *)next;
204     parse_rtmsg(rtm, rtm->rtm_msglen, m);
205   }
206   return 0;
207 }
208
209 static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event,void *userdata) {
210   AvahiInterfaceMonitor *m = (AvahiInterfaceMonitor *)userdata;
211   AvahiPfRoute *nl = m->osdep.pfroute;
212     ssize_t bytes;
213     char msg[2048];
214
215     assert(m);
216     assert(w);
217     assert(nl);
218     assert(fd == nl->fd);
219
220     do {
221       time_t now = time(NULL);
222       if((bytes = recv(nl->fd, msg, 2048, MSG_DONTWAIT)) < 0) {
223         if (errno == EAGAIN || errno == EINTR)
224           return;
225         avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
226         return;
227       }
228
229       (void) printf("\ngot message of size %d on %s", bytes, ctime(&now));
230       printf("parse_rtmsg\n");
231       parse_rtmsg((struct rt_msghdr *)msg, bytes ,m);
232     }
233     while (bytes > 0);
234 }
235
236 int avahi_interface_monitor_init_osdep(AvahiInterfaceMonitor *m) {
237     int fd = -1;
238     m->osdep.pfroute = NULL;
239
240     assert(m);
241
242     if ((fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) {
243         avahi_log_error(__FILE__": socket(PF_ROUTE): %s", strerror(errno));
244         goto fail;
245     }
246
247     if (!(m->osdep.pfroute = avahi_new(AvahiPfRoute , 1))) {
248         avahi_log_error(__FILE__": avahi_new() failed.");
249         goto fail;
250     }
251     m->osdep.pfroute->fd = fd;
252
253     if (!(m->osdep.pfroute->watch = m->server->poll_api->watch_new(m->server->poll_api, 
254                                                                    m->osdep.pfroute->fd, 
255                                                                    AVAHI_WATCH_IN, 
256                                                                    socket_event, 
257                                                                    m))) {
258       avahi_log_error(__FILE__": Failed to create watch.");
259       goto fail;
260     }
261     
262     printf("avahi_interface_monitor_init_osdep\n");
263     return 0;
264
265 fail:
266
267     if (m->osdep.pfroute) {
268       if (m->osdep.pfroute->watch)
269         m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
270       
271       if (fd >= 0)
272         close(fd);
273       
274       m->osdep.pfroute = NULL;
275     }
276
277     return -1;
278 }
279
280 void avahi_interface_monitor_free_osdep(AvahiInterfaceMonitor *m) {
281     assert(m);
282
283     if (m->osdep.pfroute) {
284       if (m->osdep.pfroute->watch)
285         m->osdep.pfroute->poll_api->watch_free(m->osdep.pfroute->watch);
286       
287       if (m->osdep.pfroute->fd >= 0)
288         close(m->osdep.pfroute->fd);
289       
290       m->osdep.pfroute = NULL;
291     }
292 }
293
294 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
295     assert(m);
296
297     wild_dump_interfaces(m);
298
299 }
300