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