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