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