shadow_copy2: improve debug in shadow_copy2_convert() in snapdirseverywhere mode
[Samba.git] / lib / tsocket / tsocket_bsd.c
blob4b54d319a0d2fd6aab4d4e3c552656d18e096f92
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;
1106 if (err == EMSGSIZE) {
1107 /* round up in 1K increments */
1108 int bufsize = ((state->len + 1023) & (~1023));
1110 ret = setsockopt(bsds->fd, SOL_SOCKET, SO_SNDBUF, &bufsize,
1111 sizeof(bufsize));
1112 if (ret == 0) {
1114 * We do the rety here, rather then via the
1115 * handler, as we only want to retry once for
1116 * this condition, so if there is a mismatch
1117 * between what setsockopt() accepts and what can
1118 * actually be sent, we do not end up in a
1119 * loop.
1122 ret = sendto(bsds->fd, state->buf, state->len,
1123 0, sa, sa_socklen);
1124 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1125 if (retry) { /* retry later */
1126 return;
1131 if (tevent_req_error(req, err)) {
1132 return;
1135 state->ret = ret;
1137 tevent_req_done(req);
1140 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1142 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1143 struct tdgram_bsd_sendto_state);
1144 ssize_t ret;
1146 ret = tsocket_simple_int_recv(req, perrno);
1147 if (ret == 0) {
1148 ret = state->ret;
1151 tevent_req_received(req);
1152 return ret;
1155 struct tdgram_bsd_disconnect_state {
1156 uint8_t __dummy;
1159 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1160 struct tevent_context *ev,
1161 struct tdgram_context *dgram)
1163 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1164 struct tevent_req *req;
1165 struct tdgram_bsd_disconnect_state *state;
1166 int ret;
1167 int err;
1168 bool dummy;
1170 req = tevent_req_create(mem_ctx, &state,
1171 struct tdgram_bsd_disconnect_state);
1172 if (req == NULL) {
1173 return NULL;
1176 if (bsds->fd == -1) {
1177 tevent_req_error(req, ENOTCONN);
1178 goto post;
1181 TALLOC_FREE(bsds->fde);
1182 ret = close(bsds->fd);
1183 bsds->fd = -1;
1184 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1185 if (tevent_req_error(req, err)) {
1186 goto post;
1189 tevent_req_done(req);
1190 post:
1191 tevent_req_post(req, ev);
1192 return req;
1195 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1196 int *perrno)
1198 int ret;
1200 ret = tsocket_simple_int_recv(req, perrno);
1202 tevent_req_received(req);
1203 return ret;
1206 static const struct tdgram_context_ops tdgram_bsd_ops = {
1207 .name = "bsd",
1209 .recvfrom_send = tdgram_bsd_recvfrom_send,
1210 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1212 .sendto_send = tdgram_bsd_sendto_send,
1213 .sendto_recv = tdgram_bsd_sendto_recv,
1215 .disconnect_send = tdgram_bsd_disconnect_send,
1216 .disconnect_recv = tdgram_bsd_disconnect_recv,
1219 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1221 TALLOC_FREE(bsds->fde);
1222 if (bsds->fd != -1) {
1223 close(bsds->fd);
1224 bsds->fd = -1;
1226 return 0;
1229 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1230 const struct tsocket_address *remote,
1231 bool broadcast,
1232 TALLOC_CTX *mem_ctx,
1233 struct tdgram_context **_dgram,
1234 const char *location)
1236 struct tsocket_address_bsd *lbsda =
1237 talloc_get_type_abort(local->private_data,
1238 struct tsocket_address_bsd);
1239 struct tsocket_address_bsd *rbsda = NULL;
1240 struct tdgram_context *dgram;
1241 struct tdgram_bsd *bsds;
1242 int fd;
1243 int ret;
1244 bool do_bind = false;
1245 bool do_reuseaddr = false;
1246 bool do_ipv6only = false;
1247 bool is_inet = false;
1248 int sa_fam = lbsda->u.sa.sa_family;
1250 if (remote) {
1251 rbsda = talloc_get_type_abort(remote->private_data,
1252 struct tsocket_address_bsd);
1255 switch (lbsda->u.sa.sa_family) {
1256 case AF_UNIX:
1257 if (broadcast) {
1258 errno = EINVAL;
1259 return -1;
1261 if (lbsda->u.un.sun_path[0] != 0) {
1262 do_reuseaddr = true;
1263 do_bind = true;
1265 break;
1266 case AF_INET:
1267 if (lbsda->u.in.sin_port != 0) {
1268 do_reuseaddr = true;
1269 do_bind = true;
1271 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1272 do_bind = true;
1274 is_inet = true;
1275 break;
1276 #ifdef HAVE_IPV6
1277 case AF_INET6:
1278 if (lbsda->u.in6.sin6_port != 0) {
1279 do_reuseaddr = true;
1280 do_bind = true;
1282 if (memcmp(&in6addr_any,
1283 &lbsda->u.in6.sin6_addr,
1284 sizeof(in6addr_any)) != 0) {
1285 do_bind = true;
1287 is_inet = true;
1288 do_ipv6only = true;
1289 break;
1290 #endif
1291 default:
1292 errno = EINVAL;
1293 return -1;
1296 if (!do_bind && is_inet && rbsda) {
1297 sa_fam = rbsda->u.sa.sa_family;
1298 switch (sa_fam) {
1299 case AF_INET:
1300 do_ipv6only = false;
1301 break;
1302 #ifdef HAVE_IPV6
1303 case AF_INET6:
1304 do_ipv6only = true;
1305 break;
1306 #endif
1310 fd = socket(sa_fam, SOCK_DGRAM, 0);
1311 if (fd < 0) {
1312 return -1;
1315 fd = tsocket_bsd_common_prepare_fd(fd, true);
1316 if (fd < 0) {
1317 return -1;
1320 dgram = tdgram_context_create(mem_ctx,
1321 &tdgram_bsd_ops,
1322 &bsds,
1323 struct tdgram_bsd,
1324 location);
1325 if (!dgram) {
1326 int saved_errno = errno;
1327 close(fd);
1328 errno = saved_errno;
1329 return -1;
1331 ZERO_STRUCTP(bsds);
1332 bsds->fd = fd;
1333 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1335 #ifdef HAVE_IPV6
1336 if (do_ipv6only) {
1337 int val = 1;
1339 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1340 (const void *)&val, sizeof(val));
1341 if (ret == -1) {
1342 int saved_errno = errno;
1343 talloc_free(dgram);
1344 errno = saved_errno;
1345 return -1;
1348 #endif
1350 if (broadcast) {
1351 int val = 1;
1353 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1354 (const void *)&val, sizeof(val));
1355 if (ret == -1) {
1356 int saved_errno = errno;
1357 talloc_free(dgram);
1358 errno = saved_errno;
1359 return -1;
1363 if (do_reuseaddr) {
1364 int val = 1;
1366 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1367 (const void *)&val, sizeof(val));
1368 if (ret == -1) {
1369 int saved_errno = errno;
1370 talloc_free(dgram);
1371 errno = saved_errno;
1372 return -1;
1376 if (do_bind) {
1377 ret = bind(fd, &lbsda->u.sa, lbsda->sa_socklen);
1378 if (ret == -1) {
1379 int saved_errno = errno;
1380 talloc_free(dgram);
1381 errno = saved_errno;
1382 return -1;
1386 if (rbsda) {
1387 if (rbsda->u.sa.sa_family != sa_fam) {
1388 talloc_free(dgram);
1389 errno = EINVAL;
1390 return -1;
1393 ret = connect(fd, &rbsda->u.sa, rbsda->sa_socklen);
1394 if (ret == -1) {
1395 int saved_errno = errno;
1396 talloc_free(dgram);
1397 errno = saved_errno;
1398 return -1;
1402 *_dgram = dgram;
1403 return 0;
1406 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1407 const struct tsocket_address *remote,
1408 TALLOC_CTX *mem_ctx,
1409 struct tdgram_context **dgram,
1410 const char *location)
1412 struct tsocket_address_bsd *lbsda =
1413 talloc_get_type_abort(local->private_data,
1414 struct tsocket_address_bsd);
1415 int ret;
1417 switch (lbsda->u.sa.sa_family) {
1418 case AF_INET:
1419 break;
1420 #ifdef HAVE_IPV6
1421 case AF_INET6:
1422 break;
1423 #endif
1424 default:
1425 errno = EINVAL;
1426 return -1;
1429 ret = tdgram_bsd_dgram_socket(local, remote, false,
1430 mem_ctx, dgram, location);
1432 return ret;
1435 int _tdgram_unix_socket(const struct tsocket_address *local,
1436 const struct tsocket_address *remote,
1437 TALLOC_CTX *mem_ctx,
1438 struct tdgram_context **dgram,
1439 const char *location)
1441 struct tsocket_address_bsd *lbsda =
1442 talloc_get_type_abort(local->private_data,
1443 struct tsocket_address_bsd);
1444 int ret;
1446 switch (lbsda->u.sa.sa_family) {
1447 case AF_UNIX:
1448 break;
1449 default:
1450 errno = EINVAL;
1451 return -1;
1454 ret = tdgram_bsd_dgram_socket(local, remote, false,
1455 mem_ctx, dgram, location);
1457 return ret;
1460 struct tstream_bsd {
1461 int fd;
1463 void *event_ptr;
1464 struct tevent_fd *fde;
1465 bool optimize_readv;
1467 void *readable_private;
1468 void (*readable_handler)(void *private_data);
1469 void *writeable_private;
1470 void (*writeable_handler)(void *private_data);
1473 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
1474 bool on)
1476 struct tstream_bsd *bsds =
1477 talloc_get_type(_tstream_context_data(stream),
1478 struct tstream_bsd);
1479 bool old;
1481 if (bsds == NULL) {
1482 /* not a bsd socket */
1483 return false;
1486 old = bsds->optimize_readv;
1487 bsds->optimize_readv = on;
1489 return old;
1492 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1493 struct tevent_fd *fde,
1494 uint16_t flags,
1495 void *private_data)
1497 struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1498 struct tstream_bsd);
1500 if (flags & TEVENT_FD_WRITE) {
1501 bsds->writeable_handler(bsds->writeable_private);
1502 return;
1504 if (flags & TEVENT_FD_READ) {
1505 if (!bsds->readable_handler) {
1506 if (bsds->writeable_handler) {
1507 bsds->writeable_handler(bsds->writeable_private);
1508 return;
1510 TEVENT_FD_NOT_READABLE(bsds->fde);
1511 return;
1513 bsds->readable_handler(bsds->readable_private);
1514 return;
1518 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1519 struct tevent_context *ev,
1520 void (*handler)(void *private_data),
1521 void *private_data)
1523 if (ev == NULL) {
1524 if (handler) {
1525 errno = EINVAL;
1526 return -1;
1528 if (!bsds->readable_handler) {
1529 return 0;
1531 bsds->readable_handler = NULL;
1532 bsds->readable_private = NULL;
1534 return 0;
1537 /* read and write must use the same tevent_context */
1538 if (bsds->event_ptr != ev) {
1539 if (bsds->readable_handler || bsds->writeable_handler) {
1540 errno = EINVAL;
1541 return -1;
1543 bsds->event_ptr = NULL;
1544 TALLOC_FREE(bsds->fde);
1547 if (tevent_fd_get_flags(bsds->fde) == 0) {
1548 TALLOC_FREE(bsds->fde);
1550 bsds->fde = tevent_add_fd(ev, bsds,
1551 bsds->fd, TEVENT_FD_READ,
1552 tstream_bsd_fde_handler,
1553 bsds);
1554 if (!bsds->fde) {
1555 errno = ENOMEM;
1556 return -1;
1559 /* cache the event context we're running on */
1560 bsds->event_ptr = ev;
1561 } else if (!bsds->readable_handler) {
1562 TEVENT_FD_READABLE(bsds->fde);
1565 bsds->readable_handler = handler;
1566 bsds->readable_private = private_data;
1568 return 0;
1571 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1572 struct tevent_context *ev,
1573 void (*handler)(void *private_data),
1574 void *private_data)
1576 if (ev == NULL) {
1577 if (handler) {
1578 errno = EINVAL;
1579 return -1;
1581 if (!bsds->writeable_handler) {
1582 return 0;
1584 bsds->writeable_handler = NULL;
1585 bsds->writeable_private = NULL;
1586 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1588 return 0;
1591 /* read and write must use the same tevent_context */
1592 if (bsds->event_ptr != ev) {
1593 if (bsds->readable_handler || bsds->writeable_handler) {
1594 errno = EINVAL;
1595 return -1;
1597 bsds->event_ptr = NULL;
1598 TALLOC_FREE(bsds->fde);
1601 if (tevent_fd_get_flags(bsds->fde) == 0) {
1602 TALLOC_FREE(bsds->fde);
1604 bsds->fde = tevent_add_fd(ev, bsds,
1605 bsds->fd,
1606 TEVENT_FD_READ | TEVENT_FD_WRITE,
1607 tstream_bsd_fde_handler,
1608 bsds);
1609 if (!bsds->fde) {
1610 errno = ENOMEM;
1611 return -1;
1614 /* cache the event context we're running on */
1615 bsds->event_ptr = ev;
1616 } else if (!bsds->writeable_handler) {
1617 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1618 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1619 tevent_fd_set_flags(bsds->fde, flags);
1622 bsds->writeable_handler = handler;
1623 bsds->writeable_private = private_data;
1625 return 0;
1628 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1630 struct tstream_bsd *bsds = tstream_context_data(stream,
1631 struct tstream_bsd);
1632 ssize_t ret;
1634 if (bsds->fd == -1) {
1635 errno = ENOTCONN;
1636 return -1;
1639 ret = tsocket_bsd_pending(bsds->fd);
1641 return ret;
1644 struct tstream_bsd_readv_state {
1645 struct tstream_context *stream;
1647 struct iovec *vector;
1648 size_t count;
1650 int ret;
1653 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1655 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1656 struct tstream_bsd);
1658 tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1660 return 0;
1663 static void tstream_bsd_readv_handler(void *private_data);
1665 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1666 struct tevent_context *ev,
1667 struct tstream_context *stream,
1668 struct iovec *vector,
1669 size_t count)
1671 struct tevent_req *req;
1672 struct tstream_bsd_readv_state *state;
1673 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1674 int ret;
1676 req = tevent_req_create(mem_ctx, &state,
1677 struct tstream_bsd_readv_state);
1678 if (!req) {
1679 return NULL;
1682 state->stream = stream;
1683 /* we make a copy of the vector so that we can modify it */
1684 state->vector = talloc_array(state, struct iovec, count);
1685 if (tevent_req_nomem(state->vector, req)) {
1686 goto post;
1688 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1689 state->count = count;
1690 state->ret = 0;
1692 talloc_set_destructor(state, tstream_bsd_readv_destructor);
1694 if (bsds->fd == -1) {
1695 tevent_req_error(req, ENOTCONN);
1696 goto post;
1700 * this is a fast path, not waiting for the
1701 * socket to become explicit readable gains
1702 * about 10%-20% performance in benchmark tests.
1704 if (bsds->optimize_readv) {
1706 * We only do the optimization on
1707 * readv if the caller asked for it.
1709 * This is needed because in most cases
1710 * we preferr to flush send buffers before
1711 * receiving incoming requests.
1713 tstream_bsd_readv_handler(req);
1714 if (!tevent_req_is_in_progress(req)) {
1715 goto post;
1719 ret = tstream_bsd_set_readable_handler(bsds, ev,
1720 tstream_bsd_readv_handler,
1721 req);
1722 if (ret == -1) {
1723 tevent_req_error(req, errno);
1724 goto post;
1727 return req;
1729 post:
1730 tevent_req_post(req, ev);
1731 return req;
1734 static void tstream_bsd_readv_handler(void *private_data)
1736 struct tevent_req *req = talloc_get_type_abort(private_data,
1737 struct tevent_req);
1738 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1739 struct tstream_bsd_readv_state);
1740 struct tstream_context *stream = state->stream;
1741 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1742 int ret;
1743 int err;
1744 bool retry;
1746 ret = readv(bsds->fd, state->vector, state->count);
1747 if (ret == 0) {
1748 /* propagate end of file */
1749 tevent_req_error(req, EPIPE);
1750 return;
1752 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1753 if (retry) {
1754 /* retry later */
1755 return;
1757 if (tevent_req_error(req, err)) {
1758 return;
1761 state->ret += ret;
1763 while (ret > 0) {
1764 if (ret < state->vector[0].iov_len) {
1765 uint8_t *base;
1766 base = (uint8_t *)state->vector[0].iov_base;
1767 base += ret;
1768 state->vector[0].iov_base = (void *)base;
1769 state->vector[0].iov_len -= ret;
1770 break;
1772 ret -= state->vector[0].iov_len;
1773 state->vector += 1;
1774 state->count -= 1;
1778 * there're maybe some empty vectors at the end
1779 * which we need to skip, otherwise we would get
1780 * ret == 0 from the readv() call and return EPIPE
1782 while (state->count > 0) {
1783 if (state->vector[0].iov_len > 0) {
1784 break;
1786 state->vector += 1;
1787 state->count -= 1;
1790 if (state->count > 0) {
1791 /* we have more to read */
1792 return;
1795 tevent_req_done(req);
1798 static int tstream_bsd_readv_recv(struct tevent_req *req,
1799 int *perrno)
1801 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1802 struct tstream_bsd_readv_state);
1803 int ret;
1805 ret = tsocket_simple_int_recv(req, perrno);
1806 if (ret == 0) {
1807 ret = state->ret;
1810 tevent_req_received(req);
1811 return ret;
1814 struct tstream_bsd_writev_state {
1815 struct tstream_context *stream;
1817 struct iovec *vector;
1818 size_t count;
1820 int ret;
1823 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1825 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1826 struct tstream_bsd);
1828 tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1830 return 0;
1833 static void tstream_bsd_writev_handler(void *private_data);
1835 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1836 struct tevent_context *ev,
1837 struct tstream_context *stream,
1838 const struct iovec *vector,
1839 size_t count)
1841 struct tevent_req *req;
1842 struct tstream_bsd_writev_state *state;
1843 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1844 int ret;
1846 req = tevent_req_create(mem_ctx, &state,
1847 struct tstream_bsd_writev_state);
1848 if (!req) {
1849 return NULL;
1852 state->stream = stream;
1853 /* we make a copy of the vector so that we can modify it */
1854 state->vector = talloc_array(state, struct iovec, count);
1855 if (tevent_req_nomem(state->vector, req)) {
1856 goto post;
1858 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1859 state->count = count;
1860 state->ret = 0;
1862 talloc_set_destructor(state, tstream_bsd_writev_destructor);
1864 if (bsds->fd == -1) {
1865 tevent_req_error(req, ENOTCONN);
1866 goto post;
1870 * this is a fast path, not waiting for the
1871 * socket to become explicit writeable gains
1872 * about 10%-20% performance in benchmark tests.
1874 tstream_bsd_writev_handler(req);
1875 if (!tevent_req_is_in_progress(req)) {
1876 goto post;
1879 ret = tstream_bsd_set_writeable_handler(bsds, ev,
1880 tstream_bsd_writev_handler,
1881 req);
1882 if (ret == -1) {
1883 tevent_req_error(req, errno);
1884 goto post;
1887 return req;
1889 post:
1890 tevent_req_post(req, ev);
1891 return req;
1894 static void tstream_bsd_writev_handler(void *private_data)
1896 struct tevent_req *req = talloc_get_type_abort(private_data,
1897 struct tevent_req);
1898 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1899 struct tstream_bsd_writev_state);
1900 struct tstream_context *stream = state->stream;
1901 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1902 ssize_t ret;
1903 int err;
1904 bool retry;
1906 ret = writev(bsds->fd, state->vector, state->count);
1907 if (ret == 0) {
1908 /* propagate end of file */
1909 tevent_req_error(req, EPIPE);
1910 return;
1912 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1913 if (retry) {
1914 /* retry later */
1915 return;
1917 if (tevent_req_error(req, err)) {
1918 return;
1921 state->ret += ret;
1923 while (ret > 0) {
1924 if (ret < state->vector[0].iov_len) {
1925 uint8_t *base;
1926 base = (uint8_t *)state->vector[0].iov_base;
1927 base += ret;
1928 state->vector[0].iov_base = (void *)base;
1929 state->vector[0].iov_len -= ret;
1930 break;
1932 ret -= state->vector[0].iov_len;
1933 state->vector += 1;
1934 state->count -= 1;
1938 * there're maybe some empty vectors at the end
1939 * which we need to skip, otherwise we would get
1940 * ret == 0 from the writev() call and return EPIPE
1942 while (state->count > 0) {
1943 if (state->vector[0].iov_len > 0) {
1944 break;
1946 state->vector += 1;
1947 state->count -= 1;
1950 if (state->count > 0) {
1951 /* we have more to read */
1952 return;
1955 tevent_req_done(req);
1958 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1960 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1961 struct tstream_bsd_writev_state);
1962 int ret;
1964 ret = tsocket_simple_int_recv(req, perrno);
1965 if (ret == 0) {
1966 ret = state->ret;
1969 tevent_req_received(req);
1970 return ret;
1973 struct tstream_bsd_disconnect_state {
1974 void *__dummy;
1977 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1978 struct tevent_context *ev,
1979 struct tstream_context *stream)
1981 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1982 struct tevent_req *req;
1983 struct tstream_bsd_disconnect_state *state;
1984 int ret;
1985 int err;
1986 bool dummy;
1988 req = tevent_req_create(mem_ctx, &state,
1989 struct tstream_bsd_disconnect_state);
1990 if (req == NULL) {
1991 return NULL;
1994 if (bsds->fd == -1) {
1995 tevent_req_error(req, ENOTCONN);
1996 goto post;
1999 TALLOC_FREE(bsds->fde);
2000 ret = close(bsds->fd);
2001 bsds->fd = -1;
2002 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
2003 if (tevent_req_error(req, err)) {
2004 goto post;
2007 tevent_req_done(req);
2008 post:
2009 tevent_req_post(req, ev);
2010 return req;
2013 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
2014 int *perrno)
2016 int ret;
2018 ret = tsocket_simple_int_recv(req, perrno);
2020 tevent_req_received(req);
2021 return ret;
2024 static const struct tstream_context_ops tstream_bsd_ops = {
2025 .name = "bsd",
2027 .pending_bytes = tstream_bsd_pending_bytes,
2029 .readv_send = tstream_bsd_readv_send,
2030 .readv_recv = tstream_bsd_readv_recv,
2032 .writev_send = tstream_bsd_writev_send,
2033 .writev_recv = tstream_bsd_writev_recv,
2035 .disconnect_send = tstream_bsd_disconnect_send,
2036 .disconnect_recv = tstream_bsd_disconnect_recv,
2039 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
2041 TALLOC_FREE(bsds->fde);
2042 if (bsds->fd != -1) {
2043 close(bsds->fd);
2044 bsds->fd = -1;
2046 return 0;
2049 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
2050 int fd,
2051 struct tstream_context **_stream,
2052 const char *location)
2054 struct tstream_context *stream;
2055 struct tstream_bsd *bsds;
2057 stream = tstream_context_create(mem_ctx,
2058 &tstream_bsd_ops,
2059 &bsds,
2060 struct tstream_bsd,
2061 location);
2062 if (!stream) {
2063 return -1;
2065 ZERO_STRUCTP(bsds);
2066 bsds->fd = fd;
2067 talloc_set_destructor(bsds, tstream_bsd_destructor);
2069 *_stream = stream;
2070 return 0;
2073 struct tstream_bsd_connect_state {
2074 int fd;
2075 struct tevent_fd *fde;
2076 struct tstream_conext *stream;
2077 struct tsocket_address *local;
2080 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
2082 TALLOC_FREE(state->fde);
2083 if (state->fd != -1) {
2084 close(state->fd);
2085 state->fd = -1;
2088 return 0;
2091 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2092 struct tevent_fd *fde,
2093 uint16_t flags,
2094 void *private_data);
2096 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2097 struct tevent_context *ev,
2098 int sys_errno,
2099 const struct tsocket_address *local,
2100 const struct tsocket_address *remote)
2102 struct tevent_req *req;
2103 struct tstream_bsd_connect_state *state;
2104 struct tsocket_address_bsd *lbsda =
2105 talloc_get_type_abort(local->private_data,
2106 struct tsocket_address_bsd);
2107 struct tsocket_address_bsd *lrbsda = NULL;
2108 struct tsocket_address_bsd *rbsda =
2109 talloc_get_type_abort(remote->private_data,
2110 struct tsocket_address_bsd);
2111 int ret;
2112 int err;
2113 bool retry;
2114 bool do_bind = false;
2115 bool do_reuseaddr = false;
2116 bool do_ipv6only = false;
2117 bool is_inet = false;
2118 int sa_fam = lbsda->u.sa.sa_family;
2120 req = tevent_req_create(mem_ctx, &state,
2121 struct tstream_bsd_connect_state);
2122 if (!req) {
2123 return NULL;
2125 state->fd = -1;
2126 state->fde = NULL;
2128 talloc_set_destructor(state, tstream_bsd_connect_destructor);
2130 /* give the wrappers a chance to report an error */
2131 if (sys_errno != 0) {
2132 tevent_req_error(req, sys_errno);
2133 goto post;
2136 switch (lbsda->u.sa.sa_family) {
2137 case AF_UNIX:
2138 if (lbsda->u.un.sun_path[0] != 0) {
2139 do_reuseaddr = true;
2140 do_bind = true;
2142 break;
2143 case AF_INET:
2144 if (lbsda->u.in.sin_port != 0) {
2145 do_reuseaddr = true;
2146 do_bind = true;
2148 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2149 do_bind = true;
2151 is_inet = true;
2152 break;
2153 #ifdef HAVE_IPV6
2154 case AF_INET6:
2155 if (lbsda->u.in6.sin6_port != 0) {
2156 do_reuseaddr = true;
2157 do_bind = true;
2159 if (memcmp(&in6addr_any,
2160 &lbsda->u.in6.sin6_addr,
2161 sizeof(in6addr_any)) != 0) {
2162 do_bind = true;
2164 is_inet = true;
2165 do_ipv6only = true;
2166 break;
2167 #endif
2168 default:
2169 tevent_req_error(req, EINVAL);
2170 goto post;
2173 if (!do_bind && is_inet) {
2174 sa_fam = rbsda->u.sa.sa_family;
2175 switch (sa_fam) {
2176 case AF_INET:
2177 do_ipv6only = false;
2178 break;
2179 #ifdef HAVE_IPV6
2180 case AF_INET6:
2181 do_ipv6only = true;
2182 break;
2183 #endif
2187 if (is_inet) {
2188 state->local = tsocket_address_create(state,
2189 &tsocket_address_bsd_ops,
2190 &lrbsda,
2191 struct tsocket_address_bsd,
2192 __location__ "bsd_connect");
2193 if (tevent_req_nomem(state->local, req)) {
2194 goto post;
2197 ZERO_STRUCTP(lrbsda);
2198 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2199 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2200 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2201 #endif
2204 state->fd = socket(sa_fam, SOCK_STREAM, 0);
2205 if (state->fd == -1) {
2206 tevent_req_error(req, errno);
2207 goto post;
2210 state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2211 if (state->fd == -1) {
2212 tevent_req_error(req, errno);
2213 goto post;
2216 #ifdef HAVE_IPV6
2217 if (do_ipv6only) {
2218 int val = 1;
2220 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2221 (const void *)&val, sizeof(val));
2222 if (ret == -1) {
2223 tevent_req_error(req, errno);
2224 goto post;
2227 #endif
2229 if (do_reuseaddr) {
2230 int val = 1;
2232 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2233 (const void *)&val, sizeof(val));
2234 if (ret == -1) {
2235 tevent_req_error(req, errno);
2236 goto post;
2240 if (do_bind) {
2241 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2242 if (ret == -1) {
2243 tevent_req_error(req, errno);
2244 goto post;
2248 if (rbsda->u.sa.sa_family != sa_fam) {
2249 tevent_req_error(req, EINVAL);
2250 goto post;
2253 ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2254 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2255 if (retry) {
2256 /* retry later */
2257 goto async;
2259 if (tevent_req_error(req, err)) {
2260 goto post;
2263 if (!state->local) {
2264 tevent_req_done(req);
2265 goto post;
2268 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2269 if (ret == -1) {
2270 tevent_req_error(req, errno);
2271 goto post;
2274 tevent_req_done(req);
2275 goto post;
2277 async:
2278 state->fde = tevent_add_fd(ev, state,
2279 state->fd,
2280 TEVENT_FD_READ | TEVENT_FD_WRITE,
2281 tstream_bsd_connect_fde_handler,
2282 req);
2283 if (tevent_req_nomem(state->fde, req)) {
2284 goto post;
2287 return req;
2289 post:
2290 tevent_req_post(req, ev);
2291 return req;
2294 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2295 struct tevent_fd *fde,
2296 uint16_t flags,
2297 void *private_data)
2299 struct tevent_req *req = talloc_get_type_abort(private_data,
2300 struct tevent_req);
2301 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2302 struct tstream_bsd_connect_state);
2303 struct tsocket_address_bsd *lrbsda = NULL;
2304 int ret;
2305 int error=0;
2306 socklen_t len = sizeof(error);
2307 int err;
2308 bool retry;
2310 ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2311 if (ret == 0) {
2312 if (error != 0) {
2313 errno = error;
2314 ret = -1;
2317 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2318 if (retry) {
2319 /* retry later */
2320 return;
2322 if (tevent_req_error(req, err)) {
2323 return;
2326 if (!state->local) {
2327 tevent_req_done(req);
2328 return;
2331 lrbsda = talloc_get_type_abort(state->local->private_data,
2332 struct tsocket_address_bsd);
2334 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2335 if (ret == -1) {
2336 tevent_req_error(req, errno);
2337 return;
2340 tevent_req_done(req);
2343 static int tstream_bsd_connect_recv(struct tevent_req *req,
2344 int *perrno,
2345 TALLOC_CTX *mem_ctx,
2346 struct tstream_context **stream,
2347 struct tsocket_address **local,
2348 const char *location)
2350 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2351 struct tstream_bsd_connect_state);
2352 int ret;
2354 ret = tsocket_simple_int_recv(req, perrno);
2355 if (ret == 0) {
2356 ret = _tstream_bsd_existing_socket(mem_ctx,
2357 state->fd,
2358 stream,
2359 location);
2360 if (ret == -1) {
2361 *perrno = errno;
2362 goto done;
2364 TALLOC_FREE(state->fde);
2365 state->fd = -1;
2367 if (local) {
2368 *local = talloc_move(mem_ctx, &state->local);
2372 done:
2373 tevent_req_received(req);
2374 return ret;
2377 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2378 struct tevent_context *ev,
2379 const struct tsocket_address *local,
2380 const struct tsocket_address *remote)
2382 struct tsocket_address_bsd *lbsda =
2383 talloc_get_type_abort(local->private_data,
2384 struct tsocket_address_bsd);
2385 struct tevent_req *req;
2386 int sys_errno = 0;
2388 switch (lbsda->u.sa.sa_family) {
2389 case AF_INET:
2390 break;
2391 #ifdef HAVE_IPV6
2392 case AF_INET6:
2393 break;
2394 #endif
2395 default:
2396 sys_errno = EINVAL;
2397 break;
2400 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2402 return req;
2405 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2406 int *perrno,
2407 TALLOC_CTX *mem_ctx,
2408 struct tstream_context **stream,
2409 struct tsocket_address **local,
2410 const char *location)
2412 return tstream_bsd_connect_recv(req, perrno,
2413 mem_ctx, stream, local,
2414 location);
2417 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2418 struct tevent_context *ev,
2419 const struct tsocket_address *local,
2420 const struct tsocket_address *remote)
2422 struct tsocket_address_bsd *lbsda =
2423 talloc_get_type_abort(local->private_data,
2424 struct tsocket_address_bsd);
2425 struct tevent_req *req;
2426 int sys_errno = 0;
2428 switch (lbsda->u.sa.sa_family) {
2429 case AF_UNIX:
2430 break;
2431 default:
2432 sys_errno = EINVAL;
2433 break;
2436 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2438 return req;
2441 int _tstream_unix_connect_recv(struct tevent_req *req,
2442 int *perrno,
2443 TALLOC_CTX *mem_ctx,
2444 struct tstream_context **stream,
2445 const char *location)
2447 return tstream_bsd_connect_recv(req, perrno,
2448 mem_ctx, stream, NULL,
2449 location);
2452 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2453 struct tstream_context **_stream1,
2454 TALLOC_CTX *mem_ctx2,
2455 struct tstream_context **_stream2,
2456 const char *location)
2458 int ret;
2459 int fds[2];
2460 int fd1;
2461 int fd2;
2462 struct tstream_context *stream1 = NULL;
2463 struct tstream_context *stream2 = NULL;
2465 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2466 if (ret == -1) {
2467 return -1;
2469 fd1 = fds[0];
2470 fd2 = fds[1];
2472 fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2473 if (fd1 == -1) {
2474 int sys_errno = errno;
2475 close(fd2);
2476 errno = sys_errno;
2477 return -1;
2480 fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2481 if (fd2 == -1) {
2482 int sys_errno = errno;
2483 close(fd1);
2484 errno = sys_errno;
2485 return -1;
2488 ret = _tstream_bsd_existing_socket(mem_ctx1,
2489 fd1,
2490 &stream1,
2491 location);
2492 if (ret == -1) {
2493 int sys_errno = errno;
2494 close(fd1);
2495 close(fd2);
2496 errno = sys_errno;
2497 return -1;
2500 ret = _tstream_bsd_existing_socket(mem_ctx2,
2501 fd2,
2502 &stream2,
2503 location);
2504 if (ret == -1) {
2505 int sys_errno = errno;
2506 talloc_free(stream1);
2507 close(fd2);
2508 errno = sys_errno;
2509 return -1;
2512 *_stream1 = stream1;
2513 *_stream2 = stream2;
2514 return 0;