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