]> git.meshlink.io Git - catta/blob - avahi-core/socket.c
cleanup socket.c a little
[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;
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     ttl = 255;
310     if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 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 #endif
495
496     /** FIXME: We need some code to set the outgoing interface here if
497      * IP_PKTINFO is not available */
498     
499     return sendmsg_loop(fd, &msg, 0);
500 }
501
502 int avahi_send_dns_packet_ipv6(int fd, int interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, uint16_t port) {
503     struct sockaddr_in6 sa;
504     struct msghdr msg;
505     struct iovec io;
506     struct cmsghdr *cmsg;
507     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
508
509     assert(fd >= 0);
510     assert(p);
511     assert(avahi_dns_packet_check_valid(p) >= 0);
512
513     if (!a)
514         mdns_mcast_group_ipv6(&sa);
515     else
516         ipv6_address_to_sockaddr(&sa, a, port);
517
518     memset(&io, 0, sizeof(io));
519     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
520     io.iov_len = p->size;
521
522     
523     memset(&msg, 0, sizeof(msg));
524     msg.msg_name = &sa;
525     msg.msg_namelen = sizeof(sa);
526     msg.msg_iov = &io;
527     msg.msg_iovlen = 1;
528     msg.msg_flags = 0;
529
530     if (interface >= 0) {
531         struct in6_pktinfo *pkti;
532     
533         memset(cmsg_data, 0, sizeof(cmsg_data));
534         cmsg = (struct cmsghdr*) cmsg_data;
535         cmsg->cmsg_len = sizeof(cmsg_data);
536         cmsg->cmsg_level = IPPROTO_IPV6;
537         cmsg->cmsg_type = IPV6_PKTINFO;
538         
539         pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
540         pkti->ipi6_ifindex = interface;
541         
542         msg.msg_control = cmsg_data;
543         msg.msg_controllen = sizeof(cmsg_data);
544     } else {
545         msg.msg_control = NULL;
546         msg.msg_controllen = 0;
547     }
548     
549     return sendmsg_loop(fd, &msg, 0);
550 }
551
552 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in *ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
553     AvahiDnsPacket *p= NULL;
554     struct msghdr msg;
555     struct iovec io;
556     uint8_t aux[1024];
557     ssize_t l;
558     struct cmsghdr *cmsg;
559     int found_iface = 0, found_addr = 0;
560     int ms;
561
562     assert(fd >= 0);
563
564     if (ioctl(fd, FIONREAD, &ms) < 0) {
565         avahi_log_warn("ioctl(): %s", strerror(errno));
566         goto fail;
567     }
568
569     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
570
571     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
572     io.iov_len = p->max_size;
573     
574     memset(&msg, 0, sizeof(msg));
575     if (ret_sa) {
576         msg.msg_name = ret_sa;
577         msg.msg_namelen = sizeof(struct sockaddr_in);
578     } else {
579         msg.msg_name = NULL;
580         msg.msg_namelen = 0;
581     }
582     msg.msg_iov = &io;
583     msg.msg_iovlen = 1;
584     msg.msg_control = aux;
585     msg.msg_controllen = sizeof(aux);
586     msg.msg_flags = 0;
587     
588     if ((l = recvmsg(fd, &msg, 0)) < 0) {
589         avahi_log_warn("recvmsg(): %s", strerror(errno));
590         goto fail;
591     }
592
593     if (ret_sa && ret_sa->sin_addr.s_addr == INADDR_ANY) {
594         /* Linux 2.4 behaves very strangely sometimes! */
595
596         /*avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); */
597         goto fail;
598     }
599     
600     assert(!(msg.msg_flags & MSG_CTRUNC));
601     assert(!(msg.msg_flags & MSG_TRUNC));
602     p->size = (size_t) l;
603
604     if (ret_ttl)
605         *ret_ttl = 255;
606     
607     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
608         
609         if (cmsg->cmsg_level == IPPROTO_IP) {
610             switch (cmsg->cmsg_type) {
611 #ifdef IP_RECVTTL
612                 case IP_RECVTTL:
613 #endif
614                 case IP_TTL:
615                     if (ret_ttl)
616                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
617
618                     break;
619
620 #ifdef IP_PKTINFO
621                 case IP_PKTINFO: {
622                     struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
623                     
624                     if (ret_iface)
625                         *ret_iface = (int) i->ipi_ifindex;
626                     
627                     if (ret_dest_address)
628                         ret_dest_address->address = i->ipi_addr.s_addr;
629                     
630                     found_addr = found_iface = 1;
631
632                     break;
633                 }
634 #elif defined(IP_RECVIF)
635                 case IP_RECVIF: {
636                     struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
637                     
638                     if (ret_iface)
639                         *ret_iface = (int) sdl->sdl_index;
640                     
641                     found_iface = 1;
642                     break;
643                 }
644 #endif
645             
646 #ifdef IP_RECVDSTADDR
647                 case IP_RECVDSTADDR:
648                     if (ret_dest_address)
649                         memcpy(&ret_dest_address->address, CMSG_DATA (cmsg), 4);
650                     found_addr = 1;
651                     break;
652                     
653 #endif
654                     
655                 default:
656                     avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
657                     break;
658             }
659         }
660     }
661
662     assert(found_iface);
663     assert(found_addr);
664
665     return p;
666
667 fail:
668     if (p)
669         avahi_dns_packet_free(p);
670
671     return NULL;
672 }
673
674 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6 *ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t* ret_ttl) {
675     AvahiDnsPacket *p = NULL;
676     struct msghdr msg;
677     struct iovec io;
678     uint8_t aux[64];
679     ssize_t l;
680     int ms;
681     
682     struct cmsghdr *cmsg;
683     int found_ttl = 0, found_iface = 0;
684
685     assert(fd >= 0);
686
687     if (ioctl(fd, FIONREAD, &ms) < 0) {
688         avahi_log_warn("ioctl(): %s", strerror(errno));
689         goto fail;
690     }
691     
692     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
693
694     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
695     io.iov_len = p->max_size;
696     
697     memset(&msg, 0, sizeof(msg));
698     if (ret_sa) {
699         msg.msg_name = ret_sa;
700         msg.msg_namelen = sizeof(struct sockaddr_in6);
701     } else {
702         msg.msg_name = NULL;
703         msg.msg_namelen = 0;
704     }
705     
706     msg.msg_iov = &io;
707     msg.msg_iovlen = 1;
708     msg.msg_control = aux;
709     msg.msg_controllen = sizeof(aux);
710     msg.msg_flags = 0;
711     
712     if ((l = recvmsg(fd, &msg, 0)) < 0) {
713         avahi_log_warn("recvmsg(): %s", strerror(errno));
714         goto fail;
715     }
716
717     p->size = (size_t) l;
718
719     if (ret_ttl)
720         *ret_ttl = 0;
721
722     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
723         if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
724
725             if (ret_ttl)
726                 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
727             
728             found_ttl = 1;
729         }
730             
731         if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
732             struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
733
734             if (ret_iface)
735                 *ret_iface = i->ipi6_ifindex;
736
737             if (ret_dest_address)
738                 memcpy(ret_dest_address->address, i->ipi6_addr.s6_addr, 16);
739             
740             found_iface = 1;
741         }
742     }
743
744     assert(found_iface);
745     assert(found_ttl);
746
747     return p;
748
749 fail:
750     if (p)
751         avahi_dns_packet_free(p);
752
753     return NULL;
754 }
755
756 int avahi_open_unicast_socket_ipv4(void) {
757     struct sockaddr_in local;
758     int fd = -1;
759         
760     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
761         avahi_log_warn("socket() failed: %s", strerror(errno));
762         goto fail;
763     }
764     
765     memset(&local, 0, sizeof(local));
766     local.sin_family = AF_INET;
767     
768     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
769         avahi_log_warn("bind() failed: %s", strerror(errno));
770         goto fail;
771     }
772
773     if (ipv4_pktinfo(fd) < 0) {
774          goto fail;
775     }
776
777     if (avahi_set_cloexec(fd) < 0) {
778         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
779         goto fail;
780     }
781     
782     if (avahi_set_nonblock(fd) < 0) {
783         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
784         goto fail;
785     }
786
787     return fd;
788
789 fail:
790     if (fd >= 0)
791         close(fd);
792
793     return -1;
794 }
795
796 int avahi_open_unicast_socket_ipv6(void) {
797     struct sockaddr_in6 local;
798     int fd = -1;
799         
800     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
801         avahi_log_warn("socket() failed: %s", strerror(errno));
802         goto fail;
803     }
804     
805     memset(&local, 0, sizeof(local));
806     local.sin6_family = AF_INET6;
807     
808     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
809         avahi_log_warn("bind() failed: %s", strerror(errno));
810         goto fail;
811     }
812
813     if (ipv6_pktinfo(fd) < 0)
814         goto fail;
815     
816     if (avahi_set_cloexec(fd) < 0) {
817         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
818         goto fail;
819     }
820     
821     if (avahi_set_nonblock(fd) < 0) {
822         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
823         goto fail;
824     }
825
826     return fd;
827
828 fail:
829     if (fd >= 0)
830         close(fd);
831
832     return -1;
833 }