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