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
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
37 #include <sys/ioctl.h>
41 #include <net/if_dl.h>
49 /* this is a portability hack */
50 #ifndef IPV6_ADD_MEMBERSHIP
51 #ifdef IPV6_JOIN_GROUP
52 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
56 #ifndef IPV6_DROP_MEMBERSHIP
57 #ifdef IPV6_LEAVE_GROUP
58 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
62 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
65 memset(ret_sa, 0, sizeof(struct sockaddr_in));
66 ret_sa->sin_family = AF_INET;
67 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
68 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
71 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
74 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
75 ret_sa->sin6_family = AF_INET6;
76 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
77 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
80 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
85 memset(ret_sa, 0, sizeof(struct sockaddr_in));
86 ret_sa->sin_family = AF_INET;
87 ret_sa->sin_port = htons(port);
88 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
91 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
96 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
97 ret_sa->sin6_family = AF_INET6;
98 ret_sa->sin6_port = htons(port);
99 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
102 #ifdef HAVE_STRUCT_IP_MREQN
103 int avahi_mdns_mcast_join_ipv4(int fd, int idx, int join) {
104 struct ip_mreqn mreq;
106 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiAddress *a, int join) {
110 struct sockaddr_in sa;
111 memset(&mreq, 0, sizeof(mreq));
113 #ifdef HAVE_STRUCT_IP_MREQN
114 mreq.imr_ifindex = idx;
117 assert(a->proto == AVAHI_PROTO_INET);
118 mreq.imr_interface.s_addr = a->data.ipv4.address;
121 mdns_mcast_group_ipv4(&sa);
122 mreq.imr_multiaddr = sa.sin_addr;
124 if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
125 avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
132 int avahi_mdns_mcast_join_ipv6(int fd, int idx, int join) {
133 struct ipv6_mreq mreq6;
134 struct sockaddr_in6 sa6;
136 mdns_mcast_group_ipv6 (&sa6);
138 memset(&mreq6, 0, sizeof(mreq6));
139 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
140 mreq6.ipv6mr_interface = idx;
142 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
143 avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
150 static int reuseaddr(int fd) {
154 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
155 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
161 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
162 avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
170 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
176 if (bind(fd, sa, l) < 0) {
178 if (errno != EADDRINUSE) {
179 avahi_log_warn("bind() failed: %s", strerror(errno));
183 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
184 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
186 /* Try again, this time with SO_REUSEADDR set */
187 if (reuseaddr(fd) < 0)
190 if (bind(fd, sa, l) < 0) {
191 avahi_log_warn("bind() failed: %s", strerror(errno));
196 /* We enable SO_REUSEADDR afterwards, to make sure that the
197 * user may run other mDNS implementations if he really
200 if (reuseaddr(fd) < 0)
207 static int ipv4_pktinfo(int fd) {
212 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
213 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
218 #ifdef IP_RECVINTERFACE
220 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
221 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
224 #elif defined(IP_RECVIF)
226 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
227 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
232 #ifdef IP_RECVDSTADDR
234 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
235 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
240 #endif /* IP_PKTINFO */
244 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
245 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
253 static int ipv6_pktinfo(int fd) {
256 #ifdef IPV6_RECVPKTINFO
258 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
259 avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
262 #elif defined(IPV6_PKTINFO)
264 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
265 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
272 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
273 avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
276 #elif defined(IPV6_RECVHOPLIMIT)
278 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
279 avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
282 #elif defined(IPV6_HOPLIMIT)
284 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
285 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
293 int avahi_open_socket_ipv4(int no_reuse) {
294 struct sockaddr_in local;
295 int fd = -1, r, ittl;
298 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
299 avahi_log_warn("socket() failed: %s", strerror(errno));
304 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
305 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
310 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
311 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
316 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
317 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
321 memset(&local, 0, sizeof(local));
322 local.sin_family = AF_INET;
323 local.sin_port = htons(AVAHI_MDNS_PORT);
326 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
328 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
333 if (ipv4_pktinfo (fd) < 0)
336 if (avahi_set_cloexec(fd) < 0) {
337 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
341 if (avahi_set_nonblock(fd) < 0) {
342 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
355 int avahi_open_socket_ipv6(int no_reuse) {
356 struct sockaddr_in6 sa, local;
360 mdns_mcast_group_ipv6(&sa);
362 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
363 avahi_log_warn("socket() failed: %s", strerror(errno));
368 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
369 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
374 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
375 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
380 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
381 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
386 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
387 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
391 memset(&local, 0, sizeof(local));
392 local.sin6_family = AF_INET6;
393 local.sin6_port = htons(AVAHI_MDNS_PORT);
396 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
398 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
403 if (ipv6_pktinfo(fd) < 0)
406 if (avahi_set_cloexec(fd) < 0) {
407 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
411 if (avahi_set_nonblock(fd) < 0) {
412 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
425 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
431 if (sendmsg(fd, msg, flags) >= 0)
434 if (errno != EAGAIN) {
435 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
439 if (avahi_wait_for_write(fd) < 0)
446 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
447 struct sockaddr_in sa;
451 struct cmsghdr *cmsg;
452 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
457 assert(avahi_dns_packet_check_valid(p) >= 0);
458 assert(!a || port > 0);
461 mdns_mcast_group_ipv4(&sa);
463 ipv4_address_to_sockaddr(&sa, a, port);
465 memset(&io, 0, sizeof(io));
466 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
467 io.iov_len = p->size;
469 memset(&msg, 0, sizeof(msg));
471 msg.msg_namelen = sizeof(sa);
475 msg.msg_control = NULL;
476 msg.msg_controllen = 0;
479 if (interface >= 0) {
480 struct in_pktinfo *pkti;
482 memset(cmsg_data, 0, sizeof(cmsg_data));
483 cmsg = (struct cmsghdr*) cmsg_data;
484 cmsg->cmsg_len = sizeof(cmsg_data);
485 cmsg->cmsg_level = IPPROTO_IP;
486 cmsg->cmsg_type = IP_PKTINFO;
488 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
489 pkti->ipi_ifindex = interface;
491 msg.msg_control = cmsg_data;
492 msg.msg_controllen = sizeof(cmsg_data);
496 #warning "FIXME: We need some code to set the outgoing interface here if IP_PKTINFO is not available"
500 return sendmsg_loop(fd, &msg, 0);
503 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
504 struct sockaddr_in6 sa;
507 struct cmsghdr *cmsg;
508 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
512 assert(avahi_dns_packet_check_valid(p) >= 0);
515 mdns_mcast_group_ipv6(&sa);
517 ipv6_address_to_sockaddr(&sa, a, port);
519 memset(&io, 0, sizeof(io));
520 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
521 io.iov_len = p->size;
524 memset(&msg, 0, sizeof(msg));
526 msg.msg_namelen = sizeof(sa);
531 if (interface >= 0) {
532 struct in6_pktinfo *pkti;
534 memset(cmsg_data, 0, sizeof(cmsg_data));
535 cmsg = (struct cmsghdr*) cmsg_data;
536 cmsg->cmsg_len = sizeof(cmsg_data);
537 cmsg->cmsg_level = IPPROTO_IPV6;
538 cmsg->cmsg_type = IPV6_PKTINFO;
540 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
541 pkti->ipi6_ifindex = interface;
543 msg.msg_control = cmsg_data;
544 msg.msg_controllen = sizeof(cmsg_data);
546 msg.msg_control = NULL;
547 msg.msg_controllen = 0;
550 return sendmsg_loop(fd, &msg, 0);
553 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
554 AvahiDnsPacket *p= NULL;
559 struct cmsghdr *cmsg;
560 int found_iface = 0, found_addr = 0;
565 if (ioctl(fd, FIONREAD, &ms) < 0) {
566 avahi_log_warn("ioctl(): %s", strerror(errno));
570 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
572 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
573 io.iov_len = p->max_size;
575 memset(&msg, 0, sizeof(msg));
577 msg.msg_name = ret_sa;
578 msg.msg_namelen = sizeof(struct sockaddr_in);
585 msg.msg_control = aux;
586 msg.msg_controllen = sizeof(aux);
589 if ((l = recvmsg(fd, &msg, 0)) < 0) {
590 avahi_log_warn("recvmsg(): %s", strerror(errno));
594 if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
595 /* Linux 2.4 behaves very strangely sometimes! */
597 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
601 assert(!(msg.msg_flags & MSG_CTRUNC));
602 assert(!(msg.msg_flags & MSG_TRUNC));
603 p->size = (size_t) l;
608 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
610 if (cmsg->cmsg_level == IPPROTO_IP) {
611 switch (cmsg->cmsg_type) {
617 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
623 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
626 *ret_iface = (int) i->ipi_ifindex;
628 if (ret_dest_address)
629 ret_dest_address->address = i->ipi_addr.s_addr;
631 found_addr = found_iface = 1;
635 #elif defined(IP_RECVIF)
637 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
640 *ret_iface = (int) sdl->sdl_index;
647 #ifdef IP_RECVDSTADDR
649 if (ret_dest_address)
650 memcpy(&ret_dest_address->address, CMSG_DATA (cmsg), 4);
657 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
670 avahi_dns_packet_free(p);
675 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
676 AvahiDnsPacket *p = NULL;
682 struct cmsghdr *cmsg;
683 int found_ttl = 0, found_iface = 0;
687 if (ioctl(fd, FIONREAD, &ms) < 0) {
688 avahi_log_warn("ioctl(): %s", strerror(errno));
692 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
694 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
695 io.iov_len = p->max_size;
697 memset(&msg, 0, sizeof(msg));
699 msg.msg_name = ret_sa;
700 msg.msg_namelen = sizeof(struct sockaddr_in6);
708 msg.msg_control = aux;
709 msg.msg_controllen = sizeof(aux);
712 if ((l = recvmsg(fd, &msg, 0)) < 0) {
713 avahi_log_warn("recvmsg(): %s", strerror(errno));
717 p->size = (size_t) l;
722 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
723 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
726 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
731 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
732 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
735 *ret_iface = i->ipi6_ifindex;
737 if (ret_dest_address)
738 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
751 avahi_dns_packet_free(p);
756 int avahi_open_unicast_socket_ipv4(void) {
757 struct sockaddr_in local;
760 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
761 avahi_log_warn("socket() failed: %s", strerror(errno));
765 memset(&local, 0, sizeof(local));
766 local.sin_family = AF_INET;
768 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
769 avahi_log_warn("bind() failed: %s", strerror(errno));
773 if (ipv4_pktinfo(fd) < 0) {
777 if (avahi_set_cloexec(fd) < 0) {
778 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
782 if (avahi_set_nonblock(fd) < 0) {
783 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
796 int avahi_open_unicast_socket_ipv6(void) {
797 struct sockaddr_in6 local;
800 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
801 avahi_log_warn("socket() failed: %s", strerror(errno));
805 memset(&local, 0, sizeof(local));
806 local.sin6_family = AF_INET6;
808 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
809 avahi_log_warn("bind() failed: %s", strerror(errno));
813 if (ipv6_pktinfo(fd) < 0)
816 if (avahi_set_cloexec(fd) < 0) {
817 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
821 if (avahi_set_nonblock(fd) < 0) {
822 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));