]> git.meshlink.io Git - catta/blob - avahi-core/iface.c
* Do not allow adding RRs to entry groups that are already commited
[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 <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <netinet/in.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33
34 #include <avahi-common/error.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
37
38 #include "iface.h"
39 #include "dns.h"
40 #include "socket.h"
41 #include "announce.h"
42 #include "util.h"
43 #include "log.h"
44 #include "multicast-lookup.h"
45 #include "querier.h"
46
47 void avahi_interface_address_update_rrs(AvahiInterfaceAddress *a, int remove_rrs) {
48     AvahiInterfaceMonitor *m;
49
50     assert(a);
51     m = a->monitor;
52
53     if (a->interface->announcing &&
54         m->list_complete &&
55         avahi_interface_address_is_relevant(a) &&
56         !remove_rrs &&
57         m->server->config.publish_addresses &&
58         (m->server->state == AVAHI_SERVER_RUNNING ||
59         m->server->state == AVAHI_SERVER_REGISTERING)) {
60
61         /* Fill the entry group */
62         if (!a->entry_group) 
63             a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
64
65         if (!a->entry_group) /* OOM */
66             return;
67         
68         if (avahi_s_entry_group_is_empty(a->entry_group)) {
69             char t[AVAHI_ADDRESS_STR_MAX];
70             avahi_address_snprint(t, sizeof(t), &a->address);
71
72             avahi_log_info("Registering new address record for %s on %s.", t, a->interface->hardware->name);
73
74             if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) {
75                 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
76                 avahi_s_entry_group_free(a->entry_group);
77                 a->entry_group = NULL;
78                 return;
79             }
80
81             avahi_s_entry_group_commit(a->entry_group);
82         }
83     } else {
84
85         /* Clear the entry group */
86
87         if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
88             char t[AVAHI_ADDRESS_STR_MAX];
89             avahi_address_snprint(t, sizeof(t), &a->address);
90
91             if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
92                 m->server->state == AVAHI_SERVER_REGISTERING)
93                 avahi_server_decrease_host_rr_pending(m->server);
94
95             avahi_log_info("Withdrawing address record for %s on %s.", t, a->interface->hardware->name);
96             
97             avahi_s_entry_group_reset(a->entry_group);
98         }
99     } 
100 }
101
102 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
103     AvahiInterfaceAddress *a;
104     
105     assert(i);
106
107     for (a = i->addresses; a; a = a->address_next)
108         avahi_interface_address_update_rrs(a, remove_rrs);
109 }
110
111 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
112     AvahiInterface *i;
113     AvahiInterfaceMonitor *m;
114
115     assert(hw);
116     m = hw->monitor;
117
118     for (i = hw->interfaces; i; i = i->by_hardware_next)
119         avahi_interface_update_rrs(i, remove_rrs);
120
121     if (m->list_complete &&
122         !remove_rrs &&
123         m->server->config.publish_workstation &&
124         (m->server->state == AVAHI_SERVER_RUNNING ||
125         m->server->state == AVAHI_SERVER_REGISTERING)) {
126
127         if (!hw->entry_group)
128             hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
129
130         if (!hw->entry_group)
131             return; /* OOM */
132         
133         if (avahi_s_entry_group_is_empty(hw->entry_group)) {
134             char name[AVAHI_LABEL_MAX];
135             char *t;
136
137             if (!(t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size)))
138                 return; /* OOM */
139
140             snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, t);
141             avahi_free(t);
142
143             if (!name)
144                 return; /* OOM */
145             
146             if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) { 
147                 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
148                 avahi_s_entry_group_free(hw->entry_group);
149                 hw->entry_group = NULL;
150             } else
151                 avahi_s_entry_group_commit(hw->entry_group);
152         }
153         
154     } else {
155
156         if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
157
158             if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
159                 avahi_server_decrease_host_rr_pending(m->server);
160
161             avahi_s_entry_group_reset(hw->entry_group);
162         }
163     }
164 }
165
166 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
167     AvahiHwInterface *hw;
168
169     assert(m);
170
171     for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
172         avahi_hw_interface_update_rrs(hw, remove_rrs);
173 }
174
175 static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
176     char at[AVAHI_ADDRESS_STR_MAX];
177     assert(i);
178
179     assert((join && !i->mcast_joined) || (!join && i->mcast_joined));
180     
181     if (join) {
182         AvahiInterfaceAddress *a;
183
184         /* Look if there's an address with global scope */
185         for (a = i->addresses; a; a = a->address_next)
186             if (a->global_scope)
187                 break;
188
189         /* No address with a global scope has been found, so let's use
190          * any. */
191         if (!a)
192             a = i->addresses;
193
194         /* Hmm, there is no address available. */
195         if (!a) {
196             avahi_log_warn(__FILE__": interface_mdns_mcast_join() called but no local address available."); 
197             return -1;
198         }
199
200         i->local_mcast_address = a->address;
201     }
202
203     avahi_log_info("%s mDNS multicast group on interface %s.%s with address %s.",
204                    join ? "Joining" : "Leaving",
205                    i->hardware->name,
206                    avahi_proto_to_string(i->protocol),
207                    avahi_address_snprint(at, sizeof(at), &i->local_mcast_address));
208
209     if (i->protocol == AVAHI_PROTO_INET6) {
210         if (avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, &i->local_mcast_address.data.ipv6, i->hardware->index, join) < 0)
211             return -1;
212         
213     } else {
214         assert(i->protocol == AVAHI_PROTO_INET);
215             
216         if (avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &i->local_mcast_address.data.ipv4, i->hardware->index, join) < 0)
217             return -1;
218     }
219
220     i->mcast_joined = join;
221     return 0;
222 }
223
224 static int interface_mdns_mcast_rejoin(AvahiInterface *i) {
225     AvahiInterfaceAddress *a, *usable = NULL, *found = NULL;
226     assert(i);
227
228     if (!i->mcast_joined)
229         return 0;
230
231     /* Check whether old address we joined with is still available. If
232      * not, rejoin using an other address. */
233     
234     for (a = i->addresses; a; a = a->address_next) {
235         if (a->global_scope && !usable)
236             usable = a;
237         
238         if (avahi_address_cmp(&a->address, &i->local_mcast_address) == 0) {
239
240             if (a->global_scope)
241                 /* No action necessary: the address still exists and
242                  * has global scope. */
243                 return 0;
244
245             found = a;
246         }
247     }
248
249     if (found && !usable)
250         /* No action necessary: the address still exists and no better one has been found */
251         return 0;
252     
253     interface_mdns_mcast_join(i, 0);
254     return interface_mdns_mcast_join(i, 1);
255 }
256
257 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
258     assert(a);
259     assert(a->interface);
260
261     avahi_interface_address_update_rrs(a, 1);
262     AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
263
264     if (a->entry_group)
265         avahi_s_entry_group_free(a->entry_group);
266
267     interface_mdns_mcast_rejoin(a->interface);
268     
269     avahi_free(a);
270 }
271
272 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
273     assert(i);
274
275     /* Handle goodbyes and remove announcers */
276     avahi_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
277     avahi_response_scheduler_force(i->response_scheduler);
278     assert(!i->announcers);
279
280     if (i->mcast_joined)
281         interface_mdns_mcast_join(i, 0);
282     
283     /* Remove queriers */
284     avahi_querier_free_all(i);
285     avahi_hashmap_free(i->queriers_by_key);
286
287     /* Remove local RRs */
288     avahi_interface_update_rrs(i, 1);
289     
290     while (i->addresses)
291         avahi_interface_address_free(i->addresses);
292
293     avahi_response_scheduler_free(i->response_scheduler);
294     avahi_query_scheduler_free(i->query_scheduler);
295     avahi_probe_scheduler_free(i->probe_scheduler);
296     avahi_cache_free(i->cache);
297     
298     AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
299     AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
300     
301     avahi_free(i);
302 }
303
304 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
305     assert(hw);
306
307     avahi_hw_interface_update_rrs(hw, 1);
308     
309     while (hw->interfaces)
310         avahi_interface_free(hw->interfaces, send_goodbye);
311
312     if (hw->entry_group)
313         avahi_s_entry_group_free(hw->entry_group);
314     
315     AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
316     avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
317
318     avahi_free(hw->name);
319     avahi_free(hw);
320 }
321
322 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
323     AvahiInterface *i;
324     
325     assert(m);
326     assert(hw);
327     assert(AVAHI_PROTO_VALID(protocol));
328
329     if (!(i = avahi_new(AvahiInterface, 1)))
330         goto fail; /* OOM */
331         
332     i->monitor = m;
333     i->hardware = hw;
334     i->protocol = protocol;
335     i->announcing = 0;
336     i->mcast_joined = 0;
337
338     AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
339     AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, i->announcers);
340
341     AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
342     i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
343
344     i->cache = avahi_cache_new(m->server, i);
345     i->response_scheduler = avahi_response_scheduler_new(i);
346     i->query_scheduler = avahi_query_scheduler_new(i);
347     i->probe_scheduler = avahi_probe_scheduler_new(i);
348
349     if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
350         goto fail; /* OOM */
351
352     AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
353     AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
354
355     return i;
356     
357 fail:
358
359     if (i) {
360         if (i->cache)
361             avahi_cache_free(i->cache);
362         if (i->response_scheduler)
363             avahi_response_scheduler_free(i->response_scheduler);
364         if (i->query_scheduler)
365             avahi_query_scheduler_free(i->query_scheduler);
366         if (i->probe_scheduler)
367             avahi_probe_scheduler_free(i->probe_scheduler);
368     }
369
370     return NULL;
371 }
372
373 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
374     AvahiHwInterface *hw;
375     
376     assert(m);
377     assert(AVAHI_IF_VALID(idx));
378
379     if  (!(hw = avahi_new(AvahiHwInterface, 1)))
380         return NULL;
381         
382     hw->monitor = m;
383     hw->name = NULL;
384     hw->flags_ok = 0;
385     hw->mtu = 1500;
386     hw->index = idx;
387     hw->mac_address_size = 0;
388     hw->entry_group = NULL;
389
390     AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
391     AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
392             
393     avahi_hashmap_insert(m->hashmap, &hw->index, hw);
394
395     if (m->server->fd_ipv4 >= 0)
396         avahi_interface_new(m, hw, AVAHI_PROTO_INET);
397     if (m->server->fd_ipv6 >= 0)
398         avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
399
400     return hw;
401 }
402
403 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
404     AvahiInterfaceAddress *a;
405
406     assert(m);
407     assert(i);
408
409     if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
410         return NULL;
411
412     a->interface = i;
413     a->monitor = m;
414     a->address = *addr;
415     a->prefix_len = prefix_len;
416     a->global_scope = 0;
417     a->entry_group = NULL;
418
419     AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
420
421     return a;
422 }
423
424 void avahi_interface_check_relevant(AvahiInterface *i) {
425     int b;
426     AvahiInterfaceMonitor *m;
427
428     assert(i);
429     m = i->monitor;
430
431     b = avahi_interface_is_relevant(i);
432
433     if (m->list_complete && b && !i->announcing) {
434         avahi_log_info("New relevant interface %s.%s for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
435
436         interface_mdns_mcast_join(i, 1);
437
438         i->announcing = 1;
439         avahi_announce_interface(m->server, i);
440         avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
441     } else if (!b && i->announcing) {
442         avahi_log_info("Interface %s.%s no longer relevant for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
443
444         interface_mdns_mcast_join(i, 0);
445
446         avahi_goodbye_interface(m->server, i, 0, 1);
447         avahi_querier_free_all(i);
448
449         avahi_response_scheduler_clear(i->response_scheduler);
450         avahi_query_scheduler_clear(i->query_scheduler);
451         avahi_probe_scheduler_clear(i->probe_scheduler);
452         avahi_cache_flush(i->cache);
453
454         i->announcing = 0;
455         
456     } else
457         interface_mdns_mcast_rejoin(i);
458 }
459
460 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
461     AvahiInterface *i;
462     
463     assert(hw);
464
465     for (i = hw->interfaces; i; i = i->by_hardware_next)
466         avahi_interface_check_relevant(i);
467 }
468
469 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
470     AvahiInterface *i;
471
472     assert(m);
473
474     for (i = m->interfaces; i; i = i->interface_next)
475         avahi_interface_check_relevant(i);
476 }
477
478 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
479     AvahiInterfaceMonitor *m = NULL;
480
481     if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
482         return NULL; /* OOM */
483         
484     m->server = s;
485     m->list_complete = 0;
486     m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
487
488     AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
489     AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
490
491     if (avahi_interface_monitor_init_osdep(m) < 0)
492         goto fail;
493
494     return m;
495
496 fail:
497     avahi_interface_monitor_free(m);
498     return NULL;
499 }
500
501 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
502     assert(m);
503
504     while (m->hw_interfaces)
505         avahi_hw_interface_free(m->hw_interfaces, 1);
506
507     assert(!m->interfaces);
508
509     avahi_interface_monitor_free_osdep(m);
510     
511     if (m->hashmap)
512         avahi_hashmap_free(m->hashmap);
513
514     avahi_free(m);
515 }
516
517
518 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
519     AvahiHwInterface *hw;
520     AvahiInterface *i;
521     
522     assert(m);
523     assert(idx >= 0);
524     assert(protocol != AVAHI_PROTO_UNSPEC);
525
526     if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
527         return NULL;
528
529     for (i = hw->interfaces; i; i = i->by_hardware_next)
530         if (i->protocol == protocol)
531             return i;
532
533     return NULL;
534 }
535
536 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
537     assert(m);
538     assert(idx > 0);
539
540     return avahi_hashmap_lookup(m->hashmap, &idx);
541 }
542
543 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
544     AvahiInterfaceAddress *ia;
545     
546     assert(m);
547     assert(i);
548     assert(raddr);
549
550     for (ia = i->addresses; ia; ia = ia->address_next)
551         if (avahi_address_cmp(&ia->address, raddr) == 0)
552             return ia;
553
554     return NULL;
555 }
556
557
558 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
559     assert(i);
560     assert(p);
561 /*     char t[AVAHI_ADDRESS_STR_MAX]; */
562
563     if (!avahi_interface_is_relevant(i))
564         return;
565     
566     assert(!a || a->proto == i->protocol);
567
568 /*     if (a) */
569 /*         avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */
570 /*     else */
571 /*         avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */
572     
573     if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
574         avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, i->mcast_joined ? &i->local_mcast_address.data.ipv4 : NULL, a ? &a->data.ipv4 : NULL, port);
575     else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
576         avahi_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, p, i->mcast_joined ? &i->local_mcast_address.data.ipv6 : NULL, a ? &a->data.ipv6 : NULL, port);
577 }
578
579 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
580     assert(i);
581     assert(p);
582
583     avahi_interface_send_packet_unicast(i, p, NULL, 0);
584 }
585
586 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately) {
587     assert(i);
588     assert(key);
589
590     if (avahi_interface_is_relevant(i))
591         return avahi_query_scheduler_post(i->query_scheduler, key, immediately);
592
593     return 0;
594 }
595
596 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
597     assert(i);
598     assert(record);
599
600     if (avahi_interface_is_relevant(i))
601         return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
602
603     return 0;
604 }
605
606 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
607     assert(i);
608     assert(record);
609     
610     if (avahi_interface_is_relevant(i))
611         return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
612
613     return 0;
614 }
615
616 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
617     AvahiInterface *i;
618     assert(m);
619
620     for (i = m->interfaces; i; i = i->interface_next) {
621         if (avahi_interface_is_relevant(i)) {
622             char ln[256];
623             snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
624             callback(ln, userdata);
625             if (avahi_cache_dump(i->cache, callback, userdata) < 0)
626                 return -1;
627         }
628     }
629
630     return 0;
631 }
632
633 int avahi_interface_is_relevant(AvahiInterface *i) {
634     AvahiInterfaceAddress *a;
635     int relevant_address;
636     
637     assert(i);
638
639     relevant_address = 0;
640     
641     for (a = i->addresses; a; a = a->address_next)
642         if (avahi_interface_address_is_relevant(a)) {
643             relevant_address = 1;
644             break;
645         }
646
647     return i->hardware->flags_ok && relevant_address;
648 }
649
650 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
651     AvahiInterfaceAddress *b;
652     assert(a);
653
654     /* Publish public IP addresses */
655     if (a->global_scope)
656         return 1;
657     else {
658         
659         /* Publish link local IP addresses if they are the only ones on the link */
660         for (b = a->interface->addresses; b; b = b->address_next) {
661             if (b == a)
662                 continue;
663             
664             if (b->global_scope)
665                 return 0;
666         }
667
668         return 1;
669     }
670
671     return 0;
672 }
673
674 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
675     assert(i);
676     
677     if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
678         return 0;
679
680     if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
681         return 0;
682
683     return 1;
684 }
685
686 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
687     assert(m);
688     assert(callback);
689     
690     if (interface != AVAHI_IF_UNSPEC) {
691         if (protocol != AVAHI_PROTO_UNSPEC) {
692             AvahiInterface *i;
693             
694             if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
695                 callback(m, i, userdata);
696             
697         } else {
698             AvahiHwInterface *hw;
699             AvahiInterface *i;
700
701             if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
702                 for (i = hw->interfaces; i; i = i->by_hardware_next)
703                     if (avahi_interface_match(i, interface, protocol))
704                         callback(m, i, userdata);
705         }
706         
707     } else {
708         AvahiInterface *i;
709         
710         for (i = m->interfaces; i; i = i->interface_next)
711             if (avahi_interface_match(i, interface, protocol))
712                 callback(m, i, userdata);
713     }
714 }
715
716
717 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
718     AvahiInterface *i;
719     AvahiInterfaceAddress *ia;
720     assert(m);
721     assert(a);
722
723     for (i = m->interfaces; i; i = i->interface_next)
724         for (ia = i->addresses; ia; ia = ia->address_next)
725             if (avahi_address_cmp(a, &ia->address) == 0)
726                 return 1;
727
728     return 0;
729 }
730
731 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
732     AvahiInterfaceAddress *ia;
733     
734     assert(i);
735     assert(a);
736
737     if (a->proto != i->protocol)
738         return 0;
739
740     for (ia = i->addresses; ia; ia = ia->address_next) {
741
742         if (a->proto == AVAHI_PROTO_INET) {
743             uint32_t m;
744             
745             m = ~(((uint32_t) -1) >> ia->prefix_len);
746             
747             if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
748                 return 1;
749         } else {
750             unsigned j;
751             unsigned char pl;
752             assert(a->proto == AVAHI_PROTO_INET6);
753
754             pl = ia->prefix_len;
755             
756             for (j = 0; j < 16; j++) {
757                 uint8_t m;
758
759                 if (pl == 0)
760                     return 1;
761                 
762                 if (pl >= 8) {
763                     m = 0xFF;
764                     pl -= 8;
765                 } else {
766                     m = ~(0xFF >> pl);
767                     pl = 0;
768                 }
769                 
770                 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
771                     break;
772             }
773         }
774     }
775
776     return 0;
777 }
778
779 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
780     AvahiInterface *i;
781     AvahiInterfaceAddress *j;
782     
783     assert(m);
784     assert(iface != AVAHI_IF_UNSPEC);
785     assert(a);
786
787     if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
788         return 0;
789
790     for (j = i->addresses; j; j = j->address_next)
791         if (avahi_address_cmp(a, &j->address) == 0)
792             return 1;
793
794     return 0;
795 }
796
797 AvahiIfIndex avahi_find_interface_for_address(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
798     AvahiInterface *i;
799     assert(m);
800
801     /* Some stupid OS don't support passing the interface index when a
802      * packet is recieved. We have to work around that limitation by
803      * looking for an interface that has the incoming address
804      * attached. This is sometimes ambiguous, but we have to live with
805      * it. */
806
807     for (i = m->interfaces; i; i = i->interface_next) {
808         AvahiInterfaceAddress *ai;
809
810         if (i->protocol != a->proto)
811             continue;
812         
813         for (ai = i->addresses; ai; ai = ai->address_next)
814             if (avahi_address_cmp(a, &ai->address) == 0)
815                 return i->hardware->index;
816     }
817
818     return AVAHI_IF_UNSPEC;
819 }