winbind: Properly error check init_lsa_ref_domain_list
[obnox/samba/samba-obnox.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 prefer 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_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1392                                 int fd,
1393                                 struct tdgram_context **_dgram,
1394                                 const char *location)
1395 {
1396         struct tdgram_context *dgram;
1397         struct tdgram_bsd *bsds;
1398
1399         dgram = tdgram_context_create(mem_ctx,
1400                                       &tdgram_bsd_ops,
1401                                       &bsds,
1402                                       struct tdgram_bsd,
1403                                       location);
1404         if (!dgram) {
1405                 return -1;
1406         }
1407         ZERO_STRUCTP(bsds);
1408         bsds->fd = fd;
1409         talloc_set_destructor(bsds, tdgram_bsd_destructor);
1410
1411         *_dgram = dgram;
1412         return 0;
1413 }
1414
1415 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1416                             const struct tsocket_address *remote,
1417                             TALLOC_CTX *mem_ctx,
1418                             struct tdgram_context **dgram,
1419                             const char *location)
1420 {
1421         struct tsocket_address_bsd *lbsda =
1422                 talloc_get_type_abort(local->private_data,
1423                 struct tsocket_address_bsd);
1424         int ret;
1425
1426         switch (lbsda->u.sa.sa_family) {
1427         case AF_INET:
1428                 break;
1429 #ifdef HAVE_IPV6
1430         case AF_INET6:
1431                 break;
1432 #endif
1433         default:
1434                 errno = EINVAL;
1435                 return -1;
1436         }
1437
1438         ret = tdgram_bsd_dgram_socket(local, remote, false,
1439                                       mem_ctx, dgram, location);
1440
1441         return ret;
1442 }
1443
1444 int _tdgram_inet_udp_broadcast_socket(const struct tsocket_address *local,
1445                                       TALLOC_CTX *mem_ctx,
1446                                       struct tdgram_context **dgram,
1447                                       const char *location)
1448 {
1449         struct tsocket_address_bsd *lbsda =
1450                 talloc_get_type_abort(local->private_data,
1451                 struct tsocket_address_bsd);
1452         int ret;
1453
1454         switch (lbsda->u.sa.sa_family) {
1455         case AF_INET:
1456                 break;
1457 #ifdef HAVE_IPV6
1458         case AF_INET6:
1459                 /* only ipv4 */
1460                 errno = EINVAL;
1461                 return -1;
1462 #endif
1463         default:
1464                 errno = EINVAL;
1465                 return -1;
1466         }
1467
1468         ret = tdgram_bsd_dgram_socket(local, NULL, true,
1469                                       mem_ctx, dgram, location);
1470
1471         return ret;
1472 }
1473
1474 int _tdgram_unix_socket(const struct tsocket_address *local,
1475                         const struct tsocket_address *remote,
1476                         TALLOC_CTX *mem_ctx,
1477                         struct tdgram_context **dgram,
1478                         const char *location)
1479 {
1480         struct tsocket_address_bsd *lbsda =
1481                 talloc_get_type_abort(local->private_data,
1482                 struct tsocket_address_bsd);
1483         int ret;
1484
1485         switch (lbsda->u.sa.sa_family) {
1486         case AF_UNIX:
1487                 break;
1488         default:
1489                 errno = EINVAL;
1490                 return -1;
1491         }
1492
1493         ret = tdgram_bsd_dgram_socket(local, remote, false,
1494                                       mem_ctx, dgram, location);
1495
1496         return ret;
1497 }
1498
1499 struct tstream_bsd {
1500         int fd;
1501
1502         void *event_ptr;
1503         struct tevent_fd *fde;
1504         bool optimize_readv;
1505
1506         void *readable_private;
1507         void (*readable_handler)(void *private_data);
1508         void *writeable_private;
1509         void (*writeable_handler)(void *private_data);
1510 };
1511
1512 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
1513                                 bool on)
1514 {
1515         struct tstream_bsd *bsds =
1516                 talloc_get_type(_tstream_context_data(stream),
1517                 struct tstream_bsd);
1518         bool old;
1519
1520         if (bsds == NULL) {
1521                 /* not a bsd socket */
1522                 return false;
1523         }
1524
1525         old = bsds->optimize_readv;
1526         bsds->optimize_readv = on;
1527
1528         return old;
1529 }
1530
1531 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1532                                     struct tevent_fd *fde,
1533                                     uint16_t flags,
1534                                     void *private_data)
1535 {
1536         struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1537                                    struct tstream_bsd);
1538
1539         if (flags & TEVENT_FD_WRITE) {
1540                 bsds->writeable_handler(bsds->writeable_private);
1541                 return;
1542         }
1543         if (flags & TEVENT_FD_READ) {
1544                 if (!bsds->readable_handler) {
1545                         if (bsds->writeable_handler) {
1546                                 bsds->writeable_handler(bsds->writeable_private);
1547                                 return;
1548                         }
1549                         TEVENT_FD_NOT_READABLE(bsds->fde);
1550                         return;
1551                 }
1552                 bsds->readable_handler(bsds->readable_private);
1553                 return;
1554         }
1555 }
1556
1557 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1558                                             struct tevent_context *ev,
1559                                             void (*handler)(void *private_data),
1560                                             void *private_data)
1561 {
1562         if (ev == NULL) {
1563                 if (handler) {
1564                         errno = EINVAL;
1565                         return -1;
1566                 }
1567                 if (!bsds->readable_handler) {
1568                         return 0;
1569                 }
1570                 bsds->readable_handler = NULL;
1571                 bsds->readable_private = NULL;
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, TEVENT_FD_READ,
1591                                           tstream_bsd_fde_handler,
1592                                           bsds);
1593                 if (!bsds->fde) {
1594                         errno = ENOMEM;
1595                         return -1;
1596                 }
1597
1598                 /* cache the event context we're running on */
1599                 bsds->event_ptr = ev;
1600         } else if (!bsds->readable_handler) {
1601                 TEVENT_FD_READABLE(bsds->fde);
1602         }
1603
1604         bsds->readable_handler = handler;
1605         bsds->readable_private = private_data;
1606
1607         return 0;
1608 }
1609
1610 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1611                                              struct tevent_context *ev,
1612                                              void (*handler)(void *private_data),
1613                                              void *private_data)
1614 {
1615         if (ev == NULL) {
1616                 if (handler) {
1617                         errno = EINVAL;
1618                         return -1;
1619                 }
1620                 if (!bsds->writeable_handler) {
1621                         return 0;
1622                 }
1623                 bsds->writeable_handler = NULL;
1624                 bsds->writeable_private = NULL;
1625                 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1626
1627                 return 0;
1628         }
1629
1630         /* read and write must use the same tevent_context */
1631         if (bsds->event_ptr != ev) {
1632                 if (bsds->readable_handler || bsds->writeable_handler) {
1633                         errno = EINVAL;
1634                         return -1;
1635                 }
1636                 bsds->event_ptr = NULL;
1637                 TALLOC_FREE(bsds->fde);
1638         }
1639
1640         if (tevent_fd_get_flags(bsds->fde) == 0) {
1641                 TALLOC_FREE(bsds->fde);
1642
1643                 bsds->fde = tevent_add_fd(ev, bsds,
1644                                           bsds->fd,
1645                                           TEVENT_FD_READ | TEVENT_FD_WRITE,
1646                                           tstream_bsd_fde_handler,
1647                                           bsds);
1648                 if (!bsds->fde) {
1649                         errno = ENOMEM;
1650                         return -1;
1651                 }
1652
1653                 /* cache the event context we're running on */
1654                 bsds->event_ptr = ev;
1655         } else if (!bsds->writeable_handler) {
1656                 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1657                 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1658                 tevent_fd_set_flags(bsds->fde, flags);
1659         }
1660
1661         bsds->writeable_handler = handler;
1662         bsds->writeable_private = private_data;
1663
1664         return 0;
1665 }
1666
1667 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1668 {
1669         struct tstream_bsd *bsds = tstream_context_data(stream,
1670                                    struct tstream_bsd);
1671         ssize_t ret;
1672
1673         if (bsds->fd == -1) {
1674                 errno = ENOTCONN;
1675                 return -1;
1676         }
1677
1678         ret = tsocket_bsd_pending(bsds->fd);
1679
1680         return ret;
1681 }
1682
1683 struct tstream_bsd_readv_state {
1684         struct tstream_context *stream;
1685
1686         struct iovec *vector;
1687         size_t count;
1688
1689         int ret;
1690 };
1691
1692 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1693 {
1694         struct tstream_bsd *bsds = tstream_context_data(state->stream,
1695                                    struct tstream_bsd);
1696
1697         tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1698
1699         return 0;
1700 }
1701
1702 static void tstream_bsd_readv_handler(void *private_data);
1703
1704 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1705                                         struct tevent_context *ev,
1706                                         struct tstream_context *stream,
1707                                         struct iovec *vector,
1708                                         size_t count)
1709 {
1710         struct tevent_req *req;
1711         struct tstream_bsd_readv_state *state;
1712         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1713         int ret;
1714
1715         req = tevent_req_create(mem_ctx, &state,
1716                                 struct tstream_bsd_readv_state);
1717         if (!req) {
1718                 return NULL;
1719         }
1720
1721         state->stream   = stream;
1722         /* we make a copy of the vector so that we can modify it */
1723         state->vector   = talloc_array(state, struct iovec, count);
1724         if (tevent_req_nomem(state->vector, req)) {
1725                 goto post;
1726         }
1727         memcpy(state->vector, vector, sizeof(struct iovec)*count);
1728         state->count    = count;
1729         state->ret      = 0;
1730
1731         talloc_set_destructor(state, tstream_bsd_readv_destructor);
1732
1733         if (bsds->fd == -1) {
1734                 tevent_req_error(req, ENOTCONN);
1735                 goto post;
1736         }
1737
1738         /*
1739          * this is a fast path, not waiting for the
1740          * socket to become explicit readable gains
1741          * about 10%-20% performance in benchmark tests.
1742          */
1743         if (bsds->optimize_readv) {
1744                 /*
1745                  * We only do the optimization on
1746                  * readv if the caller asked for it.
1747                  *
1748                  * This is needed because in most cases
1749                  * we prefer to flush send buffers before
1750                  * receiving incoming requests.
1751                  */
1752                 tstream_bsd_readv_handler(req);
1753                 if (!tevent_req_is_in_progress(req)) {
1754                         goto post;
1755                 }
1756         }
1757
1758         ret = tstream_bsd_set_readable_handler(bsds, ev,
1759                                               tstream_bsd_readv_handler,
1760                                               req);
1761         if (ret == -1) {
1762                 tevent_req_error(req, errno);
1763                 goto post;
1764         }
1765
1766         return req;
1767
1768  post:
1769         tevent_req_post(req, ev);
1770         return req;
1771 }
1772
1773 static void tstream_bsd_readv_handler(void *private_data)
1774 {
1775         struct tevent_req *req = talloc_get_type_abort(private_data,
1776                                  struct tevent_req);
1777         struct tstream_bsd_readv_state *state = tevent_req_data(req,
1778                                         struct tstream_bsd_readv_state);
1779         struct tstream_context *stream = state->stream;
1780         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1781         int ret;
1782         int err;
1783         int _count;
1784         bool ok, retry;
1785
1786         ret = readv(bsds->fd, state->vector, state->count);
1787         if (ret == 0) {
1788                 /* propagate end of file */
1789                 tevent_req_error(req, EPIPE);
1790                 return;
1791         }
1792         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1793         if (retry) {
1794                 /* retry later */
1795                 return;
1796         }
1797         if (tevent_req_error(req, err)) {
1798                 return;
1799         }
1800
1801         state->ret += ret;
1802
1803         _count = state->count; /* tstream has size_t count, readv has int */
1804         ok = iov_advance(&state->vector, &_count, ret);
1805         state->count = _count;
1806
1807         if (!ok) {
1808                 tevent_req_error(req, EINVAL);
1809                 return;
1810         }
1811
1812         if (state->count > 0) {
1813                 /* we have more to read */
1814                 return;
1815         }
1816
1817         tevent_req_done(req);
1818 }
1819
1820 static int tstream_bsd_readv_recv(struct tevent_req *req,
1821                                   int *perrno)
1822 {
1823         struct tstream_bsd_readv_state *state = tevent_req_data(req,
1824                                         struct tstream_bsd_readv_state);
1825         int ret;
1826
1827         ret = tsocket_simple_int_recv(req, perrno);
1828         if (ret == 0) {
1829                 ret = state->ret;
1830         }
1831
1832         tevent_req_received(req);
1833         return ret;
1834 }
1835
1836 struct tstream_bsd_writev_state {
1837         struct tstream_context *stream;
1838
1839         struct iovec *vector;
1840         size_t count;
1841
1842         int ret;
1843 };
1844
1845 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1846 {
1847         struct tstream_bsd *bsds = tstream_context_data(state->stream,
1848                                   struct tstream_bsd);
1849
1850         tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1851
1852         return 0;
1853 }
1854
1855 static void tstream_bsd_writev_handler(void *private_data);
1856
1857 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1858                                                  struct tevent_context *ev,
1859                                                  struct tstream_context *stream,
1860                                                  const struct iovec *vector,
1861                                                  size_t count)
1862 {
1863         struct tevent_req *req;
1864         struct tstream_bsd_writev_state *state;
1865         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1866         int ret;
1867
1868         req = tevent_req_create(mem_ctx, &state,
1869                                 struct tstream_bsd_writev_state);
1870         if (!req) {
1871                 return NULL;
1872         }
1873
1874         state->stream   = stream;
1875         /* we make a copy of the vector so that we can modify it */
1876         state->vector   = talloc_array(state, struct iovec, count);
1877         if (tevent_req_nomem(state->vector, req)) {
1878                 goto post;
1879         }
1880         memcpy(state->vector, vector, sizeof(struct iovec)*count);
1881         state->count    = count;
1882         state->ret      = 0;
1883
1884         talloc_set_destructor(state, tstream_bsd_writev_destructor);
1885
1886         if (bsds->fd == -1) {
1887                 tevent_req_error(req, ENOTCONN);
1888                 goto post;
1889         }
1890
1891         /*
1892          * this is a fast path, not waiting for the
1893          * socket to become explicit writeable gains
1894          * about 10%-20% performance in benchmark tests.
1895          */
1896         tstream_bsd_writev_handler(req);
1897         if (!tevent_req_is_in_progress(req)) {
1898                 goto post;
1899         }
1900
1901         ret = tstream_bsd_set_writeable_handler(bsds, ev,
1902                                                tstream_bsd_writev_handler,
1903                                                req);
1904         if (ret == -1) {
1905                 tevent_req_error(req, errno);
1906                 goto post;
1907         }
1908
1909         return req;
1910
1911  post:
1912         tevent_req_post(req, ev);
1913         return req;
1914 }
1915
1916 static void tstream_bsd_writev_handler(void *private_data)
1917 {
1918         struct tevent_req *req = talloc_get_type_abort(private_data,
1919                                  struct tevent_req);
1920         struct tstream_bsd_writev_state *state = tevent_req_data(req,
1921                                         struct tstream_bsd_writev_state);
1922         struct tstream_context *stream = state->stream;
1923         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1924         ssize_t ret;
1925         int err;
1926         int _count;
1927         bool ok, retry;
1928
1929         ret = writev(bsds->fd, state->vector, state->count);
1930         if (ret == 0) {
1931                 /* propagate end of file */
1932                 tevent_req_error(req, EPIPE);
1933                 return;
1934         }
1935         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1936         if (retry) {
1937                 /* retry later */
1938                 return;
1939         }
1940         if (tevent_req_error(req, err)) {
1941                 return;
1942         }
1943
1944         state->ret += ret;
1945
1946         _count = state->count; /* tstream has size_t count, writev has int */
1947         ok = iov_advance(&state->vector, &_count, ret);
1948         state->count = _count;
1949
1950         if (!ok) {
1951                 tevent_req_error(req, EINVAL);
1952                 return;
1953         }
1954
1955         if (state->count > 0) {
1956                 /* we have more to read */
1957                 return;
1958         }
1959
1960         tevent_req_done(req);
1961 }
1962
1963 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1964 {
1965         struct tstream_bsd_writev_state *state = tevent_req_data(req,
1966                                         struct tstream_bsd_writev_state);
1967         int ret;
1968
1969         ret = tsocket_simple_int_recv(req, perrno);
1970         if (ret == 0) {
1971                 ret = state->ret;
1972         }
1973
1974         tevent_req_received(req);
1975         return ret;
1976 }
1977
1978 struct tstream_bsd_disconnect_state {
1979         void *__dummy;
1980 };
1981
1982 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1983                                                      struct tevent_context *ev,
1984                                                      struct tstream_context *stream)
1985 {
1986         struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1987         struct tevent_req *req;
1988         struct tstream_bsd_disconnect_state *state;
1989         int ret;
1990         int err;
1991         bool dummy;
1992
1993         req = tevent_req_create(mem_ctx, &state,
1994                                 struct tstream_bsd_disconnect_state);
1995         if (req == NULL) {
1996                 return NULL;
1997         }
1998
1999         if (bsds->fd == -1) {
2000                 tevent_req_error(req, ENOTCONN);
2001                 goto post;
2002         }
2003
2004         TALLOC_FREE(bsds->fde);
2005         ret = close(bsds->fd);
2006         bsds->fd = -1;
2007         err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
2008         if (tevent_req_error(req, err)) {
2009                 goto post;
2010         }
2011
2012         tevent_req_done(req);
2013 post:
2014         tevent_req_post(req, ev);
2015         return req;
2016 }
2017
2018 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
2019                                       int *perrno)
2020 {
2021         int ret;
2022
2023         ret = tsocket_simple_int_recv(req, perrno);
2024
2025         tevent_req_received(req);
2026         return ret;
2027 }
2028
2029 static const struct tstream_context_ops tstream_bsd_ops = {
2030         .name                   = "bsd",
2031
2032         .pending_bytes          = tstream_bsd_pending_bytes,
2033
2034         .readv_send             = tstream_bsd_readv_send,
2035         .readv_recv             = tstream_bsd_readv_recv,
2036
2037         .writev_send            = tstream_bsd_writev_send,
2038         .writev_recv            = tstream_bsd_writev_recv,
2039
2040         .disconnect_send        = tstream_bsd_disconnect_send,
2041         .disconnect_recv        = tstream_bsd_disconnect_recv,
2042 };
2043
2044 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
2045 {
2046         TALLOC_FREE(bsds->fde);
2047         if (bsds->fd != -1) {
2048                 close(bsds->fd);
2049                 bsds->fd = -1;
2050         }
2051         return 0;
2052 }
2053
2054 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
2055                                  int fd,
2056                                  struct tstream_context **_stream,
2057                                  const char *location)
2058 {
2059         struct tstream_context *stream;
2060         struct tstream_bsd *bsds;
2061
2062         stream = tstream_context_create(mem_ctx,
2063                                         &tstream_bsd_ops,
2064                                         &bsds,
2065                                         struct tstream_bsd,
2066                                         location);
2067         if (!stream) {
2068                 return -1;
2069         }
2070         ZERO_STRUCTP(bsds);
2071         bsds->fd = fd;
2072         talloc_set_destructor(bsds, tstream_bsd_destructor);
2073
2074         *_stream = stream;
2075         return 0;
2076 }
2077
2078 struct tstream_bsd_connect_state {
2079         int fd;
2080         struct tevent_fd *fde;
2081         struct tstream_conext *stream;
2082         struct tsocket_address *local;
2083 };
2084
2085 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
2086 {
2087         TALLOC_FREE(state->fde);
2088         if (state->fd != -1) {
2089                 close(state->fd);
2090                 state->fd = -1;
2091         }
2092
2093         return 0;
2094 }
2095
2096 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2097                                             struct tevent_fd *fde,
2098                                             uint16_t flags,
2099                                             void *private_data);
2100
2101 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2102                                         struct tevent_context *ev,
2103                                         int sys_errno,
2104                                         const struct tsocket_address *local,
2105                                         const struct tsocket_address *remote)
2106 {
2107         struct tevent_req *req;
2108         struct tstream_bsd_connect_state *state;
2109         struct tsocket_address_bsd *lbsda =
2110                 talloc_get_type_abort(local->private_data,
2111                 struct tsocket_address_bsd);
2112         struct tsocket_address_bsd *lrbsda = NULL;
2113         struct tsocket_address_bsd *rbsda =
2114                 talloc_get_type_abort(remote->private_data,
2115                 struct tsocket_address_bsd);
2116         int ret;
2117         bool do_bind = false;
2118         bool do_reuseaddr = false;
2119         bool do_ipv6only = false;
2120         bool is_inet = false;
2121         int sa_fam = lbsda->u.sa.sa_family;
2122
2123         req = tevent_req_create(mem_ctx, &state,
2124                                 struct tstream_bsd_connect_state);
2125         if (!req) {
2126                 return NULL;
2127         }
2128         state->fd = -1;
2129         state->fde = NULL;
2130
2131         talloc_set_destructor(state, tstream_bsd_connect_destructor);
2132
2133         /* give the wrappers a chance to report an error */
2134         if (sys_errno != 0) {
2135                 tevent_req_error(req, sys_errno);
2136                 goto post;
2137         }
2138
2139         switch (lbsda->u.sa.sa_family) {
2140         case AF_UNIX:
2141                 if (lbsda->u.un.sun_path[0] != 0) {
2142                         do_reuseaddr = true;
2143                         do_bind = true;
2144                 }
2145                 break;
2146         case AF_INET:
2147                 if (lbsda->u.in.sin_port != 0) {
2148                         do_reuseaddr = true;
2149                         do_bind = true;
2150                 }
2151                 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2152                         do_bind = true;
2153                 }
2154                 is_inet = true;
2155                 break;
2156 #ifdef HAVE_IPV6
2157         case AF_INET6:
2158                 if (lbsda->u.in6.sin6_port != 0) {
2159                         do_reuseaddr = true;
2160                         do_bind = true;
2161                 }
2162                 if (memcmp(&in6addr_any,
2163                            &lbsda->u.in6.sin6_addr,
2164                            sizeof(in6addr_any)) != 0) {
2165                         do_bind = true;
2166                 }
2167                 is_inet = true;
2168                 do_ipv6only = true;
2169                 break;
2170 #endif
2171         default:
2172                 tevent_req_error(req, EINVAL);
2173                 goto post;
2174         }
2175
2176         if (!do_bind && is_inet) {
2177                 sa_fam = rbsda->u.sa.sa_family;
2178                 switch (sa_fam) {
2179                 case AF_INET:
2180                         do_ipv6only = false;
2181                         break;
2182 #ifdef HAVE_IPV6
2183                 case AF_INET6:
2184                         do_ipv6only = true;
2185                         break;
2186 #endif
2187                 }
2188         }
2189
2190         if (is_inet) {
2191                 state->local = tsocket_address_create(state,
2192                                                       &tsocket_address_bsd_ops,
2193                                                       &lrbsda,
2194                                                       struct tsocket_address_bsd,
2195                                                       __location__ "bsd_connect");
2196                 if (tevent_req_nomem(state->local, req)) {
2197                         goto post;
2198                 }
2199
2200                 ZERO_STRUCTP(lrbsda);
2201                 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2202 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2203                 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2204 #endif
2205         }
2206
2207         state->fd = socket(sa_fam, SOCK_STREAM, 0);
2208         if (state->fd == -1) {
2209                 tevent_req_error(req, errno);
2210                 goto post;
2211         }
2212
2213         state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2214         if (state->fd == -1) {
2215                 tevent_req_error(req, errno);
2216                 goto post;
2217         }
2218
2219 #ifdef HAVE_IPV6
2220         if (do_ipv6only) {
2221                 int val = 1;
2222
2223                 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2224                                  (const void *)&val, sizeof(val));
2225                 if (ret == -1) {
2226                         tevent_req_error(req, errno);
2227                         goto post;
2228                 }
2229         }
2230 #endif
2231
2232         if (do_reuseaddr) {
2233                 int val = 1;
2234
2235                 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2236                                  (const void *)&val, sizeof(val));
2237                 if (ret == -1) {
2238                         tevent_req_error(req, errno);
2239                         goto post;
2240                 }
2241         }
2242
2243         if (do_bind) {
2244                 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2245                 if (ret == -1) {
2246                         tevent_req_error(req, errno);
2247                         goto post;
2248                 }
2249         }
2250
2251         if (rbsda->u.sa.sa_family != sa_fam) {
2252                 tevent_req_error(req, EINVAL);
2253                 goto post;
2254         }
2255
2256         ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2257         if (ret == -1) {
2258                 if (errno == EINPROGRESS) {
2259                         goto async;
2260                 }
2261                 tevent_req_error(req, errno);
2262                 goto post;
2263         }
2264
2265         if (!state->local) {
2266                 tevent_req_done(req);
2267                 goto post;
2268         }
2269
2270         ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2271         if (ret == -1) {
2272                 tevent_req_error(req, errno);
2273                 goto post;
2274         }
2275
2276         tevent_req_done(req);
2277         goto post;
2278
2279  async:
2280         state->fde = tevent_add_fd(ev, state,
2281                                    state->fd,
2282                                    TEVENT_FD_READ | TEVENT_FD_WRITE,
2283                                    tstream_bsd_connect_fde_handler,
2284                                    req);
2285         if (tevent_req_nomem(state->fde, req)) {
2286                 goto post;
2287         }
2288
2289         return req;
2290
2291  post:
2292         tevent_req_post(req, ev);
2293         return req;
2294 }
2295
2296 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2297                                             struct tevent_fd *fde,
2298                                             uint16_t flags,
2299                                             void *private_data)
2300 {
2301         struct tevent_req *req = talloc_get_type_abort(private_data,
2302                                  struct tevent_req);
2303         struct tstream_bsd_connect_state *state = tevent_req_data(req,
2304                                         struct tstream_bsd_connect_state);
2305         struct tsocket_address_bsd *lrbsda = NULL;
2306         int ret;
2307         int error=0;
2308         socklen_t len = sizeof(error);
2309         int err;
2310         bool retry;
2311
2312         ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2313         if (ret == 0) {
2314                 if (error != 0) {
2315                         errno = error;
2316                         ret = -1;
2317                 }
2318         }
2319         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2320         if (retry) {
2321                 /* retry later */
2322                 return;
2323         }
2324         if (tevent_req_error(req, err)) {
2325                 return;
2326         }
2327
2328         if (!state->local) {
2329                 tevent_req_done(req);
2330                 return;
2331         }
2332
2333         lrbsda = talloc_get_type_abort(state->local->private_data,
2334                                        struct tsocket_address_bsd);
2335
2336         ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2337         if (ret == -1) {
2338                 tevent_req_error(req, errno);
2339                 return;
2340         }
2341
2342         tevent_req_done(req);
2343 }
2344
2345 static int tstream_bsd_connect_recv(struct tevent_req *req,
2346                                     int *perrno,
2347                                     TALLOC_CTX *mem_ctx,
2348                                     struct tstream_context **stream,
2349                                     struct tsocket_address **local,
2350                                     const char *location)
2351 {
2352         struct tstream_bsd_connect_state *state = tevent_req_data(req,
2353                                         struct tstream_bsd_connect_state);
2354         int ret;
2355
2356         ret = tsocket_simple_int_recv(req, perrno);
2357         if (ret == 0) {
2358                 ret = _tstream_bsd_existing_socket(mem_ctx,
2359                                                    state->fd,
2360                                                    stream,
2361                                                    location);
2362                 if (ret == -1) {
2363                         *perrno = errno;
2364                         goto done;
2365                 }
2366                 TALLOC_FREE(state->fde);
2367                 state->fd = -1;
2368
2369                 if (local) {
2370                         *local = talloc_move(mem_ctx, &state->local);
2371                 }
2372         }
2373
2374 done:
2375         tevent_req_received(req);
2376         return ret;
2377 }
2378
2379 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2380                                         struct tevent_context *ev,
2381                                         const struct tsocket_address *local,
2382                                         const struct tsocket_address *remote)
2383 {
2384         struct tsocket_address_bsd *lbsda =
2385                 talloc_get_type_abort(local->private_data,
2386                 struct tsocket_address_bsd);
2387         struct tevent_req *req;
2388         int sys_errno = 0;
2389
2390         switch (lbsda->u.sa.sa_family) {
2391         case AF_INET:
2392                 break;
2393 #ifdef HAVE_IPV6
2394         case AF_INET6:
2395                 break;
2396 #endif
2397         default:
2398                 sys_errno = EINVAL;
2399                 break;
2400         }
2401
2402         req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2403
2404         return req;
2405 }
2406
2407 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2408                                    int *perrno,
2409                                    TALLOC_CTX *mem_ctx,
2410                                    struct tstream_context **stream,
2411                                    struct tsocket_address **local,
2412                                    const char *location)
2413 {
2414         return tstream_bsd_connect_recv(req, perrno,
2415                                         mem_ctx, stream, local,
2416                                         location);
2417 }
2418
2419 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2420                                         struct tevent_context *ev,
2421                                         const struct tsocket_address *local,
2422                                         const struct tsocket_address *remote)
2423 {
2424         struct tsocket_address_bsd *lbsda =
2425                 talloc_get_type_abort(local->private_data,
2426                 struct tsocket_address_bsd);
2427         struct tevent_req *req;
2428         int sys_errno = 0;
2429
2430         switch (lbsda->u.sa.sa_family) {
2431         case AF_UNIX:
2432                 break;
2433         default:
2434                 sys_errno = EINVAL;
2435                 break;
2436         }
2437
2438         req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2439
2440         return req;
2441 }
2442
2443 int _tstream_unix_connect_recv(struct tevent_req *req,
2444                                       int *perrno,
2445                                       TALLOC_CTX *mem_ctx,
2446                                       struct tstream_context **stream,
2447                                       const char *location)
2448 {
2449         return tstream_bsd_connect_recv(req, perrno,
2450                                         mem_ctx, stream, NULL,
2451                                         location);
2452 }
2453
2454 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2455                              struct tstream_context **_stream1,
2456                              TALLOC_CTX *mem_ctx2,
2457                              struct tstream_context **_stream2,
2458                              const char *location)
2459 {
2460         int ret;
2461         int fds[2];
2462         int fd1;
2463         int fd2;
2464         struct tstream_context *stream1 = NULL;
2465         struct tstream_context *stream2 = NULL;
2466
2467         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2468         if (ret == -1) {
2469                 return -1;
2470         }
2471         fd1 = fds[0];
2472         fd2 = fds[1];
2473
2474         fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2475         if (fd1 == -1) {
2476                 int sys_errno = errno;
2477                 close(fd2);
2478                 errno = sys_errno;
2479                 return -1;
2480         }
2481
2482         fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2483         if (fd2 == -1) {
2484                 int sys_errno = errno;
2485                 close(fd1);
2486                 errno = sys_errno;
2487                 return -1;
2488         }
2489
2490         ret = _tstream_bsd_existing_socket(mem_ctx1,
2491                                            fd1,
2492                                            &stream1,
2493                                            location);
2494         if (ret == -1) {
2495                 int sys_errno = errno;
2496                 close(fd1);
2497                 close(fd2);
2498                 errno = sys_errno;
2499                 return -1;
2500         }
2501
2502         ret = _tstream_bsd_existing_socket(mem_ctx2,
2503                                            fd2,
2504                                            &stream2,
2505                                            location);
2506         if (ret == -1) {
2507                 int sys_errno = errno;
2508                 talloc_free(stream1);
2509                 close(fd2);
2510                 errno = sys_errno;
2511                 return -1;
2512         }
2513
2514         *_stream1 = stream1;
2515         *_stream2 = stream2;
2516         return 0;
2517 }
2518