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