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