Drop one-liner service_announce function
[project/mdnsd.git] / interface.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _GNU_SOURCE
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/utsname.h>
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/types.h>
25
26 #include <ifaddrs.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <errno.h>
32
33 #include <libubox/usock.h>
34 #include <libubox/uloop.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include "interface.h"
38 #include "util.h"
39 #include "dns.h"
40 #include "announce.h"
41 #include "service.h"
42
43 static int
44 interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len)
45 {
46         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
47         static struct sockaddr_in a;
48         static struct msghdr m = {
49                 .msg_name = (struct sockaddr *) &a,
50                 .msg_namelen = sizeof(a),
51                 .msg_control = cmsg_data,
52                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
53         };
54         struct in_pktinfo *pkti;
55         struct cmsghdr *cmsg;
56         int fd = iface->fd.fd;
57
58         a.sin_family = AF_INET;
59         a.sin_port = htons(MCAST_PORT);
60         m.msg_iov = iov;
61         m.msg_iovlen = iov_len;
62
63         memset(cmsg_data, 0, sizeof(cmsg_data));
64         cmsg = CMSG_FIRSTHDR(&m);
65         cmsg->cmsg_len = m.msg_controllen;
66         cmsg->cmsg_level = IPPROTO_IP;
67         cmsg->cmsg_type = IP_PKTINFO;
68
69         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
70         pkti->ipi_ifindex = iface->ifindex;
71
72         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
73
74         return sendmsg(fd, &m, 0);
75 }
76
77 static int
78 interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len)
79 {
80         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
81         static struct sockaddr_in6 a;
82         static struct msghdr m = {
83                 .msg_name = (struct sockaddr *) &a,
84                 .msg_namelen = sizeof(a),
85                 .msg_control = cmsg_data,
86                 .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)),
87         };
88         struct in6_pktinfo *pkti;
89         struct cmsghdr *cmsg;
90         int fd = iface->fd.fd;
91
92         a.sin6_family = AF_INET6;
93         a.sin6_port = htons(MCAST_PORT);
94         m.msg_iov = iov;
95         m.msg_iovlen = iov_len;
96
97         memset(cmsg_data, 0, sizeof(cmsg_data));
98         cmsg = CMSG_FIRSTHDR(&m);
99         cmsg->cmsg_len = m.msg_controllen;
100         cmsg->cmsg_level = IPPROTO_IPV6;
101         cmsg->cmsg_type = IPV6_PKTINFO;
102
103         pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
104         pkti->ipi6_ifindex = iface->ifindex;
105
106         inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
107
108         return sendmsg(fd, &m, 0);
109 }
110
111 int
112 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
113 {
114         if (debug > 1) {
115                 fprintf(stderr, "TX ipv%d: %s\n", iface->v6 * 2 + 4, iface->name);
116                 fprintf(stderr, "  multicast: %d\n", iface->multicast);
117         }
118
119         if (iface->v6)
120                 return interface_send_packet6(iface, iov, iov_len);
121
122         return interface_send_packet4(iface, iov, iov_len);
123 }
124
125 static void interface_close(struct interface *iface)
126 {
127         if (iface->fd.fd < 0)
128                 return;
129
130         announce_free(iface);
131         uloop_fd_delete(&iface->fd);
132         close(iface->fd.fd);
133         iface->fd.fd = -1;
134 }
135
136 static void interface_free(struct interface *iface)
137 {
138         interface_close(iface);
139         free(iface);
140 }
141
142 static int
143 interface_valid_src(void *ip1, void *mask, void *ip2, int len)
144 {
145         uint8_t *i1 = ip1;
146         uint8_t *i2 = ip2;
147         uint8_t *m = mask;
148         int i;
149
150         if (cfg_no_subnet)
151                 return 0;
152
153         for (i = 0; i < len; i++, i1++, i2++, m++) {
154                 if ((*i1 & *m) != (*i2 & *m))
155                         return -1;
156         }
157
158         return 0;
159 }
160
161 static void
162 read_socket4(struct uloop_fd *u, unsigned int events)
163 {
164         struct interface *iface = container_of(u, struct interface, fd);
165         static uint8_t buffer[8 * 1024];
166         struct iovec iov[1];
167         char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
168         struct cmsghdr *cmsgptr;
169         struct msghdr msg;
170         socklen_t len;
171         struct sockaddr_in from;
172         int flags = 0, ifindex = -1;
173         uint8_t ttl = 0;
174         struct in_pktinfo *inp = NULL;
175
176         if (u->eof) {
177                 interface_close(iface);
178                 uloop_timeout_set(&iface->reconnect, 1000);
179                 return;
180         }
181
182         iov[0].iov_base = buffer;
183         iov[0].iov_len = sizeof(buffer);
184
185         memset(&msg, 0, sizeof(msg));
186         msg.msg_name = (struct sockaddr *) &from;
187         msg.msg_namelen = sizeof(struct sockaddr_in);
188         msg.msg_iov = iov;
189         msg.msg_iovlen = 1;
190         msg.msg_control = &cmsg;
191         msg.msg_controllen = sizeof(cmsg);
192
193         len = recvmsg(u->fd, &msg, flags);
194         if (len == -1) {
195                 perror("read failed");
196                 return;
197         }
198         for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
199                 void *c = CMSG_DATA(cmsgptr);
200
201                 switch (cmsgptr->cmsg_type) {
202                 case IP_PKTINFO:
203                         inp = ((struct in_pktinfo *) c);
204                         break;
205
206                 case IP_TTL:
207                         ttl = (uint8_t) *((int *) c);
208                         break;
209
210                 default:
211                         fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
212                         return;
213                 }
214         }
215
216         if (ttl != 255)
217                 return;
218
219         if (debug > 1) {
220                 char buf[256];
221
222                 fprintf(stderr, "RX ipv4: %s\n", iface->name);
223                 fprintf(stderr, "  multicast: %d\n", iface->multicast);
224                 inet_ntop(AF_INET, &from.sin_addr, buf, 256);
225                 fprintf(stderr, "  src %s:%d\n", buf, from.sin_port);
226                 inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
227                 fprintf(stderr, "  dst %s\n", buf);
228                 inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
229                 fprintf(stderr, "  real %s\n", buf);
230         }
231
232         if (inp->ipi_ifindex != iface->ifindex)
233                 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
234         else if (!interface_valid_src((void *) &iface->v4_addr, (void *) &iface->v4_netmask, (void *) &from.sin_addr, 4))
235                 dns_handle_packet(iface, (struct sockaddr *) &from, from.sin_port, buffer, len);
236 }
237
238 static void
239 read_socket6(struct uloop_fd *u, unsigned int events)
240 {
241         struct interface *iface = container_of(u, struct interface, fd);
242         static uint8_t buffer[8 * 1024];
243         struct iovec iov[1];
244         char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
245         struct cmsghdr *cmsgptr;
246         struct msghdr msg;
247         socklen_t len;
248         struct sockaddr_in6 from;
249         int flags = 0, ifindex = -1;
250         int ttl = 0;
251         struct in6_pktinfo *inp = NULL;
252
253         if (u->eof) {
254                 interface_close(iface);
255                 uloop_timeout_set(&iface->reconnect, 1000);
256                 return;
257         }
258
259         iov[0].iov_base = buffer;
260         iov[0].iov_len = sizeof(buffer);
261
262         memset(&msg, 0, sizeof(msg));
263         msg.msg_name = (struct sockaddr *) &from;
264         msg.msg_namelen = sizeof(struct sockaddr_in6);
265         msg.msg_iov = iov;
266         msg.msg_iovlen = 1;
267         msg.msg_control = &cmsg6;
268         msg.msg_controllen = sizeof(cmsg6);
269
270         len = recvmsg(u->fd, &msg, flags);
271         if (len == -1) {
272                 perror("read failed");
273                 return;
274         }
275         for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
276                 void *c = CMSG_DATA(cmsgptr);
277
278                 switch (cmsgptr->cmsg_type) {
279                 case IPV6_PKTINFO:
280                         inp = ((struct in6_pktinfo *) c);
281                         break;
282
283                 case IPV6_HOPLIMIT:
284                         ttl = (uint8_t) *((int *) c);
285                         break;
286
287                 default:
288                         fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type);
289                         return;
290                 }
291         }
292
293         if (ttl != 255)
294                 return;
295
296         if (debug > 1) {
297                 char buf[256];
298
299                 fprintf(stderr, "RX ipv6: %s\n", iface->name);
300                 fprintf(stderr, "  multicast: %d\n", iface->multicast);
301                 inet_ntop(AF_INET6, &from.sin6_addr, buf, 256);
302                 fprintf(stderr, "  src %s:%d\n", buf, from.sin6_port);
303                 inet_ntop(AF_INET6, &inp->ipi6_addr, buf, 256);
304                 fprintf(stderr, "  dst %s\n", buf);
305         }
306
307         if (inp->ipi6_ifindex != iface->ifindex)
308                 fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
309         else if (!interface_valid_src((void *) &iface->v6_addr, (void *) &iface->v6_netmask, (void *) &from.sin6_addr, 16))
310                 dns_handle_packet(iface, (struct sockaddr *) &from, from.sin6_port, buffer, len);
311 }
312
313 static int
314 interface_mcast_setup4(struct interface *iface)
315 {
316         struct ip_mreqn mreq;
317         uint8_t ttl = 255;
318         int no = 0;
319         struct sockaddr_in sa = { 0 };
320         int fd = iface->fd.fd;
321
322         sa.sin_family = AF_INET;
323         sa.sin_port = htons(MCAST_PORT);
324         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
325
326         memset(&mreq, 0, sizeof(mreq));
327         mreq.imr_address.s_addr = iface->v4_addr.s_addr;
328         mreq.imr_multiaddr = sa.sin_addr;
329         mreq.imr_ifindex = iface->ifindex;
330
331         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
332                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
333
334         /* Some network drivers have issues with dropping membership of
335          * mcast groups when the iface is down, but don't allow rejoining
336          * when it comes back up. This is an ugly workaround
337          * -- this was copied from avahi --
338          */
339         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
340
341         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
342                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
343                 close(fd);
344                 fd = -1;
345                 return -1;
346         }
347
348         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
349                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
350
351         return 0;
352 }
353
354 static int
355 interface_socket_setup6(struct interface *iface)
356 {
357         struct ipv6_mreq mreq;
358         int ttl = 255;
359         int no = 0;
360         struct sockaddr_in6 sa = { 0 };
361         int fd = iface->fd.fd;
362
363         sa.sin6_family = AF_INET6;
364         sa.sin6_port = htons(MCAST_PORT);
365         inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr);
366
367         memset(&mreq, 0, sizeof(mreq));
368         mreq.ipv6mr_multiaddr = sa.sin6_addr;
369         mreq.ipv6mr_interface = iface->ifindex;
370
371         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
372                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
373
374         setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
375         if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
376                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
377                 close(fd);
378                 fd = -1;
379                 return -1;
380         }
381
382         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
383                 fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
384
385         return 0;
386 }
387
388 static void
389 reconnect_socket4(struct uloop_timeout *timeout)
390 {
391         struct interface *iface = container_of(timeout, struct interface, reconnect);
392         int yes = 1;
393
394         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY,
395                 (iface->multicast) ? (iface->mcast_addr) : (iface->v4_addrs), "5353");
396         if (iface->fd.fd < 0) {
397                 fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno));
398                 goto retry;
399         }
400
401         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
402                 fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
403
404         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
405                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
406
407         if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
408                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
409
410         if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
411                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
412
413         if (iface->multicast && interface_mcast_setup4(iface)) {
414                 iface->fd.fd = -1;
415                 goto retry;
416         }
417
418         uloop_fd_add(&iface->fd, ULOOP_READ);
419         if (iface->multicast) {
420                 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0);
421                 announce_init(iface);
422         }
423
424         return;
425
426 retry:
427         uloop_timeout_set(timeout, 1000);
428 }
429
430 static void
431 reconnect_socket6(struct uloop_timeout *timeout)
432 {
433         struct interface *iface = container_of(timeout, struct interface, reconnect);
434         char mcast_addr[128];
435         int ttl = 255;
436         int yes = 1;
437
438         snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", (iface->multicast) ? (iface->mcast_addr) : (iface->v6_addrs), iface->name);
439         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353");
440         if (iface->fd.fd < 0) {
441                 fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
442                 goto retry;
443         }
444
445         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
446                 fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
447
448         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
449                 fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
450
451         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
452                 fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
453
454         if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
455                 fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
456
457         if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
458                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
459
460         if (iface->multicast && interface_socket_setup6(iface)) {
461                 iface->fd.fd = -1;
462                 goto retry;
463         }
464
465         uloop_fd_add(&iface->fd, ULOOP_READ);
466
467         if (iface->multicast) {
468                 dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0);
469                 announce_init(iface);
470         }
471
472         return;
473
474 retry:
475         uloop_timeout_set(timeout, 1000);
476 }
477
478
479 static void interface_start(struct interface *iface)
480 {
481         if (iface->v6) {
482                 iface->fd.cb = read_socket6;
483                 iface->reconnect.cb = reconnect_socket6;
484         } else {
485                 iface->fd.cb = read_socket4;
486                 iface->reconnect.cb = reconnect_socket4;
487         }
488         uloop_timeout_set(&iface->reconnect, 100);
489 }
490
491 static void
492 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
493                 struct vlist_node *node_old)
494 {
495         struct interface *iface;
496
497         if (node_old) {
498                 iface = container_of(node_old, struct interface, node);
499                 interface_free(iface);
500         }
501
502         if (node_new) {
503                 iface = container_of(node_new, struct interface, node);
504                 interface_start(iface);
505         }
506 }
507
508 static struct interface* _interface_add(const char *name, int multicast, int v6)
509 {
510         struct interface *iface;
511         char *name_buf;
512         char *id_buf;
513
514         iface = calloc_a(sizeof(*iface),
515                 &name_buf, strlen(name) + 1,
516                 &id_buf, strlen(name) + 5);
517
518         sprintf(id_buf, "%d_%d_%s", multicast, v6, name);
519         iface->name = strcpy(name_buf, name);
520         iface->id = id_buf;
521         iface->ifindex = if_nametoindex(name);
522         iface->fd.fd = -1;
523         iface->multicast = multicast;
524         iface->v6 = v6;
525         if (v6)
526                 iface->mcast_addr = MCAST_ADDR6;
527         else
528                 iface->mcast_addr = MCAST_ADDR;
529
530         if (iface->ifindex <= 0)
531                 goto error;
532
533         vlist_add(&interfaces, &iface->node, iface->id);
534         return iface;
535
536 error:
537         free(iface);
538         return NULL;
539 }
540
541 int interface_add(const char *name)
542 {
543         struct interface *v4 = NULL, *v6 = NULL, *unicast;
544         struct ifaddrs *ifap, *ifa;
545
546         getifaddrs(&ifap);
547
548         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
549                 if (strcmp(ifa->ifa_name, name))
550                         continue;
551                 if (ifa->ifa_addr->sa_family == AF_INET && !v4) {
552                         struct sockaddr_in *sa;
553
554                         if (cfg_proto && (cfg_proto != 4))
555                                 continue;
556
557                         unicast = _interface_add(name, 0, 0);
558                         if (!unicast)
559                                 continue;
560                         v4 = _interface_add(name, 1, 0);
561                         if (!v4)
562                                 continue;
563
564                         sa = (struct sockaddr_in *) ifa->ifa_addr;
565                         memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
566                         memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
567
568                         inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
569                         inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
570
571                         sa = (struct sockaddr_in *) ifa->ifa_netmask;
572                         memcpy(&unicast->v4_netmask, &sa->sin_addr, sizeof(unicast->v4_netmask));
573                         memcpy(&v4->v4_netmask, &sa->sin_addr, sizeof(v4->v4_netmask));
574
575                         v4->peer = unicast;
576                         unicast->peer = v4;
577                 }
578
579                 if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
580                         uint8_t ll_prefix[] = {0xfe, 0x80 };
581                         struct sockaddr_in6 *sa6;
582
583                         if (cfg_proto && (cfg_proto != 6))
584                                 continue;
585
586                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
587                         if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
588                                 continue;
589
590                         unicast = _interface_add(name, 0, 1);
591                         if (!unicast)
592                                 continue;
593                         v6 = _interface_add(name, 1, 1);
594                         if (!v6)
595                                 continue;
596
597                         memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
598                         memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
599
600                         inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
601                         inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
602
603                         sa6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
604                         memcpy(&v6->v6_netmask, &sa6->sin6_addr, sizeof(v6->v6_netmask));
605                         memcpy(&unicast->v6_netmask, &sa6->sin6_addr, sizeof(unicast->v6_netmask));
606
607                         v6->peer = unicast;
608                         unicast->peer = v6;
609                 }
610         }
611
612         freeifaddrs(ifap);
613
614         return !v4 && !v6;
615 }
616
617 void interface_shutdown(void)
618 {
619         struct interface *iface;
620
621         vlist_for_each_element(&interfaces, iface, node)
622                 if (iface->fd.fd > 0 && iface->multicast) {
623                         dns_reply_a(iface, 0);
624                         service_announce_services(iface, 0);
625                 }
626         vlist_for_each_element(&interfaces, iface, node)
627                 interface_close(iface);
628 }
629
630 struct interface*
631 interface_get(const char *name, int v6, int multicast)
632 {
633         char id_buf[32];
634         snprintf(id_buf, sizeof(id_buf), "%d_%d_%s", multicast, v6, name);
635         struct interface *iface = vlist_find(&interfaces, id_buf, iface, node);
636         return iface;
637 }
638
639 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);