tsocket: Use common code in tsocket_bsd_common_prepare_fd
[kai/samba-autobuild/.git] / lib / tsocket / tsocket_bsd.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 2009
5
6      ** NOTE! The following LGPL license applies to the tsocket
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "replace.h"
25 #include "system/filesys.h"
26 #include "system/network.h"
27 #include "tsocket.h"
28 #include "tsocket_internal.h"
29 #include "lib/util/iov_buf.h"
30 #include "lib/util/blocking.h"
31
32 static int tsocket_bsd_error_from_errno(int ret,
33                                         int sys_errno,
34                                         bool *retry)
35 {
36         *retry = false;
37
38         if (ret >= 0) {
39                 return 0;
40         }
41
42         if (ret != -1) {
43                 return EIO;
44         }
45
46         if (sys_errno == 0) {
47                 return EIO;
48         }
49
50         if (sys_errno == EINTR) {
51                 *retry = true;
52                 return sys_errno;
53         }
54
55         if (sys_errno == EINPROGRESS) {
56                 *retry = true;
57                 return sys_errno;
58         }
59
60         if (sys_errno == EAGAIN) {
61                 *retry = true;
62                 return sys_errno;
63         }
64
65         /* ENOMEM is retryable on Solaris/illumos, and possibly other systems. */
66         if (sys_errno == ENOMEM) {
67                 *retry = true;
68                 return sys_errno;
69         }
70
71 #ifdef EWOULDBLOCK
72         if (sys_errno == EWOULDBLOCK) {
73                 *retry = true;
74                 return sys_errno;
75         }
76 #endif
77
78         return sys_errno;
79 }
80
81 static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
82 {
83         int i;
84         int sys_errno = 0;
85         int fds[3];
86         int num_fds = 0;
87
88         int result;
89         bool ok;
90
91         if (fd == -1) {
92                 return -1;
93         }
94
95         /* first make a fd >= 3 */
96         if (high_fd) {
97                 while (fd < 3) {
98                         fds[num_fds++] = fd;
99                         fd = dup(fd);
100                         if (fd == -1) {
101                                 sys_errno = errno;
102                                 break;
103                         }
104                 }
105                 for (i=0; i<num_fds; i++) {
106                         close(fds[i]);
107                 }
108                 if (fd == -1) {
109                         errno = sys_errno;
110                         return fd;
111                 }
112         }
113
114         result = set_blocking(fd, false);
115         if (result == -1) {
116                 goto fail;
117         }
118
119         ok = smb_set_close_on_exec(fd);
120         if (!ok) {
121                 goto fail;
122         }
123
124         return fd;
125
126  fail:
127         if (fd != -1) {
128                 sys_errno = errno;
129                 close(fd);
130                 errno = sys_errno;
131         }
132         return -1;
133 }
134
135 static ssize_t tsocket_bsd_pending(int fd)
136 {
137         int ret, error;
138         int value = 0;
139         socklen_t len;
140
141         ret = ioctl(fd, FIONREAD, &value);
142         if (ret == -1) {
143                 return ret;
144         }
145
146         if (ret != 0) {
147                 /* this should not be reached */
148                 errno = EIO;
149                 return -1;
150         }
151
152         if (value != 0) {
153                 return value;
154         }
155
156         error = 0;
157         len = sizeof(error);
158
159         /*
160          * if no data is available check if the socket is in error state. For
161          * dgram sockets it's the way to return ICMP error messages of
162          * connected sockets to the caller.
163          */
164         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
165         if (ret == -1) {
166                 return ret;
167         }
168         if (error != 0) {
169                 errno = error;
170                 return -1;
171         }
172         return 0;
173 }
174
175 static const struct tsocket_address_ops tsocket_address_bsd_ops;
176
177 struct tsocket_address_bsd {
178         socklen_t sa_socklen;
179         union {
180                 struct sockaddr sa;
181                 struct sockaddr_in in;
182 #ifdef HAVE_IPV6
183                 struct sockaddr_in6 in6;
184 #endif
185                 struct sockaddr_un un;
186                 struct sockaddr_storage ss;
187         } u;
188 };
189
190 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
191                                        const struct sockaddr *sa,
192                                        size_t sa_socklen,
193                                        struct tsocket_address **_addr,
194                                        const char *location)
195 {
196         struct tsocket_address *addr;
197         struct tsocket_address_bsd *bsda;
198
199         if (sa_socklen < sizeof(sa->sa_family)) {
200                 errno = EINVAL;
201                 return -1;
202         }
203
204         switch (sa->sa_family) {
205         case AF_UNIX:
206                 if (sa_socklen > sizeof(struct sockaddr_un)) {
207                         sa_socklen = sizeof(struct sockaddr_un);
208                 }
209                 break;
210         case AF_INET:
211                 if (sa_socklen < sizeof(struct sockaddr_in)) {
212                         errno = EINVAL;
213                         return -1;
214                 }
215                 sa_socklen = sizeof(struct sockaddr_in);
216                 break;
217 #ifdef HAVE_IPV6
218         case AF_INET6:
219                 if (sa_socklen < sizeof(struct sockaddr_in6)) {
220                         errno = EINVAL;
221                         return -1;
222                 }
223                 sa_socklen = sizeof(struct sockaddr_in6);
224                 break;
225 #endif
226         default:
227                 errno = EAFNOSUPPORT;
228                 return -1;
229         }
230
231         if (sa_socklen > sizeof(struct sockaddr_storage)) {
232                 errno = EINVAL;
233                 return -1;
234         }
235
236         addr = tsocket_address_create(mem_ctx,
237                                       &tsocket_address_bsd_ops,
238                                       &bsda,
239                                       struct tsocket_address_bsd,
240                                       location);
241         if (!addr) {
242                 errno = ENOMEM;
243                 return -1;
244         }
245
246         ZERO_STRUCTP(bsda);
247
248         memcpy(&bsda->u.ss, sa, sa_socklen);
249
250         bsda->sa_socklen = sa_socklen;
251 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
252         bsda->u.sa.sa_len = bsda->sa_socklen;
253 #endif
254
255         *_addr = addr;
256         return 0;
257 }
258
259 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
260                                      struct sockaddr *sa,
261                                      size_t sa_socklen)
262 {
263         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
264                                            struct tsocket_address_bsd);
265
266         if (!bsda) {
267                 errno = EINVAL;
268                 return -1;
269         }
270
271         if (sa_socklen < bsda->sa_socklen) {
272                 errno = EINVAL;
273                 return -1;
274         }
275
276         if (sa_socklen > bsda->sa_socklen) {
277                 memset(sa, 0, sa_socklen);
278                 sa_socklen = bsda->sa_socklen;
279         }
280
281         memcpy(sa, &bsda->u.ss, sa_socklen);
282 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
283         sa->sa_len = sa_socklen;
284 #endif
285         return sa_socklen;
286 }
287
288 bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam)
289 {
290         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
291                                            struct tsocket_address_bsd);
292
293         if (!bsda) {
294                 return false;
295         }
296
297         switch (bsda->u.sa.sa_family) {
298         case AF_INET:
299                 if (strcasecmp(fam, "ip") == 0) {
300                         return true;
301                 }
302
303                 if (strcasecmp(fam, "ipv4") == 0) {
304                         return true;
305                 }
306
307                 return false;
308 #ifdef HAVE_IPV6
309         case AF_INET6:
310                 if (strcasecmp(fam, "ip") == 0) {
311                         return true;
312                 }
313
314                 if (strcasecmp(fam, "ipv6") == 0) {
315                         return true;
316                 }
317
318                 return false;
319 #endif
320         }
321
322         return false;
323 }
324
325 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
326                                        const char *fam,
327                                        const char *addr,
328                                        uint16_t port,
329                                        struct tsocket_address **_addr,
330                                        const char *location)
331 {
332         struct addrinfo hints;
333         struct addrinfo *result = NULL;
334         char port_str[6];
335         int ret;
336
337         ZERO_STRUCT(hints);
338         /*
339          * we use SOCKET_STREAM here to get just one result
340          * back from getaddrinfo().
341          */
342         hints.ai_socktype = SOCK_STREAM;
343         hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
344
345         if (strcasecmp(fam, "ip") == 0) {
346                 hints.ai_family = AF_UNSPEC;
347                 if (!addr) {
348 #ifdef HAVE_IPV6
349                         addr = "::";
350 #else
351                         addr = "0.0.0.0";
352 #endif
353                 }
354         } else if (strcasecmp(fam, "ipv4") == 0) {
355                 hints.ai_family = AF_INET;
356                 if (!addr) {
357                         addr = "0.0.0.0";
358                 }
359 #ifdef HAVE_IPV6
360         } else if (strcasecmp(fam, "ipv6") == 0) {
361                 hints.ai_family = AF_INET6;
362                 if (!addr) {
363                         addr = "::";
364                 }
365 #endif
366         } else {
367                 errno = EAFNOSUPPORT;
368                 return -1;
369         }
370
371         snprintf(port_str, sizeof(port_str), "%u", port);
372
373         ret = getaddrinfo(addr, port_str, &hints, &result);
374         if (ret != 0) {
375                 switch (ret) {
376                 case EAI_FAIL:
377                         errno = EINVAL;
378                         break;
379                 }
380                 ret = -1;
381                 goto done;
382         }
383
384         if (result->ai_socktype != SOCK_STREAM) {
385                 errno = EINVAL;
386                 ret = -1;
387                 goto done;
388         }
389
390         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
391                                                   result->ai_addr,
392                                                   result->ai_addrlen,
393                                                   _addr,
394                                                   location);
395
396 done:
397         if (result) {
398                 freeaddrinfo(result);
399         }
400         return ret;
401 }
402
403 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
404                                        TALLOC_CTX *mem_ctx)
405 {
406         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
407                                            struct tsocket_address_bsd);
408         char addr_str[INET6_ADDRSTRLEN+1];
409         const char *str;
410
411         if (!bsda) {
412                 errno = EINVAL;
413                 return NULL;
414         }
415
416         switch (bsda->u.sa.sa_family) {
417         case AF_INET:
418                 str = inet_ntop(bsda->u.in.sin_family,
419                                 &bsda->u.in.sin_addr,
420                                 addr_str, sizeof(addr_str));
421                 break;
422 #ifdef HAVE_IPV6
423         case AF_INET6:
424                 str = inet_ntop(bsda->u.in6.sin6_family,
425                                 &bsda->u.in6.sin6_addr,
426                                 addr_str, sizeof(addr_str));
427                 break;
428 #endif
429         default:
430                 errno = EINVAL;
431                 return NULL;
432         }
433
434         if (!str) {
435                 return NULL;
436         }
437
438         return talloc_strdup(mem_ctx, str);
439 }
440
441 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
442 {
443         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
444                                            struct tsocket_address_bsd);
445         uint16_t port = 0;
446
447         if (!bsda) {
448                 errno = EINVAL;
449                 return 0;
450         }
451
452         switch (bsda->u.sa.sa_family) {
453         case AF_INET:
454                 port = ntohs(bsda->u.in.sin_port);
455                 break;
456 #ifdef HAVE_IPV6
457         case AF_INET6:
458                 port = ntohs(bsda->u.in6.sin6_port);
459                 break;
460 #endif
461         default:
462                 errno = EINVAL;
463                 return 0;
464         }
465
466         return port;
467 }
468
469 int tsocket_address_inet_set_port(struct tsocket_address *addr,
470                                   uint16_t port)
471 {
472         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
473                                            struct tsocket_address_bsd);
474
475         if (!bsda) {
476                 errno = EINVAL;
477                 return -1;
478         }
479
480         switch (bsda->u.sa.sa_family) {
481         case AF_INET:
482                 bsda->u.in.sin_port = htons(port);
483                 break;
484 #ifdef HAVE_IPV6
485         case AF_INET6:
486                 bsda->u.in6.sin6_port = htons(port);
487                 break;
488 #endif
489         default:
490                 errno = EINVAL;
491                 return -1;
492         }
493
494         return 0;
495 }
496
497 bool tsocket_address_is_unix(const struct tsocket_address *addr)
498 {
499         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
500                                            struct tsocket_address_bsd);
501
502         if (!bsda) {
503                 return false;
504         }
505
506         switch (bsda->u.sa.sa_family) {
507         case AF_UNIX:
508                 return true;
509         }
510
511         return false;
512 }
513
514 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
515                                     const char *path,
516                                     struct tsocket_address **_addr,
517                                     const char *location)
518 {
519         struct sockaddr_un un;
520         void *p = &un;
521         int ret;
522
523         if (!path) {
524                 path = "";
525         }
526
527         if (strlen(path) > sizeof(un.sun_path)-1) {
528                 errno = ENAMETOOLONG;
529                 return -1;
530         }
531
532         ZERO_STRUCT(un);
533         un.sun_family = AF_UNIX;
534         strncpy(un.sun_path, path, sizeof(un.sun_path)-1);
535
536         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
537                                                  (struct sockaddr *)p,
538                                                  sizeof(un),
539                                                  _addr,
540                                                  location);
541
542         return ret;
543 }
544
545 char *tsocket_address_unix_path(const struct tsocket_address *addr,
546                                 TALLOC_CTX *mem_ctx)
547 {
548         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
549                                            struct tsocket_address_bsd);
550         const char *str;
551
552         if (!bsda) {
553                 errno = EINVAL;
554                 return NULL;
555         }
556
557         switch (bsda->u.sa.sa_family) {
558         case AF_UNIX:
559                 str = bsda->u.un.sun_path;
560                 break;
561         default:
562                 errno = EINVAL;
563                 return NULL;
564         }
565
566         return talloc_strdup(mem_ctx, str);
567 }
568
569 static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
570                                         TALLOC_CTX *mem_ctx)
571 {
572         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
573                                            struct tsocket_address_bsd);
574         char *str;
575         char *addr_str;
576         const char *prefix = NULL;
577         uint16_t port;
578
579         switch (bsda->u.sa.sa_family) {
580         case AF_UNIX:
581                 return talloc_asprintf(mem_ctx, "unix:%s",
582                                        bsda->u.un.sun_path);
583         case AF_INET:
584                 prefix = "ipv4";
585                 break;
586 #ifdef HAVE_IPV6
587         case AF_INET6:
588                 prefix = "ipv6";
589                 break;
590 #endif
591         default:
592                 errno = EINVAL;
593                 return NULL;
594         }
595
596         addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
597         if (!addr_str) {
598                 return NULL;
599         }
600
601         port = tsocket_address_inet_port(addr);
602
603         str = talloc_asprintf(mem_ctx, "%s:%s:%u",
604                               prefix, addr_str, port);
605         talloc_free(addr_str);
606
607         return str;
608 }
609
610 static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
611                                                          TALLOC_CTX *mem_ctx,
612                                                          const char *location)
613 {
614         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
615                                            struct tsocket_address_bsd);
616         struct tsocket_address *copy;
617         int ret;
618
619         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
620                                                  &bsda->u.sa,
621                                                  bsda->sa_socklen,
622                                                  &copy,
623                                                  location);
624         if (ret != 0) {
625                 return NULL;
626         }
627
628         return copy;
629 }
630
631 static const struct tsocket_address_ops tsocket_address_bsd_ops = {
632         .name           = "bsd",
633         .string         = tsocket_address_bsd_string,
634         .copy           = tsocket_address_bsd_copy,
635 };
636
637 struct tdgram_bsd {
638         int fd;
639
640         void *event_ptr;
641         struct tevent_fd *fde;
642         bool optimize_recvfrom;
643
644         void *readable_private;
645         void (*readable_handler)(void *private_data);
646         void *writeable_private;
647         void (*writeable_handler)(void *private_data);
648 };
649
650 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context *dgram,
651                                   bool on)
652 {
653         struct tdgram_bsd *bsds =
654                 talloc_get_type(_tdgram_context_data(dgram),
655                 struct tdgram_bsd);
656         bool old;
657
658         if (bsds == NULL) {
659                 /* not a bsd socket */
660                 return false;
661         }
662
663         old = bsds->optimize_recvfrom;
664         bsds->optimize_recvfrom = on;
665
666         return old;
667 }
668
669 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
670                                    struct tevent_fd *fde,
671                                    uint16_t flags,
672                                    void *private_data)
673 {
674         struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
675                                   struct tdgram_bsd);
676
677         if (flags & TEVENT_FD_WRITE) {
678                 bsds->writeable_handler(bsds->writeable_private);
679                 return;
680         }
681         if (flags & TEVENT_FD_READ) {
682                 if (!bsds->readable_handler) {
683                         TEVENT_FD_NOT_READABLE(bsds->fde);
684                         return;
685                 }
686                 bsds->readable_handler(bsds->readable_private);
687                 return;
688         }
689 }
690
691 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
692                                            struct tevent_context *ev,
693                                            void (*handler)(void *private_data),
694                                            void *private_data)
695 {
696         if (ev == NULL) {
697                 if (handler) {
698                         errno = EINVAL;
699                         return -1;
700                 }
701                 if (!bsds->readable_handler) {
702                         return 0;
703                 }
704                 bsds->readable_handler = NULL;
705                 bsds->readable_private = NULL;
706
707                 return 0;
708         }
709
710         /* read and write must use the same tevent_context */
711         if (bsds->event_ptr != ev) {
712                 if (bsds->readable_handler || bsds->writeable_handler) {
713                         errno = EINVAL;
714                         return -1;
715                 }
716                 bsds->event_ptr = NULL;
717                 TALLOC_FREE(bsds->fde);
718         }
719
720         if (tevent_fd_get_flags(bsds->fde) == 0) {
721                 TALLOC_FREE(bsds->fde);
722
723                 bsds->fde = tevent_add_fd(ev, bsds,
724                                           bsds->fd, TEVENT_FD_READ,
725                                           tdgram_bsd_fde_handler,
726                                           bsds);
727                 if (!bsds->fde) {
728                         errno = ENOMEM;
729                         return -1;
730                 }
731
732                 /* cache the event context we're running on */
733                 bsds->event_ptr = ev;
734         } else if (!bsds->readable_handler) {
735                 TEVENT_FD_READABLE(bsds->fde);
736         }
737
738         bsds->readable_handler = handler;
739         bsds->readable_private = private_data;
740
741         return 0;
742 }
743
744 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
745                                             struct tevent_context *ev,
746                                             void (*handler)(void *private_data),
747                                             void *private_data)
748 {
749         if (ev == NULL) {
750                 if (handler) {
751                         errno = EINVAL;
752                         return -1;
753                 }
754                 if (!bsds->writeable_handler) {
755                         return 0;
756                 }
757                 bsds->writeable_handler = NULL;
758                 bsds->writeable_private = NULL;
759                 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
760
761                 return 0;
762         }
763
764         /* read and write must use the same tevent_context */
765         if (bsds->event_ptr != ev) {
766                 if (bsds->readable_handler || bsds->writeable_handler) {
767                         errno = EINVAL;
768                         return -1;
769                 }
770                 bsds->event_ptr = NULL;
771                 TALLOC_FREE(bsds->fde);
772         }
773
774         if (tevent_fd_get_flags(bsds->fde) == 0) {
775                 TALLOC_FREE(bsds->fde);
776
777                 bsds->fde = tevent_add_fd(ev, bsds,
778                                           bsds->fd, TEVENT_FD_WRITE,
779                                           tdgram_bsd_fde_handler,
780                                           bsds);
781                 if (!bsds->fde) {
782                         errno = ENOMEM;
783                         return -1;
784                 }
785
786                 /* cache the event context we're running on */
787                 bsds->event_ptr = ev;
788         } else if (!bsds->writeable_handler) {
789                 TEVENT_FD_WRITEABLE(bsds->fde);
790         }
791
792         bsds->writeable_handler = handler;
793         bsds->writeable_private = private_data;
794
795         return 0;
796 }
797
798 struct tdgram_bsd_recvfrom_state {
799         struct tdgram_context *dgram;
800         bool first_try;
801         uint8_t *buf;
802         size_t len;
803         struct tsocket_address *src;
804 };
805
806 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
807 {
808         struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
809                                   struct tdgram_bsd);
810
811         tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
812
813         return 0;
814 }
815
816 static void tdgram_bsd_recvfrom_handler(void *private_data);
817
818 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
819                                         struct tevent_context *ev,
820                                         struct tdgram_context *dgram)
821 {
822         struct tevent_req *req;
823         struct tdgram_bsd_recvfrom_state *state;
824         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
825         int ret;
826
827         req = tevent_req_create(mem_ctx, &state,
828                                 struct tdgram_bsd_recvfrom_state);
829         if (!req) {
830                 return NULL;
831         }
832
833         state->dgram    = dgram;
834         state->first_try= true;
835         state->buf      = NULL;
836         state->len      = 0;
837         state->src      = NULL;
838
839         talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
840
841         if (bsds->fd == -1) {
842                 tevent_req_error(req, ENOTCONN);
843                 goto post;
844         }
845
846
847         /*
848          * this is a fast path, not waiting for the
849          * socket to become explicit readable gains
850          * about 10%-20% performance in benchmark tests.
851          */
852         if (bsds->optimize_recvfrom) {
853                 /*
854                  * We only do the optimization on
855                  * recvfrom if the caller asked for it.
856                  *
857                  * This is needed because in most cases
858                  * we preferr to flush send buffers before
859                  * receiving incoming requests.
860                  */
861                 tdgram_bsd_recvfrom_handler(req);
862                 if (!tevent_req_is_in_progress(req)) {
863                         goto post;
864                 }
865         }
866
867         ret = tdgram_bsd_set_readable_handler(bsds, ev,
868                                               tdgram_bsd_recvfrom_handler,
869                                               req);
870         if (ret == -1) {
871                 tevent_req_error(req, errno);
872                 goto post;
873         }
874
875         return req;
876
877  post:
878         tevent_req_post(req, ev);
879         return req;
880 }
881
882 static void tdgram_bsd_recvfrom_handler(void *private_data)
883 {
884         struct tevent_req *req = talloc_get_type_abort(private_data,
885                                  struct tevent_req);
886         struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
887                                         struct tdgram_bsd_recvfrom_state);
888         struct tdgram_context *dgram = state->dgram;
889         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
890         struct tsocket_address_bsd *bsda;
891         ssize_t ret;
892         int err;
893         bool retry;
894
895         ret = tsocket_bsd_pending(bsds->fd);
896         if (state->first_try && ret == 0) {
897                 state->first_try = false;
898                 /* retry later */
899                 return;
900         }
901         state->first_try = false;
902
903         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
904         if (retry) {
905                 /* retry later */
906                 return;
907         }
908         if (tevent_req_error(req, err)) {
909                 return;
910         }
911
912         /* note that 'ret' can be 0 here */
913         state->buf = talloc_array(state, uint8_t, ret);
914         if (tevent_req_nomem(state->buf, req)) {
915                 return;
916         }
917         state->len = ret;
918
919         state->src = tsocket_address_create(state,
920                                             &tsocket_address_bsd_ops,
921                                             &bsda,
922                                             struct tsocket_address_bsd,
923                                             __location__ "bsd_recvfrom");
924         if (tevent_req_nomem(state->src, req)) {
925                 return;
926         }
927
928         ZERO_STRUCTP(bsda);
929         bsda->sa_socklen = sizeof(bsda->u.ss);
930 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
931         bsda->u.sa.sa_len = bsda->sa_socklen;
932 #endif
933
934         ret = recvfrom(bsds->fd, state->buf, state->len, 0,
935                        &bsda->u.sa, &bsda->sa_socklen);
936         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
937         if (retry) {
938                 /* retry later */
939                 return;
940         }
941         if (tevent_req_error(req, err)) {
942                 return;
943         }
944
945         /*
946          * Some systems (FreeBSD, see bug #7115) return too much
947          * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
948          * the return value includes some IP/UDP header bytes,
949          * while recvfrom() just returns the payload.
950          */
951         state->buf = talloc_realloc(state, state->buf, uint8_t, ret);
952         if (tevent_req_nomem(state->buf, req)) {
953                 return;
954         }
955         state->len = ret;
956
957         tevent_req_done(req);
958 }
959
960 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
961                                         int *perrno,
962                                         TALLOC_CTX *mem_ctx,
963                                         uint8_t **buf,
964                                         struct tsocket_address **src)
965 {
966         struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
967                                         struct tdgram_bsd_recvfrom_state);
968         ssize_t ret;
969
970         ret = tsocket_simple_int_recv(req, perrno);
971         if (ret == 0) {
972                 *buf = talloc_move(mem_ctx, &state->buf);
973                 ret = state->len;
974                 if (src) {
975                         *src = talloc_move(mem_ctx, &state->src);
976                 }
977         }
978
979         tevent_req_received(req);
980         return ret;
981 }
982
983 struct tdgram_bsd_sendto_state {
984         struct tdgram_context *dgram;
985
986         const uint8_t *buf;
987         size_t len;
988         const struct tsocket_address *dst;
989
990         ssize_t ret;
991 };
992
993 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
994 {
995         struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
996                                   struct tdgram_bsd);
997
998         tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
999
1000         return 0;
1001 }
1002
1003 static void tdgram_bsd_sendto_handler(void *private_data);
1004
1005 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
1006                                                  struct tevent_context *ev,
1007                                                  struct tdgram_context *dgram,
1008                                                  const uint8_t *buf,
1009                                                  size_t len,
1010                                                  const struct tsocket_address *dst)
1011 {
1012         struct tevent_req *req;
1013         struct tdgram_bsd_sendto_state *state;
1014         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1015         int ret;
1016
1017         req = tevent_req_create(mem_ctx, &state,
1018                                 struct tdgram_bsd_sendto_state);
1019         if (!req) {
1020                 return NULL;
1021         }
1022
1023         state->dgram    = dgram;
1024         state->buf      = buf;
1025         state->len      = len;
1026         state->dst      = dst;
1027         state->ret      = -1;
1028
1029         talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
1030
1031         if (bsds->fd == -1) {
1032                 tevent_req_error(req, ENOTCONN);
1033                 goto post;
1034         }
1035
1036         /*
1037          * this is a fast path, not waiting for the
1038          * socket to become explicit writeable gains
1039          * about 10%-20% performance in benchmark tests.
1040          */
1041         tdgram_bsd_sendto_handler(req);
1042         if (!tevent_req_is_in_progress(req)) {
1043                 goto post;
1044         }
1045
1046         ret = tdgram_bsd_set_writeable_handler(bsds, ev,
1047                                                tdgram_bsd_sendto_handler,
1048                                                req);
1049         if (ret == -1) {
1050                 tevent_req_error(req, errno);
1051                 goto post;
1052         }
1053
1054         return req;
1055
1056  post:
1057         tevent_req_post(req, ev);
1058         return req;
1059 }
1060
1061 static void tdgram_bsd_sendto_handler(void *private_data)
1062 {
1063         struct tevent_req *req = talloc_get_type_abort(private_data,
1064                                  struct tevent_req);
1065         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1066                                         struct tdgram_bsd_sendto_state);
1067         struct tdgram_context *dgram = state->dgram;
1068         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1069         struct sockaddr *sa = NULL;
1070         socklen_t sa_socklen = 0;
1071         ssize_t ret;
1072         int err;
1073         bool retry;
1074
1075         if (state->dst) {
1076                 struct tsocket_address_bsd *bsda =
1077                         talloc_get_type(state->dst->private_data,
1078                         struct tsocket_address_bsd);
1079
1080                 sa = &bsda->u.sa;
1081                 sa_socklen = bsda->sa_socklen;
1082         }
1083
1084         ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_socklen);
1085         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1086         if (retry) {
1087                 /* retry later */
1088                 return;
1089         }
1090
1091         if (err == EMSGSIZE) {
1092                 /* round up in 1K increments */
1093                 int bufsize = ((state->len + 1023) & (~1023));
1094
1095                 ret = setsockopt(bsds->fd, SOL_SOCKET, SO_SNDBUF, &bufsize,
1096                                  sizeof(bufsize));
1097                 if (ret == 0) {
1098                         /*
1099                          * We do the retry here, rather then via the
1100                          * handler, as we only want to retry once for
1101                          * this condition, so if there is a mismatch
1102                          * between what setsockopt() accepts and what can
1103                          * actually be sent, we do not end up in a
1104                          * loop.
1105                          */
1106
1107                         ret = sendto(bsds->fd, state->buf, state->len,
1108                                      0, sa, sa_socklen);
1109                         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1110                         if (retry) { /* retry later */
1111                                 return;
1112                         }
1113                 }
1114         }
1115
1116         if (tevent_req_error(req, err)) {
1117                 return;
1118         }
1119
1120         state->ret = ret;
1121
1122         tevent_req_done(req);
1123 }
1124
1125 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1126 {
1127         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1128                                         struct tdgram_bsd_sendto_state);
1129         ssize_t ret;
1130
1131         ret = tsocket_simple_int_recv(req, perrno);
1132         if (ret == 0) {
1133                 ret = state->ret;
1134         }
1135
1136         tevent_req_received(req);
1137         return ret;
1138 }
1139
1140 struct tdgram_bsd_disconnect_state {
1141         uint8_t __dummy;
1142 };
1143
1144 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1145                                                      struct tevent_context *ev,
1146                                                      struct tdgram_context *dgram)
1147 {
1148         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1149         struct tevent_req *req;
1150         struct tdgram_bsd_disconnect_state *state;
1151         int ret;
1152         int err;
1153         bool dummy;
1154
1155         req = tevent_req_create(mem_ctx, &state,
1156                                 struct tdgram_bsd_disconnect_state);
1157         if (req == NULL) {
1158                 return NULL;
1159         }
1160
1161         if (bsds->fd == -1) {
1162                 tevent_req_error(req, ENOTCONN);
1163                 goto post;
1164         }
1165
1166         TALLOC_FREE(bsds->fde);
1167         ret = close(bsds->fd);
1168         bsds->fd = -1;
1169         err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1170         if (tevent_req_error(req, err)) {
1171                 goto post;
1172         }
1173
1174         tevent_req_done(req);
1175 post:
1176         tevent_req_post(req, ev);
1177         return req;
1178 }
1179
1180 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1181                                       int *perrno)
1182 {
1183         int ret;
1184
1185         ret = tsocket_simple_int_recv(req, perrno);
1186
1187         tevent_req_received(req);
1188         return ret;
1189 }
1190
1191 static const struct tdgram_context_ops tdgram_bsd_ops = {
1192         .name                   = "bsd",
1193
1194         .recvfrom_send          = tdgram_bsd_recvfrom_send,
1195         .recvfrom_recv          = tdgram_bsd_recvfrom_recv,
1196
1197         .sendto_send            = tdgram_bsd_sendto_send,
1198         .sendto_recv            = tdgram_bsd_sendto_recv,
1199
1200         .disconnect_send        = tdgram_bsd_disconnect_send,
1201         .disconnect_recv        = tdgram_bsd_disconnect_recv,
1202 };
1203
1204 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1205 {
1206         TALLOC_FREE(bsds->fde);
1207         if (bsds->fd != -1) {
1208                 close(bsds->fd);
1209                 bsds->fd = -1;
1210         }
1211         return 0;
1212 }
1213
1214 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1215                                    const struct tsocket_address *remote,
1216                                    bool broadcast,
1217                                    TALLOC_CTX *mem_ctx,
1218                                    struct tdgram_context **_dgram,
1219                                    const char *location)
1220 {
1221         struct tsocket_address_bsd *lbsda =
1222                 talloc_get_type_abort(local->private_data,
1223                 struct tsocket_address_bsd);
1224         struct tsocket_address_bsd *rbsda = NULL;
1225         struct tdgram_context *dgram;
1226         struct tdgram_bsd *bsds;
1227         int fd;
1228         int ret;
1229         bool do_bind = false;
1230         bool do_reuseaddr = false;
1231         bool do_ipv6only = false;
1232         bool is_inet = false;
1233         int sa_fam = lbsda->u.sa.sa_family;
1234
1235         if (remote) {
1236                 rbsda = talloc_get_type_abort(remote->private_data,
1237                         struct tsocket_address_bsd);
1238         }
1239
1240         switch (lbsda->u.sa.sa_family) {
1241         case AF_UNIX:
1242                 if (broadcast) {
1243                         errno = EINVAL;
1244                         return -1;
1245                 }
1246                 if (lbsda->u.un.sun_path[0] != 0) {
1247                         do_reuseaddr = true;
1248                         do_bind = true;
1249                 }
1250                 break;
1251         case AF_INET:
1252                 if (lbsda->u.in.sin_port != 0) {
1253                         do_reuseaddr = true;
1254                         do_bind = true;
1255                 }
1256                 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1257                         do_bind = true;
1258                 }
1259                 is_inet = true;
1260                 break;
1261 #ifdef HAVE_IPV6
1262         case AF_INET6:
1263                 if (lbsda->u.in6.sin6_port != 0) {
1264                         do_reuseaddr = true;
1265                         do_bind = true;
1266                 }
1267                 if (memcmp(&in6addr_any,
1268                            &lbsda->u.in6.sin6_addr,
1269                            sizeof(in6addr_any)) != 0) {
1270                         do_bind = true;
1271                 }
1272                 is_inet = true;
1273                 do_ipv6only = true;
1274                 break;
1275 #endif
1276         default:
1277                 errno = EINVAL;
1278                 return -1;
1279         }
1280
1281         if (!do_bind && is_inet && rbsda) {
1282                 sa_fam = rbsda->u.sa.sa_family;
1283                 switch (sa_fam) {
1284                 case AF_INET:
1285                         do_ipv6only = false;
1286                         break;
1287 #ifdef HAVE_IPV6
1288                 case AF_INET6:
1289                         do_ipv6only = true;
1290                         break;
1291 #endif
1292                 }
1293         }
1294
1295         fd = socket(sa_fam, SOCK_DGRAM, 0);
1296         if (fd < 0) {
1297                 return -1;
1298         }
1299
1300         fd = tsocket_bsd_common_prepare_fd(fd, true);
1301         if (fd < 0) {
1302                 return -1;
1303         }
1304
1305         dgram = tdgram_context_create(mem_ctx,
1306                                       &tdgram_bsd_ops,
1307                                       &bsds,
1308                                       struct tdgram_bsd,
1309                                       location);
1310         if (!dgram) {
1311                 int saved_errno = errno;
1312                 close(fd);
1313                 errno = saved_errno;
1314                 return -1;
1315         }
1316         ZERO_STRUCTP(bsds);
1317         bsds->fd = fd;
1318         talloc_set_destructor(bsds, tdgram_bsd_destructor);
1319
1320 #ifdef HAVE_IPV6
1321         if (do_ipv6only) {
1322                 int val = 1;
1323
1324                 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1325                                  (const void *)&val, sizeof(val));
1326                 if (ret == -1) {
1327                         int saved_errno = errno;
1328                         talloc_free(dgram);
1329                         errno = saved_errno;
1330                         return -1;
1331                 }
1332         }
1333 #endif
1334
1335         if (broadcast) {
1336                 int val = 1;
1337
1338                 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1339                                  (const void *)&val, sizeof(val));
1340                 if (ret == -1) {
1341                         int saved_errno = errno;
1342                         talloc_free(dgram);
1343                         errno = saved_errno;
1344                         return -1;
1345                 }
1346         }
1347
1348         if (do_reuseaddr) {
1349                 int val = 1;
1350
1351                 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1352                                  (const void *)&val, sizeof(val));
1353                 if (ret == -1) {
1354                         int saved_errno = errno;
1355                         talloc_free(dgram);
1356                         errno = saved_errno;
1357                         return -1;
1358                 }
1359         }
1360
1361         if (do_bind) {
1362                 ret = bind(fd, &lbsda->u.sa, lbsda->sa_socklen);
1363                 if (ret == -1) {
1364                         int saved_errno = errno;
1365                         talloc_free(dgram);
1366                         errno = saved_errno;
1367                         return -1;
1368                 }
1369         }
1370
1371         if (rbsda) {
1372                 if (rbsda->u.sa.sa_family != sa_fam) {
1373                         talloc_free(dgram);
1374                         errno = EINVAL;
1375                         return -1;
1376                 }
1377
1378                 ret = connect(fd, &rbsda->u.sa, rbsda->sa_socklen);
1379                 if (ret == -1) {
1380                         int saved_errno = errno;
1381                         talloc_free(dgram);
1382                         errno = saved_errno;
1383                         return -1;
1384                 }
1385         }
1386
1387         *_dgram = dgram;
1388         return 0;
1389 }
1390
1391 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1392                             const struct tsocket_address *remote,
1393                             TALLOC_CTX *mem_ctx,
1394                             struct tdgram_context **dgram,
1395                             const char *location)
1396 {
1397         struct tsocket_address_bsd *lbsda =
1398                 talloc_get_type_abort(local->private_data,
1399                 struct tsocket_address_bsd);
1400         int ret;
1401
1402         switch (lbsda->u.sa.sa_family) {
1403         case AF_INET:
1404                 break;
1405 #ifdef HAVE_IPV6
1406         case AF_INET6:
1407                 break;
1408 #endif
1409         default:
1410                 errno = EINVAL;
1411                 return -1;
1412         }
1413
1414         ret = tdgram_bsd_dgram_socket(local, remote, false,
1415                                       mem_ctx, dgram, location);
1416
1417         return ret;
1418 }
1419
1420 int _tdgram_unix_socket(const struct tsocket_address *local,
1421                         const struct tsocket_address *remote,
1422                         TALLOC_CTX *mem_ctx,
1423                         struct tdgram_context **dgram,
1424                         const char *location)
1425 {
1426         struct tsocket_address_bsd *lbsda =
1427                 talloc_get_type_abort(local->private_data,
1428                 struct tsocket_address_bsd);
1429         int ret;
1430
1431         switch (lbsda->u.sa.sa_family) {
1432         case AF_UNIX:
1433                 break;
1434         default:
1435                 errno = EINVAL;
1436                 return -1;
1437         }
1438
1439         ret = tdgram_bsd_dgram_socket(local, remote, false,
1440                                       mem_ctx, dgram, location);
1441
1442         return ret;
1443 }
1444
1445 struct tstream_bsd {
1446         int fd;
1447
1448         void *event_ptr;
1449         struct tevent_fd *fde;
1450         bool optimize_readv;
1451
1452         void *readable_private;
1453         void (*readable_handler)(void *private_data);
1454         void *writeable_private;
1455         void (*writeable_handler)(void *private_data);
1456 };
1457
1458 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
1459                                 bool on)
1460 {
1461         struct tstream_bsd *bsds =
1462                 talloc_get_type(_tstream_context_data(stream),
1463                 struct tstream_bsd);
1464         bool old;
1465
1466         if (bsds == NULL) {
1467                 /* not a bsd socket */
1468                 return false;
1469         }
1470
1471         old = bsds->optimize_readv;
1472         bsds->optimize_readv = on;
1473
1474         return old;
1475 }
1476
1477 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1478                                     struct tevent_fd *fde,
1479                                     uint16_t flags,
1480                                     void *private_data)
1481 {
1482         struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1483                                    struct tstream_bsd);
1484
1485         if (flags & TEVENT_FD_WRITE) {
1486                 bsds->writeable_handler(bsds->writeable_private);
1487                 return;
1488         }
1489         if (flags & TEVENT_FD_READ) {
1490                 if (!bsds->readable_handler) {
1491                         if (bsds->writeable_handler) {
1492                                 bsds->writeable_handler(bsds->writeable_private);
1493                                 return;
1494                         }
1495                         TEVENT_FD_NOT_READABLE(bsds->fde);
1496                         return;
1497                 }
1498                 bsds->readable_handler(bsds->readable_private);
1499                 return;
1500         }
1501 }
1502
1503 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1504                                             struct tevent_context *ev,
1505                                             void (*handler)(void *private_data),
1506                                             void *private_data)
1507 {
1508         if (ev == NULL) {
1509                 if (handler) {
1510                         errno = EINVAL;
1511                         return -1;
1512                 }
1513                 if (!bsds->readable_handler) {
1514                         return 0;
1515                 }
1516                 bsds->readable_handler = NULL;
1517                 bsds->readable_private = NULL;
1518
1519                 return 0;
1520         }
1521
1522         /* read and write must use the same tevent_context */
1523         if (bsds->event_ptr != ev) {
1524                 if (bsds->readable_handler || bsds->writeable_handler) {
1525                         errno = EINVAL;
1526                         return -1;
1527                 }
1528                 bsds->event_ptr = NULL;
1529                 TALLOC_FREE(bsds->fde);
1530         }
1531
1532         if (tevent_fd_get_flags(bsds->fde) == 0) {
1533                 TALLOC_FREE(bsds->fde);
1534
1535                 bsds->fde = tevent_add_fd(ev, bsds,
1536                                           bsds->fd, TEVENT_FD_READ,
1537                                           tstream_bsd_fde_handler,
1538                                           bsds);
1539                 if (!bsds->fde) {
1540                         errno = ENOMEM;
1541                         return -1;
1542                 }
1543
1544                 /* cache the event context we're running on */
1545                 bsds->event_ptr = ev;
1546         } else if (!bsds->readable_handler) {
1547                 TEVENT_FD_READABLE(bsds->fde);
1548         }
1549
1550         bsds->readable_handler = handler;
1551         bsds->readable_private = private_data;
1552
1553         return 0;
1554 }
1555
1556 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1557                                              struct tevent_context *ev,
1558                                              void (*handler)(void *private_data),
1559                                              void *private_data)
1560 {
1561         if (ev == NULL) {
1562                 if (handler) {
1563                         errno = EINVAL;
1564                         return -1;
1565                 }
1566                 if (!bsds->writeable_handler) {
1567                         return 0;
1568                 }
1569                 bsds->writeable_handler = NULL;
1570                 bsds->writeable_private = NULL;
1571                 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1572
1573                 return 0;
1574         }
1575
1576         /* read and write must use the same tevent_context */
1577         if (bsds->event_ptr != ev) {
1578                 if (bsds->readable_handler || bsds->writeable_handler) {
1579                         errno = EINVAL;
1580                         return -1;
1581                 }
1582                 bsds->event_ptr = NULL;
1583                 TALLOC_FREE(bsds->fde);
1584         }
1585
1586         if (tevent_fd_get_flags(bsds->fde) == 0) {
1587                 TALLOC_FREE(bsds->fde);
1588
1589                 bsds->fde = tevent_add_fd(ev, bsds,
1590                                           bsds->fd,
1591                                           TEVENT_FD_READ | TEVENT_FD_WRITE,
1592                                           tstream_bsd_fde_handler,
1593                                           bsds);
1594                 if (!bsds->fde) {
1595                         errno = ENOMEM;
1596                         return -1;
1597                 }
1598
1599                 /* cache the event context we're running on */
1600                 bsds->event_ptr = ev;
1601         } else if (!bsds->writeable_handler) {
1602                 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1603                 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1604                 tevent_fd_set_flags(bsds->fde, flags);
1605         }
1606
1607         bsds->writeable_handler = handler;
1608         bsds->writeable_private = private_data;
1609
1610         return 0;
1611 }
1612
1613 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1614 {
1615         struct tstream_bsd *bsds = tstream_context_data(stream,
1616                                    struct tstream_bsd);
1617         ssize_t ret;
1618
1619         if (bsds->fd == -1) {
1620                 errno = ENOTCONN;
1621                 return -1;
1622         }
1623
1624         ret = tsocket_bsd_pending(bsds->fd);
1625
1626         return ret;
1627 }
1628
1629 struct tstream_bsd_readv_state {
1630         struct tstream_context *stream;
1631
1632         struct iovec *vector;
1633         size_t count;
1634
1635         int ret;
1636 };
1637
1638 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1639 {
1640         struct tstream_bsd *bsds = tstream_context_data(state->stream,
1641                                    struct tstream_bsd);
1642
1643         tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1644
1645         return 0;
1646 }
1647
1648 static void tstream_bsd_readv_handler(void *private_data);
1649
1650 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1651                                         struct tevent_context *ev,
1652                                         struct tstream_context *stream,
1653                                         struct iovec *vector,
1654                                         size_t count)
1655 {
1656         struct tevent_req *req;
1657         struct tstream_bsd_readv_state *state;
1658         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1659         int ret;
1660
1661         req = tevent_req_create(mem_ctx, &state,
1662                                 struct tstream_bsd_readv_state);
1663         if (!req) {
1664                 return NULL;
1665         }
1666
1667         state->stream   = stream;
1668         /* we make a copy of the vector so that we can modify it */
1669         state->vector   = talloc_array(state, struct iovec, count);
1670         if (tevent_req_nomem(state->vector, req)) {
1671                 goto post;
1672         }
1673         memcpy(state->vector, vector, sizeof(struct iovec)*count);
1674         state->count    = count;
1675         state->ret      = 0;
1676
1677         talloc_set_destructor(state, tstream_bsd_readv_destructor);
1678
1679         if (bsds->fd == -1) {
1680                 tevent_req_error(req, ENOTCONN);
1681                 goto post;
1682         }
1683
1684         /*
1685          * this is a fast path, not waiting for the
1686          * socket to become explicit readable gains
1687          * about 10%-20% performance in benchmark tests.
1688          */
1689         if (bsds->optimize_readv) {
1690                 /*
1691                  * We only do the optimization on
1692                  * readv if the caller asked for it.
1693                  *
1694                  * This is needed because in most cases
1695                  * we preferr to flush send buffers before
1696                  * receiving incoming requests.
1697                  */
1698                 tstream_bsd_readv_handler(req);
1699                 if (!tevent_req_is_in_progress(req)) {
1700                         goto post;
1701                 }
1702         }
1703
1704         ret = tstream_bsd_set_readable_handler(bsds, ev,
1705                                               tstream_bsd_readv_handler,
1706                                               req);
1707         if (ret == -1) {
1708                 tevent_req_error(req, errno);
1709                 goto post;
1710         }
1711
1712         return req;
1713
1714  post:
1715         tevent_req_post(req, ev);
1716         return req;
1717 }
1718
1719 static void tstream_bsd_readv_handler(void *private_data)
1720 {
1721         struct tevent_req *req = talloc_get_type_abort(private_data,
1722                                  struct tevent_req);
1723         struct tstream_bsd_readv_state *state = tevent_req_data(req,
1724                                         struct tstream_bsd_readv_state);
1725         struct tstream_context *stream = state->stream;
1726         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1727         int ret;
1728         int err;
1729         int _count;
1730         bool ok, retry;
1731
1732         ret = readv(bsds->fd, state->vector, state->count);
1733         if (ret == 0) {
1734                 /* propagate end of file */
1735                 tevent_req_error(req, EPIPE);
1736                 return;
1737         }
1738         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1739         if (retry) {
1740                 /* retry later */
1741                 return;
1742         }
1743         if (tevent_req_error(req, err)) {
1744                 return;
1745         }
1746
1747         state->ret += ret;
1748
1749         _count = state->count; /* tstream has size_t count, readv has int */
1750         ok = iov_advance(&state->vector, &_count, ret);
1751         state->count = _count;
1752
1753         if (!ok) {
1754                 tevent_req_error(req, EINVAL);
1755                 return;
1756         }
1757
1758         if (state->count > 0) {
1759                 /* we have more to read */
1760                 return;
1761         }
1762
1763         tevent_req_done(req);
1764 }
1765
1766 static int tstream_bsd_readv_recv(struct tevent_req *req,
1767                                   int *perrno)
1768 {
1769         struct tstream_bsd_readv_state *state = tevent_req_data(req,
1770                                         struct tstream_bsd_readv_state);
1771         int ret;
1772
1773         ret = tsocket_simple_int_recv(req, perrno);
1774         if (ret == 0) {
1775                 ret = state->ret;
1776         }
1777
1778         tevent_req_received(req);
1779         return ret;
1780 }
1781
1782 struct tstream_bsd_writev_state {
1783         struct tstream_context *stream;
1784
1785         struct iovec *vector;
1786         size_t count;
1787
1788         int ret;
1789 };
1790
1791 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1792 {
1793         struct tstream_bsd *bsds = tstream_context_data(state->stream,
1794                                   struct tstream_bsd);
1795
1796         tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1797
1798         return 0;
1799 }
1800
1801 static void tstream_bsd_writev_handler(void *private_data);
1802
1803 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1804                                                  struct tevent_context *ev,
1805                                                  struct tstream_context *stream,
1806                                                  const struct iovec *vector,
1807                                                  size_t count)
1808 {
1809         struct tevent_req *req;
1810         struct tstream_bsd_writev_state *state;
1811         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1812         int ret;
1813
1814         req = tevent_req_create(mem_ctx, &state,
1815                                 struct tstream_bsd_writev_state);
1816         if (!req) {
1817                 return NULL;
1818         }
1819
1820         state->stream   = stream;
1821         /* we make a copy of the vector so that we can modify it */
1822         state->vector   = talloc_array(state, struct iovec, count);
1823         if (tevent_req_nomem(state->vector, req)) {
1824                 goto post;
1825         }
1826         memcpy(state->vector, vector, sizeof(struct iovec)*count);
1827         state->count    = count;
1828         state->ret      = 0;
1829
1830         talloc_set_destructor(state, tstream_bsd_writev_destructor);
1831
1832         if (bsds->fd == -1) {
1833                 tevent_req_error(req, ENOTCONN);
1834                 goto post;
1835         }
1836
1837         /*
1838          * this is a fast path, not waiting for the
1839          * socket to become explicit writeable gains
1840          * about 10%-20% performance in benchmark tests.
1841          */
1842         tstream_bsd_writev_handler(req);
1843         if (!tevent_req_is_in_progress(req)) {
1844                 goto post;
1845         }
1846
1847         ret = tstream_bsd_set_writeable_handler(bsds, ev,
1848                                                tstream_bsd_writev_handler,
1849                                                req);
1850         if (ret == -1) {
1851                 tevent_req_error(req, errno);
1852                 goto post;
1853         }
1854
1855         return req;
1856
1857  post:
1858         tevent_req_post(req, ev);
1859         return req;
1860 }
1861
1862 static void tstream_bsd_writev_handler(void *private_data)
1863 {
1864         struct tevent_req *req = talloc_get_type_abort(private_data,
1865                                  struct tevent_req);
1866         struct tstream_bsd_writev_state *state = tevent_req_data(req,
1867                                         struct tstream_bsd_writev_state);
1868         struct tstream_context *stream = state->stream;
1869         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1870         ssize_t ret;
1871         int err;
1872         int _count;
1873         bool ok, retry;
1874
1875         ret = writev(bsds->fd, state->vector, state->count);
1876         if (ret == 0) {
1877                 /* propagate end of file */
1878                 tevent_req_error(req, EPIPE);
1879                 return;
1880         }
1881         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1882         if (retry) {
1883                 /* retry later */
1884                 return;
1885         }
1886         if (tevent_req_error(req, err)) {
1887                 return;
1888         }
1889
1890         state->ret += ret;
1891
1892         _count = state->count; /* tstream has size_t count, writev has int */
1893         ok = iov_advance(&state->vector, &_count, ret);
1894         state->count = _count;
1895
1896         if (!ok) {
1897                 tevent_req_error(req, EINVAL);
1898                 return;
1899         }
1900
1901         if (state->count > 0) {
1902                 /* we have more to read */
1903                 return;
1904         }
1905
1906         tevent_req_done(req);
1907 }
1908
1909 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1910 {
1911         struct tstream_bsd_writev_state *state = tevent_req_data(req,
1912                                         struct tstream_bsd_writev_state);
1913         int ret;
1914
1915         ret = tsocket_simple_int_recv(req, perrno);
1916         if (ret == 0) {
1917                 ret = state->ret;
1918         }
1919
1920         tevent_req_received(req);
1921         return ret;
1922 }
1923
1924 struct tstream_bsd_disconnect_state {
1925         void *__dummy;
1926 };
1927
1928 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1929                                                      struct tevent_context *ev,
1930                                                      struct tstream_context *stream)
1931 {
1932         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1933         struct tevent_req *req;
1934         struct tstream_bsd_disconnect_state *state;
1935         int ret;
1936         int err;
1937         bool dummy;
1938
1939         req = tevent_req_create(mem_ctx, &state,
1940                                 struct tstream_bsd_disconnect_state);
1941         if (req == NULL) {
1942                 return NULL;
1943         }
1944
1945         if (bsds->fd == -1) {
1946                 tevent_req_error(req, ENOTCONN);
1947                 goto post;
1948         }
1949
1950         TALLOC_FREE(bsds->fde);
1951         ret = close(bsds->fd);
1952         bsds->fd = -1;
1953         err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1954         if (tevent_req_error(req, err)) {
1955                 goto post;
1956         }
1957
1958         tevent_req_done(req);
1959 post:
1960         tevent_req_post(req, ev);
1961         return req;
1962 }
1963
1964 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
1965                                       int *perrno)
1966 {
1967         int ret;
1968
1969         ret = tsocket_simple_int_recv(req, perrno);
1970
1971         tevent_req_received(req);
1972         return ret;
1973 }
1974
1975 static const struct tstream_context_ops tstream_bsd_ops = {
1976         .name                   = "bsd",
1977
1978         .pending_bytes          = tstream_bsd_pending_bytes,
1979
1980         .readv_send             = tstream_bsd_readv_send,
1981         .readv_recv             = tstream_bsd_readv_recv,
1982
1983         .writev_send            = tstream_bsd_writev_send,
1984         .writev_recv            = tstream_bsd_writev_recv,
1985
1986         .disconnect_send        = tstream_bsd_disconnect_send,
1987         .disconnect_recv        = tstream_bsd_disconnect_recv,
1988 };
1989
1990 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
1991 {
1992         TALLOC_FREE(bsds->fde);
1993         if (bsds->fd != -1) {
1994                 close(bsds->fd);
1995                 bsds->fd = -1;
1996         }
1997         return 0;
1998 }
1999
2000 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
2001                                  int fd,
2002                                  struct tstream_context **_stream,
2003                                  const char *location)
2004 {
2005         struct tstream_context *stream;
2006         struct tstream_bsd *bsds;
2007
2008         stream = tstream_context_create(mem_ctx,
2009                                         &tstream_bsd_ops,
2010                                         &bsds,
2011                                         struct tstream_bsd,
2012                                         location);
2013         if (!stream) {
2014                 return -1;
2015         }
2016         ZERO_STRUCTP(bsds);
2017         bsds->fd = fd;
2018         talloc_set_destructor(bsds, tstream_bsd_destructor);
2019
2020         *_stream = stream;
2021         return 0;
2022 }
2023
2024 struct tstream_bsd_connect_state {
2025         int fd;
2026         struct tevent_fd *fde;
2027         struct tstream_conext *stream;
2028         struct tsocket_address *local;
2029 };
2030
2031 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
2032 {
2033         TALLOC_FREE(state->fde);
2034         if (state->fd != -1) {
2035                 close(state->fd);
2036                 state->fd = -1;
2037         }
2038
2039         return 0;
2040 }
2041
2042 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2043                                             struct tevent_fd *fde,
2044                                             uint16_t flags,
2045                                             void *private_data);
2046
2047 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2048                                         struct tevent_context *ev,
2049                                         int sys_errno,
2050                                         const struct tsocket_address *local,
2051                                         const struct tsocket_address *remote)
2052 {
2053         struct tevent_req *req;
2054         struct tstream_bsd_connect_state *state;
2055         struct tsocket_address_bsd *lbsda =
2056                 talloc_get_type_abort(local->private_data,
2057                 struct tsocket_address_bsd);
2058         struct tsocket_address_bsd *lrbsda = NULL;
2059         struct tsocket_address_bsd *rbsda =
2060                 talloc_get_type_abort(remote->private_data,
2061                 struct tsocket_address_bsd);
2062         int ret;
2063         int err;
2064         bool retry;
2065         bool do_bind = false;
2066         bool do_reuseaddr = false;
2067         bool do_ipv6only = false;
2068         bool is_inet = false;
2069         int sa_fam = lbsda->u.sa.sa_family;
2070
2071         req = tevent_req_create(mem_ctx, &state,
2072                                 struct tstream_bsd_connect_state);
2073         if (!req) {
2074                 return NULL;
2075         }
2076         state->fd = -1;
2077         state->fde = NULL;
2078
2079         talloc_set_destructor(state, tstream_bsd_connect_destructor);
2080
2081         /* give the wrappers a chance to report an error */
2082         if (sys_errno != 0) {
2083                 tevent_req_error(req, sys_errno);
2084                 goto post;
2085         }
2086
2087         switch (lbsda->u.sa.sa_family) {
2088         case AF_UNIX:
2089                 if (lbsda->u.un.sun_path[0] != 0) {
2090                         do_reuseaddr = true;
2091                         do_bind = true;
2092                 }
2093                 break;
2094         case AF_INET:
2095                 if (lbsda->u.in.sin_port != 0) {
2096                         do_reuseaddr = true;
2097                         do_bind = true;
2098                 }
2099                 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2100                         do_bind = true;
2101                 }
2102                 is_inet = true;
2103                 break;
2104 #ifdef HAVE_IPV6
2105         case AF_INET6:
2106                 if (lbsda->u.in6.sin6_port != 0) {
2107                         do_reuseaddr = true;
2108                         do_bind = true;
2109                 }
2110                 if (memcmp(&in6addr_any,
2111                            &lbsda->u.in6.sin6_addr,
2112                            sizeof(in6addr_any)) != 0) {
2113                         do_bind = true;
2114                 }
2115                 is_inet = true;
2116                 do_ipv6only = true;
2117                 break;
2118 #endif
2119         default:
2120                 tevent_req_error(req, EINVAL);
2121                 goto post;
2122         }
2123
2124         if (!do_bind && is_inet) {
2125                 sa_fam = rbsda->u.sa.sa_family;
2126                 switch (sa_fam) {
2127                 case AF_INET:
2128                         do_ipv6only = false;
2129                         break;
2130 #ifdef HAVE_IPV6
2131                 case AF_INET6:
2132                         do_ipv6only = true;
2133                         break;
2134 #endif
2135                 }
2136         }
2137
2138         if (is_inet) {
2139                 state->local = tsocket_address_create(state,
2140                                                       &tsocket_address_bsd_ops,
2141                                                       &lrbsda,
2142                                                       struct tsocket_address_bsd,
2143                                                       __location__ "bsd_connect");
2144                 if (tevent_req_nomem(state->local, req)) {
2145                         goto post;
2146                 }
2147
2148                 ZERO_STRUCTP(lrbsda);
2149                 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2150 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2151                 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2152 #endif
2153         }
2154
2155         state->fd = socket(sa_fam, SOCK_STREAM, 0);
2156         if (state->fd == -1) {
2157                 tevent_req_error(req, errno);
2158                 goto post;
2159         }
2160
2161         state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2162         if (state->fd == -1) {
2163                 tevent_req_error(req, errno);
2164                 goto post;
2165         }
2166
2167 #ifdef HAVE_IPV6
2168         if (do_ipv6only) {
2169                 int val = 1;
2170
2171                 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2172                                  (const void *)&val, sizeof(val));
2173                 if (ret == -1) {
2174                         tevent_req_error(req, errno);
2175                         goto post;
2176                 }
2177         }
2178 #endif
2179
2180         if (do_reuseaddr) {
2181                 int val = 1;
2182
2183                 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2184                                  (const void *)&val, sizeof(val));
2185                 if (ret == -1) {
2186                         tevent_req_error(req, errno);
2187                         goto post;
2188                 }
2189         }
2190
2191         if (do_bind) {
2192                 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2193                 if (ret == -1) {
2194                         tevent_req_error(req, errno);
2195                         goto post;
2196                 }
2197         }
2198
2199         if (rbsda->u.sa.sa_family != sa_fam) {
2200                 tevent_req_error(req, EINVAL);
2201                 goto post;
2202         }
2203
2204         ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2205         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2206         if (retry) {
2207                 /* retry later */
2208                 goto async;
2209         }
2210         if (tevent_req_error(req, err)) {
2211                 goto post;
2212         }
2213
2214         if (!state->local) {
2215                 tevent_req_done(req);
2216                 goto post;
2217         }
2218
2219         ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2220         if (ret == -1) {
2221                 tevent_req_error(req, errno);
2222                 goto post;
2223         }
2224
2225         tevent_req_done(req);
2226         goto post;
2227
2228  async:
2229         state->fde = tevent_add_fd(ev, state,
2230                                    state->fd,
2231                                    TEVENT_FD_READ | TEVENT_FD_WRITE,
2232                                    tstream_bsd_connect_fde_handler,
2233                                    req);
2234         if (tevent_req_nomem(state->fde, req)) {
2235                 goto post;
2236         }
2237
2238         return req;
2239
2240  post:
2241         tevent_req_post(req, ev);
2242         return req;
2243 }
2244
2245 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2246                                             struct tevent_fd *fde,
2247                                             uint16_t flags,
2248                                             void *private_data)
2249 {
2250         struct tevent_req *req = talloc_get_type_abort(private_data,
2251                                  struct tevent_req);
2252         struct tstream_bsd_connect_state *state = tevent_req_data(req,
2253                                         struct tstream_bsd_connect_state);
2254         struct tsocket_address_bsd *lrbsda = NULL;
2255         int ret;
2256         int error=0;
2257         socklen_t len = sizeof(error);
2258         int err;
2259         bool retry;
2260
2261         ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2262         if (ret == 0) {
2263                 if (error != 0) {
2264                         errno = error;
2265                         ret = -1;
2266                 }
2267         }
2268         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2269         if (retry) {
2270                 /* retry later */
2271                 return;
2272         }
2273         if (tevent_req_error(req, err)) {
2274                 return;
2275         }
2276
2277         if (!state->local) {
2278                 tevent_req_done(req);
2279                 return;
2280         }
2281
2282         lrbsda = talloc_get_type_abort(state->local->private_data,
2283                                        struct tsocket_address_bsd);
2284
2285         ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2286         if (ret == -1) {
2287                 tevent_req_error(req, errno);
2288                 return;
2289         }
2290
2291         tevent_req_done(req);
2292 }
2293
2294 static int tstream_bsd_connect_recv(struct tevent_req *req,
2295                                     int *perrno,
2296                                     TALLOC_CTX *mem_ctx,
2297                                     struct tstream_context **stream,
2298                                     struct tsocket_address **local,
2299                                     const char *location)
2300 {
2301         struct tstream_bsd_connect_state *state = tevent_req_data(req,
2302                                         struct tstream_bsd_connect_state);
2303         int ret;
2304
2305         ret = tsocket_simple_int_recv(req, perrno);
2306         if (ret == 0) {
2307                 ret = _tstream_bsd_existing_socket(mem_ctx,
2308                                                    state->fd,
2309                                                    stream,
2310                                                    location);
2311                 if (ret == -1) {
2312                         *perrno = errno;
2313                         goto done;
2314                 }
2315                 TALLOC_FREE(state->fde);
2316                 state->fd = -1;
2317
2318                 if (local) {
2319                         *local = talloc_move(mem_ctx, &state->local);
2320                 }
2321         }
2322
2323 done:
2324         tevent_req_received(req);
2325         return ret;
2326 }
2327
2328 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2329                                         struct tevent_context *ev,
2330                                         const struct tsocket_address *local,
2331                                         const struct tsocket_address *remote)
2332 {
2333         struct tsocket_address_bsd *lbsda =
2334                 talloc_get_type_abort(local->private_data,
2335                 struct tsocket_address_bsd);
2336         struct tevent_req *req;
2337         int sys_errno = 0;
2338
2339         switch (lbsda->u.sa.sa_family) {
2340         case AF_INET:
2341                 break;
2342 #ifdef HAVE_IPV6
2343         case AF_INET6:
2344                 break;
2345 #endif
2346         default:
2347                 sys_errno = EINVAL;
2348                 break;
2349         }
2350
2351         req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2352
2353         return req;
2354 }
2355
2356 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2357                                    int *perrno,
2358                                    TALLOC_CTX *mem_ctx,
2359                                    struct tstream_context **stream,
2360                                    struct tsocket_address **local,
2361                                    const char *location)
2362 {
2363         return tstream_bsd_connect_recv(req, perrno,
2364                                         mem_ctx, stream, local,
2365                                         location);
2366 }
2367
2368 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2369                                         struct tevent_context *ev,
2370                                         const struct tsocket_address *local,
2371                                         const struct tsocket_address *remote)
2372 {
2373         struct tsocket_address_bsd *lbsda =
2374                 talloc_get_type_abort(local->private_data,
2375                 struct tsocket_address_bsd);
2376         struct tevent_req *req;
2377         int sys_errno = 0;
2378
2379         switch (lbsda->u.sa.sa_family) {
2380         case AF_UNIX:
2381                 break;
2382         default:
2383                 sys_errno = EINVAL;
2384                 break;
2385         }
2386
2387         req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2388
2389         return req;
2390 }
2391
2392 int _tstream_unix_connect_recv(struct tevent_req *req,
2393                                       int *perrno,
2394                                       TALLOC_CTX *mem_ctx,
2395                                       struct tstream_context **stream,
2396                                       const char *location)
2397 {
2398         return tstream_bsd_connect_recv(req, perrno,
2399                                         mem_ctx, stream, NULL,
2400                                         location);
2401 }
2402
2403 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2404                              struct tstream_context **_stream1,
2405                              TALLOC_CTX *mem_ctx2,
2406                              struct tstream_context **_stream2,
2407                              const char *location)
2408 {
2409         int ret;
2410         int fds[2];
2411         int fd1;
2412         int fd2;
2413         struct tstream_context *stream1 = NULL;
2414         struct tstream_context *stream2 = NULL;
2415
2416         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2417         if (ret == -1) {
2418                 return -1;
2419         }
2420         fd1 = fds[0];
2421         fd2 = fds[1];
2422
2423         fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2424         if (fd1 == -1) {
2425                 int sys_errno = errno;
2426                 close(fd2);
2427                 errno = sys_errno;
2428                 return -1;
2429         }
2430
2431         fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2432         if (fd2 == -1) {
2433                 int sys_errno = errno;
2434                 close(fd1);
2435                 errno = sys_errno;
2436                 return -1;
2437         }
2438
2439         ret = _tstream_bsd_existing_socket(mem_ctx1,
2440                                            fd1,
2441                                            &stream1,
2442                                            location);
2443         if (ret == -1) {
2444                 int sys_errno = errno;
2445                 close(fd1);
2446                 close(fd2);
2447                 errno = sys_errno;
2448                 return -1;
2449         }
2450
2451         ret = _tstream_bsd_existing_socket(mem_ctx2,
2452                                            fd2,
2453                                            &stream2,
2454                                            location);
2455         if (ret == -1) {
2456                 int sys_errno = errno;
2457                 talloc_free(stream1);
2458                 close(fd2);
2459                 errno = sys_errno;
2460                 return -1;
2461         }
2462
2463         *_stream1 = stream1;
2464         *_stream2 = stream2;
2465         return 0;
2466 }
2467