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>
43 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
46 memset(ret_sa, 0, sizeof(struct sockaddr_in));
48 ret_sa->sin_family = AF_INET;
49 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
50 inet_pton(AF_INET, "224.0.0.251", &ret_sa->sin_addr);
53 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
57 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
59 ret_sa->sin6_family = AF_INET6;
60 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
61 inet_pton(AF_INET6, "ff02::fb", &ret_sa->sin6_addr);
64 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, guint16 port) {
69 memset(ret_sa, 0, sizeof(struct sockaddr_in));
70 ret_sa->sin_family = AF_INET;
71 ret_sa->sin_port = htons(port);
72 memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
75 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, guint16 port) {
80 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
81 ret_sa->sin6_family = AF_INET6;
82 ret_sa->sin6_port = htons(port);
83 memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
86 int avahi_mdns_mcast_join_ipv4 (int index, int fd) {
88 struct sockaddr_in sa;
90 mdns_mcast_group_ipv4 (&sa);
92 memset(&mreq, 0, sizeof(mreq));
93 mreq.imr_multiaddr = sa.sin_addr;
94 mreq.imr_ifindex = index;
96 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
97 g_warning("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
104 int avahi_mdns_mcast_join_ipv6 (int index, int fd) {
105 struct ipv6_mreq mreq6;
106 struct sockaddr_in6 sa6;
108 mdns_mcast_group_ipv6 (&sa6);
110 memset(&mreq6, 0, sizeof(mreq6));
111 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
112 mreq6.ipv6mr_interface = index;
114 if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
115 g_warning("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
122 int avahi_mdns_mcast_leave_ipv4 (int index, int fd) {
123 struct ip_mreqn mreq;
124 struct sockaddr_in sa;
126 mdns_mcast_group_ipv4 (&sa);
128 memset(&mreq, 0, sizeof(mreq));
129 mreq.imr_multiaddr = sa.sin_addr;
130 mreq.imr_ifindex = index;
132 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
133 g_warning("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
140 int avahi_mdns_mcast_leave_ipv6 (int index, int fd) {
141 struct ipv6_mreq mreq6;
142 struct sockaddr_in6 sa6;
144 mdns_mcast_group_ipv6 (&sa6);
146 memset(&mreq6, 0, sizeof(mreq6));
147 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
148 mreq6.ipv6mr_interface = index;
150 if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
151 g_warning("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
158 gint avahi_open_socket_ipv4(void) {
159 struct sockaddr_in local;
160 int fd = -1, ttl, yes;
162 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
163 g_warning("socket() failed: %s\n", strerror(errno));
168 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
169 g_warning("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
174 if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
175 g_warning("IP_TTL failed: %s\n", strerror(errno));
180 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
181 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
186 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
187 g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
192 memset(&local, 0, sizeof(local));
193 local.sin_family = AF_INET;
194 local.sin_port = htons(AVAHI_MDNS_PORT);
196 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
197 g_warning("bind() failed: %s\n", strerror(errno));
202 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
203 g_warning("IP_RECVTTL failed: %s\n", strerror(errno));
208 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
209 g_warning("IP_PKTINFO failed: %s\n", strerror(errno));
213 if (avahi_set_cloexec(fd) < 0) {
214 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
218 if (avahi_set_nonblock(fd) < 0) {
219 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
232 gint avahi_open_socket_ipv6(void) {
233 struct sockaddr_in6 sa, local;
234 int fd = -1, ttl, yes;
236 mdns_mcast_group_ipv6(&sa);
238 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
239 g_warning("socket() failed: %s\n", strerror(errno));
244 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
245 g_warning("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
250 if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
251 g_warning("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
256 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
257 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
262 if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
263 g_warning("IPV6_V6ONLY failed: %s\n", strerror(errno));
268 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
269 g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
273 memset(&local, 0, sizeof(local));
274 local.sin6_family = AF_INET6;
275 local.sin6_port = htons(AVAHI_MDNS_PORT);
277 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
278 g_warning("bind() failed: %s\n", strerror(errno));
283 if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
284 g_warning("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
289 if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
290 g_warning("IPV6_PKTINFO failed: %s\n", strerror(errno));
294 if (avahi_set_cloexec(fd) < 0) {
295 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
299 if (avahi_set_nonblock(fd) < 0) {
300 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
313 static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) {
319 if (sendmsg(fd, msg, flags) >= 0)
322 if (errno != EAGAIN) {
323 g_message("sendmsg() failed: %s\n", strerror(errno));
327 if (avahi_wait_for_write(fd) < 0)
334 gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, guint16 port) {
335 struct sockaddr_in sa;
338 struct cmsghdr *cmsg;
339 struct in_pktinfo *pkti;
340 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
344 g_assert(avahi_dns_packet_check_valid(p) >= 0);
345 g_assert(!a || port > 0);
348 mdns_mcast_group_ipv4(&sa);
350 ipv4_address_to_sockaddr(&sa, a, port);
352 memset(&io, 0, sizeof(io));
353 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
354 io.iov_len = p->size;
356 memset(cmsg_data, 0, sizeof(cmsg_data));
357 cmsg = (struct cmsghdr*) cmsg_data;
358 cmsg->cmsg_len = sizeof(cmsg_data);
359 cmsg->cmsg_level = IPPROTO_IP;
360 cmsg->cmsg_type = IP_PKTINFO;
362 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
363 pkti->ipi_ifindex = interface;
365 memset(&msg, 0, sizeof(msg));
367 msg.msg_namelen = sizeof(sa);
370 msg.msg_control = cmsg_data;
371 msg.msg_controllen = sizeof(cmsg_data);
374 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
377 gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, guint16 port) {
378 struct sockaddr_in6 sa;
381 struct cmsghdr *cmsg;
382 struct in6_pktinfo *pkti;
383 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
387 g_assert(avahi_dns_packet_check_valid(p) >= 0);
390 mdns_mcast_group_ipv6(&sa);
392 ipv6_address_to_sockaddr(&sa, a, port);
394 memset(&io, 0, sizeof(io));
395 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
396 io.iov_len = p->size;
398 memset(cmsg_data, 0, sizeof(cmsg_data));
399 cmsg = (struct cmsghdr*) cmsg_data;
400 cmsg->cmsg_len = sizeof(cmsg_data);
401 cmsg->cmsg_level = IPPROTO_IPV6;
402 cmsg->cmsg_type = IPV6_PKTINFO;
404 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
405 pkti->ipi6_ifindex = interface;
407 memset(&msg, 0, sizeof(msg));
409 msg.msg_namelen = sizeof(sa);
412 msg.msg_control = cmsg_data;
413 msg.msg_controllen = sizeof(cmsg_data);
416 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
419 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint *ret_iface, guint8* ret_ttl) {
420 AvahiDnsPacket *p= NULL;
425 struct cmsghdr *cmsg;
426 gboolean found_ttl = FALSE, found_iface = FALSE;
433 p = avahi_dns_packet_new(0);
435 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
436 io.iov_len = p->max_size;
438 memset(&msg, 0, sizeof(msg));
439 msg.msg_name = ret_sa;
440 msg.msg_namelen = sizeof(struct sockaddr_in);
443 msg.msg_control = aux;
444 msg.msg_controllen = sizeof(aux);
447 if ((l = recvmsg(fd, &msg, 0)) < 0) {
448 g_warning("recvmsg(): %s\n", strerror(errno));
452 if (ret_sa->sin_addr.s_addr == INADDR_ANY) {
453 /* Linux 2.4 behaves very strangely sometimes! */
455 avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l);
461 g_assert(!(msg.msg_flags & MSG_CTRUNC));
462 g_assert(!(msg.msg_flags & MSG_TRUNC));
463 p->size = (size_t) l;
467 /* avahi_hexdump(msg.msg_control, msg.msg_controllen); */
469 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
471 /* avahi_hexdump(CMSG_DATA(cmsg), cmsg->cmsg_len - sizeof(struct cmsghdr)); */
473 if (cmsg->cmsg_level == SOL_IP) {
475 if (cmsg->cmsg_type == IP_TTL) {
476 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
478 } else if (cmsg->cmsg_type == IP_PKTINFO) {
479 *ret_iface = (gint) ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
485 /* g_message("ttl=%u iface=%i", *ret_ttl, *ret_iface); */
487 g_assert(found_iface);
494 avahi_dns_packet_free(p);
499 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
500 AvahiDnsPacket *p = NULL;
505 struct cmsghdr *cmsg;
506 gboolean found_ttl = FALSE, found_iface = FALSE;
513 p = avahi_dns_packet_new(0);
515 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
516 io.iov_len = p->max_size;
518 memset(&msg, 0, sizeof(msg));
519 msg.msg_name = ret_sa;
520 msg.msg_namelen = sizeof(struct sockaddr_in6);
523 msg.msg_control = aux;
524 msg.msg_controllen = sizeof(aux);
527 if ((l = recvmsg(fd, &msg, 0)) < 0)
530 p->size = (size_t) l;
534 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
535 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
536 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
540 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
541 *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
546 g_assert(found_iface);
553 avahi_dns_packet_free(p);