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 /* this is a portability hack */
46 #ifndef IPV6_ADD_MEMBERSHIP
47 #ifdef IPV6_JOIN_GROUP
48 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
52 #ifndef IPV6_DROP_MEMBERSHIP
53 #ifdef IPV6_LEAVE_GROUP
54 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
58 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
61 memset(ret_sa, 0, sizeof(struct sockaddr_in));
62 ret_sa->sin_family = AF_INET;
63 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
64 inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
67 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
70 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
71 ret_sa->sin6_family = AF_INET6;
72 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
73 inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
76 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
81 memset(ret_sa, 0, sizeof(struct sockaddr_in));
82 ret_sa->sin_family = AF_INET;
83 ret_sa->sin_port = htons(port);
84 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
87 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
92 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
93 ret_sa->sin6_family = AF_INET6;
94 ret_sa->sin6_port = htons(port);
95 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
98 int avahi_mdns_mcast_join_ipv4(int fd, int idx) {
100 struct sockaddr_in sa;
102 mdns_mcast_group_ipv4 (&sa);
104 memset(&mreq, 0, sizeof(mreq));
105 mreq.imr_multiaddr = sa.sin_addr;
106 mreq.imr_ifindex = idx;
108 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
109 avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
116 int avahi_mdns_mcast_join_ipv6(int fd, int idx) {
117 struct ipv6_mreq mreq6;
118 struct sockaddr_in6 sa6;
120 mdns_mcast_group_ipv6 (&sa6);
122 memset(&mreq6, 0, sizeof(mreq6));
123 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
124 mreq6.ipv6mr_interface = idx;
126 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
127 avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s", strerror(errno));
134 int avahi_mdns_mcast_leave_ipv4(int fd, int idx) {
135 struct ip_mreqn mreq;
136 struct sockaddr_in sa;
138 mdns_mcast_group_ipv4 (&sa);
140 memset(&mreq, 0, sizeof(mreq));
141 mreq.imr_multiaddr = sa.sin_addr;
142 mreq.imr_ifindex = idx;
144 if (setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
145 avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s", strerror(errno));
152 int avahi_mdns_mcast_leave_ipv6(int fd, int idx) {
153 struct ipv6_mreq mreq6;
154 struct sockaddr_in6 sa6;
156 mdns_mcast_group_ipv6 (&sa6);
158 memset(&mreq6, 0, sizeof(mreq6));
159 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
160 mreq6.ipv6mr_interface = idx;
162 if (setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
163 avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s", strerror(errno));
170 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
177 if (bind(fd, sa, l) < 0) {
179 if (errno != EADDRINUSE) {
180 avahi_log_warn("bind() failed: %s", strerror(errno));
184 avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
185 sa->sa_family == AF_INET ? "IPv4" : "IPv6");
187 /* Try again, this time with SO_REUSEADDR set */
189 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
190 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
194 if (bind(fd, sa, l) < 0) {
195 avahi_log_warn("bind() failed: %s", strerror(errno));
200 /* We enable SO_REUSEADDR afterwards, to make sure that the
201 * user may run other mDNS implementations if he really
205 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
206 avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
214 int avahi_open_socket_ipv4(int no_reuse) {
215 struct sockaddr_in local;
216 int fd = -1, ttl, yes, r;
218 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
219 avahi_log_warn("socket() failed: %s", strerror(errno));
224 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
225 avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
230 if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
231 avahi_log_warn("IP_TTL failed: %s", strerror(errno));
236 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
237 avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
241 memset(&local, 0, sizeof(local));
242 local.sin_family = AF_INET;
243 local.sin_port = htons(AVAHI_MDNS_PORT);
246 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
248 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
254 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
255 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
260 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
261 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
265 if (avahi_set_cloexec(fd) < 0) {
266 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
270 if (avahi_set_nonblock(fd) < 0) {
271 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
284 int avahi_open_socket_ipv6(int no_reuse) {
285 struct sockaddr_in6 sa, local;
286 int fd = -1, ttl, yes, r;
288 mdns_mcast_group_ipv6(&sa);
290 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
291 avahi_log_warn("socket() failed: %s", strerror(errno));
296 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
297 avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
302 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
303 avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
308 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
309 avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
314 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
315 avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
319 memset(&local, 0, sizeof(local));
320 local.sin6_family = AF_INET6;
321 local.sin6_port = htons(AVAHI_MDNS_PORT);
324 r = bind(fd, (struct sockaddr*) &local, sizeof(local));
326 r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
332 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
333 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
338 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
339 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
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 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
368 if (sendmsg(fd, msg, flags) >= 0)
371 if (errno != EAGAIN) {
372 avahi_log_debug("sendmsg() failed: %s", strerror(errno));
376 if (avahi_wait_for_write(fd) < 0)
383 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
384 struct sockaddr_in sa;
387 struct cmsghdr *cmsg;
388 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
392 assert(avahi_dns_packet_check_valid(p) >= 0);
393 assert(!a || port > 0);
396 mdns_mcast_group_ipv4(&sa);
398 ipv4_address_to_sockaddr(&sa, a, port);
400 memset(&io, 0, sizeof(io));
401 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
402 io.iov_len = p->size;
404 memset(&msg, 0, sizeof(msg));
406 msg.msg_namelen = sizeof(sa);
411 if (interface >= 0) {
412 struct in_pktinfo *pkti;
414 memset(cmsg_data, 0, sizeof(cmsg_data));
415 cmsg = (struct cmsghdr*) cmsg_data;
416 cmsg->cmsg_len = sizeof(cmsg_data);
417 cmsg->cmsg_level = IPPROTO_IP;
418 cmsg->cmsg_type = IP_PKTINFO;
420 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
421 pkti->ipi_ifindex = interface;
423 msg.msg_control = cmsg_data;
424 msg.msg_controllen = sizeof(cmsg_data);
426 msg.msg_control = NULL;
427 msg.msg_controllen = 0;
430 return sendmsg_loop(fd, &msg, 0);
433 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
434 struct sockaddr_in6 sa;
437 struct cmsghdr *cmsg;
438 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
442 assert(avahi_dns_packet_check_valid(p) >= 0);
445 mdns_mcast_group_ipv6(&sa);
447 ipv6_address_to_sockaddr(&sa, a, port);
449 memset(&io, 0, sizeof(io));
450 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
451 io.iov_len = p->size;
454 memset(&msg, 0, sizeof(msg));
456 msg.msg_namelen = sizeof(sa);
461 if (interface >= 0) {
462 struct in6_pktinfo *pkti;
464 memset(cmsg_data, 0, sizeof(cmsg_data));
465 cmsg = (struct cmsghdr*) cmsg_data;
466 cmsg->cmsg_len = sizeof(cmsg_data);
467 cmsg->cmsg_level = IPPROTO_IPV6;
468 cmsg->cmsg_type = IPV6_PKTINFO;
470 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
471 pkti->ipi6_ifindex = interface;
473 msg.msg_control = cmsg_data;
474 msg.msg_controllen = sizeof(cmsg_data);
476 msg.msg_control = NULL;
477 msg.msg_controllen = 0;
480 return sendmsg_loop(fd, &msg, 0);
483 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
484 AvahiDnsPacket *p= NULL;
489 struct cmsghdr *cmsg;
490 int found_ttl = 0, found_iface = 0;
495 if (ioctl(fd, FIONREAD, &ms) < 0) {
496 avahi_log_warn("ioctl(): %s", strerror(errno));
500 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
502 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
503 io.iov_len = p->max_size;
505 memset(&msg, 0, sizeof(msg));
507 msg.msg_name = ret_sa;
508 msg.msg_namelen = sizeof(struct sockaddr_in);
515 msg.msg_control = aux;
516 msg.msg_controllen = sizeof(aux);
519 if ((l = recvmsg(fd, &msg, 0)) < 0) {
520 avahi_log_warn("recvmsg(): %s", strerror(errno));
524 if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
525 /* Linux 2.4 behaves very strangely sometimes! */
527 /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
531 assert(!(msg.msg_flags & MSG_CTRUNC));
532 assert(!(msg.msg_flags & MSG_TRUNC));
533 p->size = (size_t) l;
538 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
540 if (cmsg->cmsg_level == IPPROTO_IP) {
542 if (cmsg->cmsg_type == IP_TTL) {
544 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
546 } else if (cmsg->cmsg_type == IP_PKTINFO) {
547 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
550 *ret_iface = (int) i->ipi_ifindex;
552 if (ret_dest_address)
553 ret_dest_address->address = i->ipi_addr.s_addr;
566 avahi_dns_packet_free(p);
571 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
572 AvahiDnsPacket *p = NULL;
579 struct cmsghdr *cmsg;
580 int found_ttl = 0, found_iface = 0;
584 if (ioctl(fd, FIONREAD, &ms) < 0) {
585 avahi_log_warn("ioctl(): %s", strerror(errno));
589 p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
591 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
592 io.iov_len = p->max_size;
594 memset(&msg, 0, sizeof(msg));
596 msg.msg_name = ret_sa;
597 msg.msg_namelen = sizeof(struct sockaddr_in6);
605 msg.msg_control = aux;
606 msg.msg_controllen = sizeof(aux);
609 if ((l = recvmsg(fd, &msg, 0)) < 0) {
610 avahi_log_warn("recvmsg(): %s", strerror(errno));
614 p->size = (size_t) l;
619 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
620 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
623 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
628 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
629 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
632 *ret_iface = i->ipi6_ifindex;
634 if (ret_dest_address)
635 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
647 avahi_dns_packet_free(p);
652 int avahi_open_unicast_socket_ipv4(void) {
653 struct sockaddr_in local;
656 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
657 avahi_log_warn("socket() failed: %s", strerror(errno));
661 memset(&local, 0, sizeof(local));
662 local.sin_family = AF_INET;
664 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
665 avahi_log_warn("bind() failed: %s", strerror(errno));
670 if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
671 avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
676 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
677 avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
681 if (avahi_set_cloexec(fd) < 0) {
682 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
686 if (avahi_set_nonblock(fd) < 0) {
687 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
700 int avahi_open_unicast_socket_ipv6(void) {
701 struct sockaddr_in6 local;
704 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
705 avahi_log_warn("socket() failed: %s", strerror(errno));
709 memset(&local, 0, sizeof(local));
710 local.sin6_family = AF_INET6;
712 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
713 avahi_log_warn("bind() failed: %s", strerror(errno));
718 if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
719 avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
724 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
725 avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
729 if (avahi_set_cloexec(fd) < 0) {
730 avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
734 if (avahi_set_nonblock(fd) < 0) {
735 avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));