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