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