4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
47 #include <net/if_dl.h>
54 #include "addr-util.h"
56 /* this is a portability hack */
57 #ifndef IPV6_ADD_MEMBERSHIP
58 #ifdef IPV6_JOIN_GROUP
59 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
63 #ifndef IPV6_DROP_MEMBERSHIP
64 #ifdef IPV6_LEAVE_GROUP
65 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
69 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
72 memset(ret_sa, 0, sizeof(struct sockaddr_in));
73 ret_sa->sin_family = AF_INET;
74 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
75 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
78 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
81 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
82 ret_sa->sin6_family = AF_INET6;
83 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
84 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
87 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
92 memset(ret_sa, 0, sizeof(struct sockaddr_in));
93 ret_sa->sin_family = AF_INET;
94 ret_sa->sin_port = htons(port);
95 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
98 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
103 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
104 ret_sa->sin6_family = AF_INET6;
105 ret_sa->sin6_port = htons(port);
106 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
109 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiIPv4Address *a, int idx, int join) {
110 #ifdef HAVE_STRUCT_IP_MREQN
111 struct ip_mreqn mreq;
115 struct sockaddr_in sa;
121 memset(&mreq, 0, sizeof(mreq));
122 #ifdef HAVE_STRUCT_IP_MREQN
123 mreq.imr_ifindex = idx;
124 mreq.imr_address.s_addr = a->address;
126 mreq.imr_interface.s_addr = a->address;
128 mdns_mcast_group_ipv4(&sa);
129 mreq.imr_multiaddr = sa.sin_addr;
131 /* Some network drivers have issues with dropping membership of
132 * mcast groups when the iface is down, but don't allow rejoining
133 * when it comes back up. This is an ugly workaround */
135 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
137 if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
138 avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
145 int avahi_mdns_mcast_join_ipv6(int fd, const AvahiIPv6Address *a, int idx, int join) {
146 struct ipv6_mreq mreq6;
147 struct sockaddr_in6 sa6;
153 memset(&mreq6, 0, sizeof(mreq6));
154 mdns_mcast_group_ipv6 (&sa6);
155 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
156 mreq6.ipv6mr_interface = idx;
159 setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));
161 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
162 avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
169 static int reuseaddr(int fd) {
173 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
174 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
180 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
181 avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
189 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
195 if (bind(fd, sa, l) < 0) {
197 if (errno != EADDRINUSE) {
198 avahi_log_warn("bind() failed: %s", strerror(errno));
202 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
203 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
205 /* Try again, this time with SO_REUSEADDR set */
206 if (reuseaddr(fd) < 0)
209 if (bind(fd, sa, l) < 0) {
210 avahi_log_warn("bind() failed: %s", strerror(errno));
215 /* We enable SO_REUSEADDR afterwards, to make sure that the
216 * user may run other mDNS implementations if he really
219 if (reuseaddr(fd) < 0)
226 static int ipv4_pktinfo(int fd) {
231 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
232 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
237 #ifdef IP_RECVINTERFACE
239 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
240 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
243 #elif defined(IP_RECVIF)
245 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
246 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
251 #ifdef IP_RECVDSTADDR
253 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
254 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
259 #endif /* IP_PKTINFO */
263 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
264 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
272 static int ipv6_pktinfo(int fd) {
275 #ifdef IPV6_RECVPKTINFO
277 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
278 avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
281 #elif defined(IPV6_PKTINFO)
283 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
284 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
291 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
292 avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
295 #elif defined(IPV6_RECVHOPLIMIT)
297 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
298 avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
301 #elif defined(IPV6_HOPLIMIT)
303 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
304 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
312 int avahi_open_socket_ipv4(int no_reuse) {
313 struct sockaddr_in local;
314 int fd = -1, r, ittl;
317 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
318 avahi_log_warn("socket() failed: %s", strerror(errno));
323 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
324 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
329 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
330 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
335 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
336 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
340 memset(&local, 0, sizeof(local));
341 local.sin_family = AF_INET;
342 local.sin_port = htons(AVAHI_MDNS_PORT);
345 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
347 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
352 if (ipv4_pktinfo (fd) < 0)
355 if (avahi_set_cloexec(fd) < 0) {
356 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
360 if (avahi_set_nonblock(fd) < 0) {
361 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
374 int avahi_open_socket_ipv6(int no_reuse) {
375 struct sockaddr_in6 sa, local;
379 mdns_mcast_group_ipv6(&sa);
381 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
382 avahi_log_warn("socket() failed: %s", strerror(errno));
387 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
388 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
393 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
394 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
399 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
400 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
405 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
406 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
410 memset(&local, 0, sizeof(local));
411 local.sin6_family = AF_INET6;
412 local.sin6_port = htons(AVAHI_MDNS_PORT);
415 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
417 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
422 if (ipv6_pktinfo(fd) < 0)
425 if (avahi_set_cloexec(fd) < 0) {
426 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
430 if (avahi_set_nonblock(fd) < 0) {
431 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
444 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
450 if (sendmsg(fd, msg, flags) >= 0)
453 if (errno != EAGAIN) {
454 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
458 if (avahi_wait_for_write(fd) < 0)
465 int avahi_send_dns_packet_ipv4(
467 AvahiIfIndex interface,
469 const AvahiIPv4Address *src_address,
470 const AvahiIPv4Address *dst_address,
473 struct sockaddr_in sa;
477 struct cmsghdr *cmsg;
478 size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
479 #elif defined(IP_SENDSRCADDR)
480 struct cmsghdr *cmsg;
481 size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_addr)) / sizeof(size_t)) + 1];
486 assert(avahi_dns_packet_check_valid(p) >= 0);
487 assert(!dst_address || dst_port > 0);
490 mdns_mcast_group_ipv4(&sa);
492 ipv4_address_to_sockaddr(&sa, dst_address, dst_port);
494 memset(&io, 0, sizeof(io));
495 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
496 io.iov_len = p->size;
498 memset(&msg, 0, sizeof(msg));
500 msg.msg_namelen = sizeof(sa);
504 msg.msg_control = NULL;
505 msg.msg_controllen = 0;
508 if (interface > 0 || src_address) {
509 struct in_pktinfo *pkti;
511 memset(cmsg_data, 0, sizeof(cmsg_data));
512 msg.msg_control = cmsg_data;
513 msg.msg_controllen = sizeof(cmsg_data);
515 cmsg = CMSG_FIRSTHDR(&msg);
516 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
517 cmsg->cmsg_level = IPPROTO_IP;
518 cmsg->cmsg_type = IP_PKTINFO;
520 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
523 pkti->ipi_ifindex = interface;
526 pkti->ipi_spec_dst.s_addr = src_address->address;
528 msg.msg_controllen = cmsg->cmsg_len;
530 #elif defined(IP_SENDSRCADDR)
532 struct in_addr *addr;
534 memset(cmsg_data, 0, sizeof(cmsg_data));
535 msg.msg_control = cmsg_data;
536 msg.msg_controllen = sizeof(cmsg_data);
538 cmsg = CMSG_FIRSTHDR(&msg);
539 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
540 cmsg->cmsg_level = IPPROTO_IP;
541 cmsg->cmsg_type = IP_SENDSRCADDR;
543 addr = (struct in_addr *)CMSG_DATA(cmsg);
544 addr->s_addr = src_address->address;
546 msg.msg_controllen = cmsg->cmsg_len;
548 #elif defined(IP_MULTICAST_IF)
550 struct in_addr any = { INADDR_ANY };
551 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, src_address ? (const void*) &src_address->address : (const void*) &any, sizeof(struct in_addr)) < 0) {
552 avahi_log_warn("IP_MULTICAST_IF failed: %s", strerror(errno));
556 #elif defined(__GNUC__)
557 #warning "FIXME: We need some code to set the outgoing interface/local address here if IP_PKTINFO/IP_MULTICAST_IF is not available"
560 return sendmsg_loop(fd, &msg, 0);
563 int avahi_send_dns_packet_ipv6(
565 AvahiIfIndex interface,
567 const AvahiIPv6Address *src_address,
568 const AvahiIPv6Address *dst_address,
571 struct sockaddr_in6 sa;
574 struct cmsghdr *cmsg;
575 size_t cmsg_data[(CMSG_SPACE(sizeof(struct in6_pktinfo))/sizeof(size_t)) + 1];
579 assert(avahi_dns_packet_check_valid(p) >= 0);
580 assert(!dst_address || dst_port > 0);
583 mdns_mcast_group_ipv6(&sa);
585 ipv6_address_to_sockaddr(&sa, dst_address, dst_port);
587 memset(&io, 0, sizeof(io));
588 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
589 io.iov_len = p->size;
591 memset(&msg, 0, sizeof(msg));
593 msg.msg_namelen = sizeof(sa);
598 if (interface > 0 || src_address) {
599 struct in6_pktinfo *pkti;
601 memset(cmsg_data, 0, sizeof(cmsg_data));
602 msg.msg_control = cmsg_data;
603 msg.msg_controllen = sizeof(cmsg_data);
605 cmsg = CMSG_FIRSTHDR(&msg);
606 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
607 cmsg->cmsg_level = IPPROTO_IPV6;
608 cmsg->cmsg_type = IPV6_PKTINFO;
610 pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
613 pkti->ipi6_ifindex = interface;
616 memcpy(&pkti->ipi6_addr, src_address->address, sizeof(src_address->address));
618 msg.msg_controllen = cmsg->cmsg_len;
620 msg.msg_control = NULL;
621 msg.msg_controllen = 0;
624 return sendmsg_loop(fd, &msg, 0);
627 AvahiDnsPacket *avahi_recv_dns_packet_ipv4(
629 AvahiIPv4Address *ret_src_address,
630 uint16_t *ret_src_port,
631 AvahiIPv4Address *ret_dst_address,
632 AvahiIfIndex *ret_iface,
635 AvahiDnsPacket *p= NULL;
638 size_t aux[1024 / sizeof(size_t)]; /* for alignment on ia64 ! */
640 struct cmsghdr *cmsg;
643 struct sockaddr_in sa;
647 if (ioctl(fd, FIONREAD, &ms) < 0) {
648 avahi_log_warn("ioctl(): %s", strerror(errno));
653 avahi_log_warn("FIONREAD returned negative value.");
657 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
659 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
660 io.iov_len = p->max_size;
662 memset(&msg, 0, sizeof(msg));
664 msg.msg_namelen = sizeof(sa);
667 msg.msg_control = aux;
668 msg.msg_controllen = sizeof(aux);
671 if ((l = recvmsg(fd, &msg, 0)) < 0) {
672 /* Linux returns EAGAIN when an invalid IP packet has been
673 recieved. We suppress warnings in this case because this might
674 create quite a bit of log traffic on machines with unstable
678 avahi_log_warn("recvmsg(): %s", strerror(errno));
683 if (sa.sin_addr.s_addr == INADDR_ANY) {
684 /* Linux 2.4 behaves very strangely sometimes! */
688 assert(!(msg.msg_flags & MSG_CTRUNC));
689 assert(!(msg.msg_flags & MSG_TRUNC));
691 p->size = (size_t) l;
694 *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
696 if (ret_src_address) {
698 avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
699 *ret_src_address = a.data.ipv4;
706 *ret_iface = AVAHI_IF_UNSPEC;
708 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
710 if (cmsg->cmsg_level == IPPROTO_IP) {
712 switch (cmsg->cmsg_type) {
718 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
724 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
727 *ret_iface = (int) i->ipi_ifindex;
730 ret_dst_address->address = i->ipi_addr.s_addr;
740 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
744 *ret_iface = *(uint_t*) sdl;
746 *ret_iface = (int) sdl->sdl_index;
753 #ifdef IP_RECVDSTADDR
756 memcpy(&ret_dst_address->address, CMSG_DATA (cmsg), 4);
763 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
775 avahi_dns_packet_free(p);
780 AvahiDnsPacket *avahi_recv_dns_packet_ipv6(
782 AvahiIPv6Address *ret_src_address,
783 uint16_t *ret_src_port,
784 AvahiIPv6Address *ret_dst_address,
785 AvahiIfIndex *ret_iface,
788 AvahiDnsPacket *p = NULL;
791 size_t aux[1024 / sizeof(size_t)];
794 struct cmsghdr *cmsg;
795 int found_ttl = 0, found_iface = 0;
796 struct sockaddr_in6 sa;
800 if (ioctl(fd, FIONREAD, &ms) < 0) {
801 avahi_log_warn("ioctl(): %s", strerror(errno));
806 avahi_log_warn("FIONREAD returned negative value.");
810 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
812 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
813 io.iov_len = p->max_size;
815 memset(&msg, 0, sizeof(msg));
816 msg.msg_name = (struct sockaddr*) &sa;
817 msg.msg_namelen = sizeof(sa);
821 msg.msg_control = aux;
822 msg.msg_controllen = sizeof(aux);
825 if ((l = recvmsg(fd, &msg, 0)) < 0) {
826 /* Linux returns EAGAIN when an invalid IP packet has been
827 recieved. We suppress warnings in this case because this might
828 create quite a bit of log traffic on machines with unstable
832 avahi_log_warn("recvmsg(): %s", strerror(errno));
837 assert(!(msg.msg_flags & MSG_CTRUNC));
838 assert(!(msg.msg_flags & MSG_TRUNC));
840 p->size = (size_t) l;
843 *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
845 if (ret_src_address) {
847 avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
848 *ret_src_address = a.data.ipv6;
851 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
853 if (cmsg->cmsg_level == IPPROTO_IPV6) {
855 switch (cmsg->cmsg_type) {
860 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
867 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
870 *ret_iface = i->ipi6_ifindex;
873 memcpy(ret_dst_address->address, i->ipi6_addr.s6_addr, 16);
880 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
893 avahi_dns_packet_free(p);
898 int avahi_open_unicast_socket_ipv4(void) {
899 struct sockaddr_in local;
902 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
903 avahi_log_warn("socket() failed: %s", strerror(errno));
907 memset(&local, 0, sizeof(local));
908 local.sin_family = AF_INET;
910 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
911 avahi_log_warn("bind() failed: %s", strerror(errno));
915 if (ipv4_pktinfo(fd) < 0) {
919 if (avahi_set_cloexec(fd) < 0) {
920 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
924 if (avahi_set_nonblock(fd) < 0) {
925 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
938 int avahi_open_unicast_socket_ipv6(void) {
939 struct sockaddr_in6 local;
942 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
943 avahi_log_warn("socket() failed: %s", strerror(errno));
948 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
949 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
953 memset(&local, 0, sizeof(local));
954 local.sin6_family = AF_INET6;
956 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
957 avahi_log_warn("bind() failed: %s", strerror(errno));
961 if (ipv6_pktinfo(fd) < 0)
964 if (avahi_set_cloexec(fd) < 0) {
965 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
969 if (avahi_set_nonblock(fd) < 0) {
970 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));