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>
51 #include "addr-util.h"
53 /* this is a portability hack */
54 #ifndef IPV6_ADD_MEMBERSHIP
55 #ifdef IPV6_JOIN_GROUP
56 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
60 #ifndef IPV6_DROP_MEMBERSHIP
61 #ifdef IPV6_LEAVE_GROUP
62 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
66 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
69 memset(ret_sa, 0, sizeof(struct sockaddr_in));
70 ret_sa->sin_family = AF_INET;
71 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
72 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
75 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
78 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
79 ret_sa->sin6_family = AF_INET6;
80 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
81 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
84 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
89 memset(ret_sa, 0, sizeof(struct sockaddr_in));
90 ret_sa->sin_family = AF_INET;
91 ret_sa->sin_port = htons(port);
92 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
95 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
100 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
101 ret_sa->sin6_family = AF_INET6;
102 ret_sa->sin6_port = htons(port);
103 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
106 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiIPv4Address *a, int idx, int join) {
107 #ifdef HAVE_STRUCT_IP_MREQN
108 struct ip_mreqn mreq;
112 struct sockaddr_in sa;
118 memset(&mreq, 0, sizeof(mreq));
119 #ifdef HAVE_STRUCT_IP_MREQN
120 mreq.imr_ifindex = idx;
121 mreq.imr_address.s_addr = a->address;
123 mreq.imr_interface.s_addr = a->address;
125 mdns_mcast_group_ipv4(&sa);
126 mreq.imr_multiaddr = sa.sin_addr;
128 if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
129 avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
136 int avahi_mdns_mcast_join_ipv6(int fd, const AvahiIPv6Address *a, int idx, int join) {
137 struct ipv6_mreq mreq6;
138 struct sockaddr_in6 sa6;
144 memset(&mreq6, 0, sizeof(mreq6));
145 mdns_mcast_group_ipv6 (&sa6);
146 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
147 mreq6.ipv6mr_interface = idx;
149 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
150 avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
157 static int reuseaddr(int fd) {
161 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
162 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
168 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
169 avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
177 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
183 if (bind(fd, sa, l) < 0) {
185 if (errno != EADDRINUSE) {
186 avahi_log_warn("bind() failed: %s", strerror(errno));
190 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
191 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
193 /* Try again, this time with SO_REUSEADDR set */
194 if (reuseaddr(fd) < 0)
197 if (bind(fd, sa, l) < 0) {
198 avahi_log_warn("bind() failed: %s", strerror(errno));
203 /* We enable SO_REUSEADDR afterwards, to make sure that the
204 * user may run other mDNS implementations if he really
207 if (reuseaddr(fd) < 0)
214 static int ipv4_pktinfo(int fd) {
219 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
220 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
225 #ifdef IP_RECVINTERFACE
227 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
228 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
231 #elif defined(IP_RECVIF)
233 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
234 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
239 #ifdef IP_RECVDSTADDR
241 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
242 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
247 #endif /* IP_PKTINFO */
251 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
252 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
260 static int ipv6_pktinfo(int fd) {
263 #ifdef IPV6_RECVPKTINFO
265 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
266 avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
269 #elif defined(IPV6_PKTINFO)
271 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
272 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
279 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
280 avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
283 #elif defined(IPV6_RECVHOPLIMIT)
285 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
286 avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
289 #elif defined(IPV6_HOPLIMIT)
291 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
292 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
300 int avahi_open_socket_ipv4(int no_reuse) {
301 struct sockaddr_in local;
302 int fd = -1, r, ittl;
305 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
306 avahi_log_warn("socket() failed: %s", strerror(errno));
311 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
312 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
317 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
318 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
323 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
324 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
328 memset(&local, 0, sizeof(local));
329 local.sin_family = AF_INET;
330 local.sin_port = htons(AVAHI_MDNS_PORT);
333 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
335 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
340 if (ipv4_pktinfo (fd) < 0)
343 if (avahi_set_cloexec(fd) < 0) {
344 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
348 if (avahi_set_nonblock(fd) < 0) {
349 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
362 int avahi_open_socket_ipv6(int no_reuse) {
363 struct sockaddr_in6 sa, local;
367 mdns_mcast_group_ipv6(&sa);
369 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
370 avahi_log_warn("socket() failed: %s", strerror(errno));
375 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
376 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
381 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
382 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
387 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
388 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
393 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
394 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
398 memset(&local, 0, sizeof(local));
399 local.sin6_family = AF_INET6;
400 local.sin6_port = htons(AVAHI_MDNS_PORT);
403 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
405 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
410 if (ipv6_pktinfo(fd) < 0)
413 if (avahi_set_cloexec(fd) < 0) {
414 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
418 if (avahi_set_nonblock(fd) < 0) {
419 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
432 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
438 if (sendmsg(fd, msg, flags) >= 0)
441 if (errno != EAGAIN) {
442 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
446 if (avahi_wait_for_write(fd) < 0)
453 int avahi_send_dns_packet_ipv4(int fd, AvahiIfIndex interface, AvahiDnsPacket *p, const AvahiIPv4Address *src_address, const AvahiIPv4Address *dst_address, uint16_t dst_port) {
454 struct sockaddr_in sa;
458 struct cmsghdr *cmsg;
459 uint8_t cmsg_data[CMSG_SPACE(sizeof(struct in_pktinfo))];
464 assert(avahi_dns_packet_check_valid(p) >= 0);
465 assert(!dst_address || dst_port > 0);
468 mdns_mcast_group_ipv4(&sa);
470 ipv4_address_to_sockaddr(&sa, dst_address, dst_port);
472 memset(&io, 0, sizeof(io));
473 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
474 io.iov_len = p->size;
476 memset(&msg, 0, sizeof(msg));
478 msg.msg_namelen = sizeof(sa);
482 msg.msg_control = NULL;
483 msg.msg_controllen = 0;
486 if (interface > 0 || src_address) {
487 struct in_pktinfo *pkti;
489 memset(cmsg_data, 0, sizeof(cmsg_data));
490 cmsg = (struct cmsghdr*) cmsg_data;
491 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
492 cmsg->cmsg_level = IPPROTO_IP;
493 cmsg->cmsg_type = IP_PKTINFO;
495 pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
498 pkti->ipi_ifindex = interface;
501 pkti->ipi_spec_dst.s_addr = src_address->address;
503 msg.msg_control = cmsg_data;
504 msg.msg_controllen = sizeof(cmsg_data);
508 #warning "FIXME: We need some code to set the outgoing interface/local address here if IP_PKTINFO is not available"
512 return sendmsg_loop(fd, &msg, 0);
515 int avahi_send_dns_packet_ipv6(int fd, AvahiIfIndex interface, AvahiDnsPacket *p, const AvahiIPv6Address *src_address, const AvahiIPv6Address *dst_address, uint16_t dst_port) {
516 struct sockaddr_in6 sa;
519 struct cmsghdr *cmsg;
520 uint8_t cmsg_data[CMSG_SPACE(sizeof(struct in6_pktinfo))];
524 assert(avahi_dns_packet_check_valid(p) >= 0);
525 assert(!dst_address || dst_port > 0);
528 mdns_mcast_group_ipv6(&sa);
530 ipv6_address_to_sockaddr(&sa, dst_address, dst_port);
532 memset(&io, 0, sizeof(io));
533 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
534 io.iov_len = p->size;
536 memset(&msg, 0, sizeof(msg));
538 msg.msg_namelen = sizeof(sa);
543 if (interface > 0 || src_address) {
544 struct in6_pktinfo *pkti;
546 memset(cmsg_data, 0, sizeof(cmsg_data));
547 cmsg = (struct cmsghdr*) cmsg_data;
548 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
549 cmsg->cmsg_level = IPPROTO_IPV6;
550 cmsg->cmsg_type = IPV6_PKTINFO;
552 pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
555 pkti->ipi6_ifindex = interface;
558 memcpy(&pkti->ipi6_addr, src_address->address, sizeof(src_address->address));
560 msg.msg_control = cmsg_data;
561 msg.msg_controllen = sizeof(cmsg_data);
563 msg.msg_control = NULL;
564 msg.msg_controllen = 0;
567 return sendmsg_loop(fd, &msg, 0);
570 AvahiDnsPacket *avahi_recv_dns_packet_ipv4(int fd, AvahiIPv4Address *ret_src_address, uint16_t *ret_src_port, AvahiIPv4Address *ret_dst_address, AvahiIfIndex *ret_iface, uint8_t *ret_ttl) {
571 AvahiDnsPacket *p= NULL;
576 struct cmsghdr *cmsg;
579 struct sockaddr_in sa;
583 if (ioctl(fd, FIONREAD, &ms) < 0) {
584 avahi_log_warn("ioctl(): %s", strerror(errno));
588 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
590 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
591 io.iov_len = p->max_size;
593 memset(&msg, 0, sizeof(msg));
595 msg.msg_namelen = sizeof(sa);
598 msg.msg_control = aux;
599 msg.msg_controllen = sizeof(aux);
602 if ((l = recvmsg(fd, &msg, 0)) < 0) {
603 avahi_log_warn("recvmsg(): %s", strerror(errno));
607 if (sa.sin_addr.s_addr == INADDR_ANY) {
608 /* Linux 2.4 behaves very strangely sometimes! */
612 assert(!(msg.msg_flags & MSG_CTRUNC));
613 assert(!(msg.msg_flags & MSG_TRUNC));
615 p->size = (size_t) l;
618 *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
620 if (ret_src_address) {
622 avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
623 *ret_src_address = a.data.ipv4;
630 *ret_iface = AVAHI_IF_UNSPEC;
632 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
634 if (cmsg->cmsg_level == IPPROTO_IP) {
636 switch (cmsg->cmsg_type) {
642 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
648 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
651 *ret_iface = (int) i->ipi_ifindex;
654 ret_dst_address->address = i->ipi_addr.s_addr;
664 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
667 *ret_iface = (int) sdl->sdl_index;
673 #ifdef IP_RECVDSTADDR
676 memcpy(&ret_dst_address->address, CMSG_DATA (cmsg), 4);
683 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
695 avahi_dns_packet_free(p);
700 AvahiDnsPacket *avahi_recv_dns_packet_ipv6(int fd, AvahiIPv6Address *ret_src_address, uint16_t *ret_src_port, AvahiIPv6Address *ret_dst_address, AvahiIfIndex *ret_iface, uint8_t *ret_ttl) {
701 AvahiDnsPacket *p = NULL;
707 struct cmsghdr *cmsg;
708 int found_ttl = 0, found_iface = 0;
709 struct sockaddr_in6 sa;
713 if (ioctl(fd, FIONREAD, &ms) < 0) {
714 avahi_log_warn("ioctl(): %s", strerror(errno));
718 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
720 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
721 io.iov_len = p->max_size;
723 memset(&msg, 0, sizeof(msg));
724 msg.msg_name = (struct sockaddr*) &sa;
725 msg.msg_namelen = sizeof(sa);
729 msg.msg_control = aux;
730 msg.msg_controllen = sizeof(aux);
733 if ((l = recvmsg(fd, &msg, 0)) < 0) {
734 avahi_log_warn("recvmsg(): %s", strerror(errno));
738 assert(!(msg.msg_flags & MSG_CTRUNC));
739 assert(!(msg.msg_flags & MSG_TRUNC));
741 p->size = (size_t) l;
744 *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
746 if (ret_src_address) {
748 avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
749 *ret_src_address = a.data.ipv6;
752 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
754 if (cmsg->cmsg_level == IPPROTO_IPV6) {
756 switch (cmsg->cmsg_type) {
761 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
768 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
771 *ret_iface = i->ipi6_ifindex;
774 memcpy(ret_dst_address->address, i->ipi6_addr.s6_addr, 16);
781 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
794 avahi_dns_packet_free(p);
799 int avahi_open_unicast_socket_ipv4(void) {
800 struct sockaddr_in local;
803 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
804 avahi_log_warn("socket() failed: %s", strerror(errno));
808 memset(&local, 0, sizeof(local));
809 local.sin_family = AF_INET;
811 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
812 avahi_log_warn("bind() failed: %s", strerror(errno));
816 if (ipv4_pktinfo(fd) < 0) {
820 if (avahi_set_cloexec(fd) < 0) {
821 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
825 if (avahi_set_nonblock(fd) < 0) {
826 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
839 int avahi_open_unicast_socket_ipv6(void) {
840 struct sockaddr_in6 local;
843 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
844 avahi_log_warn("socket() failed: %s", strerror(errno));
848 memset(&local, 0, sizeof(local));
849 local.sin6_family = AF_INET6;
851 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
852 avahi_log_warn("bind() failed: %s", strerror(errno));
856 if (ipv6_pktinfo(fd) < 0)
859 if (avahi_set_cloexec(fd) < 0) {
860 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
864 if (avahi_set_nonblock(fd) < 0) {
865 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));