]> git.meshlink.io Git - catta/blob - avahi-core/iface.c
* split packet scheduler into three seperate parts
[catta] / avahi-core / iface.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 <string.h>
27 #include <sys/socket.h>
28 #include <asm/types.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #include <errno.h>
32 #include <net/if.h>
33
34 #include "iface.h"
35 #include "netlink.h"
36 #include "dns.h"
37 #include "socket.h"
38 #include "announce.h"
39
40 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove) {
41     g_assert(m);
42     g_assert(a);
43
44     if (!avahi_interface_address_relevant(a) || remove) {
45         if (a->entry_group) {
46             avahi_entry_group_free(a->entry_group);
47             a->entry_group = NULL;
48         }
49     } else {
50         if (!a->entry_group) {
51             a->entry_group = avahi_entry_group_new(m->server, NULL, NULL);
52             avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address); 
53             avahi_entry_group_commit(a->entry_group);
54         }
55     }
56 }
57
58 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove) {
59     AvahiInterfaceAddress *a;
60     g_assert(m);
61     g_assert(i);
62
63     for (a = i->addresses; a; a = a->address_next)
64         update_address_rr(m, a, remove);
65 }
66
67 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove) {
68     AvahiInterface *i;
69
70     g_assert(m);
71     g_assert(hw);
72
73     for (i = hw->interfaces; i; i = i->by_hardware_next)
74         update_interface_rr(m, i, remove);
75 }
76
77 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
78     g_assert(m);
79     g_assert(a);
80     g_assert(a->interface);
81
82     AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
83
84     if (a->entry_group)
85         avahi_entry_group_free(a->entry_group);
86     
87     g_free(a);
88 }
89
90 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
91     g_assert(m);
92     g_assert(i);
93
94     g_message("removing interface %s.%i", i->hardware->name, i->protocol);
95     avahi_goodbye_interface(m->server, i, send_goodbye);
96     g_message("forcing responses...");
97     avahi_response_scheduler_force(i->response_scheduler);
98     g_message("done");
99     
100     g_assert(!i->announcements);
101
102     while (i->addresses)
103         free_address(m, i->addresses);
104
105     avahi_response_scheduler_free(i->response_scheduler);
106     avahi_query_scheduler_free(i->query_scheduler);
107     avahi_probe_scheduler_free(i->probe_scheduler);
108     avahi_cache_free(i->cache);
109     
110     AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
111     AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
112     
113     g_free(i);
114 }
115
116 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
117     g_assert(m);
118     g_assert(hw);
119
120     while (hw->interfaces)
121         free_interface(m, hw->interfaces, send_goodbye);
122
123     AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
124     g_hash_table_remove(m->hash_table, &hw->index);
125
126     g_free(hw->name);
127     g_free(hw);
128 }
129
130 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
131     AvahiInterfaceAddress *ia;
132     
133     g_assert(m);
134     g_assert(i);
135     g_assert(raddr);
136
137     for (ia = i->addresses; ia; ia = ia->address_next)
138         if (avahi_address_cmp(&ia->address, raddr) == 0)
139             return ia;
140
141     return NULL;
142 }
143
144 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
145     struct nlmsghdr *n;
146     struct rtgenmsg *gen;
147     guint8 req[1024];
148     
149     memset(&req, 0, sizeof(req));
150     n = (struct nlmsghdr*) req;
151     n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
152     n->nlmsg_type = type;
153     n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
154     n->nlmsg_pid = 0;
155
156     gen = NLMSG_DATA(n);
157     memset(gen, 0, sizeof(struct rtgenmsg));
158     gen->rtgen_family = AF_UNSPEC;
159
160     return avahi_netlink_send(nl, n, ret_seq);
161 }
162
163 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, guchar protocol) {
164     AvahiInterface *i;
165     
166     g_assert(m);
167     g_assert(hw);
168     g_assert(protocol != AF_UNSPEC);
169
170     i = g_new(AvahiInterface, 1);
171     i->monitor = m;
172     i->hardware = hw;
173     i->protocol = protocol;
174     i->announcing = FALSE;
175
176     AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
177     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
178
179     i->cache = avahi_cache_new(m->server, i);
180     i->response_scheduler = avahi_response_scheduler_new(i);
181     i->query_scheduler = avahi_query_scheduler_new(i);
182     i->probe_scheduler = avahi_probe_scheduler_new(i);
183
184     AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
185     AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
186 }
187
188 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
189     gboolean b;
190
191     g_assert(m);
192     g_assert(i);
193
194     b = avahi_interface_relevant(i);
195
196     if (b && !i->announcing) {
197         g_message("New relevant interface %s.%i", i->hardware->name, i->protocol);
198
199         if (i->protocol == AF_INET)
200             avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4);
201         if (i->protocol == AF_INET6)
202             avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6);
203
204         i->announcing = TRUE;
205         avahi_announce_interface(m->server, i);
206     } else if (!b && i->announcing) {
207         g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
208
209         if (i->protocol == AF_INET)
210             avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4);
211         if (i->protocol == AF_INET6)
212             avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6);
213
214         avahi_goodbye_interface(m->server, i, FALSE);
215         avahi_response_scheduler_clear(i->response_scheduler);
216         avahi_query_scheduler_clear(i->query_scheduler);
217         avahi_probe_scheduler_clear(i->probe_scheduler);
218         avahi_cache_flush(i->cache);
219
220         i->announcing = FALSE;
221     }
222 }
223
224 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
225     AvahiInterface *i;
226     
227     g_assert(m);
228     g_assert(hw);
229
230     for (i = hw->interfaces; i; i = i->by_hardware_next)
231         check_interface_relevant(m, i);
232 }
233
234 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
235     AvahiInterfaceMonitor *m = userdata;
236     
237     g_assert(m);
238     g_assert(n);
239     g_assert(m->netlink == nl);
240
241     if (n->nlmsg_type == RTM_NEWLINK) {
242         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
243         AvahiHwInterface *hw;
244         struct rtattr *a = NULL;
245         size_t l;
246
247         if (ifinfomsg->ifi_family != AF_UNSPEC)
248             return;
249
250         if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
251             hw = g_new(AvahiHwInterface, 1);
252             hw->monitor = m;
253             hw->name = NULL;
254             hw->flags = 0;
255             hw->mtu = 1500;
256             hw->index = ifinfomsg->ifi_index;
257
258             AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
259             AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
260             
261             g_hash_table_insert(m->hash_table, &hw->index, hw);
262
263             if (m->server->fd_ipv4 >= 0)
264                 new_interface(m, hw, AF_INET);
265             if (m->server->fd_ipv6 >= 0)
266                 new_interface(m, hw, AF_INET6);
267         }
268         
269         hw->flags = ifinfomsg->ifi_flags;
270
271         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
272         a = IFLA_RTA(ifinfomsg);
273
274         while (RTA_OK(a, l)) {
275             switch(a->rta_type) {
276                 case IFLA_IFNAME:
277                     g_free(hw->name);
278                     hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
279                     break;
280
281                 case IFLA_MTU:
282                     g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
283                     hw->mtu = *((unsigned int*) RTA_DATA(a));
284                     break;
285                     
286                 default:
287                     ;
288             }
289
290             a = RTA_NEXT(a, l);
291         }
292
293         update_hw_interface_rr(m, hw, FALSE);
294         check_hw_interface_relevant(m, hw);
295         
296     } else if (n->nlmsg_type == RTM_DELLINK) {
297         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
298         AvahiHwInterface *hw;
299
300         if (ifinfomsg->ifi_family != AF_UNSPEC)
301             return;
302         
303         if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
304             return;
305
306         update_hw_interface_rr(m, hw, TRUE);
307         free_hw_interface(m, hw, FALSE);
308         
309     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
310
311         struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
312         AvahiInterface *i;
313         struct rtattr *a = NULL;
314         size_t l;
315         AvahiAddress raddr;
316         int raddr_valid = 0;
317
318         if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
319             return;
320
321         if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
322             return;
323
324         raddr.family = ifaddrmsg->ifa_family;
325
326         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
327         a = IFA_RTA(ifaddrmsg);
328
329         while (RTA_OK(a, l)) {
330             switch(a->rta_type) {
331                 case IFA_ADDRESS:
332                     if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
333                         (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
334                         return;
335
336                     memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
337                     raddr_valid = 1;
338
339                     break;
340                     
341                 default:
342                     ;
343             }
344             
345             a = RTA_NEXT(a, l);
346         }
347
348         
349         if (!raddr_valid)
350             return;
351
352         if (n->nlmsg_type == RTM_NEWADDR) {
353             AvahiInterfaceAddress *addr;
354             
355             if (!(addr = get_address(m, i, &raddr))) {
356                 addr = g_new(AvahiInterfaceAddress, 1);
357                 addr->monitor = m;
358                 addr->address = raddr;
359                 addr->interface = i;
360                 addr->entry_group = NULL;
361
362                 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
363             }
364             
365             addr->flags = ifaddrmsg->ifa_flags;
366             addr->scope = ifaddrmsg->ifa_scope;
367
368             update_address_rr(m, addr, FALSE);
369             check_interface_relevant(m, i);
370         } else {
371             AvahiInterfaceAddress *addr;
372             
373             if (!(addr = get_address(m, i, &raddr)))
374                 return;
375
376             update_address_rr(m, addr, TRUE);
377             free_address(m, addr);
378
379             check_interface_relevant(m, i);
380         }
381                 
382     } else if (n->nlmsg_type == NLMSG_DONE) {
383         
384         if (m->list == LIST_IFACE) {
385             m->list = LIST_DONE;
386             
387             if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
388                 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
389             else
390                 m->list = LIST_ADDR;
391         } else {
392             m->list = LIST_DONE;
393             g_message("Enumeration complete");
394         }
395         
396     } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
397         struct nlmsgerr *e = NLMSG_DATA (n);
398                     
399         if (e->error)
400             g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
401     }
402 }
403
404 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
405     AvahiInterfaceMonitor *m = NULL;
406
407     m = g_new0(AvahiInterfaceMonitor, 1);
408     m->server = s;
409     if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
410         goto fail;
411
412     m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
413
414     AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
415     AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
416
417     if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
418         goto fail;
419
420     m->list = LIST_IFACE;
421
422     return m;
423
424 fail:
425     avahi_interface_monitor_free(m);
426     return NULL;
427 }
428
429 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
430     g_assert(m);
431     
432     while (m->list != LIST_DONE) {
433         if (!avahi_netlink_work(m->netlink, TRUE))
434             break;
435     } 
436 }
437
438 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
439     g_assert(m);
440
441     while (m->hw_interfaces)
442         free_hw_interface(m, m->hw_interfaces, TRUE);
443
444     g_assert(!m->interfaces);
445
446     
447     if (m->netlink)
448         avahi_netlink_free(m->netlink);
449     
450     if (m->hash_table)
451         g_hash_table_destroy(m->hash_table);
452
453     g_free(m);
454 }
455
456
457 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, gint index, guchar protocol) {
458     AvahiHwInterface *hw;
459     AvahiInterface *i;
460     
461     g_assert(m);
462     g_assert(index > 0);
463     g_assert(protocol != AF_UNSPEC);
464
465     if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
466         return NULL;
467
468     for (i = hw->interfaces; i; i = i->by_hardware_next)
469         if (i->protocol == protocol)
470             return i;
471
472     return NULL;
473 }
474
475 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, gint index) {
476     g_assert(m);
477     g_assert(index > 0);
478
479     return g_hash_table_lookup(m->hash_table, &index);
480 }
481
482
483 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
484     g_assert(i);
485     g_assert(p);
486     char t[64];
487
488     if (!avahi_interface_relevant(i))
489         return;
490     
491     g_assert(!a || a->family == i->protocol);
492
493     if (a)
494         g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port);
495     else
496         g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol);
497     
498     if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0)
499         avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
500     else if (i->protocol == AF_INET6 && i->monitor->server->fd_ipv6 >= 0)
501         avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
502 }
503
504 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
505     g_assert(i);
506     g_assert(p);
507
508     avahi_interface_send_packet_unicast(i, p, NULL, 0);
509 }
510
511 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
512     g_assert(i);
513     g_assert(key);
514
515     if (avahi_interface_relevant(i))
516         return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
517
518     return FALSE;
519 }
520
521 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
522     g_assert(i);
523     g_assert(record);
524
525     if (avahi_interface_relevant(i))
526         return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
527
528     return FALSE;
529 }
530
531 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
532     g_assert(i);
533     g_assert(record);
534     
535     if (avahi_interface_relevant(i))
536         return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
537
538     return FALSE;
539 }
540
541 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
542     AvahiInterface *i;
543     g_assert(m);
544
545     for (i = m->interfaces; i; i = i->interface_next) {
546         if (avahi_interface_relevant(i)) {
547             fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
548             avahi_cache_dump(i->cache, f);
549         }
550     }
551     fprintf(f, "\n");
552 }
553
554 gboolean avahi_interface_relevant(AvahiInterface *i) {
555     g_assert(i);
556
557     return
558         (i->hardware->flags & IFF_UP) &&
559         (i->hardware->flags & IFF_RUNNING) &&
560         !(i->hardware->flags & IFF_LOOPBACK) &&
561         (i->hardware->flags & IFF_MULTICAST) &&
562         i->addresses;
563 }
564
565 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { 
566     g_assert(a);
567
568     return a->scope == RT_SCOPE_UNIVERSE;
569 }
570
571
572 gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) {
573     g_assert(i);
574     
575     if (index > 0 && index != i->hardware->index)
576         return FALSE;
577
578     if (protocol != AF_UNSPEC && protocol != i->protocol)
579         return FALSE;
580
581     return TRUE;
582 }
583
584
585 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
586     g_assert(m);
587     g_assert(callback);
588     
589     if (interface > 0) {
590         if (protocol != AF_UNSPEC) {
591             AvahiInterface *i;
592             
593             if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
594                 callback(m, i, userdata);
595             
596         } else {
597             AvahiHwInterface *hw;
598             AvahiInterface *i;
599
600             if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
601                 for (i = hw->interfaces; i; i = i->by_hardware_next)
602                     if (avahi_interface_match(i, interface, protocol))
603                         callback(m, i, userdata);
604         }
605         
606     } else {
607         AvahiInterface *i;
608         
609         for (i = m->interfaces; i; i = i->interface_next)
610             if (avahi_interface_match(i, interface, protocol))
611                 callback(m, i, userdata);
612     }
613 }