vfs: Fix CID 1312073 Argument cannot be negative
[Samba.git] / lib / tsocket / tsocket_bsd.c
blob8203755e5d13d664d326e5c6b799c0eb87b333ad
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"
29 #include "lib/util/iov_buf.h"
30 #include "lib/util/blocking.h"
32 static int tsocket_bsd_error_from_errno(int ret,
33 int sys_errno,
34 bool *retry)
36 *retry = false;
38 if (ret >= 0) {
39 return 0;
42 if (ret != -1) {
43 return EIO;
46 if (sys_errno == 0) {
47 return EIO;
50 if (sys_errno == EINTR) {
51 *retry = true;
52 return sys_errno;
55 if (sys_errno == EINPROGRESS) {
56 *retry = true;
57 return sys_errno;
60 if (sys_errno == EAGAIN) {
61 *retry = true;
62 return sys_errno;
65 /* ENOMEM is retryable on Solaris/illumos, and possibly other systems. */
66 if (sys_errno == ENOMEM) {
67 *retry = true;
68 return sys_errno;
71 #ifdef EWOULDBLOCK
72 if (sys_errno == EWOULDBLOCK) {
73 *retry = true;
74 return sys_errno;
76 #endif
78 return sys_errno;
81 static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
83 int i;
84 int sys_errno = 0;
85 int fds[3];
86 int num_fds = 0;
88 int result;
89 bool ok;
91 if (fd == -1) {
92 return -1;
95 /* first make a fd >= 3 */
96 if (high_fd) {
97 while (fd < 3) {
98 fds[num_fds++] = fd;
99 fd = dup(fd);
100 if (fd == -1) {
101 sys_errno = errno;
102 break;
105 for (i=0; i<num_fds; i++) {
106 close(fds[i]);
108 if (fd == -1) {
109 errno = sys_errno;
110 return fd;
114 result = set_blocking(fd, false);
115 if (result == -1) {
116 goto fail;
119 ok = smb_set_close_on_exec(fd);
120 if (!ok) {
121 goto fail;
124 return fd;
126 fail:
127 if (fd != -1) {
128 sys_errno = errno;
129 close(fd);
130 errno = sys_errno;
132 return -1;
135 static ssize_t tsocket_bsd_pending(int fd)
137 int ret, error;
138 int value = 0;
139 socklen_t len;
141 ret = ioctl(fd, FIONREAD, &value);
142 if (ret == -1) {
143 return ret;
146 if (ret != 0) {
147 /* this should not be reached */
148 errno = EIO;
149 return -1;
152 if (value != 0) {
153 return value;
156 error = 0;
157 len = sizeof(error);
160 * if no data is available check if the socket is in error state. For
161 * dgram sockets it's the way to return ICMP error messages of
162 * connected sockets to the caller.
164 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
165 if (ret == -1) {
166 return ret;
168 if (error != 0) {
169 errno = error;
170 return -1;
172 return 0;
175 static const struct tsocket_address_ops tsocket_address_bsd_ops;
177 struct tsocket_address_bsd {
178 socklen_t sa_socklen;
179 union {
180 struct sockaddr sa;
181 struct sockaddr_in in;
182 #ifdef HAVE_IPV6
183 struct sockaddr_in6 in6;
184 #endif
185 struct sockaddr_un un;
186 struct sockaddr_storage ss;
187 } u;
190 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
191 const struct sockaddr *sa,
192 size_t sa_socklen,
193 struct tsocket_address **_addr,
194 const char *location)
196 struct tsocket_address *addr;
197 struct tsocket_address_bsd *bsda;
199 if (sa_socklen < sizeof(sa->sa_family)) {
200 errno = EINVAL;
201 return -1;
204 switch (sa->sa_family) {
205 case AF_UNIX:
206 if (sa_socklen > sizeof(struct sockaddr_un)) {
207 sa_socklen = sizeof(struct sockaddr_un);
209 break;
210 case AF_INET:
211 if (sa_socklen < sizeof(struct sockaddr_in)) {
212 errno = EINVAL;
213 return -1;
215 sa_socklen = sizeof(struct sockaddr_in);
216 break;
217 #ifdef HAVE_IPV6
218 case AF_INET6:
219 if (sa_socklen < sizeof(struct sockaddr_in6)) {
220 errno = EINVAL;
221 return -1;
223 sa_socklen = sizeof(struct sockaddr_in6);
224 break;
225 #endif
226 default:
227 errno = EAFNOSUPPORT;
228 return -1;
231 if (sa_socklen > sizeof(struct sockaddr_storage)) {
232 errno = EINVAL;
233 return -1;
236 addr = tsocket_address_create(mem_ctx,
237 &tsocket_address_bsd_ops,
238 &bsda,
239 struct tsocket_address_bsd,
240 location);
241 if (!addr) {
242 errno = ENOMEM;
243 return -1;
246 ZERO_STRUCTP(bsda);
248 memcpy(&bsda->u.ss, sa, sa_socklen);
250 bsda->sa_socklen = sa_socklen;
251 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
252 bsda->u.sa.sa_len = bsda->sa_socklen;
253 #endif
255 *_addr = addr;
256 return 0;
259 ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
260 struct sockaddr *sa,
261 size_t sa_socklen)
263 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
264 struct tsocket_address_bsd);
266 if (!bsda) {
267 errno = EINVAL;
268 return -1;
271 if (sa_socklen < bsda->sa_socklen) {
272 errno = EINVAL;
273 return -1;
276 if (sa_socklen > bsda->sa_socklen) {
277 memset(sa, 0, sa_socklen);
278 sa_socklen = bsda->sa_socklen;
281 memcpy(sa, &bsda->u.ss, sa_socklen);
282 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
283 sa->sa_len = sa_socklen;
284 #endif
285 return sa_socklen;
288 bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam)
290 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
291 struct tsocket_address_bsd);
293 if (!bsda) {
294 return false;
297 switch (bsda->u.sa.sa_family) {
298 case AF_INET:
299 if (strcasecmp(fam, "ip") == 0) {
300 return true;
303 if (strcasecmp(fam, "ipv4") == 0) {
304 return true;
307 return false;
308 #ifdef HAVE_IPV6
309 case AF_INET6:
310 if (strcasecmp(fam, "ip") == 0) {
311 return true;
314 if (strcasecmp(fam, "ipv6") == 0) {
315 return true;
318 return false;
319 #endif
322 return false;
325 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
326 const char *fam,
327 const char *addr,
328 uint16_t port,
329 struct tsocket_address **_addr,
330 const char *location)
332 struct addrinfo hints;
333 struct addrinfo *result = NULL;
334 char port_str[6];
335 int ret;
337 ZERO_STRUCT(hints);
339 * we use SOCKET_STREAM here to get just one result
340 * back from getaddrinfo().
342 hints.ai_socktype = SOCK_STREAM;
343 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
345 if (strcasecmp(fam, "ip") == 0) {
346 hints.ai_family = AF_UNSPEC;
347 if (!addr) {
348 #ifdef HAVE_IPV6
349 addr = "::";
350 #else
351 addr = "0.0.0.0";
352 #endif
354 } else if (strcasecmp(fam, "ipv4") == 0) {
355 hints.ai_family = AF_INET;
356 if (!addr) {
357 addr = "0.0.0.0";
359 #ifdef HAVE_IPV6
360 } else if (strcasecmp(fam, "ipv6") == 0) {
361 hints.ai_family = AF_INET6;
362 if (!addr) {
363 addr = "::";
365 #endif
366 } else {
367 errno = EAFNOSUPPORT;
368 return -1;
371 snprintf(port_str, sizeof(port_str), "%u", port);
373 ret = getaddrinfo(addr, port_str, &hints, &result);
374 if (ret != 0) {
375 switch (ret) {
376 case EAI_FAIL:
377 errno = EINVAL;
378 break;
380 ret = -1;
381 goto done;
384 if (result->ai_socktype != SOCK_STREAM) {
385 errno = EINVAL;
386 ret = -1;
387 goto done;
390 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
391 result->ai_addr,
392 result->ai_addrlen,
393 _addr,
394 location);
396 done:
397 if (result) {
398 freeaddrinfo(result);
400 return ret;
403 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
404 TALLOC_CTX *mem_ctx)
406 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
407 struct tsocket_address_bsd);
408 char addr_str[INET6_ADDRSTRLEN+1];
409 const char *str;
411 if (!bsda) {
412 errno = EINVAL;
413 return NULL;
416 switch (bsda->u.sa.sa_family) {
417 case AF_INET:
418 str = inet_ntop(bsda->u.in.sin_family,
419 &bsda->u.in.sin_addr,
420 addr_str, sizeof(addr_str));
421 break;
422 #ifdef HAVE_IPV6
423 case AF_INET6:
424 str = inet_ntop(bsda->u.in6.sin6_family,
425 &bsda->u.in6.sin6_addr,
426 addr_str, sizeof(addr_str));
427 break;
428 #endif
429 default:
430 errno = EINVAL;
431 return NULL;
434 if (!str) {
435 return NULL;
438 return talloc_strdup(mem_ctx, str);
441 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
443 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
444 struct tsocket_address_bsd);
445 uint16_t port = 0;
447 if (!bsda) {
448 errno = EINVAL;
449 return 0;
452 switch (bsda->u.sa.sa_family) {
453 case AF_INET:
454 port = ntohs(bsda->u.in.sin_port);
455 break;
456 #ifdef HAVE_IPV6
457 case AF_INET6:
458 port = ntohs(bsda->u.in6.sin6_port);
459 break;
460 #endif
461 default:
462 errno = EINVAL;
463 return 0;
466 return port;
469 int tsocket_address_inet_set_port(struct tsocket_address *addr,
470 uint16_t port)
472 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
473 struct tsocket_address_bsd);
475 if (!bsda) {
476 errno = EINVAL;
477 return -1;
480 switch (bsda->u.sa.sa_family) {
481 case AF_INET:
482 bsda->u.in.sin_port = htons(port);
483 break;
484 #ifdef HAVE_IPV6
485 case AF_INET6:
486 bsda->u.in6.sin6_port = htons(port);
487 break;
488 #endif
489 default:
490 errno = EINVAL;
491 return -1;
494 return 0;
497 bool tsocket_address_is_unix(const struct tsocket_address *addr)
499 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
500 struct tsocket_address_bsd);
502 if (!bsda) {
503 return false;
506 switch (bsda->u.sa.sa_family) {
507 case AF_UNIX:
508 return true;
511 return false;
514 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
515 const char *path,
516 struct tsocket_address **_addr,
517 const char *location)
519 struct sockaddr_un un;
520 void *p = &un;
521 int ret;
523 if (!path) {
524 path = "";
527 if (strlen(path) > sizeof(un.sun_path)-1) {
528 errno = ENAMETOOLONG;
529 return -1;
532 ZERO_STRUCT(un);
533 un.sun_family = AF_UNIX;
534 strncpy(un.sun_path, path, sizeof(un.sun_path)-1);
536 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
537 (struct sockaddr *)p,
538 sizeof(un),
539 _addr,
540 location);
542 return ret;
545 char *tsocket_address_unix_path(const struct tsocket_address *addr,
546 TALLOC_CTX *mem_ctx)
548 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
549 struct tsocket_address_bsd);
550 const char *str;
552 if (!bsda) {
553 errno = EINVAL;
554 return NULL;
557 switch (bsda->u.sa.sa_family) {
558 case AF_UNIX:
559 str = bsda->u.un.sun_path;
560 break;
561 default:
562 errno = EINVAL;
563 return NULL;
566 return talloc_strdup(mem_ctx, str);
569 static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
570 TALLOC_CTX *mem_ctx)
572 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
573 struct tsocket_address_bsd);
574 char *str;
575 char *addr_str;
576 const char *prefix = NULL;
577 uint16_t port;
579 switch (bsda->u.sa.sa_family) {
580 case AF_UNIX:
581 return talloc_asprintf(mem_ctx, "unix:%s",
582 bsda->u.un.sun_path);
583 case AF_INET:
584 prefix = "ipv4";
585 break;
586 #ifdef HAVE_IPV6
587 case AF_INET6:
588 prefix = "ipv6";
589 break;
590 #endif
591 default:
592 errno = EINVAL;
593 return NULL;
596 addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
597 if (!addr_str) {
598 return NULL;
601 port = tsocket_address_inet_port(addr);
603 str = talloc_asprintf(mem_ctx, "%s:%s:%u",
604 prefix, addr_str, port);
605 talloc_free(addr_str);
607 return str;
610 static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
611 TALLOC_CTX *mem_ctx,
612 const char *location)
614 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
615 struct tsocket_address_bsd);
616 struct tsocket_address *copy;
617 int ret;
619 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
620 &bsda->u.sa,
621 bsda->sa_socklen,
622 &copy,
623 location);
624 if (ret != 0) {
625 return NULL;
628 return copy;
631 static const struct tsocket_address_ops tsocket_address_bsd_ops = {
632 .name = "bsd",
633 .string = tsocket_address_bsd_string,
634 .copy = tsocket_address_bsd_copy,
637 struct tdgram_bsd {
638 int fd;
640 void *event_ptr;
641 struct tevent_fd *fde;
642 bool optimize_recvfrom;
644 void *readable_private;
645 void (*readable_handler)(void *private_data);
646 void *writeable_private;
647 void (*writeable_handler)(void *private_data);
650 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context *dgram,
651 bool on)
653 struct tdgram_bsd *bsds =
654 talloc_get_type(_tdgram_context_data(dgram),
655 struct tdgram_bsd);
656 bool old;
658 if (bsds == NULL) {
659 /* not a bsd socket */
660 return false;
663 old = bsds->optimize_recvfrom;
664 bsds->optimize_recvfrom = on;
666 return old;
669 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
670 struct tevent_fd *fde,
671 uint16_t flags,
672 void *private_data)
674 struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
675 struct tdgram_bsd);
677 if (flags & TEVENT_FD_WRITE) {
678 bsds->writeable_handler(bsds->writeable_private);
679 return;
681 if (flags & TEVENT_FD_READ) {
682 if (!bsds->readable_handler) {
683 TEVENT_FD_NOT_READABLE(bsds->fde);
684 return;
686 bsds->readable_handler(bsds->readable_private);
687 return;
691 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
692 struct tevent_context *ev,
693 void (*handler)(void *private_data),
694 void *private_data)
696 if (ev == NULL) {
697 if (handler) {
698 errno = EINVAL;
699 return -1;
701 if (!bsds->readable_handler) {
702 return 0;
704 bsds->readable_handler = NULL;
705 bsds->readable_private = NULL;
707 return 0;
710 /* read and write must use the same tevent_context */
711 if (bsds->event_ptr != ev) {
712 if (bsds->readable_handler || bsds->writeable_handler) {
713 errno = EINVAL;
714 return -1;
716 bsds->event_ptr = NULL;
717 TALLOC_FREE(bsds->fde);
720 if (tevent_fd_get_flags(bsds->fde) == 0) {
721 TALLOC_FREE(bsds->fde);
723 bsds->fde = tevent_add_fd(ev, bsds,
724 bsds->fd, TEVENT_FD_READ,
725 tdgram_bsd_fde_handler,
726 bsds);
727 if (!bsds->fde) {
728 errno = ENOMEM;
729 return -1;
732 /* cache the event context we're running on */
733 bsds->event_ptr = ev;
734 } else if (!bsds->readable_handler) {
735 TEVENT_FD_READABLE(bsds->fde);
738 bsds->readable_handler = handler;
739 bsds->readable_private = private_data;
741 return 0;
744 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
745 struct tevent_context *ev,
746 void (*handler)(void *private_data),
747 void *private_data)
749 if (ev == NULL) {
750 if (handler) {
751 errno = EINVAL;
752 return -1;
754 if (!bsds->writeable_handler) {
755 return 0;
757 bsds->writeable_handler = NULL;
758 bsds->writeable_private = NULL;
759 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
761 return 0;
764 /* read and write must use the same tevent_context */
765 if (bsds->event_ptr != ev) {
766 if (bsds->readable_handler || bsds->writeable_handler) {
767 errno = EINVAL;
768 return -1;
770 bsds->event_ptr = NULL;
771 TALLOC_FREE(bsds->fde);
774 if (tevent_fd_get_flags(bsds->fde) == 0) {
775 TALLOC_FREE(bsds->fde);
777 bsds->fde = tevent_add_fd(ev, bsds,
778 bsds->fd, TEVENT_FD_WRITE,
779 tdgram_bsd_fde_handler,
780 bsds);
781 if (!bsds->fde) {
782 errno = ENOMEM;
783 return -1;
786 /* cache the event context we're running on */
787 bsds->event_ptr = ev;
788 } else if (!bsds->writeable_handler) {
789 TEVENT_FD_WRITEABLE(bsds->fde);
792 bsds->writeable_handler = handler;
793 bsds->writeable_private = private_data;
795 return 0;
798 struct tdgram_bsd_recvfrom_state {
799 struct tdgram_context *dgram;
800 bool first_try;
801 uint8_t *buf;
802 size_t len;
803 struct tsocket_address *src;
806 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
808 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
809 struct tdgram_bsd);
811 tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
813 return 0;
816 static void tdgram_bsd_recvfrom_handler(void *private_data);
818 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
819 struct tevent_context *ev,
820 struct tdgram_context *dgram)
822 struct tevent_req *req;
823 struct tdgram_bsd_recvfrom_state *state;
824 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
825 int ret;
827 req = tevent_req_create(mem_ctx, &state,
828 struct tdgram_bsd_recvfrom_state);
829 if (!req) {
830 return NULL;
833 state->dgram = dgram;
834 state->first_try= true;
835 state->buf = NULL;
836 state->len = 0;
837 state->src = NULL;
839 talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
841 if (bsds->fd == -1) {
842 tevent_req_error(req, ENOTCONN);
843 goto post;
848 * this is a fast path, not waiting for the
849 * socket to become explicit readable gains
850 * about 10%-20% performance in benchmark tests.
852 if (bsds->optimize_recvfrom) {
854 * We only do the optimization on
855 * recvfrom if the caller asked for it.
857 * This is needed because in most cases
858 * we prefer to flush send buffers before
859 * receiving incoming requests.
861 tdgram_bsd_recvfrom_handler(req);
862 if (!tevent_req_is_in_progress(req)) {
863 goto post;
867 ret = tdgram_bsd_set_readable_handler(bsds, ev,
868 tdgram_bsd_recvfrom_handler,
869 req);
870 if (ret == -1) {
871 tevent_req_error(req, errno);
872 goto post;
875 return req;
877 post:
878 tevent_req_post(req, ev);
879 return req;
882 static void tdgram_bsd_recvfrom_handler(void *private_data)
884 struct tevent_req *req = talloc_get_type_abort(private_data,
885 struct tevent_req);
886 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
887 struct tdgram_bsd_recvfrom_state);
888 struct tdgram_context *dgram = state->dgram;
889 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
890 struct tsocket_address_bsd *bsda;
891 ssize_t ret;
892 int err;
893 bool retry;
895 ret = tsocket_bsd_pending(bsds->fd);
896 if (state->first_try && ret == 0) {
897 state->first_try = false;
898 /* retry later */
899 return;
901 state->first_try = false;
903 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
904 if (retry) {
905 /* retry later */
906 return;
908 if (tevent_req_error(req, err)) {
909 return;
912 /* note that 'ret' can be 0 here */
913 state->buf = talloc_array(state, uint8_t, ret);
914 if (tevent_req_nomem(state->buf, req)) {
915 return;
917 state->len = ret;
919 state->src = tsocket_address_create(state,
920 &tsocket_address_bsd_ops,
921 &bsda,
922 struct tsocket_address_bsd,
923 __location__ "bsd_recvfrom");
924 if (tevent_req_nomem(state->src, req)) {
925 return;
928 ZERO_STRUCTP(bsda);
929 bsda->sa_socklen = sizeof(bsda->u.ss);
930 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
931 bsda->u.sa.sa_len = bsda->sa_socklen;
932 #endif
934 ret = recvfrom(bsds->fd, state->buf, state->len, 0,
935 &bsda->u.sa, &bsda->sa_socklen);
936 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
937 if (retry) {
938 /* retry later */
939 return;
941 if (tevent_req_error(req, err)) {
942 return;
946 * Some systems (FreeBSD, see bug #7115) return too much
947 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
948 * the return value includes some IP/UDP header bytes,
949 * while recvfrom() just returns the payload.
951 state->buf = talloc_realloc(state, state->buf, uint8_t, ret);
952 if (tevent_req_nomem(state->buf, req)) {
953 return;
955 state->len = ret;
957 tevent_req_done(req);
960 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
961 int *perrno,
962 TALLOC_CTX *mem_ctx,
963 uint8_t **buf,
964 struct tsocket_address **src)
966 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
967 struct tdgram_bsd_recvfrom_state);
968 ssize_t ret;
970 ret = tsocket_simple_int_recv(req, perrno);
971 if (ret == 0) {
972 *buf = talloc_move(mem_ctx, &state->buf);
973 ret = state->len;
974 if (src) {
975 *src = talloc_move(mem_ctx, &state->src);
979 tevent_req_received(req);
980 return ret;
983 struct tdgram_bsd_sendto_state {
984 struct tdgram_context *dgram;
986 const uint8_t *buf;
987 size_t len;
988 const struct tsocket_address *dst;
990 ssize_t ret;
993 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
995 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
996 struct tdgram_bsd);
998 tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1000 return 0;
1003 static void tdgram_bsd_sendto_handler(void *private_data);
1005 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
1006 struct tevent_context *ev,
1007 struct tdgram_context *dgram,
1008 const uint8_t *buf,
1009 size_t len,
1010 const struct tsocket_address *dst)
1012 struct tevent_req *req;
1013 struct tdgram_bsd_sendto_state *state;
1014 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1015 int ret;
1017 req = tevent_req_create(mem_ctx, &state,
1018 struct tdgram_bsd_sendto_state);
1019 if (!req) {
1020 return NULL;
1023 state->dgram = dgram;
1024 state->buf = buf;
1025 state->len = len;
1026 state->dst = dst;
1027 state->ret = -1;
1029 talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
1031 if (bsds->fd == -1) {
1032 tevent_req_error(req, ENOTCONN);
1033 goto post;
1037 * this is a fast path, not waiting for the
1038 * socket to become explicit writeable gains
1039 * about 10%-20% performance in benchmark tests.
1041 tdgram_bsd_sendto_handler(req);
1042 if (!tevent_req_is_in_progress(req)) {
1043 goto post;
1046 ret = tdgram_bsd_set_writeable_handler(bsds, ev,
1047 tdgram_bsd_sendto_handler,
1048 req);
1049 if (ret == -1) {
1050 tevent_req_error(req, errno);
1051 goto post;
1054 return req;
1056 post:
1057 tevent_req_post(req, ev);
1058 return req;
1061 static void tdgram_bsd_sendto_handler(void *private_data)
1063 struct tevent_req *req = talloc_get_type_abort(private_data,
1064 struct tevent_req);
1065 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1066 struct tdgram_bsd_sendto_state);
1067 struct tdgram_context *dgram = state->dgram;
1068 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1069 struct sockaddr *sa = NULL;
1070 socklen_t sa_socklen = 0;
1071 ssize_t ret;
1072 int err;
1073 bool retry;
1075 if (state->dst) {
1076 struct tsocket_address_bsd *bsda =
1077 talloc_get_type(state->dst->private_data,
1078 struct tsocket_address_bsd);
1080 sa = &bsda->u.sa;
1081 sa_socklen = bsda->sa_socklen;
1084 ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_socklen);
1085 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1086 if (retry) {
1087 /* retry later */
1088 return;
1091 if (err == EMSGSIZE) {
1092 /* round up in 1K increments */
1093 int bufsize = ((state->len + 1023) & (~1023));
1095 ret = setsockopt(bsds->fd, SOL_SOCKET, SO_SNDBUF, &bufsize,
1096 sizeof(bufsize));
1097 if (ret == 0) {
1099 * We do the retry here, rather then via the
1100 * handler, as we only want to retry once for
1101 * this condition, so if there is a mismatch
1102 * between what setsockopt() accepts and what can
1103 * actually be sent, we do not end up in a
1104 * loop.
1107 ret = sendto(bsds->fd, state->buf, state->len,
1108 0, sa, sa_socklen);
1109 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1110 if (retry) { /* retry later */
1111 return;
1116 if (tevent_req_error(req, err)) {
1117 return;
1120 state->ret = ret;
1122 tevent_req_done(req);
1125 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1127 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1128 struct tdgram_bsd_sendto_state);
1129 ssize_t ret;
1131 ret = tsocket_simple_int_recv(req, perrno);
1132 if (ret == 0) {
1133 ret = state->ret;
1136 tevent_req_received(req);
1137 return ret;
1140 struct tdgram_bsd_disconnect_state {
1141 uint8_t __dummy;
1144 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1145 struct tevent_context *ev,
1146 struct tdgram_context *dgram)
1148 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1149 struct tevent_req *req;
1150 struct tdgram_bsd_disconnect_state *state;
1151 int ret;
1152 int err;
1153 bool dummy;
1155 req = tevent_req_create(mem_ctx, &state,
1156 struct tdgram_bsd_disconnect_state);
1157 if (req == NULL) {
1158 return NULL;
1161 if (bsds->fd == -1) {
1162 tevent_req_error(req, ENOTCONN);
1163 goto post;
1166 TALLOC_FREE(bsds->fde);
1167 ret = close(bsds->fd);
1168 bsds->fd = -1;
1169 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1170 if (tevent_req_error(req, err)) {
1171 goto post;
1174 tevent_req_done(req);
1175 post:
1176 tevent_req_post(req, ev);
1177 return req;
1180 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1181 int *perrno)
1183 int ret;
1185 ret = tsocket_simple_int_recv(req, perrno);
1187 tevent_req_received(req);
1188 return ret;
1191 static const struct tdgram_context_ops tdgram_bsd_ops = {
1192 .name = "bsd",
1194 .recvfrom_send = tdgram_bsd_recvfrom_send,
1195 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1197 .sendto_send = tdgram_bsd_sendto_send,
1198 .sendto_recv = tdgram_bsd_sendto_recv,
1200 .disconnect_send = tdgram_bsd_disconnect_send,
1201 .disconnect_recv = tdgram_bsd_disconnect_recv,
1204 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1206 TALLOC_FREE(bsds->fde);
1207 if (bsds->fd != -1) {
1208 close(bsds->fd);
1209 bsds->fd = -1;
1211 return 0;
1214 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1215 const struct tsocket_address *remote,
1216 bool broadcast,
1217 TALLOC_CTX *mem_ctx,
1218 struct tdgram_context **_dgram,
1219 const char *location)
1221 struct tsocket_address_bsd *lbsda =
1222 talloc_get_type_abort(local->private_data,
1223 struct tsocket_address_bsd);
1224 struct tsocket_address_bsd *rbsda = NULL;
1225 struct tdgram_context *dgram;
1226 struct tdgram_bsd *bsds;
1227 int fd;
1228 int ret;
1229 bool do_bind = false;
1230 bool do_reuseaddr = false;
1231 bool do_ipv6only = false;
1232 bool is_inet = false;
1233 int sa_fam = lbsda->u.sa.sa_family;
1235 if (remote) {
1236 rbsda = talloc_get_type_abort(remote->private_data,
1237 struct tsocket_address_bsd);
1240 switch (lbsda->u.sa.sa_family) {
1241 case AF_UNIX:
1242 if (broadcast) {
1243 errno = EINVAL;
1244 return -1;
1246 if (lbsda->u.un.sun_path[0] != 0) {
1247 do_reuseaddr = true;
1248 do_bind = true;
1250 break;
1251 case AF_INET:
1252 if (lbsda->u.in.sin_port != 0) {
1253 do_reuseaddr = true;
1254 do_bind = true;
1256 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1257 do_bind = true;
1259 is_inet = true;
1260 break;
1261 #ifdef HAVE_IPV6
1262 case AF_INET6:
1263 if (lbsda->u.in6.sin6_port != 0) {
1264 do_reuseaddr = true;
1265 do_bind = true;
1267 if (memcmp(&in6addr_any,
1268 &lbsda->u.in6.sin6_addr,
1269 sizeof(in6addr_any)) != 0) {
1270 do_bind = true;
1272 is_inet = true;
1273 do_ipv6only = true;
1274 break;
1275 #endif
1276 default:
1277 errno = EINVAL;
1278 return -1;
1281 if (!do_bind && is_inet && rbsda) {
1282 sa_fam = rbsda->u.sa.sa_family;
1283 switch (sa_fam) {
1284 case AF_INET:
1285 do_ipv6only = false;
1286 break;
1287 #ifdef HAVE_IPV6
1288 case AF_INET6:
1289 do_ipv6only = true;
1290 break;
1291 #endif
1295 fd = socket(sa_fam, SOCK_DGRAM, 0);
1296 if (fd < 0) {
1297 return -1;
1300 fd = tsocket_bsd_common_prepare_fd(fd, true);
1301 if (fd < 0) {
1302 return -1;
1305 dgram = tdgram_context_create(mem_ctx,
1306 &tdgram_bsd_ops,
1307 &bsds,
1308 struct tdgram_bsd,
1309 location);
1310 if (!dgram) {
1311 int saved_errno = errno;
1312 close(fd);
1313 errno = saved_errno;
1314 return -1;
1316 ZERO_STRUCTP(bsds);
1317 bsds->fd = fd;
1318 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1320 #ifdef HAVE_IPV6
1321 if (do_ipv6only) {
1322 int val = 1;
1324 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1325 (const void *)&val, sizeof(val));
1326 if (ret == -1) {
1327 int saved_errno = errno;
1328 talloc_free(dgram);
1329 errno = saved_errno;
1330 return -1;
1333 #endif
1335 if (broadcast) {
1336 int val = 1;
1338 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1339 (const void *)&val, sizeof(val));
1340 if (ret == -1) {
1341 int saved_errno = errno;
1342 talloc_free(dgram);
1343 errno = saved_errno;
1344 return -1;
1348 if (do_reuseaddr) {
1349 int val = 1;
1351 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1352 (const void *)&val, sizeof(val));
1353 if (ret == -1) {
1354 int saved_errno = errno;
1355 talloc_free(dgram);
1356 errno = saved_errno;
1357 return -1;
1361 if (do_bind) {
1362 ret = bind(fd, &lbsda->u.sa, lbsda->sa_socklen);
1363 if (ret == -1) {
1364 int saved_errno = errno;
1365 talloc_free(dgram);
1366 errno = saved_errno;
1367 return -1;
1371 if (rbsda) {
1372 if (rbsda->u.sa.sa_family != sa_fam) {
1373 talloc_free(dgram);
1374 errno = EINVAL;
1375 return -1;
1378 ret = connect(fd, &rbsda->u.sa, rbsda->sa_socklen);
1379 if (ret == -1) {
1380 int saved_errno = errno;
1381 talloc_free(dgram);
1382 errno = saved_errno;
1383 return -1;
1387 *_dgram = dgram;
1388 return 0;
1391 int _tdgram_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1392 int fd,
1393 struct tdgram_context **_dgram,
1394 const char *location)
1396 struct tdgram_context *dgram;
1397 struct tdgram_bsd *bsds;
1399 dgram = tdgram_context_create(mem_ctx,
1400 &tdgram_bsd_ops,
1401 &bsds,
1402 struct tdgram_bsd,
1403 location);
1404 if (!dgram) {
1405 return -1;
1407 ZERO_STRUCTP(bsds);
1408 bsds->fd = fd;
1409 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1411 *_dgram = dgram;
1412 return 0;
1415 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1416 const struct tsocket_address *remote,
1417 TALLOC_CTX *mem_ctx,
1418 struct tdgram_context **dgram,
1419 const char *location)
1421 struct tsocket_address_bsd *lbsda =
1422 talloc_get_type_abort(local->private_data,
1423 struct tsocket_address_bsd);
1424 int ret;
1426 switch (lbsda->u.sa.sa_family) {
1427 case AF_INET:
1428 break;
1429 #ifdef HAVE_IPV6
1430 case AF_INET6:
1431 break;
1432 #endif
1433 default:
1434 errno = EINVAL;
1435 return -1;
1438 ret = tdgram_bsd_dgram_socket(local, remote, false,
1439 mem_ctx, dgram, location);
1441 return ret;
1444 int _tdgram_inet_udp_broadcast_socket(const struct tsocket_address *local,
1445 TALLOC_CTX *mem_ctx,
1446 struct tdgram_context **dgram,
1447 const char *location)
1449 struct tsocket_address_bsd *lbsda =
1450 talloc_get_type_abort(local->private_data,
1451 struct tsocket_address_bsd);
1452 int ret;
1454 switch (lbsda->u.sa.sa_family) {
1455 case AF_INET:
1456 break;
1457 #ifdef HAVE_IPV6
1458 case AF_INET6:
1459 /* only ipv4 */
1460 errno = EINVAL;
1461 return -1;
1462 #endif
1463 default:
1464 errno = EINVAL;
1465 return -1;
1468 ret = tdgram_bsd_dgram_socket(local, NULL, true,
1469 mem_ctx, dgram, location);
1471 return ret;
1474 int _tdgram_unix_socket(const struct tsocket_address *local,
1475 const struct tsocket_address *remote,
1476 TALLOC_CTX *mem_ctx,
1477 struct tdgram_context **dgram,
1478 const char *location)
1480 struct tsocket_address_bsd *lbsda =
1481 talloc_get_type_abort(local->private_data,
1482 struct tsocket_address_bsd);
1483 int ret;
1485 switch (lbsda->u.sa.sa_family) {
1486 case AF_UNIX:
1487 break;
1488 default:
1489 errno = EINVAL;
1490 return -1;
1493 ret = tdgram_bsd_dgram_socket(local, remote, false,
1494 mem_ctx, dgram, location);
1496 return ret;
1499 struct tstream_bsd {
1500 int fd;
1502 void *event_ptr;
1503 struct tevent_fd *fde;
1504 bool optimize_readv;
1506 void *readable_private;
1507 void (*readable_handler)(void *private_data);
1508 void *writeable_private;
1509 void (*writeable_handler)(void *private_data);
1512 bool tstream_bsd_optimize_readv(struct tstream_context *stream,
1513 bool on)
1515 struct tstream_bsd *bsds =
1516 talloc_get_type(_tstream_context_data(stream),
1517 struct tstream_bsd);
1518 bool old;
1520 if (bsds == NULL) {
1521 /* not a bsd socket */
1522 return false;
1525 old = bsds->optimize_readv;
1526 bsds->optimize_readv = on;
1528 return old;
1531 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1532 struct tevent_fd *fde,
1533 uint16_t flags,
1534 void *private_data)
1536 struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1537 struct tstream_bsd);
1539 if (flags & TEVENT_FD_WRITE) {
1540 bsds->writeable_handler(bsds->writeable_private);
1541 return;
1543 if (flags & TEVENT_FD_READ) {
1544 if (!bsds->readable_handler) {
1545 if (bsds->writeable_handler) {
1546 bsds->writeable_handler(bsds->writeable_private);
1547 return;
1549 TEVENT_FD_NOT_READABLE(bsds->fde);
1550 return;
1552 bsds->readable_handler(bsds->readable_private);
1553 return;
1557 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1558 struct tevent_context *ev,
1559 void (*handler)(void *private_data),
1560 void *private_data)
1562 if (ev == NULL) {
1563 if (handler) {
1564 errno = EINVAL;
1565 return -1;
1567 if (!bsds->readable_handler) {
1568 return 0;
1570 bsds->readable_handler = NULL;
1571 bsds->readable_private = NULL;
1573 return 0;
1576 /* read and write must use the same tevent_context */
1577 if (bsds->event_ptr != ev) {
1578 if (bsds->readable_handler || bsds->writeable_handler) {
1579 errno = EINVAL;
1580 return -1;
1582 bsds->event_ptr = NULL;
1583 TALLOC_FREE(bsds->fde);
1586 if (tevent_fd_get_flags(bsds->fde) == 0) {
1587 TALLOC_FREE(bsds->fde);
1589 bsds->fde = tevent_add_fd(ev, bsds,
1590 bsds->fd, TEVENT_FD_READ,
1591 tstream_bsd_fde_handler,
1592 bsds);
1593 if (!bsds->fde) {
1594 errno = ENOMEM;
1595 return -1;
1598 /* cache the event context we're running on */
1599 bsds->event_ptr = ev;
1600 } else if (!bsds->readable_handler) {
1601 TEVENT_FD_READABLE(bsds->fde);
1604 bsds->readable_handler = handler;
1605 bsds->readable_private = private_data;
1607 return 0;
1610 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1611 struct tevent_context *ev,
1612 void (*handler)(void *private_data),
1613 void *private_data)
1615 if (ev == NULL) {
1616 if (handler) {
1617 errno = EINVAL;
1618 return -1;
1620 if (!bsds->writeable_handler) {
1621 return 0;
1623 bsds->writeable_handler = NULL;
1624 bsds->writeable_private = NULL;
1625 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1627 return 0;
1630 /* read and write must use the same tevent_context */
1631 if (bsds->event_ptr != ev) {
1632 if (bsds->readable_handler || bsds->writeable_handler) {
1633 errno = EINVAL;
1634 return -1;
1636 bsds->event_ptr = NULL;
1637 TALLOC_FREE(bsds->fde);
1640 if (tevent_fd_get_flags(bsds->fde) == 0) {
1641 TALLOC_FREE(bsds->fde);
1643 bsds->fde = tevent_add_fd(ev, bsds,
1644 bsds->fd,
1645 TEVENT_FD_READ | TEVENT_FD_WRITE,
1646 tstream_bsd_fde_handler,
1647 bsds);
1648 if (!bsds->fde) {
1649 errno = ENOMEM;
1650 return -1;
1653 /* cache the event context we're running on */
1654 bsds->event_ptr = ev;
1655 } else if (!bsds->writeable_handler) {
1656 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1657 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1658 tevent_fd_set_flags(bsds->fde, flags);
1661 bsds->writeable_handler = handler;
1662 bsds->writeable_private = private_data;
1664 return 0;
1667 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1669 struct tstream_bsd *bsds = tstream_context_data(stream,
1670 struct tstream_bsd);
1671 ssize_t ret;
1673 if (bsds->fd == -1) {
1674 errno = ENOTCONN;
1675 return -1;
1678 ret = tsocket_bsd_pending(bsds->fd);
1680 return ret;
1683 struct tstream_bsd_readv_state {
1684 struct tstream_context *stream;
1686 struct iovec *vector;
1687 size_t count;
1689 int ret;
1692 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1694 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1695 struct tstream_bsd);
1697 tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1699 return 0;
1702 static void tstream_bsd_readv_handler(void *private_data);
1704 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1705 struct tevent_context *ev,
1706 struct tstream_context *stream,
1707 struct iovec *vector,
1708 size_t count)
1710 struct tevent_req *req;
1711 struct tstream_bsd_readv_state *state;
1712 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1713 int ret;
1715 req = tevent_req_create(mem_ctx, &state,
1716 struct tstream_bsd_readv_state);
1717 if (!req) {
1718 return NULL;
1721 state->stream = stream;
1722 /* we make a copy of the vector so that we can modify it */
1723 state->vector = talloc_array(state, struct iovec, count);
1724 if (tevent_req_nomem(state->vector, req)) {
1725 goto post;
1727 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1728 state->count = count;
1729 state->ret = 0;
1731 talloc_set_destructor(state, tstream_bsd_readv_destructor);
1733 if (bsds->fd == -1) {
1734 tevent_req_error(req, ENOTCONN);
1735 goto post;
1739 * this is a fast path, not waiting for the
1740 * socket to become explicit readable gains
1741 * about 10%-20% performance in benchmark tests.
1743 if (bsds->optimize_readv) {
1745 * We only do the optimization on
1746 * readv if the caller asked for it.
1748 * This is needed because in most cases
1749 * we prefer to flush send buffers before
1750 * receiving incoming requests.
1752 tstream_bsd_readv_handler(req);
1753 if (!tevent_req_is_in_progress(req)) {
1754 goto post;
1758 ret = tstream_bsd_set_readable_handler(bsds, ev,
1759 tstream_bsd_readv_handler,
1760 req);
1761 if (ret == -1) {
1762 tevent_req_error(req, errno);
1763 goto post;
1766 return req;
1768 post:
1769 tevent_req_post(req, ev);
1770 return req;
1773 static void tstream_bsd_readv_handler(void *private_data)
1775 struct tevent_req *req = talloc_get_type_abort(private_data,
1776 struct tevent_req);
1777 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1778 struct tstream_bsd_readv_state);
1779 struct tstream_context *stream = state->stream;
1780 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1781 int ret;
1782 int err;
1783 int _count;
1784 bool ok, retry;
1786 ret = readv(bsds->fd, state->vector, state->count);
1787 if (ret == 0) {
1788 /* propagate end of file */
1789 tevent_req_error(req, EPIPE);
1790 return;
1792 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1793 if (retry) {
1794 /* retry later */
1795 return;
1797 if (tevent_req_error(req, err)) {
1798 return;
1801 state->ret += ret;
1803 _count = state->count; /* tstream has size_t count, readv has int */
1804 ok = iov_advance(&state->vector, &_count, ret);
1805 state->count = _count;
1807 if (!ok) {
1808 tevent_req_error(req, EINVAL);
1809 return;
1812 if (state->count > 0) {
1813 /* we have more to read */
1814 return;
1817 tevent_req_done(req);
1820 static int tstream_bsd_readv_recv(struct tevent_req *req,
1821 int *perrno)
1823 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1824 struct tstream_bsd_readv_state);
1825 int ret;
1827 ret = tsocket_simple_int_recv(req, perrno);
1828 if (ret == 0) {
1829 ret = state->ret;
1832 tevent_req_received(req);
1833 return ret;
1836 struct tstream_bsd_writev_state {
1837 struct tstream_context *stream;
1839 struct iovec *vector;
1840 size_t count;
1842 int ret;
1845 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1847 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1848 struct tstream_bsd);
1850 tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1852 return 0;
1855 static void tstream_bsd_writev_handler(void *private_data);
1857 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1858 struct tevent_context *ev,
1859 struct tstream_context *stream,
1860 const struct iovec *vector,
1861 size_t count)
1863 struct tevent_req *req;
1864 struct tstream_bsd_writev_state *state;
1865 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1866 int ret;
1868 req = tevent_req_create(mem_ctx, &state,
1869 struct tstream_bsd_writev_state);
1870 if (!req) {
1871 return NULL;
1874 state->stream = stream;
1875 /* we make a copy of the vector so that we can modify it */
1876 state->vector = talloc_array(state, struct iovec, count);
1877 if (tevent_req_nomem(state->vector, req)) {
1878 goto post;
1880 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1881 state->count = count;
1882 state->ret = 0;
1884 talloc_set_destructor(state, tstream_bsd_writev_destructor);
1886 if (bsds->fd == -1) {
1887 tevent_req_error(req, ENOTCONN);
1888 goto post;
1892 * this is a fast path, not waiting for the
1893 * socket to become explicit writeable gains
1894 * about 10%-20% performance in benchmark tests.
1896 tstream_bsd_writev_handler(req);
1897 if (!tevent_req_is_in_progress(req)) {
1898 goto post;
1901 ret = tstream_bsd_set_writeable_handler(bsds, ev,
1902 tstream_bsd_writev_handler,
1903 req);
1904 if (ret == -1) {
1905 tevent_req_error(req, errno);
1906 goto post;
1909 return req;
1911 post:
1912 tevent_req_post(req, ev);
1913 return req;
1916 static void tstream_bsd_writev_handler(void *private_data)
1918 struct tevent_req *req = talloc_get_type_abort(private_data,
1919 struct tevent_req);
1920 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1921 struct tstream_bsd_writev_state);
1922 struct tstream_context *stream = state->stream;
1923 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1924 ssize_t ret;
1925 int err;
1926 int _count;
1927 bool ok, retry;
1929 ret = writev(bsds->fd, state->vector, state->count);
1930 if (ret == 0) {
1931 /* propagate end of file */
1932 tevent_req_error(req, EPIPE);
1933 return;
1935 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1936 if (retry) {
1937 /* retry later */
1938 return;
1940 if (tevent_req_error(req, err)) {
1941 return;
1944 state->ret += ret;
1946 _count = state->count; /* tstream has size_t count, writev has int */
1947 ok = iov_advance(&state->vector, &_count, ret);
1948 state->count = _count;
1950 if (!ok) {
1951 tevent_req_error(req, EINVAL);
1952 return;
1955 if (state->count > 0) {
1956 /* we have more to read */
1957 return;
1960 tevent_req_done(req);
1963 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1965 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1966 struct tstream_bsd_writev_state);
1967 int ret;
1969 ret = tsocket_simple_int_recv(req, perrno);
1970 if (ret == 0) {
1971 ret = state->ret;
1974 tevent_req_received(req);
1975 return ret;
1978 struct tstream_bsd_disconnect_state {
1979 void *__dummy;
1982 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1983 struct tevent_context *ev,
1984 struct tstream_context *stream)
1986 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1987 struct tevent_req *req;
1988 struct tstream_bsd_disconnect_state *state;
1989 int ret;
1990 int err;
1991 bool dummy;
1993 req = tevent_req_create(mem_ctx, &state,
1994 struct tstream_bsd_disconnect_state);
1995 if (req == NULL) {
1996 return NULL;
1999 if (bsds->fd == -1) {
2000 tevent_req_error(req, ENOTCONN);
2001 goto post;
2004 TALLOC_FREE(bsds->fde);
2005 ret = close(bsds->fd);
2006 bsds->fd = -1;
2007 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
2008 if (tevent_req_error(req, err)) {
2009 goto post;
2012 tevent_req_done(req);
2013 post:
2014 tevent_req_post(req, ev);
2015 return req;
2018 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
2019 int *perrno)
2021 int ret;
2023 ret = tsocket_simple_int_recv(req, perrno);
2025 tevent_req_received(req);
2026 return ret;
2029 static const struct tstream_context_ops tstream_bsd_ops = {
2030 .name = "bsd",
2032 .pending_bytes = tstream_bsd_pending_bytes,
2034 .readv_send = tstream_bsd_readv_send,
2035 .readv_recv = tstream_bsd_readv_recv,
2037 .writev_send = tstream_bsd_writev_send,
2038 .writev_recv = tstream_bsd_writev_recv,
2040 .disconnect_send = tstream_bsd_disconnect_send,
2041 .disconnect_recv = tstream_bsd_disconnect_recv,
2044 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
2046 TALLOC_FREE(bsds->fde);
2047 if (bsds->fd != -1) {
2048 close(bsds->fd);
2049 bsds->fd = -1;
2051 return 0;
2054 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
2055 int fd,
2056 struct tstream_context **_stream,
2057 const char *location)
2059 struct tstream_context *stream;
2060 struct tstream_bsd *bsds;
2062 stream = tstream_context_create(mem_ctx,
2063 &tstream_bsd_ops,
2064 &bsds,
2065 struct tstream_bsd,
2066 location);
2067 if (!stream) {
2068 return -1;
2070 ZERO_STRUCTP(bsds);
2071 bsds->fd = fd;
2072 talloc_set_destructor(bsds, tstream_bsd_destructor);
2074 *_stream = stream;
2075 return 0;
2078 struct tstream_bsd_connect_state {
2079 int fd;
2080 struct tevent_fd *fde;
2081 struct tstream_conext *stream;
2082 struct tsocket_address *local;
2085 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
2087 TALLOC_FREE(state->fde);
2088 if (state->fd != -1) {
2089 close(state->fd);
2090 state->fd = -1;
2093 return 0;
2096 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2097 struct tevent_fd *fde,
2098 uint16_t flags,
2099 void *private_data);
2101 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2102 struct tevent_context *ev,
2103 int sys_errno,
2104 const struct tsocket_address *local,
2105 const struct tsocket_address *remote)
2107 struct tevent_req *req;
2108 struct tstream_bsd_connect_state *state;
2109 struct tsocket_address_bsd *lbsda =
2110 talloc_get_type_abort(local->private_data,
2111 struct tsocket_address_bsd);
2112 struct tsocket_address_bsd *lrbsda = NULL;
2113 struct tsocket_address_bsd *rbsda =
2114 talloc_get_type_abort(remote->private_data,
2115 struct tsocket_address_bsd);
2116 int ret;
2117 int err;
2118 bool retry;
2119 bool do_bind = false;
2120 bool do_reuseaddr = false;
2121 bool do_ipv6only = false;
2122 bool is_inet = false;
2123 int sa_fam = lbsda->u.sa.sa_family;
2125 req = tevent_req_create(mem_ctx, &state,
2126 struct tstream_bsd_connect_state);
2127 if (!req) {
2128 return NULL;
2130 state->fd = -1;
2131 state->fde = NULL;
2133 talloc_set_destructor(state, tstream_bsd_connect_destructor);
2135 /* give the wrappers a chance to report an error */
2136 if (sys_errno != 0) {
2137 tevent_req_error(req, sys_errno);
2138 goto post;
2141 switch (lbsda->u.sa.sa_family) {
2142 case AF_UNIX:
2143 if (lbsda->u.un.sun_path[0] != 0) {
2144 do_reuseaddr = true;
2145 do_bind = true;
2147 break;
2148 case AF_INET:
2149 if (lbsda->u.in.sin_port != 0) {
2150 do_reuseaddr = true;
2151 do_bind = true;
2153 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2154 do_bind = true;
2156 is_inet = true;
2157 break;
2158 #ifdef HAVE_IPV6
2159 case AF_INET6:
2160 if (lbsda->u.in6.sin6_port != 0) {
2161 do_reuseaddr = true;
2162 do_bind = true;
2164 if (memcmp(&in6addr_any,
2165 &lbsda->u.in6.sin6_addr,
2166 sizeof(in6addr_any)) != 0) {
2167 do_bind = true;
2169 is_inet = true;
2170 do_ipv6only = true;
2171 break;
2172 #endif
2173 default:
2174 tevent_req_error(req, EINVAL);
2175 goto post;
2178 if (!do_bind && is_inet) {
2179 sa_fam = rbsda->u.sa.sa_family;
2180 switch (sa_fam) {
2181 case AF_INET:
2182 do_ipv6only = false;
2183 break;
2184 #ifdef HAVE_IPV6
2185 case AF_INET6:
2186 do_ipv6only = true;
2187 break;
2188 #endif
2192 if (is_inet) {
2193 state->local = tsocket_address_create(state,
2194 &tsocket_address_bsd_ops,
2195 &lrbsda,
2196 struct tsocket_address_bsd,
2197 __location__ "bsd_connect");
2198 if (tevent_req_nomem(state->local, req)) {
2199 goto post;
2202 ZERO_STRUCTP(lrbsda);
2203 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2204 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2205 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2206 #endif
2209 state->fd = socket(sa_fam, SOCK_STREAM, 0);
2210 if (state->fd == -1) {
2211 tevent_req_error(req, errno);
2212 goto post;
2215 state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2216 if (state->fd == -1) {
2217 tevent_req_error(req, errno);
2218 goto post;
2221 #ifdef HAVE_IPV6
2222 if (do_ipv6only) {
2223 int val = 1;
2225 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2226 (const void *)&val, sizeof(val));
2227 if (ret == -1) {
2228 tevent_req_error(req, errno);
2229 goto post;
2232 #endif
2234 if (do_reuseaddr) {
2235 int val = 1;
2237 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2238 (const void *)&val, sizeof(val));
2239 if (ret == -1) {
2240 tevent_req_error(req, errno);
2241 goto post;
2245 if (do_bind) {
2246 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2247 if (ret == -1) {
2248 tevent_req_error(req, errno);
2249 goto post;
2253 if (rbsda->u.sa.sa_family != sa_fam) {
2254 tevent_req_error(req, EINVAL);
2255 goto post;
2258 ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2259 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2260 if (retry) {
2261 /* retry later */
2262 goto async;
2264 if (tevent_req_error(req, err)) {
2265 goto post;
2268 if (!state->local) {
2269 tevent_req_done(req);
2270 goto post;
2273 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2274 if (ret == -1) {
2275 tevent_req_error(req, errno);
2276 goto post;
2279 tevent_req_done(req);
2280 goto post;
2282 async:
2283 state->fde = tevent_add_fd(ev, state,
2284 state->fd,
2285 TEVENT_FD_READ | TEVENT_FD_WRITE,
2286 tstream_bsd_connect_fde_handler,
2287 req);
2288 if (tevent_req_nomem(state->fde, req)) {
2289 goto post;
2292 return req;
2294 post:
2295 tevent_req_post(req, ev);
2296 return req;
2299 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2300 struct tevent_fd *fde,
2301 uint16_t flags,
2302 void *private_data)
2304 struct tevent_req *req = talloc_get_type_abort(private_data,
2305 struct tevent_req);
2306 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2307 struct tstream_bsd_connect_state);
2308 struct tsocket_address_bsd *lrbsda = NULL;
2309 int ret;
2310 int error=0;
2311 socklen_t len = sizeof(error);
2312 int err;
2313 bool retry;
2315 ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2316 if (ret == 0) {
2317 if (error != 0) {
2318 errno = error;
2319 ret = -1;
2322 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2323 if (retry) {
2324 /* retry later */
2325 return;
2327 if (tevent_req_error(req, err)) {
2328 return;
2331 if (!state->local) {
2332 tevent_req_done(req);
2333 return;
2336 lrbsda = talloc_get_type_abort(state->local->private_data,
2337 struct tsocket_address_bsd);
2339 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2340 if (ret == -1) {
2341 tevent_req_error(req, errno);
2342 return;
2345 tevent_req_done(req);
2348 static int tstream_bsd_connect_recv(struct tevent_req *req,
2349 int *perrno,
2350 TALLOC_CTX *mem_ctx,
2351 struct tstream_context **stream,
2352 struct tsocket_address **local,
2353 const char *location)
2355 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2356 struct tstream_bsd_connect_state);
2357 int ret;
2359 ret = tsocket_simple_int_recv(req, perrno);
2360 if (ret == 0) {
2361 ret = _tstream_bsd_existing_socket(mem_ctx,
2362 state->fd,
2363 stream,
2364 location);
2365 if (ret == -1) {
2366 *perrno = errno;
2367 goto done;
2369 TALLOC_FREE(state->fde);
2370 state->fd = -1;
2372 if (local) {
2373 *local = talloc_move(mem_ctx, &state->local);
2377 done:
2378 tevent_req_received(req);
2379 return ret;
2382 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2383 struct tevent_context *ev,
2384 const struct tsocket_address *local,
2385 const struct tsocket_address *remote)
2387 struct tsocket_address_bsd *lbsda =
2388 talloc_get_type_abort(local->private_data,
2389 struct tsocket_address_bsd);
2390 struct tevent_req *req;
2391 int sys_errno = 0;
2393 switch (lbsda->u.sa.sa_family) {
2394 case AF_INET:
2395 break;
2396 #ifdef HAVE_IPV6
2397 case AF_INET6:
2398 break;
2399 #endif
2400 default:
2401 sys_errno = EINVAL;
2402 break;
2405 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2407 return req;
2410 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2411 int *perrno,
2412 TALLOC_CTX *mem_ctx,
2413 struct tstream_context **stream,
2414 struct tsocket_address **local,
2415 const char *location)
2417 return tstream_bsd_connect_recv(req, perrno,
2418 mem_ctx, stream, local,
2419 location);
2422 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2423 struct tevent_context *ev,
2424 const struct tsocket_address *local,
2425 const struct tsocket_address *remote)
2427 struct tsocket_address_bsd *lbsda =
2428 talloc_get_type_abort(local->private_data,
2429 struct tsocket_address_bsd);
2430 struct tevent_req *req;
2431 int sys_errno = 0;
2433 switch (lbsda->u.sa.sa_family) {
2434 case AF_UNIX:
2435 break;
2436 default:
2437 sys_errno = EINVAL;
2438 break;
2441 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2443 return req;
2446 int _tstream_unix_connect_recv(struct tevent_req *req,
2447 int *perrno,
2448 TALLOC_CTX *mem_ctx,
2449 struct tstream_context **stream,
2450 const char *location)
2452 return tstream_bsd_connect_recv(req, perrno,
2453 mem_ctx, stream, NULL,
2454 location);
2457 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2458 struct tstream_context **_stream1,
2459 TALLOC_CTX *mem_ctx2,
2460 struct tstream_context **_stream2,
2461 const char *location)
2463 int ret;
2464 int fds[2];
2465 int fd1;
2466 int fd2;
2467 struct tstream_context *stream1 = NULL;
2468 struct tstream_context *stream2 = NULL;
2470 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2471 if (ret == -1) {
2472 return -1;
2474 fd1 = fds[0];
2475 fd2 = fds[1];
2477 fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2478 if (fd1 == -1) {
2479 int sys_errno = errno;
2480 close(fd2);
2481 errno = sys_errno;
2482 return -1;
2485 fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2486 if (fd2 == -1) {
2487 int sys_errno = errno;
2488 close(fd1);
2489 errno = sys_errno;
2490 return -1;
2493 ret = _tstream_bsd_existing_socket(mem_ctx1,
2494 fd1,
2495 &stream1,
2496 location);
2497 if (ret == -1) {
2498 int sys_errno = errno;
2499 close(fd1);
2500 close(fd2);
2501 errno = sys_errno;
2502 return -1;
2505 ret = _tstream_bsd_existing_socket(mem_ctx2,
2506 fd2,
2507 &stream2,
2508 location);
2509 if (ret == -1) {
2510 int sys_errno = errno;
2511 talloc_free(stream1);
2512 close(fd2);
2513 errno = sys_errno;
2514 return -1;
2517 *_stream1 = stream1;
2518 *_stream2 = stream2;
2519 return 0;