]> git.meshlink.io Git - catta/blob - avahi-core/socket.c
merge ia64 fixes from Jason Vas Dias
[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     size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
460 #elif defined(IP_SENDSRCADDR)
461     struct cmsghdr *cmsg;
462     size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_addr)) / sizeof(size_t)) + 1];
463 #endif
464
465     assert(fd >= 0);
466     assert(p);
467     assert(avahi_dns_packet_check_valid(p) >= 0);
468     assert(!dst_address || dst_port > 0);
469
470     if (!dst_address)
471         mdns_mcast_group_ipv4(&sa);
472     else
473         ipv4_address_to_sockaddr(&sa, dst_address, dst_port);
474
475     memset(&io, 0, sizeof(io));
476     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
477     io.iov_len = p->size;
478
479     memset(&msg, 0, sizeof(msg));
480     msg.msg_name = &sa;
481     msg.msg_namelen = sizeof(sa);
482     msg.msg_iov = &io;
483     msg.msg_iovlen = 1;
484     msg.msg_flags = 0;
485     msg.msg_control = NULL;
486     msg.msg_controllen = 0;
487
488 #ifdef IP_PKTINFO
489     if (interface > 0 || src_address) {
490         struct in_pktinfo *pkti;
491         
492         memset(cmsg_data, 0, sizeof(cmsg_data));
493         cmsg = (struct cmsghdr*) cmsg_data;
494         cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
495         cmsg->cmsg_level = IPPROTO_IP;
496         cmsg->cmsg_type = IP_PKTINFO;
497
498         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
499
500         if (interface > 0) 
501             pkti->ipi_ifindex = interface;
502
503         if (src_address)
504             pkti->ipi_spec_dst.s_addr = src_address->address;
505
506         msg.msg_control = cmsg_data;
507         msg.msg_controllen = sizeof(cmsg_data);
508     }
509 #elif defined(IP_SENDSRCADDR)
510     if (src_address) {
511         struct in_addr *addr;
512       
513         memset(cmsg_data, 0, sizeof(cmsg_data));
514         cmsg = (struct cmsghdr*) cmsg_data;
515         cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
516         cmsg->cmsg_level = IPPROTO_IP;
517         cmsg->cmsg_type = IP_SENDSRCADDR;
518         
519         addr = (struct in_addr *)CMSG_DATA(cmsg);
520         addr->s_addr =  src_address->address;
521         
522         msg.msg_control = cmsg_data;
523         msg.msg_controllen = sizeof(cmsg_data);
524     }
525 #elif defined(IP_MULTICAST_IF)
526     {
527         struct in_addr any = { INADDR_ANY };
528         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, src_address ? (const void*) &src_address->address : (const void*) &any, sizeof(struct in_addr)) < 0) {
529             avahi_log_warn("IP_MULTICAST_IF failed: %s", strerror(errno));
530             return -1;
531         }
532     }
533 #elif defined(__GNUC__)
534 #warning "FIXME: We need some code to set the outgoing interface/local address here if IP_PKTINFO/IP_MULTICAST_IF is not available"
535 #endif
536
537     return sendmsg_loop(fd, &msg, 0);
538 }
539
540 int avahi_send_dns_packet_ipv6(int fd, AvahiIfIndex interface, AvahiDnsPacket *p, const AvahiIPv6Address *src_address, const AvahiIPv6Address *dst_address, uint16_t dst_port) {
541     struct sockaddr_in6 sa;
542     struct msghdr msg;
543     struct iovec io;
544     struct cmsghdr *cmsg;
545     size_t cmsg_data[(CMSG_SPACE(sizeof(struct in6_pktinfo))/sizeof(size_t)) + 1];
546
547     assert(fd >= 0);
548     assert(p);
549     assert(avahi_dns_packet_check_valid(p) >= 0);
550     assert(!dst_address || dst_port > 0);
551
552     if (!dst_address)
553         mdns_mcast_group_ipv6(&sa);
554     else
555         ipv6_address_to_sockaddr(&sa, dst_address, dst_port);
556
557     memset(&io, 0, sizeof(io));
558     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
559     io.iov_len = p->size;
560
561     memset(&msg, 0, sizeof(msg));
562     msg.msg_name = &sa;
563     msg.msg_namelen = sizeof(sa);
564     msg.msg_iov = &io;
565     msg.msg_iovlen = 1;
566     msg.msg_flags = 0;
567
568     if (interface > 0 || src_address) {
569         struct in6_pktinfo *pkti;
570     
571         memset(cmsg_data, 0, sizeof(cmsg_data));
572         cmsg = (struct cmsghdr*) cmsg_data;
573         cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
574         cmsg->cmsg_level = IPPROTO_IPV6;
575         cmsg->cmsg_type = IPV6_PKTINFO;
576         
577         pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
578
579         if (interface > 0)
580             pkti->ipi6_ifindex = interface;
581
582         if (src_address)
583             memcpy(&pkti->ipi6_addr, src_address->address, sizeof(src_address->address));
584         
585         msg.msg_control = cmsg_data;
586         msg.msg_controllen = sizeof(cmsg_data);
587     } else {
588         msg.msg_control = NULL;
589         msg.msg_controllen = 0;
590     }
591     
592     return sendmsg_loop(fd, &msg, 0);
593 }
594
595 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) {
596     AvahiDnsPacket *p= NULL;
597     struct msghdr msg;
598     struct iovec io;
599     size_t aux[1024 / sizeof(size_t)]; /* for alignment on ia64 ! */
600     ssize_t l;
601     struct cmsghdr *cmsg;
602     int found_addr = 0;
603     int ms;
604     struct sockaddr_in sa;
605
606     assert(fd >= 0);
607
608     if (ioctl(fd, FIONREAD, &ms) < 0) {
609         avahi_log_warn("ioctl(): %s", strerror(errno));
610         goto fail;
611     }
612
613     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
614
615     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
616     io.iov_len = p->max_size;
617     
618     memset(&msg, 0, sizeof(msg));
619     msg.msg_name = &sa;
620     msg.msg_namelen = sizeof(sa);
621     msg.msg_iov = &io;
622     msg.msg_iovlen = 1;
623     msg.msg_control = aux;
624     msg.msg_controllen = sizeof(aux);
625     msg.msg_flags = 0;
626     
627     if ((l = recvmsg(fd, &msg, 0)) < 0) {
628         avahi_log_warn("recvmsg(): %s", strerror(errno));
629         goto fail;
630     }
631
632     if (sa.sin_addr.s_addr == INADDR_ANY) {
633         /* Linux 2.4 behaves very strangely sometimes! */
634         goto fail;
635     }
636
637     assert(!(msg.msg_flags & MSG_CTRUNC));
638     assert(!(msg.msg_flags & MSG_TRUNC));
639
640     p->size = (size_t) l;
641
642     if (ret_src_port)
643         *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
644
645     if (ret_src_address) {
646         AvahiAddress a;
647         avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
648         *ret_src_address = a.data.ipv4;
649     }
650
651     if (ret_ttl)
652         *ret_ttl = 255;
653
654     if (ret_iface)
655         *ret_iface = AVAHI_IF_UNSPEC;
656     
657     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
658         
659         if (cmsg->cmsg_level == IPPROTO_IP) {
660             
661             switch (cmsg->cmsg_type) {
662 #ifdef IP_RECVTTL
663                 case IP_RECVTTL:
664 #endif
665                 case IP_TTL:
666                     if (ret_ttl)
667                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
668
669                     break;
670
671 #ifdef IP_PKTINFO
672                 case IP_PKTINFO: {
673                     struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
674                     
675                     if (ret_iface)
676                         *ret_iface = (int) i->ipi_ifindex;
677                     
678                     if (ret_dst_address)
679                         ret_dst_address->address = i->ipi_addr.s_addr;
680                     
681                     found_addr = 1;
682
683                     break;
684                 }
685 #endif
686                     
687 #ifdef IP_RECVIF
688                 case IP_RECVIF: {
689                     struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA (cmsg);
690                     
691                     if (ret_iface)
692                         *ret_iface = (int) sdl->sdl_index;
693
694                     break;
695                 }
696 #endif
697             
698 #ifdef IP_RECVDSTADDR
699                 case IP_RECVDSTADDR:
700                     if (ret_dst_address)
701                         memcpy(&ret_dst_address->address, CMSG_DATA (cmsg), 4);
702                     
703                     found_addr = 1;
704                     break;
705 #endif
706                     
707                 default:
708                     avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
709                     break;
710             }
711         }
712     }
713
714     assert(found_addr);
715
716     return p;
717
718 fail:
719     if (p)
720         avahi_dns_packet_free(p);
721
722     return NULL;
723 }
724
725 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) {
726     AvahiDnsPacket *p = NULL;
727     struct msghdr msg;
728     struct iovec io;
729     size_t aux[1024 / sizeof(size_t)];
730     ssize_t l;
731     int ms;
732     struct cmsghdr *cmsg;
733     int found_ttl = 0, found_iface = 0;
734     struct sockaddr_in6 sa;
735
736     assert(fd >= 0);
737
738     if (ioctl(fd, FIONREAD, &ms) < 0) {
739         avahi_log_warn("ioctl(): %s", strerror(errno));
740         goto fail;
741     }
742     
743     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
744
745     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
746     io.iov_len = p->max_size;
747     
748     memset(&msg, 0, sizeof(msg));
749     msg.msg_name = (struct sockaddr*) &sa;
750     msg.msg_namelen = sizeof(sa);
751     
752     msg.msg_iov = &io;
753     msg.msg_iovlen = 1;
754     msg.msg_control = aux;
755     msg.msg_controllen = sizeof(aux);
756     msg.msg_flags = 0;
757     
758     if ((l = recvmsg(fd, &msg, 0)) < 0) {
759         avahi_log_warn("recvmsg(): %s", strerror(errno));
760         goto fail;
761     }
762
763     assert(!(msg.msg_flags & MSG_CTRUNC));
764     assert(!(msg.msg_flags & MSG_TRUNC));
765  
766     p->size = (size_t) l;
767
768     if (ret_src_port)
769         *ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
770
771     if (ret_src_address) {
772         AvahiAddress a;
773         avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
774         *ret_src_address = a.data.ipv6;
775     }
776
777     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
778
779         if (cmsg->cmsg_level == IPPROTO_IPV6) {
780
781             switch (cmsg->cmsg_type) {
782
783                 case IPV6_HOPLIMIT:
784
785                     if (ret_ttl)
786                         *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
787
788                     found_ttl = 1;
789                     
790                     break;
791
792                 case IPV6_PKTINFO: {
793                     struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
794                     
795                     if (ret_iface)
796                         *ret_iface = i->ipi6_ifindex;
797                     
798                     if (ret_dst_address)
799                         memcpy(ret_dst_address->address, i->ipi6_addr.s6_addr, 16);
800
801                     found_iface = 1;
802                     break;
803                 }
804                     
805                 default:
806                     avahi_log_warn("Unhandled cmsg_type : %d", cmsg->cmsg_type);
807                     break;
808             }
809         }
810     }
811
812     assert(found_iface);
813     assert(found_ttl);
814
815     return p;
816
817 fail:
818     if (p)
819         avahi_dns_packet_free(p);
820
821     return NULL;
822 }
823
824 int avahi_open_unicast_socket_ipv4(void) {
825     struct sockaddr_in local;
826     int fd = -1;
827         
828     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
829         avahi_log_warn("socket() failed: %s", strerror(errno));
830         goto fail;
831     }
832     
833     memset(&local, 0, sizeof(local));
834     local.sin_family = AF_INET;
835     
836     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
837         avahi_log_warn("bind() failed: %s", strerror(errno));
838         goto fail;
839     }
840
841     if (ipv4_pktinfo(fd) < 0) {
842          goto fail;
843     }
844
845     if (avahi_set_cloexec(fd) < 0) {
846         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
847         goto fail;
848     }
849     
850     if (avahi_set_nonblock(fd) < 0) {
851         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
852         goto fail;
853     }
854
855     return fd;
856
857 fail:
858     if (fd >= 0)
859         close(fd);
860
861     return -1;
862 }
863
864 int avahi_open_unicast_socket_ipv6(void) {
865     struct sockaddr_in6 local;
866     int fd = -1;
867         
868     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
869         avahi_log_warn("socket() failed: %s", strerror(errno));
870         goto fail;
871     }
872     
873     memset(&local, 0, sizeof(local));
874     local.sin6_family = AF_INET6;
875     
876     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
877         avahi_log_warn("bind() failed: %s", strerror(errno));
878         goto fail;
879     }
880
881     if (ipv6_pktinfo(fd) < 0)
882         goto fail;
883     
884     if (avahi_set_cloexec(fd) < 0) {
885         avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
886         goto fail;
887     }
888     
889     if (avahi_set_nonblock(fd) < 0) {
890         avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
891         goto fail;
892     }
893
894     return fd;
895
896 fail:
897     if (fd >= 0)
898         close(fd);
899
900     return -1;
901 }