2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/ioctl.h>
32 #ifdef HAVE_SYS_FILIO_H
33 #include <sys/filio.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #include <net/if_dl.h>
48 #include <catta/log.h>
52 #include "addr-util.h"
55 /* this is a portability hack */
56 #ifndef IPV6_ADD_MEMBERSHIP
57 #ifdef IPV6_JOIN_GROUP
58 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
62 #ifndef IPV6_DROP_MEMBERSHIP
63 #ifdef IPV6_LEAVE_GROUP
64 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
68 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
71 memset(ret_sa, 0, sizeof(struct sockaddr_in));
72 ret_sa->sin_family = AF_INET;
73 ret_sa->sin_port = htons(CATTA_MDNS_PORT);
74 inet_pton(AF_INET, CATTA_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
77 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
80 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
81 ret_sa->sin6_family = AF_INET6;
82 ret_sa->sin6_port = htons(CATTA_MDNS_PORT);
83 inet_pton(AF_INET6, CATTA_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
86 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const CattaIPv4Address *a, uint16_t port) {
91 memset(ret_sa, 0, sizeof(struct sockaddr_in));
92 ret_sa->sin_family = AF_INET;
93 ret_sa->sin_port = htons(port);
94 memcpy(&ret_sa->sin_addr, a, sizeof(CattaIPv4Address));
97 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const CattaIPv6Address *a, uint16_t port) {
102 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
103 ret_sa->sin6_family = AF_INET6;
104 ret_sa->sin6_port = htons(port);
105 memcpy(&ret_sa->sin6_addr, a, sizeof(CattaIPv6Address));
108 int catta_mdns_mcast_join_ipv4(int fd, const CattaIPv4Address *a, int idx, int join) {
109 #ifdef HAVE_STRUCT_IP_MREQN
110 struct ip_mreqn mreq;
114 struct sockaddr_in sa;
120 memset(&mreq, 0, sizeof(mreq));
121 #ifdef HAVE_STRUCT_IP_MREQN
122 mreq.imr_ifindex = idx;
123 mreq.imr_address.s_addr = a->address;
125 mreq.imr_interface.s_addr = a->address;
127 mdns_mcast_group_ipv4(&sa);
128 mreq.imr_multiaddr = sa.sin_addr;
130 /* Some network drivers have issues with dropping membership of
131 * mcast groups when the iface is down, but don't allow rejoining
132 * when it comes back up. This is an ugly workaround */
134 setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq));
136 if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
137 catta_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
144 int catta_mdns_mcast_join_ipv6(int fd, const CattaIPv6Address *a, int idx, int join) {
145 struct ipv6_mreq mreq6;
146 struct sockaddr_in6 sa6;
152 memset(&mreq6, 0, sizeof(mreq6));
153 mdns_mcast_group_ipv6 (&sa6);
154 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
155 mreq6.ipv6mr_interface = idx;
158 setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, (void *)&mreq6, sizeof(mreq6));
160 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, (void *)&mreq6, sizeof(mreq6)) < 0) {
161 catta_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
168 static int reuseaddr(int fd) {
172 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)) < 0) {
173 catta_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
179 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&yes, sizeof(yes)) < 0) {
180 catta_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
188 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
194 if (bind(fd, sa, l) < 0) {
196 if (errno != EADDRINUSE) {
197 catta_log_warn("bind() failed: %s", strerror(errno));
201 catta_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
202 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
204 /* Try again, this time with SO_REUSEADDR set */
205 if (reuseaddr(fd) < 0)
208 if (bind(fd, sa, l) < 0) {
209 catta_log_warn("bind() failed: %s", strerror(errno));
214 /* We enable SO_REUSEADDR afterwards, to make sure that the
215 * user may run other mDNS implementations if he really
218 if (reuseaddr(fd) < 0)
225 static int ipv4_pktinfo(int fd) {
230 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, (void *)&yes, sizeof(yes)) < 0) {
231 catta_log_warn("IP_PKTINFO failed: %s", strerror(errno));
236 #ifdef IP_RECVINTERFACE
238 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, (void *)&yes, sizeof(yes)) < 0) {
239 catta_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
242 #elif defined(IP_RECVIF)
244 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, (void *)&yes, sizeof(yes)) < 0) {
245 catta_log_warn("IP_RECVIF failed: %s", strerror(errno));
250 #ifdef IP_RECVDSTADDR
252 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, (void *)&yes, sizeof(yes)) < 0) {
253 catta_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
258 #endif /* IP_PKTINFO */
262 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, (void *)&yes, sizeof(yes)) < 0) {
263 catta_log_warn("IP_RECVTTL failed: %s", strerror(errno));
271 static int ipv6_pktinfo(int fd) {
274 #ifdef IPV6_RECVPKTINFO
276 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (void *)&yes, sizeof(yes)) < 0) {
277 catta_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
280 #elif defined(IPV6_PKTINFO)
282 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, (void *)&yes, sizeof(yes)) < 0) {
283 catta_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
290 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, (void *)&yes, sizeof(yes)) < 0) {
291 catta_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
294 #elif defined(IPV6_RECVHOPLIMIT)
296 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, (void *)&yes, sizeof(yes)) < 0) {
297 catta_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
300 #elif defined(IPV6_HOPLIMIT)
302 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, (void *)&yes, sizeof(yes)) < 0) {
303 catta_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
311 int catta_open_socket_ipv4(int no_reuse) {
312 struct sockaddr_in local;
313 int fd = -1, r, ittl;
316 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
317 catta_log_warn("socket() failed: %s", strerror(errno));
322 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl)) < 0) {
323 catta_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
328 if (setsockopt(fd, IPPROTO_IP, IP_TTL, (void *)&ittl, sizeof(ittl)) < 0) {
329 catta_log_warn("IP_TTL failed: %s", strerror(errno));
334 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&cyes, sizeof(cyes)) < 0) {
335 catta_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
339 memset(&local, 0, sizeof(local));
340 local.sin_family = AF_INET;
341 local.sin_port = htons(CATTA_MDNS_PORT);
344 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
346 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
351 if (ipv4_pktinfo (fd) < 0)
354 if (catta_set_cloexec(fd) < 0) {
355 catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
359 if (catta_set_nonblock(fd) < 0) {
360 catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
373 int catta_open_socket_ipv6(int no_reuse) {
374 struct sockaddr_in6 sa, local;
378 mdns_mcast_group_ipv6(&sa);
380 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
381 catta_log_warn("socket() failed: %s", strerror(errno));
386 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof(ttl)) < 0) {
387 catta_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
392 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (void *)&ttl, sizeof(ttl)) < 0) {
393 catta_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
398 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&yes, sizeof(yes)) < 0) {
399 catta_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
404 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (void *)&yes, sizeof(yes)) < 0) {
405 catta_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
409 memset(&local, 0, sizeof(local));
410 local.sin6_family = AF_INET6;
411 local.sin6_port = htons(CATTA_MDNS_PORT);
414 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
416 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
421 if (ipv6_pktinfo(fd) < 0)
424 if (catta_set_cloexec(fd) < 0) {
425 catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
429 if (catta_set_nonblock(fd) < 0) {
430 catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
443 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
449 if (sendmsg(fd, msg, flags) >= 0)
455 if (errno != EAGAIN) {
457 struct sockaddr_in *sin = msg->msg_name;
459 inet_ntop(sin->sin_family, &sin->sin_addr, where, sizeof(where));
460 catta_log_debug("sendmsg() to %s failed: %s", where, strerror(errno));
464 if (catta_wait_for_write(fd) < 0)
471 int catta_send_dns_packet_ipv4(
475 const CattaIPv4Address *src_address,
476 const CattaIPv4Address *dst_address,
479 struct sockaddr_in sa;
483 struct cmsghdr *cmsg;
484 size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
485 #elif !defined(IP_MULTICAST_IF) && defined(IP_SENDSRCADDR)
486 struct cmsghdr *cmsg;
487 size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_addr)) / sizeof(size_t)) + 1];
492 assert(catta_dns_packet_check_valid(p) >= 0);
493 assert(!dst_address || dst_port > 0);
496 mdns_mcast_group_ipv4(&sa);
498 ipv4_address_to_sockaddr(&sa, dst_address, dst_port);
500 memset(&io, 0, sizeof(io));
501 io.iov_base = CATTA_DNS_PACKET_DATA(p);
502 io.iov_len = p->size;
504 memset(&msg, 0, sizeof(msg));
506 msg.msg_namelen = sizeof(sa);
510 msg.msg_control = NULL;
511 msg.msg_controllen = 0;
514 if (iface > 0 || src_address) {
515 struct in_pktinfo *pkti;
517 memset(cmsg_data, 0, sizeof(cmsg_data));
518 msg.msg_control = cmsg_data;
519 msg.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
521 cmsg = CMSG_FIRSTHDR(&msg);
522 cmsg->cmsg_len = msg.msg_controllen;
523 cmsg->cmsg_level = IPPROTO_IP;
524 cmsg->cmsg_type = IP_PKTINFO;
526 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
529 pkti->ipi_ifindex = iface;
531 #ifdef HAVE_IPI_SPEC_DST
533 pkti->ipi_spec_dst.s_addr = src_address->address;
536 #elif defined(IP_MULTICAST_IF)
538 struct in_addr any = { INADDR_ANY };
539 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *)(src_address ? &src_address->address : &any), sizeof(struct in_addr)) < 0) {
540 catta_log_warn("IP_MULTICAST_IF failed: %s", strerror(errno));
544 #elif defined(IP_SENDSRCADDR)
546 struct in_addr *addr;
548 memset(cmsg_data, 0, sizeof(cmsg_data));
549 msg.msg_control = cmsg_data;
550 msg.msg_controllen = CMSG_LEN(sizeof(struct in_addr));
552 cmsg = CMSG_FIRSTHDR(&msg);
553 cmsg->cmsg_len = msg.msg_controllen;
554 cmsg->cmsg_level = IPPROTO_IP;
555 cmsg->cmsg_type = IP_SENDSRCADDR;
557 addr = (struct in_addr *)CMSG_DATA(cmsg);
558 addr->s_addr = src_address->address;
560 #elif defined(__GNUC__)
561 #warning "FIXME: We need some code to set the outgoing interface/local address here if IP_PKTINFO/IP_MULTICAST_IF is not available"
564 return sendmsg_loop(fd, &msg, 0);
567 int catta_send_dns_packet_ipv6(
571 const CattaIPv6Address *src_address,
572 const CattaIPv6Address *dst_address,
575 struct sockaddr_in6 sa;
578 struct cmsghdr *cmsg;
579 size_t cmsg_data[(CMSG_SPACE(sizeof(struct in6_pktinfo))/sizeof(size_t)) + 1];
583 assert(catta_dns_packet_check_valid(p) >= 0);
584 assert(!dst_address || dst_port > 0);
587 mdns_mcast_group_ipv6(&sa);
589 ipv6_address_to_sockaddr(&sa, dst_address, dst_port);
591 memset(&io, 0, sizeof(io));
592 io.iov_base = CATTA_DNS_PACKET_DATA(p);
593 io.iov_len = p->size;
595 memset(&msg, 0, sizeof(msg));
597 msg.msg_namelen = sizeof(sa);
602 if (iface > 0 || src_address) {
603 struct in6_pktinfo *pkti;
605 memset(cmsg_data, 0, sizeof(cmsg_data));
606 msg.msg_control = cmsg_data;
607 msg.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
609 cmsg = CMSG_FIRSTHDR(&msg);
610 cmsg->cmsg_len = msg.msg_controllen;
611 cmsg->cmsg_level = IPPROTO_IPV6;
612 cmsg->cmsg_type = IPV6_PKTINFO;
614 pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
617 pkti->ipi6_ifindex = iface;
620 memcpy(&pkti->ipi6_addr, src_address->address, sizeof(src_address->address));
622 msg.msg_control = NULL;
623 msg.msg_controllen = 0;
626 return sendmsg_loop(fd, &msg, 0);
629 CattaDnsPacket *catta_recv_dns_packet_ipv4(
631 CattaIPv4Address *ret_src_address,
632 uint16_t *ret_src_port,
633 CattaIPv4Address *ret_dst_address,
634 CattaIfIndex *ret_iface,
637 CattaDnsPacket *p= NULL;
640 size_t aux[1024 / sizeof(size_t)]; /* for alignment on ia64 ! */
642 struct cmsghdr *cmsg;
645 struct sockaddr_in sa;
649 if (ioctl(fd, FIONREAD, &ms) < 0) {
650 catta_log_warn("ioctl(): %s", strerror(errno));
655 catta_log_warn("FIONREAD returned negative value.");
659 p = catta_dns_packet_new(ms + CATTA_DNS_PACKET_EXTRA_SIZE);
661 io.iov_base = CATTA_DNS_PACKET_DATA(p);
662 io.iov_len = p->max_size;
664 memset(&msg, 0, sizeof(msg));
666 msg.msg_namelen = sizeof(sa);
669 msg.msg_control = aux;
670 msg.msg_controllen = sizeof(aux);
673 if ((l = recvmsg(fd, &msg, 0)) < 0) {
674 /* Linux returns EAGAIN when an invalid IP packet has been
675 received. We suppress warnings in this case because this might
676 create quite a bit of log traffic on machines with unstable
680 catta_log_warn("recvmsg(): %s", strerror(errno));
685 /* For corrupt packets FIONREAD returns zero size (See rhbz #607297). So
686 * fail after having read them. */
690 if (sa.sin_addr.s_addr == INADDR_ANY)
691 /* Linux 2.4 behaves very strangely sometimes! */
694 assert(!(msg.msg_flags & MSG_CTRUNC));
695 assert(!(msg.msg_flags & MSG_TRUNC));
697 p->size = (size_t) l;
700 *ret_src_port = catta_port_from_sockaddr((struct sockaddr*) &sa);
702 if (ret_src_address) {
704 catta_address_from_sockaddr((struct sockaddr*) &sa, &a);
705 *ret_src_address = a.data.ipv4;
712 *ret_iface = CATTA_IF_UNSPEC;
714 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
716 if (cmsg->cmsg_level == IPPROTO_IP) {
718 switch (cmsg->cmsg_type) {
724 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
730 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
732 if (ret_iface && i->ipi_ifindex > 0)
733 *ret_iface = (int) i->ipi_ifindex;
736 ret_dst_address->address = i->ipi_addr.s_addr;
746 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
750 if (*(uint_t*) sdl > 0)
751 *ret_iface = *(uint_t*) sdl;
754 if (sdl->sdl_index > 0)
755 *ret_iface = (int) sdl->sdl_index;
763 #ifdef IP_RECVDSTADDR
766 memcpy(&ret_dst_address->address, CMSG_DATA (cmsg), 4);
773 catta_log_warn("Unhandled cmsg_type: %d", cmsg->cmsg_type);
785 catta_dns_packet_free(p);
790 CattaDnsPacket *catta_recv_dns_packet_ipv6(
792 CattaIPv6Address *ret_src_address,
793 uint16_t *ret_src_port,
794 CattaIPv6Address *ret_dst_address,
795 CattaIfIndex *ret_iface,
798 CattaDnsPacket *p = NULL;
801 size_t aux[1024 / sizeof(size_t)];
804 struct cmsghdr *cmsg;
805 int found_ttl = 0, found_iface = 0;
806 struct sockaddr_in6 sa;
810 if (ioctl(fd, FIONREAD, &ms) < 0) {
811 catta_log_warn("ioctl(): %s", strerror(errno));
816 catta_log_warn("FIONREAD returned negative value.");
820 p = catta_dns_packet_new(ms + CATTA_DNS_PACKET_EXTRA_SIZE);
822 io.iov_base = CATTA_DNS_PACKET_DATA(p);
823 io.iov_len = p->max_size;
825 memset(&msg, 0, sizeof(msg));
826 msg.msg_name = (struct sockaddr*) &sa;
827 msg.msg_namelen = sizeof(sa);
831 msg.msg_control = aux;
832 msg.msg_controllen = sizeof(aux);
835 if ((l = recvmsg(fd, &msg, 0)) < 0) {
836 /* Linux returns EAGAIN when an invalid IP packet has been
837 received. We suppress warnings in this case because this might
838 create quite a bit of log traffic on machines with unstable
842 catta_log_warn("recvmsg(): %s", strerror(errno));
847 /* For corrupt packets FIONREAD returns zero size (See rhbz #607297). So
848 * fail after having read them. */
852 assert(!(msg.msg_flags & MSG_CTRUNC));
853 assert(!(msg.msg_flags & MSG_TRUNC));
855 p->size = (size_t) l;
858 *ret_src_port = catta_port_from_sockaddr((struct sockaddr*) &sa);
860 if (ret_src_address) {
862 catta_address_from_sockaddr((struct sockaddr*) &sa, &a);
863 *ret_src_address = a.data.ipv6;
866 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
868 if (cmsg->cmsg_level == IPPROTO_IPV6) {
870 switch (cmsg->cmsg_type) {
875 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
882 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
884 if (ret_iface && i->ipi6_ifindex > 0)
885 *ret_iface = i->ipi6_ifindex;
888 memcpy(ret_dst_address->address, i->ipi6_addr.s6_addr, 16);
895 catta_log_warn("Unhandled cmsg_type: %d", cmsg->cmsg_type);
908 catta_dns_packet_free(p);
913 int catta_open_unicast_socket_ipv4(void) {
914 struct sockaddr_in local;
917 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
918 catta_log_warn("socket() failed: %s", strerror(errno));
922 memset(&local, 0, sizeof(local));
923 local.sin_family = AF_INET;
925 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
926 catta_log_warn("bind() failed: %s", strerror(errno));
930 if (ipv4_pktinfo(fd) < 0) {
934 if (catta_set_cloexec(fd) < 0) {
935 catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
939 if (catta_set_nonblock(fd) < 0) {
940 catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));
953 int catta_open_unicast_socket_ipv6(void) {
954 struct sockaddr_in6 local;
957 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
958 catta_log_warn("socket() failed: %s", strerror(errno));
963 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&yes, sizeof(yes)) < 0) {
964 catta_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
968 memset(&local, 0, sizeof(local));
969 local.sin6_family = AF_INET6;
971 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
972 catta_log_warn("bind() failed: %s", strerror(errno));
976 if (ipv6_pktinfo(fd) < 0)
979 if (catta_set_cloexec(fd) < 0) {
980 catta_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
984 if (catta_set_nonblock(fd) < 0) {
985 catta_log_warn("O_NONBLOCK failed: %s", strerror(errno));