s3-printing: fix potential print db refcount leak
[Samba/vl.git] / lib / tsocket / tsocket_bsd.c
blob9e800652270db19de021c90b930212ae3b9006c6
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;
658 void *readable_private;
659 void (*readable_handler)(void *private_data);
660 void *writeable_private;
661 void (*writeable_handler)(void *private_data);
664 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
665 struct tevent_fd *fde,
666 uint16_t flags,
667 void *private_data)
669 struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
670 struct tdgram_bsd);
672 if (flags & TEVENT_FD_WRITE) {
673 bsds->writeable_handler(bsds->writeable_private);
674 return;
676 if (flags & TEVENT_FD_READ) {
677 if (!bsds->readable_handler) {
678 TEVENT_FD_NOT_READABLE(bsds->fde);
679 return;
681 bsds->readable_handler(bsds->readable_private);
682 return;
686 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
687 struct tevent_context *ev,
688 void (*handler)(void *private_data),
689 void *private_data)
691 if (ev == NULL) {
692 if (handler) {
693 errno = EINVAL;
694 return -1;
696 if (!bsds->readable_handler) {
697 return 0;
699 bsds->readable_handler = NULL;
700 bsds->readable_private = NULL;
702 return 0;
705 /* read and write must use the same tevent_context */
706 if (bsds->event_ptr != ev) {
707 if (bsds->readable_handler || bsds->writeable_handler) {
708 errno = EINVAL;
709 return -1;
711 bsds->event_ptr = NULL;
712 TALLOC_FREE(bsds->fde);
715 if (tevent_fd_get_flags(bsds->fde) == 0) {
716 TALLOC_FREE(bsds->fde);
718 bsds->fde = tevent_add_fd(ev, bsds,
719 bsds->fd, TEVENT_FD_READ,
720 tdgram_bsd_fde_handler,
721 bsds);
722 if (!bsds->fde) {
723 errno = ENOMEM;
724 return -1;
727 /* cache the event context we're running on */
728 bsds->event_ptr = ev;
729 } else if (!bsds->readable_handler) {
730 TEVENT_FD_READABLE(bsds->fde);
733 bsds->readable_handler = handler;
734 bsds->readable_private = private_data;
736 return 0;
739 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
740 struct tevent_context *ev,
741 void (*handler)(void *private_data),
742 void *private_data)
744 if (ev == NULL) {
745 if (handler) {
746 errno = EINVAL;
747 return -1;
749 if (!bsds->writeable_handler) {
750 return 0;
752 bsds->writeable_handler = NULL;
753 bsds->writeable_private = NULL;
754 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
756 return 0;
759 /* read and write must use the same tevent_context */
760 if (bsds->event_ptr != ev) {
761 if (bsds->readable_handler || bsds->writeable_handler) {
762 errno = EINVAL;
763 return -1;
765 bsds->event_ptr = NULL;
766 TALLOC_FREE(bsds->fde);
769 if (tevent_fd_get_flags(bsds->fde) == 0) {
770 TALLOC_FREE(bsds->fde);
772 bsds->fde = tevent_add_fd(ev, bsds,
773 bsds->fd, TEVENT_FD_WRITE,
774 tdgram_bsd_fde_handler,
775 bsds);
776 if (!bsds->fde) {
777 errno = ENOMEM;
778 return -1;
781 /* cache the event context we're running on */
782 bsds->event_ptr = ev;
783 } else if (!bsds->writeable_handler) {
784 TEVENT_FD_WRITEABLE(bsds->fde);
787 bsds->writeable_handler = handler;
788 bsds->writeable_private = private_data;
790 return 0;
793 struct tdgram_bsd_recvfrom_state {
794 struct tdgram_context *dgram;
796 uint8_t *buf;
797 size_t len;
798 struct tsocket_address *src;
801 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
803 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
804 struct tdgram_bsd);
806 tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
808 return 0;
811 static void tdgram_bsd_recvfrom_handler(void *private_data);
813 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
814 struct tevent_context *ev,
815 struct tdgram_context *dgram)
817 struct tevent_req *req;
818 struct tdgram_bsd_recvfrom_state *state;
819 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
820 int ret;
822 req = tevent_req_create(mem_ctx, &state,
823 struct tdgram_bsd_recvfrom_state);
824 if (!req) {
825 return NULL;
828 state->dgram = dgram;
829 state->buf = NULL;
830 state->len = 0;
831 state->src = NULL;
833 talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
835 if (bsds->fd == -1) {
836 tevent_req_error(req, ENOTCONN);
837 goto post;
841 * this is a fast path, not waiting for the
842 * socket to become explicit readable gains
843 * about 10%-20% performance in benchmark tests.
845 tdgram_bsd_recvfrom_handler(req);
846 if (!tevent_req_is_in_progress(req)) {
847 goto post;
850 ret = tdgram_bsd_set_readable_handler(bsds, ev,
851 tdgram_bsd_recvfrom_handler,
852 req);
853 if (ret == -1) {
854 tevent_req_error(req, errno);
855 goto post;
858 return req;
860 post:
861 tevent_req_post(req, ev);
862 return req;
865 static void tdgram_bsd_recvfrom_handler(void *private_data)
867 struct tevent_req *req = talloc_get_type_abort(private_data,
868 struct tevent_req);
869 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
870 struct tdgram_bsd_recvfrom_state);
871 struct tdgram_context *dgram = state->dgram;
872 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
873 struct tsocket_address_bsd *bsda;
874 ssize_t ret;
875 int err;
876 bool retry;
878 ret = tsocket_bsd_pending(bsds->fd);
879 if (ret == 0) {
880 /* retry later */
881 return;
883 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
884 if (retry) {
885 /* retry later */
886 return;
888 if (tevent_req_error(req, err)) {
889 return;
892 state->buf = talloc_array(state, uint8_t, ret);
893 if (tevent_req_nomem(state->buf, req)) {
894 return;
896 state->len = ret;
898 state->src = tsocket_address_create(state,
899 &tsocket_address_bsd_ops,
900 &bsda,
901 struct tsocket_address_bsd,
902 __location__ "bsd_recvfrom");
903 if (tevent_req_nomem(state->src, req)) {
904 return;
907 ZERO_STRUCTP(bsda);
908 bsda->sa_socklen = sizeof(bsda->u.ss);
909 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
910 bsda->u.sa.sa_len = bsda->sa_socklen;
911 #endif
913 ret = recvfrom(bsds->fd, state->buf, state->len, 0,
914 &bsda->u.sa, &bsda->sa_socklen);
915 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
916 if (retry) {
917 /* retry later */
918 return;
920 if (tevent_req_error(req, err)) {
921 return;
925 * Some systems (FreeBSD, see bug #7115) return too much
926 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
927 * the return value includes some IP/UDP header bytes,
928 * while recvfrom() just returns the payload.
930 state->buf = talloc_realloc(state, state->buf, uint8_t, ret);
931 if (tevent_req_nomem(state->buf, req)) {
932 return;
934 state->len = ret;
936 tevent_req_done(req);
939 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
940 int *perrno,
941 TALLOC_CTX *mem_ctx,
942 uint8_t **buf,
943 struct tsocket_address **src)
945 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
946 struct tdgram_bsd_recvfrom_state);
947 ssize_t ret;
949 ret = tsocket_simple_int_recv(req, perrno);
950 if (ret == 0) {
951 *buf = talloc_move(mem_ctx, &state->buf);
952 ret = state->len;
953 if (src) {
954 *src = talloc_move(mem_ctx, &state->src);
958 tevent_req_received(req);
959 return ret;
962 struct tdgram_bsd_sendto_state {
963 struct tdgram_context *dgram;
965 const uint8_t *buf;
966 size_t len;
967 const struct tsocket_address *dst;
969 ssize_t ret;
972 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
974 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
975 struct tdgram_bsd);
977 tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
979 return 0;
982 static void tdgram_bsd_sendto_handler(void *private_data);
984 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
985 struct tevent_context *ev,
986 struct tdgram_context *dgram,
987 const uint8_t *buf,
988 size_t len,
989 const struct tsocket_address *dst)
991 struct tevent_req *req;
992 struct tdgram_bsd_sendto_state *state;
993 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
994 int ret;
996 req = tevent_req_create(mem_ctx, &state,
997 struct tdgram_bsd_sendto_state);
998 if (!req) {
999 return NULL;
1002 state->dgram = dgram;
1003 state->buf = buf;
1004 state->len = len;
1005 state->dst = dst;
1006 state->ret = -1;
1008 talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
1010 if (bsds->fd == -1) {
1011 tevent_req_error(req, ENOTCONN);
1012 goto post;
1016 * this is a fast path, not waiting for the
1017 * socket to become explicit writeable gains
1018 * about 10%-20% performance in benchmark tests.
1020 tdgram_bsd_sendto_handler(req);
1021 if (!tevent_req_is_in_progress(req)) {
1022 goto post;
1025 ret = tdgram_bsd_set_writeable_handler(bsds, ev,
1026 tdgram_bsd_sendto_handler,
1027 req);
1028 if (ret == -1) {
1029 tevent_req_error(req, errno);
1030 goto post;
1033 return req;
1035 post:
1036 tevent_req_post(req, ev);
1037 return req;
1040 static void tdgram_bsd_sendto_handler(void *private_data)
1042 struct tevent_req *req = talloc_get_type_abort(private_data,
1043 struct tevent_req);
1044 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1045 struct tdgram_bsd_sendto_state);
1046 struct tdgram_context *dgram = state->dgram;
1047 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1048 struct sockaddr *sa = NULL;
1049 socklen_t sa_socklen = 0;
1050 ssize_t ret;
1051 int err;
1052 bool retry;
1054 if (state->dst) {
1055 struct tsocket_address_bsd *bsda =
1056 talloc_get_type(state->dst->private_data,
1057 struct tsocket_address_bsd);
1059 sa = &bsda->u.sa;
1060 sa_socklen = bsda->sa_socklen;
1063 ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_socklen);
1064 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1065 if (retry) {
1066 /* retry later */
1067 return;
1069 if (tevent_req_error(req, err)) {
1070 return;
1073 state->ret = ret;
1075 tevent_req_done(req);
1078 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1080 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1081 struct tdgram_bsd_sendto_state);
1082 ssize_t ret;
1084 ret = tsocket_simple_int_recv(req, perrno);
1085 if (ret == 0) {
1086 ret = state->ret;
1089 tevent_req_received(req);
1090 return ret;
1093 struct tdgram_bsd_disconnect_state {
1094 uint8_t __dummy;
1097 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1098 struct tevent_context *ev,
1099 struct tdgram_context *dgram)
1101 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1102 struct tevent_req *req;
1103 struct tdgram_bsd_disconnect_state *state;
1104 int ret;
1105 int err;
1106 bool dummy;
1108 req = tevent_req_create(mem_ctx, &state,
1109 struct tdgram_bsd_disconnect_state);
1110 if (req == NULL) {
1111 return NULL;
1114 if (bsds->fd == -1) {
1115 tevent_req_error(req, ENOTCONN);
1116 goto post;
1119 TALLOC_FREE(bsds->fde);
1120 ret = close(bsds->fd);
1121 bsds->fd = -1;
1122 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1123 if (tevent_req_error(req, err)) {
1124 goto post;
1127 tevent_req_done(req);
1128 post:
1129 tevent_req_post(req, ev);
1130 return req;
1133 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1134 int *perrno)
1136 int ret;
1138 ret = tsocket_simple_int_recv(req, perrno);
1140 tevent_req_received(req);
1141 return ret;
1144 static const struct tdgram_context_ops tdgram_bsd_ops = {
1145 .name = "bsd",
1147 .recvfrom_send = tdgram_bsd_recvfrom_send,
1148 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1150 .sendto_send = tdgram_bsd_sendto_send,
1151 .sendto_recv = tdgram_bsd_sendto_recv,
1153 .disconnect_send = tdgram_bsd_disconnect_send,
1154 .disconnect_recv = tdgram_bsd_disconnect_recv,
1157 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1159 TALLOC_FREE(bsds->fde);
1160 if (bsds->fd != -1) {
1161 close(bsds->fd);
1162 bsds->fd = -1;
1164 return 0;
1167 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1168 const struct tsocket_address *remote,
1169 bool broadcast,
1170 TALLOC_CTX *mem_ctx,
1171 struct tdgram_context **_dgram,
1172 const char *location)
1174 struct tsocket_address_bsd *lbsda =
1175 talloc_get_type_abort(local->private_data,
1176 struct tsocket_address_bsd);
1177 struct tsocket_address_bsd *rbsda = NULL;
1178 struct tdgram_context *dgram;
1179 struct tdgram_bsd *bsds;
1180 int fd;
1181 int ret;
1182 bool do_bind = false;
1183 bool do_reuseaddr = false;
1184 bool do_ipv6only = false;
1185 bool is_inet = false;
1186 int sa_fam = lbsda->u.sa.sa_family;
1188 if (remote) {
1189 rbsda = talloc_get_type_abort(remote->private_data,
1190 struct tsocket_address_bsd);
1193 switch (lbsda->u.sa.sa_family) {
1194 case AF_UNIX:
1195 if (broadcast) {
1196 errno = EINVAL;
1197 return -1;
1199 if (lbsda->u.un.sun_path[0] != 0) {
1200 do_reuseaddr = true;
1201 do_bind = true;
1203 break;
1204 case AF_INET:
1205 if (lbsda->u.in.sin_port != 0) {
1206 do_reuseaddr = true;
1207 do_bind = true;
1209 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1210 do_bind = true;
1212 is_inet = true;
1213 break;
1214 #ifdef HAVE_IPV6
1215 case AF_INET6:
1216 if (lbsda->u.in6.sin6_port != 0) {
1217 do_reuseaddr = true;
1218 do_bind = true;
1220 if (memcmp(&in6addr_any,
1221 &lbsda->u.in6.sin6_addr,
1222 sizeof(in6addr_any)) != 0) {
1223 do_bind = true;
1225 is_inet = true;
1226 do_ipv6only = true;
1227 break;
1228 #endif
1229 default:
1230 errno = EINVAL;
1231 return -1;
1234 if (!do_bind && is_inet && rbsda) {
1235 sa_fam = rbsda->u.sa.sa_family;
1236 switch (sa_fam) {
1237 case AF_INET:
1238 do_ipv6only = false;
1239 break;
1240 #ifdef HAVE_IPV6
1241 case AF_INET6:
1242 do_ipv6only = true;
1243 break;
1244 #endif
1248 fd = socket(sa_fam, SOCK_DGRAM, 0);
1249 if (fd < 0) {
1250 return -1;
1253 fd = tsocket_bsd_common_prepare_fd(fd, true);
1254 if (fd < 0) {
1255 return -1;
1258 dgram = tdgram_context_create(mem_ctx,
1259 &tdgram_bsd_ops,
1260 &bsds,
1261 struct tdgram_bsd,
1262 location);
1263 if (!dgram) {
1264 int saved_errno = errno;
1265 close(fd);
1266 errno = saved_errno;
1267 return -1;
1269 ZERO_STRUCTP(bsds);
1270 bsds->fd = fd;
1271 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1273 #ifdef HAVE_IPV6
1274 if (do_ipv6only) {
1275 int val = 1;
1277 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1278 (const void *)&val, sizeof(val));
1279 if (ret == -1) {
1280 int saved_errno = errno;
1281 talloc_free(dgram);
1282 errno = saved_errno;
1283 return -1;
1286 #endif
1288 if (broadcast) {
1289 int val = 1;
1291 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1292 (const void *)&val, sizeof(val));
1293 if (ret == -1) {
1294 int saved_errno = errno;
1295 talloc_free(dgram);
1296 errno = saved_errno;
1297 return -1;
1301 if (do_reuseaddr) {
1302 int val = 1;
1304 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1305 (const void *)&val, sizeof(val));
1306 if (ret == -1) {
1307 int saved_errno = errno;
1308 talloc_free(dgram);
1309 errno = saved_errno;
1310 return -1;
1314 if (do_bind) {
1315 ret = bind(fd, &lbsda->u.sa, lbsda->sa_socklen);
1316 if (ret == -1) {
1317 int saved_errno = errno;
1318 talloc_free(dgram);
1319 errno = saved_errno;
1320 return -1;
1324 if (rbsda) {
1325 if (rbsda->u.sa.sa_family != sa_fam) {
1326 talloc_free(dgram);
1327 errno = EINVAL;
1328 return -1;
1331 ret = connect(fd, &rbsda->u.sa, rbsda->sa_socklen);
1332 if (ret == -1) {
1333 int saved_errno = errno;
1334 talloc_free(dgram);
1335 errno = saved_errno;
1336 return -1;
1340 *_dgram = dgram;
1341 return 0;
1344 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1345 const struct tsocket_address *remote,
1346 TALLOC_CTX *mem_ctx,
1347 struct tdgram_context **dgram,
1348 const char *location)
1350 struct tsocket_address_bsd *lbsda =
1351 talloc_get_type_abort(local->private_data,
1352 struct tsocket_address_bsd);
1353 int ret;
1355 switch (lbsda->u.sa.sa_family) {
1356 case AF_INET:
1357 break;
1358 #ifdef HAVE_IPV6
1359 case AF_INET6:
1360 break;
1361 #endif
1362 default:
1363 errno = EINVAL;
1364 return -1;
1367 ret = tdgram_bsd_dgram_socket(local, remote, false,
1368 mem_ctx, dgram, location);
1370 return ret;
1373 int _tdgram_unix_socket(const struct tsocket_address *local,
1374 const struct tsocket_address *remote,
1375 TALLOC_CTX *mem_ctx,
1376 struct tdgram_context **dgram,
1377 const char *location)
1379 struct tsocket_address_bsd *lbsda =
1380 talloc_get_type_abort(local->private_data,
1381 struct tsocket_address_bsd);
1382 int ret;
1384 switch (lbsda->u.sa.sa_family) {
1385 case AF_UNIX:
1386 break;
1387 default:
1388 errno = EINVAL;
1389 return -1;
1392 ret = tdgram_bsd_dgram_socket(local, remote, false,
1393 mem_ctx, dgram, location);
1395 return ret;
1398 struct tstream_bsd {
1399 int fd;
1401 void *event_ptr;
1402 struct tevent_fd *fde;
1404 void *readable_private;
1405 void (*readable_handler)(void *private_data);
1406 void *writeable_private;
1407 void (*writeable_handler)(void *private_data);
1410 static void tstream_bsd_fde_handler(struct tevent_context *ev,
1411 struct tevent_fd *fde,
1412 uint16_t flags,
1413 void *private_data)
1415 struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1416 struct tstream_bsd);
1418 if (flags & TEVENT_FD_WRITE) {
1419 bsds->writeable_handler(bsds->writeable_private);
1420 return;
1422 if (flags & TEVENT_FD_READ) {
1423 if (!bsds->readable_handler) {
1424 if (bsds->writeable_handler) {
1425 bsds->writeable_handler(bsds->writeable_private);
1426 return;
1428 TEVENT_FD_NOT_READABLE(bsds->fde);
1429 return;
1431 bsds->readable_handler(bsds->readable_private);
1432 return;
1436 static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1437 struct tevent_context *ev,
1438 void (*handler)(void *private_data),
1439 void *private_data)
1441 if (ev == NULL) {
1442 if (handler) {
1443 errno = EINVAL;
1444 return -1;
1446 if (!bsds->readable_handler) {
1447 return 0;
1449 bsds->readable_handler = NULL;
1450 bsds->readable_private = NULL;
1452 return 0;
1455 /* read and write must use the same tevent_context */
1456 if (bsds->event_ptr != ev) {
1457 if (bsds->readable_handler || bsds->writeable_handler) {
1458 errno = EINVAL;
1459 return -1;
1461 bsds->event_ptr = NULL;
1462 TALLOC_FREE(bsds->fde);
1465 if (tevent_fd_get_flags(bsds->fde) == 0) {
1466 TALLOC_FREE(bsds->fde);
1468 bsds->fde = tevent_add_fd(ev, bsds,
1469 bsds->fd, TEVENT_FD_READ,
1470 tstream_bsd_fde_handler,
1471 bsds);
1472 if (!bsds->fde) {
1473 errno = ENOMEM;
1474 return -1;
1477 /* cache the event context we're running on */
1478 bsds->event_ptr = ev;
1479 } else if (!bsds->readable_handler) {
1480 TEVENT_FD_READABLE(bsds->fde);
1483 bsds->readable_handler = handler;
1484 bsds->readable_private = private_data;
1486 return 0;
1489 static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1490 struct tevent_context *ev,
1491 void (*handler)(void *private_data),
1492 void *private_data)
1494 if (ev == NULL) {
1495 if (handler) {
1496 errno = EINVAL;
1497 return -1;
1499 if (!bsds->writeable_handler) {
1500 return 0;
1502 bsds->writeable_handler = NULL;
1503 bsds->writeable_private = NULL;
1504 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1506 return 0;
1509 /* read and write must use the same tevent_context */
1510 if (bsds->event_ptr != ev) {
1511 if (bsds->readable_handler || bsds->writeable_handler) {
1512 errno = EINVAL;
1513 return -1;
1515 bsds->event_ptr = NULL;
1516 TALLOC_FREE(bsds->fde);
1519 if (tevent_fd_get_flags(bsds->fde) == 0) {
1520 TALLOC_FREE(bsds->fde);
1522 bsds->fde = tevent_add_fd(ev, bsds,
1523 bsds->fd,
1524 TEVENT_FD_READ | TEVENT_FD_WRITE,
1525 tstream_bsd_fde_handler,
1526 bsds);
1527 if (!bsds->fde) {
1528 errno = ENOMEM;
1529 return -1;
1532 /* cache the event context we're running on */
1533 bsds->event_ptr = ev;
1534 } else if (!bsds->writeable_handler) {
1535 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1536 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1537 tevent_fd_set_flags(bsds->fde, flags);
1540 bsds->writeable_handler = handler;
1541 bsds->writeable_private = private_data;
1543 return 0;
1546 static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1548 struct tstream_bsd *bsds = tstream_context_data(stream,
1549 struct tstream_bsd);
1550 ssize_t ret;
1552 if (bsds->fd == -1) {
1553 errno = ENOTCONN;
1554 return -1;
1557 ret = tsocket_bsd_pending(bsds->fd);
1559 return ret;
1562 struct tstream_bsd_readv_state {
1563 struct tstream_context *stream;
1565 struct iovec *vector;
1566 size_t count;
1568 int ret;
1571 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1573 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1574 struct tstream_bsd);
1576 tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1578 return 0;
1581 static void tstream_bsd_readv_handler(void *private_data);
1583 static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1584 struct tevent_context *ev,
1585 struct tstream_context *stream,
1586 struct iovec *vector,
1587 size_t count)
1589 struct tevent_req *req;
1590 struct tstream_bsd_readv_state *state;
1591 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1592 int ret;
1594 req = tevent_req_create(mem_ctx, &state,
1595 struct tstream_bsd_readv_state);
1596 if (!req) {
1597 return NULL;
1600 state->stream = stream;
1601 /* we make a copy of the vector so that we can modify it */
1602 state->vector = talloc_array(state, struct iovec, count);
1603 if (tevent_req_nomem(state->vector, req)) {
1604 goto post;
1606 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1607 state->count = count;
1608 state->ret = 0;
1610 talloc_set_destructor(state, tstream_bsd_readv_destructor);
1612 if (bsds->fd == -1) {
1613 tevent_req_error(req, ENOTCONN);
1614 goto post;
1618 * this is a fast path, not waiting for the
1619 * socket to become explicit readable gains
1620 * about 10%-20% performance in benchmark tests.
1622 tstream_bsd_readv_handler(req);
1623 if (!tevent_req_is_in_progress(req)) {
1624 goto post;
1627 ret = tstream_bsd_set_readable_handler(bsds, ev,
1628 tstream_bsd_readv_handler,
1629 req);
1630 if (ret == -1) {
1631 tevent_req_error(req, errno);
1632 goto post;
1635 return req;
1637 post:
1638 tevent_req_post(req, ev);
1639 return req;
1642 static void tstream_bsd_readv_handler(void *private_data)
1644 struct tevent_req *req = talloc_get_type_abort(private_data,
1645 struct tevent_req);
1646 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1647 struct tstream_bsd_readv_state);
1648 struct tstream_context *stream = state->stream;
1649 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1650 int ret;
1651 int err;
1652 bool retry;
1654 ret = readv(bsds->fd, state->vector, state->count);
1655 if (ret == 0) {
1656 /* propagate end of file */
1657 tevent_req_error(req, EPIPE);
1658 return;
1660 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1661 if (retry) {
1662 /* retry later */
1663 return;
1665 if (tevent_req_error(req, err)) {
1666 return;
1669 state->ret += ret;
1671 while (ret > 0) {
1672 if (ret < state->vector[0].iov_len) {
1673 uint8_t *base;
1674 base = (uint8_t *)state->vector[0].iov_base;
1675 base += ret;
1676 state->vector[0].iov_base = (void *)base;
1677 state->vector[0].iov_len -= ret;
1678 break;
1680 ret -= state->vector[0].iov_len;
1681 state->vector += 1;
1682 state->count -= 1;
1686 * there're maybe some empty vectors at the end
1687 * which we need to skip, otherwise we would get
1688 * ret == 0 from the readv() call and return EPIPE
1690 while (state->count > 0) {
1691 if (state->vector[0].iov_len > 0) {
1692 break;
1694 state->vector += 1;
1695 state->count -= 1;
1698 if (state->count > 0) {
1699 /* we have more to read */
1700 return;
1703 tevent_req_done(req);
1706 static int tstream_bsd_readv_recv(struct tevent_req *req,
1707 int *perrno)
1709 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1710 struct tstream_bsd_readv_state);
1711 int ret;
1713 ret = tsocket_simple_int_recv(req, perrno);
1714 if (ret == 0) {
1715 ret = state->ret;
1718 tevent_req_received(req);
1719 return ret;
1722 struct tstream_bsd_writev_state {
1723 struct tstream_context *stream;
1725 struct iovec *vector;
1726 size_t count;
1728 int ret;
1731 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1733 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1734 struct tstream_bsd);
1736 tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1738 return 0;
1741 static void tstream_bsd_writev_handler(void *private_data);
1743 static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1744 struct tevent_context *ev,
1745 struct tstream_context *stream,
1746 const struct iovec *vector,
1747 size_t count)
1749 struct tevent_req *req;
1750 struct tstream_bsd_writev_state *state;
1751 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1752 int ret;
1754 req = tevent_req_create(mem_ctx, &state,
1755 struct tstream_bsd_writev_state);
1756 if (!req) {
1757 return NULL;
1760 state->stream = stream;
1761 /* we make a copy of the vector so that we can modify it */
1762 state->vector = talloc_array(state, struct iovec, count);
1763 if (tevent_req_nomem(state->vector, req)) {
1764 goto post;
1766 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1767 state->count = count;
1768 state->ret = 0;
1770 talloc_set_destructor(state, tstream_bsd_writev_destructor);
1772 if (bsds->fd == -1) {
1773 tevent_req_error(req, ENOTCONN);
1774 goto post;
1778 * this is a fast path, not waiting for the
1779 * socket to become explicit writeable gains
1780 * about 10%-20% performance in benchmark tests.
1782 tstream_bsd_writev_handler(req);
1783 if (!tevent_req_is_in_progress(req)) {
1784 goto post;
1787 ret = tstream_bsd_set_writeable_handler(bsds, ev,
1788 tstream_bsd_writev_handler,
1789 req);
1790 if (ret == -1) {
1791 tevent_req_error(req, errno);
1792 goto post;
1795 return req;
1797 post:
1798 tevent_req_post(req, ev);
1799 return req;
1802 static void tstream_bsd_writev_handler(void *private_data)
1804 struct tevent_req *req = talloc_get_type_abort(private_data,
1805 struct tevent_req);
1806 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1807 struct tstream_bsd_writev_state);
1808 struct tstream_context *stream = state->stream;
1809 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1810 ssize_t ret;
1811 int err;
1812 bool retry;
1814 ret = writev(bsds->fd, state->vector, state->count);
1815 if (ret == 0) {
1816 /* propagate end of file */
1817 tevent_req_error(req, EPIPE);
1818 return;
1820 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1821 if (retry) {
1822 /* retry later */
1823 return;
1825 if (tevent_req_error(req, err)) {
1826 return;
1829 state->ret += ret;
1831 while (ret > 0) {
1832 if (ret < state->vector[0].iov_len) {
1833 uint8_t *base;
1834 base = (uint8_t *)state->vector[0].iov_base;
1835 base += ret;
1836 state->vector[0].iov_base = (void *)base;
1837 state->vector[0].iov_len -= ret;
1838 break;
1840 ret -= state->vector[0].iov_len;
1841 state->vector += 1;
1842 state->count -= 1;
1846 * there're maybe some empty vectors at the end
1847 * which we need to skip, otherwise we would get
1848 * ret == 0 from the writev() call and return EPIPE
1850 while (state->count > 0) {
1851 if (state->vector[0].iov_len > 0) {
1852 break;
1854 state->vector += 1;
1855 state->count -= 1;
1858 if (state->count > 0) {
1859 /* we have more to read */
1860 return;
1863 tevent_req_done(req);
1866 static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1868 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1869 struct tstream_bsd_writev_state);
1870 int ret;
1872 ret = tsocket_simple_int_recv(req, perrno);
1873 if (ret == 0) {
1874 ret = state->ret;
1877 tevent_req_received(req);
1878 return ret;
1881 struct tstream_bsd_disconnect_state {
1882 void *__dummy;
1885 static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1886 struct tevent_context *ev,
1887 struct tstream_context *stream)
1889 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1890 struct tevent_req *req;
1891 struct tstream_bsd_disconnect_state *state;
1892 int ret;
1893 int err;
1894 bool dummy;
1896 req = tevent_req_create(mem_ctx, &state,
1897 struct tstream_bsd_disconnect_state);
1898 if (req == NULL) {
1899 return NULL;
1902 if (bsds->fd == -1) {
1903 tevent_req_error(req, ENOTCONN);
1904 goto post;
1907 TALLOC_FREE(bsds->fde);
1908 ret = close(bsds->fd);
1909 bsds->fd = -1;
1910 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1911 if (tevent_req_error(req, err)) {
1912 goto post;
1915 tevent_req_done(req);
1916 post:
1917 tevent_req_post(req, ev);
1918 return req;
1921 static int tstream_bsd_disconnect_recv(struct tevent_req *req,
1922 int *perrno)
1924 int ret;
1926 ret = tsocket_simple_int_recv(req, perrno);
1928 tevent_req_received(req);
1929 return ret;
1932 static const struct tstream_context_ops tstream_bsd_ops = {
1933 .name = "bsd",
1935 .pending_bytes = tstream_bsd_pending_bytes,
1937 .readv_send = tstream_bsd_readv_send,
1938 .readv_recv = tstream_bsd_readv_recv,
1940 .writev_send = tstream_bsd_writev_send,
1941 .writev_recv = tstream_bsd_writev_recv,
1943 .disconnect_send = tstream_bsd_disconnect_send,
1944 .disconnect_recv = tstream_bsd_disconnect_recv,
1947 static int tstream_bsd_destructor(struct tstream_bsd *bsds)
1949 TALLOC_FREE(bsds->fde);
1950 if (bsds->fd != -1) {
1951 close(bsds->fd);
1952 bsds->fd = -1;
1954 return 0;
1957 int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1958 int fd,
1959 struct tstream_context **_stream,
1960 const char *location)
1962 struct tstream_context *stream;
1963 struct tstream_bsd *bsds;
1965 stream = tstream_context_create(mem_ctx,
1966 &tstream_bsd_ops,
1967 &bsds,
1968 struct tstream_bsd,
1969 location);
1970 if (!stream) {
1971 return -1;
1973 ZERO_STRUCTP(bsds);
1974 bsds->fd = fd;
1975 talloc_set_destructor(bsds, tstream_bsd_destructor);
1977 *_stream = stream;
1978 return 0;
1981 struct tstream_bsd_connect_state {
1982 int fd;
1983 struct tevent_fd *fde;
1984 struct tstream_conext *stream;
1985 struct tsocket_address *local;
1988 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
1990 TALLOC_FREE(state->fde);
1991 if (state->fd != -1) {
1992 close(state->fd);
1993 state->fd = -1;
1996 return 0;
1999 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2000 struct tevent_fd *fde,
2001 uint16_t flags,
2002 void *private_data);
2004 static struct tevent_req *tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
2005 struct tevent_context *ev,
2006 int sys_errno,
2007 const struct tsocket_address *local,
2008 const struct tsocket_address *remote)
2010 struct tevent_req *req;
2011 struct tstream_bsd_connect_state *state;
2012 struct tsocket_address_bsd *lbsda =
2013 talloc_get_type_abort(local->private_data,
2014 struct tsocket_address_bsd);
2015 struct tsocket_address_bsd *lrbsda = NULL;
2016 struct tsocket_address_bsd *rbsda =
2017 talloc_get_type_abort(remote->private_data,
2018 struct tsocket_address_bsd);
2019 int ret;
2020 int err;
2021 bool retry;
2022 bool do_bind = false;
2023 bool do_reuseaddr = false;
2024 bool do_ipv6only = false;
2025 bool is_inet = false;
2026 int sa_fam = lbsda->u.sa.sa_family;
2028 req = tevent_req_create(mem_ctx, &state,
2029 struct tstream_bsd_connect_state);
2030 if (!req) {
2031 return NULL;
2033 state->fd = -1;
2034 state->fde = NULL;
2036 talloc_set_destructor(state, tstream_bsd_connect_destructor);
2038 /* give the wrappers a chance to report an error */
2039 if (sys_errno != 0) {
2040 tevent_req_error(req, sys_errno);
2041 goto post;
2044 switch (lbsda->u.sa.sa_family) {
2045 case AF_UNIX:
2046 if (lbsda->u.un.sun_path[0] != 0) {
2047 do_reuseaddr = true;
2048 do_bind = true;
2050 break;
2051 case AF_INET:
2052 if (lbsda->u.in.sin_port != 0) {
2053 do_reuseaddr = true;
2054 do_bind = true;
2056 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2057 do_bind = true;
2059 is_inet = true;
2060 break;
2061 #ifdef HAVE_IPV6
2062 case AF_INET6:
2063 if (lbsda->u.in6.sin6_port != 0) {
2064 do_reuseaddr = true;
2065 do_bind = true;
2067 if (memcmp(&in6addr_any,
2068 &lbsda->u.in6.sin6_addr,
2069 sizeof(in6addr_any)) != 0) {
2070 do_bind = true;
2072 is_inet = true;
2073 do_ipv6only = true;
2074 break;
2075 #endif
2076 default:
2077 tevent_req_error(req, EINVAL);
2078 goto post;
2081 if (!do_bind && is_inet) {
2082 sa_fam = rbsda->u.sa.sa_family;
2083 switch (sa_fam) {
2084 case AF_INET:
2085 do_ipv6only = false;
2086 break;
2087 #ifdef HAVE_IPV6
2088 case AF_INET6:
2089 do_ipv6only = true;
2090 break;
2091 #endif
2095 if (is_inet) {
2096 state->local = tsocket_address_create(state,
2097 &tsocket_address_bsd_ops,
2098 &lrbsda,
2099 struct tsocket_address_bsd,
2100 __location__ "bsd_connect");
2101 if (tevent_req_nomem(state->local, req)) {
2102 goto post;
2105 ZERO_STRUCTP(lrbsda);
2106 lrbsda->sa_socklen = sizeof(lrbsda->u.ss);
2107 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2108 lrbsda->u.sa.sa_len = lrbsda->sa_socklen;
2109 #endif
2112 state->fd = socket(sa_fam, SOCK_STREAM, 0);
2113 if (state->fd == -1) {
2114 tevent_req_error(req, errno);
2115 goto post;
2118 state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2119 if (state->fd == -1) {
2120 tevent_req_error(req, errno);
2121 goto post;
2124 #ifdef HAVE_IPV6
2125 if (do_ipv6only) {
2126 int val = 1;
2128 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2129 (const void *)&val, sizeof(val));
2130 if (ret == -1) {
2131 tevent_req_error(req, errno);
2132 goto post;
2135 #endif
2137 if (do_reuseaddr) {
2138 int val = 1;
2140 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2141 (const void *)&val, sizeof(val));
2142 if (ret == -1) {
2143 tevent_req_error(req, errno);
2144 goto post;
2148 if (do_bind) {
2149 ret = bind(state->fd, &lbsda->u.sa, lbsda->sa_socklen);
2150 if (ret == -1) {
2151 tevent_req_error(req, errno);
2152 goto post;
2156 if (rbsda->u.sa.sa_family != sa_fam) {
2157 tevent_req_error(req, EINVAL);
2158 goto post;
2161 ret = connect(state->fd, &rbsda->u.sa, rbsda->sa_socklen);
2162 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2163 if (retry) {
2164 /* retry later */
2165 goto async;
2167 if (tevent_req_error(req, err)) {
2168 goto post;
2171 if (!state->local) {
2172 tevent_req_done(req);
2173 goto post;
2176 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2177 if (ret == -1) {
2178 tevent_req_error(req, errno);
2179 goto post;
2182 tevent_req_done(req);
2183 goto post;
2185 async:
2186 state->fde = tevent_add_fd(ev, state,
2187 state->fd,
2188 TEVENT_FD_READ | TEVENT_FD_WRITE,
2189 tstream_bsd_connect_fde_handler,
2190 req);
2191 if (tevent_req_nomem(state->fde, req)) {
2192 goto post;
2195 return req;
2197 post:
2198 tevent_req_post(req, ev);
2199 return req;
2202 static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2203 struct tevent_fd *fde,
2204 uint16_t flags,
2205 void *private_data)
2207 struct tevent_req *req = talloc_get_type_abort(private_data,
2208 struct tevent_req);
2209 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2210 struct tstream_bsd_connect_state);
2211 struct tsocket_address_bsd *lrbsda = NULL;
2212 int ret;
2213 int error=0;
2214 socklen_t len = sizeof(error);
2215 int err;
2216 bool retry;
2218 ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2219 if (ret == 0) {
2220 if (error != 0) {
2221 errno = error;
2222 ret = -1;
2225 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2226 if (retry) {
2227 /* retry later */
2228 return;
2230 if (tevent_req_error(req, err)) {
2231 return;
2234 if (!state->local) {
2235 tevent_req_done(req);
2236 return;
2239 lrbsda = talloc_get_type_abort(state->local->private_data,
2240 struct tsocket_address_bsd);
2242 ret = getsockname(state->fd, &lrbsda->u.sa, &lrbsda->sa_socklen);
2243 if (ret == -1) {
2244 tevent_req_error(req, errno);
2245 return;
2248 tevent_req_done(req);
2251 static int tstream_bsd_connect_recv(struct tevent_req *req,
2252 int *perrno,
2253 TALLOC_CTX *mem_ctx,
2254 struct tstream_context **stream,
2255 struct tsocket_address **local,
2256 const char *location)
2258 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2259 struct tstream_bsd_connect_state);
2260 int ret;
2262 ret = tsocket_simple_int_recv(req, perrno);
2263 if (ret == 0) {
2264 ret = _tstream_bsd_existing_socket(mem_ctx,
2265 state->fd,
2266 stream,
2267 location);
2268 if (ret == -1) {
2269 *perrno = errno;
2270 goto done;
2272 TALLOC_FREE(state->fde);
2273 state->fd = -1;
2275 if (local) {
2276 *local = talloc_move(mem_ctx, &state->local);
2280 done:
2281 tevent_req_received(req);
2282 return ret;
2285 struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2286 struct tevent_context *ev,
2287 const struct tsocket_address *local,
2288 const struct tsocket_address *remote)
2290 struct tsocket_address_bsd *lbsda =
2291 talloc_get_type_abort(local->private_data,
2292 struct tsocket_address_bsd);
2293 struct tevent_req *req;
2294 int sys_errno = 0;
2296 switch (lbsda->u.sa.sa_family) {
2297 case AF_INET:
2298 break;
2299 #ifdef HAVE_IPV6
2300 case AF_INET6:
2301 break;
2302 #endif
2303 default:
2304 sys_errno = EINVAL;
2305 break;
2308 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2310 return req;
2313 int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2314 int *perrno,
2315 TALLOC_CTX *mem_ctx,
2316 struct tstream_context **stream,
2317 struct tsocket_address **local,
2318 const char *location)
2320 return tstream_bsd_connect_recv(req, perrno,
2321 mem_ctx, stream, local,
2322 location);
2325 struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2326 struct tevent_context *ev,
2327 const struct tsocket_address *local,
2328 const struct tsocket_address *remote)
2330 struct tsocket_address_bsd *lbsda =
2331 talloc_get_type_abort(local->private_data,
2332 struct tsocket_address_bsd);
2333 struct tevent_req *req;
2334 int sys_errno = 0;
2336 switch (lbsda->u.sa.sa_family) {
2337 case AF_UNIX:
2338 break;
2339 default:
2340 sys_errno = EINVAL;
2341 break;
2344 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2346 return req;
2349 int _tstream_unix_connect_recv(struct tevent_req *req,
2350 int *perrno,
2351 TALLOC_CTX *mem_ctx,
2352 struct tstream_context **stream,
2353 const char *location)
2355 return tstream_bsd_connect_recv(req, perrno,
2356 mem_ctx, stream, NULL,
2357 location);
2360 int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2361 struct tstream_context **_stream1,
2362 TALLOC_CTX *mem_ctx2,
2363 struct tstream_context **_stream2,
2364 const char *location)
2366 int ret;
2367 int fds[2];
2368 int fd1;
2369 int fd2;
2370 struct tstream_context *stream1 = NULL;
2371 struct tstream_context *stream2 = NULL;
2373 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2374 if (ret == -1) {
2375 return -1;
2377 fd1 = fds[0];
2378 fd2 = fds[1];
2380 fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2381 if (fd1 == -1) {
2382 int sys_errno = errno;
2383 close(fd2);
2384 errno = sys_errno;
2385 return -1;
2388 fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2389 if (fd2 == -1) {
2390 int sys_errno = errno;
2391 close(fd1);
2392 errno = sys_errno;
2393 return -1;
2396 ret = _tstream_bsd_existing_socket(mem_ctx1,
2397 fd1,
2398 &stream1,
2399 location);
2400 if (ret == -1) {
2401 int sys_errno = errno;
2402 close(fd1);
2403 close(fd2);
2404 errno = sys_errno;
2405 return -1;
2408 ret = _tstream_bsd_existing_socket(mem_ctx2,
2409 fd2,
2410 &stream2,
2411 location);
2412 if (ret == -1) {
2413 int sys_errno = errno;
2414 talloc_free(stream1);
2415 close(fd2);
2416 errno = sys_errno;
2417 return -1;
2420 *_stream1 = stream1;
2421 *_stream2 = stream2;
2422 return 0;