]> git.meshlink.io Git - catta/blob - avahi-core/socket.c
* add logging API and make all code make use of it
[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
39 #include "dns.h"
40 #include "util.h"
41 #include "socket.h"
42 #include "log.h"
43
44 static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
45     g_assert(ret_sa);
46
47     memset(ret_sa, 0, sizeof(struct sockaddr_in));
48     
49     ret_sa->sin_family = AF_INET;
50     ret_sa->sin_port = htons(AVAHI_MDNS_PORT);
51     inet_pton(AF_INET, "224.0.0.251", &ret_sa->sin_addr);
52 }
53
54 static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) {
55
56     g_assert(ret_sa);
57
58     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
59     
60     ret_sa->sin6_family = AF_INET6;
61     ret_sa->sin6_port = htons(AVAHI_MDNS_PORT);
62     inet_pton(AF_INET6, "ff02::fb", &ret_sa->sin6_addr);
63 }
64
65 static void ipv4_address_to_sockaddr(struct sockaddr_in *ret_sa, const AvahiIPv4Address *a, guint16 port) {
66     g_assert(ret_sa);
67     g_assert(a);
68     g_assert(port > 0);
69
70     memset(ret_sa, 0, sizeof(struct sockaddr_in));
71     ret_sa->sin_family = AF_INET;
72     ret_sa->sin_port = htons(port);
73     memcpy(&ret_sa->sin_addr, a, sizeof(AvahiIPv4Address));
74 }
75
76 static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv6Address *a, guint16 port) {
77     g_assert(ret_sa);
78     g_assert(a);
79     g_assert(port > 0);
80
81     memset(ret_sa, 0, sizeof(struct sockaddr_in6));
82     ret_sa->sin6_family = AF_INET6;
83     ret_sa->sin6_port = htons(port);
84     memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
85 }
86
87 int avahi_mdns_mcast_join_ipv4 (int index, int fd) {
88     struct ip_mreqn mreq; 
89     struct sockaddr_in sa;
90
91     mdns_mcast_group_ipv4 (&sa);
92  
93     memset(&mreq, 0, sizeof(mreq));
94     mreq.imr_multiaddr = sa.sin_addr;
95     mreq.imr_ifindex = index;
96  
97     if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
98         avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
99         return -1;
100     } 
101
102     return 0;
103 }
104
105 int avahi_mdns_mcast_join_ipv6 (int index, int fd) {
106     struct ipv6_mreq mreq6; 
107     struct sockaddr_in6 sa6;
108
109     mdns_mcast_group_ipv6 (&sa6);
110
111     memset(&mreq6, 0, sizeof(mreq6));
112     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
113     mreq6.ipv6mr_interface = index;
114
115     if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
116         avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
117         return -1;
118     }
119
120     return 0;
121 }
122
123 int avahi_mdns_mcast_leave_ipv4 (int index, int fd) {
124     struct ip_mreqn mreq; 
125     struct sockaddr_in sa;
126     
127     mdns_mcast_group_ipv4 (&sa);
128  
129     memset(&mreq, 0, sizeof(mreq));
130     mreq.imr_multiaddr = sa.sin_addr;
131     mreq.imr_ifindex = index;
132  
133     if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
134         avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
135         return -1;
136     }
137
138     return 0;
139 }
140
141 int avahi_mdns_mcast_leave_ipv6 (int index, int fd) {
142     struct ipv6_mreq mreq6; 
143     struct sockaddr_in6 sa6;
144
145     mdns_mcast_group_ipv6 (&sa6);
146
147     memset(&mreq6, 0, sizeof(mreq6));
148     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
149     mreq6.ipv6mr_interface = index;
150
151     if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
152         avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno));
153         return -1;
154     }
155
156     return 0;
157 }
158
159 gint avahi_open_socket_ipv4(void) {
160     struct sockaddr_in local;
161     int fd = -1, ttl, yes;
162         
163     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
164         avahi_log_warn("socket() failed: %s\n", strerror(errno));
165         goto fail;
166     }
167     
168     ttl = 255;
169     if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
170         avahi_log_warn("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
171         goto fail;
172     }
173
174     ttl = 255;
175     if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
176         avahi_log_warn("IP_TTL failed: %s\n", strerror(errno));
177         goto fail;
178     }
179     
180     yes = 1;
181     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
182         avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
183         goto fail;
184     }
185
186     yes = 1;
187     if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
188         avahi_log_warn("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
189         goto fail;
190     }
191
192     
193     memset(&local, 0, sizeof(local));
194     local.sin_family = AF_INET;
195     local.sin_port = htons(AVAHI_MDNS_PORT);
196     
197     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
198         avahi_log_warn("bind() failed: %s\n", strerror(errno));
199         goto fail;
200     }
201
202     yes = 1;
203     if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
204         avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
205         goto fail;
206     }
207
208     yes = 1;
209     if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
210         avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
211         goto fail;
212     }
213     
214     if (avahi_set_cloexec(fd) < 0) {
215         avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
216         goto fail;
217     }
218     
219     if (avahi_set_nonblock(fd) < 0) {
220         avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
221         goto fail;
222     }
223
224     return fd;
225
226 fail:
227     if (fd >= 0)
228         close(fd);
229
230     return -1;
231 }
232
233 gint avahi_open_socket_ipv6(void) {
234     struct sockaddr_in6 sa, local;
235     int fd = -1, ttl, yes;
236
237     mdns_mcast_group_ipv6(&sa);
238         
239     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
240         avahi_log_warn("socket() failed: %s\n", strerror(errno));
241         goto fail;
242     }
243     
244     ttl = 255;
245     if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
246         avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
247         goto fail;
248     }
249
250     ttl = 255;
251     if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
252         avahi_log_warn("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
253         goto fail;
254     }
255     
256     yes = 1;
257     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
258         avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
259         goto fail;
260     }
261
262     yes = 1;
263     if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
264         avahi_log_warn("IPV6_V6ONLY failed: %s\n", strerror(errno));
265         goto fail;
266     }
267
268     yes = 1;
269     if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
270         avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
271         goto fail;
272     }
273
274     memset(&local, 0, sizeof(local));
275     local.sin6_family = AF_INET6;
276     local.sin6_port = htons(AVAHI_MDNS_PORT);
277     
278     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
279         avahi_log_warn("bind() failed: %s\n", strerror(errno));
280         goto fail;
281     }
282
283     yes = 1;
284     if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
285         avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
286         goto fail;
287     }
288
289     yes = 1;
290     if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
291         avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
292         goto fail;
293     }
294     
295     if (avahi_set_cloexec(fd) < 0) {
296         avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
297         goto fail;
298     }
299     
300     if (avahi_set_nonblock(fd) < 0) {
301         avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
302         goto fail;
303     }
304
305     return fd;
306
307 fail:
308     if (fd >= 0)
309         close(fd);
310
311     return -1;
312 }
313
314 static gint sendmsg_loop(gint fd, struct msghdr *msg, gint flags) {
315     g_assert(fd >= 0);
316     g_assert(msg);
317
318     for (;;) {
319     
320         if (sendmsg(fd, msg, flags) >= 0)
321             break;
322         
323         if (errno != EAGAIN) {
324             avahi_log_debug("sendmsg() failed: %s\n", strerror(errno));
325             return -1;
326         }
327         
328         if (avahi_wait_for_write(fd) < 0)
329             return -1;
330     }
331
332     return 0;
333 }
334
335 gint avahi_send_dns_packet_ipv4(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv4Address *a, guint16 port) {
336     struct sockaddr_in sa;
337     struct msghdr msg;
338     struct iovec io;
339     struct cmsghdr *cmsg;
340     struct in_pktinfo *pkti;
341     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
342
343     g_assert(fd >= 0);
344     g_assert(p);
345     g_assert(avahi_dns_packet_check_valid(p) >= 0);
346     g_assert(!a || port > 0);
347
348     if (!a)
349         mdns_mcast_group_ipv4(&sa);
350     else
351         ipv4_address_to_sockaddr(&sa, a, port);
352
353     memset(&io, 0, sizeof(io));
354     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
355     io.iov_len = p->size;
356
357     memset(cmsg_data, 0, sizeof(cmsg_data));
358     cmsg = (struct cmsghdr*) cmsg_data;
359     cmsg->cmsg_len = sizeof(cmsg_data);
360     cmsg->cmsg_level = IPPROTO_IP;
361     cmsg->cmsg_type = IP_PKTINFO;
362
363     pkti = (struct in_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
364     pkti->ipi_ifindex = interface;
365     
366     memset(&msg, 0, sizeof(msg));
367     msg.msg_name = &sa;
368     msg.msg_namelen = sizeof(sa);
369     msg.msg_iov = &io;
370     msg.msg_iovlen = 1;
371     msg.msg_control = cmsg_data;
372     msg.msg_controllen = sizeof(cmsg_data);
373     msg.msg_flags = 0;
374
375     return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
376 }
377
378 gint avahi_send_dns_packet_ipv6(gint fd, gint interface, AvahiDnsPacket *p, const AvahiIPv6Address *a, guint16 port) {
379     struct sockaddr_in6 sa;
380     struct msghdr msg;
381     struct iovec io;
382     struct cmsghdr *cmsg;
383     struct in6_pktinfo *pkti;
384     uint8_t cmsg_data[sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo)];
385
386     g_assert(fd >= 0);
387     g_assert(p);
388     g_assert(avahi_dns_packet_check_valid(p) >= 0);
389
390     if (!a)
391         mdns_mcast_group_ipv6(&sa);
392     else
393         ipv6_address_to_sockaddr(&sa, a, port);
394
395     memset(&io, 0, sizeof(io));
396     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
397     io.iov_len = p->size;
398
399     memset(cmsg_data, 0, sizeof(cmsg_data));
400     cmsg = (struct cmsghdr*) cmsg_data;
401     cmsg->cmsg_len = sizeof(cmsg_data);
402     cmsg->cmsg_level = IPPROTO_IPV6;
403     cmsg->cmsg_type = IPV6_PKTINFO;
404
405     pkti = (struct in6_pktinfo*) (cmsg_data + sizeof(struct cmsghdr));
406     pkti->ipi6_ifindex = interface;
407     
408     memset(&msg, 0, sizeof(msg));
409     msg.msg_name = &sa;
410     msg.msg_namelen = sizeof(sa);
411     msg.msg_iov = &io;
412     msg.msg_iovlen = 1;
413     msg.msg_control = cmsg_data;
414     msg.msg_controllen = sizeof(cmsg_data);
415     msg.msg_flags = 0;
416
417     return sendmsg_loop(fd, &msg, MSG_DONTROUTE);
418 }
419
420 AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint *ret_iface, guint8* ret_ttl) {
421     AvahiDnsPacket *p= NULL;
422     struct msghdr msg;
423     struct iovec io;
424     uint8_t aux[1024];
425     ssize_t l;
426     struct cmsghdr *cmsg;
427     gboolean found_ttl = FALSE, found_iface = FALSE;
428     guint ms;
429
430     g_assert(fd >= 0);
431     g_assert(ret_sa);
432     g_assert(ret_iface);
433     g_assert(ret_ttl);
434
435     if (ioctl(fd, FIONREAD, &ms) < 0) {
436         avahi_log_warn("ioctl(): %s", strerror(errno));
437         goto fail;
438     }
439
440     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
441
442     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
443     io.iov_len = p->max_size;
444     
445     memset(&msg, 0, sizeof(msg));
446     msg.msg_name = ret_sa;
447     msg.msg_namelen = sizeof(struct sockaddr_in);
448     msg.msg_iov = &io;
449     msg.msg_iovlen = 1;
450     msg.msg_control = aux;
451     msg.msg_controllen = sizeof(aux);
452     msg.msg_flags = 0;
453     
454     if ((l = recvmsg(fd, &msg, 0)) < 0) {
455         avahi_log_warn("recvmsg(): %s", strerror(errno));
456         goto fail;
457     }
458
459     if (ret_sa->sin_addr.s_addr == INADDR_ANY) {
460         /* Linux 2.4 behaves very strangely sometimes! */
461
462         avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), l); 
463         
464         
465         goto fail;
466     }
467     
468     g_assert(!(msg.msg_flags & MSG_CTRUNC));
469     g_assert(!(msg.msg_flags & MSG_TRUNC));
470     p->size = (size_t) l;
471     
472     *ret_ttl = 0;
473
474 /*     avahi_hexdump(msg.msg_control, msg.msg_controllen); */
475         
476     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
477
478 /*         avahi_hexdump(CMSG_DATA(cmsg), cmsg->cmsg_len - sizeof(struct cmsghdr)); */
479         
480         if (cmsg->cmsg_level == SOL_IP) {
481             
482             if (cmsg->cmsg_type == IP_TTL) {
483                 *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
484                 found_ttl = TRUE;
485             } else if (cmsg->cmsg_type == IP_PKTINFO) {
486                 *ret_iface = (gint) ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
487                 found_iface = TRUE;
488             }
489         }
490     }
491
492 /*     avahi_log_debug("ttl=%u iface=%i", *ret_ttl, *ret_iface); */
493
494     g_assert(found_iface);
495     g_assert(found_ttl);
496
497     return p;
498
499 fail:
500     if (p)
501         avahi_dns_packet_free(p);
502
503     return NULL;
504 }
505
506 AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
507     AvahiDnsPacket *p = NULL;
508     struct msghdr msg;
509     struct iovec io;
510     uint8_t aux[64];
511     ssize_t l;
512     guint ms;
513     
514     struct cmsghdr *cmsg;
515     gboolean found_ttl = FALSE, found_iface = FALSE;
516
517     g_assert(fd >= 0);
518     g_assert(ret_sa);
519     g_assert(ret_iface);
520     g_assert(ret_ttl);
521
522     if (ioctl(fd, FIONREAD, &ms) < 0) {
523         avahi_log_warn("ioctl(): %s", strerror(errno));
524         goto fail;
525     }
526     
527     p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
528
529     io.iov_base = AVAHI_DNS_PACKET_DATA(p);
530     io.iov_len = p->max_size;
531     
532     memset(&msg, 0, sizeof(msg));
533     msg.msg_name = ret_sa;
534     msg.msg_namelen = sizeof(struct sockaddr_in6);
535     msg.msg_iov = &io;
536     msg.msg_iovlen = 1;
537     msg.msg_control = aux;
538     msg.msg_controllen = sizeof(aux);
539     msg.msg_flags = 0;
540     
541     if ((l = recvmsg(fd, &msg, 0)) < 0) {
542         avahi_log_warn("recvmsg(): %s", strerror(errno));
543         goto fail;
544     }
545
546     p->size = (size_t) l;
547     
548     *ret_ttl = 0;
549
550     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
551         if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
552             *ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
553             found_ttl = TRUE;
554         }
555             
556         if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
557             *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
558             found_iface = TRUE;
559         }
560     }
561
562     g_assert(found_iface);
563     g_assert(found_ttl);
564
565     return p;
566
567 fail:
568     if (p)
569         avahi_dns_packet_free(p);
570
571     return NULL;
572 }
573
574 gint avahi_open_legacy_unicast_socket_ipv4(void) {
575     struct sockaddr_in local;
576     int fd = -1, yes;
577         
578     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
579         avahi_log_warn("socket() failed: %s\n", strerror(errno));
580         goto fail;
581     }
582     
583     memset(&local, 0, sizeof(local));
584     local.sin_family = AF_INET;
585     
586     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
587         avahi_log_warn("bind() failed: %s\n", strerror(errno));
588         goto fail;
589     }
590
591     yes = 1;
592     if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
593         avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
594         goto fail;
595     }
596
597     yes = 1;
598     if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
599         avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
600         goto fail;
601     }
602     
603     if (avahi_set_cloexec(fd) < 0) {
604         avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
605         goto fail;
606     }
607     
608     if (avahi_set_nonblock(fd) < 0) {
609         avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
610         goto fail;
611     }
612
613     return fd;
614
615 fail:
616     if (fd >= 0)
617         close(fd);
618
619     return -1;
620 }
621
622 gint avahi_open_legacy_unicast_socket_ipv6(void) {
623     struct sockaddr_in local;
624     int fd = -1, yes;
625         
626     if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
627         avahi_log_warn("socket() failed: %s\n", strerror(errno));
628         goto fail;
629     }
630     
631     memset(&local, 0, sizeof(local));
632     local.sin_family = AF_INET;
633     
634     if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
635         avahi_log_warn("bind() failed: %s\n", strerror(errno));
636         goto fail;
637     }
638
639     yes = 1;
640     if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
641         avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
642         goto fail;
643     }
644
645     yes = 1;
646     if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
647         avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
648         goto fail;
649     }
650     
651     if (avahi_set_cloexec(fd) < 0) {
652         avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
653         goto fail;
654     }
655     
656     if (avahi_set_nonblock(fd) < 0) {
657         avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
658         goto fail;
659     }
660
661     return fd;
662
663 fail:
664     if (fd >= 0)
665         close(fd);
666
667     return -1;
668 }