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