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