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