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 int avahi_mdns_mcast_join_ipv4(int fd, int idx) {
103 /* FIXME: sebest : we must factorize this part of the code */
104 struct sockaddr_in sa;
105 #ifdef HAVE_STRUCT_IP_MREQN
106 struct ip_mreqn mreq;
108 memset(&mreq, 0, sizeof(mreq));
109 mreq.imr_ifindex = idx;
114 memset(&mreq, 0, sizeof(mreq));
116 if ((!if_indextoname(idx, ifreq.ifr_name))) {
117 avahi_log_warn("if_indextoname failed: %s", strerror(errno));
121 if (ioctl(fd, SIOCGIFADDR, &ifreq) < 0) {
122 avahi_log_warn("SIOCGIFADDR failed: %s", strerror(errno));
126 memcpy(&mreq.imr_interface,
127 &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
128 sizeof(struct in_addr));
130 mdns_mcast_group_ipv4 (&sa);
131 mreq.imr_multiaddr = sa.sin_addr;
133 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
134 avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
141 int avahi_mdns_mcast_join_ipv6(int fd, int idx) {
142 struct ipv6_mreq mreq6;
143 struct sockaddr_in6 sa6;
145 mdns_mcast_group_ipv6 (&sa6);
147 memset(&mreq6, 0, sizeof(mreq6));
148 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
149 mreq6.ipv6mr_interface = idx;
151 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
152 avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s", strerror(errno));
159 int avahi_mdns_mcast_leave_ipv4(int fd, int idx) {
160 /* FIXME: sebest : we must factorize this part of the code */
161 struct sockaddr_in sa;
162 #ifdef HAVE_STRUCT_IP_MREQN
163 struct ip_mreqn mreq;
165 memset(&mreq, 0, sizeof(mreq));
166 mreq.imr_ifindex = idx;
171 memset(&mreq, 0, sizeof(mreq));
173 if ((!if_indextoname(idx, ifreq.ifr_name))) {
174 avahi_log_warn("if_indextoname failed: %s", strerror(errno));
178 if (ioctl(fd, SIOCGIFADDR, &ifreq) < 0) {
179 avahi_log_warn("SIOCGIFADDR failed: %s", strerror(errno));
183 memcpy(&mreq.imr_interface,
184 &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
185 sizeof(struct in_addr));
187 mdns_mcast_group_ipv4 (&sa);
188 mreq.imr_multiaddr = sa.sin_addr;
190 if (setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
191 avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s", strerror(errno));
198 int avahi_mdns_mcast_leave_ipv6(int fd, int idx) {
199 struct ipv6_mreq mreq6;
200 struct sockaddr_in6 sa6;
202 mdns_mcast_group_ipv6 (&sa6);
204 memset(&mreq6, 0, sizeof(mreq6));
205 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
206 mreq6.ipv6mr_interface = idx;
208 if (setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
209 avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s", strerror(errno));
216 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
223 if (bind(fd, sa, l) < 0) {
225 if (errno != EADDRINUSE) {
226 avahi_log_warn("bind() failed: %s", strerror(errno));
230 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
231 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
233 /* Try again, this time with SO_REUSEADDR set */
235 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
236 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
240 if (bind(fd, sa, l) < 0) {
241 avahi_log_warn("bind() failed: %s", strerror(errno));
246 /* We enable SO_REUSEADDR afterwards, to make sure that the
247 * user may run other mDNS implementations if he really
251 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
252 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
260 static int ip_pktinfo (int fd, int yes)
265 if ((ret = setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes))) < 0) {
266 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
269 #ifdef IP_RECVINTERFACE
270 if ((ret = setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof (yes))) < 0) {
271 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
275 if ((ret = setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof (yes))) < 0) {
276 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
278 #endif /* IP_RECVIF */
279 #endif /* IP_RECVINTERFACE */
280 #if defined (IP_RECVDSTADDR) /* && !defined(solaris) */
281 if ((ret = setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof (yes))) < 0) {
282 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
284 #endif /* IP_RECVDSTADDR */
285 #endif /* IP_PKTINFO */
290 int avahi_open_socket_ipv4(int no_reuse) {
291 struct sockaddr_in local;
292 int fd = -1, ttl, yes, r;
294 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
295 avahi_log_warn("socket() failed: %s", strerror(errno));
300 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
301 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
306 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
307 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
312 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
313 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
317 memset(&local, 0, sizeof(local));
318 local.sin_family = AF_INET;
319 local.sin_port = htons(AVAHI_MDNS_PORT);
322 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
324 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
330 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
331 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
336 if (ip_pktinfo (fd, yes) < 0) {
340 if (avahi_set_cloexec(fd) < 0) {
341 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
345 if (avahi_set_nonblock(fd) < 0) {
346 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
359 int avahi_open_socket_ipv6(int no_reuse) {
360 struct sockaddr_in6 sa, local;
361 int fd = -1, ttl, yes, r;
363 mdns_mcast_group_ipv6(&sa);
365 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
366 avahi_log_warn("socket() failed: %s", strerror(errno));
371 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
372 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
377 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
378 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
383 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
384 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
389 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
390 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
394 memset(&local, 0, sizeof(local));
395 local.sin6_family = AF_INET6;
396 local.sin6_port = htons(AVAHI_MDNS_PORT);
399 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
401 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
407 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
408 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
413 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
414 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
418 if (avahi_set_cloexec(fd) < 0) {
419 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
423 if (avahi_set_nonblock(fd) < 0) {
424 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
437 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
443 if (sendmsg(fd, msg, flags) >= 0)
446 if (errno != EAGAIN) {
447 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
451 if (avahi_wait_for_write(fd) < 0)
458 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
459 struct sockaddr_in sa;
462 struct cmsghdr *cmsg;
464 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
466 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct sockaddr_dl)];
467 #elif IP_RECVINTERFACE
468 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(u_short)];
473 assert(avahi_dns_packet_check_valid(p) >= 0);
474 assert(!a || port > 0);
477 mdns_mcast_group_ipv4(&sa);
479 ipv4_address_to_sockaddr(&sa, a, port);
481 memset(&io, 0, sizeof(io));
482 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
483 io.iov_len = p->size;
485 memset(&msg, 0, sizeof(msg));
487 msg.msg_namelen = sizeof(sa);
492 if (interface >= 0) {
494 memset(cmsg_data, 0, sizeof(cmsg_data));
495 cmsg = (struct cmsghdr*) cmsg_data;
496 cmsg->cmsg_len = sizeof(cmsg_data);
497 cmsg->cmsg_level = IPPROTO_IP;
500 struct in_pktinfo *pkti;
502 cmsg->cmsg_type = IP_PKTINFO;
503 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
504 pkti->ipi_ifindex = interface;
508 struct sockaddr_dl *pkti;
510 cmsg->cmsg_type = IP_RECVIF;
511 pkti = (struct sockaddr_dl*) (cmsg_data + sizeof(struct cmsghdr));
512 pkti->sdl_index = interface;
514 #elif IP_RECVINTERFACE
518 cmsg->cmsg_type = IP_RECVINTERFACE;
519 i = (u_short *) (cmsg_data + sizeof(u_short));
520 memcpy(&i, CMSG_DATA (cmsg), sizeof(u_short));
523 msg.msg_control = cmsg_data;
524 msg.msg_controllen = sizeof(cmsg_data);
526 msg.msg_control = NULL;
527 msg.msg_controllen = 0;
530 return sendmsg_loop(fd, &msg, 0);
533 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
534 struct sockaddr_in6 sa;
537 struct cmsghdr *cmsg;
538 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
542 assert(avahi_dns_packet_check_valid(p) >= 0);
545 mdns_mcast_group_ipv6(&sa);
547 ipv6_address_to_sockaddr(&sa, a, port);
549 memset(&io, 0, sizeof(io));
550 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
551 io.iov_len = p->size;
554 memset(&msg, 0, sizeof(msg));
556 msg.msg_namelen = sizeof(sa);
561 if (interface >= 0) {
562 struct in6_pktinfo *pkti;
564 memset(cmsg_data, 0, sizeof(cmsg_data));
565 cmsg = (struct cmsghdr*) cmsg_data;
566 cmsg->cmsg_len = sizeof(cmsg_data);
567 cmsg->cmsg_level = IPPROTO_IPV6;
568 cmsg->cmsg_type = IPV6_PKTINFO;
570 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
571 pkti->ipi6_ifindex = interface;
573 msg.msg_control = cmsg_data;
574 msg.msg_controllen = sizeof(cmsg_data);
576 msg.msg_control = NULL;
577 msg.msg_controllen = 0;
580 return sendmsg_loop(fd, &msg, 0);
583 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
584 AvahiDnsPacket *p= NULL;
589 struct cmsghdr *cmsg;
590 int found_ttl = 0, found_iface = 0, found_addr = 0;
595 if (ioctl(fd, FIONREAD, &ms) < 0) {
596 avahi_log_warn("ioctl(): %s", strerror(errno));
600 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
602 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
603 io.iov_len = p->max_size;
605 memset(&msg, 0, sizeof(msg));
607 msg.msg_name = ret_sa;
608 msg.msg_namelen = sizeof(struct sockaddr_in);
615 msg.msg_control = aux;
616 msg.msg_controllen = sizeof(aux);
619 if ((l = recvmsg(fd, &msg, 0)) < 0) {
620 avahi_log_warn("recvmsg(): %s", strerror(errno));
624 if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
625 /* Linux 2.4 behaves very strangely sometimes! */
627 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
631 assert(!(msg.msg_flags & MSG_CTRUNC));
632 assert(!(msg.msg_flags & MSG_TRUNC));
633 p->size = (size_t) l;
638 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
640 if (cmsg->cmsg_level == IPPROTO_IP) {
641 switch (cmsg->cmsg_type) {
647 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
653 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
656 *ret_iface = (int) i->ipi_ifindex;
658 if (ret_dest_address)
659 ret_dest_address->address = i->ipi_addr.s_addr;
660 found_addr = found_iface = 1;
666 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
669 *ret_iface = (int) sdl->sdl_index;
674 #ifdef IP_RECVDSTADDR
676 if (ret_dest_address)
677 memcpy (&ret_dest_address->address, CMSG_DATA (cmsg), 4);
682 avahi_log_warn("Unhandled cmsg_type : %d\n",cmsg->cmsg_type);
696 avahi_dns_packet_free(p);
701 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
702 AvahiDnsPacket *p = NULL;
709 struct cmsghdr *cmsg;
710 int found_ttl = 0, found_iface = 0;
714 if (ioctl(fd, FIONREAD, &ms) < 0) {
715 avahi_log_warn("ioctl(): %s", strerror(errno));
719 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
721 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
722 io.iov_len = p->max_size;
724 memset(&msg, 0, sizeof(msg));
726 msg.msg_name = ret_sa;
727 msg.msg_namelen = sizeof(struct sockaddr_in6);
735 msg.msg_control = aux;
736 msg.msg_controllen = sizeof(aux);
739 if ((l = recvmsg(fd, &msg, 0)) < 0) {
740 avahi_log_warn("recvmsg(): %s", strerror(errno));
744 p->size = (size_t) l;
749 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
750 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
753 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
758 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
759 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
762 *ret_iface = i->ipi6_ifindex;
764 if (ret_dest_address)
765 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
777 avahi_dns_packet_free(p);
782 int avahi_open_unicast_socket_ipv4(void) {
783 struct sockaddr_in local;
786 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
787 avahi_log_warn("socket() failed: %s", strerror(errno));
791 memset(&local, 0, sizeof(local));
792 local.sin_family = AF_INET;
794 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
795 avahi_log_warn("bind() failed: %s", strerror(errno));
800 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
801 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
806 if (ip_pktinfo (fd, yes) < 0) {
810 if (avahi_set_cloexec(fd) < 0) {
811 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
815 if (avahi_set_nonblock(fd) < 0) {
816 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
829 int avahi_open_unicast_socket_ipv6(void) {
830 struct sockaddr_in6 local;
833 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
834 avahi_log_warn("socket() failed: %s", strerror(errno));
838 memset(&local, 0, sizeof(local));
839 local.sin6_family = AF_INET6;
841 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
842 avahi_log_warn("bind() failed: %s", strerror(errno));
847 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
848 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
853 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
854 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
858 if (avahi_set_cloexec(fd) < 0) {
859 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
863 if (avahi_set_nonblock(fd) < 0) {
864 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));