]> git.meshlink.io Git - catta/blob - avahi-core/iface.c
* implement new source address check mechanisms
[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 #include "util.h"
40 #include "log.h"
41
42 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove) {
43     g_assert(m);
44     g_assert(a);
45
46     if (avahi_interface_address_relevant(a) &&
47         !remove &&
48         m->server->config.publish_addresses &&
49         (m->server->state == AVAHI_SERVER_RUNNING ||
50         m->server->state == AVAHI_SERVER_REGISTERING)) {
51
52         if (!a->entry_group) {
53             a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
54             if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
55                 avahi_log_warn(__FILE__": avahi_server_add_address() failed.");
56                 avahi_entry_group_free(a->entry_group);
57                 a->entry_group = NULL;
58             } else
59                 avahi_entry_group_commit(a->entry_group);
60         }
61     } else {
62
63         if (a->entry_group) {
64
65             if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
66                 avahi_server_decrease_host_rr_pending(m->server);
67             
68             avahi_entry_group_free(a->entry_group);
69             a->entry_group = NULL;
70         }
71     } 
72 }
73
74 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove) {
75     AvahiInterfaceAddress *a;
76     
77     g_assert(m);
78     g_assert(i);
79
80     for (a = i->addresses; a; a = a->address_next)
81         update_address_rr(m, a, remove);
82 }
83
84 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove) {
85     AvahiInterface *i;
86
87     g_assert(m);
88     g_assert(hw);
89
90     for (i = hw->interfaces; i; i = i->by_hardware_next)
91         update_interface_rr(m, i, remove);
92
93     if (!remove &&
94         m->server->config.publish_workstation &&
95         (m->server->state == AVAHI_SERVER_RUNNING ||
96         m->server->state == AVAHI_SERVER_REGISTERING)) {
97
98         if (!hw->entry_group) {
99             gchar *name;
100             gchar *t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size);
101             name = g_strdup_printf("%s [%s]", m->server->host_name, t);
102             g_free(t);
103             
104             hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
105             if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, "_workstation._tcp", name, NULL, NULL, 9, NULL) < 0) { 
106                 avahi_log_warn(__FILE__": avahi_server_add_service() failed.");
107                 avahi_entry_group_free(hw->entry_group);
108                 hw->entry_group = NULL;
109             } else
110                 avahi_entry_group_commit(hw->entry_group);
111
112             g_free(name);
113         }
114         
115     } else {
116
117         if (hw->entry_group) {
118
119             if (avahi_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
120                 avahi_server_decrease_host_rr_pending(m->server);
121
122             avahi_entry_group_free(hw->entry_group);
123             hw->entry_group = NULL;
124         }
125     }
126 }
127
128 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
129     g_assert(m);
130     g_assert(a);
131     g_assert(a->interface);
132
133     update_address_rr(m, a, TRUE);
134     AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
135     
136     g_free(a);
137 }
138
139 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
140     g_assert(m);
141     g_assert(i);
142
143     avahi_goodbye_interface(m->server, i, send_goodbye);
144     avahi_response_scheduler_force(i->response_scheduler);
145     
146     g_assert(!i->announcements);
147
148     update_interface_rr(m, i, TRUE);
149     
150     while (i->addresses)
151         free_address(m, i->addresses);
152
153     avahi_response_scheduler_free(i->response_scheduler);
154     avahi_query_scheduler_free(i->query_scheduler);
155     avahi_probe_scheduler_free(i->probe_scheduler);
156     avahi_cache_free(i->cache);
157     
158     AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
159     AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
160     
161     g_free(i);
162 }
163
164 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
165     g_assert(m);
166     g_assert(hw);
167
168     update_hw_interface_rr(m, hw, TRUE);
169     
170     while (hw->interfaces)
171         free_interface(m, hw->interfaces, send_goodbye);
172
173     AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
174     g_hash_table_remove(m->hash_table, &hw->index);
175
176     g_free(hw->name);
177     g_free(hw);
178 }
179
180 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
181     AvahiInterfaceAddress *ia;
182     
183     g_assert(m);
184     g_assert(i);
185     g_assert(raddr);
186
187     for (ia = i->addresses; ia; ia = ia->address_next)
188         if (avahi_address_cmp(&ia->address, raddr) == 0)
189             return ia;
190
191     return NULL;
192 }
193
194 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
195     struct nlmsghdr *n;
196     struct rtgenmsg *gen;
197     guint8 req[1024];
198     
199     memset(&req, 0, sizeof(req));
200     n = (struct nlmsghdr*) req;
201     n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
202     n->nlmsg_type = type;
203     n->nlmsg_flags = NLM_F_ROOT/*|NLM_F_MATCH*/|NLM_F_REQUEST;
204     n->nlmsg_pid = 0;
205
206     gen = NLMSG_DATA(n);
207     memset(gen, 0, sizeof(struct rtgenmsg));
208     gen->rtgen_family = AF_UNSPEC;
209
210     return avahi_netlink_send(nl, n, ret_seq);
211 }
212
213 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
214     AvahiInterface *i;
215     
216     g_assert(m);
217     g_assert(hw);
218     g_assert(protocol != AVAHI_PROTO_UNSPEC);
219
220     i = g_new(AvahiInterface, 1);
221     i->monitor = m;
222     i->hardware = hw;
223     i->protocol = protocol;
224     i->announcing = FALSE;
225
226     AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
227     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
228
229     i->cache = avahi_cache_new(m->server, i);
230     i->response_scheduler = avahi_response_scheduler_new(i);
231     i->query_scheduler = avahi_query_scheduler_new(i);
232     i->probe_scheduler = avahi_probe_scheduler_new(i);
233
234     AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
235     AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
236 }
237
238 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
239     gboolean b;
240
241     g_assert(m);
242     g_assert(i);
243
244     b = avahi_interface_relevant(i);
245
246     if (b && !i->announcing) {
247         avahi_log_debug("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index);
248
249         if (i->protocol == AVAHI_PROTO_INET)
250             avahi_mdns_mcast_join_ipv4(i->hardware->index, m->server->fd_ipv4);
251         if (i->protocol == AVAHI_PROTO_INET6)
252             avahi_mdns_mcast_join_ipv6(i->hardware->index, m->server->fd_ipv6);
253
254         i->announcing = TRUE;
255         avahi_announce_interface(m->server, i);
256         avahi_browser_new_interface(m->server, i);
257     } else if (!b && i->announcing) {
258         avahi_log_debug("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
259
260         if (i->protocol == AVAHI_PROTO_INET)
261             avahi_mdns_mcast_leave_ipv4(i->hardware->index, m->server->fd_ipv4);
262         if (i->protocol == AVAHI_PROTO_INET6)
263             avahi_mdns_mcast_leave_ipv6(i->hardware->index, m->server->fd_ipv6);
264
265         avahi_goodbye_interface(m->server, i, FALSE);
266         avahi_response_scheduler_clear(i->response_scheduler);
267         avahi_query_scheduler_clear(i->query_scheduler);
268         avahi_probe_scheduler_clear(i->probe_scheduler);
269         avahi_cache_flush(i->cache);
270
271         i->announcing = FALSE;
272     }
273 }
274
275 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
276     AvahiInterface *i;
277     
278     g_assert(m);
279     g_assert(hw);
280
281     for (i = hw->interfaces; i; i = i->by_hardware_next)
282         check_interface_relevant(m, i);
283 }
284
285 static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
286     AvahiInterfaceMonitor *m = userdata;
287     
288     g_assert(m);
289     g_assert(n);
290     g_assert(m->netlink == nl);
291
292     if (n->nlmsg_type == RTM_NEWLINK) {
293         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
294         AvahiHwInterface *hw;
295         struct rtattr *a = NULL;
296         size_t l;
297
298         if (ifinfomsg->ifi_family != AF_UNSPEC)
299             return;
300
301         if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
302             hw = g_new(AvahiHwInterface, 1);
303             hw->monitor = m;
304             hw->name = NULL;
305             hw->flags = 0;
306             hw->mtu = 1500;
307             hw->index = ifinfomsg->ifi_index;
308             hw->mac_address_size = 0;
309             hw->entry_group = NULL;
310
311             AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
312             AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
313             
314             g_hash_table_insert(m->hash_table, &hw->index, hw);
315
316             if (m->server->fd_ipv4 >= 0)
317                 new_interface(m, hw, AVAHI_PROTO_INET);
318             if (m->server->fd_ipv6 >= 0)
319                 new_interface(m, hw, AVAHI_PROTO_INET6);
320         }
321         
322         hw->flags = ifinfomsg->ifi_flags;
323
324         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
325         a = IFLA_RTA(ifinfomsg);
326
327         while (RTA_OK(a, l)) {
328             switch(a->rta_type) {
329                 case IFLA_IFNAME:
330                     g_free(hw->name);
331                     hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
332                     break;
333
334                 case IFLA_MTU:
335                     g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
336                     hw->mtu = *((unsigned int*) RTA_DATA(a));
337                     break;
338
339                 case IFLA_ADDRESS: {
340                     hw->mac_address_size = RTA_PAYLOAD(a);
341                     if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS)
342                         hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS;
343                     
344                     memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size);
345                     break;
346                 }
347                     
348                 default:
349                     ;
350             }
351
352             a = RTA_NEXT(a, l);
353         }
354
355         update_hw_interface_rr(m, hw, FALSE);
356         check_hw_interface_relevant(m, hw);
357         
358     } else if (n->nlmsg_type == RTM_DELLINK) {
359         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
360         AvahiHwInterface *hw;
361
362         if (ifinfomsg->ifi_family != AF_UNSPEC)
363             return;
364         
365         if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
366             return;
367
368         update_hw_interface_rr(m, hw, TRUE);
369         free_hw_interface(m, hw, FALSE);
370         
371     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
372
373         struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
374         AvahiInterface *i;
375         struct rtattr *a = NULL;
376         size_t l;
377         AvahiAddress raddr;
378         gboolean raddr_valid = FALSE;
379
380         if (ifaddrmsg->ifa_family != AVAHI_PROTO_INET && ifaddrmsg->ifa_family != AVAHI_PROTO_INET6)
381             return;
382
383         if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
384             return;
385
386         raddr.family = ifaddrmsg->ifa_family;
387
388         l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg));
389         a = IFA_RTA(ifaddrmsg);
390
391         while (RTA_OK(a, l)) {
392
393             switch(a->rta_type) {
394                 case IFA_ADDRESS:
395                     if ((raddr.family == AVAHI_PROTO_INET6 && RTA_PAYLOAD(a) != 16) ||
396                         (raddr.family == AVAHI_PROTO_INET && RTA_PAYLOAD(a) != 4))
397                         return;
398
399                     memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
400                     raddr_valid = TRUE;
401
402                     break;
403
404                 default:
405                     ;
406             }
407             
408             a = RTA_NEXT(a, l);
409         }
410         
411         if (!raddr_valid)
412             return;
413
414         if (n->nlmsg_type == RTM_NEWADDR) {
415             AvahiInterfaceAddress *addr;
416             
417             if (!(addr = get_address(m, i, &raddr))) {
418                 addr = g_new(AvahiInterfaceAddress, 1);
419                 addr->monitor = m;
420                 addr->address = raddr;
421                 addr->interface = i;
422                 addr->entry_group = NULL;
423
424                 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
425             }
426             
427             addr->flags = ifaddrmsg->ifa_flags;
428             addr->scope = ifaddrmsg->ifa_scope;
429             addr->prefix_len = ifaddrmsg->ifa_prefixlen;
430
431             update_address_rr(m, addr, FALSE);
432             check_interface_relevant(m, i);
433         } else {
434             AvahiInterfaceAddress *addr;
435             
436             if (!(addr = get_address(m, i, &raddr)))
437                 return;
438
439             update_address_rr(m, addr, TRUE);
440             free_address(m, addr);
441
442             check_interface_relevant(m, i);
443         }
444                 
445     } else if (n->nlmsg_type == NLMSG_DONE) {
446         
447         if (m->list == LIST_IFACE) {
448             m->list = LIST_DONE;
449             
450             if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0)
451                 avahi_log_warn("NETLINK: Failed to list addrs: %s", strerror(errno));
452             else
453                 m->list = LIST_ADDR;
454         } else {
455             m->list = LIST_DONE;
456             avahi_log_debug("Enumeration complete");
457         }
458         
459     } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
460         struct nlmsgerr *e = NLMSG_DATA (n);
461                     
462         if (e->error)
463             avahi_log_warn("NETLINK: Failed to browse: %s", strerror(-e->error));
464     }
465 }
466
467 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
468     AvahiInterfaceMonitor *m = NULL;
469
470     m = g_new0(AvahiInterfaceMonitor, 1);
471     m->server = s;
472     if (!(m->netlink = avahi_netlink_new(s->context, G_PRIORITY_DEFAULT-10, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
473         goto fail;
474
475     m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
476
477     AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
478     AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
479
480     if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
481         goto fail;
482
483     m->list = LIST_IFACE;
484
485     return m;
486
487 fail:
488     avahi_interface_monitor_free(m);
489     return NULL;
490 }
491
492 void avahi_interface_monitor_sync(AvahiInterfaceMonitor *m) {
493     g_assert(m);
494     
495     while (m->list != LIST_DONE) {
496         if (!avahi_netlink_work(m->netlink, TRUE))
497             break;
498     } 
499 }
500
501 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
502     g_assert(m);
503
504     while (m->hw_interfaces)
505         free_hw_interface(m, m->hw_interfaces, TRUE);
506
507     g_assert(!m->interfaces);
508
509     
510     if (m->netlink)
511         avahi_netlink_free(m->netlink);
512     
513     if (m->hash_table)
514         g_hash_table_destroy(m->hash_table);
515
516     g_free(m);
517 }
518
519
520 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex index, AvahiProtocol protocol) {
521     AvahiHwInterface *hw;
522     AvahiInterface *i;
523     
524     g_assert(m);
525     g_assert(index > 0);
526     g_assert(protocol != AVAHI_PROTO_UNSPEC);
527
528     if (!(hw = avahi_interface_monitor_get_hw_interface(m, index)))
529         return NULL;
530
531     for (i = hw->interfaces; i; i = i->by_hardware_next)
532         if (i->protocol == protocol)
533             return i;
534
535     return NULL;
536 }
537
538 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex index) {
539     g_assert(m);
540     g_assert(index > 0);
541
542     return g_hash_table_lookup(m->hash_table, &index);
543 }
544
545
546 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) {
547     g_assert(i);
548     g_assert(p);
549 /*     char t[64]; */
550
551     if (!avahi_interface_relevant(i))
552         return;
553     
554     g_assert(!a || a->family == i->protocol);
555
556 /*     if (a) */
557 /*         avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
558 /*     else */
559 /*         avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
560     
561     if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
562         avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port);
563     else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
564         avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, a ? &a->data.ipv6 : NULL, port);
565 }
566
567 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
568     g_assert(i);
569     g_assert(p);
570
571     avahi_interface_send_packet_unicast(i, p, NULL, 0);
572 }
573
574 gboolean avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, gboolean immediately) {
575     g_assert(i);
576     g_assert(key);
577
578     if (avahi_interface_relevant(i))
579         return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
580
581     return FALSE;
582 }
583
584 gboolean avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
585     g_assert(i);
586     g_assert(record);
587
588     if (avahi_interface_relevant(i))
589         return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
590
591     return FALSE;
592 }
593
594 gboolean avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, gboolean immediately) {
595     g_assert(i);
596     g_assert(record);
597     
598     if (avahi_interface_relevant(i))
599         return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
600
601     return FALSE;
602 }
603
604 void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) {
605     AvahiInterface *i;
606     g_assert(m);
607
608     for (i = m->interfaces; i; i = i->interface_next) {
609         if (avahi_interface_relevant(i)) {
610             fprintf(f, "\n;;; INTERFACE %s.%i ;;;\n", i->hardware->name, i->protocol);
611             avahi_cache_dump(i->cache, f);
612         }
613     }
614     fprintf(f, "\n");
615 }
616
617 gboolean avahi_interface_relevant(AvahiInterface *i) {
618     AvahiInterfaceAddress *a;
619     gboolean relevant_address;
620     
621     g_assert(i);
622
623     relevant_address = FALSE;
624     
625     for (a = i->addresses; a; a = a->address_next)
626         if (avahi_interface_address_relevant(a)) {
627             relevant_address = TRUE;
628             break;
629         }
630
631 /*     avahi_log_debug("%p. iface-relevant: %i %i %i %i %i %i", i, relevant_address, */
632 /*               (i->hardware->flags & IFF_UP), */
633 /*               (i->hardware->flags & IFF_RUNNING), */
634 /*               !(i->hardware->flags & IFF_LOOPBACK), */
635 /*               (i->hardware->flags & IFF_MULTICAST), */
636 /*               !(i->hardware->flags & IFF_POINTOPOINT)); */
637     
638     return
639         (i->hardware->flags & IFF_UP) &&
640         (!i->monitor->server->config.use_iff_running || (i->hardware->flags & IFF_RUNNING)) &&
641         !(i->hardware->flags & IFF_LOOPBACK) &&
642         (i->hardware->flags & IFF_MULTICAST) &&
643         !(i->hardware->flags & IFF_POINTOPOINT) && 
644         relevant_address;
645 }
646
647 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { 
648     g_assert(a);
649
650     return a->scope == RT_SCOPE_UNIVERSE;
651 }
652
653
654 gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex index, AvahiProtocol protocol) {
655     g_assert(i);
656     
657     if (index > 0 && index != i->hardware->index)
658         return FALSE;
659
660     if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
661         return FALSE;
662
663     return TRUE;
664 }
665
666 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
667     g_assert(m);
668     g_assert(callback);
669     
670     if (interface > 0) {
671         if (protocol != AVAHI_PROTO_UNSPEC) {
672             AvahiInterface *i;
673             
674             if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
675                 callback(m, i, userdata);
676             
677         } else {
678             AvahiHwInterface *hw;
679             AvahiInterface *i;
680
681             if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
682                 for (i = hw->interfaces; i; i = i->by_hardware_next)
683                     if (avahi_interface_match(i, interface, protocol))
684                         callback(m, i, userdata);
685         }
686         
687     } else {
688         AvahiInterface *i;
689         
690         for (i = m->interfaces; i; i = i->interface_next)
691             if (avahi_interface_match(i, interface, protocol))
692                 callback(m, i, userdata);
693     }
694 }
695
696 void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove) {
697     AvahiHwInterface *hw;
698
699     g_assert(m);
700
701     for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
702         update_hw_interface_rr(m, hw, remove);
703 }
704
705 gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
706     AvahiInterface *i;
707     AvahiInterfaceAddress *ia;
708     g_assert(m);
709     g_assert(a);
710
711     for (i = m->interfaces; i; i = i->interface_next)
712         for (ia = i->addresses; ia; ia = ia->address_next)
713             if (avahi_address_cmp(a, &ia->address) == 0)
714                 return TRUE;
715
716     return FALSE;
717 }
718
719 gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
720     AvahiInterfaceAddress *ia;
721     
722     g_assert(i);
723     g_assert(a);
724
725     if (a->family != i->protocol)
726         return FALSE;
727
728     for (ia = i->addresses; ia; ia = ia->address_next) {
729
730         if (a->family == AVAHI_PROTO_INET) {
731             guint32 m;
732             
733             m = ~(((guint32) -1) >> ia->prefix_len);
734             
735             if ((g_ntohl(a->data.ipv4.address) & m) == (g_ntohl(ia->address.data.ipv4.address) & m))
736                 return TRUE;
737         } else {
738             guint i;
739             guchar pl;
740             g_assert(a->family == AVAHI_PROTO_INET6);
741
742             pl = ia->prefix_len;
743             
744             for (i = 0; i < 16; i++) {
745                 guint8 m;
746
747                 if (pl == 0)
748                     return TRUE;
749                 
750                 if (pl >= 8) {
751                     m = 0xFF;
752                     pl -= 8;
753                 } else {
754                     m = ~(0xFF >> pl);
755                     pl = 0;
756                 }
757                 
758                 if ((a->data.ipv6.address[i] & m) != (ia->address.data.ipv6.address[i] & m))
759                     break;
760             }
761         }
762     }
763
764     return FALSE;
765 }