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
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
39 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
42 memset(ret_sa, 0, sizeof(struct sockaddr_in));
44 ret_sa->sin_family = AF_INET;
45 ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
46 inet_pton(AF_INET, "224.0.0.251", &ret_sa->sin_addr);
49 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
53 memset(ret_sa, 0, sizeof(struct sockaddr_in6));
55 ret_sa->sin6_family = AF_INET6;
56 ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
57 inet_pton(AF_INET6, "ff02::fb", &ret_sa->sin6_addr);
60 int avahi_mdns_mcast_join_ipv4 (int index, int fd)
63 struct sockaddr_in sa;
65 mdns_mcast_group_ipv4 (&sa);
67 memset(&mreq, 0, sizeof(mreq));
68 mreq.imr_multiaddr = sa.sin_addr;
69 mreq.imr_ifindex = index;
71 if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
72 g_warning("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
79 int avahi_mdns_mcast_join_ipv6 (int index, int fd)
81 struct ipv6_mreq mreq6;
82 struct sockaddr_in6 sa6;
84 mdns_mcast_group_ipv6 (&sa6);
86 memset(&mreq6, 0, sizeof(mreq6));
87 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
88 mreq6.ipv6mr_interface = index;
90 if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
91 g_warning("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
98 int avahi_mdns_mcast_leave_ipv4 (int index, int fd)
100 struct ip_mreqn mreq;
101 struct sockaddr_in sa;
103 mdns_mcast_group_ipv4 (&sa);
105 memset(&mreq, 0, sizeof(mreq));
106 mreq.imr_multiaddr = sa.sin_addr;
107 mreq.imr_ifindex = index;
109 if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
110 g_warning("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
117 int avahi_mdns_mcast_leave_ipv6 (int index, int fd)
119 struct ipv6_mreq mreq6;
120 struct sockaddr_in6 sa6;
122 mdns_mcast_group_ipv6 (&sa6);
124 memset(&mreq6, 0, sizeof(mreq6));
125 mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
126 mreq6.ipv6mr_interface = index;
128 if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
129 g_warning("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
136 gint avahi_open_socket_ipv4(void) {
137 struct sockaddr_in local;
138 int fd = -1, ttl, yes;
140 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
141 g_warning("socket() failed: %s\n", strerror(errno));
146 if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
147 g_warning("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
152 if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
153 g_warning("IP_TTL failed: %s\n", strerror(errno));
158 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
159 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
164 if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
165 g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
170 memset(&local, 0, sizeof(local));
171 local.sin_family = AF_INET;
172 local.sin_port = htons(AVAHI_MDNS_PORT);
174 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
175 g_warning("bind() failed: %s\n", strerror(errno));
180 if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
181 g_warning("IP_RECVTTL failed: %s\n", strerror(errno));
186 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
187 g_warning("IP_PKTINFO failed: %s\n", strerror(errno));
191 if (avahi_set_cloexec(fd) < 0) {
192 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
196 if (avahi_set_nonblock(fd) < 0) {
197 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
210 gint avahi_open_socket_ipv6(void) {
211 struct sockaddr_in6 sa, local;
212 int fd = -1, ttl, yes;
214 mdns_mcast_group_ipv6(&sa);
216 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
217 g_warning("socket() failed: %s\n", strerror(errno));
222 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
223 g_warning("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
228 if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
229 g_warning("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
234 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
235 g_warning("SO_REUSEADDR failed: %s\n", strerror(errno));
240 if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
241 g_warning("IPV6_V6ONLY failed: %s\n", strerror(errno));
246 if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
247 g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
251 memset(&local, 0, sizeof(local));
252 local.sin6_family = AF_INET6;
253 local.sin6_port = htons(AVAHI_MDNS_PORT);
255 if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
256 g_warning("bind() failed: %s\n", strerror(errno));
261 if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
262 g_warning("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
267 if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
268 g_warning("IPV6_PKTINFO failed: %s\n", strerror(errno));
272 if (avahi_set_cloexec(fd) < 0) {
273 g_warning("FD_CLOEXEC failed: %s\n", strerror(errno));
277 if (avahi_set_nonblock(fd) < 0) {
278 g_warning("O_NONBLOCK failed: %s\n", strerror(errno));
291 static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) {
297 if (sendmsg(fd, msg, flags) >= 0)
300 if (errno != EAGAIN) {
301 g_message("sendmsg() failed: %s\n", strerror(errno));
305 if (avahi_wait_for_write(fd) < 0)
312 gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p) {
313 struct sockaddr_in sa;
316 struct cmsghdr *cmsg;
317 struct in_pktinfo *pkti;
318 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
322 g_assert(avahi_dns_packet_check_valid(p) >= 0);
324 mdns_mcast_group_ipv4(&sa);
326 memset(&io, 0, sizeof(io));
327 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
328 io.iov_len = p->size;
330 memset(cmsg_data, 0, sizeof(cmsg_data));
331 cmsg = (struct cmsghdr*) cmsg_data;
332 cmsg->cmsg_len = sizeof(cmsg_data);
333 cmsg->cmsg_level = IPPROTO_IP;
334 cmsg->cmsg_type = IP_PKTINFO;
336 pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
337 pkti->ipi_ifindex = interface;
339 memset(&msg, 0, sizeof(msg));
341 msg.msg_namelen = sizeof(sa);
344 msg.msg_control = cmsg_data;
345 msg.msg_controllen = sizeof(cmsg_data);
348 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
351 gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p) {
352 struct sockaddr_in6 sa;
355 struct cmsghdr *cmsg;
356 struct in6_pktinfo *pkti;
357 uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
361 g_assert(avahi_dns_packet_check_valid(p) >= 0);
363 mdns_mcast_group_ipv6(&sa);
365 memset(&io, 0, sizeof(io));
366 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
367 io.iov_len = p->size;
369 memset(cmsg_data, 0, sizeof(cmsg_data));
370 cmsg = (struct cmsghdr*) cmsg_data;
371 cmsg->cmsg_len = sizeof(cmsg_data);
372 cmsg->cmsg_level = IPPROTO_IPV6;
373 cmsg->cmsg_type = IPV6_PKTINFO;
375 pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
376 pkti->ipi6_ifindex = interface;
378 memset(&msg, 0, sizeof(msg));
380 msg.msg_namelen = sizeof(sa);
383 msg.msg_control = cmsg_data;
384 msg.msg_controllen = sizeof(cmsg_data);
387 return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
390 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint *ret_iface, guint8* ret_ttl) {
391 AvahiDnsPacket *p= NULL;
396 struct cmsghdr *cmsg;
397 gboolean found_ttl = FALSE, found_iface = FALSE;
404 p = avahi_dns_packet_new(0);
406 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
407 io.iov_len = p->max_size;
409 memset(&msg, 0, sizeof(msg));
410 msg.msg_name = ret_sa;
411 msg.msg_namelen = sizeof(struct sockaddr_in);
414 msg.msg_control = aux;
415 msg.msg_controllen = sizeof(aux);
418 if ((l = recvmsg(fd, &msg, 0)) < 0)
421 p->size = (size_t) l;
425 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg,cmsg)) {
426 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_TTL) {
427 *ret_ttl = *(uint8_t *) CMSG_DATA(cmsg);
431 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO) {
432 *ret_iface = ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
437 g_assert(found_iface);
444 avahi_dns_packet_free(p);
449 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
450 AvahiDnsPacket *p = NULL;
455 struct cmsghdr *cmsg;
456 gboolean found_ttl = FALSE, found_iface = FALSE;
463 p = avahi_dns_packet_new(0);
465 io.iov_base = AVAHI_DNS_PACKET_DATA(p);
466 io.iov_len = p->max_size;
468 memset(&msg, 0, sizeof(msg));
469 msg.msg_name = ret_sa;
470 msg.msg_namelen = sizeof(struct sockaddr_in6);
473 msg.msg_control = aux;
474 msg.msg_controllen = sizeof(aux);
477 if ((l = recvmsg(fd, &msg, 0)) < 0)
480 p->size = (size_t) l;
484 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
485 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
486 *ret_ttl = *(uint8_t *) CMSG_DATA(cmsg);
490 if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
491 *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
496 g_assert(found_iface);
503 avahi_dns_packet_free(p);