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