s3:libsmb: add cli_{query,set}_security_descriptor() which take sec_info flags
[Samba/gebeck_regimport.git] / lib / tsocket / tsocket_bsd.c
blob56dff68dd2f6b21a015ac2729da517d002627abc
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
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
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.
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.
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/>.
24 #include "replace.h"
25 #include "system/filesys.h"
26 #include "system/network.h"
27 #include "tsocket.h"
28 #include "tsocket_internal.h"
30 static int tsocket_bsd_error_from_errno(int ret,
31 int sys_errno,
32 bool *retry)
34 *retry = false;
36 if (ret >= 0) {
37 return 0;
40 if (ret != -1) {
41 return EIO;
44 if (sys_errno == 0) {
45 return EIO;
48 if (sys_errno == EINTR) {
49 *retry = true;
50 return sys_errno;
53 if (sys_errno == EINPROGRESS) {
54 *retry = true;
55 return sys_errno;
58 if (sys_errno == EAGAIN) {
59 *retry = true;
60 return sys_errno;
63 #ifdef EWOULDBLOCK
64 if (sys_errno == EWOULDBLOCK) {
65 *retry = true;
66 return sys_errno;
68 #endif
70 return sys_errno;
73 static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
75 int i;
76 int sys_errno = 0;
77 int fds[3];
78 int num_fds = 0;
80 int result, flags;
82 if (fd == -1) {
83 return -1;
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;
96 for (i=0; i<num_fds; i++) {
97 close(fds[i]);
99 if (fd == -1) {
100 errno = sys_errno;
101 return fd;
105 /* fd should be nonblocking. */
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
117 if ((flags = fcntl(fd, F_GETFL)) == -1) {
118 goto fail;
121 flags |= FLAG_TO_SET;
122 if (fcntl(fd, F_SETFL, flags) == -1) {
123 goto fail;
126 #undef FLAG_TO_SET
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);
135 if (result < 0) {
136 goto fail;
138 #endif
139 return fd;
141 fail:
142 if (fd != -1) {
143 sys_errno = errno;
144 close(fd);
145 errno = sys_errno;
147 return -1;
150 static ssize_t tsocket_bsd_pending(int fd)
152 int ret, error;
153 int value = 0;
154 socklen_t len;
156 ret = ioctl(fd, FIONREAD, &value);
157 if (ret == -1) {
158 return ret;
161 if (ret != 0) {
162 /* this should not be reached */
163 errno = EIO;
164 return -1;
167 if (value != 0) {
168 return value;
171 error = 0;
172 len = sizeof(error);
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.
179 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
180 if (ret == -1) {
181 return ret;
183 if (error != 0) {
184 errno = error;
185 return -1;
187 return 0;
190 static const struct tsocket_address_ops tsocket_address_bsd_ops;
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;
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)
211 struct tsocket_address *addr;
212 struct tsocket_address_bsd *bsda;
214 if (sa_socklen < sizeof(sa->sa_family)) {
215 errno = EINVAL;
216 return -1;
219 switch (sa->sa_family) {
220 case AF_UNIX:
221 if (sa_socklen > sizeof(struct sockaddr_un)) {
222 sa_socklen = sizeof(struct sockaddr_un);
224 break;
225 case AF_INET:
226 if (sa_socklen < sizeof(struct sockaddr_in)) {
227 errno = EINVAL;
228 return -1;
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;
238 sa_socklen = sizeof(struct sockaddr_in6);
239 break;
240 #endif
241 default:
242 errno = EAFNOSUPPORT;
243 return -1;
246 if (sa_socklen > sizeof(struct sockaddr_storage)) {
247 errno = EINVAL;
248 return -1;
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;
261 ZERO_STRUCTP(bsda);
263 memcpy(&bsda->u.ss, sa, sa_socklen);
265 bsda->sa_socklen = sa_socklen;
266 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
267 bsda->u.sa.sa_len = bsda->sa_socklen;
268 #endif
270 *_addr = addr;
271 return 0;
274 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
275 struct sockaddr *sa,
276 size_t sa_socklen)
278 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
279 struct tsocket_address_bsd);
281 if (!bsda) {
282 errno = EINVAL;
283 return -1;
286 if (sa_socklen < bsda->sa_socklen) {
287 errno = EINVAL;
288 return -1;
291 if (sa_socklen > bsda->sa_socklen) {
292 memset(sa, 0, sa_socklen);
293 sa_socklen = bsda->sa_socklen;
296 memcpy(sa, &bsda->u.ss, sa_socklen);
297 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
298 sa->sa_len = sa_socklen;
299 #endif
300 return sa_socklen;
303 bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam)
305 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
306 struct tsocket_address_bsd);
308 if (!bsda) {
309 return false;
312 switch (bsda->u.sa.sa_family) {
313 case AF_INET:
314 if (strcasecmp(fam, "ip") == 0) {
315 return true;
318 if (strcasecmp(fam, "ipv4") == 0) {
319 return true;
322 return false;
323 #ifdef HAVE_IPV6
324 case AF_INET6:
325 if (strcasecmp(fam, "ip") == 0) {
326 return true;
329 if (strcasecmp(fam, "ipv6") == 0) {
330 return true;
333 return false;
334 #endif
337 return false;
340 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
341 const char *fam,
342 const char *addr,
343 uint16_t port,
344 struct tsocket_address **_addr,
345 const char *location)
347 struct addrinfo hints;
348 struct addrinfo *result = NULL;
349 char port_str[6];
350 int ret;
352 ZERO_STRUCT(hints);
354 * we use SOCKET_STREAM here to get just one result
355 * back from getaddrinfo().
357 hints.ai_socktype = SOCK_STREAM;
358 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
360 if (strcasecmp(fam, "ip") == 0) {
361 hints.ai_family = AF_UNSPEC;
362 if (!addr) {
363 #ifdef HAVE_IPV6
364 addr = "::";
365 #else
366 addr = "0.0.0.0";
367 #endif
369 } else if (strcasecmp(fam, "ipv4") == 0) {
370 hints.ai_family = AF_INET;
371 if (!addr) {
372 addr = "0.0.0.0";
374 #ifdef HAVE_IPV6
375 } else if (strcasecmp(fam, "ipv6") == 0) {
376 hints.ai_family = AF_INET6;
377 if (!addr) {
378 addr = "::";
380 #endif
381 } else {
382 errno = EAFNOSUPPORT;
383 return -1;
386 snprintf(port_str, sizeof(port_str) - 1, "%u", port);
388 ret = getaddrinfo(addr, port_str, &hints, &result);
389 if (ret != 0) {
390 switch (ret) {
391 case EAI_FAIL:
392 errno = EINVAL;
393 break;
395 ret = -1;
396 goto done;
399 if (result->ai_socktype != SOCK_STREAM) {
400 errno = EINVAL;
401 ret = -1;
402 goto done;
405 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
406 result->ai_addr,
407 result->ai_addrlen,
408 _addr,
409 location);
411 done:
412 if (result) {
413 freeaddrinfo(result);
415 return ret;
418 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
419 TALLOC_CTX *mem_ctx)
421 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
422 struct tsocket_address_bsd);
423 char addr_str[INET6_ADDRSTRLEN+1];
424 const char *str;
426 if (!bsda) {
427 errno = EINVAL;
428 return NULL;
431 switch (bsda->u.sa.sa_family) {
432 case AF_INET:
433 str = inet_ntop(bsda->u.in.sin_family,
434 &bsda->u.in.sin_addr,
435 addr_str, sizeof(addr_str));
436 break;
437 #ifdef HAVE_IPV6
438 case AF_INET6:
439 str = inet_ntop(bsda->u.in6.sin6_family,
440 &bsda->u.in6.sin6_addr,
441 addr_str, sizeof(addr_str));
442 break;
443 #endif
444 default:
445 errno = EINVAL;
446 return NULL;
449 if (!str) {
450 return NULL;
453 return talloc_strdup(mem_ctx, str);
456 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
458 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
459 struct tsocket_address_bsd);
460 uint16_t port = 0;
462 if (!bsda) {
463 errno = EINVAL;
464 return 0;
467 switch (bsda->u.sa.sa_family) {
468 case AF_INET:
469 port = ntohs(bsda->u.in.sin_port);
470 break;
471 #ifdef HAVE_IPV6
472 case AF_INET6:
473 port = ntohs(bsda->u.in6.sin6_port);
474 break;
475 #endif
476 default:
477 errno = EINVAL;
478 return 0;
481 return port;
484 int tsocket_address_inet_set_port(struct tsocket_address *addr,
485 uint16_t port)
487 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
488 struct tsocket_address_bsd);
490 if (!bsda) {
491 errno = EINVAL;
492 return -1;
495 switch (bsda->u.sa.sa_family) {
496 case AF_INET:
497 bsda->u.in.sin_port = htons(port);
498 break;
499 #ifdef HAVE_IPV6
500 case AF_INET6:
501 bsda->u.in6.sin6_port = htons(port);
502 break;
503 #endif
504 default:
505 errno = EINVAL;
506 return -1;
509 return 0;
512 bool tsocket_address_is_unix(const struct tsocket_address *addr)
514 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
515 struct tsocket_address_bsd);
517 if (!bsda) {
518 return false;
521 switch (bsda->u.sa.sa_family) {
522 case AF_UNIX:
523 return true;
526 return false;
529 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
530 const char *path,
531 struct tsocket_address **_addr,
532 const char *location)
534 struct sockaddr_un un;
535 void *p = &un;
536 int ret;
538 if (!path) {
539 path = "";
542 if (strlen(path) > sizeof(un.sun_path)-1) {
543 errno = ENAMETOOLONG;
544 return -1;
547 ZERO_STRUCT(un);
548 un.sun_family = AF_UNIX;
549 strncpy(un.sun_path, path, sizeof(un.sun_path)-1);
551 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
552 (struct sockaddr *)p,
553 sizeof(un),
554 _addr,
555 location);
557 return ret;
560 char *tsocket_address_unix_path(const struct tsocket_address *addr,
561 TALLOC_CTX *mem_ctx)
563 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
564 struct tsocket_address_bsd);
565 const char *str;
567 if (!bsda) {
568 errno = EINVAL;
569 return NULL;
572 switch (bsda->u.sa.sa_family) {
573 case AF_UNIX:
574 str = bsda->u.un.sun_path;
575 break;
576 default:
577 errno = EINVAL;
578 return NULL;
581 return talloc_strdup(mem_ctx, str);
584 static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
585 TALLOC_CTX *mem_ctx)
587 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
588 struct tsocket_address_bsd);
589 char *str;
590 char *addr_str;
591 const char *prefix = NULL;
592 uint16_t port;
594 switch (bsda->u.sa.sa_family) {
595 case AF_UNIX:
596 return talloc_asprintf(mem_ctx, "unix:%s",
597 bsda->u.un.sun_path);
598 case AF_INET:
599 prefix = "ipv4";
600 break;
601 #ifdef HAVE_IPV6
602 case AF_INET6:
603 prefix = "ipv6";
604 break;
605 #endif
606 default:
607 errno = EINVAL;
608 return NULL;
611 addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
612 if (!addr_str) {
613 return NULL;
616 port = tsocket_address_inet_port(addr);
618 str = talloc_asprintf(mem_ctx, "%s:%s:%u",
619 prefix, addr_str, port);
620 talloc_free(addr_str);
622 return str;
625 static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
626 TALLOC_CTX *mem_ctx,
627 const char *location)
629 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
630 struct tsocket_address_bsd);
631 struct tsocket_address *copy;
632 int ret;
634 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
635 &bsda->u.sa,
636 bsda->sa_socklen,
637 &copy,
638 location);
639 if (ret != 0) {
640 return NULL;
643 return copy;
646 static const struct tsocket_address_ops tsocket_address_bsd_ops = {
647 .name = "bsd",
648 .string = tsocket_address_bsd_string,
649 .copy = tsocket_address_bsd_copy,
652 struct tdgram_bsd {
653 int fd;
655 void *event_ptr;
656 struct tevent_fd *fde;
657 bool optimize_recvfrom;
659 void *readable_private;
660 void (*readable_handler)(void *private_data);
661 void *writeable_private;
662 void (*writeable_handler)(void *private_data);
665 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context *dgram,
666 bool on)
668 struct tdgram_bsd *bsds =
669 talloc_get_type(_tdgram_context_data(dgram),
670 struct tdgram_bsd);
671 bool old;
673 if (bsds == NULL) {
674 /* not a bsd socket */
675 return false;
678 old = bsds->optimize_recvfrom;
679 bsds->optimize_recvfrom = on;
681 return old;
684 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
685 struct tevent_fd *fde,
686 uint16_t flags,
687 void *private_data)
689 struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
690 struct tdgram_bsd);
692 if (flags & TEVENT_FD_WRITE) {
693 bsds->writeable_handler(bsds->writeable_private);
694 return;
696 if (flags & TEVENT_FD_READ) {
697 if (!bsds->readable_handler) {
698 TEVENT_FD_NOT_READABLE(bsds->fde);
699 return;
701 bsds->readable_handler(bsds->readable_private);
702 return;
706 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
707 struct tevent_context *ev,
708 void (*handler)(void *private_data),
709 void *private_data)
711 if (ev == NULL) {
712 if (handler) {
713 errno = EINVAL;
714 return -1;
716 if (!bsds->readable_handler) {
717 return 0;
719 bsds->readable_handler = NULL;
720 bsds->readable_private = NULL;
722 return 0;
725 /* read and write must use the same tevent_context */
726 if (bsds->event_ptr != ev) {
727 if (bsds->readable_handler || bsds->writeable_handler) {
728 errno = EINVAL;
729 return -1;
731 bsds->event_ptr = NULL;
732 TALLOC_FREE(bsds->fde);
735 if (tevent_fd_get_flags(bsds->fde) == 0) {
736 TALLOC_FREE(bsds->fde);
738 bsds->fde = tevent_add_fd(ev, bsds,
739 bsds->fd, TEVENT_FD_READ,
740 tdgram_bsd_fde_handler,
741 bsds);
742 if (!bsds->fde) {
743 errno = ENOMEM;
744 return -1;
747 /* cache the event context we're running on */
748 bsds->event_ptr = ev;
749 } else if (!bsds->readable_handler) {
750 TEVENT_FD_READABLE(bsds->fde);
753 bsds->readable_handler = handler;
754 bsds->readable_private = private_data;
756 return 0;
759 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
760 struct tevent_context *ev,
761 void (*handler)(void *private_data),
762 void *private_data)
764 if (ev == NULL) {
765 if (handler) {
766 errno = EINVAL;
767 return -1;
769 if (!bsds->writeable_handler) {
770 return 0;
772 bsds->writeable_handler = NULL;
773 bsds->writeable_private = NULL;
774 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
776 return 0;
779 /* read and write must use the same tevent_context */
780 if (bsds->event_ptr != ev) {
781 if (bsds->readable_handler || bsds->writeable_handler) {
782 errno = EINVAL;
783 return -1;
785 bsds->event_ptr = NULL;
786 TALLOC_FREE(bsds->fde);
789 if (tevent_fd_get_flags(bsds->fde) == 0) {
790 TALLOC_FREE(bsds->fde);
792 bsds->fde = tevent_add_fd(ev, bsds,
793 bsds->fd, TEVENT_FD_WRITE,
794 tdgram_bsd_fde_handler,
795 bsds);
796 if (!bsds->fde) {
797 errno = ENOMEM;
798 return -1;
801 /* cache the event context we're running on */
802 bsds->event_ptr = ev;
803 } else if (!bsds->writeable_handler) {
804 TEVENT_FD_WRITEABLE(bsds->fde);
807 bsds->writeable_handler = handler;
808 bsds->writeable_private = private_data;
810 return 0;
813 struct tdgram_bsd_recvfrom_state {
814 struct tdgram_context *dgram;
815 bool first_try;
816 uint8_t *buf;
817 size_t len;
818 struct tsocket_address *src;
821 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
823 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
824 struct tdgram_bsd);
826 tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
828 return 0;
831 static void tdgram_bsd_recvfrom_handler(void *private_data);
833 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
834 struct tevent_context *ev,
835 struct tdgram_context *dgram)
837 struct tevent_req *req;
838 struct tdgram_bsd_recvfrom_state *state;
839 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
840 int ret;
842 req = tevent_req_create(mem_ctx, &state,
843 struct tdgram_bsd_recvfrom_state);
844 if (!req) {
845 return NULL;
848 state->dgram = dgram;
849 state->first_try= true;
850 state->buf = NULL;
851 state->len = 0;
852 state->src = NULL;
854 talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
856 if (bsds->fd == -1) {
857 tevent_req_error(req, ENOTCONN);
858 goto post;
863 * this is a fast path, not waiting for the
864 * socket to become explicit readable gains
865 * about 10%-20% performance in benchmark tests.
867 if (bsds->optimize_recvfrom) {
869 * We only do the optimization on
870 * recvfrom if the caller asked for it.
872 * This is needed because in most cases
873 * we preferr to flush send buffers before
874 * receiving incoming requests.
876 tdgram_bsd_recvfrom_handler(req);
877 if (!tevent_req_is_in_progress(req)) {
878 goto post;
882 ret = tdgram_bsd_set_readable_handler(bsds, ev,
883 tdgram_bsd_recvfrom_handler,
884 req);
885 if (ret == -1) {
886 tevent_req_error(req, errno);
887 goto post;
890 return req;
892 post:
893 tevent_req_post(req, ev);
894 return req;
897 static void tdgram_bsd_recvfrom_handler(void *private_data)
899 struct tevent_req *req = talloc_get_type_abort(private_data,
900 struct tevent_req);
901 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
902 struct tdgram_bsd_recvfrom_state);
903 struct tdgram_context *dgram = state->dgram;
904 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
905 struct tsocket_address_bsd *bsda;
906 ssize_t ret;
907 int err;
908 bool retry;
910 ret = tsocket_bsd_pending(bsds->fd);
911 if (state->first_try && ret == 0) {
912 state->first_try = false;
913 /* retry later */
914 return;
916 state->first_try = false;
918 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
919 if (retry) {
920 /* retry later */
921 return;
923 if (tevent_req_error(req, err)) {
924 return;
927 /* note that 'ret' can be 0 here */
928 state->buf = talloc_array(state, uint8_t, ret);
929 if (tevent_req_nomem(state->buf, req)) {
930 return;
932 state->len = ret;
934 state->src = tsocket_address_create(state,
935 &tsocket_address_bsd_ops,
936 &bsda,
937 struct tsocket_address_bsd,
938 __location__ "bsd_recvfrom");
939 if (tevent_req_nomem(state->src, req)) {
940 return;
943 ZERO_STRUCTP(bsda);
944 bsda->sa_socklen = sizeof(bsda->u.ss);
945 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
946 bsda->u.sa.sa_len = bsda->sa_socklen;
947 #endif
949 ret = recvfrom(bsds->fd, state->buf, state->len, 0,
950 &bsda->u.sa, &bsda->sa_socklen);
951 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
952 if (retry) {
953 /* retry later */
954 return;
956 if (tevent_req_error(req, err)) {
957 return;
961 * Some systems (FreeBSD, see bug #7115) return too much
962 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
963 * the return value includes some IP/UDP header bytes,
964 * while recvfrom() just returns the payload.
966 state->buf = talloc_realloc(state, state->buf, uint8_t, ret);
967 if (tevent_req_nomem(state->buf, req)) {
968 return;
970 state->len = ret;
972 tevent_req_done(req);
975 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
976 int *perrno,
977 TALLOC_CTX *mem_ctx,
978 uint8_t **buf,
979 struct tsocket_address **src)
981 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
982 struct tdgram_bsd_recvfrom_state);
983 ssize_t ret;
985 ret = tsocket_simple_int_recv(req, perrno);
986 if (ret == 0) {
987 *buf = talloc_move(mem_ctx, &state->buf);
988 ret = state->len;
989 if (src) {
990 *src = talloc_move(mem_ctx, &state->src);
994 tevent_req_received(req);
995 return ret;
998 struct tdgram_bsd_sendto_state {
999 struct tdgram_context *dgram;
1001 const uint8_t *buf;
1002 size_t len;
1003 const struct tsocket_address *dst;
1005 ssize_t ret;
1008 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
1010 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
1011 struct tdgram_bsd);
1013 tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1015 return 0;
1018 static void tdgram_bsd_sendto_handler(void *private_data);
1020 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
1021 struct tevent_context *ev,
1022 struct tdgram_context *dgram,
1023 const uint8_t *buf,
1024 size_t len,
1025 const struct tsocket_address *dst)
1027 struct tevent_req *req;
1028 struct tdgram_bsd_sendto_state *state;
1029 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1030 int ret;
1032 req = tevent_req_create(mem_ctx, &state,
1033 struct tdgram_bsd_sendto_state);
1034 if (!req) {
1035 return NULL;
1038 state->dgram = dgram;
1039 state->buf = buf;
1040 state->len = len;
1041 state->dst = dst;
1042 state->ret = -1;
1044 talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
1046 if (bsds->fd == -1) {
1047 tevent_req_error(req, ENOTCONN);
1048 goto post;
1052 * this is a fast path, not waiting for the
1053 * socket to become explicit writeable gains
1054 * about 10%-20% performance in benchmark tests.
1056 tdgram_bsd_sendto_handler(req);
1057 if (!tevent_req_is_in_progress(req)) {
1058 goto post;
1061 ret = tdgram_bsd_set_writeable_handler(bsds, ev,
1062 tdgram_bsd_sendto_handler,
1063 req);
1064 if (ret == -1) {
1065 tevent_req_error(req, errno);
1066 goto post;
1069 return req;
1071 post:
1072 tevent_req_post(req, ev);
1073 return req;
1076 static void tdgram_bsd_sendto_handler(void *private_data)
1078 struct tevent_req *req = talloc_get_type_abort(private_data,
1079 struct tevent_req);
1080 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1081 struct tdgram_bsd_sendto_state);
1082 struct tdgram_context *dgram = state->dgram;
1083 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1084 struct sockaddr *sa = NULL;
1085 socklen_t sa_socklen = 0;
1086 ssize_t ret;
1087 int err;
1088 bool retry;
1090 if (state->dst) {
1091 struct tsocket_address_bsd *bsda =
1092 talloc_get_type(state->dst->private_data,
1093 struct tsocket_address_bsd);
1095 sa = &bsda->u.sa;
1096 sa_socklen = bsda->sa_socklen;
1099 ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_socklen);
1100 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1101 if (retry) {
1102 /* retry later */
1103 return;
1105 if (tevent_req_error(req, err)) {
1106 return;
1109 state->ret = ret;
1111 tevent_req_done(req);
1114 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1116 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1117 struct tdgram_bsd_sendto_state);
1118 ssize_t ret;
1120 ret = tsocket_simple_int_recv(req, perrno);
1121 if (ret == 0) {
1122 ret = state->ret;
1125 tevent_req_received(req);
1126 return ret;
1129 struct tdgram_bsd_disconnect_state {
1130 uint8_t __dummy;
1133 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1134 struct tevent_context *ev,
1135 struct tdgram_context *dgram)
1137 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1138 struct tevent_req *req;
1139 struct tdgram_bsd_disconnect_state *state;
1140 int ret;
1141 int err;
1142 bool dummy;
1144 req = tevent_req_create(mem_ctx, &state,
1145 struct tdgram_bsd_disconnect_state);
1146 if (req == NULL) {
1147 return NULL;
1150 if (bsds->fd == -1) {
1151 tevent_req_error(req, ENOTCONN);
1152 goto post;
1155 TALLOC_FREE(bsds->fde);
1156 ret = close(bsds->fd);
1157 bsds->fd = -1;
1158 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1159 if (tevent_req_error(req, err)) {
1160 goto post;
1163 tevent_req_done(req);
1164 post:
1165 tevent_req_post(req, ev);
1166 return req;
1169 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1170 int *perrno)
1172 int ret;
1174 ret = tsocket_simple_int_recv(req, perrno);
1176 tevent_req_received(req);
1177 return ret;
1180 static const struct tdgram_context_ops tdgram_bsd_ops = {
1181 .name = "bsd",
1183 .recvfrom_send = tdgram_bsd_recvfrom_send,
1184 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1186 .sendto_send = tdgram_bsd_sendto_send,
1187 .sendto_recv = tdgram_bsd_sendto_recv,
1189 .disconnect_send = tdgram_bsd_disconnect_send,
1190 .disconnect_recv = tdgram_bsd_disconnect_recv,
1193 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1195 TALLOC_FREE(bsds->fde);
1196 if (bsds->fd != -1) {
1197 close(bsds->fd);
1198 bsds->fd = -1;
1200 return 0;
1203 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1204 const struct tsocket_address *remote,
1205 bool broadcast,
1206 TALLOC_CTX *mem_ctx,
1207 struct tdgram_context **_dgram,
1208 const char *location)
1210 struct tsocket_address_bsd *lbsda =
1211 talloc_get_type_abort(local->private_data,
1212 struct tsocket_address_bsd);
1213 struct tsocket_address_bsd *rbsda = NULL;
1214 struct tdgram_context *dgram;
1215 struct tdgram_bsd *bsds;
1216 int fd;
1217 int ret;
1218 bool do_bind = false;
1219 bool do_reuseaddr = false;
1220 bool do_ipv6only = false;
1221 bool is_inet = false;
1222 int sa_fam = lbsda->u.sa.sa_family;
1224 if (remote) {
1225 rbsda = talloc_get_type_abort(remote->private_data,
1226 struct tsocket_address_bsd);
1229 switch (lbsda->u.sa.sa_family) {
1230 case AF_UNIX:
1231 if (broadcast) {
1232 errno = EINVAL;
1233 return -1;
1235 if (lbsda->u.un.sun_path[0] != 0) {
1236 do_reuseaddr = true;
1237 do_bind = true;
1239 break;
1240 case AF_INET:
1241 if (lbsda->u.in.sin_port != 0) {
1242 do_reuseaddr = true;
1243 do_bind = true;
1245 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1246 do_bind = true;
1248 is_inet = true;
1249 break;
1250 #ifdef HAVE_IPV6
1251 case AF_INET6:
1252 if (lbsda->u.in6.sin6_port != 0) {
1253 do_reuseaddr = true;
1254 do_bind = true;
1256 if (memcmp(&in6addr_any,
1257 &lbsda->u.in6.sin6_addr,
1258 sizeof(in6addr_any)) != 0) {
1259 do_bind = true;
1261 is_inet = true;
1262 do_ipv6only = true;
1263 break;
1264 #endif
1265 default:
1266 errno = EINVAL;
1267 return -1;
1270 if (!do_bind && is_inet && rbsda) {
1271 sa_fam = rbsda->u.sa.sa_family;
1272 switch (sa_fam) {
1273 case AF_INET:
1274 do_ipv6only = false;
1275 break;
1276 #ifdef HAVE_IPV6
1277 case AF_INET6:
1278 do_ipv6only = true;
1279 break;
1280 #endif
1284 fd = socket(sa_fam, SOCK_DGRAM, 0);
1285 if (fd < 0) {
1286 return -1;
1289 fd = tsocket_bsd_common_prepare_fd(fd, true);
1290 if (fd < 0) {
1291 return -1;
1294 dgram = tdgram_context_create(mem_ctx,
1295 &tdgram_bsd_ops,
1296 &bsds,
1297 struct tdgram_bsd,
1298 location);
1299 if (!dgram) {
1300 int saved_errno = errno;
1301 close(fd);
1302 errno = saved_errno;
1303 return -1;
1305 ZERO_STRUCTP(bsds);
1306 bsds->fd = fd;
1307 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1309 #ifdef HAVE_IPV6
1310 if (do_ipv6only) {
1311 int val = 1;
1313 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1314 (const void *)&val, sizeof(val));
1315 if (ret == -1) {
1316 int saved_errno = errno;
1317 talloc_free(dgram);
1318 errno = saved_errno;
1319 return -1;
1322 #endif
1324 if (broadcast) {
1325 int val = 1;
1327 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1328 (const void *)&val, sizeof(val));
1329 if (ret == -1) {
1330 int saved_errno = errno;
1331 talloc_free(dgram);
1332 errno = saved_errno;
1333 return -1;
1337 if (do_reuseaddr) {
1338 int val = 1;
1340 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1341 (const void *)&val, sizeof(val));
1342 if (ret == -1) {
1343 int saved_errno = errno;
1344 talloc_free(dgram);
1345 errno = saved_errno;
1346 return -1;
1350 if (do_bind) {
1351 ret = bind(fd, &lbsda->u.sa, lbsda->sa_socklen);
1352 if (ret == -1) {
1353 int saved_errno = errno;
1354 talloc_free(dgram);
1355 errno = saved_errno;
1356 return -1;
1360 if (rbsda) {
1361 if (rbsda->u.sa.sa_family != sa_fam) {
1362 talloc_free(dgram);
1363 errno = EINVAL;
1364 return -1;
1367 ret = connect(fd, &rbsda->u.sa, rbsda->sa_socklen);
1368 if (ret == -1) {
1369 int saved_errno = errno;
1370 talloc_free(dgram);
1371 errno = saved_errno;
1372 return -1;
1376 *_dgram = dgram;
1377 return 0;
1380 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1381 const struct tsocket_address *remote,
1382 TALLOC_CTX *mem_ctx,
1383 struct tdgram_context **dgram,
1384 const char *location)
1386 struct tsocket_address_bsd *lbsda =
1387 talloc_get_type_abort(local->private_data,
1388 struct tsocket_address_bsd);
1389 int ret;
1391 switch (lbsda->u.sa.sa_family) {
1392 case AF_INET:
1393 break;
1394 #ifdef HAVE_IPV6
1395 case AF_INET6:
1396 break;
1397 #endif
1398 default:
1399 errno = EINVAL;
1400 return -1;
1403 ret = tdgram_bsd_dgram_socket(local, remote, false,
1404 mem_ctx, dgram, location);
1406 return ret;
1409 int _tdgram_unix_socket(const struct tsocket_address *local,
1410 const struct tsocket_address *remote,
1411 TALLOC_CTX *mem_ctx,
1412 struct tdgram_context **dgram,
1413 const char *location)
1415 struct tsocket_address_bsd *lbsda =
1416 talloc_get_type_abort(local->private_data,
1417 struct tsocket_address_bsd);
1418 int ret;
1420 switch (lbsda->u.sa.sa_family) {
1421 case AF_UNIX:
1422 break;
1423 default:
1424 errno = EINVAL;
1425 return -1;
1428 ret = tdgram_bsd_dgram_socket(local, remote, false,
1429 mem_ctx, dgram, location);
1431 return ret;
1434 struct tstream_bsd {
1435 int fd;
1437 void *event_ptr;
1438 struct tevent_fd *fde;
1439 bool optimize_readv;
1441 void *readable_private;
1442 void (*readable_handler)(void *private_data);
1443 void *writeable_private;
1444 void (*writeable_handler)(void *private_data);
1447 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
1448 bool on)
1450 struct tstream_bsd *bsds =
1451 talloc_get_type(_tstream_context_data(stream),
1452 struct tstream_bsd);
1453 bool old;
1455 if (bsds == NULL) {
1456 /* not a bsd socket */
1457 return false;
1460 old = bsds->optimize_readv;
1461 bsds->optimize_readv = on;
1463 return old;
1466 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1467 struct tevent_fd *fde,
1468 uint16_t flags,
1469 void *private_data)
1471 struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1472 struct tstream_bsd);
1474 if (flags & TEVENT_FD_WRITE) {
1475 bsds->writeable_handler(bsds->writeable_private);
1476 return;
1478 if (flags & TEVENT_FD_READ) {
1479 if (!bsds->readable_handler) {
1480 if (bsds->writeable_handler) {
1481 bsds->writeable_handler(bsds->writeable_private);
1482 return;
1484 TEVENT_FD_NOT_READABLE(bsds->fde);
1485 return;
1487 bsds->readable_handler(bsds->readable_private);
1488 return;
1492 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1493 struct tevent_context *ev,
1494 void (*handler)(void *private_data),
1495 void *private_data)
1497 if (ev == NULL) {
1498 if (handler) {
1499 errno = EINVAL;
1500 return -1;
1502 if (!bsds->readable_handler) {
1503 return 0;
1505 bsds->readable_handler = NULL;
1506 bsds->readable_private = NULL;
1508 return 0;
1511 /* read and write must use the same tevent_context */
1512 if (bsds->event_ptr != ev) {
1513 if (bsds->readable_handler || bsds->writeable_handler) {
1514 errno = EINVAL;
1515 return -1;
1517 bsds->event_ptr = NULL;
1518 TALLOC_FREE(bsds->fde);
1521 if (tevent_fd_get_flags(bsds->fde) == 0) {
1522 TALLOC_FREE(bsds->fde);
1524 bsds->fde = tevent_add_fd(ev, bsds,
1525 bsds->fd, TEVENT_FD_READ,
1526 tstream_bsd_fde_handler,
1527 bsds);
1528 if (!bsds->fde) {
1529 errno = ENOMEM;
1530 return -1;
1533 /* cache the event context we're running on */
1534 bsds->event_ptr = ev;
1535 } else if (!bsds->readable_handler) {
1536 TEVENT_FD_READABLE(bsds->fde);
1539 bsds->readable_handler = handler;
1540 bsds->readable_private = private_data;
1542 return 0;
1545 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1546 struct tevent_context *ev,
1547 void (*handler)(void *private_data),
1548 void *private_data)
1550 if (ev == NULL) {
1551 if (handler) {
1552 errno = EINVAL;
1553 return -1;
1555 if (!bsds->writeable_handler) {
1556 return 0;
1558 bsds->writeable_handler = NULL;
1559 bsds->writeable_private = NULL;
1560 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1562 return 0;
1565 /* read and write must use the same tevent_context */
1566 if (bsds->event_ptr != ev) {
1567 if (bsds->readable_handler || bsds->writeable_handler) {
1568 errno = EINVAL;
1569 return -1;
1571 bsds->event_ptr = NULL;
1572 TALLOC_FREE(bsds->fde);
1575 if (tevent_fd_get_flags(bsds->fde) == 0) {
1576 TALLOC_FREE(bsds->fde);
1578 bsds->fde = tevent_add_fd(ev, bsds,
1579 bsds->fd,
1580 TEVENT_FD_READ | TEVENT_FD_WRITE,
1581 tstream_bsd_fde_handler,
1582 bsds);
1583 if (!bsds->fde) {
1584 errno = ENOMEM;
1585 return -1;
1588 /* cache the event context we're running on */
1589 bsds->event_ptr = ev;
1590 } else if (!bsds->writeable_handler) {
1591 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1592 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1593 tevent_fd_set_flags(bsds->fde, flags);
1596 bsds->writeable_handler = handler;
1597 bsds->writeable_private = private_data;
1599 return 0;
1602 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1604 struct tstream_bsd *bsds = tstream_context_data(stream,
1605 struct tstream_bsd);
1606 ssize_t ret;
1608 if (bsds->fd == -1) {
1609 errno = ENOTCONN;
1610 return -1;
1613 ret = tsocket_bsd_pending(bsds->fd);
1615 return ret;
1618 struct tstream_bsd_readv_state {
1619 struct tstream_context *stream;
1621 struct iovec *vector;
1622 size_t count;
1624 int ret;
1627 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1629 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1630 struct tstream_bsd);
1632 tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1634 return 0;
1637 static void tstream_bsd_readv_handler(void *private_data);
1639 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1640 struct tevent_context *ev,
1641 struct tstream_context *stream,
1642 struct iovec *vector,
1643 size_t count)
1645 struct tevent_req *req;
1646 struct tstream_bsd_readv_state *state;
1647 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1648 int ret;
1650 req = tevent_req_create(mem_ctx, &state,
1651 struct tstream_bsd_readv_state);
1652 if (!req) {
1653 return NULL;
1656 state->stream = stream;
1657 /* we make a copy of the vector so that we can modify it */
1658 state->vector = talloc_array(state, struct iovec, count);
1659 if (tevent_req_nomem(state->vector, req)) {
1660 goto post;
1662 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1663 state->count = count;
1664 state->ret = 0;
1666 talloc_set_destructor(state, tstream_bsd_readv_destructor);
1668 if (bsds->fd == -1) {
1669 tevent_req_error(req, ENOTCONN);
1670 goto post;
1674 * this is a fast path, not waiting for the
1675 * socket to become explicit readable gains
1676 * about 10%-20% performance in benchmark tests.
1678 if (bsds->optimize_readv) {
1680 * We only do the optimization on
1681 * readv if the caller asked for it.
1683 * This is needed because in most cases
1684 * we preferr to flush send buffers before
1685 * receiving incoming requests.
1687 tstream_bsd_readv_handler(req);
1688 if (!tevent_req_is_in_progress(req)) {
1689 goto post;
1693 ret = tstream_bsd_set_readable_handler(bsds, ev,
1694 tstream_bsd_readv_handler,
1695 req);
1696 if (ret == -1) {
1697 tevent_req_error(req, errno);
1698 goto post;
1701 return req;
1703 post:
1704 tevent_req_post(req, ev);
1705 return req;
1708 static void tstream_bsd_readv_handler(void *private_data)
1710 struct tevent_req *req = talloc_get_type_abort(private_data,
1711 struct tevent_req);
1712 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1713 struct tstream_bsd_readv_state);
1714 struct tstream_context *stream = state->stream;
1715 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1716 int ret;
1717 int err;
1718 bool retry;
1720 ret = readv(bsds->fd, state->vector, state->count);
1721 if (ret == 0) {
1722 /* propagate end of file */
1723 tevent_req_error(req, EPIPE);
1724 return;
1726 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1727 if (retry) {
1728 /* retry later */
1729 return;
1731 if (tevent_req_error(req, err)) {
1732 return;
1735 state->ret += ret;
1737 while (ret > 0) {
1738 if (ret < state->vector[0].iov_len) {
1739 uint8_t *base;
1740 base = (uint8_t *)state->vector[0].iov_base;
1741 base += ret;
1742 state->vector[0].iov_base = (void *)base;
1743 state->vector[0].iov_len -= ret;
1744 break;
1746 ret -= state->vector[0].iov_len;
1747 state->vector += 1;
1748 state->count -= 1;
1752 * there're maybe some empty vectors at the end
1753 * which we need to skip, otherwise we would get
1754 * ret == 0 from the readv() call and return EPIPE
1756 while (state->count > 0) {
1757 if (state->vector[0].iov_len > 0) {
1758 break;
1760 state->vector += 1;
1761 state->count -= 1;
1764 if (state->count > 0) {
1765 /* we have more to read */
1766 return;
1769 tevent_req_done(req);
1772 static int tstream_bsd_readv_recv(struct tevent_req *req,
1773 int *perrno)
1775 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1776 struct tstream_bsd_readv_state);
1777 int ret;
1779 ret = tsocket_simple_int_recv(req, perrno);
1780 if (ret == 0) {
1781 ret = state->ret;
1784 tevent_req_received(req);
1785 return ret;
1788 struct tstream_bsd_writev_state {
1789 struct tstream_context *stream;
1791 struct iovec *vector;
1792 size_t count;
1794 int ret;
1797 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1799 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1800 struct tstream_bsd);
1802 tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1804 return 0;
1807 static void tstream_bsd_writev_handler(void *private_data);
1809 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1810 struct tevent_context *ev,
1811 struct tstream_context *stream,
1812 const struct iovec *vector,
1813 size_t count)
1815 struct tevent_req *req;
1816 struct tstream_bsd_writev_state *state;
1817 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1818 int ret;
1820 req = tevent_req_create(mem_ctx, &state,
1821 struct tstream_bsd_writev_state);
1822 if (!req) {
1823 return NULL;
1826 state->stream = stream;
1827 /* we make a copy of the vector so that we can modify it */
1828 state->vector = talloc_array(state, struct iovec, count);
1829 if (tevent_req_nomem(state->vector, req)) {
1830 goto post;
1832 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1833 state->count = count;
1834 state->ret = 0;
1836 talloc_set_destructor(state, tstream_bsd_writev_destructor);
1838 if (bsds->fd == -1) {
1839 tevent_req_error(req, ENOTCONN);
1840 goto post;
1844 * this is a fast path, not waiting for the
1845 * socket to become explicit writeable gains
1846 * about 10%-20% performance in benchmark tests.
1848 tstream_bsd_writev_handler(req);
1849 if (!tevent_req_is_in_progress(req)) {
1850 goto post;
1853 ret = tstream_bsd_set_writeable_handler(bsds, ev,
1854 tstream_bsd_writev_handler,
1855 req);
1856 if (ret == -1) {
1857 tevent_req_error(req, errno);
1858 goto post;
1861 return req;
1863 post:
1864 tevent_req_post(req, ev);
1865 return req;
1868 static void tstream_bsd_writev_handler(void *private_data)
1870 struct tevent_req *req = talloc_get_type_abort(private_data,
1871 struct tevent_req);
1872 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1873 struct tstream_bsd_writev_state);
1874 struct tstream_context *stream = state->stream;
1875 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1876 ssize_t ret;
1877 int err;
1878 bool retry;
1880 ret = writev(bsds->fd, state->vector, state->count);
1881 if (ret == 0) {
1882 /* propagate end of file */
1883 tevent_req_error(req, EPIPE);
1884 return;
1886 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1887 if (retry) {
1888 /* retry later */
1889 return;
1891 if (tevent_req_error(req, err)) {
1892 return;
1895 state->ret += ret;
1897 while (ret > 0) {
1898 if (ret < state->vector[0].iov_len) {
1899 uint8_t *base;
1900 base = (uint8_t *)state->vector[0].iov_base;
1901 base += ret;
1902 state->vector[0].iov_base = (void *)base;
1903 state->vector[0].iov_len -= ret;
1904 break;
1906 ret -= state->vector[0].iov_len;
1907 state->vector += 1;
1908 state->count -= 1;
1912 * there're maybe some empty vectors at the end
1913 * which we need to skip, otherwise we would get
1914 * ret == 0 from the writev() call and return EPIPE
1916 while (state->count > 0) {
1917 if (state->vector[0].iov_len > 0) {
1918 break;
1920 state->vector += 1;
1921 state->count -= 1;
1924 if (state->count > 0) {
1925 /* we have more to read */
1926 return;
1929 tevent_req_done(req);
1932 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1934 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1935 struct tstream_bsd_writev_state);
1936 int ret;
1938 ret = tsocket_simple_int_recv(req, perrno);
1939 if (ret == 0) {
1940 ret = state->ret;
1943 tevent_req_received(req);
1944 return ret;
1947 struct tstream_bsd_disconnect_state {
1948 void *__dummy;
1951 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1952 struct tevent_context *ev,
1953 struct tstream_context *stream)
1955 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1956 struct tevent_req *req;
1957 struct tstream_bsd_disconnect_state *state;
1958 int ret;
1959 int err;
1960 bool dummy;
1962 req = tevent_req_create(mem_ctx, &state,
1963 struct tstream_bsd_disconnect_state);
1964 if (req == NULL) {
1965 return NULL;
1968 if (bsds->fd == -1) {
1969 tevent_req_error(req, ENOTCONN);
1970 goto post;
1973 TALLOC_FREE(bsds->fde);
1974 ret = close(bsds->fd);
1975 bsds->fd = -1;
1976 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1977 if (tevent_req_error(req, err)) {
1978 goto post;
1981 tevent_req_done(req);
1982 post:
1983 tevent_req_post(req, ev);
1984 return req;
1987 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
1988 int *perrno)
1990 int ret;
1992 ret = tsocket_simple_int_recv(req, perrno);
1994 tevent_req_received(req);
1995 return ret;
1998 static const struct tstream_context_ops tstream_bsd_ops = {
1999 .name = "bsd",
2001 .pending_bytes = tstream_bsd_pending_bytes,
2003 .readv_send = tstream_bsd_readv_send,
2004 .readv_recv = tstream_bsd_readv_recv,
2006 .writev_send = tstream_bsd_writev_send,
2007 .writev_recv = tstream_bsd_writev_recv,
2009 .disconnect_send = tstream_bsd_disconnect_send,
2010 .disconnect_recv = tstream_bsd_disconnect_recv,
2013 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
2015 TALLOC_FREE(bsds->fde);
2016 if (bsds->fd != -1) {
2017 close(bsds->fd);
2018 bsds->fd = -1;
2020 return 0;
2023 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
2024 int fd,
2025 struct tstream_context **_stream,
2026 const char *location)
2028 struct tstream_context *stream;
2029 struct tstream_bsd *bsds;
2031 stream = tstream_context_create(mem_ctx,
2032 &tstream_bsd_ops,
2033 &bsds,
2034 struct tstream_bsd,
2035 location);
2036 if (!stream) {
2037 return -1;
2039 ZERO_STRUCTP(bsds);
2040 bsds->fd = fd;
2041 talloc_set_destructor(bsds, tstream_bsd_destructor);
2043 *_stream = stream;
2044 return 0;
2047 struct tstream_bsd_connect_state {
2048 int fd;
2049 struct tevent_fd *fde;
2050 struct tstream_conext *stream;
2051 struct tsocket_address *local;
2054 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
2056 TALLOC_FREE(state->fde);
2057 if (state->fd != -1) {
2058 close(state->fd);
2059 state->fd = -1;
2062 return 0;
2065 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2066 struct tevent_fd *fde,
2067 uint16_t flags,
2068 void *private_data);
2070 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2071 struct tevent_context *ev,
2072 int sys_errno,
2073 const struct tsocket_address *local,
2074 const struct tsocket_address *remote)
2076 struct tevent_req *req;
2077 struct tstream_bsd_connect_state *state;
2078 struct tsocket_address_bsd *lbsda =
2079 talloc_get_type_abort(local->private_data,
2080 struct tsocket_address_bsd);
2081 struct tsocket_address_bsd *lrbsda = NULL;
2082 struct tsocket_address_bsd *rbsda =
2083 talloc_get_type_abort(remote->private_data,
2084 struct tsocket_address_bsd);
2085 int ret;
2086 int err;
2087 bool retry;
2088 bool do_bind = false;
2089 bool do_reuseaddr = false;
2090 bool do_ipv6only = false;
2091 bool is_inet = false;
2092 int sa_fam = lbsda->u.sa.sa_family;
2094 req = tevent_req_create(mem_ctx, &state,
2095 struct tstream_bsd_connect_state);
2096 if (!req) {
2097 return NULL;
2099 state->fd = -1;
2100 state->fde = NULL;
2102 talloc_set_destructor(state, tstream_bsd_connect_destructor);
2104 /* give the wrappers a chance to report an error */
2105 if (sys_errno != 0) {
2106 tevent_req_error(req, sys_errno);
2107 goto post;
2110 switch (lbsda->u.sa.sa_family) {
2111 case AF_UNIX:
2112 if (lbsda->u.un.sun_path[0] != 0) {
2113 do_reuseaddr = true;
2114 do_bind = true;
2116 break;
2117 case AF_INET:
2118 if (lbsda->u.in.sin_port != 0) {
2119 do_reuseaddr = true;
2120 do_bind = true;
2122 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2123 do_bind = true;
2125 is_inet = true;
2126 break;
2127 #ifdef HAVE_IPV6
2128 case AF_INET6:
2129 if (lbsda->u.in6.sin6_port != 0) {
2130 do_reuseaddr = true;
2131 do_bind = true;
2133 if (memcmp(&in6addr_any,
2134 &lbsda->u.in6.sin6_addr,
2135 sizeof(in6addr_any)) != 0) {
2136 do_bind = true;
2138 is_inet = true;
2139 do_ipv6only = true;
2140 break;
2141 #endif
2142 default:
2143 tevent_req_error(req, EINVAL);
2144 goto post;
2147 if (!do_bind && is_inet) {
2148 sa_fam = rbsda->u.sa.sa_family;
2149 switch (sa_fam) {
2150 case AF_INET:
2151 do_ipv6only = false;
2152 break;
2153 #ifdef HAVE_IPV6
2154 case AF_INET6:
2155 do_ipv6only = true;
2156 break;
2157 #endif
2161 if (is_inet) {
2162 state->local = tsocket_address_create(state,
2163 &tsocket_address_bsd_ops,
2164 &lrbsda,
2165 struct tsocket_address_bsd,
2166 __location__ "bsd_connect");
2167 if (tevent_req_nomem(state->local, req)) {
2168 goto post;
2171 ZERO_STRUCTP(lrbsda);
2172 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2173 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2174 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2175 #endif
2178 state->fd = socket(sa_fam, SOCK_STREAM, 0);
2179 if (state->fd == -1) {
2180 tevent_req_error(req, errno);
2181 goto post;
2184 state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2185 if (state->fd == -1) {
2186 tevent_req_error(req, errno);
2187 goto post;
2190 #ifdef HAVE_IPV6
2191 if (do_ipv6only) {
2192 int val = 1;
2194 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2195 (const void *)&val, sizeof(val));
2196 if (ret == -1) {
2197 tevent_req_error(req, errno);
2198 goto post;
2201 #endif
2203 if (do_reuseaddr) {
2204 int val = 1;
2206 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2207 (const void *)&val, sizeof(val));
2208 if (ret == -1) {
2209 tevent_req_error(req, errno);
2210 goto post;
2214 if (do_bind) {
2215 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2216 if (ret == -1) {
2217 tevent_req_error(req, errno);
2218 goto post;
2222 if (rbsda->u.sa.sa_family != sa_fam) {
2223 tevent_req_error(req, EINVAL);
2224 goto post;
2227 ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2228 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2229 if (retry) {
2230 /* retry later */
2231 goto async;
2233 if (tevent_req_error(req, err)) {
2234 goto post;
2237 if (!state->local) {
2238 tevent_req_done(req);
2239 goto post;
2242 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2243 if (ret == -1) {
2244 tevent_req_error(req, errno);
2245 goto post;
2248 tevent_req_done(req);
2249 goto post;
2251 async:
2252 state->fde = tevent_add_fd(ev, state,
2253 state->fd,
2254 TEVENT_FD_READ | TEVENT_FD_WRITE,
2255 tstream_bsd_connect_fde_handler,
2256 req);
2257 if (tevent_req_nomem(state->fde, req)) {
2258 goto post;
2261 return req;
2263 post:
2264 tevent_req_post(req, ev);
2265 return req;
2268 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2269 struct tevent_fd *fde,
2270 uint16_t flags,
2271 void *private_data)
2273 struct tevent_req *req = talloc_get_type_abort(private_data,
2274 struct tevent_req);
2275 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2276 struct tstream_bsd_connect_state);
2277 struct tsocket_address_bsd *lrbsda = NULL;
2278 int ret;
2279 int error=0;
2280 socklen_t len = sizeof(error);
2281 int err;
2282 bool retry;
2284 ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2285 if (ret == 0) {
2286 if (error != 0) {
2287 errno = error;
2288 ret = -1;
2291 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2292 if (retry) {
2293 /* retry later */
2294 return;
2296 if (tevent_req_error(req, err)) {
2297 return;
2300 if (!state->local) {
2301 tevent_req_done(req);
2302 return;
2305 lrbsda = talloc_get_type_abort(state->local->private_data,
2306 struct tsocket_address_bsd);
2308 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2309 if (ret == -1) {
2310 tevent_req_error(req, errno);
2311 return;
2314 tevent_req_done(req);
2317 static int tstream_bsd_connect_recv(struct tevent_req *req,
2318 int *perrno,
2319 TALLOC_CTX *mem_ctx,
2320 struct tstream_context **stream,
2321 struct tsocket_address **local,
2322 const char *location)
2324 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2325 struct tstream_bsd_connect_state);
2326 int ret;
2328 ret = tsocket_simple_int_recv(req, perrno);
2329 if (ret == 0) {
2330 ret = _tstream_bsd_existing_socket(mem_ctx,
2331 state->fd,
2332 stream,
2333 location);
2334 if (ret == -1) {
2335 *perrno = errno;
2336 goto done;
2338 TALLOC_FREE(state->fde);
2339 state->fd = -1;
2341 if (local) {
2342 *local = talloc_move(mem_ctx, &state->local);
2346 done:
2347 tevent_req_received(req);
2348 return ret;
2351 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2352 struct tevent_context *ev,
2353 const struct tsocket_address *local,
2354 const struct tsocket_address *remote)
2356 struct tsocket_address_bsd *lbsda =
2357 talloc_get_type_abort(local->private_data,
2358 struct tsocket_address_bsd);
2359 struct tevent_req *req;
2360 int sys_errno = 0;
2362 switch (lbsda->u.sa.sa_family) {
2363 case AF_INET:
2364 break;
2365 #ifdef HAVE_IPV6
2366 case AF_INET6:
2367 break;
2368 #endif
2369 default:
2370 sys_errno = EINVAL;
2371 break;
2374 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2376 return req;
2379 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2380 int *perrno,
2381 TALLOC_CTX *mem_ctx,
2382 struct tstream_context **stream,
2383 struct tsocket_address **local,
2384 const char *location)
2386 return tstream_bsd_connect_recv(req, perrno,
2387 mem_ctx, stream, local,
2388 location);
2391 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2392 struct tevent_context *ev,
2393 const struct tsocket_address *local,
2394 const struct tsocket_address *remote)
2396 struct tsocket_address_bsd *lbsda =
2397 talloc_get_type_abort(local->private_data,
2398 struct tsocket_address_bsd);
2399 struct tevent_req *req;
2400 int sys_errno = 0;
2402 switch (lbsda->u.sa.sa_family) {
2403 case AF_UNIX:
2404 break;
2405 default:
2406 sys_errno = EINVAL;
2407 break;
2410 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2412 return req;
2415 int _tstream_unix_connect_recv(struct tevent_req *req,
2416 int *perrno,
2417 TALLOC_CTX *mem_ctx,
2418 struct tstream_context **stream,
2419 const char *location)
2421 return tstream_bsd_connect_recv(req, perrno,
2422 mem_ctx, stream, NULL,
2423 location);
2426 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2427 struct tstream_context **_stream1,
2428 TALLOC_CTX *mem_ctx2,
2429 struct tstream_context **_stream2,
2430 const char *location)
2432 int ret;
2433 int fds[2];
2434 int fd1;
2435 int fd2;
2436 struct tstream_context *stream1 = NULL;
2437 struct tstream_context *stream2 = NULL;
2439 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2440 if (ret == -1) {
2441 return -1;
2443 fd1 = fds[0];
2444 fd2 = fds[1];
2446 fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2447 if (fd1 == -1) {
2448 int sys_errno = errno;
2449 close(fd2);
2450 errno = sys_errno;
2451 return -1;
2454 fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2455 if (fd2 == -1) {
2456 int sys_errno = errno;
2457 close(fd1);
2458 errno = sys_errno;
2459 return -1;
2462 ret = _tstream_bsd_existing_socket(mem_ctx1,
2463 fd1,
2464 &stream1,
2465 location);
2466 if (ret == -1) {
2467 int sys_errno = errno;
2468 close(fd1);
2469 close(fd2);
2470 errno = sys_errno;
2471 return -1;
2474 ret = _tstream_bsd_existing_socket(mem_ctx2,
2475 fd2,
2476 &stream2,
2477 location);
2478 if (ret == -1) {
2479 int sys_errno = errno;
2480 talloc_free(stream1);
2481 close(fd2);
2482 errno = sys_errno;
2483 return -1;
2486 *_stream1 = stream1;
2487 *_stream2 = stream2;
2488 return 0;