]> git.meshlink.io Git - catta/blob - avahi-core/iface.c
ad26ab01bc06095ab09f4533973fd96ce62f9c36
[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 #include <stdio.h>
34
35 #include "iface.h"
36 #include "netlink.h"
37 #include "dns.h"
38 #include "socket.h"
39 #include "announce.h"
40 #include "util.h"
41 #include "log.h"
42
43 static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove_rrs) {
44     g_assert(m);
45     g_assert(a);
46
47     if (avahi_interface_address_relevant(a) &&
48         !remove_rrs &&
49         m->server->config.publish_addresses &&
50         (m->server->state == AVAHI_SERVER_RUNNING ||
51         m->server->state == AVAHI_SERVER_REGISTERING)) {
52
53         if (!a->entry_group) {
54             a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
55             if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
56                 avahi_log_warn(__FILE__": avahi_server_add_address() failed.");
57                 avahi_entry_group_free(a->entry_group);
58                 a->entry_group = NULL;
59             } else
60                 avahi_entry_group_commit(a->entry_group);
61         }
62     } else {
63
64         if (a->entry_group) {
65
66             if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
67                 avahi_server_decrease_host_rr_pending(m->server);
68             
69             avahi_entry_group_free(a->entry_group);
70             a->entry_group = NULL;
71         }
72     } 
73 }
74
75 static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove_rrs) {
76     AvahiInterfaceAddress *a;
77     
78     g_assert(m);
79     g_assert(i);
80
81     for (a = i->addresses; a; a = a->address_next)
82         update_address_rr(m, a, remove_rrs);
83 }
84
85 static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove_rrs) {
86     AvahiInterface *i;
87
88     g_assert(m);
89     g_assert(hw);
90
91     for (i = hw->interfaces; i; i = i->by_hardware_next)
92         update_interface_rr(m, i, remove_rrs);
93
94     if (!remove_rrs &&
95         m->server->config.publish_workstation &&
96         (m->server->state == AVAHI_SERVER_RUNNING ||
97         m->server->state == AVAHI_SERVER_REGISTERING)) {
98
99         if (!hw->entry_group) {
100             gchar *name;
101             gchar *t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size);
102             name = g_strdup_printf("%s [%s]", m->server->host_name, t);
103             g_free(t);
104             
105             hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
106             if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) { 
107                 avahi_log_warn(__FILE__": avahi_server_add_service() failed.");
108                 avahi_entry_group_free(hw->entry_group);
109                 hw->entry_group = NULL;
110             } else
111                 avahi_entry_group_commit(hw->entry_group);
112
113             g_free(name);
114         }
115         
116     } else {
117
118         if (hw->entry_group) {
119
120             if (avahi_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
121                 avahi_server_decrease_host_rr_pending(m->server);
122
123             avahi_entry_group_free(hw->entry_group);
124             hw->entry_group = NULL;
125         }
126     }
127 }
128
129 static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) {
130     g_assert(m);
131     g_assert(a);
132     g_assert(a->interface);
133
134     update_address_rr(m, a, TRUE);
135     AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
136     
137     g_free(a);
138 }
139
140 static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean send_goodbye) {
141     g_assert(m);
142     g_assert(i);
143
144     avahi_goodbye_interface(m->server, i, send_goodbye);
145     avahi_response_scheduler_force(i->response_scheduler);
146     
147     g_assert(!i->announcements);
148
149     update_interface_rr(m, i, TRUE);
150     
151     while (i->addresses)
152         free_address(m, i->addresses);
153
154     avahi_response_scheduler_free(i->response_scheduler);
155     avahi_query_scheduler_free(i->query_scheduler);
156     avahi_probe_scheduler_free(i->probe_scheduler);
157     avahi_cache_free(i->cache);
158     
159     AVAHI_LLIST_REMOVE(AvahiInterface, interface, m->interfaces, i);
160     AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
161     
162     g_free(i);
163 }
164
165 static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean send_goodbye) {
166     g_assert(m);
167     g_assert(hw);
168
169     update_hw_interface_rr(m, hw, TRUE);
170     
171     while (hw->interfaces)
172         free_interface(m, hw->interfaces, send_goodbye);
173
174     AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, m->hw_interfaces, hw);
175     g_hash_table_remove(m->hash_table, &hw->index);
176
177     g_free(hw->name);
178     g_free(hw);
179 }
180
181 static AvahiInterfaceAddress* get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
182     AvahiInterfaceAddress *ia;
183     
184     g_assert(m);
185     g_assert(i);
186     g_assert(raddr);
187
188     for (ia = i->addresses; ia; ia = ia->address_next)
189         if (avahi_address_cmp(&ia->address, raddr) == 0)
190             return ia;
191
192     return NULL;
193 }
194
195 static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) {
196     struct nlmsghdr *n;
197     struct rtgenmsg *gen;
198     guint8 req[1024];
199     
200     memset(&req, 0, sizeof(req));
201     n = (struct nlmsghdr*) req;
202     n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
203     n->nlmsg_type = type;
204     n->nlmsg_flags = NLM_F_ROOT/*|NLM_F_MATCH*/|NLM_F_REQUEST;
205     n->nlmsg_pid = 0;
206
207     gen = NLMSG_DATA(n);
208     memset(gen, 0, sizeof(struct rtgenmsg));
209     gen->rtgen_family = AF_UNSPEC;
210
211     return avahi_netlink_send(nl, n, ret_seq);
212 }
213
214 static void new_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
215     AvahiInterface *i;
216     
217     g_assert(m);
218     g_assert(hw);
219     g_assert(protocol != AVAHI_PROTO_UNSPEC);
220
221     i = g_new(AvahiInterface, 1);
222     i->monitor = m;
223     i->hardware = hw;
224     i->protocol = protocol;
225     i->announcing = FALSE;
226
227     AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
228     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, i->announcements);
229
230     i->cache = avahi_cache_new(m->server, i);
231     i->response_scheduler = avahi_response_scheduler_new(i);
232     i->query_scheduler = avahi_query_scheduler_new(i);
233     i->probe_scheduler = avahi_probe_scheduler_new(i);
234
235     AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
236     AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
237 }
238
239 static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i) {
240     gboolean b;
241
242     g_assert(m);
243     g_assert(i);
244
245     b = avahi_interface_relevant(i);
246
247     if (b && !i->announcing) {
248         avahi_log_debug("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index);
249
250         if (i->protocol == AVAHI_PROTO_INET)
251             avahi_mdns_mcast_join_ipv4(m->server->fd_ipv4, i->hardware->index);
252         if (i->protocol == AVAHI_PROTO_INET6)
253             avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index);
254
255         i->announcing = TRUE;
256         avahi_announce_interface(m->server, i);
257         avahi_browser_new_interface(m->server, i);
258     } else if (!b && i->announcing) {
259         avahi_log_debug("Interface %s.%i no longer relevant", i->hardware->name, i->protocol);
260
261         if (i->protocol == AVAHI_PROTO_INET)
262             avahi_mdns_mcast_leave_ipv4(m->server->fd_ipv4, i->hardware->index);
263         if (i->protocol == AVAHI_PROTO_INET6)
264             avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index);
265
266         avahi_goodbye_interface(m->server, i, FALSE);
267         avahi_response_scheduler_clear(i->response_scheduler);
268         avahi_query_scheduler_clear(i->query_scheduler);
269         avahi_probe_scheduler_clear(i->probe_scheduler);
270         avahi_cache_flush(i->cache);
271
272         i->announcing = FALSE;
273     }
274 }
275
276 static void check_hw_interface_relevant(AvahiInterfaceMonitor *m, AvahiHwInterface *hw) {
277     AvahiInterface *i;
278     
279     g_assert(m);
280     g_assert(hw);
281
282     for (i = hw->interfaces; i; i = i->by_hardware_next)
283         check_interface_relevant(m, i);
284 }
285
286 static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
287     AvahiInterfaceMonitor *m = userdata;
288     
289     g_assert(m);
290     g_assert(n);
291     g_assert(m->netlink == nl);
292
293     if (n->nlmsg_type == RTM_NEWLINK) {
294         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
295         AvahiHwInterface *hw;
296         struct rtattr *a = NULL;
297         size_t l;
298
299         if (ifinfomsg->ifi_family != AF_UNSPEC)
300             return;
301
302         if (!(hw = g_hash_table_lookup(m->hash_table, &ifinfomsg->ifi_index))) {
303             hw = g_new(AvahiHwInterface, 1);
304             hw->monitor = m;
305             hw->name = NULL;
306             hw->flags = 0;
307             hw->mtu = 1500;
308             hw->index = ifinfomsg->ifi_index;
309             hw->mac_address_size = 0;
310             hw->entry_group = NULL;
311
312             AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
313             AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
314             
315             g_hash_table_insert(m->hash_table, &hw->index, hw);
316
317             if (m->server->fd_ipv4 >= 0)
318                 new_interface(m, hw, AVAHI_PROTO_INET);
319             if (m->server->fd_ipv6 >= 0)
320                 new_interface(m, hw, AVAHI_PROTO_INET6);
321         }
322         
323         hw->flags = ifinfomsg->ifi_flags;
324
325         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
326         a = IFLA_RTA(ifinfomsg);
327
328         while (RTA_OK(a, l)) {
329             switch(a->rta_type) {
330                 case IFLA_IFNAME:
331                     g_free(hw->name);
332                     hw->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
333                     break;
334
335                 case IFLA_MTU:
336                     g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
337                     hw->mtu = *((unsigned int*) RTA_DATA(a));
338                     break;
339
340                 case IFLA_ADDRESS: {
341                     hw->mac_address_size = RTA_PAYLOAD(a);
342                     if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS)
343                         hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS;
344                     
345                     memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size);
346                     break;
347                 }
348                     
349                 default:
350                     ;
351             }
352
353             a = RTA_NEXT(a, l);
354         }
355
356         update_hw_interface_rr(m, hw, FALSE);
357         check_hw_interface_relevant(m, hw);
358         
359     } else if (n->nlmsg_type == RTM_DELLINK) {
360         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
361         AvahiHwInterface *hw;
362
363         if (ifinfomsg->ifi_family != AF_UNSPEC)
364             return;
365         
366         if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
367             return;
368
369         update_hw_interface_rr(m, hw, TRUE);
370         free_hw_interface(m, hw, FALSE);
371         
372     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
373
374         struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
375         AvahiInterface *i;
376         struct rtattr *a = NULL;
377         size_t l;
378         AvahiAddress raddr;
379         gboolean raddr_valid = FALSE;
380
381         if (ifaddrmsg->ifa_family != AVAHI_PROTO_INET && ifaddrmsg->ifa_family != AVAHI_PROTO_INET6)
382             return;
383
384         if (!(i = (AvahiInterface*) avahi_interface_monitor_get_interface(m, ifaddrmsg->ifa_index, ifaddrmsg->ifa_family)))
385             return;
386
387         raddr.family = ifaddrmsg->ifa_family;
388
389         l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg));
390         a = IFA_RTA(ifaddrmsg);
391
392         while (RTA_OK(a, l)) {
393
394             switch(a->rta_type) {
395                 case IFA_ADDRESS:
396                     if ((raddr.family == AVAHI_PROTO_INET6 && RTA_PAYLOAD(a) != 16) ||
397                         (raddr.family == AVAHI_PROTO_INET && RTA_PAYLOAD(a) != 4))
398                         return;
399
400                     memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a));
401                     raddr_valid = TRUE;
402
403                     break;
404
405                 default:
406                     ;
407             }
408             
409             a = RTA_NEXT(a, l);
410         }
411         
412         if (!raddr_valid)
413             return;
414
415         if (n->nlmsg_type == RTM_NEWADDR) {
416             AvahiInterfaceAddress *addr;
417             
418             if (!(addr = get_address(m, i, &raddr))) {
419                 addr = g_new(AvahiInterfaceAddress, 1);
420                 addr->monitor = m;
421                 addr->address = raddr;
422                 addr->interface = i;
423                 addr->entry_group = NULL;
424
425                 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, addr);
426             }
427             
428             addr->flags = ifaddrmsg->ifa_flags;
429             addr->scope = ifaddrmsg->ifa_scope;
430             addr->prefix_len = ifaddrmsg->ifa_prefixlen;
431
432             update_address_rr(m, addr, FALSE);
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
443         check_interface_relevant(m, i);
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, netlink_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 idx, AvahiProtocol protocol) {
521     AvahiHwInterface *hw;
522     AvahiInterface *i;
523     
524     g_assert(m);
525     g_assert(idx > 0);
526     g_assert(protocol != AVAHI_PROTO_UNSPEC);
527
528     if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
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 idx) {
539     g_assert(m);
540     g_assert(idx > 0);
541
542     return g_hash_table_lookup(m->hash_table, &idx);
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, AvahiDumpCallback callback, gpointer userdata) {
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             char ln[256];
611             snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%i ;;;", i->hardware->name, i->protocol);
612             callback(ln, userdata);
613             avahi_cache_dump(i->cache, callback, userdata);
614         }
615     }
616 }
617
618 gboolean avahi_interface_relevant(AvahiInterface *i) {
619     AvahiInterfaceAddress *a;
620     gboolean relevant_address;
621     
622     g_assert(i);
623
624     relevant_address = FALSE;
625     
626     for (a = i->addresses; a; a = a->address_next)
627         if (avahi_interface_address_relevant(a)) {
628             relevant_address = TRUE;
629             break;
630         }
631
632 /*     avahi_log_debug("%p. iface-relevant: %i %i %i %i %i %i", i, relevant_address, */
633 /*               (i->hardware->flags & IFF_UP), */
634 /*               (i->hardware->flags & IFF_RUNNING), */
635 /*               !(i->hardware->flags & IFF_LOOPBACK), */
636 /*               (i->hardware->flags & IFF_MULTICAST), */
637 /*               !(i->hardware->flags & IFF_POINTOPOINT)); */
638     
639     return
640         (i->hardware->flags & IFF_UP) &&
641         (!i->monitor->server->config.use_iff_running || (i->hardware->flags & IFF_RUNNING)) &&
642         !(i->hardware->flags & IFF_LOOPBACK) &&
643         (i->hardware->flags & IFF_MULTICAST) &&
644         !(i->hardware->flags & IFF_POINTOPOINT) && 
645         relevant_address;
646 }
647
648 gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { 
649     g_assert(a);
650
651     return a->scope == RT_SCOPE_UNIVERSE;
652 }
653
654
655 gboolean avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
656     g_assert(i);
657     
658     if (idx > 0 && idx != i->hardware->index)
659         return FALSE;
660
661     if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
662         return FALSE;
663
664     return TRUE;
665 }
666
667 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) {
668     g_assert(m);
669     g_assert(callback);
670     
671     if (interface > 0) {
672         if (protocol != AVAHI_PROTO_UNSPEC) {
673             AvahiInterface *i;
674             
675             if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
676                 callback(m, i, userdata);
677             
678         } else {
679             AvahiHwInterface *hw;
680             AvahiInterface *i;
681
682             if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
683                 for (i = hw->interfaces; i; i = i->by_hardware_next)
684                     if (avahi_interface_match(i, interface, protocol))
685                         callback(m, i, userdata);
686         }
687         
688     } else {
689         AvahiInterface *i;
690         
691         for (i = m->interfaces; i; i = i->interface_next)
692             if (avahi_interface_match(i, interface, protocol))
693                 callback(m, i, userdata);
694     }
695 }
696
697 void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove_rrs) {
698     AvahiHwInterface *hw;
699
700     g_assert(m);
701
702     for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
703         update_hw_interface_rr(m, hw, remove_rrs);
704 }
705
706 gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
707     AvahiInterface *i;
708     AvahiInterfaceAddress *ia;
709     g_assert(m);
710     g_assert(a);
711
712     for (i = m->interfaces; i; i = i->interface_next)
713         for (ia = i->addresses; ia; ia = ia->address_next)
714             if (avahi_address_cmp(a, &ia->address) == 0)
715                 return TRUE;
716
717     return FALSE;
718 }
719
720 gboolean avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
721     AvahiInterfaceAddress *ia;
722     
723     g_assert(i);
724     g_assert(a);
725
726     if (a->family != i->protocol)
727         return FALSE;
728
729     for (ia = i->addresses; ia; ia = ia->address_next) {
730
731         if (a->family == AVAHI_PROTO_INET) {
732             guint32 m;
733             
734             m = ~(((guint32) -1) >> ia->prefix_len);
735             
736             if ((g_ntohl(a->data.ipv4.address) & m) == (g_ntohl(ia->address.data.ipv4.address) & m))
737                 return TRUE;
738         } else {
739             guint j;
740             guchar pl;
741             g_assert(a->family == AVAHI_PROTO_INET6);
742
743             pl = ia->prefix_len;
744             
745             for (j = 0; j < 16; j++) {
746                 guint8 m;
747
748                 if (pl == 0)
749                     return TRUE;
750                 
751                 if (pl >= 8) {
752                     m = 0xFF;
753                     pl -= 8;
754                 } else {
755                     m = ~(0xFF >> pl);
756                     pl = 0;
757                 }
758                 
759                 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
760                     break;
761             }
762         }
763     }
764
765     return FALSE;
766 }