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 #ifdef HAVE_STRUCT_IP_MREQN
106 int avahi_mdns_mcast_join_ipv4(int fd, int idx, int join) {
107 struct ip_mreqn mreq;
109 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiAddress *a, int join) {
113 struct sockaddr_in sa;
114 memset(&mreq, 0, sizeof(mreq));
116 #ifdef HAVE_STRUCT_IP_MREQN
117 mreq.imr_ifindex = idx;
120 assert(a->proto == AVAHI_PROTO_INET);
121 mreq.imr_interface.s_addr = a->data.ipv4.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, int idx, int join) {
136 struct ipv6_mreq mreq6;
137 struct sockaddr_in6 sa6;
139 mdns_mcast_group_ipv6 (&sa6);
141 memset(&mreq6, 0, sizeof(mreq6));
142 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
143 mreq6.ipv6mr_interface = idx;
145 if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
146 avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
153 static int reuseaddr(int fd) {
157 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
158 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
164 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
165 avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
173 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
179 if (bind(fd, sa, l) < 0) {
181 if (errno != EADDRINUSE) {
182 avahi_log_warn("bind() failed: %s", strerror(errno));
186 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
187 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
189 /* Try again, this time with SO_REUSEADDR set */
190 if (reuseaddr(fd) < 0)
193 if (bind(fd, sa, l) < 0) {
194 avahi_log_warn("bind() failed: %s", strerror(errno));
199 /* We enable SO_REUSEADDR afterwards, to make sure that the
200 * user may run other mDNS implementations if he really
203 if (reuseaddr(fd) < 0)
210 static int ipv4_pktinfo(int fd) {
215 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
216 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
221 #ifdef IP_RECVINTERFACE
223 if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
224 avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
227 #elif defined(IP_RECVIF)
229 if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
230 avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
235 #ifdef IP_RECVDSTADDR
237 if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
238 avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
243 #endif /* IP_PKTINFO */
247 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
248 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
256 static int ipv6_pktinfo(int fd) {
259 #ifdef IPV6_RECVPKTINFO
261 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
262 avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
265 #elif defined(IPV6_PKTINFO)
267 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
268 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
275 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
276 avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
279 #elif defined(IPV6_RECVHOPLIMIT)
281 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
282 avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
285 #elif defined(IPV6_HOPLIMIT)
287 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
288 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
296 int avahi_open_socket_ipv4(int no_reuse) {
297 struct sockaddr_in local;
298 int fd = -1, r, ittl;
301 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
302 avahi_log_warn("socket() failed: %s", strerror(errno));
307 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
308 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
313 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
314 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
319 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
320 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
324 memset(&local, 0, sizeof(local));
325 local.sin_family = AF_INET;
326 local.sin_port = htons(AVAHI_MDNS_PORT);
329 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
331 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
336 if (ipv4_pktinfo (fd) < 0)
339 if (avahi_set_cloexec(fd) < 0) {
340 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
344 if (avahi_set_nonblock(fd) < 0) {
345 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
358 int avahi_open_socket_ipv6(int no_reuse) {
359 struct sockaddr_in6 sa, local;
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));
406 if (ipv6_pktinfo(fd) < 0)
409 if (avahi_set_cloexec(fd) < 0) {
410 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
414 if (avahi_set_nonblock(fd) < 0) {
415 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
428 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
434 if (sendmsg(fd, msg, flags) >= 0)
437 if (errno != EAGAIN) {
438 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
442 if (avahi_wait_for_write(fd) < 0)
449 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
450 struct sockaddr_in sa;
454 struct cmsghdr *cmsg;
455 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
460 assert(avahi_dns_packet_check_valid(p) >= 0);
461 assert(!a || port > 0);
464 mdns_mcast_group_ipv4(&sa);
466 ipv4_address_to_sockaddr(&sa, a, port);
468 memset(&io, 0, sizeof(io));
469 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
470 io.iov_len = p->size;
472 memset(&msg, 0, sizeof(msg));
474 msg.msg_namelen = sizeof(sa);
478 msg.msg_control = NULL;
479 msg.msg_controllen = 0;
482 if (interface >= 0) {
483 struct in_pktinfo *pkti;
485 memset(cmsg_data, 0, sizeof(cmsg_data));
486 cmsg = (struct cmsghdr*) cmsg_data;
487 cmsg->cmsg_len = sizeof(cmsg_data);
488 cmsg->cmsg_level = IPPROTO_IP;
489 cmsg->cmsg_type = IP_PKTINFO;
491 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
492 pkti->ipi_ifindex = interface;
494 msg.msg_control = cmsg_data;
495 msg.msg_controllen = sizeof(cmsg_data);
499 #warning "FIXME: We need some code to set the outgoing interface here if IP_PKTINFO is not available"
503 return sendmsg_loop(fd, &msg, 0);
506 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
507 struct sockaddr_in6 sa;
510 struct cmsghdr *cmsg;
511 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
515 assert(avahi_dns_packet_check_valid(p) >= 0);
518 mdns_mcast_group_ipv6(&sa);
520 ipv6_address_to_sockaddr(&sa, a, port);
522 memset(&io, 0, sizeof(io));
523 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
524 io.iov_len = p->size;
527 memset(&msg, 0, sizeof(msg));
529 msg.msg_namelen = sizeof(sa);
534 if (interface >= 0) {
535 struct in6_pktinfo *pkti;
537 memset(cmsg_data, 0, sizeof(cmsg_data));
538 cmsg = (struct cmsghdr*) cmsg_data;
539 cmsg->cmsg_len = sizeof(cmsg_data);
540 cmsg->cmsg_level = IPPROTO_IPV6;
541 cmsg->cmsg_type = IPV6_PKTINFO;
543 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
544 pkti->ipi6_ifindex = interface;
546 msg.msg_control = cmsg_data;
547 msg.msg_controllen = sizeof(cmsg_data);
549 msg.msg_control = NULL;
550 msg.msg_controllen = 0;
553 return sendmsg_loop(fd, &msg, 0);
556 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
557 AvahiDnsPacket *p= NULL;
562 struct cmsghdr *cmsg;
563 int found_iface = 0, found_addr = 0;
568 if (ioctl(fd, FIONREAD, &ms) < 0) {
569 avahi_log_warn("ioctl(): %s", strerror(errno));
573 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
575 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
576 io.iov_len = p->max_size;
578 memset(&msg, 0, sizeof(msg));
580 msg.msg_name = ret_sa;
581 msg.msg_namelen = sizeof(struct sockaddr_in);
588 msg.msg_control = aux;
589 msg.msg_controllen = sizeof(aux);
592 if ((l = recvmsg(fd, &msg, 0)) < 0) {
593 avahi_log_warn("recvmsg(): %s", strerror(errno));
597 if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
598 /* Linux 2.4 behaves very strangely sometimes! */
600 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
604 assert(!(msg.msg_flags & MSG_CTRUNC));
605 assert(!(msg.msg_flags & MSG_TRUNC));
606 p->size = (size_t) l;
611 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
613 if (cmsg->cmsg_level == IPPROTO_IP) {
614 switch (cmsg->cmsg_type) {
620 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
626 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
629 *ret_iface = (int) i->ipi_ifindex;
631 if (ret_dest_address)
632 ret_dest_address->address = i->ipi_addr.s_addr;
634 found_addr = found_iface = 1;
638 #elif defined(IP_RECVIF)
640 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
643 *ret_iface = (int) sdl->sdl_index;
650 #ifdef IP_RECVDSTADDR
652 if (ret_dest_address)
653 memcpy(&ret_dest_address->address, CMSG_DATA (cmsg), 4);
660 avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
673 avahi_dns_packet_free(p);
678 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
679 AvahiDnsPacket *p = NULL;
685 struct cmsghdr *cmsg;
686 int found_ttl = 0, found_iface = 0;
690 if (ioctl(fd, FIONREAD, &ms) < 0) {
691 avahi_log_warn("ioctl(): %s", strerror(errno));
695 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
697 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
698 io.iov_len = p->max_size;
700 memset(&msg, 0, sizeof(msg));
702 msg.msg_name = ret_sa;
703 msg.msg_namelen = sizeof(struct sockaddr_in6);
711 msg.msg_control = aux;
712 msg.msg_controllen = sizeof(aux);
715 if ((l = recvmsg(fd, &msg, 0)) < 0) {
716 avahi_log_warn("recvmsg(): %s", strerror(errno));
720 p->size = (size_t) l;
725 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
726 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
729 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
734 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
735 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
738 *ret_iface = i->ipi6_ifindex;
740 if (ret_dest_address)
741 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
754 avahi_dns_packet_free(p);
759 int avahi_open_unicast_socket_ipv4(void) {
760 struct sockaddr_in local;
763 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
764 avahi_log_warn("socket() failed: %s", strerror(errno));
768 memset(&local, 0, sizeof(local));
769 local.sin_family = AF_INET;
771 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
772 avahi_log_warn("bind() failed: %s", strerror(errno));
776 if (ipv4_pktinfo(fd) < 0) {
780 if (avahi_set_cloexec(fd) < 0) {
781 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
785 if (avahi_set_nonblock(fd) < 0) {
786 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
799 int avahi_open_unicast_socket_ipv6(void) {
800 struct sockaddr_in6 local;
803 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
804 avahi_log_warn("socket() failed: %s", strerror(errno));
808 memset(&local, 0, sizeof(local));
809 local.sin6_family = AF_INET6;
811 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
812 avahi_log_warn("bind() failed: %s", strerror(errno));
816 if (ipv6_pktinfo(fd) < 0)
819 if (avahi_set_cloexec(fd) < 0) {
820 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
824 if (avahi_set_nonblock(fd) < 0) {
825 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));