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 int avahi_mdns_mcast_join_ipv4 (int index, int fd)
67 struct sockaddr_in sa;
69 mdns_mcast_group_ipv4 (&sa);
71 memset(&mreq, 0, sizeof(mreq));
72 mreq.imr_multiaddr = sa.sin_addr;
73 mreq.imr_ifindex = index;
75 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
76 g_warning("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
83 int avahi_mdns_mcast_join_ipv6 (int index, int fd)
85 struct ipv6_mreq mreq6;
86 struct sockaddr_in6 sa6;
88 mdns_mcast_group_ipv6 (&sa6);
90 memset(&mreq6, 0, sizeof(mreq6));
91 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
92 mreq6.ipv6mr_interface = index;
94 if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
95 g_warning("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
102 int avahi_mdns_mcast_leave_ipv4 (int index, int fd)
104 struct ip_mreqn mreq;
105 struct sockaddr_in sa;
107 mdns_mcast_group_ipv4 (&sa);
109 memset(&mreq, 0, sizeof(mreq));
110 mreq.imr_multiaddr = sa.sin_addr;
111 mreq.imr_ifindex = index;
113 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
114 g_warning("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
121 int avahi_mdns_mcast_leave_ipv6 (int index, int fd)
123 struct ipv6_mreq mreq6;
124 struct sockaddr_in6 sa6;
126 mdns_mcast_group_ipv6 (&sa6);
128 memset(&mreq6, 0, sizeof(mreq6));
129 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
130 mreq6.ipv6mr_interface = index;
132 if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
133 g_warning("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
140 gint avahi_open_socket_ipv4(void) {
141 struct sockaddr_in local;
142 int fd = -1, ttl, yes;
144 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
145 g_warning("socket() failed: %s\n", strerror(errno));
150 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
151 g_warning("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
156 if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
157 g_warning("IP_TTL failed: %s\n", strerror(errno));
162 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
163 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
168 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
169 g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
174 memset(&local, 0, sizeof(local));
175 local.sin_family = AF_INET;
176 local.sin_port = htons(AVAHI_MDNS_PORT);
178 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
179 g_warning("bind() failed: %s\n", strerror(errno));
184 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
185 g_warning("IP_RECVTTL failed: %s\n", strerror(errno));
190 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
191 g_warning("IP_PKTINFO failed: %s\n", strerror(errno));
195 if (avahi_set_cloexec(fd) < 0) {
196 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
200 if (avahi_set_nonblock(fd) < 0) {
201 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
214 gint avahi_open_socket_ipv6(void) {
215 struct sockaddr_in6 sa, local;
216 int fd = -1, ttl, yes;
218 mdns_mcast_group_ipv6(&sa);
220 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
221 g_warning("socket() failed: %s\n", strerror(errno));
226 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
227 g_warning("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
232 if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
233 g_warning("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
238 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
239 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
244 if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
245 g_warning("IPV6_V6ONLY failed: %s\n", strerror(errno));
250 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
251 g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
255 memset(&local, 0, sizeof(local));
256 local.sin6_family = AF_INET6;
257 local.sin6_port = htons(AVAHI_MDNS_PORT);
259 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
260 g_warning("bind() failed: %s\n", strerror(errno));
265 if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
266 g_warning("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
271 if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
272 g_warning("IPV6_PKTINFO failed: %s\n", strerror(errno));
276 if (avahi_set_cloexec(fd) < 0) {
277 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
281 if (avahi_set_nonblock(fd) < 0) {
282 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
295 static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) {
301 if (sendmsg(fd, msg, flags) >= 0)
304 if (errno != EAGAIN) {
305 g_message("sendmsg() failed: %s\n", strerror(errno));
309 if (avahi_wait_for_write(fd) < 0)
316 gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p) {
317 struct sockaddr_in sa;
320 struct cmsghdr *cmsg;
321 struct in_pktinfo *pkti;
322 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
326 g_assert(avahi_dns_packet_check_valid(p) >= 0);
328 mdns_mcast_group_ipv4(&sa);
330 memset(&io, 0, sizeof(io));
331 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
332 io.iov_len = p->size;
334 memset(cmsg_data, 0, sizeof(cmsg_data));
335 cmsg = (struct cmsghdr*) cmsg_data;
336 cmsg->cmsg_len = sizeof(cmsg_data);
337 cmsg->cmsg_level = IPPROTO_IP;
338 cmsg->cmsg_type = IP_PKTINFO;
340 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
341 pkti->ipi_ifindex = interface;
343 memset(&msg, 0, sizeof(msg));
345 msg.msg_namelen = sizeof(sa);
348 msg.msg_control = cmsg_data;
349 msg.msg_controllen = sizeof(cmsg_data);
352 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
355 gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p) {
356 struct sockaddr_in6 sa;
359 struct cmsghdr *cmsg;
360 struct in6_pktinfo *pkti;
361 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
365 g_assert(avahi_dns_packet_check_valid(p) >= 0);
367 mdns_mcast_group_ipv6(&sa);
369 memset(&io, 0, sizeof(io));
370 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
371 io.iov_len = p->size;
373 memset(cmsg_data, 0, sizeof(cmsg_data));
374 cmsg = (struct cmsghdr*) cmsg_data;
375 cmsg->cmsg_len = sizeof(cmsg_data);
376 cmsg->cmsg_level = IPPROTO_IPV6;
377 cmsg->cmsg_type = IPV6_PKTINFO;
379 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
380 pkti->ipi6_ifindex = interface;
382 memset(&msg, 0, sizeof(msg));
384 msg.msg_namelen = sizeof(sa);
387 msg.msg_control = cmsg_data;
388 msg.msg_controllen = sizeof(cmsg_data);
391 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
394 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint *ret_iface, guint8* ret_ttl) {
395 AvahiDnsPacket *p= NULL;
400 struct cmsghdr *cmsg;
401 gboolean found_ttl = FALSE, found_iface = FALSE;
408 p = avahi_dns_packet_new(0);
410 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
411 io.iov_len = p->max_size;
413 memset(&msg, 0, sizeof(msg));
414 msg.msg_name = ret_sa;
415 msg.msg_namelen = sizeof(struct sockaddr_in);
418 msg.msg_control = aux;
419 msg.msg_controllen = sizeof(aux);
422 if ((l = recvmsg(fd, &msg, 0)) < 0)
425 p->size = (size_t) l;
429 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg,cmsg)) {
430 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_TTL) {
431 *ret_ttl = *(uint8_t *) CMSG_DATA(cmsg);
435 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
436 *ret_iface = ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
441 g_assert(found_iface);
448 avahi_dns_packet_free(p);
453 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
454 AvahiDnsPacket *p = NULL;
459 struct cmsghdr *cmsg;
460 gboolean found_ttl = FALSE, found_iface = FALSE;
467 p = avahi_dns_packet_new(0);
469 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
470 io.iov_len = p->max_size;
472 memset(&msg, 0, sizeof(msg));
473 msg.msg_name = ret_sa;
474 msg.msg_namelen = sizeof(struct sockaddr_in6);
477 msg.msg_control = aux;
478 msg.msg_controllen = sizeof(aux);
481 if ((l = recvmsg(fd, &msg, 0)) < 0)
484 p->size = (size_t) l;
488 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
489 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
490 *ret_ttl = *(uint8_t *) CMSG_DATA(cmsg);
494 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
495 *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
500 g_assert(found_iface);
507 avahi_dns_packet_free(p);