]> git.meshlink.io Git - catta/blob - avahi-core/socket.c
add a compiler warning that avahi_send_dns_packet_ipv4() still misses support for...
[catta] / avahi-core / socket.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <inttypes.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <net/if.h>
37 #include <sys/ioctl.h>
38 #include <assert.h>
39
40 #ifdef IP_RECVIF
41 #include <net/if_dl.h>
42 #endif
43
44 #include "dns.h"
45 #include "fdutil.h"
46 #include "socket.h"
47 #include "log.h"
48
49 /* this is a portability hack */
50 #ifndef IPV6_ADD_MEMBERSHIP
51 #ifdef  IPV6_JOIN_GROUP
52 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
53 #endif
54 #endif
55
56 #ifndef IPV6_DROP_MEMBERSHIP
57 #ifdef  IPV6_LEAVE_GROUP
58 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
59 #endif
60 #endif
61
62 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
63     assert(ret_sa);
64
65     memset(ret_sa, 0, sizeof(struct sockaddr_in));
66     ret_sa->sin_family = AF_INET;
67     ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
68     inet_pton(AF_INET, AVAHI_IPV4_MCAST_GROUP, &ret_sa->sin_addr);
69 }
70
71 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
72     assert(ret_sa);
73
74     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
75     ret_sa->sin6_family = AF_INET6;
76     ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
77     inet_pton(AF_INET6, AVAHI_IPV6_MCAST_GROUP, &ret_sa->sin6_addr);
78 }
79
80 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, uint16_t port) {
81     assert(ret_sa);
82     assert(a);
83     assert(port > 0);
84
85     memset(ret_sa, 0, sizeof(struct sockaddr_in));
86     ret_sa->sin_family = AF_INET;
87     ret_sa->sin_port = htons(port);
88     memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
89 }
90
91 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, uint16_t port) {
92     assert(ret_sa);
93     assert(a);
94     assert(port > 0);
95
96     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
97     ret_sa->sin6_family = AF_INET6;
98     ret_sa->sin6_port = htons(port);
99     memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
100 }
101
102 #ifdef HAVE_STRUCT_IP_MREQN
103 int avahi_mdns_mcast_join_ipv4(int fd, int idx, int join) {
104     struct ip_mreqn mreq;
105 #else
106 int avahi_mdns_mcast_join_ipv4(int fd, const AvahiAddress *a, int join) {
107     struct ip_mreq mreq;
108 #endif
109         
110     struct sockaddr_in sa;
111     memset(&mreq, 0, sizeof(mreq));
112
113 #ifdef HAVE_STRUCT_IP_MREQN
114     mreq.imr_ifindex = idx;
115 #else
116     assert(a);
117     assert(a->proto == AVAHI_PROTO_INET);
118     mreq.imr_interface.s_addr = a->data.ipv4.address;
119 #endif
120     
121     mdns_mcast_group_ipv4(&sa);
122     mreq.imr_multiaddr = sa.sin_addr;
123
124     if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
125         avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
126         return -1;
127     } 
128
129     return 0;
130 }
131
132 int avahi_mdns_mcast_join_ipv6(int fd, int idx, int join) {
133     struct ipv6_mreq mreq6; 
134     struct sockaddr_in6 sa6;
135
136     mdns_mcast_group_ipv6 (&sa6);
137
138     memset(&mreq6, 0, sizeof(mreq6));
139     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
140     mreq6.ipv6mr_interface = idx;
141
142     if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
143         avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
144         return -1;
145     }
146
147     return 0;
148 }
149
150 static int reuseaddr(int fd) {
151     int yes;
152     
153     yes = 1;
154     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
155         avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno));
156         return -1;
157     }
158     
159 #ifdef SO_REUSEPORT
160     yes = 1;
161     if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) < 0) {
162         avahi_log_warn("SO_REUSEPORT failed: %s", strerror(errno));
163         return -1;
164     }
165 #endif
166     
167     return 0;
168 }
169
170 static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
171     
172     assert(fd >= 0);
173     assert(sa);
174     assert(l > 0);
175     
176     if (bind(fd, sa, l) < 0) {
177
178         if (errno != EADDRINUSE) {
179             avahi_log_warn("bind() failed: %s", strerror(errno));
180             return -1;
181         }
182             
183         avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
184                        sa->sa_family == AF_INET ? "IPv4" : "IPv6");
185
186         /* Try again, this time with SO_REUSEADDR set */
187         if (reuseaddr(fd) < 0)
188             return -1;
189         
190         if (bind(fd, sa, l) < 0) {
191             avahi_log_warn("bind() failed: %s", strerror(errno));
192             return -1;
193         }
194     } else {
195
196         /* We enable SO_REUSEADDR afterwards, to make sure that the
197          * user may run other mDNS implementations if he really
198          * wants. */
199         
200         if (reuseaddr(fd) < 0)
201             return -1;
202     }
203
204     return 0;
205 }
206
207 static int ipv4_pktinfo(int fd) {
208     int yes;
209     
210 #ifdef IP_PKTINFO
211     yes = 1;
212     if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
213         avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
214         return -1;
215     }
216 #else
217     
218 #ifdef IP_RECVINTERFACE
219     yes = 1;
220     if (setsockopt (fd, IPPROTO_IP, IP_RECVINTERFACE, &yes, sizeof(yes)) < 0) {
221         avahi_log_warn("IP_RECVINTERFACE failed: %s", strerror(errno));
222         return -1;
223     }
224 #elif defined(IP_RECVIF)
225     yes = 1;
226     if (setsockopt (fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes)) < 0) {
227         avahi_log_warn("IP_RECVIF failed: %s", strerror(errno));
228         return -1;
229     }
230 #endif
231     
232 #ifdef IP_RECVDSTADDR
233     yes = 1;
234     if (setsockopt (fd, IPPROTO_IP, IP_RECVDSTADDR, &yes, sizeof(yes)) < 0) {
235         avahi_log_warn("IP_RECVDSTADDR failed: %s", strerror(errno));
236         return -1;
237     }
238 #endif
239     
240 #endif /* IP_PKTINFO */
241
242 #ifdef IP_RECVTTL
243     yes = 1;
244     if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
245         avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
246         return -1;
247     }
248 #endif
249
250     return 0;
251 }
252
253 static int ipv6_pktinfo(int fd) {
254     int yes;
255     
256 #ifdef IPV6_RECVPKTINFO
257     yes = 1;
258     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) {
259         avahi_log_warn("IPV6_RECVPKTINFO failed: %s", strerror(errno));
260         return -1;
261     }
262 #elif defined(IPV6_PKTINFO)
263     yes = 1;
264     if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
265         avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
266         return -1;
267     }
268 #endif
269
270 #ifdef IPV6_RECVHOPS
271     yes = 1;
272     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPS, &yes, sizeof(yes)) < 0) {
273         avahi_log_warn("IPV6_RECVHOPS failed: %s", strerror(errno));
274         return -1;
275     }
276 #elif defined(IPV6_RECVHOPLIMIT)
277     yes = 1;
278     if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) {
279         avahi_log_warn("IPV6_RECVHOPLIMIT failed: %s", strerror(errno));
280         return -1;
281     }
282 #elif defined(IPV6_HOPLIMIT)
283     yes = 1;
284     if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
285         avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
286         return -1;
287     }
288 #endif
289
290     return 0;
291 }
292
293 int avahi_open_socket_ipv4(int no_reuse) {
294     struct sockaddr_in local;
295     int fd = -1, r, ittl;
296     uint8_t ttl, cyes;
297         
298     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
299         avahi_log_warn("socket() failed: %s", strerror(errno));
300         goto fail;
301     }
302     
303     ttl = 255;
304     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
305         avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno));
306         goto fail;
307     }
308
309     ittl = 255;
310     if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl)) < 0) {
311         avahi_log_warn("IP_TTL failed: %s", strerror(errno));
312         goto fail;
313     }
314     
315     cyes = 1;
316     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &cyes, sizeof(cyes)) < 0) {
317         avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno));
318         goto fail;
319     }
320     
321     memset(&local, 0, sizeof(local));
322     local.sin_family = AF_INET;
323     local.sin_port = htons(AVAHI_MDNS_PORT);
324
325     if (no_reuse)
326         r = bind(fd, (struct sockaddr*) &local, sizeof(local));
327     else
328         r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
329
330     if (r < 0)
331         goto fail;
332
333     if (ipv4_pktinfo (fd) < 0)
334          goto fail;
335
336     if (avahi_set_cloexec(fd) < 0) {
337         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
338         goto fail;
339     }
340     
341     if (avahi_set_nonblock(fd) < 0) {
342         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
343         goto fail;
344     }
345
346     return fd;
347
348 fail:
349     if (fd >= 0)
350         close(fd);
351
352     return -1;
353 }
354
355 int avahi_open_socket_ipv6(int no_reuse) {
356     struct sockaddr_in6 sa, local;
357     int fd = -1, yes, r;
358     int ttl;
359
360     mdns_mcast_group_ipv6(&sa);
361         
362     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
363         avahi_log_warn("socket() failed: %s", strerror(errno));
364         goto fail;
365     }
366     
367     ttl = 255;
368     if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
369         avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno));
370         goto fail;
371     }
372
373     ttl = 255;
374     if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
375         avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno));
376         goto fail;
377     }
378     
379     yes = 1;
380     if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
381         avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
382         goto fail;
383     }
384
385     yes = 1;
386     if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
387         avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno));
388         goto fail;
389     }
390
391     memset(&local, 0, sizeof(local));
392     local.sin6_family = AF_INET6;
393     local.sin6_port = htons(AVAHI_MDNS_PORT);
394     
395     if (no_reuse)
396         r = bind(fd, (struct sockaddr*) &local, sizeof(local));
397     else
398         r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local));
399
400     if (r < 0)
401         goto fail;
402
403     if (ipv6_pktinfo(fd) < 0)
404         goto fail;
405     
406     if (avahi_set_cloexec(fd) < 0) {
407         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
408         goto fail;
409     }
410     
411     if (avahi_set_nonblock(fd) < 0) {
412         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
413         goto fail;
414     }
415
416     return fd;
417
418 fail:
419     if (fd >= 0)
420         close(fd);
421
422     return -1;
423 }
424
425 static int sendmsg_loop(int fd, struct msghdr *msg, int flags) {
426     assert(fd >= 0);
427     assert(msg);
428
429     for (;;) {
430     
431         if (sendmsg(fd, msg, flags) >= 0)
432             break;
433         
434         if (errno != EAGAIN) {
435             avahi_log_debug("sendmsg() failed: %s", strerror(errno));
436             return -1;
437         }
438         
439         if (avahi_wait_for_write(fd) < 0)
440             return -1;
441     }
442
443     return 0;
444 }
445
446 int avahi_send_dns_packet_ipv4(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, uint16_t port) {
447     struct sockaddr_in sa;
448     struct msghdr msg;
449     struct iovec io;
450 #ifdef IP_PKTINFO
451     struct cmsghdr *cmsg;
452     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
453 #endif
454
455     assert(fd >= 0);
456     assert(p);
457     assert(avahi_dns_packet_check_valid(p) >= 0);
458     assert(!a || port > 0);
459
460     if (!a)
461         mdns_mcast_group_ipv4(&sa);
462     else
463         ipv4_address_to_sockaddr(&sa, a, port);
464
465     memset(&io, 0, sizeof(io));
466     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
467     io.iov_len = p->size;
468
469     memset(&msg, 0, sizeof(msg));
470     msg.msg_name = &sa;
471     msg.msg_namelen = sizeof(sa);
472     msg.msg_iov = &io;
473     msg.msg_iovlen = 1;
474     msg.msg_flags = 0;
475     msg.msg_control = NULL;
476     msg.msg_controllen = 0;
477
478 #ifdef IP_PKTINFO
479     if (interface >= 0) {
480         struct in_pktinfo *pkti;
481         
482         memset(cmsg_data, 0, sizeof(cmsg_data));
483         cmsg = (struct cmsghdr*) cmsg_data;
484         cmsg->cmsg_len = sizeof(cmsg_data);
485         cmsg->cmsg_level = IPPROTO_IP;
486         cmsg->cmsg_type = IP_PKTINFO;
487
488         pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
489         pkti->ipi_ifindex = interface;
490
491         msg.msg_control = cmsg_data;
492         msg.msg_controllen = sizeof(cmsg_data);
493     }
494 #else
495 #ifdef __GNUC__
496 #warning "FIXME: We need some code to set the outgoing interface here if IP_PKTINFO is not available"
497 #endif
498 #endif
499     
500     return sendmsg_loop(fd, &msg, 0);
501 }
502
503 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
504     struct sockaddr_in6 sa;
505     struct msghdr msg;
506     struct iovec io;
507     struct cmsghdr *cmsg;
508     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
509
510     assert(fd >= 0);
511     assert(p);
512     assert(avahi_dns_packet_check_valid(p) >= 0);
513
514     if (!a)
515         mdns_mcast_group_ipv6(&sa);
516     else
517         ipv6_address_to_sockaddr(&sa, a, port);
518
519     memset(&io, 0, sizeof(io));
520     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
521     io.iov_len = p->size;
522
523     
524     memset(&msg, 0, sizeof(msg));
525     msg.msg_name = &sa;
526     msg.msg_namelen = sizeof(sa);
527     msg.msg_iov = &io;
528     msg.msg_iovlen = 1;
529     msg.msg_flags = 0;
530
531     if (interface >= 0) {
532         struct in6_pktinfo *pkti;
533     
534         memset(cmsg_data, 0, sizeof(cmsg_data));
535         cmsg = (struct cmsghdr*) cmsg_data;
536         cmsg->cmsg_len = sizeof(cmsg_data);
537         cmsg->cmsg_level = IPPROTO_IPV6;
538         cmsg->cmsg_type = IPV6_PKTINFO;
539         
540         pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
541         pkti->ipi6_ifindex = interface;
542         
543         msg.msg_control = cmsg_data;
544         msg.msg_controllen = sizeof(cmsg_data);
545     } else {
546         msg.msg_control = NULL;
547         msg.msg_controllen = 0;
548     }
549     
550     return sendmsg_loop(fd, &msg, 0);
551 }
552
553 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
554     AvahiDnsPacket *p= NULL;
555     struct msghdr msg;
556     struct iovec io;
557     uint8_t aux[1024];
558     ssize_t l;
559     struct cmsghdr *cmsg;
560     int found_iface = 0, found_addr = 0;
561     int ms;
562
563     assert(fd >= 0);
564
565     if (ioctl(fd, FIONREAD, &ms) < 0) {
566         avahi_log_warn("ioctl(): %s", strerror(errno));
567         goto fail;
568     }
569
570     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
571
572     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
573     io.iov_len = p->max_size;
574     
575     memset(&msg, 0, sizeof(msg));
576     if (ret_sa) {
577         msg.msg_name = ret_sa;
578         msg.msg_namelen = sizeof(struct sockaddr_in);
579     } else {
580         msg.msg_name = NULL;
581         msg.msg_namelen = 0;
582     }
583     msg.msg_iov = &io;
584     msg.msg_iovlen = 1;
585     msg.msg_control = aux;
586     msg.msg_controllen = sizeof(aux);
587     msg.msg_flags = 0;
588     
589     if ((l = recvmsg(fd, &msg, 0)) < 0) {
590         avahi_log_warn("recvmsg(): %s", strerror(errno));
591         goto fail;
592     }
593
594     if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
595         /* Linux 2.4 behaves very strangely sometimes! */
596
597         /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
598         goto fail;
599     }
600     
601     assert(!(msg.msg_flags & MSG_CTRUNC));
602     assert(!(msg.msg_flags & MSG_TRUNC));
603     p->size = (size_t) l;
604
605     if (ret_ttl)
606         *ret_ttl = 255;
607     
608     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
609         
610         if (cmsg->cmsg_level == IPPROTO_IP) {
611             switch (cmsg->cmsg_type) {
612 #ifdef IP_RECVTTL
613                 case IP_RECVTTL:
614 #endif
615                 case IP_TTL:
616                     if (ret_ttl)
617                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
618
619                     break;
620
621 #ifdef IP_PKTINFO
622                 case IP_PKTINFO: {
623                     struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
624                     
625                     if (ret_iface)
626                         *ret_iface = (int) i->ipi_ifindex;
627                     
628                     if (ret_dest_address)
629                         ret_dest_address->address = i->ipi_addr.s_addr;
630                     
631                     found_addr = found_iface = 1;
632
633                     break;
634                 }
635 #elif defined(IP_RECVIF)
636                 case IP_RECVIF: {
637                     struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
638                     
639                     if (ret_iface)
640                         *ret_iface = (int) sdl->sdl_index;
641                     
642                     found_iface = 1;
643                     break;
644                 }
645 #endif
646             
647 #ifdef IP_RECVDSTADDR
648                 case IP_RECVDSTADDR:
649                     if (ret_dest_address)
650                         memcpy(&ret_dest_address->address, CMSG_DATA (cmsg), 4);
651                     found_addr = 1;
652                     break;
653                     
654 #endif
655                     
656                 default:
657                     avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
658                     break;
659             }
660         }
661     }
662
663     assert(found_iface);
664     assert(found_addr);
665
666     return p;
667
668 fail:
669     if (p)
670         avahi_dns_packet_free(p);
671
672     return NULL;
673 }
674
675 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
676     AvahiDnsPacket *p = NULL;
677     struct msghdr msg;
678     struct iovec io;
679     uint8_t aux[64];
680     ssize_t l;
681     int ms;
682     
683     struct cmsghdr *cmsg;
684     int found_ttl = 0, found_iface = 0;
685
686     assert(fd >= 0);
687
688     if (ioctl(fd, FIONREAD, &ms) < 0) {
689         avahi_log_warn("ioctl(): %s", strerror(errno));
690         goto fail;
691     }
692     
693     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
694
695     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
696     io.iov_len = p->max_size;
697     
698     memset(&msg, 0, sizeof(msg));
699     if (ret_sa) {
700         msg.msg_name = ret_sa;
701         msg.msg_namelen = sizeof(struct sockaddr_in6);
702     } else {
703         msg.msg_name = NULL;
704         msg.msg_namelen = 0;
705     }
706     
707     msg.msg_iov = &io;
708     msg.msg_iovlen = 1;
709     msg.msg_control = aux;
710     msg.msg_controllen = sizeof(aux);
711     msg.msg_flags = 0;
712     
713     if ((l = recvmsg(fd, &msg, 0)) < 0) {
714         avahi_log_warn("recvmsg(): %s", strerror(errno));
715         goto fail;
716     }
717
718     p->size = (size_t) l;
719
720     if (ret_ttl)
721         *ret_ttl = 0;
722
723     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
724         if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
725
726             if (ret_ttl)
727                 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
728             
729             found_ttl = 1;
730         }
731             
732         if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
733             struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
734
735             if (ret_iface)
736                 *ret_iface = i->ipi6_ifindex;
737
738             if (ret_dest_address)
739                 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
740             
741             found_iface = 1;
742         }
743     }
744
745     assert(found_iface);
746     assert(found_ttl);
747
748     return p;
749
750 fail:
751     if (p)
752         avahi_dns_packet_free(p);
753
754     return NULL;
755 }
756
757 int avahi_open_unicast_socket_ipv4(void) {
758     struct sockaddr_in local;
759     int fd = -1;
760         
761     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
762         avahi_log_warn("socket() failed: %s", strerror(errno));
763         goto fail;
764     }
765     
766     memset(&local, 0, sizeof(local));
767     local.sin_family = AF_INET;
768     
769     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
770         avahi_log_warn("bind() failed: %s", strerror(errno));
771         goto fail;
772     }
773
774     if (ipv4_pktinfo(fd) < 0) {
775          goto fail;
776     }
777
778     if (avahi_set_cloexec(fd) < 0) {
779         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
780         goto fail;
781     }
782     
783     if (avahi_set_nonblock(fd) < 0) {
784         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
785         goto fail;
786     }
787
788     return fd;
789
790 fail:
791     if (fd >= 0)
792         close(fd);
793
794     return -1;
795 }
796
797 int avahi_open_unicast_socket_ipv6(void) {
798     struct sockaddr_in6 local;
799     int fd = -1;
800         
801     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
802         avahi_log_warn("socket() failed: %s", strerror(errno));
803         goto fail;
804     }
805     
806     memset(&local, 0, sizeof(local));
807     local.sin6_family = AF_INET6;
808     
809     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
810         avahi_log_warn("bind() failed: %s", strerror(errno));
811         goto fail;
812     }
813
814     if (ipv6_pktinfo(fd) < 0)
815         goto fail;
816     
817     if (avahi_set_cloexec(fd) < 0) {
818         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
819         goto fail;
820     }
821     
822     if (avahi_set_nonblock(fd) < 0) {
823         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
824         goto fail;
825     }
826
827     return fd;
828
829 fail:
830     if (fd >= 0)
831         close(fd);
832
833     return -1;
834 }