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