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