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 if (sa_socklen
< sizeof(sa
->sa_family
)) {
218 switch (sa
->sa_family
) {
220 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
221 sa_socklen
= sizeof(struct sockaddr_un
);
225 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
229 sa_socklen
= sizeof(struct sockaddr_in
);
233 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
237 sa_socklen
= sizeof(struct sockaddr_in6
);
241 errno
= EAFNOSUPPORT
;
245 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
250 addr
= tsocket_address_create(mem_ctx
,
251 &tsocket_address_bsd_ops
,
253 struct tsocket_address_bsd
,
262 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
268 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
272 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
273 struct tsocket_address_bsd
);
281 switch (bsda
->u
.sa
.sa_family
) {
283 rlen
= sizeof(struct sockaddr_un
);
286 rlen
= sizeof(struct sockaddr_in
);
290 rlen
= sizeof(struct sockaddr_in6
);
294 errno
= EAFNOSUPPORT
;
298 if (sa_socklen
< rlen
) {
303 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
304 memset(sa
, 0, sa_socklen
);
305 sa_socklen
= sizeof(struct sockaddr_storage
);
308 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
312 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
316 struct tsocket_address
**_addr
,
317 const char *location
)
319 struct addrinfo hints
;
320 struct addrinfo
*result
= NULL
;
326 * we use SOCKET_STREAM here to get just one result
327 * back from getaddrinfo().
329 hints
.ai_socktype
= SOCK_STREAM
;
330 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
332 if (strcasecmp(fam
, "ip") == 0) {
333 hints
.ai_family
= AF_UNSPEC
;
341 } else if (strcasecmp(fam
, "ipv4") == 0) {
342 hints
.ai_family
= AF_INET
;
347 } else if (strcasecmp(fam
, "ipv6") == 0) {
348 hints
.ai_family
= AF_INET6
;
354 errno
= EAFNOSUPPORT
;
358 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
360 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
371 if (result
->ai_socktype
!= SOCK_STREAM
) {
377 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
385 freeaddrinfo(result
);
390 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
393 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
394 struct tsocket_address_bsd
);
395 char addr_str
[INET6_ADDRSTRLEN
+1];
403 switch (bsda
->u
.sa
.sa_family
) {
405 str
= inet_ntop(bsda
->u
.in
.sin_family
,
406 &bsda
->u
.in
.sin_addr
,
407 addr_str
, sizeof(addr_str
));
411 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
412 &bsda
->u
.in6
.sin6_addr
,
413 addr_str
, sizeof(addr_str
));
425 return talloc_strdup(mem_ctx
, str
);
428 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
430 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
431 struct tsocket_address_bsd
);
439 switch (bsda
->u
.sa
.sa_family
) {
441 port
= ntohs(bsda
->u
.in
.sin_port
);
445 port
= ntohs(bsda
->u
.in6
.sin6_port
);
456 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
459 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
460 struct tsocket_address_bsd
);
467 switch (bsda
->u
.sa
.sa_family
) {
469 bsda
->u
.in
.sin_port
= htons(port
);
473 bsda
->u
.in6
.sin6_port
= htons(port
);
484 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
486 struct tsocket_address
**_addr
,
487 const char *location
)
489 struct sockaddr_un un
;
497 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
498 errno
= ENAMETOOLONG
;
503 un
.sun_family
= AF_UNIX
;
504 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
506 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
507 (struct sockaddr
*)p
,
515 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
518 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
519 struct tsocket_address_bsd
);
527 switch (bsda
->u
.sa
.sa_family
) {
529 str
= bsda
->u
.un
.sun_path
;
536 return talloc_strdup(mem_ctx
, str
);
539 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
542 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
543 struct tsocket_address_bsd
);
546 const char *prefix
= NULL
;
549 switch (bsda
->u
.sa
.sa_family
) {
551 return talloc_asprintf(mem_ctx
, "unix:%s",
552 bsda
->u
.un
.sun_path
);
566 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
571 port
= tsocket_address_inet_port(addr
);
573 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
574 prefix
, addr_str
, port
);
575 talloc_free(addr_str
);
580 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
582 const char *location
)
584 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
585 struct tsocket_address_bsd
);
586 struct tsocket_address
*copy
;
589 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
601 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
603 .string
= tsocket_address_bsd_string
,
604 .copy
= tsocket_address_bsd_copy
,
611 struct tevent_fd
*fde
;
613 void *readable_private
;
614 void (*readable_handler
)(void *private_data
);
615 void *writeable_private
;
616 void (*writeable_handler
)(void *private_data
);
619 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
620 struct tevent_fd
*fde
,
624 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
627 if (flags
& TEVENT_FD_WRITE
) {
628 bsds
->writeable_handler(bsds
->writeable_private
);
631 if (flags
& TEVENT_FD_READ
) {
632 if (!bsds
->readable_handler
) {
633 TEVENT_FD_NOT_READABLE(bsds
->fde
);
636 bsds
->readable_handler(bsds
->readable_private
);
641 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
642 struct tevent_context
*ev
,
643 void (*handler
)(void *private_data
),
651 if (!bsds
->readable_handler
) {
654 bsds
->readable_handler
= NULL
;
655 bsds
->readable_private
= NULL
;
660 /* read and write must use the same tevent_context */
661 if (bsds
->event_ptr
!= ev
) {
662 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
666 bsds
->event_ptr
= NULL
;
667 TALLOC_FREE(bsds
->fde
);
670 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
671 TALLOC_FREE(bsds
->fde
);
673 bsds
->fde
= tevent_add_fd(ev
, bsds
,
674 bsds
->fd
, TEVENT_FD_READ
,
675 tdgram_bsd_fde_handler
,
682 /* cache the event context we're running on */
683 bsds
->event_ptr
= ev
;
684 } else if (!bsds
->readable_handler
) {
685 TEVENT_FD_READABLE(bsds
->fde
);
688 bsds
->readable_handler
= handler
;
689 bsds
->readable_private
= private_data
;
694 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
695 struct tevent_context
*ev
,
696 void (*handler
)(void *private_data
),
704 if (!bsds
->writeable_handler
) {
707 bsds
->writeable_handler
= NULL
;
708 bsds
->writeable_private
= NULL
;
709 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
714 /* read and write must use the same tevent_context */
715 if (bsds
->event_ptr
!= ev
) {
716 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
720 bsds
->event_ptr
= NULL
;
721 TALLOC_FREE(bsds
->fde
);
724 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
725 TALLOC_FREE(bsds
->fde
);
727 bsds
->fde
= tevent_add_fd(ev
, bsds
,
728 bsds
->fd
, TEVENT_FD_WRITE
,
729 tdgram_bsd_fde_handler
,
736 /* cache the event context we're running on */
737 bsds
->event_ptr
= ev
;
738 } else if (!bsds
->writeable_handler
) {
739 TEVENT_FD_WRITEABLE(bsds
->fde
);
742 bsds
->writeable_handler
= handler
;
743 bsds
->writeable_private
= private_data
;
748 struct tdgram_bsd_recvfrom_state
{
749 struct tdgram_context
*dgram
;
753 struct tsocket_address
*src
;
756 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
758 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
761 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
766 static void tdgram_bsd_recvfrom_handler(void *private_data
);
768 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
769 struct tevent_context
*ev
,
770 struct tdgram_context
*dgram
)
772 struct tevent_req
*req
;
773 struct tdgram_bsd_recvfrom_state
*state
;
774 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
777 req
= tevent_req_create(mem_ctx
, &state
,
778 struct tdgram_bsd_recvfrom_state
);
783 state
->dgram
= dgram
;
788 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
790 if (bsds
->fd
== -1) {
791 tevent_req_error(req
, ENOTCONN
);
796 * this is a fast path, not waiting for the
797 * socket to become explicit readable gains
798 * about 10%-20% performance in benchmark tests.
800 tdgram_bsd_recvfrom_handler(req
);
801 if (!tevent_req_is_in_progress(req
)) {
805 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
806 tdgram_bsd_recvfrom_handler
,
809 tevent_req_error(req
, errno
);
816 tevent_req_post(req
, ev
);
820 static void tdgram_bsd_recvfrom_handler(void *private_data
)
822 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
824 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
825 struct tdgram_bsd_recvfrom_state
);
826 struct tdgram_context
*dgram
= state
->dgram
;
827 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
828 struct tsocket_address_bsd
*bsda
;
830 struct sockaddr
*sa
= NULL
;
831 socklen_t sa_socklen
= 0;
835 ret
= tsocket_bsd_pending(bsds
->fd
);
840 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
845 if (tevent_req_error(req
, err
)) {
849 state
->buf
= talloc_array(state
, uint8_t, ret
);
850 if (tevent_req_nomem(state
->buf
, req
)) {
855 state
->src
= tsocket_address_create(state
,
856 &tsocket_address_bsd_ops
,
858 struct tsocket_address_bsd
,
859 __location__
"bsd_recvfrom");
860 if (tevent_req_nomem(state
->src
, req
)) {
867 sa_socklen
= sizeof(bsda
->u
.ss
);
869 * for unix sockets we can't use the size of sockaddr_storage
870 * we would get EINVAL
872 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
873 sa_socklen
= sizeof(bsda
->u
.un
);
876 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, &sa_socklen
);
877 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
882 if (tevent_req_error(req
, err
)) {
886 if (ret
!= state
->len
) {
887 tevent_req_error(req
, EIO
);
891 tevent_req_done(req
);
894 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
898 struct tsocket_address
**src
)
900 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
901 struct tdgram_bsd_recvfrom_state
);
904 ret
= tsocket_simple_int_recv(req
, perrno
);
906 *buf
= talloc_move(mem_ctx
, &state
->buf
);
909 *src
= talloc_move(mem_ctx
, &state
->src
);
913 tevent_req_received(req
);
917 struct tdgram_bsd_sendto_state
{
918 struct tdgram_context
*dgram
;
922 const struct tsocket_address
*dst
;
927 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
929 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
932 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
937 static void tdgram_bsd_sendto_handler(void *private_data
);
939 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
940 struct tevent_context
*ev
,
941 struct tdgram_context
*dgram
,
944 const struct tsocket_address
*dst
)
946 struct tevent_req
*req
;
947 struct tdgram_bsd_sendto_state
*state
;
948 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
951 req
= tevent_req_create(mem_ctx
, &state
,
952 struct tdgram_bsd_sendto_state
);
957 state
->dgram
= dgram
;
963 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
965 if (bsds
->fd
== -1) {
966 tevent_req_error(req
, ENOTCONN
);
971 * this is a fast path, not waiting for the
972 * socket to become explicit writeable gains
973 * about 10%-20% performance in benchmark tests.
975 tdgram_bsd_sendto_handler(req
);
976 if (!tevent_req_is_in_progress(req
)) {
980 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
981 tdgram_bsd_sendto_handler
,
984 tevent_req_error(req
, errno
);
991 tevent_req_post(req
, ev
);
995 static void tdgram_bsd_sendto_handler(void *private_data
)
997 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
999 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1000 struct tdgram_bsd_sendto_state
);
1001 struct tdgram_context
*dgram
= state
->dgram
;
1002 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1003 struct sockaddr
*sa
= NULL
;
1004 socklen_t sa_socklen
= 0;
1010 struct tsocket_address_bsd
*bsda
=
1011 talloc_get_type(state
->dst
->private_data
,
1012 struct tsocket_address_bsd
);
1015 sa_socklen
= sizeof(bsda
->u
.ss
);
1017 * for unix sockets we can't use the size of sockaddr_storage
1018 * we would get EINVAL
1020 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
1021 sa_socklen
= sizeof(bsda
->u
.un
);
1025 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1026 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1031 if (tevent_req_error(req
, err
)) {
1037 tevent_req_done(req
);
1040 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1042 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1043 struct tdgram_bsd_sendto_state
);
1046 ret
= tsocket_simple_int_recv(req
, perrno
);
1051 tevent_req_received(req
);
1055 struct tdgram_bsd_disconnect_state
{
1059 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1060 struct tevent_context
*ev
,
1061 struct tdgram_context
*dgram
)
1063 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1064 struct tevent_req
*req
;
1065 struct tdgram_bsd_disconnect_state
*state
;
1070 req
= tevent_req_create(mem_ctx
, &state
,
1071 struct tdgram_bsd_disconnect_state
);
1076 if (bsds
->fd
== -1) {
1077 tevent_req_error(req
, ENOTCONN
);
1081 ret
= close(bsds
->fd
);
1083 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1084 if (tevent_req_error(req
, err
)) {
1088 tevent_req_done(req
);
1090 tevent_req_post(req
, ev
);
1094 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1099 ret
= tsocket_simple_int_recv(req
, perrno
);
1101 tevent_req_received(req
);
1105 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1108 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1109 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1111 .sendto_send
= tdgram_bsd_sendto_send
,
1112 .sendto_recv
= tdgram_bsd_sendto_recv
,
1114 .disconnect_send
= tdgram_bsd_disconnect_send
,
1115 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1118 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1120 TALLOC_FREE(bsds
->fde
);
1121 if (bsds
->fd
!= -1) {
1128 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1129 const struct tsocket_address
*remote
,
1131 TALLOC_CTX
*mem_ctx
,
1132 struct tdgram_context
**_dgram
,
1133 const char *location
)
1135 struct tsocket_address_bsd
*lbsda
=
1136 talloc_get_type_abort(local
->private_data
,
1137 struct tsocket_address_bsd
);
1138 struct tsocket_address_bsd
*rbsda
= NULL
;
1139 struct tdgram_context
*dgram
;
1140 struct tdgram_bsd
*bsds
;
1143 bool do_bind
= false;
1144 bool do_reuseaddr
= false;
1145 bool do_ipv6only
= false;
1146 bool is_inet
= false;
1147 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1148 socklen_t sa_socklen
= sizeof(lbsda
->u
.ss
);
1151 rbsda
= talloc_get_type_abort(remote
->private_data
,
1152 struct tsocket_address_bsd
);
1155 switch (lbsda
->u
.sa
.sa_family
) {
1161 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1162 do_reuseaddr
= true;
1166 * for unix sockets we can't use the size of sockaddr_storage
1167 * we would get EINVAL
1169 sa_socklen
= sizeof(lbsda
->u
.un
);
1172 if (lbsda
->u
.in
.sin_port
!= 0) {
1173 do_reuseaddr
= true;
1176 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1180 sa_socklen
= sizeof(rbsda
->u
.in
);
1184 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1185 do_reuseaddr
= true;
1188 if (memcmp(&in6addr_any
,
1189 &lbsda
->u
.in6
.sin6_addr
,
1190 sizeof(in6addr_any
)) != 0) {
1194 sa_socklen
= sizeof(rbsda
->u
.in6
);
1203 if (!do_bind
&& is_inet
&& rbsda
) {
1204 sa_fam
= rbsda
->u
.sa
.sa_family
;
1207 sa_socklen
= sizeof(rbsda
->u
.in
);
1208 do_ipv6only
= false;
1212 sa_socklen
= sizeof(rbsda
->u
.in6
);
1219 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1224 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1229 dgram
= tdgram_context_create(mem_ctx
,
1235 int saved_errno
= errno
;
1237 errno
= saved_errno
;
1242 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1248 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1249 (const void *)&val
, sizeof(val
));
1251 int saved_errno
= errno
;
1253 errno
= saved_errno
;
1262 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1263 (const void *)&val
, sizeof(val
));
1265 int saved_errno
= errno
;
1267 errno
= saved_errno
;
1275 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1276 (const void *)&val
, sizeof(val
));
1278 int saved_errno
= errno
;
1280 errno
= saved_errno
;
1286 ret
= bind(fd
, &lbsda
->u
.sa
, sa_socklen
);
1288 int saved_errno
= errno
;
1290 errno
= saved_errno
;
1296 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1302 ret
= connect(fd
, &rbsda
->u
.sa
, sa_socklen
);
1304 int saved_errno
= errno
;
1306 errno
= saved_errno
;
1315 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1316 const struct tsocket_address
*remote
,
1317 TALLOC_CTX
*mem_ctx
,
1318 struct tdgram_context
**dgram
,
1319 const char *location
)
1321 struct tsocket_address_bsd
*lbsda
=
1322 talloc_get_type_abort(local
->private_data
,
1323 struct tsocket_address_bsd
);
1326 switch (lbsda
->u
.sa
.sa_family
) {
1338 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1339 mem_ctx
, dgram
, location
);
1344 int _tdgram_unix_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
);
1355 switch (lbsda
->u
.sa
.sa_family
) {
1363 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1364 mem_ctx
, dgram
, location
);
1369 struct tstream_bsd
{
1373 struct tevent_fd
*fde
;
1375 void *readable_private
;
1376 void (*readable_handler
)(void *private_data
);
1377 void *writeable_private
;
1378 void (*writeable_handler
)(void *private_data
);
1381 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1382 struct tevent_fd
*fde
,
1386 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1387 struct tstream_bsd
);
1389 if (flags
& TEVENT_FD_WRITE
) {
1390 bsds
->writeable_handler(bsds
->writeable_private
);
1393 if (flags
& TEVENT_FD_READ
) {
1394 if (!bsds
->readable_handler
) {
1395 if (bsds
->writeable_handler
) {
1396 bsds
->writeable_handler(bsds
->writeable_private
);
1399 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1402 bsds
->readable_handler(bsds
->readable_private
);
1407 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1408 struct tevent_context
*ev
,
1409 void (*handler
)(void *private_data
),
1417 if (!bsds
->readable_handler
) {
1420 bsds
->readable_handler
= NULL
;
1421 bsds
->readable_private
= NULL
;
1426 /* read and write must use the same tevent_context */
1427 if (bsds
->event_ptr
!= ev
) {
1428 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1432 bsds
->event_ptr
= NULL
;
1433 TALLOC_FREE(bsds
->fde
);
1436 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1437 TALLOC_FREE(bsds
->fde
);
1439 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1440 bsds
->fd
, TEVENT_FD_READ
,
1441 tstream_bsd_fde_handler
,
1448 /* cache the event context we're running on */
1449 bsds
->event_ptr
= ev
;
1450 } else if (!bsds
->readable_handler
) {
1451 TEVENT_FD_READABLE(bsds
->fde
);
1454 bsds
->readable_handler
= handler
;
1455 bsds
->readable_private
= private_data
;
1460 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1461 struct tevent_context
*ev
,
1462 void (*handler
)(void *private_data
),
1470 if (!bsds
->writeable_handler
) {
1473 bsds
->writeable_handler
= NULL
;
1474 bsds
->writeable_private
= NULL
;
1475 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1480 /* read and write must use the same tevent_context */
1481 if (bsds
->event_ptr
!= ev
) {
1482 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1486 bsds
->event_ptr
= NULL
;
1487 TALLOC_FREE(bsds
->fde
);
1490 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1491 TALLOC_FREE(bsds
->fde
);
1493 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1495 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1496 tstream_bsd_fde_handler
,
1503 /* cache the event context we're running on */
1504 bsds
->event_ptr
= ev
;
1505 } else if (!bsds
->writeable_handler
) {
1506 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1507 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1508 tevent_fd_set_flags(bsds
->fde
, flags
);
1511 bsds
->writeable_handler
= handler
;
1512 bsds
->writeable_private
= private_data
;
1517 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1519 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1520 struct tstream_bsd
);
1523 if (bsds
->fd
== -1) {
1528 ret
= tsocket_bsd_pending(bsds
->fd
);
1533 struct tstream_bsd_readv_state
{
1534 struct tstream_context
*stream
;
1536 struct iovec
*vector
;
1542 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1544 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1545 struct tstream_bsd
);
1547 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1552 static void tstream_bsd_readv_handler(void *private_data
);
1554 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1555 struct tevent_context
*ev
,
1556 struct tstream_context
*stream
,
1557 struct iovec
*vector
,
1560 struct tevent_req
*req
;
1561 struct tstream_bsd_readv_state
*state
;
1562 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1565 req
= tevent_req_create(mem_ctx
, &state
,
1566 struct tstream_bsd_readv_state
);
1571 state
->stream
= stream
;
1572 /* we make a copy of the vector so that we can modify it */
1573 state
->vector
= talloc_array(state
, struct iovec
, count
);
1574 if (tevent_req_nomem(state
->vector
, req
)) {
1577 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1578 state
->count
= count
;
1581 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1583 if (bsds
->fd
== -1) {
1584 tevent_req_error(req
, ENOTCONN
);
1589 * this is a fast path, not waiting for the
1590 * socket to become explicit readable gains
1591 * about 10%-20% performance in benchmark tests.
1593 tstream_bsd_readv_handler(req
);
1594 if (!tevent_req_is_in_progress(req
)) {
1598 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1599 tstream_bsd_readv_handler
,
1602 tevent_req_error(req
, errno
);
1609 tevent_req_post(req
, ev
);
1613 static void tstream_bsd_readv_handler(void *private_data
)
1615 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1617 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1618 struct tstream_bsd_readv_state
);
1619 struct tstream_context
*stream
= state
->stream
;
1620 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1625 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1627 /* propagate end of file */
1628 tevent_req_error(req
, EPIPE
);
1631 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1636 if (tevent_req_error(req
, err
)) {
1643 if (ret
< state
->vector
[0].iov_len
) {
1645 base
= (uint8_t *)state
->vector
[0].iov_base
;
1647 state
->vector
[0].iov_base
= base
;
1648 state
->vector
[0].iov_len
-= ret
;
1651 ret
-= state
->vector
[0].iov_len
;
1657 * there're maybe some empty vectors at the end
1658 * which we need to skip, otherwise we would get
1659 * ret == 0 from the readv() call and return EPIPE
1661 while (state
->count
> 0) {
1662 if (state
->vector
[0].iov_len
> 0) {
1669 if (state
->count
> 0) {
1670 /* we have more to read */
1674 tevent_req_done(req
);
1677 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1680 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1681 struct tstream_bsd_readv_state
);
1684 ret
= tsocket_simple_int_recv(req
, perrno
);
1689 tevent_req_received(req
);
1693 struct tstream_bsd_writev_state
{
1694 struct tstream_context
*stream
;
1696 struct iovec
*vector
;
1702 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1704 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1705 struct tstream_bsd
);
1707 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1712 static void tstream_bsd_writev_handler(void *private_data
);
1714 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1715 struct tevent_context
*ev
,
1716 struct tstream_context
*stream
,
1717 const struct iovec
*vector
,
1720 struct tevent_req
*req
;
1721 struct tstream_bsd_writev_state
*state
;
1722 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1725 req
= tevent_req_create(mem_ctx
, &state
,
1726 struct tstream_bsd_writev_state
);
1731 state
->stream
= stream
;
1732 /* we make a copy of the vector so that we can modify it */
1733 state
->vector
= talloc_array(state
, struct iovec
, count
);
1734 if (tevent_req_nomem(state
->vector
, req
)) {
1737 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1738 state
->count
= count
;
1741 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1743 if (bsds
->fd
== -1) {
1744 tevent_req_error(req
, ENOTCONN
);
1749 * this is a fast path, not waiting for the
1750 * socket to become explicit writeable gains
1751 * about 10%-20% performance in benchmark tests.
1753 tstream_bsd_writev_handler(req
);
1754 if (!tevent_req_is_in_progress(req
)) {
1758 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1759 tstream_bsd_writev_handler
,
1762 tevent_req_error(req
, errno
);
1769 tevent_req_post(req
, ev
);
1773 static void tstream_bsd_writev_handler(void *private_data
)
1775 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1777 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1778 struct tstream_bsd_writev_state
);
1779 struct tstream_context
*stream
= state
->stream
;
1780 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1785 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1787 /* propagate end of file */
1788 tevent_req_error(req
, EPIPE
);
1791 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1796 if (tevent_req_error(req
, err
)) {
1803 if (ret
< state
->vector
[0].iov_len
) {
1805 base
= (uint8_t *)state
->vector
[0].iov_base
;
1807 state
->vector
[0].iov_base
= base
;
1808 state
->vector
[0].iov_len
-= ret
;
1811 ret
-= state
->vector
[0].iov_len
;
1817 * there're maybe some empty vectors at the end
1818 * which we need to skip, otherwise we would get
1819 * ret == 0 from the writev() call and return EPIPE
1821 while (state
->count
> 0) {
1822 if (state
->vector
[0].iov_len
> 0) {
1829 if (state
->count
> 0) {
1830 /* we have more to read */
1834 tevent_req_done(req
);
1837 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1839 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1840 struct tstream_bsd_writev_state
);
1843 ret
= tsocket_simple_int_recv(req
, perrno
);
1848 tevent_req_received(req
);
1852 struct tstream_bsd_disconnect_state
{
1856 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1857 struct tevent_context
*ev
,
1858 struct tstream_context
*stream
)
1860 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1861 struct tevent_req
*req
;
1862 struct tstream_bsd_disconnect_state
*state
;
1867 req
= tevent_req_create(mem_ctx
, &state
,
1868 struct tstream_bsd_disconnect_state
);
1873 if (bsds
->fd
== -1) {
1874 tevent_req_error(req
, ENOTCONN
);
1878 ret
= close(bsds
->fd
);
1880 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1881 if (tevent_req_error(req
, err
)) {
1885 tevent_req_done(req
);
1887 tevent_req_post(req
, ev
);
1891 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1896 ret
= tsocket_simple_int_recv(req
, perrno
);
1898 tevent_req_received(req
);
1902 static const struct tstream_context_ops tstream_bsd_ops
= {
1905 .pending_bytes
= tstream_bsd_pending_bytes
,
1907 .readv_send
= tstream_bsd_readv_send
,
1908 .readv_recv
= tstream_bsd_readv_recv
,
1910 .writev_send
= tstream_bsd_writev_send
,
1911 .writev_recv
= tstream_bsd_writev_recv
,
1913 .disconnect_send
= tstream_bsd_disconnect_send
,
1914 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1917 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1919 TALLOC_FREE(bsds
->fde
);
1920 if (bsds
->fd
!= -1) {
1927 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1929 struct tstream_context
**_stream
,
1930 const char *location
)
1932 struct tstream_context
*stream
;
1933 struct tstream_bsd
*bsds
;
1935 stream
= tstream_context_create(mem_ctx
,
1945 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1951 struct tstream_bsd_connect_state
{
1953 struct tevent_fd
*fde
;
1954 struct tstream_conext
*stream
;
1957 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1959 TALLOC_FREE(state
->fde
);
1960 if (state
->fd
!= -1) {
1968 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1969 struct tevent_fd
*fde
,
1971 void *private_data
);
1973 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1974 struct tevent_context
*ev
,
1976 const struct tsocket_address
*local
,
1977 const struct tsocket_address
*remote
)
1979 struct tevent_req
*req
;
1980 struct tstream_bsd_connect_state
*state
;
1981 struct tsocket_address_bsd
*lbsda
=
1982 talloc_get_type_abort(local
->private_data
,
1983 struct tsocket_address_bsd
);
1984 struct tsocket_address_bsd
*rbsda
=
1985 talloc_get_type_abort(remote
->private_data
,
1986 struct tsocket_address_bsd
);
1990 bool do_bind
= false;
1991 bool do_reuseaddr
= false;
1992 bool do_ipv6only
= false;
1993 bool is_inet
= false;
1994 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1995 socklen_t sa_socklen
= sizeof(rbsda
->u
.ss
);
1997 req
= tevent_req_create(mem_ctx
, &state
,
1998 struct tstream_bsd_connect_state
);
2005 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2007 /* give the wrappers a chance to report an error */
2008 if (sys_errno
!= 0) {
2009 tevent_req_error(req
, sys_errno
);
2013 switch (lbsda
->u
.sa
.sa_family
) {
2015 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2016 do_reuseaddr
= true;
2020 * for unix sockets we can't use the size of sockaddr_storage
2021 * we would get EINVAL
2023 sa_socklen
= sizeof(rbsda
->u
.un
);
2026 if (lbsda
->u
.in
.sin_port
!= 0) {
2027 do_reuseaddr
= true;
2030 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2034 sa_socklen
= sizeof(rbsda
->u
.in
);
2038 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2039 do_reuseaddr
= true;
2042 if (memcmp(&in6addr_any
,
2043 &lbsda
->u
.in6
.sin6_addr
,
2044 sizeof(in6addr_any
)) != 0) {
2048 sa_socklen
= sizeof(rbsda
->u
.in6
);
2053 tevent_req_error(req
, EINVAL
);
2057 if (!do_bind
&& is_inet
) {
2058 sa_fam
= rbsda
->u
.sa
.sa_family
;
2061 sa_socklen
= sizeof(rbsda
->u
.in
);
2062 do_ipv6only
= false;
2066 sa_socklen
= sizeof(rbsda
->u
.in6
);
2073 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2074 if (state
->fd
== -1) {
2075 tevent_req_error(req
, errno
);
2079 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2080 if (state
->fd
== -1) {
2081 tevent_req_error(req
, errno
);
2089 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2090 (const void *)&val
, sizeof(val
));
2092 tevent_req_error(req
, errno
);
2101 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2102 (const void *)&val
, sizeof(val
));
2104 tevent_req_error(req
, errno
);
2110 ret
= bind(state
->fd
, &lbsda
->u
.sa
, sa_socklen
);
2112 tevent_req_error(req
, errno
);
2117 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2118 tevent_req_error(req
, EINVAL
);
2122 ret
= connect(state
->fd
, &rbsda
->u
.sa
, sa_socklen
);
2123 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2128 if (tevent_req_error(req
, err
)) {
2132 tevent_req_done(req
);
2136 state
->fde
= tevent_add_fd(ev
, state
,
2138 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2139 tstream_bsd_connect_fde_handler
,
2141 if (tevent_req_nomem(state
->fde
, req
)) {
2148 tevent_req_post(req
, ev
);
2152 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2153 struct tevent_fd
*fde
,
2157 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2159 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2160 struct tstream_bsd_connect_state
);
2163 socklen_t len
= sizeof(error
);
2167 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2174 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2179 if (tevent_req_error(req
, err
)) {
2183 tevent_req_done(req
);
2186 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2188 TALLOC_CTX
*mem_ctx
,
2189 struct tstream_context
**stream
,
2190 const char *location
)
2192 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2193 struct tstream_bsd_connect_state
);
2196 ret
= tsocket_simple_int_recv(req
, perrno
);
2198 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2206 TALLOC_FREE(state
->fde
);
2211 tevent_req_received(req
);
2215 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2216 struct tevent_context
*ev
,
2217 const struct tsocket_address
*local
,
2218 const struct tsocket_address
*remote
)
2220 struct tsocket_address_bsd
*lbsda
=
2221 talloc_get_type_abort(local
->private_data
,
2222 struct tsocket_address_bsd
);
2223 struct tevent_req
*req
;
2226 switch (lbsda
->u
.sa
.sa_family
) {
2238 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2243 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2245 TALLOC_CTX
*mem_ctx
,
2246 struct tstream_context
**stream
,
2247 const char *location
)
2249 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2252 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2253 struct tevent_context
*ev
,
2254 const struct tsocket_address
*local
,
2255 const struct tsocket_address
*remote
)
2257 struct tsocket_address_bsd
*lbsda
=
2258 talloc_get_type_abort(local
->private_data
,
2259 struct tsocket_address_bsd
);
2260 struct tevent_req
*req
;
2263 switch (lbsda
->u
.sa
.sa_family
) {
2271 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2276 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2278 TALLOC_CTX
*mem_ctx
,
2279 struct tstream_context
**stream
,
2280 const char *location
)
2282 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2285 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2286 struct tstream_context
**_stream1
,
2287 TALLOC_CTX
*mem_ctx2
,
2288 struct tstream_context
**_stream2
,
2289 const char *location
)
2295 struct tstream_context
*stream1
= NULL
;
2296 struct tstream_context
*stream2
= NULL
;
2298 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2305 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2307 int sys_errno
= errno
;
2313 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2315 int sys_errno
= errno
;
2321 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2326 int sys_errno
= errno
;
2333 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2338 int sys_errno
= errno
;
2339 talloc_free(stream1
);
2345 *_stream1
= stream1
;
2346 *_stream2
= stream2
;