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
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/>.
25 #include "system/filesys.h"
26 #include "system/network.h"
28 #include "tsocket_internal.h"
30 static int tsocket_bsd_error_from_errno(int ret
,
48 if (sys_errno
== EINTR
) {
53 if (sys_errno
== EINPROGRESS
) {
58 if (sys_errno
== EAGAIN
) {
64 if (sys_errno
== EWOULDBLOCK
) {
73 static int tsocket_bsd_common_prepare_fd(int fd
, bool high_fd
)
86 /* first make a fd >= 3 */
96 for (i
=0; i
<num_fds
; i
++) {
105 /* fd should be nonblocking. */
108 #define FLAG_TO_SET O_NONBLOCK
111 #define FLAG_TO_SET O_NDELAY
113 #define FLAG_TO_SET FNDELAY
117 if ((flags
= fcntl(fd
, F_GETFL
)) == -1) {
121 flags
|= FLAG_TO_SET
;
122 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
128 /* fd should be closed on exec() */
130 result
= flags
= fcntl(fd
, F_GETFD
, 0);
133 result
= fcntl(fd
, F_SETFD
, flags
);
150 static ssize_t
tsocket_bsd_pending(int fd
)
156 ret
= ioctl(fd
, FIONREAD
, &value
);
162 /* this should not be reached */
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
);
190 static const struct tsocket_address_ops tsocket_address_bsd_ops
;
192 struct tsocket_address_bsd
{
195 struct sockaddr_in in
;
197 struct sockaddr_in6 in6
;
199 struct sockaddr_un un
;
200 struct sockaddr_storage ss
;
204 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
207 struct tsocket_address
**_addr
,
208 const char *location
)
210 struct tsocket_address
*addr
;
211 struct tsocket_address_bsd
*bsda
;
213 switch (sa
->sa_family
) {
215 if (sa_socklen
< sizeof(struct sockaddr_un
)) {
221 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
228 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
235 errno
= EAFNOSUPPORT
;
239 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
244 addr
= tsocket_address_create(mem_ctx
,
245 &tsocket_address_bsd_ops
,
247 struct tsocket_address_bsd
,
256 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
262 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
266 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
267 struct tsocket_address_bsd
);
275 switch (bsda
->u
.sa
.sa_family
) {
277 rlen
= sizeof(struct sockaddr_un
);
280 rlen
= sizeof(struct sockaddr_in
);
284 rlen
= sizeof(struct sockaddr_in6
);
288 errno
= EAFNOSUPPORT
;
292 if (sa_socklen
< rlen
) {
297 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
298 memset(sa
, 0, sa_socklen
);
299 sa_socklen
= sizeof(struct sockaddr_storage
);
302 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
306 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
310 struct tsocket_address
**_addr
,
311 const char *location
)
313 struct addrinfo hints
;
314 struct addrinfo
*result
= NULL
;
320 * we use SOCKET_STREAM here to get just one result
321 * back from getaddrinfo().
323 hints
.ai_socktype
= SOCK_STREAM
;
324 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
326 if (strcasecmp(fam
, "ip") == 0) {
327 hints
.ai_family
= AF_UNSPEC
;
335 } else if (strcasecmp(fam
, "ipv4") == 0) {
336 hints
.ai_family
= AF_INET
;
341 } else if (strcasecmp(fam
, "ipv6") == 0) {
342 hints
.ai_family
= AF_INET6
;
348 errno
= EAFNOSUPPORT
;
352 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
354 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
365 if (result
->ai_socktype
!= SOCK_STREAM
) {
371 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
379 freeaddrinfo(result
);
384 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
387 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
388 struct tsocket_address_bsd
);
389 char addr_str
[INET6_ADDRSTRLEN
+1];
397 switch (bsda
->u
.sa
.sa_family
) {
399 str
= inet_ntop(bsda
->u
.in
.sin_family
,
400 &bsda
->u
.in
.sin_addr
,
401 addr_str
, sizeof(addr_str
));
405 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
406 &bsda
->u
.in6
.sin6_addr
,
407 addr_str
, sizeof(addr_str
));
419 return talloc_strdup(mem_ctx
, str
);
422 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
424 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
425 struct tsocket_address_bsd
);
433 switch (bsda
->u
.sa
.sa_family
) {
435 port
= ntohs(bsda
->u
.in
.sin_port
);
439 port
= ntohs(bsda
->u
.in6
.sin6_port
);
450 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
453 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
454 struct tsocket_address_bsd
);
461 switch (bsda
->u
.sa
.sa_family
) {
463 bsda
->u
.in
.sin_port
= htons(port
);
467 bsda
->u
.in6
.sin6_port
= htons(port
);
478 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
480 struct tsocket_address
**_addr
,
481 const char *location
)
483 struct sockaddr_un un
;
491 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
492 errno
= ENAMETOOLONG
;
497 un
.sun_family
= AF_UNIX
;
498 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
500 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
501 (struct sockaddr
*)p
,
509 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
512 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
513 struct tsocket_address_bsd
);
521 switch (bsda
->u
.sa
.sa_family
) {
523 str
= bsda
->u
.un
.sun_path
;
530 return talloc_strdup(mem_ctx
, str
);
533 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
536 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
537 struct tsocket_address_bsd
);
540 const char *prefix
= NULL
;
543 switch (bsda
->u
.sa
.sa_family
) {
545 return talloc_asprintf(mem_ctx
, "unix:%s",
546 bsda
->u
.un
.sun_path
);
560 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
565 port
= tsocket_address_inet_port(addr
);
567 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
568 prefix
, addr_str
, port
);
569 talloc_free(addr_str
);
574 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
576 const char *location
)
578 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
579 struct tsocket_address_bsd
);
580 struct tsocket_address
*copy
;
583 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
595 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
597 .string
= tsocket_address_bsd_string
,
598 .copy
= tsocket_address_bsd_copy
,
605 struct tevent_fd
*fde
;
607 void *readable_private
;
608 void (*readable_handler
)(void *private_data
);
609 void *writeable_private
;
610 void (*writeable_handler
)(void *private_data
);
613 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
614 struct tevent_fd
*fde
,
618 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
621 if (flags
& TEVENT_FD_WRITE
) {
622 bsds
->writeable_handler(bsds
->writeable_private
);
625 if (flags
& TEVENT_FD_READ
) {
626 if (!bsds
->readable_handler
) {
627 TEVENT_FD_NOT_READABLE(bsds
->fde
);
630 bsds
->readable_handler(bsds
->readable_private
);
635 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
636 struct tevent_context
*ev
,
637 void (*handler
)(void *private_data
),
645 if (!bsds
->readable_handler
) {
648 bsds
->readable_handler
= NULL
;
649 bsds
->readable_private
= NULL
;
654 /* read and write must use the same tevent_context */
655 if (bsds
->event_ptr
!= ev
) {
656 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
660 bsds
->event_ptr
= NULL
;
661 TALLOC_FREE(bsds
->fde
);
664 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
665 TALLOC_FREE(bsds
->fde
);
667 bsds
->fde
= tevent_add_fd(ev
, bsds
,
668 bsds
->fd
, TEVENT_FD_READ
,
669 tdgram_bsd_fde_handler
,
676 /* cache the event context we're running on */
677 bsds
->event_ptr
= ev
;
678 } else if (!bsds
->readable_handler
) {
679 TEVENT_FD_READABLE(bsds
->fde
);
682 bsds
->readable_handler
= handler
;
683 bsds
->readable_private
= private_data
;
688 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
689 struct tevent_context
*ev
,
690 void (*handler
)(void *private_data
),
698 if (!bsds
->writeable_handler
) {
701 bsds
->writeable_handler
= NULL
;
702 bsds
->writeable_private
= NULL
;
703 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
708 /* read and write must use the same tevent_context */
709 if (bsds
->event_ptr
!= ev
) {
710 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
714 bsds
->event_ptr
= NULL
;
715 TALLOC_FREE(bsds
->fde
);
718 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
719 TALLOC_FREE(bsds
->fde
);
721 bsds
->fde
= tevent_add_fd(ev
, bsds
,
722 bsds
->fd
, TEVENT_FD_WRITE
,
723 tdgram_bsd_fde_handler
,
730 /* cache the event context we're running on */
731 bsds
->event_ptr
= ev
;
732 } else if (!bsds
->writeable_handler
) {
733 TEVENT_FD_WRITEABLE(bsds
->fde
);
736 bsds
->writeable_handler
= handler
;
737 bsds
->writeable_private
= private_data
;
742 struct tdgram_bsd_recvfrom_state
{
743 struct tdgram_context
*dgram
;
747 struct tsocket_address
*src
;
750 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
752 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
755 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
760 static void tdgram_bsd_recvfrom_handler(void *private_data
);
762 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
763 struct tevent_context
*ev
,
764 struct tdgram_context
*dgram
)
766 struct tevent_req
*req
;
767 struct tdgram_bsd_recvfrom_state
*state
;
768 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
771 req
= tevent_req_create(mem_ctx
, &state
,
772 struct tdgram_bsd_recvfrom_state
);
777 state
->dgram
= dgram
;
782 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
784 if (bsds
->fd
== -1) {
785 tevent_req_error(req
, ENOTCONN
);
790 * this is a fast path, not waiting for the
791 * socket to become explicit readable gains
792 * about 10%-20% performance in benchmark tests.
794 tdgram_bsd_recvfrom_handler(req
);
795 if (!tevent_req_is_in_progress(req
)) {
799 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
800 tdgram_bsd_recvfrom_handler
,
803 tevent_req_error(req
, errno
);
810 tevent_req_post(req
, ev
);
814 static void tdgram_bsd_recvfrom_handler(void *private_data
)
816 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
818 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
819 struct tdgram_bsd_recvfrom_state
);
820 struct tdgram_context
*dgram
= state
->dgram
;
821 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
822 struct tsocket_address_bsd
*bsda
;
824 struct sockaddr
*sa
= NULL
;
825 socklen_t sa_socklen
= 0;
829 ret
= tsocket_bsd_pending(bsds
->fd
);
834 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
839 if (tevent_req_error(req
, err
)) {
843 state
->buf
= talloc_array(state
, uint8_t, ret
);
844 if (tevent_req_nomem(state
->buf
, req
)) {
849 state
->src
= tsocket_address_create(state
,
850 &tsocket_address_bsd_ops
,
852 struct tsocket_address_bsd
,
853 __location__
"bsd_recvfrom");
854 if (tevent_req_nomem(state
->src
, req
)) {
861 sa_socklen
= sizeof(bsda
->u
.ss
);
863 * for unix sockets we can't use the size of sockaddr_storage
864 * we would get EINVAL
866 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
867 sa_socklen
= sizeof(bsda
->u
.un
);
870 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, &sa_socklen
);
871 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
876 if (tevent_req_error(req
, err
)) {
881 * some systems too much bytes in tsocket_bsd_pending()
882 * the return value includes some IP/UDP header bytes
885 talloc_realloc(state
, state
->buf
, uint8_t, ret
);
887 tevent_req_done(req
);
890 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
894 struct tsocket_address
**src
)
896 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
897 struct tdgram_bsd_recvfrom_state
);
900 ret
= tsocket_simple_int_recv(req
, perrno
);
902 *buf
= talloc_move(mem_ctx
, &state
->buf
);
905 *src
= talloc_move(mem_ctx
, &state
->src
);
909 tevent_req_received(req
);
913 struct tdgram_bsd_sendto_state
{
914 struct tdgram_context
*dgram
;
918 const struct tsocket_address
*dst
;
923 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
925 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
928 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
933 static void tdgram_bsd_sendto_handler(void *private_data
);
935 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
936 struct tevent_context
*ev
,
937 struct tdgram_context
*dgram
,
940 const struct tsocket_address
*dst
)
942 struct tevent_req
*req
;
943 struct tdgram_bsd_sendto_state
*state
;
944 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
947 req
= tevent_req_create(mem_ctx
, &state
,
948 struct tdgram_bsd_sendto_state
);
953 state
->dgram
= dgram
;
959 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
961 if (bsds
->fd
== -1) {
962 tevent_req_error(req
, ENOTCONN
);
967 * this is a fast path, not waiting for the
968 * socket to become explicit writeable gains
969 * about 10%-20% performance in benchmark tests.
971 tdgram_bsd_sendto_handler(req
);
972 if (!tevent_req_is_in_progress(req
)) {
976 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
977 tdgram_bsd_sendto_handler
,
980 tevent_req_error(req
, errno
);
987 tevent_req_post(req
, ev
);
991 static void tdgram_bsd_sendto_handler(void *private_data
)
993 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
995 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
996 struct tdgram_bsd_sendto_state
);
997 struct tdgram_context
*dgram
= state
->dgram
;
998 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
999 struct sockaddr
*sa
= NULL
;
1000 socklen_t sa_socklen
= 0;
1006 struct tsocket_address_bsd
*bsda
=
1007 talloc_get_type(state
->dst
->private_data
,
1008 struct tsocket_address_bsd
);
1011 sa_socklen
= sizeof(bsda
->u
.ss
);
1013 * for unix sockets we can't use the size of sockaddr_storage
1014 * we would get EINVAL
1016 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
1017 sa_socklen
= sizeof(bsda
->u
.un
);
1021 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1022 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1027 if (tevent_req_error(req
, err
)) {
1033 tevent_req_done(req
);
1036 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1038 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1039 struct tdgram_bsd_sendto_state
);
1042 ret
= tsocket_simple_int_recv(req
, perrno
);
1047 tevent_req_received(req
);
1051 struct tdgram_bsd_disconnect_state
{
1055 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1056 struct tevent_context
*ev
,
1057 struct tdgram_context
*dgram
)
1059 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1060 struct tevent_req
*req
;
1061 struct tdgram_bsd_disconnect_state
*state
;
1066 req
= tevent_req_create(mem_ctx
, &state
,
1067 struct tdgram_bsd_disconnect_state
);
1072 if (bsds
->fd
== -1) {
1073 tevent_req_error(req
, ENOTCONN
);
1077 ret
= close(bsds
->fd
);
1079 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1080 if (tevent_req_error(req
, err
)) {
1084 tevent_req_done(req
);
1086 tevent_req_post(req
, ev
);
1090 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1095 ret
= tsocket_simple_int_recv(req
, perrno
);
1097 tevent_req_received(req
);
1101 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1104 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1105 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1107 .sendto_send
= tdgram_bsd_sendto_send
,
1108 .sendto_recv
= tdgram_bsd_sendto_recv
,
1110 .disconnect_send
= tdgram_bsd_disconnect_send
,
1111 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1114 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1116 TALLOC_FREE(bsds
->fde
);
1117 if (bsds
->fd
!= -1) {
1124 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1125 const struct tsocket_address
*remote
,
1127 TALLOC_CTX
*mem_ctx
,
1128 struct tdgram_context
**_dgram
,
1129 const char *location
)
1131 struct tsocket_address_bsd
*lbsda
=
1132 talloc_get_type_abort(local
->private_data
,
1133 struct tsocket_address_bsd
);
1134 struct tsocket_address_bsd
*rbsda
= NULL
;
1135 struct tdgram_context
*dgram
;
1136 struct tdgram_bsd
*bsds
;
1139 bool do_bind
= false;
1140 bool do_reuseaddr
= false;
1141 bool do_ipv6only
= false;
1142 bool is_inet
= false;
1143 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1144 socklen_t sa_socklen
= sizeof(lbsda
->u
.ss
);
1147 rbsda
= talloc_get_type_abort(remote
->private_data
,
1148 struct tsocket_address_bsd
);
1151 switch (lbsda
->u
.sa
.sa_family
) {
1157 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1158 do_reuseaddr
= true;
1162 * for unix sockets we can't use the size of sockaddr_storage
1163 * we would get EINVAL
1165 sa_socklen
= sizeof(lbsda
->u
.un
);
1168 if (lbsda
->u
.in
.sin_port
!= 0) {
1169 do_reuseaddr
= true;
1172 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1176 sa_socklen
= sizeof(rbsda
->u
.in
);
1180 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1181 do_reuseaddr
= true;
1184 if (memcmp(&in6addr_any
,
1185 &lbsda
->u
.in6
.sin6_addr
,
1186 sizeof(in6addr_any
)) != 0) {
1190 sa_socklen
= sizeof(rbsda
->u
.in6
);
1199 if (!do_bind
&& is_inet
&& rbsda
) {
1200 sa_fam
= rbsda
->u
.sa
.sa_family
;
1203 sa_socklen
= sizeof(rbsda
->u
.in
);
1204 do_ipv6only
= false;
1208 sa_socklen
= sizeof(rbsda
->u
.in6
);
1215 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1220 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1225 dgram
= tdgram_context_create(mem_ctx
,
1231 int saved_errno
= errno
;
1233 errno
= saved_errno
;
1238 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1244 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1245 (const void *)&val
, sizeof(val
));
1247 int saved_errno
= errno
;
1249 errno
= saved_errno
;
1258 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1259 (const void *)&val
, sizeof(val
));
1261 int saved_errno
= errno
;
1263 errno
= saved_errno
;
1271 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1272 (const void *)&val
, sizeof(val
));
1274 int saved_errno
= errno
;
1276 errno
= saved_errno
;
1282 ret
= bind(fd
, &lbsda
->u
.sa
, sa_socklen
);
1284 int saved_errno
= errno
;
1286 errno
= saved_errno
;
1292 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1298 ret
= connect(fd
, &rbsda
->u
.sa
, sa_socklen
);
1300 int saved_errno
= errno
;
1302 errno
= saved_errno
;
1311 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1312 const struct tsocket_address
*remote
,
1313 TALLOC_CTX
*mem_ctx
,
1314 struct tdgram_context
**dgram
,
1315 const char *location
)
1317 struct tsocket_address_bsd
*lbsda
=
1318 talloc_get_type_abort(local
->private_data
,
1319 struct tsocket_address_bsd
);
1322 switch (lbsda
->u
.sa
.sa_family
) {
1334 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1335 mem_ctx
, dgram
, location
);
1340 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1341 const struct tsocket_address
*remote
,
1342 TALLOC_CTX
*mem_ctx
,
1343 struct tdgram_context
**dgram
,
1344 const char *location
)
1346 struct tsocket_address_bsd
*lbsda
=
1347 talloc_get_type_abort(local
->private_data
,
1348 struct tsocket_address_bsd
);
1351 switch (lbsda
->u
.sa
.sa_family
) {
1359 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1360 mem_ctx
, dgram
, location
);
1365 struct tstream_bsd
{
1369 struct tevent_fd
*fde
;
1371 void *readable_private
;
1372 void (*readable_handler
)(void *private_data
);
1373 void *writeable_private
;
1374 void (*writeable_handler
)(void *private_data
);
1377 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1378 struct tevent_fd
*fde
,
1382 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1383 struct tstream_bsd
);
1385 if (flags
& TEVENT_FD_WRITE
) {
1386 bsds
->writeable_handler(bsds
->writeable_private
);
1389 if (flags
& TEVENT_FD_READ
) {
1390 if (!bsds
->readable_handler
) {
1391 if (bsds
->writeable_handler
) {
1392 bsds
->writeable_handler(bsds
->writeable_private
);
1395 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1398 bsds
->readable_handler(bsds
->readable_private
);
1403 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1404 struct tevent_context
*ev
,
1405 void (*handler
)(void *private_data
),
1413 if (!bsds
->readable_handler
) {
1416 bsds
->readable_handler
= NULL
;
1417 bsds
->readable_private
= NULL
;
1422 /* read and write must use the same tevent_context */
1423 if (bsds
->event_ptr
!= ev
) {
1424 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1428 bsds
->event_ptr
= NULL
;
1429 TALLOC_FREE(bsds
->fde
);
1432 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1433 TALLOC_FREE(bsds
->fde
);
1435 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1436 bsds
->fd
, TEVENT_FD_READ
,
1437 tstream_bsd_fde_handler
,
1444 /* cache the event context we're running on */
1445 bsds
->event_ptr
= ev
;
1446 } else if (!bsds
->readable_handler
) {
1447 TEVENT_FD_READABLE(bsds
->fde
);
1450 bsds
->readable_handler
= handler
;
1451 bsds
->readable_private
= private_data
;
1456 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1457 struct tevent_context
*ev
,
1458 void (*handler
)(void *private_data
),
1466 if (!bsds
->writeable_handler
) {
1469 bsds
->writeable_handler
= NULL
;
1470 bsds
->writeable_private
= NULL
;
1471 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1476 /* read and write must use the same tevent_context */
1477 if (bsds
->event_ptr
!= ev
) {
1478 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1482 bsds
->event_ptr
= NULL
;
1483 TALLOC_FREE(bsds
->fde
);
1486 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1487 TALLOC_FREE(bsds
->fde
);
1489 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1491 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1492 tstream_bsd_fde_handler
,
1499 /* cache the event context we're running on */
1500 bsds
->event_ptr
= ev
;
1501 } else if (!bsds
->writeable_handler
) {
1502 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1503 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1504 tevent_fd_set_flags(bsds
->fde
, flags
);
1507 bsds
->writeable_handler
= handler
;
1508 bsds
->writeable_private
= private_data
;
1513 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1515 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1516 struct tstream_bsd
);
1519 if (bsds
->fd
== -1) {
1524 ret
= tsocket_bsd_pending(bsds
->fd
);
1529 struct tstream_bsd_readv_state
{
1530 struct tstream_context
*stream
;
1532 struct iovec
*vector
;
1538 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1540 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1541 struct tstream_bsd
);
1543 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1548 static void tstream_bsd_readv_handler(void *private_data
);
1550 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1551 struct tevent_context
*ev
,
1552 struct tstream_context
*stream
,
1553 struct iovec
*vector
,
1556 struct tevent_req
*req
;
1557 struct tstream_bsd_readv_state
*state
;
1558 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1561 req
= tevent_req_create(mem_ctx
, &state
,
1562 struct tstream_bsd_readv_state
);
1567 state
->stream
= stream
;
1568 /* we make a copy of the vector so that we can modify it */
1569 state
->vector
= talloc_array(state
, struct iovec
, count
);
1570 if (tevent_req_nomem(state
->vector
, req
)) {
1573 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1574 state
->count
= count
;
1577 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1579 if (bsds
->fd
== -1) {
1580 tevent_req_error(req
, ENOTCONN
);
1585 * this is a fast path, not waiting for the
1586 * socket to become explicit readable gains
1587 * about 10%-20% performance in benchmark tests.
1589 tstream_bsd_readv_handler(req
);
1590 if (!tevent_req_is_in_progress(req
)) {
1594 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1595 tstream_bsd_readv_handler
,
1598 tevent_req_error(req
, errno
);
1605 tevent_req_post(req
, ev
);
1609 static void tstream_bsd_readv_handler(void *private_data
)
1611 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1613 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1614 struct tstream_bsd_readv_state
);
1615 struct tstream_context
*stream
= state
->stream
;
1616 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1621 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1623 /* propagate end of file */
1624 tevent_req_error(req
, EPIPE
);
1627 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1632 if (tevent_req_error(req
, err
)) {
1639 if (ret
< state
->vector
[0].iov_len
) {
1641 base
= (uint8_t *)state
->vector
[0].iov_base
;
1643 state
->vector
[0].iov_base
= base
;
1644 state
->vector
[0].iov_len
-= ret
;
1647 ret
-= state
->vector
[0].iov_len
;
1653 * there're maybe some empty vectors at the end
1654 * which we need to skip, otherwise we would get
1655 * ret == 0 from the readv() call and return EPIPE
1657 while (state
->count
> 0) {
1658 if (state
->vector
[0].iov_len
> 0) {
1665 if (state
->count
> 0) {
1666 /* we have more to read */
1670 tevent_req_done(req
);
1673 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1676 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1677 struct tstream_bsd_readv_state
);
1680 ret
= tsocket_simple_int_recv(req
, perrno
);
1685 tevent_req_received(req
);
1689 struct tstream_bsd_writev_state
{
1690 struct tstream_context
*stream
;
1692 struct iovec
*vector
;
1698 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1700 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1701 struct tstream_bsd
);
1703 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1708 static void tstream_bsd_writev_handler(void *private_data
);
1710 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1711 struct tevent_context
*ev
,
1712 struct tstream_context
*stream
,
1713 const struct iovec
*vector
,
1716 struct tevent_req
*req
;
1717 struct tstream_bsd_writev_state
*state
;
1718 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1721 req
= tevent_req_create(mem_ctx
, &state
,
1722 struct tstream_bsd_writev_state
);
1727 state
->stream
= stream
;
1728 /* we make a copy of the vector so that we can modify it */
1729 state
->vector
= talloc_array(state
, struct iovec
, count
);
1730 if (tevent_req_nomem(state
->vector
, req
)) {
1733 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1734 state
->count
= count
;
1737 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1739 if (bsds
->fd
== -1) {
1740 tevent_req_error(req
, ENOTCONN
);
1745 * this is a fast path, not waiting for the
1746 * socket to become explicit writeable gains
1747 * about 10%-20% performance in benchmark tests.
1749 tstream_bsd_writev_handler(req
);
1750 if (!tevent_req_is_in_progress(req
)) {
1754 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1755 tstream_bsd_writev_handler
,
1758 tevent_req_error(req
, errno
);
1765 tevent_req_post(req
, ev
);
1769 static void tstream_bsd_writev_handler(void *private_data
)
1771 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1773 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1774 struct tstream_bsd_writev_state
);
1775 struct tstream_context
*stream
= state
->stream
;
1776 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1781 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1783 /* propagate end of file */
1784 tevent_req_error(req
, EPIPE
);
1787 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1792 if (tevent_req_error(req
, err
)) {
1799 if (ret
< state
->vector
[0].iov_len
) {
1801 base
= (uint8_t *)state
->vector
[0].iov_base
;
1803 state
->vector
[0].iov_base
= base
;
1804 state
->vector
[0].iov_len
-= ret
;
1807 ret
-= state
->vector
[0].iov_len
;
1813 * there're maybe some empty vectors at the end
1814 * which we need to skip, otherwise we would get
1815 * ret == 0 from the writev() call and return EPIPE
1817 while (state
->count
> 0) {
1818 if (state
->vector
[0].iov_len
> 0) {
1825 if (state
->count
> 0) {
1826 /* we have more to read */
1830 tevent_req_done(req
);
1833 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1835 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1836 struct tstream_bsd_writev_state
);
1839 ret
= tsocket_simple_int_recv(req
, perrno
);
1844 tevent_req_received(req
);
1848 struct tstream_bsd_disconnect_state
{
1852 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1853 struct tevent_context
*ev
,
1854 struct tstream_context
*stream
)
1856 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1857 struct tevent_req
*req
;
1858 struct tstream_bsd_disconnect_state
*state
;
1863 req
= tevent_req_create(mem_ctx
, &state
,
1864 struct tstream_bsd_disconnect_state
);
1869 if (bsds
->fd
== -1) {
1870 tevent_req_error(req
, ENOTCONN
);
1874 ret
= close(bsds
->fd
);
1876 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1877 if (tevent_req_error(req
, err
)) {
1881 tevent_req_done(req
);
1883 tevent_req_post(req
, ev
);
1887 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1892 ret
= tsocket_simple_int_recv(req
, perrno
);
1894 tevent_req_received(req
);
1898 static const struct tstream_context_ops tstream_bsd_ops
= {
1901 .pending_bytes
= tstream_bsd_pending_bytes
,
1903 .readv_send
= tstream_bsd_readv_send
,
1904 .readv_recv
= tstream_bsd_readv_recv
,
1906 .writev_send
= tstream_bsd_writev_send
,
1907 .writev_recv
= tstream_bsd_writev_recv
,
1909 .disconnect_send
= tstream_bsd_disconnect_send
,
1910 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1913 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1915 TALLOC_FREE(bsds
->fde
);
1916 if (bsds
->fd
!= -1) {
1923 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1925 struct tstream_context
**_stream
,
1926 const char *location
)
1928 struct tstream_context
*stream
;
1929 struct tstream_bsd
*bsds
;
1931 stream
= tstream_context_create(mem_ctx
,
1941 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1947 struct tstream_bsd_connect_state
{
1949 struct tevent_fd
*fde
;
1950 struct tstream_conext
*stream
;
1953 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1955 TALLOC_FREE(state
->fde
);
1956 if (state
->fd
!= -1) {
1964 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1965 struct tevent_fd
*fde
,
1967 void *private_data
);
1969 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1970 struct tevent_context
*ev
,
1972 const struct tsocket_address
*local
,
1973 const struct tsocket_address
*remote
)
1975 struct tevent_req
*req
;
1976 struct tstream_bsd_connect_state
*state
;
1977 struct tsocket_address_bsd
*lbsda
=
1978 talloc_get_type_abort(local
->private_data
,
1979 struct tsocket_address_bsd
);
1980 struct tsocket_address_bsd
*rbsda
=
1981 talloc_get_type_abort(remote
->private_data
,
1982 struct tsocket_address_bsd
);
1986 bool do_bind
= false;
1987 bool do_reuseaddr
= false;
1988 bool do_ipv6only
= false;
1989 bool is_inet
= false;
1990 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1991 socklen_t sa_socklen
= sizeof(rbsda
->u
.ss
);
1993 req
= tevent_req_create(mem_ctx
, &state
,
1994 struct tstream_bsd_connect_state
);
2001 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2003 /* give the wrappers a chance to report an error */
2004 if (sys_errno
!= 0) {
2005 tevent_req_error(req
, sys_errno
);
2009 switch (lbsda
->u
.sa
.sa_family
) {
2011 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2012 do_reuseaddr
= true;
2016 * for unix sockets we can't use the size of sockaddr_storage
2017 * we would get EINVAL
2019 sa_socklen
= sizeof(rbsda
->u
.un
);
2022 if (lbsda
->u
.in
.sin_port
!= 0) {
2023 do_reuseaddr
= true;
2026 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2030 sa_socklen
= sizeof(rbsda
->u
.in
);
2034 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2035 do_reuseaddr
= true;
2038 if (memcmp(&in6addr_any
,
2039 &lbsda
->u
.in6
.sin6_addr
,
2040 sizeof(in6addr_any
)) != 0) {
2044 sa_socklen
= sizeof(rbsda
->u
.in6
);
2049 tevent_req_error(req
, EINVAL
);
2053 if (!do_bind
&& is_inet
) {
2054 sa_fam
= rbsda
->u
.sa
.sa_family
;
2057 sa_socklen
= sizeof(rbsda
->u
.in
);
2058 do_ipv6only
= false;
2062 sa_socklen
= sizeof(rbsda
->u
.in6
);
2069 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2070 if (state
->fd
== -1) {
2071 tevent_req_error(req
, errno
);
2075 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2076 if (state
->fd
== -1) {
2077 tevent_req_error(req
, errno
);
2085 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2086 (const void *)&val
, sizeof(val
));
2088 tevent_req_error(req
, errno
);
2097 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2098 (const void *)&val
, sizeof(val
));
2100 tevent_req_error(req
, errno
);
2106 ret
= bind(state
->fd
, &lbsda
->u
.sa
, sa_socklen
);
2108 tevent_req_error(req
, errno
);
2113 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2114 tevent_req_error(req
, EINVAL
);
2118 ret
= connect(state
->fd
, &rbsda
->u
.sa
, sa_socklen
);
2119 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2124 if (tevent_req_error(req
, err
)) {
2128 tevent_req_done(req
);
2132 state
->fde
= tevent_add_fd(ev
, state
,
2134 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2135 tstream_bsd_connect_fde_handler
,
2137 if (tevent_req_nomem(state
->fde
, req
)) {
2144 tevent_req_post(req
, ev
);
2148 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2149 struct tevent_fd
*fde
,
2153 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2155 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2156 struct tstream_bsd_connect_state
);
2159 socklen_t len
= sizeof(error
);
2163 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2170 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2175 if (tevent_req_error(req
, err
)) {
2179 tevent_req_done(req
);
2182 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2184 TALLOC_CTX
*mem_ctx
,
2185 struct tstream_context
**stream
,
2186 const char *location
)
2188 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2189 struct tstream_bsd_connect_state
);
2192 ret
= tsocket_simple_int_recv(req
, perrno
);
2194 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2202 TALLOC_FREE(state
->fde
);
2207 tevent_req_received(req
);
2211 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2212 struct tevent_context
*ev
,
2213 const struct tsocket_address
*local
,
2214 const struct tsocket_address
*remote
)
2216 struct tsocket_address_bsd
*lbsda
=
2217 talloc_get_type_abort(local
->private_data
,
2218 struct tsocket_address_bsd
);
2219 struct tevent_req
*req
;
2222 switch (lbsda
->u
.sa
.sa_family
) {
2234 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2239 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2241 TALLOC_CTX
*mem_ctx
,
2242 struct tstream_context
**stream
,
2243 const char *location
)
2245 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2248 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2249 struct tevent_context
*ev
,
2250 const struct tsocket_address
*local
,
2251 const struct tsocket_address
*remote
)
2253 struct tsocket_address_bsd
*lbsda
=
2254 talloc_get_type_abort(local
->private_data
,
2255 struct tsocket_address_bsd
);
2256 struct tevent_req
*req
;
2259 switch (lbsda
->u
.sa
.sa_family
) {
2267 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2272 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2274 TALLOC_CTX
*mem_ctx
,
2275 struct tstream_context
**stream
,
2276 const char *location
)
2278 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2281 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2282 struct tstream_context
**_stream1
,
2283 TALLOC_CTX
*mem_ctx2
,
2284 struct tstream_context
**_stream2
,
2285 const char *location
)
2291 struct tstream_context
*stream1
= NULL
;
2292 struct tstream_context
*stream2
= NULL
;
2294 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2301 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2303 int sys_errno
= errno
;
2309 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2311 int sys_errno
= errno
;
2317 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2322 int sys_errno
= errno
;
2329 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2334 int sys_errno
= errno
;
2335 talloc_free(stream1
);
2341 *_stream1
= stream1
;
2342 *_stream2
= stream2
;