• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/iserver/avahi-0.6.25/avahi-core/

Lines Matching defs:*

0 /* $Id$ */
4 This file is part of avahi.
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.
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.
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.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
38 #include "iface.h"
39 #include "dns.h"
40 #include "socket.h"
41 #include "announce.h"
42 #include "util.h"
43 #include "log.h"
44 #include "multicast-lookup.h"
45 #include "querier.h"
47 void avahi_interface_address_update_rrs(AvahiInterfaceAddress *a, int remove_rrs) {
48 AvahiInterfaceMonitor *m;
50 assert(a);
51 m = a->monitor;
53 if((strcmp(a->interface->hardware->name,"eth0")==0) || (strcmp(a->interface->hardware->name,"vlan2")==0)|| (strcmp(a->interface->hardware->name,"wl0.1")==0)|| (strcmp(a->interface->hardware->name,"wl1.1")==0))
54 return;
56 if (m->list_complete &&
57 avahi_interface_address_is_relevant(a) &&
58 avahi_interface_is_relevant(a->interface) &&
59 !remove_rrs &&
60 m->server->config.publish_addresses &&
61 (m->server->state == AVAHI_SERVER_RUNNING ||
62 m->server->state == AVAHI_SERVER_REGISTERING)) {
64 /* Fill the entry group */
65 if (!a->entry_group)
66 a->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
68 if (!a->entry_group) /* OOM */
69 return;
71 if (avahi_s_entry_group_is_empty(a->entry_group)) {
72 char t[AVAHI_ADDRESS_STR_MAX];
73 AvahiProtocol p;
75 p = (a->interface->protocol == AVAHI_PROTO_INET && m->server->config.publish_a_on_ipv6) ||
76 (a->interface->protocol == AVAHI_PROTO_INET6 && m->server->config.publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : a->interface->protocol;
78 avahi_address_snprint(t, sizeof(t), &a->address);
79 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));
81 if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, p, 0, NULL, &a->address) < 0) {
82 avahi_log_warn(__FILE__": avahi_server_add_address() failed: %s", avahi_strerror(m->server->error));
83 avahi_s_entry_group_free(a->entry_group);
84 a->entry_group = NULL;
85 return;
88 avahi_s_entry_group_commit(a->entry_group);
90 } else {
92 /* Clear the entry group */
94 if (a->entry_group && !avahi_s_entry_group_is_empty(a->entry_group)) {
95 char t[AVAHI_ADDRESS_STR_MAX];
96 avahi_address_snprint(t, sizeof(t), &a->address);
98 if (avahi_s_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING &&
99 m->server->state == AVAHI_SERVER_REGISTERING)
100 avahi_server_decrease_host_rr_pending(m->server);
102 avahi_log_info("Withdrawing address record for %s on %s.", t, a->interface->hardware->name);
104 avahi_s_entry_group_reset(a->entry_group);
109 void avahi_interface_update_rrs(AvahiInterface *i, int remove_rrs) {
110 AvahiInterfaceAddress *a;
112 assert(i);
114 for (a = i->addresses; a; a = a->address_next)
115 avahi_interface_address_update_rrs(a, remove_rrs);
118 void avahi_hw_interface_update_rrs(AvahiHwInterface *hw, int remove_rrs) {
119 AvahiInterface *i;
120 AvahiInterfaceMonitor *m;
122 assert(hw);
123 m = hw->monitor;
125 for (i = hw->interfaces; i; i = i->by_hardware_next)
126 avahi_interface_update_rrs(i, remove_rrs);
128 if (m->list_complete &&
129 !remove_rrs &&
130 m->server->config.publish_workstation &&
131 (m->server->state == AVAHI_SERVER_RUNNING)) {
133 if (!hw->entry_group)
134 hw->entry_group = avahi_s_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL);
136 if (!hw->entry_group)
137 return; /* OOM */
139 if (avahi_s_entry_group_is_empty(hw->entry_group)) {
140 char name[AVAHI_LABEL_MAX], mac[256];
142 avahi_format_mac_address(mac, sizeof(mac), hw->mac_address, hw->mac_address_size);
143 snprintf(name, sizeof(name), "%s [%s]", m->server->host_name, mac);
145 if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AVAHI_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
146 avahi_log_warn(__FILE__": avahi_server_add_service() failed: %s", avahi_strerror(m->server->error));
147 avahi_s_entry_group_free(hw->entry_group);
148 hw->entry_group = NULL;
149 } else
150 avahi_s_entry_group_commit(hw->entry_group);
153 } else {
155 if (hw->entry_group && !avahi_s_entry_group_is_empty(hw->entry_group)) {
157 if (avahi_s_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING)
158 avahi_server_decrease_host_rr_pending(m->server);
160 avahi_s_entry_group_reset(hw->entry_group);
165 void avahi_interface_monitor_update_rrs(AvahiInterfaceMonitor *m, int remove_rrs) {
166 AvahiHwInterface *hw;
168 assert(m);
170 for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
171 avahi_hw_interface_update_rrs(hw, remove_rrs);
174 static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
175 char at[AVAHI_ADDRESS_STR_MAX];
176 int r;
177 assert(i);
179 if((strcmp(i->hardware->name,"eth0")==0)|| (strcmp(i->hardware->name,"vlan2")==0)|| (strcmp(i->hardware->name,"wl0.1")==0)|| (strcmp(i->hardware->name,"w11.1")==0))
180 return -1;
182 if (!!join == !!i->mcast_joined)
183 return 0;
185 if ((i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 < 0) ||
186 (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 < 0))
187 return -1;
189 if (join) {
190 AvahiInterfaceAddress *a;
192 /* Look if there's an address with global scope */
193 for (a = i->addresses; a; a = a->address_next)
194 if (a->global_scope)
195 break;
197 /* No address with a global scope has been found, so let's use
198 * any. */
199 if (!a)
200 a = i->addresses;
202 /* Hmm, there is no address available. */
203 if (!a)
204 return -1;
206 i->local_mcast_address = a->address;
209 avahi_log_info("%s mDNS multicast group on interface %s.%s with address %s.",
210 join ? "Joining" : "Leaving",
211 i->hardware->name,
212 avahi_proto_to_string(i->protocol),
213 avahi_address_snprint(at, sizeof(at), &i->local_mcast_address));
215 if (i->protocol == AVAHI_PROTO_INET6)
216 r = avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, &i->local_mcast_address.data.ipv6, i->hardware->index, join);
217 else {
218 assert(i->protocol == AVAHI_PROTO_INET);
220 r = avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &i->local_mcast_address.data.ipv4, i->hardware->index, join);
223 if (r < 0)
224 i->mcast_joined = 0;
225 else
226 i->mcast_joined = join;
228 return 0;
231 static int interface_mdns_mcast_rejoin(AvahiInterface *i) {
232 AvahiInterfaceAddress *a, *usable = NULL, *found = NULL;
233 assert(i);
235 if (!i->mcast_joined)
236 return 0;
238 /* Check whether old address we joined with is still available. If
239 * not, rejoin using an other address. */
241 for (a = i->addresses; a; a = a->address_next) {
242 if (a->global_scope && !usable)
243 usable = a;
245 if (avahi_address_cmp(&a->address, &i->local_mcast_address) == 0) {
247 if (a->global_scope)
248 /* No action necessary: the address still exists and
249 * has global scope. */
250 return 0;
252 found = a;
256 if (found && !usable)
257 /* No action necessary: the address still exists and no better one has been found */
258 return 0;
260 interface_mdns_mcast_join(i, 0);
261 return interface_mdns_mcast_join(i, 1);
264 void avahi_interface_address_free(AvahiInterfaceAddress *a) {
265 assert(a);
266 assert(a->interface);
268 avahi_interface_address_update_rrs(a, 1);
269 AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a);
271 if (a->entry_group)
272 avahi_s_entry_group_free(a->entry_group);
274 interface_mdns_mcast_rejoin(a->interface);
276 avahi_free(a);
279 void avahi_interface_free(AvahiInterface *i, int send_goodbye) {
280 assert(i);
282 /* Handle goodbyes and remove announcers */
283 avahi_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
284 avahi_response_scheduler_force(i->response_scheduler);
285 assert(!i->announcers);
287 if (i->mcast_joined)
288 interface_mdns_mcast_join(i, 0);
290 /* Remove queriers */
291 avahi_querier_free_all(i);
292 avahi_hashmap_free(i->queriers_by_key);
294 /* Remove local RRs */
295 avahi_interface_update_rrs(i, 1);
297 while (i->addresses)
298 avahi_interface_address_free(i->addresses);
300 avahi_response_scheduler_free(i->response_scheduler);
301 avahi_query_scheduler_free(i->query_scheduler);
302 avahi_probe_scheduler_free(i->probe_scheduler);
303 avahi_cache_free(i->cache);
305 AVAHI_LLIST_REMOVE(AvahiInterface, interface, i->monitor->interfaces, i);
306 AVAHI_LLIST_REMOVE(AvahiInterface, by_hardware, i->hardware->interfaces, i);
308 avahi_free(i);
311 void avahi_hw_interface_free(AvahiHwInterface *hw, int send_goodbye) {
312 assert(hw);
314 avahi_hw_interface_update_rrs(hw, 1);
316 while (hw->interfaces)
317 avahi_interface_free(hw->interfaces, send_goodbye);
319 if (hw->entry_group)
320 avahi_s_entry_group_free(hw->entry_group);
322 AVAHI_LLIST_REMOVE(AvahiHwInterface, hardware, hw->monitor->hw_interfaces, hw);
323 avahi_hashmap_remove(hw->monitor->hashmap, &hw->index);
325 avahi_free(hw->name);
326 avahi_free(hw);
329 AvahiInterface* avahi_interface_new(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, AvahiProtocol protocol) {
330 AvahiInterface *i;
332 assert(m);
333 assert(hw);
334 assert(AVAHI_PROTO_VALID(protocol));
336 if (!(i = avahi_new(AvahiInterface, 1)))
337 goto fail; /* OOM */
339 i->monitor = m;
340 i->hardware = hw;
341 i->protocol = protocol;
342 i->announcing = 0;
343 i->mcast_joined = 0;
345 AVAHI_LLIST_HEAD_INIT(AvahiInterfaceAddress, i->addresses);
346 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncer, i->announcers);
348 AVAHI_LLIST_HEAD_INIT(AvahiQuerier, i->queriers);
349 i->queriers_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL);
351 i->cache = avahi_cache_new(m->server, i);
352 i->response_scheduler = avahi_response_scheduler_new(i);
353 i->query_scheduler = avahi_query_scheduler_new(i);
354 i->probe_scheduler = avahi_probe_scheduler_new(i);
356 if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
357 goto fail; /* OOM */
359 AVAHI_LLIST_PREPEND(AvahiInterface, by_hardware, hw->interfaces, i);
360 AVAHI_LLIST_PREPEND(AvahiInterface, interface, m->interfaces, i);
362 return i;
364 fail:
366 if (i) {
367 if (i->cache)
368 avahi_cache_free(i->cache);
369 if (i->response_scheduler)
370 avahi_response_scheduler_free(i->response_scheduler);
371 if (i->query_scheduler)
372 avahi_query_scheduler_free(i->query_scheduler);
373 if (i->probe_scheduler)
374 avahi_probe_scheduler_free(i->probe_scheduler);
377 return NULL;
380 AvahiHwInterface *avahi_hw_interface_new(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
381 AvahiHwInterface *hw;
383 assert(m);
384 assert(AVAHI_IF_VALID(idx));
386 if (!(hw = avahi_new(AvahiHwInterface, 1)))
387 return NULL;
389 hw->monitor = m;
390 hw->name = NULL;
391 hw->flags_ok = 0;
392 hw->mtu = 1500;
393 hw->index = idx;
394 hw->mac_address_size = 0;
395 hw->entry_group = NULL;
397 AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces);
398 AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw);
400 avahi_hashmap_insert(m->hashmap, &hw->index, hw);
402 if (m->server->fd_ipv4 >= 0 || m->server->config.publish_a_on_ipv6)
403 avahi_interface_new(m, hw, AVAHI_PROTO_INET);
404 if (m->server->fd_ipv6 >= 0 || m->server->config.publish_aaaa_on_ipv4)
405 avahi_interface_new(m, hw, AVAHI_PROTO_INET6);
407 return hw;
410 AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *addr, unsigned prefix_len) {
411 AvahiInterfaceAddress *a;
413 assert(m);
414 assert(i);
416 if (!(a = avahi_new(AvahiInterfaceAddress, 1)))
417 return NULL;
419 a->interface = i;
420 a->monitor = m;
421 a->address = *addr;
422 a->prefix_len = prefix_len;
423 a->global_scope = 0;
424 a->entry_group = NULL;
426 AVAHI_LLIST_PREPEND(AvahiInterfaceAddress, address, i->addresses, a);
428 return a;
431 void avahi_interface_check_relevant(AvahiInterface *i) {
432 int b;
433 AvahiInterfaceMonitor *m;
435 assert(i);
436 m = i->monitor;
438 b = avahi_interface_is_relevant(i);
440 if (m->list_complete && b && !i->announcing) {
441 interface_mdns_mcast_join(i, 1);
443 if (i->mcast_joined) {
444 avahi_log_info("New relevant interface %s.%s for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
446 i->announcing = 1;
447 avahi_announce_interface(m->server, i);
448 avahi_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
451 } else if (!b && i->announcing) {
452 avahi_log_info("Interface %s.%s no longer relevant for mDNS.", i->hardware->name, avahi_proto_to_string(i->protocol));
454 interface_mdns_mcast_join(i, 0);
456 avahi_goodbye_interface(m->server, i, 0, 1);
457 avahi_querier_free_all(i);
459 avahi_response_scheduler_clear(i->response_scheduler);
460 avahi_query_scheduler_clear(i->query_scheduler);
461 avahi_probe_scheduler_clear(i->probe_scheduler);
462 avahi_cache_flush(i->cache);
464 i->announcing = 0;
466 } else
467 interface_mdns_mcast_rejoin(i);
470 void avahi_hw_interface_check_relevant(AvahiHwInterface *hw) {
471 AvahiInterface *i;
473 assert(hw);
475 for (i = hw->interfaces; i; i = i->by_hardware_next)
476 avahi_interface_check_relevant(i);
479 void avahi_interface_monitor_check_relevant(AvahiInterfaceMonitor *m) {
480 AvahiInterface *i;
482 assert(m);
484 for (i = m->interfaces; i; i = i->interface_next)
485 avahi_interface_check_relevant(i);
488 AvahiInterfaceMonitor *avahi_interface_monitor_new(AvahiServer *s) {
489 AvahiInterfaceMonitor *m = NULL;
491 if (!(m = avahi_new0(AvahiInterfaceMonitor, 1)))
492 return NULL; /* OOM */
494 m->server = s;
495 m->list_complete = 0;
496 m->hashmap = avahi_hashmap_new(avahi_int_hash, avahi_int_equal, NULL, NULL);
498 AVAHI_LLIST_HEAD_INIT(AvahiInterface, m->interfaces);
499 AVAHI_LLIST_HEAD_INIT(AvahiHwInterface, m->hw_interfaces);
501 if (avahi_interface_monitor_init_osdep(m) < 0)
502 goto fail;
504 return m;
506 fail:
507 avahi_interface_monitor_free(m);
508 return NULL;
511 void avahi_interface_monitor_free(AvahiInterfaceMonitor *m) {
512 assert(m);
514 while (m->hw_interfaces)
515 avahi_hw_interface_free(m->hw_interfaces, 1);
517 assert(!m->interfaces);
519 avahi_interface_monitor_free_osdep(m);
521 if (m->hashmap)
522 avahi_hashmap_free(m->hashmap);
524 avahi_free(m);
528 AvahiInterface* avahi_interface_monitor_get_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx, AvahiProtocol protocol) {
529 AvahiHwInterface *hw;
530 AvahiInterface *i;
532 assert(m);
533 assert(idx >= 0);
534 assert(protocol != AVAHI_PROTO_UNSPEC);
536 if (!(hw = avahi_interface_monitor_get_hw_interface(m, idx)))
537 return NULL;
539 for (i = hw->interfaces; i; i = i->by_hardware_next)
540 if (i->protocol == protocol)
541 return i;
543 return NULL;
546 AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor *m, AvahiIfIndex idx) {
547 assert(m);
548 assert(idx >= 0);
550 return avahi_hashmap_lookup(m->hashmap, &idx);
553 AvahiInterfaceAddress* avahi_interface_monitor_get_address(AvahiInterfaceMonitor *m, AvahiInterface *i, const AvahiAddress *raddr) {
554 AvahiInterfaceAddress *ia;
556 assert(m);
557 assert(i);
558 assert(raddr);
560 for (ia = i->addresses; ia; ia = ia->address_next)
561 if (avahi_address_cmp(&ia->address, raddr) == 0)
562 return ia;
564 return NULL;
567 void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, uint16_t port) {
568 assert(i);
569 assert(p);
571 if (!i->announcing)
572 return;
574 assert(!a || a->proto == i->protocol);
576 if (i->protocol == AVAHI_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
577 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);
578 else if (i->protocol == AVAHI_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
579 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);
582 void avahi_interface_send_packet(AvahiInterface *i, AvahiDnsPacket *p) {
583 assert(i);
584 assert(p);
586 avahi_interface_send_packet_unicast(i, p, NULL, 0);
589 int avahi_interface_post_query(AvahiInterface *i, AvahiKey *key, int immediately, unsigned *ret_id) {
590 assert(i);
591 assert(key);
593 if (!i->announcing)
594 return 0;
596 return avahi_query_scheduler_post(i->query_scheduler, key, immediately, ret_id);
599 int avahi_interface_withraw_query(AvahiInterface *i, unsigned id) {
601 return avahi_query_scheduler_withdraw_by_id(i->query_scheduler, id);
604 int avahi_interface_post_response(AvahiInterface *i, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
605 assert(i);
606 assert(record);
608 if (!i->announcing)
609 return 0;
611 return avahi_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
614 int avahi_interface_post_probe(AvahiInterface *i, AvahiRecord *record, int immediately) {
615 assert(i);
616 assert(record);
618 if (!i->announcing)
619 return 0;
621 return avahi_probe_scheduler_post(i->probe_scheduler, record, immediately);
624 int avahi_dump_caches(AvahiInterfaceMonitor *m, AvahiDumpCallback callback, void* userdata) {
625 AvahiInterface *i;
626 assert(m);
628 for (i = m->interfaces; i; i = i->interface_next) {
629 if (avahi_interface_is_relevant(i)) {
630 char ln[256];
631 snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, avahi_proto_to_string(i->protocol));
632 callback(ln, userdata);
633 if (avahi_cache_dump(i->cache, callback, userdata) < 0)
634 return -1;
638 return 0;
641 static int avahi_interface_is_relevant_internal(AvahiInterface *i) {
642 AvahiInterfaceAddress *a;
644 assert(i);
646 if (!i->hardware->flags_ok)
647 return 0;
649 for (a = i->addresses; a; a = a->address_next)
650 if (avahi_interface_address_is_relevant(a))
651 return 1;
653 return 0;
656 int avahi_interface_is_relevant(AvahiInterface *i) {
657 AvahiStringList *l;
658 assert(i);
660 for (l = i->monitor->server->config.deny_interfaces; l; l = l->next)
661 if (strcasecmp((char*) l->text, i->hardware->name) == 0)
662 return 0;
664 if (i->monitor->server->config.allow_interfaces) {
666 for (l = i->monitor->server->config.allow_interfaces; l; l = l->next)
667 if (strcasecmp((char*) l->text, i->hardware->name) == 0)
668 goto good;
670 return 0;
673 good:
674 return avahi_interface_is_relevant_internal(i);
677 int avahi_interface_address_is_relevant(AvahiInterfaceAddress *a) {
678 AvahiInterfaceAddress *b;
679 assert(a);
681 /* Publish public IP addresses */
682 if (a->global_scope)
683 return 1;
684 else {
686 /* Publish link local IP addresses if they are the only ones on the link */
687 for (b = a->interface->addresses; b; b = b->address_next) {
688 if (b == a)
689 continue;
691 if (b->global_scope)
692 return 0;
695 return 1;
698 return 0;
701 int avahi_interface_match(AvahiInterface *i, AvahiIfIndex idx, AvahiProtocol protocol) {
702 assert(i);
704 if (idx != AVAHI_IF_UNSPEC && idx != i->hardware->index)
705 return 0;
707 if (protocol != AVAHI_PROTO_UNSPEC && protocol != i->protocol)
708 return 0;
710 return 1;
713 void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, AvahiIfIndex interface, AvahiProtocol protocol, AvahiInterfaceMonitorWalkCallback callback, void* userdata) {
714 assert(m);
715 assert(callback);
717 if (interface != AVAHI_IF_UNSPEC) {
718 if (protocol != AVAHI_PROTO_UNSPEC) {
719 AvahiInterface *i;
721 if ((i = avahi_interface_monitor_get_interface(m, interface, protocol)))
722 callback(m, i, userdata);
724 } else {
725 AvahiHwInterface *hw;
726 AvahiInterface *i;
728 if ((hw = avahi_interface_monitor_get_hw_interface(m, interface)))
729 for (i = hw->interfaces; i; i = i->by_hardware_next)
730 if (avahi_interface_match(i, interface, protocol))
731 callback(m, i, userdata);
734 } else {
735 AvahiInterface *i;
737 for (i = m->interfaces; i; i = i->interface_next)
738 if (avahi_interface_match(i, interface, protocol))
739 callback(m, i, userdata);
744 int avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
745 AvahiInterface *i;
746 AvahiInterfaceAddress *ia;
747 assert(m);
748 assert(a);
750 for (i = m->interfaces; i; i = i->interface_next)
751 for (ia = i->addresses; ia; ia = ia->address_next)
752 if (avahi_address_cmp(a, &ia->address) == 0)
753 return 1;
755 return 0;
758 int avahi_interface_address_on_link(AvahiInterface *i, const AvahiAddress *a) {
759 AvahiInterfaceAddress *ia;
761 assert(i);
762 assert(a);
764 if (a->proto != i->protocol)
765 return 0;
767 for (ia = i->addresses; ia; ia = ia->address_next) {
769 if (a->proto == AVAHI_PROTO_INET) {
770 uint32_t m;
772 m = ~(((uint32_t) -1) >> ia->prefix_len);
774 if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
775 return 1;
776 } else {
777 unsigned j;
778 unsigned char pl;
779 assert(a->proto == AVAHI_PROTO_INET6);
781 pl = ia->prefix_len;
783 for (j = 0; j < 16; j++) {
784 uint8_t m;
786 if (pl == 0)
787 return 1;
789 if (pl >= 8) {
790 m = 0xFF;
791 pl -= 8;
792 } else {
793 m = ~(0xFF >> pl);
794 pl = 0;
797 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
798 break;
803 return 0;
806 int avahi_interface_has_address(AvahiInterfaceMonitor *m, AvahiIfIndex iface, const AvahiAddress *a) {
807 AvahiInterface *i;
808 AvahiInterfaceAddress *j;
810 assert(m);
811 assert(iface != AVAHI_IF_UNSPEC);
812 assert(a);
814 if (!(i = avahi_interface_monitor_get_interface(m, iface, a->proto)))
815 return 0;
817 for (j = i->addresses; j; j = j->address_next)
818 if (avahi_address_cmp(a, &j->address) == 0)
819 return 1;
821 return 0;
824 AvahiIfIndex avahi_find_interface_for_address(AvahiInterfaceMonitor *m, const AvahiAddress *a) {
825 AvahiInterface *i;
826 assert(m);
828 /* Some stupid OS don't support passing the interface index when a
829 * packet is received. We have to work around that limitation by
830 * looking for an interface that has the incoming address
831 * attached. This is sometimes ambiguous, but we have to live with
832 * it. */
834 for (i = m->interfaces; i; i = i->interface_next) {
835 AvahiInterfaceAddress *ai;
837 if (i->protocol != a->proto)
838 continue;
840 for (ai = i->addresses; ai; ai = ai->address_next)
841 if (avahi_address_cmp(a, &ai->address) == 0)
842 return i->hardware->index;
845 return AVAHI_IF_UNSPEC;