]> git.meshlink.io Git - catta/blob - src/iface.c
Fix some compiler warnings found by GCC 9 and Clang 10.
[catta] / src / iface.c
1 /***
2   This file is part of catta.
3
4   catta 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   catta 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 catta; 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 <catta/error.h>
33 #include <catta/malloc.h>
34 #include <catta/domain.h>
35 #include <catta/log.h>
36
37 #include "iface.h"
38 #include "dns.h"
39 #include "socket.h"
40 #include "announce.h"
41 #include "util.h"
42 #include "multicast-lookup.h"
43 #include "querier.h"
44
45 void catta_interface_address_update_rrs(CattaInterfaceAddress *a, int remove_rrs) {
46     CattaInterfaceMonitor *m;
47
48     assert(a);
49     m = a->monitor;
50
51     if (m->list_complete &&
52         catta_interface_address_is_relevant(a) &&
53         catta_interface_is_relevant(a->iface) &&
54         !remove_rrs &&
55         m->server->config.publish_addresses &&
56         (m->server->state == CATTA_SERVER_RUNNING ||
57         m->server->state == CATTA_SERVER_REGISTERING)) {
58
59         /* Fill the entry group */
60         if (!a->entry_group)
61             a->entry_group = catta_s_entry_group_new(m->server, catta_host_rr_entry_group_callback, NULL);
62
63         if (!a->entry_group) /* OOM */
64             return;
65
66         if (catta_s_entry_group_is_empty(a->entry_group)) {
67             char t[CATTA_ADDRESS_STR_MAX];
68             CattaProtocol p;
69
70             p = (a->iface->protocol == CATTA_PROTO_INET && m->server->config.publish_a_on_ipv6) ||
71                 (a->iface->protocol == CATTA_PROTO_INET6 && m->server->config.publish_aaaa_on_ipv4) ? CATTA_PROTO_UNSPEC : a->iface->protocol;
72
73             catta_address_snprint(t, sizeof(t), &a->address);
74             catta_log_info("Registering new address record for %s on %s.%s.", t, a->iface->hardware->name, p == CATTA_PROTO_UNSPEC ? "*" : catta_proto_to_string(p));
75
76             if (catta_server_add_address(m->server, a->entry_group, a->iface->hardware->index, p, m->server->config.publish_no_reverse ? CATTA_PUBLISH_NO_REVERSE : 0, NULL, &a->address) < 0) {
77                 catta_log_warn(__FILE__": catta_server_add_address() failed: %s", catta_strerror(m->server->error));
78                 catta_s_entry_group_free(a->entry_group);
79                 a->entry_group = NULL;
80                 return;
81             }
82
83             catta_s_entry_group_commit(a->entry_group);
84         }
85     } else {
86
87         /* Clear the entry group */
88
89         if (a->entry_group && !catta_s_entry_group_is_empty(a->entry_group)) {
90             char t[CATTA_ADDRESS_STR_MAX];
91             catta_address_snprint(t, sizeof(t), &a->address);
92
93             catta_log_info("Withdrawing address record for %s on %s.", t, a->iface->hardware->name);
94
95             if (catta_s_entry_group_get_state(a->entry_group) == CATTA_ENTRY_GROUP_REGISTERING &&
96                 m->server->state == CATTA_SERVER_REGISTERING)
97                 catta_server_decrease_host_rr_pending(m->server);
98
99             catta_s_entry_group_reset(a->entry_group);
100         }
101     }
102 }
103
104 void catta_interface_update_rrs(CattaInterface *i, int remove_rrs) {
105     CattaInterfaceAddress *a;
106
107     assert(i);
108
109     for (a = i->addresses; a; a = a->address_next)
110         catta_interface_address_update_rrs(a, remove_rrs);
111 }
112
113 void catta_hw_interface_update_rrs(CattaHwInterface *hw, int remove_rrs) {
114     CattaInterface *i;
115     CattaInterfaceMonitor *m;
116
117     assert(hw);
118     m = hw->monitor;
119
120     for (i = hw->interfaces; i; i = i->by_hardware_next)
121         catta_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 == CATTA_SERVER_RUNNING)) {
127
128         if (!hw->entry_group)
129             hw->entry_group = catta_s_entry_group_new(m->server, catta_host_rr_entry_group_callback, NULL);
130
131         if (!hw->entry_group)
132             return; /* OOM */
133
134         if (catta_s_entry_group_is_empty(hw->entry_group)) {
135             char name[CATTA_LABEL_MAX + 256 + 20], unescaped[CATTA_LABEL_MAX], mac[256];
136             const char *p = m->server->host_name;
137
138             catta_unescape_label(&p, unescaped, sizeof(unescaped));
139             catta_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 (catta_server_add_service(m->server, hw->entry_group, hw->index, CATTA_PROTO_UNSPEC, 0, name, "_workstation._tcp", NULL, NULL, 9, NULL) < 0) {
143                 catta_log_warn(__FILE__": catta_server_add_service() failed: %s", catta_strerror(m->server->error));
144                 catta_s_entry_group_free(hw->entry_group);
145                 hw->entry_group = NULL;
146             } else
147                 catta_s_entry_group_commit(hw->entry_group);
148         }
149
150     } else {
151
152         if (hw->entry_group && !catta_s_entry_group_is_empty(hw->entry_group)) {
153
154             catta_log_info("Withdrawing workstation service for %s.", hw->name);
155
156             if (catta_s_entry_group_get_state(hw->entry_group) == CATTA_ENTRY_GROUP_REGISTERING &&
157                 m->server->state == CATTA_SERVER_REGISTERING)
158                 catta_server_decrease_host_rr_pending(m->server);
159
160             catta_s_entry_group_reset(hw->entry_group);
161         }
162     }
163 }
164
165 void catta_interface_monitor_update_rrs(CattaInterfaceMonitor *m, int remove_rrs) {
166     CattaHwInterface *hw;
167
168     assert(m);
169
170     for (hw = m->hw_interfaces; hw; hw = hw->hardware_next)
171         catta_hw_interface_update_rrs(hw, remove_rrs);
172 }
173
174 static int interface_mdns_mcast_join(CattaInterface *i, int join) {
175     char at[CATTA_ADDRESS_STR_MAX];
176     int r;
177     assert(i);
178
179     if (!!join  == !!i->mcast_joined)
180         return 0;
181
182     if ((i->protocol == CATTA_PROTO_INET6 && i->monitor->server->fd_ipv6 < 0) ||
183         (i->protocol == CATTA_PROTO_INET && i->monitor->server->fd_ipv4 < 0))
184         return -1;
185
186     if (join) {
187         CattaInterfaceAddress *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     catta_log_info("%s mDNS multicast group on interface %s.%s with address %s.",
207                    join ? "Joining" : "Leaving",
208                    i->hardware->name,
209                    catta_proto_to_string(i->protocol),
210                    catta_address_snprint(at, sizeof(at), &i->local_mcast_address));
211
212     if (i->protocol == CATTA_PROTO_INET6)
213         r = catta_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 == CATTA_PROTO_INET);
216
217         r = catta_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(CattaInterface *i) {
229     CattaInterfaceAddress *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 (catta_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 catta_interface_address_free(CattaInterfaceAddress *a) {
262     assert(a);
263     assert(a->iface);
264
265     catta_interface_address_update_rrs(a, 1);
266     CATTA_LLIST_REMOVE(CattaInterfaceAddress, address, a->iface->addresses, a);
267
268     if (a->entry_group)
269         catta_s_entry_group_free(a->entry_group);
270
271     interface_mdns_mcast_rejoin(a->iface);
272
273     catta_free(a);
274 }
275
276 void catta_interface_free(CattaInterface *i, int send_goodbye) {
277     assert(i);
278
279     /* Handle goodbyes and remove announcers */
280     catta_goodbye_interface(i->monitor->server, i, send_goodbye, 1);
281     catta_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     catta_querier_free_all(i);
289     catta_hashmap_free(i->queriers_by_key);
290
291     /* Remove local RRs */
292     catta_interface_update_rrs(i, 1);
293
294     while (i->addresses)
295         catta_interface_address_free(i->addresses);
296
297     catta_response_scheduler_free(i->response_scheduler);
298     catta_query_scheduler_free(i->query_scheduler);
299     catta_probe_scheduler_free(i->probe_scheduler);
300     catta_cache_free(i->cache);
301
302     CATTA_LLIST_REMOVE(CattaInterface, iface, i->monitor->interfaces, i);
303     CATTA_LLIST_REMOVE(CattaInterface, by_hardware, i->hardware->interfaces, i);
304
305     catta_free(i);
306 }
307
308 void catta_hw_interface_free(CattaHwInterface *hw, int send_goodbye) {
309     assert(hw);
310
311     catta_hw_interface_update_rrs(hw, 1);
312
313     while (hw->interfaces)
314         catta_interface_free(hw->interfaces, send_goodbye);
315
316     if (hw->entry_group)
317         catta_s_entry_group_free(hw->entry_group);
318
319     CATTA_LLIST_REMOVE(CattaHwInterface, hardware, hw->monitor->hw_interfaces, hw);
320     catta_hashmap_remove(hw->monitor->hashmap, &hw->index);
321
322     catta_free(hw->name);
323     catta_free(hw);
324 }
325
326 CattaInterface* catta_interface_new(CattaInterfaceMonitor *m, CattaHwInterface *hw, CattaProtocol protocol) {
327     CattaInterface *i;
328
329     assert(m);
330     assert(hw);
331     assert(CATTA_PROTO_VALID(protocol));
332
333     if (!(i = catta_new(CattaInterface, 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     CATTA_LLIST_HEAD_INIT(CattaInterfaceAddress, i->addresses);
343     CATTA_LLIST_HEAD_INIT(CattaAnnouncer, i->announcers);
344
345     CATTA_LLIST_HEAD_INIT(CattaQuerier, i->queriers);
346     i->queriers_by_key = catta_hashmap_new((CattaHashFunc) catta_key_hash, (CattaEqualFunc) catta_key_equal, NULL, NULL);
347
348     i->cache = catta_cache_new(m->server, i);
349     i->response_scheduler = catta_response_scheduler_new(i);
350     i->query_scheduler = catta_query_scheduler_new(i);
351     i->probe_scheduler = catta_probe_scheduler_new(i);
352
353     if (!i->cache || !i->response_scheduler || !i->query_scheduler || !i->probe_scheduler)
354         goto fail; /* OOM */
355
356     CATTA_LLIST_PREPEND(CattaInterface, by_hardware, hw->interfaces, i);
357     CATTA_LLIST_PREPEND(CattaInterface, iface, m->interfaces, i);
358
359     return i;
360
361 fail:
362
363     if (i) {
364         if (i->cache)
365             catta_cache_free(i->cache);
366         if (i->response_scheduler)
367             catta_response_scheduler_free(i->response_scheduler);
368         if (i->query_scheduler)
369             catta_query_scheduler_free(i->query_scheduler);
370         if (i->probe_scheduler)
371             catta_probe_scheduler_free(i->probe_scheduler);
372     }
373
374     return NULL;
375 }
376
377 CattaHwInterface *catta_hw_interface_new(CattaInterfaceMonitor *m, CattaIfIndex idx) {
378     CattaHwInterface *hw;
379
380     assert(m);
381     assert(CATTA_IF_VALID(idx));
382
383     if  (!(hw = catta_new(CattaHwInterface, 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     hw->ratelimit_begin.tv_sec = 0;
394     hw->ratelimit_begin.tv_usec = 0;
395     hw->ratelimit_counter = 0;
396
397     CATTA_LLIST_HEAD_INIT(CattaInterface, hw->interfaces);
398     CATTA_LLIST_PREPEND(CattaHwInterface, hardware, m->hw_interfaces, hw);
399
400     catta_hashmap_insert(m->hashmap, &hw->index, hw);
401
402     if (m->server->fd_ipv4 >= 0 || m->server->config.publish_a_on_ipv6)
403         catta_interface_new(m, hw, CATTA_PROTO_INET);
404     if (m->server->fd_ipv6 >= 0 || m->server->config.publish_aaaa_on_ipv4)
405         catta_interface_new(m, hw, CATTA_PROTO_INET6);
406
407     return hw;
408 }
409
410 CattaInterfaceAddress *catta_interface_address_new(CattaInterfaceMonitor *m, CattaInterface *i, const CattaAddress *addr, unsigned prefix_len) {
411     CattaInterfaceAddress *a;
412
413     assert(m);
414     assert(i);
415
416     if (!(a = catta_new(CattaInterfaceAddress, 1)))
417         return NULL;
418
419     a->iface = i;
420     a->monitor = m;
421     a->address = *addr;
422     a->prefix_len = prefix_len;
423     a->global_scope = 0;
424     a->deprecated = 0;
425     a->entry_group = NULL;
426
427     CATTA_LLIST_PREPEND(CattaInterfaceAddress, address, i->addresses, a);
428
429     return a;
430 }
431
432 void catta_interface_check_relevant(CattaInterface *i) {
433     int b;
434     CattaInterfaceMonitor *m;
435
436     assert(i);
437     m = i->monitor;
438
439     b = catta_interface_is_relevant(i);
440
441     if (m->list_complete && b && !i->announcing) {
442         interface_mdns_mcast_join(i, 1);
443
444         if (i->mcast_joined) {
445             catta_log_info("New relevant interface %s.%s for mDNS.", i->hardware->name, catta_proto_to_string(i->protocol));
446
447             i->announcing = 1;
448             catta_announce_interface(m->server, i);
449             catta_multicast_lookup_engine_new_interface(m->server->multicast_lookup_engine, i);
450         }
451
452     } else if (!b && i->announcing) {
453         catta_log_info("Interface %s.%s no longer relevant for mDNS.", i->hardware->name, catta_proto_to_string(i->protocol));
454
455         interface_mdns_mcast_join(i, 0);
456
457         catta_goodbye_interface(m->server, i, 0, 1);
458         catta_querier_free_all(i);
459
460         catta_response_scheduler_clear(i->response_scheduler);
461         catta_query_scheduler_clear(i->query_scheduler);
462         catta_probe_scheduler_clear(i->probe_scheduler);
463         catta_cache_flush(i->cache);
464
465         i->announcing = 0;
466
467     } else
468         interface_mdns_mcast_rejoin(i);
469 }
470
471 void catta_hw_interface_check_relevant(CattaHwInterface *hw) {
472     CattaInterface *i;
473
474     assert(hw);
475
476     for (i = hw->interfaces; i; i = i->by_hardware_next)
477         catta_interface_check_relevant(i);
478 }
479
480 void catta_interface_monitor_check_relevant(CattaInterfaceMonitor *m) {
481     CattaInterface *i;
482
483     assert(m);
484
485     for (i = m->interfaces; i; i = i->iface_next)
486         catta_interface_check_relevant(i);
487 }
488
489 CattaInterfaceMonitor *catta_interface_monitor_new(CattaServer *s) {
490     CattaInterfaceMonitor *m = NULL;
491
492     if (!(m = catta_new0(CattaInterfaceMonitor, 1)))
493         return NULL; /* OOM */
494
495     m->server = s;
496     m->list_complete = 0;
497     m->hashmap = catta_hashmap_new(catta_int_hash, catta_int_equal, NULL, NULL);
498
499     CATTA_LLIST_HEAD_INIT(CattaInterface, m->interfaces);
500     CATTA_LLIST_HEAD_INIT(CattaHwInterface, m->hw_interfaces);
501
502     if (catta_interface_monitor_init_osdep(m) < 0)
503         goto fail;
504
505     return m;
506
507 fail:
508     catta_interface_monitor_free(m);
509     return NULL;
510 }
511
512 void catta_interface_monitor_free(CattaInterfaceMonitor *m) {
513     assert(m);
514
515     while (m->hw_interfaces)
516         catta_hw_interface_free(m->hw_interfaces, 1);
517
518     assert(!m->interfaces);
519
520     catta_interface_monitor_free_osdep(m);
521
522     if (m->hashmap)
523         catta_hashmap_free(m->hashmap);
524
525     catta_free(m);
526 }
527
528
529 CattaInterface* catta_interface_monitor_get_interface(CattaInterfaceMonitor *m, CattaIfIndex idx, CattaProtocol protocol) {
530     CattaHwInterface *hw;
531     CattaInterface *i;
532
533     assert(m);
534     assert(idx >= 0);
535     assert(protocol != CATTA_PROTO_UNSPEC);
536
537     if (!(hw = catta_interface_monitor_get_hw_interface(m, idx)))
538         return NULL;
539
540     for (i = hw->interfaces; i; i = i->by_hardware_next)
541         if (i->protocol == protocol)
542             return i;
543
544     return NULL;
545 }
546
547 CattaHwInterface* catta_interface_monitor_get_hw_interface(CattaInterfaceMonitor *m, CattaIfIndex idx) {
548     assert(m);
549     assert(idx >= 0);
550
551     return catta_hashmap_lookup(m->hashmap, &idx);
552 }
553
554 CattaInterfaceAddress* catta_interface_monitor_get_address(CattaInterfaceMonitor *m, CattaInterface *i, const CattaAddress *raddr) {
555     CattaInterfaceAddress *ia;
556
557     assert(m);
558     assert(i);
559     assert(raddr);
560
561     for (ia = i->addresses; ia; ia = ia->address_next)
562         if (catta_address_cmp(&ia->address, raddr) == 0)
563             return ia;
564
565     return NULL;
566 }
567
568 void catta_interface_send_packet_unicast(CattaInterface *i, CattaDnsPacket *p, const CattaAddress *a, uint16_t port) {
569     assert(i);
570     assert(p);
571
572     if (!i->announcing)
573         return;
574
575     assert(!a || a->proto == i->protocol);
576
577     if (i->monitor->server->config.ratelimit_interval > 0) {
578         struct timeval now, end;
579
580         gettimeofday(&now, NULL);
581
582         end = i->hardware->ratelimit_begin;
583         catta_timeval_add(&end, i->monitor->server->config.ratelimit_interval);
584
585         if (i->hardware->ratelimit_begin.tv_sec <= 0 ||
586             catta_timeval_compare(&end, &now) < 0) {
587
588             i->hardware->ratelimit_begin = now;
589             i->hardware->ratelimit_counter = 0;
590         }
591
592         if (i->hardware->ratelimit_counter > i->monitor->server->config.ratelimit_burst)
593             return;
594
595         i->hardware->ratelimit_counter++;
596     }
597
598     if (i->protocol == CATTA_PROTO_INET && i->monitor->server->fd_ipv4 >= 0)
599         catta_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);
600     else if (i->protocol == CATTA_PROTO_INET6 && i->monitor->server->fd_ipv6 >= 0)
601         catta_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);
602 }
603
604 void catta_interface_send_packet(CattaInterface *i, CattaDnsPacket *p) {
605     assert(i);
606     assert(p);
607
608     catta_interface_send_packet_unicast(i, p, NULL, 0);
609 }
610
611 int catta_interface_post_query(CattaInterface *i, CattaKey *key, int immediately, unsigned *ret_id) {
612     assert(i);
613     assert(key);
614
615     if (!i->announcing)
616         return 0;
617
618     return catta_query_scheduler_post(i->query_scheduler, key, immediately, ret_id);
619 }
620
621 int catta_interface_withraw_query(CattaInterface *i, unsigned id) {
622
623     return catta_query_scheduler_withdraw_by_id(i->query_scheduler, id);
624 }
625
626 int catta_interface_post_response(CattaInterface *i, CattaRecord *record, int flush_cache, const CattaAddress *querier, int immediately) {
627     assert(i);
628     assert(record);
629
630     if (!i->announcing)
631         return 0;
632
633     return catta_response_scheduler_post(i->response_scheduler, record, flush_cache, querier, immediately);
634 }
635
636 int catta_interface_post_probe(CattaInterface *i, CattaRecord *record, int immediately) {
637     assert(i);
638     assert(record);
639
640     if (!i->announcing)
641         return 0;
642
643     return catta_probe_scheduler_post(i->probe_scheduler, record, immediately);
644 }
645
646 int catta_dump_caches(CattaInterfaceMonitor *m, CattaDumpCallback callback, void* userdata) {
647     CattaInterface *i;
648     assert(m);
649
650     for (i = m->interfaces; i; i = i->iface_next) {
651         if (catta_interface_is_relevant(i)) {
652             char ln[256];
653             snprintf(ln, sizeof(ln), ";;; INTERFACE %s.%s ;;;", i->hardware->name, catta_proto_to_string(i->protocol));
654             callback(ln, userdata);
655             if (catta_cache_dump(i->cache, callback, userdata) < 0)
656                 return -1;
657         }
658     }
659
660     return 0;
661 }
662
663 static int catta_interface_is_relevant_internal(CattaInterface *i) {
664     CattaInterfaceAddress *a;
665
666     assert(i);
667
668     if (!i->hardware->flags_ok)
669         return 0;
670
671     for (a = i->addresses; a; a = a->address_next)
672         if (catta_interface_address_is_relevant(a))
673             return 1;
674
675     return 0;
676 }
677
678 int catta_interface_is_relevant(CattaInterface *i) {
679     CattaStringList *l;
680     assert(i);
681
682     for (l = i->monitor->server->config.deny_interfaces; l; l = l->next)
683         if (strcasecmp((char*) l->text, i->hardware->name) == 0)
684             return 0;
685
686     if (i->monitor->server->config.allow_interfaces) {
687
688         for (l = i->monitor->server->config.allow_interfaces; l; l = l->next)
689             if (strcasecmp((char*) l->text, i->hardware->name) == 0)
690                 goto good;
691
692         return 0;
693     }
694
695 good:
696     return catta_interface_is_relevant_internal(i);
697 }
698
699 int catta_interface_address_is_relevant(CattaInterfaceAddress *a) {
700     CattaInterfaceAddress *b;
701     assert(a);
702
703     /* Publish public and non-deprecated IP addresses */
704     if (a->global_scope && !a->deprecated)
705         return 1;
706
707     /* Publish link-local and deprecated IP addresses only if they are
708      * the only ones on the link */
709     for (b = a->iface->addresses; b; b = b->address_next) {
710         if (b == a)
711             continue;
712
713         if (b->global_scope && !b->deprecated)
714             return 0;
715     }
716
717     return 1;
718 }
719
720 int catta_interface_match(CattaInterface *i, CattaIfIndex idx, CattaProtocol protocol) {
721     assert(i);
722
723     if (idx != CATTA_IF_UNSPEC && idx != i->hardware->index)
724         return 0;
725
726     if (protocol != CATTA_PROTO_UNSPEC && protocol != i->protocol)
727         return 0;
728
729     return 1;
730 }
731
732 void catta_interface_monitor_walk(CattaInterfaceMonitor *m, CattaIfIndex iface, CattaProtocol protocol, CattaInterfaceMonitorWalkCallback callback, void* userdata) {
733     assert(m);
734     assert(callback);
735
736     if (iface != CATTA_IF_UNSPEC) {
737         if (protocol != CATTA_PROTO_UNSPEC) {
738             CattaInterface *i;
739
740             if ((i = catta_interface_monitor_get_interface(m, iface, protocol)))
741                 callback(m, i, userdata);
742
743         } else {
744             CattaHwInterface *hw;
745             CattaInterface *i;
746
747             if ((hw = catta_interface_monitor_get_hw_interface(m, iface)))
748                 for (i = hw->interfaces; i; i = i->by_hardware_next)
749                     if (catta_interface_match(i, iface, protocol))
750                         callback(m, i, userdata);
751         }
752
753     } else {
754         CattaInterface *i;
755
756         for (i = m->interfaces; i; i = i->iface_next)
757             if (catta_interface_match(i, iface, protocol))
758                 callback(m, i, userdata);
759     }
760 }
761
762
763 int catta_address_is_local(CattaInterfaceMonitor *m, const CattaAddress *a) {
764     CattaInterface *i;
765     CattaInterfaceAddress *ia;
766     assert(m);
767     assert(a);
768
769     for (i = m->interfaces; i; i = i->iface_next)
770         for (ia = i->addresses; ia; ia = ia->address_next)
771             if (catta_address_cmp(a, &ia->address) == 0)
772                 return 1;
773
774     return 0;
775 }
776
777 int catta_interface_address_on_link(CattaInterface *i, const CattaAddress *a) {
778     CattaInterfaceAddress *ia;
779
780     assert(i);
781     assert(a);
782
783     if (a->proto != i->protocol)
784         return 0;
785
786     for (ia = i->addresses; ia; ia = ia->address_next) {
787
788         if (a->proto == CATTA_PROTO_INET) {
789             uint32_t m;
790
791             m = ~(((uint32_t) -1) >> ia->prefix_len);
792
793             if ((ntohl(a->data.ipv4.address) & m) == (ntohl(ia->address.data.ipv4.address) & m))
794                 return 1;
795         } else {
796             unsigned j;
797             unsigned char pl;
798             assert(a->proto == CATTA_PROTO_INET6);
799
800             pl = ia->prefix_len;
801
802             for (j = 0; j < 16; j++) {
803                 uint8_t m;
804
805                 if (pl == 0)
806                     return 1;
807
808                 if (pl >= 8) {
809                     m = 0xFF;
810                     pl -= 8;
811                 } else {
812                     m = ~(0xFF >> pl);
813                     pl = 0;
814                 }
815
816                 if ((a->data.ipv6.address[j] & m) != (ia->address.data.ipv6.address[j] & m))
817                     break;
818             }
819         }
820     }
821
822     return 0;
823 }
824
825 int catta_interface_has_address(CattaInterfaceMonitor *m, CattaIfIndex iface, const CattaAddress *a) {
826     CattaInterface *i;
827     CattaInterfaceAddress *j;
828
829     assert(m);
830     assert(iface != CATTA_IF_UNSPEC);
831     assert(a);
832
833     if (!(i = catta_interface_monitor_get_interface(m, iface, a->proto)))
834         return 0;
835
836     for (j = i->addresses; j; j = j->address_next)
837         if (catta_address_cmp(a, &j->address) == 0)
838             return 1;
839
840     return 0;
841 }
842
843 CattaIfIndex catta_find_interface_for_address(CattaInterfaceMonitor *m, const CattaAddress *a) {
844     CattaInterface *i;
845     assert(m);
846
847     /* Some stupid OS don't support passing the interface index when a
848      * packet is received. We have to work around that limitation by
849      * looking for an interface that has the incoming address
850      * attached. This is sometimes ambiguous, but we have to live with
851      * it. */
852
853     for (i = m->interfaces; i; i = i->iface_next) {
854         CattaInterfaceAddress *ai;
855
856         if (i->protocol != a->proto)
857             continue;
858
859         for (ai = i->addresses; ai; ai = ai->address_next)
860             if (catta_address_cmp(a, &ai->address) == 0)
861                 return i->hardware->index;
862     }
863
864     return CATTA_IF_UNSPEC;
865 }