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>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 #include <net/if_dl.h>
52 /* this is a portability hack */
53 #ifndef IPV6_ADD_MEMBERSHIP
54 #ifdef IPV6_JOIN_GROUP
55 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
59 #ifndef IPV6_DROP_MEMBERSHIP
60 #ifdef IPV6_LEAVE_GROUP
61 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
65 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
68 memset(ret_sa, 0, sizeof(struct sockaddr_in));
69 ret_sa->sin_family = AF_INET;
70 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
71 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
74 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
77 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
78 ret_sa->sin6_family = AF_INET6;
79 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
80 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
83 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
88 memset(ret_sa, 0, sizeof(struct sockaddr_in));
89 ret_sa->sin_family = AF_INET;
90 ret_sa->sin_port = htons(port);
91 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
94 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
99 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
100 ret_sa->sin6_family = AF_INET6;
101 ret_sa->sin6_port = htons(port);
102 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
105 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiIPv4Address *a, int idx, int join) {
106 #ifdef HAVE_STRUCT_IP_MREQN
107 struct ip_mreqn mreq;
111 struct sockaddr_in sa;
117 memset(&mreq, 0, sizeof(mreq));
118 #ifdef HAVE_STRUCT_IP_MREQN
119 mreq.imr_ifindex = idx;
120 mreq.imr_address.s_addr = a->address;
122 mreq.imr_interface.s_addr = a->address;
124 mdns_mcast_group_ipv4(&sa);
125 mreq.imr_multiaddr = sa.sin_addr;
127 if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
128 avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
135 int avahi_mdns_mcast_join_ipv6(int fd, const AvahiIPv6Address *a, int idx, int join) {
136 struct ipv6_mreq mreq6;
137 struct sockaddr_in6 sa6;
143 memset(&mreq6, 0, sizeof(mreq6));
144 mdns_mcast_group_ipv6 (&sa6);
145 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
146 mreq6.ipv6mr_interface = idx;
148 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
149 avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
156 static int reuseaddr(int fd) {
160 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
161 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
167 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
168 avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
176 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
182 if (bind(fd, sa, l) < 0) {
184 if (errno != EADDRINUSE) {
185 avahi_log_warn("bind() failed: %s", strerror(errno));
189 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
190 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
192 /* Try again, this time with SO_REUSEADDR set */
193 if (reuseaddr(fd) < 0)
196 if (bind(fd, sa, l) < 0) {
197 avahi_log_warn("bind() failed: %s", strerror(errno));
202 /* We enable SO_REUSEADDR afterwards, to make sure that the
203 * user may run other mDNS implementations if he really
206 if (reuseaddr(fd) < 0)
213 static int ipv4_pktinfo(int fd) {
218 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
219 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
224 #ifdef IP_RECVINTERFACE
226 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
227 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
230 #elif defined(IP_RECVIF)
232 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
233 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
238 #ifdef IP_RECVDSTADDR
240 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
241 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
246 #endif /* IP_PKTINFO */
250 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
251 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
259 static int ipv6_pktinfo(int fd) {
262 #ifdef IPV6_RECVPKTINFO
264 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
265 avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
268 #elif defined(IPV6_PKTINFO)
270 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
271 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
278 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
279 avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
282 #elif defined(IPV6_RECVHOPLIMIT)
284 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
285 avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
288 #elif defined(IPV6_HOPLIMIT)
290 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
291 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
299 int avahi_open_socket_ipv4(int no_reuse) {
300 struct sockaddr_in local;
301 int fd = -1, r, ittl;
304 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
305 avahi_log_warn("socket() failed: %s", strerror(errno));
310 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
311 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
316 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
317 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
322 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
323 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
327 memset(&local, 0, sizeof(local));
328 local.sin_family = AF_INET;
329 local.sin_port = htons(AVAHI_MDNS_PORT);
332 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
334 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
339 if (ipv4_pktinfo (fd) < 0)
342 if (avahi_set_cloexec(fd) < 0) {
343 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
347 if (avahi_set_nonblock(fd) < 0) {
348 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
361 int avahi_open_socket_ipv6(int no_reuse) {
362 struct sockaddr_in6 sa, local;
366 mdns_mcast_group_ipv6(&sa);
368 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
369 avahi_log_warn("socket() failed: %s", strerror(errno));
374 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
375 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
380 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
381 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
386 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
387 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
392 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
393 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
397 memset(&local, 0, sizeof(local));
398 local.sin6_family = AF_INET6;
399 local.sin6_port = htons(AVAHI_MDNS_PORT);
402 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
404 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
409 if (ipv6_pktinfo(fd) < 0)
412 if (avahi_set_cloexec(fd) < 0) {
413 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
417 if (avahi_set_nonblock(fd) < 0) {
418 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
431 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
437 if (sendmsg(fd, msg, flags) >= 0)
440 if (errno != EAGAIN) {
441 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
445 if (avahi_wait_for_write(fd) < 0)
452 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
453 struct sockaddr_in sa;
457 struct cmsghdr *cmsg;
458 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
463 assert(avahi_dns_packet_check_valid(p) >= 0);
464 assert(!a || port > 0);
467 mdns_mcast_group_ipv4(&sa);
469 ipv4_address_to_sockaddr(&sa, a, port);
471 memset(&io, 0, sizeof(io));
472 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
473 io.iov_len = p->size;
475 memset(&msg, 0, sizeof(msg));
477 msg.msg_namelen = sizeof(sa);
481 msg.msg_control = NULL;
482 msg.msg_controllen = 0;
485 if (interface >= 0) {
486 struct in_pktinfo *pkti;
488 memset(cmsg_data, 0, sizeof(cmsg_data));
489 cmsg = (struct cmsghdr*) cmsg_data;
490 cmsg->cmsg_len = sizeof(cmsg_data);
491 cmsg->cmsg_level = IPPROTO_IP;
492 cmsg->cmsg_type = IP_PKTINFO;
494 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
495 pkti->ipi_ifindex = interface;
497 msg.msg_control = cmsg_data;
498 msg.msg_controllen = sizeof(cmsg_data);
502 #warning "FIXME: We need some code to set the outgoing interface here if IP_PKTINFO is not available"
506 return sendmsg_loop(fd, &msg, 0);
509 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
510 struct sockaddr_in6 sa;
513 struct cmsghdr *cmsg;
514 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
518 assert(avahi_dns_packet_check_valid(p) >= 0);
521 mdns_mcast_group_ipv6(&sa);
523 ipv6_address_to_sockaddr(&sa, a, port);
525 memset(&io, 0, sizeof(io));
526 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
527 io.iov_len = p->size;
530 memset(&msg, 0, sizeof(msg));
532 msg.msg_namelen = sizeof(sa);
537 if (interface >= 0) {
538 struct in6_pktinfo *pkti;
540 memset(cmsg_data, 0, sizeof(cmsg_data));
541 cmsg = (struct cmsghdr*) cmsg_data;
542 cmsg->cmsg_len = sizeof(cmsg_data);
543 cmsg->cmsg_level = IPPROTO_IPV6;
544 cmsg->cmsg_type = IPV6_PKTINFO;
546 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
547 pkti->ipi6_ifindex = interface;
549 msg.msg_control = cmsg_data;
550 msg.msg_controllen = sizeof(cmsg_data);
552 msg.msg_control = NULL;
553 msg.msg_controllen = 0;
556 return sendmsg_loop(fd, &msg, 0);
559 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
560 AvahiDnsPacket *p= NULL;
565 struct cmsghdr *cmsg;
566 int found_iface = 0, found_addr = 0;
571 if (ioctl(fd, FIONREAD, &ms) < 0) {
572 avahi_log_warn("ioctl(): %s", strerror(errno));
576 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
578 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
579 io.iov_len = p->max_size;
581 memset(&msg, 0, sizeof(msg));
583 msg.msg_name = ret_sa;
584 msg.msg_namelen = sizeof(struct sockaddr_in);
591 msg.msg_control = aux;
592 msg.msg_controllen = sizeof(aux);
595 if ((l = recvmsg(fd, &msg, 0)) < 0) {
596 avahi_log_warn("recvmsg(): %s", strerror(errno));
600 if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
601 /* Linux 2.4 behaves very strangely sometimes! */
603 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
607 assert(!(msg.msg_flags & MSG_CTRUNC));
608 assert(!(msg.msg_flags & MSG_TRUNC));
609 p->size = (size_t) l;
614 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
616 if (cmsg->cmsg_level == IPPROTO_IP) {
617 switch (cmsg->cmsg_type) {
623 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
629 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
632 *ret_iface = (int) i->ipi_ifindex;
634 if (ret_dest_address)
635 ret_dest_address->address = i->ipi_addr.s_addr;
637 found_addr = found_iface = 1;
641 #elif defined(IP_RECVIF)
643 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
646 *ret_iface = (int) sdl->sdl_index;
653 #ifdef IP_RECVDSTADDR
655 if (ret_dest_address)
656 memcpy(&ret_dest_address->address, CMSG_DATA (cmsg), 4);
663 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
676 avahi_dns_packet_free(p);
681 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
682 AvahiDnsPacket *p = NULL;
688 struct cmsghdr *cmsg;
689 int found_ttl = 0, found_iface = 0;
693 if (ioctl(fd, FIONREAD, &ms) < 0) {
694 avahi_log_warn("ioctl(): %s", strerror(errno));
698 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
700 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
701 io.iov_len = p->max_size;
703 memset(&msg, 0, sizeof(msg));
705 msg.msg_name = ret_sa;
706 msg.msg_namelen = sizeof(struct sockaddr_in6);
714 msg.msg_control = aux;
715 msg.msg_controllen = sizeof(aux);
718 if ((l = recvmsg(fd, &msg, 0)) < 0) {
719 avahi_log_warn("recvmsg(): %s", strerror(errno));
723 p->size = (size_t) l;
728 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
729 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
732 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
737 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
738 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
741 *ret_iface = i->ipi6_ifindex;
743 if (ret_dest_address)
744 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
757 avahi_dns_packet_free(p);
762 int avahi_open_unicast_socket_ipv4(void) {
763 struct sockaddr_in local;
766 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
767 avahi_log_warn("socket() failed: %s", strerror(errno));
771 memset(&local, 0, sizeof(local));
772 local.sin_family = AF_INET;
774 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
775 avahi_log_warn("bind() failed: %s", strerror(errno));
779 if (ipv4_pktinfo(fd) < 0) {
783 if (avahi_set_cloexec(fd) < 0) {
784 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
788 if (avahi_set_nonblock(fd) < 0) {
789 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
802 int avahi_open_unicast_socket_ipv6(void) {
803 struct sockaddr_in6 local;
806 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
807 avahi_log_warn("socket() failed: %s", strerror(errno));
811 memset(&local, 0, sizeof(local));
812 local.sin6_family = AF_INET6;
814 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
815 avahi_log_warn("bind() failed: %s", strerror(errno));
819 if (ipv6_pktinfo(fd) < 0)
822 if (avahi_set_cloexec(fd) < 0) {
823 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
827 if (avahi_set_nonblock(fd) < 0) {
828 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));