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
{
193 socklen_t sa_socklen
;
196 struct sockaddr_in in
;
198 struct sockaddr_in6 in6
;
200 struct sockaddr_un un
;
201 struct sockaddr_storage ss
;
205 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
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
)) {
219 switch (sa
->sa_family
) {
221 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
222 sa_socklen
= sizeof(struct sockaddr_un
);
226 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
230 sa_socklen
= sizeof(struct sockaddr_in
);
234 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
238 sa_socklen
= sizeof(struct sockaddr_in6
);
242 errno
= EAFNOSUPPORT
;
246 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
251 addr
= tsocket_address_create(mem_ctx
,
252 &tsocket_address_bsd_ops
,
254 struct tsocket_address_bsd
,
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
;
274 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
278 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
279 struct tsocket_address_bsd
);
286 if (sa_socklen
< bsda
->sa_socklen
) {
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
;
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
);
312 switch (bsda
->u
.sa
.sa_family
) {
314 if (strcasecmp(fam
, "ip") == 0) {
318 if (strcasecmp(fam
, "ipv4") == 0) {
325 if (strcasecmp(fam
, "ip") == 0) {
329 if (strcasecmp(fam
, "ipv6") == 0) {
340 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
344 struct tsocket_address
**_addr
,
345 const char *location
)
347 struct addrinfo hints
;
348 struct addrinfo
*result
= NULL
;
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
;
369 } else if (strcasecmp(fam
, "ipv4") == 0) {
370 hints
.ai_family
= AF_INET
;
375 } else if (strcasecmp(fam
, "ipv6") == 0) {
376 hints
.ai_family
= AF_INET6
;
382 errno
= EAFNOSUPPORT
;
386 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
388 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
399 if (result
->ai_socktype
!= SOCK_STREAM
) {
405 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
413 freeaddrinfo(result
);
418 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
421 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
422 struct tsocket_address_bsd
);
423 char addr_str
[INET6_ADDRSTRLEN
+1];
431 switch (bsda
->u
.sa
.sa_family
) {
433 str
= inet_ntop(bsda
->u
.in
.sin_family
,
434 &bsda
->u
.in
.sin_addr
,
435 addr_str
, sizeof(addr_str
));
439 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
440 &bsda
->u
.in6
.sin6_addr
,
441 addr_str
, sizeof(addr_str
));
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
);
467 switch (bsda
->u
.sa
.sa_family
) {
469 port
= ntohs(bsda
->u
.in
.sin_port
);
473 port
= ntohs(bsda
->u
.in6
.sin6_port
);
484 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
487 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
488 struct tsocket_address_bsd
);
495 switch (bsda
->u
.sa
.sa_family
) {
497 bsda
->u
.in
.sin_port
= htons(port
);
501 bsda
->u
.in6
.sin6_port
= htons(port
);
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
);
521 switch (bsda
->u
.sa
.sa_family
) {
529 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
531 struct tsocket_address
**_addr
,
532 const char *location
)
534 struct sockaddr_un un
;
542 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
543 errno
= ENAMETOOLONG
;
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
,
560 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
563 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
564 struct tsocket_address_bsd
);
572 switch (bsda
->u
.sa
.sa_family
) {
574 str
= bsda
->u
.un
.sun_path
;
581 return talloc_strdup(mem_ctx
, str
);
584 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
587 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
588 struct tsocket_address_bsd
);
591 const char *prefix
= NULL
;
594 switch (bsda
->u
.sa
.sa_family
) {
596 return talloc_asprintf(mem_ctx
, "unix:%s",
597 bsda
->u
.un
.sun_path
);
611 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
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
);
625 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
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
;
634 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
646 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
648 .string
= tsocket_address_bsd_string
,
649 .copy
= tsocket_address_bsd_copy
,
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
,
669 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
672 if (flags
& TEVENT_FD_WRITE
) {
673 bsds
->writeable_handler(bsds
->writeable_private
);
676 if (flags
& TEVENT_FD_READ
) {
677 if (!bsds
->readable_handler
) {
678 TEVENT_FD_NOT_READABLE(bsds
->fde
);
681 bsds
->readable_handler(bsds
->readable_private
);
686 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
687 struct tevent_context
*ev
,
688 void (*handler
)(void *private_data
),
696 if (!bsds
->readable_handler
) {
699 bsds
->readable_handler
= NULL
;
700 bsds
->readable_private
= NULL
;
705 /* read and write must use the same tevent_context */
706 if (bsds
->event_ptr
!= ev
) {
707 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
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
,
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
;
739 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
740 struct tevent_context
*ev
,
741 void (*handler
)(void *private_data
),
749 if (!bsds
->writeable_handler
) {
752 bsds
->writeable_handler
= NULL
;
753 bsds
->writeable_private
= NULL
;
754 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
759 /* read and write must use the same tevent_context */
760 if (bsds
->event_ptr
!= ev
) {
761 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
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
,
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
;
793 struct tdgram_bsd_recvfrom_state
{
794 struct tdgram_context
*dgram
;
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
,
806 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
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
);
822 req
= tevent_req_create(mem_ctx
, &state
,
823 struct tdgram_bsd_recvfrom_state
);
828 state
->dgram
= dgram
;
829 state
->first_try
= true;
834 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
836 if (bsds
->fd
== -1) {
837 tevent_req_error(req
, ENOTCONN
);
842 * this is a fast path, not waiting for the
843 * socket to become explicit readable gains
844 * about 10%-20% performance in benchmark tests.
846 tdgram_bsd_recvfrom_handler(req
);
847 if (!tevent_req_is_in_progress(req
)) {
851 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
852 tdgram_bsd_recvfrom_handler
,
855 tevent_req_error(req
, errno
);
862 tevent_req_post(req
, ev
);
866 static void tdgram_bsd_recvfrom_handler(void *private_data
)
868 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
870 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
871 struct tdgram_bsd_recvfrom_state
);
872 struct tdgram_context
*dgram
= state
->dgram
;
873 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
874 struct tsocket_address_bsd
*bsda
;
879 ret
= tsocket_bsd_pending(bsds
->fd
);
880 if (state
->first_try
&& ret
== 0) {
881 state
->first_try
= false;
885 state
->first_try
= false;
887 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
892 if (tevent_req_error(req
, err
)) {
896 /* note that 'ret' can be 0 here */
897 state
->buf
= talloc_array(state
, uint8_t, ret
);
898 if (tevent_req_nomem(state
->buf
, req
)) {
903 state
->src
= tsocket_address_create(state
,
904 &tsocket_address_bsd_ops
,
906 struct tsocket_address_bsd
,
907 __location__
"bsd_recvfrom");
908 if (tevent_req_nomem(state
->src
, req
)) {
913 bsda
->sa_socklen
= sizeof(bsda
->u
.ss
);
914 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
915 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
918 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0,
919 &bsda
->u
.sa
, &bsda
->sa_socklen
);
920 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
925 if (tevent_req_error(req
, err
)) {
930 * Some systems (FreeBSD, see bug #7115) return too much
931 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
932 * the return value includes some IP/UDP header bytes,
933 * while recvfrom() just returns the payload.
935 state
->buf
= talloc_realloc(state
, state
->buf
, uint8_t, ret
);
936 if (tevent_req_nomem(state
->buf
, req
)) {
941 tevent_req_done(req
);
944 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
948 struct tsocket_address
**src
)
950 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
951 struct tdgram_bsd_recvfrom_state
);
954 ret
= tsocket_simple_int_recv(req
, perrno
);
956 *buf
= talloc_move(mem_ctx
, &state
->buf
);
959 *src
= talloc_move(mem_ctx
, &state
->src
);
963 tevent_req_received(req
);
967 struct tdgram_bsd_sendto_state
{
968 struct tdgram_context
*dgram
;
972 const struct tsocket_address
*dst
;
977 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
979 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
982 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
987 static void tdgram_bsd_sendto_handler(void *private_data
);
989 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
990 struct tevent_context
*ev
,
991 struct tdgram_context
*dgram
,
994 const struct tsocket_address
*dst
)
996 struct tevent_req
*req
;
997 struct tdgram_bsd_sendto_state
*state
;
998 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1001 req
= tevent_req_create(mem_ctx
, &state
,
1002 struct tdgram_bsd_sendto_state
);
1007 state
->dgram
= dgram
;
1013 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
1015 if (bsds
->fd
== -1) {
1016 tevent_req_error(req
, ENOTCONN
);
1021 * this is a fast path, not waiting for the
1022 * socket to become explicit writeable gains
1023 * about 10%-20% performance in benchmark tests.
1025 tdgram_bsd_sendto_handler(req
);
1026 if (!tevent_req_is_in_progress(req
)) {
1030 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
1031 tdgram_bsd_sendto_handler
,
1034 tevent_req_error(req
, errno
);
1041 tevent_req_post(req
, ev
);
1045 static void tdgram_bsd_sendto_handler(void *private_data
)
1047 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1049 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1050 struct tdgram_bsd_sendto_state
);
1051 struct tdgram_context
*dgram
= state
->dgram
;
1052 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1053 struct sockaddr
*sa
= NULL
;
1054 socklen_t sa_socklen
= 0;
1060 struct tsocket_address_bsd
*bsda
=
1061 talloc_get_type(state
->dst
->private_data
,
1062 struct tsocket_address_bsd
);
1065 sa_socklen
= bsda
->sa_socklen
;
1068 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1069 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1074 if (tevent_req_error(req
, err
)) {
1080 tevent_req_done(req
);
1083 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1085 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1086 struct tdgram_bsd_sendto_state
);
1089 ret
= tsocket_simple_int_recv(req
, perrno
);
1094 tevent_req_received(req
);
1098 struct tdgram_bsd_disconnect_state
{
1102 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1103 struct tevent_context
*ev
,
1104 struct tdgram_context
*dgram
)
1106 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1107 struct tevent_req
*req
;
1108 struct tdgram_bsd_disconnect_state
*state
;
1113 req
= tevent_req_create(mem_ctx
, &state
,
1114 struct tdgram_bsd_disconnect_state
);
1119 if (bsds
->fd
== -1) {
1120 tevent_req_error(req
, ENOTCONN
);
1124 TALLOC_FREE(bsds
->fde
);
1125 ret
= close(bsds
->fd
);
1127 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1128 if (tevent_req_error(req
, err
)) {
1132 tevent_req_done(req
);
1134 tevent_req_post(req
, ev
);
1138 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1143 ret
= tsocket_simple_int_recv(req
, perrno
);
1145 tevent_req_received(req
);
1149 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1152 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1153 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1155 .sendto_send
= tdgram_bsd_sendto_send
,
1156 .sendto_recv
= tdgram_bsd_sendto_recv
,
1158 .disconnect_send
= tdgram_bsd_disconnect_send
,
1159 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1162 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1164 TALLOC_FREE(bsds
->fde
);
1165 if (bsds
->fd
!= -1) {
1172 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1173 const struct tsocket_address
*remote
,
1175 TALLOC_CTX
*mem_ctx
,
1176 struct tdgram_context
**_dgram
,
1177 const char *location
)
1179 struct tsocket_address_bsd
*lbsda
=
1180 talloc_get_type_abort(local
->private_data
,
1181 struct tsocket_address_bsd
);
1182 struct tsocket_address_bsd
*rbsda
= NULL
;
1183 struct tdgram_context
*dgram
;
1184 struct tdgram_bsd
*bsds
;
1187 bool do_bind
= false;
1188 bool do_reuseaddr
= false;
1189 bool do_ipv6only
= false;
1190 bool is_inet
= false;
1191 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1194 rbsda
= talloc_get_type_abort(remote
->private_data
,
1195 struct tsocket_address_bsd
);
1198 switch (lbsda
->u
.sa
.sa_family
) {
1204 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1205 do_reuseaddr
= true;
1210 if (lbsda
->u
.in
.sin_port
!= 0) {
1211 do_reuseaddr
= true;
1214 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1221 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1222 do_reuseaddr
= true;
1225 if (memcmp(&in6addr_any
,
1226 &lbsda
->u
.in6
.sin6_addr
,
1227 sizeof(in6addr_any
)) != 0) {
1239 if (!do_bind
&& is_inet
&& rbsda
) {
1240 sa_fam
= rbsda
->u
.sa
.sa_family
;
1243 do_ipv6only
= false;
1253 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1258 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1263 dgram
= tdgram_context_create(mem_ctx
,
1269 int saved_errno
= errno
;
1271 errno
= saved_errno
;
1276 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1282 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1283 (const void *)&val
, sizeof(val
));
1285 int saved_errno
= errno
;
1287 errno
= saved_errno
;
1296 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1297 (const void *)&val
, sizeof(val
));
1299 int saved_errno
= errno
;
1301 errno
= saved_errno
;
1309 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1310 (const void *)&val
, sizeof(val
));
1312 int saved_errno
= errno
;
1314 errno
= saved_errno
;
1320 ret
= bind(fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
1322 int saved_errno
= errno
;
1324 errno
= saved_errno
;
1330 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1336 ret
= connect(fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
1338 int saved_errno
= errno
;
1340 errno
= saved_errno
;
1349 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1350 const struct tsocket_address
*remote
,
1351 TALLOC_CTX
*mem_ctx
,
1352 struct tdgram_context
**dgram
,
1353 const char *location
)
1355 struct tsocket_address_bsd
*lbsda
=
1356 talloc_get_type_abort(local
->private_data
,
1357 struct tsocket_address_bsd
);
1360 switch (lbsda
->u
.sa
.sa_family
) {
1372 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1373 mem_ctx
, dgram
, location
);
1378 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1379 const struct tsocket_address
*remote
,
1380 TALLOC_CTX
*mem_ctx
,
1381 struct tdgram_context
**dgram
,
1382 const char *location
)
1384 struct tsocket_address_bsd
*lbsda
=
1385 talloc_get_type_abort(local
->private_data
,
1386 struct tsocket_address_bsd
);
1389 switch (lbsda
->u
.sa
.sa_family
) {
1397 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1398 mem_ctx
, dgram
, location
);
1403 struct tstream_bsd
{
1407 struct tevent_fd
*fde
;
1409 void *readable_private
;
1410 void (*readable_handler
)(void *private_data
);
1411 void *writeable_private
;
1412 void (*writeable_handler
)(void *private_data
);
1415 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1416 struct tevent_fd
*fde
,
1420 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1421 struct tstream_bsd
);
1423 if (flags
& TEVENT_FD_WRITE
) {
1424 bsds
->writeable_handler(bsds
->writeable_private
);
1427 if (flags
& TEVENT_FD_READ
) {
1428 if (!bsds
->readable_handler
) {
1429 if (bsds
->writeable_handler
) {
1430 bsds
->writeable_handler(bsds
->writeable_private
);
1433 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1436 bsds
->readable_handler(bsds
->readable_private
);
1441 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1442 struct tevent_context
*ev
,
1443 void (*handler
)(void *private_data
),
1451 if (!bsds
->readable_handler
) {
1454 bsds
->readable_handler
= NULL
;
1455 bsds
->readable_private
= NULL
;
1460 /* read and write must use the same tevent_context */
1461 if (bsds
->event_ptr
!= ev
) {
1462 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1466 bsds
->event_ptr
= NULL
;
1467 TALLOC_FREE(bsds
->fde
);
1470 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1471 TALLOC_FREE(bsds
->fde
);
1473 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1474 bsds
->fd
, TEVENT_FD_READ
,
1475 tstream_bsd_fde_handler
,
1482 /* cache the event context we're running on */
1483 bsds
->event_ptr
= ev
;
1484 } else if (!bsds
->readable_handler
) {
1485 TEVENT_FD_READABLE(bsds
->fde
);
1488 bsds
->readable_handler
= handler
;
1489 bsds
->readable_private
= private_data
;
1494 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1495 struct tevent_context
*ev
,
1496 void (*handler
)(void *private_data
),
1504 if (!bsds
->writeable_handler
) {
1507 bsds
->writeable_handler
= NULL
;
1508 bsds
->writeable_private
= NULL
;
1509 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1514 /* read and write must use the same tevent_context */
1515 if (bsds
->event_ptr
!= ev
) {
1516 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1520 bsds
->event_ptr
= NULL
;
1521 TALLOC_FREE(bsds
->fde
);
1524 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1525 TALLOC_FREE(bsds
->fde
);
1527 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1529 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1530 tstream_bsd_fde_handler
,
1537 /* cache the event context we're running on */
1538 bsds
->event_ptr
= ev
;
1539 } else if (!bsds
->writeable_handler
) {
1540 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1541 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1542 tevent_fd_set_flags(bsds
->fde
, flags
);
1545 bsds
->writeable_handler
= handler
;
1546 bsds
->writeable_private
= private_data
;
1551 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1553 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1554 struct tstream_bsd
);
1557 if (bsds
->fd
== -1) {
1562 ret
= tsocket_bsd_pending(bsds
->fd
);
1567 struct tstream_bsd_readv_state
{
1568 struct tstream_context
*stream
;
1570 struct iovec
*vector
;
1576 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1578 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1579 struct tstream_bsd
);
1581 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1586 static void tstream_bsd_readv_handler(void *private_data
);
1588 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1589 struct tevent_context
*ev
,
1590 struct tstream_context
*stream
,
1591 struct iovec
*vector
,
1594 struct tevent_req
*req
;
1595 struct tstream_bsd_readv_state
*state
;
1596 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1599 req
= tevent_req_create(mem_ctx
, &state
,
1600 struct tstream_bsd_readv_state
);
1605 state
->stream
= stream
;
1606 /* we make a copy of the vector so that we can modify it */
1607 state
->vector
= talloc_array(state
, struct iovec
, count
);
1608 if (tevent_req_nomem(state
->vector
, req
)) {
1611 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1612 state
->count
= count
;
1615 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1617 if (bsds
->fd
== -1) {
1618 tevent_req_error(req
, ENOTCONN
);
1623 * this is a fast path, not waiting for the
1624 * socket to become explicit readable gains
1625 * about 10%-20% performance in benchmark tests.
1627 tstream_bsd_readv_handler(req
);
1628 if (!tevent_req_is_in_progress(req
)) {
1632 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1633 tstream_bsd_readv_handler
,
1636 tevent_req_error(req
, errno
);
1643 tevent_req_post(req
, ev
);
1647 static void tstream_bsd_readv_handler(void *private_data
)
1649 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1651 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1652 struct tstream_bsd_readv_state
);
1653 struct tstream_context
*stream
= state
->stream
;
1654 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1659 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1661 /* propagate end of file */
1662 tevent_req_error(req
, EPIPE
);
1665 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1670 if (tevent_req_error(req
, err
)) {
1677 if (ret
< state
->vector
[0].iov_len
) {
1679 base
= (uint8_t *)state
->vector
[0].iov_base
;
1681 state
->vector
[0].iov_base
= (void *)base
;
1682 state
->vector
[0].iov_len
-= ret
;
1685 ret
-= state
->vector
[0].iov_len
;
1691 * there're maybe some empty vectors at the end
1692 * which we need to skip, otherwise we would get
1693 * ret == 0 from the readv() call and return EPIPE
1695 while (state
->count
> 0) {
1696 if (state
->vector
[0].iov_len
> 0) {
1703 if (state
->count
> 0) {
1704 /* we have more to read */
1708 tevent_req_done(req
);
1711 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1714 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1715 struct tstream_bsd_readv_state
);
1718 ret
= tsocket_simple_int_recv(req
, perrno
);
1723 tevent_req_received(req
);
1727 struct tstream_bsd_writev_state
{
1728 struct tstream_context
*stream
;
1730 struct iovec
*vector
;
1736 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1738 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1739 struct tstream_bsd
);
1741 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1746 static void tstream_bsd_writev_handler(void *private_data
);
1748 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1749 struct tevent_context
*ev
,
1750 struct tstream_context
*stream
,
1751 const struct iovec
*vector
,
1754 struct tevent_req
*req
;
1755 struct tstream_bsd_writev_state
*state
;
1756 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1759 req
= tevent_req_create(mem_ctx
, &state
,
1760 struct tstream_bsd_writev_state
);
1765 state
->stream
= stream
;
1766 /* we make a copy of the vector so that we can modify it */
1767 state
->vector
= talloc_array(state
, struct iovec
, count
);
1768 if (tevent_req_nomem(state
->vector
, req
)) {
1771 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1772 state
->count
= count
;
1775 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1777 if (bsds
->fd
== -1) {
1778 tevent_req_error(req
, ENOTCONN
);
1783 * this is a fast path, not waiting for the
1784 * socket to become explicit writeable gains
1785 * about 10%-20% performance in benchmark tests.
1787 tstream_bsd_writev_handler(req
);
1788 if (!tevent_req_is_in_progress(req
)) {
1792 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1793 tstream_bsd_writev_handler
,
1796 tevent_req_error(req
, errno
);
1803 tevent_req_post(req
, ev
);
1807 static void tstream_bsd_writev_handler(void *private_data
)
1809 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1811 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1812 struct tstream_bsd_writev_state
);
1813 struct tstream_context
*stream
= state
->stream
;
1814 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1819 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1821 /* propagate end of file */
1822 tevent_req_error(req
, EPIPE
);
1825 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1830 if (tevent_req_error(req
, err
)) {
1837 if (ret
< state
->vector
[0].iov_len
) {
1839 base
= (uint8_t *)state
->vector
[0].iov_base
;
1841 state
->vector
[0].iov_base
= (void *)base
;
1842 state
->vector
[0].iov_len
-= ret
;
1845 ret
-= state
->vector
[0].iov_len
;
1851 * there're maybe some empty vectors at the end
1852 * which we need to skip, otherwise we would get
1853 * ret == 0 from the writev() call and return EPIPE
1855 while (state
->count
> 0) {
1856 if (state
->vector
[0].iov_len
> 0) {
1863 if (state
->count
> 0) {
1864 /* we have more to read */
1868 tevent_req_done(req
);
1871 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1873 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1874 struct tstream_bsd_writev_state
);
1877 ret
= tsocket_simple_int_recv(req
, perrno
);
1882 tevent_req_received(req
);
1886 struct tstream_bsd_disconnect_state
{
1890 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1891 struct tevent_context
*ev
,
1892 struct tstream_context
*stream
)
1894 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1895 struct tevent_req
*req
;
1896 struct tstream_bsd_disconnect_state
*state
;
1901 req
= tevent_req_create(mem_ctx
, &state
,
1902 struct tstream_bsd_disconnect_state
);
1907 if (bsds
->fd
== -1) {
1908 tevent_req_error(req
, ENOTCONN
);
1912 TALLOC_FREE(bsds
->fde
);
1913 ret
= close(bsds
->fd
);
1915 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1916 if (tevent_req_error(req
, err
)) {
1920 tevent_req_done(req
);
1922 tevent_req_post(req
, ev
);
1926 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1931 ret
= tsocket_simple_int_recv(req
, perrno
);
1933 tevent_req_received(req
);
1937 static const struct tstream_context_ops tstream_bsd_ops
= {
1940 .pending_bytes
= tstream_bsd_pending_bytes
,
1942 .readv_send
= tstream_bsd_readv_send
,
1943 .readv_recv
= tstream_bsd_readv_recv
,
1945 .writev_send
= tstream_bsd_writev_send
,
1946 .writev_recv
= tstream_bsd_writev_recv
,
1948 .disconnect_send
= tstream_bsd_disconnect_send
,
1949 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1952 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1954 TALLOC_FREE(bsds
->fde
);
1955 if (bsds
->fd
!= -1) {
1962 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1964 struct tstream_context
**_stream
,
1965 const char *location
)
1967 struct tstream_context
*stream
;
1968 struct tstream_bsd
*bsds
;
1970 stream
= tstream_context_create(mem_ctx
,
1980 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1986 struct tstream_bsd_connect_state
{
1988 struct tevent_fd
*fde
;
1989 struct tstream_conext
*stream
;
1990 struct tsocket_address
*local
;
1993 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1995 TALLOC_FREE(state
->fde
);
1996 if (state
->fd
!= -1) {
2004 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2005 struct tevent_fd
*fde
,
2007 void *private_data
);
2009 static struct tevent_req
*tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
2010 struct tevent_context
*ev
,
2012 const struct tsocket_address
*local
,
2013 const struct tsocket_address
*remote
)
2015 struct tevent_req
*req
;
2016 struct tstream_bsd_connect_state
*state
;
2017 struct tsocket_address_bsd
*lbsda
=
2018 talloc_get_type_abort(local
->private_data
,
2019 struct tsocket_address_bsd
);
2020 struct tsocket_address_bsd
*lrbsda
= NULL
;
2021 struct tsocket_address_bsd
*rbsda
=
2022 talloc_get_type_abort(remote
->private_data
,
2023 struct tsocket_address_bsd
);
2027 bool do_bind
= false;
2028 bool do_reuseaddr
= false;
2029 bool do_ipv6only
= false;
2030 bool is_inet
= false;
2031 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2033 req
= tevent_req_create(mem_ctx
, &state
,
2034 struct tstream_bsd_connect_state
);
2041 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2043 /* give the wrappers a chance to report an error */
2044 if (sys_errno
!= 0) {
2045 tevent_req_error(req
, sys_errno
);
2049 switch (lbsda
->u
.sa
.sa_family
) {
2051 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2052 do_reuseaddr
= true;
2057 if (lbsda
->u
.in
.sin_port
!= 0) {
2058 do_reuseaddr
= true;
2061 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2068 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2069 do_reuseaddr
= true;
2072 if (memcmp(&in6addr_any
,
2073 &lbsda
->u
.in6
.sin6_addr
,
2074 sizeof(in6addr_any
)) != 0) {
2082 tevent_req_error(req
, EINVAL
);
2086 if (!do_bind
&& is_inet
) {
2087 sa_fam
= rbsda
->u
.sa
.sa_family
;
2090 do_ipv6only
= false;
2101 state
->local
= tsocket_address_create(state
,
2102 &tsocket_address_bsd_ops
,
2104 struct tsocket_address_bsd
,
2105 __location__
"bsd_connect");
2106 if (tevent_req_nomem(state
->local
, req
)) {
2110 ZERO_STRUCTP(lrbsda
);
2111 lrbsda
->sa_socklen
= sizeof(lrbsda
->u
.ss
);
2112 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2113 lrbsda
->u
.sa
.sa_len
= lrbsda
->sa_socklen
;
2117 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2118 if (state
->fd
== -1) {
2119 tevent_req_error(req
, errno
);
2123 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2124 if (state
->fd
== -1) {
2125 tevent_req_error(req
, errno
);
2133 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2134 (const void *)&val
, sizeof(val
));
2136 tevent_req_error(req
, errno
);
2145 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2146 (const void *)&val
, sizeof(val
));
2148 tevent_req_error(req
, errno
);
2154 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2156 tevent_req_error(req
, errno
);
2161 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2162 tevent_req_error(req
, EINVAL
);
2166 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2167 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2172 if (tevent_req_error(req
, err
)) {
2176 if (!state
->local
) {
2177 tevent_req_done(req
);
2181 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2183 tevent_req_error(req
, errno
);
2187 tevent_req_done(req
);
2191 state
->fde
= tevent_add_fd(ev
, state
,
2193 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2194 tstream_bsd_connect_fde_handler
,
2196 if (tevent_req_nomem(state
->fde
, req
)) {
2203 tevent_req_post(req
, ev
);
2207 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2208 struct tevent_fd
*fde
,
2212 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2214 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2215 struct tstream_bsd_connect_state
);
2216 struct tsocket_address_bsd
*lrbsda
= NULL
;
2219 socklen_t len
= sizeof(error
);
2223 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2230 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2235 if (tevent_req_error(req
, err
)) {
2239 if (!state
->local
) {
2240 tevent_req_done(req
);
2244 lrbsda
= talloc_get_type_abort(state
->local
->private_data
,
2245 struct tsocket_address_bsd
);
2247 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2249 tevent_req_error(req
, errno
);
2253 tevent_req_done(req
);
2256 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2258 TALLOC_CTX
*mem_ctx
,
2259 struct tstream_context
**stream
,
2260 struct tsocket_address
**local
,
2261 const char *location
)
2263 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2264 struct tstream_bsd_connect_state
);
2267 ret
= tsocket_simple_int_recv(req
, perrno
);
2269 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2277 TALLOC_FREE(state
->fde
);
2281 *local
= talloc_move(mem_ctx
, &state
->local
);
2286 tevent_req_received(req
);
2290 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2291 struct tevent_context
*ev
,
2292 const struct tsocket_address
*local
,
2293 const struct tsocket_address
*remote
)
2295 struct tsocket_address_bsd
*lbsda
=
2296 talloc_get_type_abort(local
->private_data
,
2297 struct tsocket_address_bsd
);
2298 struct tevent_req
*req
;
2301 switch (lbsda
->u
.sa
.sa_family
) {
2313 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2318 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2320 TALLOC_CTX
*mem_ctx
,
2321 struct tstream_context
**stream
,
2322 struct tsocket_address
**local
,
2323 const char *location
)
2325 return tstream_bsd_connect_recv(req
, perrno
,
2326 mem_ctx
, stream
, local
,
2330 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2331 struct tevent_context
*ev
,
2332 const struct tsocket_address
*local
,
2333 const struct tsocket_address
*remote
)
2335 struct tsocket_address_bsd
*lbsda
=
2336 talloc_get_type_abort(local
->private_data
,
2337 struct tsocket_address_bsd
);
2338 struct tevent_req
*req
;
2341 switch (lbsda
->u
.sa
.sa_family
) {
2349 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2354 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2356 TALLOC_CTX
*mem_ctx
,
2357 struct tstream_context
**stream
,
2358 const char *location
)
2360 return tstream_bsd_connect_recv(req
, perrno
,
2361 mem_ctx
, stream
, NULL
,
2365 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2366 struct tstream_context
**_stream1
,
2367 TALLOC_CTX
*mem_ctx2
,
2368 struct tstream_context
**_stream2
,
2369 const char *location
)
2375 struct tstream_context
*stream1
= NULL
;
2376 struct tstream_context
*stream2
= NULL
;
2378 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2385 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2387 int sys_errno
= errno
;
2393 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2395 int sys_errno
= errno
;
2401 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2406 int sys_errno
= errno
;
2413 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2418 int sys_errno
= errno
;
2419 talloc_free(stream1
);
2425 *_stream1
= stream1
;
2426 *_stream2
= stream2
;