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