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>
45 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
48 memset(ret_sa, 0, sizeof(struct sockaddr_in));
49 ret_sa->sin_family = AF_INET;
50 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
51 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
54 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
57 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
58 ret_sa->sin6_family = AF_INET6;
59 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
60 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
63 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
68 memset(ret_sa, 0, sizeof(struct sockaddr_in));
69 ret_sa->sin_family = AF_INET;
70 ret_sa->sin_port = htons(port);
71 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
74 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
79 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
80 ret_sa->sin6_family = AF_INET6;
81 ret_sa->sin6_port = htons(port);
82 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
85 int avahi_mdns_mcast_join_ipv4(int fd, int idx) {
87 struct sockaddr_in sa;
89 mdns_mcast_group_ipv4 (&sa);
91 memset(&mreq, 0, sizeof(mreq));
92 mreq.imr_multiaddr = sa.sin_addr;
93 mreq.imr_ifindex = idx;
95 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
96 avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
103 int avahi_mdns_mcast_join_ipv6(int fd, int idx) {
104 struct ipv6_mreq mreq6;
105 struct sockaddr_in6 sa6;
107 mdns_mcast_group_ipv6 (&sa6);
109 memset(&mreq6, 0, sizeof(mreq6));
110 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
111 mreq6.ipv6mr_interface = idx;
113 if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
114 avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
121 int avahi_mdns_mcast_leave_ipv4(int fd, int idx) {
122 struct ip_mreqn mreq;
123 struct sockaddr_in sa;
125 mdns_mcast_group_ipv4 (&sa);
127 memset(&mreq, 0, sizeof(mreq));
128 mreq.imr_multiaddr = sa.sin_addr;
129 mreq.imr_ifindex = idx;
131 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
132 avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
139 int avahi_mdns_mcast_leave_ipv6(int fd, int idx) {
140 struct ipv6_mreq mreq6;
141 struct sockaddr_in6 sa6;
143 mdns_mcast_group_ipv6 (&sa6);
145 memset(&mreq6, 0, sizeof(mreq6));
146 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
147 mreq6.ipv6mr_interface = idx;
149 if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
150 avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
157 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
164 if (bind(fd, sa, l) < 0) {
166 if (errno != EADDRINUSE) {
167 avahi_log_warn("bind() failed: %s\n", strerror(errno));
171 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
172 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
174 /* Try again, this time with SO_REUSEADDR set */
176 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
177 avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
181 if (bind(fd, sa, l) < 0) {
182 avahi_log_warn("bind() failed: %s\n", strerror(errno));
187 /* We enable SO_REUSEADDR afterwards, to make sure that the
188 * user may run other mDNS implementations if he really
192 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
193 avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
201 int avahi_open_socket_ipv4(void) {
202 struct sockaddr_in local;
203 int fd = -1, ttl, yes;
205 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
206 avahi_log_warn("socket() failed: %s\n", strerror(errno));
211 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
212 avahi_log_warn("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
217 if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
218 avahi_log_warn("IP_TTL failed: %s\n", strerror(errno));
223 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
224 avahi_log_warn("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
228 memset(&local, 0, sizeof(local));
229 local.sin_family = AF_INET;
230 local.sin_port = htons(AVAHI_MDNS_PORT);
232 if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0)
236 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
237 avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
242 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
243 avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
247 if (avahi_set_cloexec(fd) < 0) {
248 avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
252 if (avahi_set_nonblock(fd) < 0) {
253 avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
266 int avahi_open_socket_ipv6(void) {
267 struct sockaddr_in6 sa, local;
268 int fd = -1, ttl, yes;
270 mdns_mcast_group_ipv6(&sa);
272 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
273 avahi_log_warn("socket() failed: %s\n", strerror(errno));
278 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
279 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
284 if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
285 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
290 if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
291 avahi_log_warn("IPV6_V6ONLY failed: %s\n", strerror(errno));
296 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
297 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
301 memset(&local, 0, sizeof(local));
302 local.sin6_family = AF_INET6;
303 local.sin6_port = htons(AVAHI_MDNS_PORT);
305 if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0)
309 if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
310 avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
315 if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
316 avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
320 if (avahi_set_cloexec(fd) < 0) {
321 avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
325 if (avahi_set_nonblock(fd) < 0) {
326 avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
339 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
345 if (sendmsg(fd, msg, flags) >= 0)
348 if (errno != EAGAIN) {
349 avahi_log_debug("sendmsg() failed: %s\n", strerror(errno));
353 if (avahi_wait_for_write(fd) < 0)
360 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
361 struct sockaddr_in sa;
364 struct cmsghdr *cmsg;
365 struct in_pktinfo *pkti;
366 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
370 assert(avahi_dns_packet_is_valid(p) >= 0);
371 assert(!a || port > 0);
374 mdns_mcast_group_ipv4(&sa);
376 ipv4_address_to_sockaddr(&sa, a, port);
378 memset(&io, 0, sizeof(io));
379 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
380 io.iov_len = p->size;
382 memset(cmsg_data, 0, sizeof(cmsg_data));
383 cmsg = (struct cmsghdr*) cmsg_data;
384 cmsg->cmsg_len = sizeof(cmsg_data);
385 cmsg->cmsg_level = IPPROTO_IP;
386 cmsg->cmsg_type = IP_PKTINFO;
388 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
389 pkti->ipi_ifindex = interface;
391 memset(&msg, 0, sizeof(msg));
393 msg.msg_namelen = sizeof(sa);
396 msg.msg_control = cmsg_data;
397 msg.msg_controllen = sizeof(cmsg_data);
400 return sendmsg_loop(fd, &msg, 0 /*MSG_DONTROUTE*/);
403 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
404 struct sockaddr_in6 sa;
407 struct cmsghdr *cmsg;
408 struct in6_pktinfo *pkti;
409 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
413 assert(avahi_dns_packet_is_valid(p) >= 0);
416 mdns_mcast_group_ipv6(&sa);
418 ipv6_address_to_sockaddr(&sa, a, port);
420 memset(&io, 0, sizeof(io));
421 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
422 io.iov_len = p->size;
424 memset(cmsg_data, 0, sizeof(cmsg_data));
425 cmsg = (struct cmsghdr*) cmsg_data;
426 cmsg->cmsg_len = sizeof(cmsg_data);
427 cmsg->cmsg_level = IPPROTO_IPV6;
428 cmsg->cmsg_type = IPV6_PKTINFO;
430 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
431 pkti->ipi6_ifindex = interface;
433 memset(&msg, 0, sizeof(msg));
435 msg.msg_namelen = sizeof(sa);
438 msg.msg_control = cmsg_data;
439 msg.msg_controllen = sizeof(cmsg_data);
442 return sendmsg_loop(fd, &msg, 0 /*MSG_DONTROUTE*/);
445 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
446 AvahiDnsPacket *p= NULL;
451 struct cmsghdr *cmsg;
452 int found_ttl = 0, found_iface = 0;
457 assert(ret_dest_address);
461 if (ioctl(fd, FIONREAD, &ms) < 0) {
462 avahi_log_warn("ioctl(): %s", strerror(errno));
466 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
468 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
469 io.iov_len = p->max_size;
471 memset(&msg, 0, sizeof(msg));
472 msg.msg_name = ret_sa;
473 msg.msg_namelen = sizeof(struct sockaddr_in);
476 msg.msg_control = aux;
477 msg.msg_controllen = sizeof(aux);
480 if ((l = recvmsg(fd, &msg, 0)) < 0) {
481 avahi_log_warn("recvmsg(): %s", strerror(errno));
485 if (ret_sa->sin_addr.s_addr == INADDR_ANY) {
486 /* Linux 2.4 behaves very strangely sometimes! */
488 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
492 assert(!(msg.msg_flags & MSG_CTRUNC));
493 assert(!(msg.msg_flags & MSG_TRUNC));
494 p->size = (size_t) l;
498 /* avahi_hexdump(msg.msg_control, msg.msg_controllen); */
500 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
502 /* avahi_hexdump(CMSG_DATA(cmsg), cmsg->cmsg_len - sizeof(struct cmsghdr)); */
504 if (cmsg->cmsg_level == SOL_IP) {
506 if (cmsg->cmsg_type == IP_TTL) {
507 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
509 } else if (cmsg->cmsg_type == IP_PKTINFO) {
510 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
511 *ret_iface = (int) i->ipi_ifindex;
512 ret_dest_address->address = i->ipi_addr.s_addr;
518 /* avahi_log_debug("ttl=%u iface=%i", *ret_ttl, *ret_iface); */
527 avahi_dns_packet_free(p);
532 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
533 AvahiDnsPacket *p = NULL;
540 struct cmsghdr *cmsg;
541 int found_ttl = 0, found_iface = 0;
545 assert(ret_dest_address);
549 if (ioctl(fd, FIONREAD, &ms) < 0) {
550 avahi_log_warn("ioctl(): %s", strerror(errno));
554 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
556 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
557 io.iov_len = p->max_size;
559 memset(&msg, 0, sizeof(msg));
560 msg.msg_name = ret_sa;
561 msg.msg_namelen = sizeof(struct sockaddr_in6);
564 msg.msg_control = aux;
565 msg.msg_controllen = sizeof(aux);
568 if ((l = recvmsg(fd, &msg, 0)) < 0) {
569 avahi_log_warn("recvmsg(): %s", strerror(errno));
573 p->size = (size_t) l;
577 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
578 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
579 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
583 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
584 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
585 *ret_iface = i->ipi6_ifindex;
586 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
598 avahi_dns_packet_free(p);
603 int avahi_open_legacy_unicast_socket_ipv4(void) {
604 struct sockaddr_in local;
607 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
608 avahi_log_warn("socket() failed: %s\n", strerror(errno));
612 memset(&local, 0, sizeof(local));
613 local.sin_family = AF_INET;
615 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
616 avahi_log_warn("bind() failed: %s\n", strerror(errno));
621 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
622 avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
627 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
628 avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
632 if (avahi_set_cloexec(fd) < 0) {
633 avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
637 if (avahi_set_nonblock(fd) < 0) {
638 avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
651 int avahi_open_legacy_unicast_socket_ipv6(void) {
652 struct sockaddr_in local;
655 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
656 avahi_log_warn("socket() failed: %s\n", strerror(errno));
660 memset(&local, 0, sizeof(local));
661 local.sin_family = AF_INET;
663 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
664 avahi_log_warn("bind() failed: %s\n", strerror(errno));
669 if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
670 avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
675 if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
676 avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
680 if (avahi_set_cloexec(fd) < 0) {
681 avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
685 if (avahi_set_nonblock(fd) < 0) {
686 avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));