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