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
) {
63 /* ENOMEM is retryable on Solaris/illumos, and possibly other systems. */
64 if (sys_errno
== ENOMEM
) {
70 if (sys_errno
== EWOULDBLOCK
) {
79 static int tsocket_bsd_common_prepare_fd(int fd
, bool high_fd
)
92 /* first make a fd >= 3 */
102 for (i
=0; i
<num_fds
; i
++) {
111 /* fd should be nonblocking. */
114 #define FLAG_TO_SET O_NONBLOCK
117 #define FLAG_TO_SET O_NDELAY
119 #define FLAG_TO_SET FNDELAY
123 if ((flags
= fcntl(fd
, F_GETFL
)) == -1) {
127 flags
|= FLAG_TO_SET
;
128 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
134 /* fd should be closed on exec() */
136 result
= flags
= fcntl(fd
, F_GETFD
, 0);
139 result
= fcntl(fd
, F_SETFD
, flags
);
156 static ssize_t
tsocket_bsd_pending(int fd
)
162 ret
= ioctl(fd
, FIONREAD
, &value
);
168 /* this should not be reached */
181 * if no data is available check if the socket is in error state. For
182 * dgram sockets it's the way to return ICMP error messages of
183 * connected sockets to the caller.
185 ret
= getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
196 static const struct tsocket_address_ops tsocket_address_bsd_ops
;
198 struct tsocket_address_bsd
{
199 socklen_t sa_socklen
;
202 struct sockaddr_in in
;
204 struct sockaddr_in6 in6
;
206 struct sockaddr_un un
;
207 struct sockaddr_storage ss
;
211 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
212 const struct sockaddr
*sa
,
214 struct tsocket_address
**_addr
,
215 const char *location
)
217 struct tsocket_address
*addr
;
218 struct tsocket_address_bsd
*bsda
;
220 if (sa_socklen
< sizeof(sa
->sa_family
)) {
225 switch (sa
->sa_family
) {
227 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
228 sa_socklen
= sizeof(struct sockaddr_un
);
232 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
236 sa_socklen
= sizeof(struct sockaddr_in
);
240 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
244 sa_socklen
= sizeof(struct sockaddr_in6
);
248 errno
= EAFNOSUPPORT
;
252 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
257 addr
= tsocket_address_create(mem_ctx
,
258 &tsocket_address_bsd_ops
,
260 struct tsocket_address_bsd
,
269 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
271 bsda
->sa_socklen
= sa_socklen
;
272 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
273 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
280 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
284 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
285 struct tsocket_address_bsd
);
292 if (sa_socklen
< bsda
->sa_socklen
) {
297 if (sa_socklen
> bsda
->sa_socklen
) {
298 memset(sa
, 0, sa_socklen
);
299 sa_socklen
= bsda
->sa_socklen
;
302 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
303 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
304 sa
->sa_len
= sa_socklen
;
309 bool tsocket_address_is_inet(const struct tsocket_address
*addr
, const char *fam
)
311 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
312 struct tsocket_address_bsd
);
318 switch (bsda
->u
.sa
.sa_family
) {
320 if (strcasecmp(fam
, "ip") == 0) {
324 if (strcasecmp(fam
, "ipv4") == 0) {
331 if (strcasecmp(fam
, "ip") == 0) {
335 if (strcasecmp(fam
, "ipv6") == 0) {
346 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
350 struct tsocket_address
**_addr
,
351 const char *location
)
353 struct addrinfo hints
;
354 struct addrinfo
*result
= NULL
;
360 * we use SOCKET_STREAM here to get just one result
361 * back from getaddrinfo().
363 hints
.ai_socktype
= SOCK_STREAM
;
364 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
366 if (strcasecmp(fam
, "ip") == 0) {
367 hints
.ai_family
= AF_UNSPEC
;
375 } else if (strcasecmp(fam
, "ipv4") == 0) {
376 hints
.ai_family
= AF_INET
;
381 } else if (strcasecmp(fam
, "ipv6") == 0) {
382 hints
.ai_family
= AF_INET6
;
388 errno
= EAFNOSUPPORT
;
392 snprintf(port_str
, sizeof(port_str
), "%u", port
);
394 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
405 if (result
->ai_socktype
!= SOCK_STREAM
) {
411 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
419 freeaddrinfo(result
);
424 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
427 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
428 struct tsocket_address_bsd
);
429 char addr_str
[INET6_ADDRSTRLEN
+1];
437 switch (bsda
->u
.sa
.sa_family
) {
439 str
= inet_ntop(bsda
->u
.in
.sin_family
,
440 &bsda
->u
.in
.sin_addr
,
441 addr_str
, sizeof(addr_str
));
445 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
446 &bsda
->u
.in6
.sin6_addr
,
447 addr_str
, sizeof(addr_str
));
459 return talloc_strdup(mem_ctx
, str
);
462 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
464 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
465 struct tsocket_address_bsd
);
473 switch (bsda
->u
.sa
.sa_family
) {
475 port
= ntohs(bsda
->u
.in
.sin_port
);
479 port
= ntohs(bsda
->u
.in6
.sin6_port
);
490 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
493 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
494 struct tsocket_address_bsd
);
501 switch (bsda
->u
.sa
.sa_family
) {
503 bsda
->u
.in
.sin_port
= htons(port
);
507 bsda
->u
.in6
.sin6_port
= htons(port
);
518 bool tsocket_address_is_unix(const struct tsocket_address
*addr
)
520 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
521 struct tsocket_address_bsd
);
527 switch (bsda
->u
.sa
.sa_family
) {
535 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
537 struct tsocket_address
**_addr
,
538 const char *location
)
540 struct sockaddr_un un
;
548 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
549 errno
= ENAMETOOLONG
;
554 un
.sun_family
= AF_UNIX
;
555 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
557 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
558 (struct sockaddr
*)p
,
566 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
569 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
570 struct tsocket_address_bsd
);
578 switch (bsda
->u
.sa
.sa_family
) {
580 str
= bsda
->u
.un
.sun_path
;
587 return talloc_strdup(mem_ctx
, str
);
590 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
593 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
594 struct tsocket_address_bsd
);
597 const char *prefix
= NULL
;
600 switch (bsda
->u
.sa
.sa_family
) {
602 return talloc_asprintf(mem_ctx
, "unix:%s",
603 bsda
->u
.un
.sun_path
);
617 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
622 port
= tsocket_address_inet_port(addr
);
624 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
625 prefix
, addr_str
, port
);
626 talloc_free(addr_str
);
631 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
633 const char *location
)
635 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
636 struct tsocket_address_bsd
);
637 struct tsocket_address
*copy
;
640 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
652 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
654 .string
= tsocket_address_bsd_string
,
655 .copy
= tsocket_address_bsd_copy
,
662 struct tevent_fd
*fde
;
663 bool optimize_recvfrom
;
665 void *readable_private
;
666 void (*readable_handler
)(void *private_data
);
667 void *writeable_private
;
668 void (*writeable_handler
)(void *private_data
);
671 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context
*dgram
,
674 struct tdgram_bsd
*bsds
=
675 talloc_get_type(_tdgram_context_data(dgram
),
680 /* not a bsd socket */
684 old
= bsds
->optimize_recvfrom
;
685 bsds
->optimize_recvfrom
= on
;
690 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
691 struct tevent_fd
*fde
,
695 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
698 if (flags
& TEVENT_FD_WRITE
) {
699 bsds
->writeable_handler(bsds
->writeable_private
);
702 if (flags
& TEVENT_FD_READ
) {
703 if (!bsds
->readable_handler
) {
704 TEVENT_FD_NOT_READABLE(bsds
->fde
);
707 bsds
->readable_handler(bsds
->readable_private
);
712 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
713 struct tevent_context
*ev
,
714 void (*handler
)(void *private_data
),
722 if (!bsds
->readable_handler
) {
725 bsds
->readable_handler
= NULL
;
726 bsds
->readable_private
= NULL
;
731 /* read and write must use the same tevent_context */
732 if (bsds
->event_ptr
!= ev
) {
733 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
737 bsds
->event_ptr
= NULL
;
738 TALLOC_FREE(bsds
->fde
);
741 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
742 TALLOC_FREE(bsds
->fde
);
744 bsds
->fde
= tevent_add_fd(ev
, bsds
,
745 bsds
->fd
, TEVENT_FD_READ
,
746 tdgram_bsd_fde_handler
,
753 /* cache the event context we're running on */
754 bsds
->event_ptr
= ev
;
755 } else if (!bsds
->readable_handler
) {
756 TEVENT_FD_READABLE(bsds
->fde
);
759 bsds
->readable_handler
= handler
;
760 bsds
->readable_private
= private_data
;
765 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
766 struct tevent_context
*ev
,
767 void (*handler
)(void *private_data
),
775 if (!bsds
->writeable_handler
) {
778 bsds
->writeable_handler
= NULL
;
779 bsds
->writeable_private
= NULL
;
780 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
785 /* read and write must use the same tevent_context */
786 if (bsds
->event_ptr
!= ev
) {
787 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
791 bsds
->event_ptr
= NULL
;
792 TALLOC_FREE(bsds
->fde
);
795 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
796 TALLOC_FREE(bsds
->fde
);
798 bsds
->fde
= tevent_add_fd(ev
, bsds
,
799 bsds
->fd
, TEVENT_FD_WRITE
,
800 tdgram_bsd_fde_handler
,
807 /* cache the event context we're running on */
808 bsds
->event_ptr
= ev
;
809 } else if (!bsds
->writeable_handler
) {
810 TEVENT_FD_WRITEABLE(bsds
->fde
);
813 bsds
->writeable_handler
= handler
;
814 bsds
->writeable_private
= private_data
;
819 struct tdgram_bsd_recvfrom_state
{
820 struct tdgram_context
*dgram
;
824 struct tsocket_address
*src
;
827 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
829 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
832 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
837 static void tdgram_bsd_recvfrom_handler(void *private_data
);
839 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
840 struct tevent_context
*ev
,
841 struct tdgram_context
*dgram
)
843 struct tevent_req
*req
;
844 struct tdgram_bsd_recvfrom_state
*state
;
845 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
848 req
= tevent_req_create(mem_ctx
, &state
,
849 struct tdgram_bsd_recvfrom_state
);
854 state
->dgram
= dgram
;
855 state
->first_try
= true;
860 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
862 if (bsds
->fd
== -1) {
863 tevent_req_error(req
, ENOTCONN
);
869 * this is a fast path, not waiting for the
870 * socket to become explicit readable gains
871 * about 10%-20% performance in benchmark tests.
873 if (bsds
->optimize_recvfrom
) {
875 * We only do the optimization on
876 * recvfrom if the caller asked for it.
878 * This is needed because in most cases
879 * we preferr to flush send buffers before
880 * receiving incoming requests.
882 tdgram_bsd_recvfrom_handler(req
);
883 if (!tevent_req_is_in_progress(req
)) {
888 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
889 tdgram_bsd_recvfrom_handler
,
892 tevent_req_error(req
, errno
);
899 tevent_req_post(req
, ev
);
903 static void tdgram_bsd_recvfrom_handler(void *private_data
)
905 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
907 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
908 struct tdgram_bsd_recvfrom_state
);
909 struct tdgram_context
*dgram
= state
->dgram
;
910 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
911 struct tsocket_address_bsd
*bsda
;
916 ret
= tsocket_bsd_pending(bsds
->fd
);
917 if (state
->first_try
&& ret
== 0) {
918 state
->first_try
= false;
922 state
->first_try
= false;
924 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
929 if (tevent_req_error(req
, err
)) {
933 /* note that 'ret' can be 0 here */
934 state
->buf
= talloc_array(state
, uint8_t, ret
);
935 if (tevent_req_nomem(state
->buf
, req
)) {
940 state
->src
= tsocket_address_create(state
,
941 &tsocket_address_bsd_ops
,
943 struct tsocket_address_bsd
,
944 __location__
"bsd_recvfrom");
945 if (tevent_req_nomem(state
->src
, req
)) {
950 bsda
->sa_socklen
= sizeof(bsda
->u
.ss
);
951 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
952 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
955 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0,
956 &bsda
->u
.sa
, &bsda
->sa_socklen
);
957 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
962 if (tevent_req_error(req
, err
)) {
967 * Some systems (FreeBSD, see bug #7115) return too much
968 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
969 * the return value includes some IP/UDP header bytes,
970 * while recvfrom() just returns the payload.
972 state
->buf
= talloc_realloc(state
, state
->buf
, uint8_t, ret
);
973 if (tevent_req_nomem(state
->buf
, req
)) {
978 tevent_req_done(req
);
981 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
985 struct tsocket_address
**src
)
987 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
988 struct tdgram_bsd_recvfrom_state
);
991 ret
= tsocket_simple_int_recv(req
, perrno
);
993 *buf
= talloc_move(mem_ctx
, &state
->buf
);
996 *src
= talloc_move(mem_ctx
, &state
->src
);
1000 tevent_req_received(req
);
1004 struct tdgram_bsd_sendto_state
{
1005 struct tdgram_context
*dgram
;
1009 const struct tsocket_address
*dst
;
1014 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
1016 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
1019 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1024 static void tdgram_bsd_sendto_handler(void *private_data
);
1026 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
1027 struct tevent_context
*ev
,
1028 struct tdgram_context
*dgram
,
1031 const struct tsocket_address
*dst
)
1033 struct tevent_req
*req
;
1034 struct tdgram_bsd_sendto_state
*state
;
1035 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1038 req
= tevent_req_create(mem_ctx
, &state
,
1039 struct tdgram_bsd_sendto_state
);
1044 state
->dgram
= dgram
;
1050 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
1052 if (bsds
->fd
== -1) {
1053 tevent_req_error(req
, ENOTCONN
);
1058 * this is a fast path, not waiting for the
1059 * socket to become explicit writeable gains
1060 * about 10%-20% performance in benchmark tests.
1062 tdgram_bsd_sendto_handler(req
);
1063 if (!tevent_req_is_in_progress(req
)) {
1067 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
1068 tdgram_bsd_sendto_handler
,
1071 tevent_req_error(req
, errno
);
1078 tevent_req_post(req
, ev
);
1082 static void tdgram_bsd_sendto_handler(void *private_data
)
1084 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1086 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1087 struct tdgram_bsd_sendto_state
);
1088 struct tdgram_context
*dgram
= state
->dgram
;
1089 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1090 struct sockaddr
*sa
= NULL
;
1091 socklen_t sa_socklen
= 0;
1097 struct tsocket_address_bsd
*bsda
=
1098 talloc_get_type(state
->dst
->private_data
,
1099 struct tsocket_address_bsd
);
1102 sa_socklen
= bsda
->sa_socklen
;
1105 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1106 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1112 if (err
== EMSGSIZE
) {
1113 /* round up in 1K increments */
1114 int bufsize
= ((state
->len
+ 1023) & (~1023));
1116 ret
= setsockopt(bsds
->fd
, SOL_SOCKET
, SO_SNDBUF
, &bufsize
,
1120 * We do the rety here, rather then via the
1121 * handler, as we only want to retry once for
1122 * this condition, so if there is a mismatch
1123 * between what setsockopt() accepts and what can
1124 * actually be sent, we do not end up in a
1128 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
,
1130 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1131 if (retry
) { /* retry later */
1137 if (tevent_req_error(req
, err
)) {
1143 tevent_req_done(req
);
1146 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1148 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1149 struct tdgram_bsd_sendto_state
);
1152 ret
= tsocket_simple_int_recv(req
, perrno
);
1157 tevent_req_received(req
);
1161 struct tdgram_bsd_disconnect_state
{
1165 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1166 struct tevent_context
*ev
,
1167 struct tdgram_context
*dgram
)
1169 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1170 struct tevent_req
*req
;
1171 struct tdgram_bsd_disconnect_state
*state
;
1176 req
= tevent_req_create(mem_ctx
, &state
,
1177 struct tdgram_bsd_disconnect_state
);
1182 if (bsds
->fd
== -1) {
1183 tevent_req_error(req
, ENOTCONN
);
1187 TALLOC_FREE(bsds
->fde
);
1188 ret
= close(bsds
->fd
);
1190 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1191 if (tevent_req_error(req
, err
)) {
1195 tevent_req_done(req
);
1197 tevent_req_post(req
, ev
);
1201 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1206 ret
= tsocket_simple_int_recv(req
, perrno
);
1208 tevent_req_received(req
);
1212 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1215 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1216 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1218 .sendto_send
= tdgram_bsd_sendto_send
,
1219 .sendto_recv
= tdgram_bsd_sendto_recv
,
1221 .disconnect_send
= tdgram_bsd_disconnect_send
,
1222 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1225 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1227 TALLOC_FREE(bsds
->fde
);
1228 if (bsds
->fd
!= -1) {
1235 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1236 const struct tsocket_address
*remote
,
1238 TALLOC_CTX
*mem_ctx
,
1239 struct tdgram_context
**_dgram
,
1240 const char *location
)
1242 struct tsocket_address_bsd
*lbsda
=
1243 talloc_get_type_abort(local
->private_data
,
1244 struct tsocket_address_bsd
);
1245 struct tsocket_address_bsd
*rbsda
= NULL
;
1246 struct tdgram_context
*dgram
;
1247 struct tdgram_bsd
*bsds
;
1250 bool do_bind
= false;
1251 bool do_reuseaddr
= false;
1252 bool do_ipv6only
= false;
1253 bool is_inet
= false;
1254 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1257 rbsda
= talloc_get_type_abort(remote
->private_data
,
1258 struct tsocket_address_bsd
);
1261 switch (lbsda
->u
.sa
.sa_family
) {
1267 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1268 do_reuseaddr
= true;
1273 if (lbsda
->u
.in
.sin_port
!= 0) {
1274 do_reuseaddr
= true;
1277 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1284 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1285 do_reuseaddr
= true;
1288 if (memcmp(&in6addr_any
,
1289 &lbsda
->u
.in6
.sin6_addr
,
1290 sizeof(in6addr_any
)) != 0) {
1302 if (!do_bind
&& is_inet
&& rbsda
) {
1303 sa_fam
= rbsda
->u
.sa
.sa_family
;
1306 do_ipv6only
= false;
1316 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1321 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1326 dgram
= tdgram_context_create(mem_ctx
,
1332 int saved_errno
= errno
;
1334 errno
= saved_errno
;
1339 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1345 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1346 (const void *)&val
, sizeof(val
));
1348 int saved_errno
= errno
;
1350 errno
= saved_errno
;
1359 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1360 (const void *)&val
, sizeof(val
));
1362 int saved_errno
= errno
;
1364 errno
= saved_errno
;
1372 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1373 (const void *)&val
, sizeof(val
));
1375 int saved_errno
= errno
;
1377 errno
= saved_errno
;
1383 ret
= bind(fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
1385 int saved_errno
= errno
;
1387 errno
= saved_errno
;
1393 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1399 ret
= connect(fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
1401 int saved_errno
= errno
;
1403 errno
= saved_errno
;
1412 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1413 const struct tsocket_address
*remote
,
1414 TALLOC_CTX
*mem_ctx
,
1415 struct tdgram_context
**dgram
,
1416 const char *location
)
1418 struct tsocket_address_bsd
*lbsda
=
1419 talloc_get_type_abort(local
->private_data
,
1420 struct tsocket_address_bsd
);
1423 switch (lbsda
->u
.sa
.sa_family
) {
1435 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1436 mem_ctx
, dgram
, location
);
1441 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1442 const struct tsocket_address
*remote
,
1443 TALLOC_CTX
*mem_ctx
,
1444 struct tdgram_context
**dgram
,
1445 const char *location
)
1447 struct tsocket_address_bsd
*lbsda
=
1448 talloc_get_type_abort(local
->private_data
,
1449 struct tsocket_address_bsd
);
1452 switch (lbsda
->u
.sa
.sa_family
) {
1460 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1461 mem_ctx
, dgram
, location
);
1466 struct tstream_bsd
{
1470 struct tevent_fd
*fde
;
1471 bool optimize_readv
;
1473 void *readable_private
;
1474 void (*readable_handler
)(void *private_data
);
1475 void *writeable_private
;
1476 void (*writeable_handler
)(void *private_data
);
1479 bool tstream_bsd_optimize_readv(struct tstream_context
*stream
,
1482 struct tstream_bsd
*bsds
=
1483 talloc_get_type(_tstream_context_data(stream
),
1484 struct tstream_bsd
);
1488 /* not a bsd socket */
1492 old
= bsds
->optimize_readv
;
1493 bsds
->optimize_readv
= on
;
1498 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1499 struct tevent_fd
*fde
,
1503 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1504 struct tstream_bsd
);
1506 if (flags
& TEVENT_FD_WRITE
) {
1507 bsds
->writeable_handler(bsds
->writeable_private
);
1510 if (flags
& TEVENT_FD_READ
) {
1511 if (!bsds
->readable_handler
) {
1512 if (bsds
->writeable_handler
) {
1513 bsds
->writeable_handler(bsds
->writeable_private
);
1516 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1519 bsds
->readable_handler(bsds
->readable_private
);
1524 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1525 struct tevent_context
*ev
,
1526 void (*handler
)(void *private_data
),
1534 if (!bsds
->readable_handler
) {
1537 bsds
->readable_handler
= NULL
;
1538 bsds
->readable_private
= NULL
;
1543 /* read and write must use the same tevent_context */
1544 if (bsds
->event_ptr
!= ev
) {
1545 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1549 bsds
->event_ptr
= NULL
;
1550 TALLOC_FREE(bsds
->fde
);
1553 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1554 TALLOC_FREE(bsds
->fde
);
1556 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1557 bsds
->fd
, TEVENT_FD_READ
,
1558 tstream_bsd_fde_handler
,
1565 /* cache the event context we're running on */
1566 bsds
->event_ptr
= ev
;
1567 } else if (!bsds
->readable_handler
) {
1568 TEVENT_FD_READABLE(bsds
->fde
);
1571 bsds
->readable_handler
= handler
;
1572 bsds
->readable_private
= private_data
;
1577 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1578 struct tevent_context
*ev
,
1579 void (*handler
)(void *private_data
),
1587 if (!bsds
->writeable_handler
) {
1590 bsds
->writeable_handler
= NULL
;
1591 bsds
->writeable_private
= NULL
;
1592 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1597 /* read and write must use the same tevent_context */
1598 if (bsds
->event_ptr
!= ev
) {
1599 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1603 bsds
->event_ptr
= NULL
;
1604 TALLOC_FREE(bsds
->fde
);
1607 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1608 TALLOC_FREE(bsds
->fde
);
1610 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1612 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1613 tstream_bsd_fde_handler
,
1620 /* cache the event context we're running on */
1621 bsds
->event_ptr
= ev
;
1622 } else if (!bsds
->writeable_handler
) {
1623 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1624 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1625 tevent_fd_set_flags(bsds
->fde
, flags
);
1628 bsds
->writeable_handler
= handler
;
1629 bsds
->writeable_private
= private_data
;
1634 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1636 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1637 struct tstream_bsd
);
1640 if (bsds
->fd
== -1) {
1645 ret
= tsocket_bsd_pending(bsds
->fd
);
1650 struct tstream_bsd_readv_state
{
1651 struct tstream_context
*stream
;
1653 struct iovec
*vector
;
1659 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1661 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1662 struct tstream_bsd
);
1664 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1669 static void tstream_bsd_readv_handler(void *private_data
);
1671 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1672 struct tevent_context
*ev
,
1673 struct tstream_context
*stream
,
1674 struct iovec
*vector
,
1677 struct tevent_req
*req
;
1678 struct tstream_bsd_readv_state
*state
;
1679 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1682 req
= tevent_req_create(mem_ctx
, &state
,
1683 struct tstream_bsd_readv_state
);
1688 state
->stream
= stream
;
1689 /* we make a copy of the vector so that we can modify it */
1690 state
->vector
= talloc_array(state
, struct iovec
, count
);
1691 if (tevent_req_nomem(state
->vector
, req
)) {
1694 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1695 state
->count
= count
;
1698 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1700 if (bsds
->fd
== -1) {
1701 tevent_req_error(req
, ENOTCONN
);
1706 * this is a fast path, not waiting for the
1707 * socket to become explicit readable gains
1708 * about 10%-20% performance in benchmark tests.
1710 if (bsds
->optimize_readv
) {
1712 * We only do the optimization on
1713 * readv if the caller asked for it.
1715 * This is needed because in most cases
1716 * we preferr to flush send buffers before
1717 * receiving incoming requests.
1719 tstream_bsd_readv_handler(req
);
1720 if (!tevent_req_is_in_progress(req
)) {
1725 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1726 tstream_bsd_readv_handler
,
1729 tevent_req_error(req
, errno
);
1736 tevent_req_post(req
, ev
);
1740 static void tstream_bsd_readv_handler(void *private_data
)
1742 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1744 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1745 struct tstream_bsd_readv_state
);
1746 struct tstream_context
*stream
= state
->stream
;
1747 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1752 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1754 /* propagate end of file */
1755 tevent_req_error(req
, EPIPE
);
1758 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1763 if (tevent_req_error(req
, err
)) {
1770 if (ret
< state
->vector
[0].iov_len
) {
1772 base
= (uint8_t *)state
->vector
[0].iov_base
;
1774 state
->vector
[0].iov_base
= (void *)base
;
1775 state
->vector
[0].iov_len
-= ret
;
1778 ret
-= state
->vector
[0].iov_len
;
1784 * there're maybe some empty vectors at the end
1785 * which we need to skip, otherwise we would get
1786 * ret == 0 from the readv() call and return EPIPE
1788 while (state
->count
> 0) {
1789 if (state
->vector
[0].iov_len
> 0) {
1796 if (state
->count
> 0) {
1797 /* we have more to read */
1801 tevent_req_done(req
);
1804 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1807 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1808 struct tstream_bsd_readv_state
);
1811 ret
= tsocket_simple_int_recv(req
, perrno
);
1816 tevent_req_received(req
);
1820 struct tstream_bsd_writev_state
{
1821 struct tstream_context
*stream
;
1823 struct iovec
*vector
;
1829 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1831 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1832 struct tstream_bsd
);
1834 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1839 static void tstream_bsd_writev_handler(void *private_data
);
1841 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1842 struct tevent_context
*ev
,
1843 struct tstream_context
*stream
,
1844 const struct iovec
*vector
,
1847 struct tevent_req
*req
;
1848 struct tstream_bsd_writev_state
*state
;
1849 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1852 req
= tevent_req_create(mem_ctx
, &state
,
1853 struct tstream_bsd_writev_state
);
1858 state
->stream
= stream
;
1859 /* we make a copy of the vector so that we can modify it */
1860 state
->vector
= talloc_array(state
, struct iovec
, count
);
1861 if (tevent_req_nomem(state
->vector
, req
)) {
1864 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1865 state
->count
= count
;
1868 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1870 if (bsds
->fd
== -1) {
1871 tevent_req_error(req
, ENOTCONN
);
1876 * this is a fast path, not waiting for the
1877 * socket to become explicit writeable gains
1878 * about 10%-20% performance in benchmark tests.
1880 tstream_bsd_writev_handler(req
);
1881 if (!tevent_req_is_in_progress(req
)) {
1885 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1886 tstream_bsd_writev_handler
,
1889 tevent_req_error(req
, errno
);
1896 tevent_req_post(req
, ev
);
1900 static void tstream_bsd_writev_handler(void *private_data
)
1902 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1904 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1905 struct tstream_bsd_writev_state
);
1906 struct tstream_context
*stream
= state
->stream
;
1907 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1912 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1914 /* propagate end of file */
1915 tevent_req_error(req
, EPIPE
);
1918 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1923 if (tevent_req_error(req
, err
)) {
1930 if (ret
< state
->vector
[0].iov_len
) {
1932 base
= (uint8_t *)state
->vector
[0].iov_base
;
1934 state
->vector
[0].iov_base
= (void *)base
;
1935 state
->vector
[0].iov_len
-= ret
;
1938 ret
-= state
->vector
[0].iov_len
;
1944 * there're maybe some empty vectors at the end
1945 * which we need to skip, otherwise we would get
1946 * ret == 0 from the writev() call and return EPIPE
1948 while (state
->count
> 0) {
1949 if (state
->vector
[0].iov_len
> 0) {
1956 if (state
->count
> 0) {
1957 /* we have more to read */
1961 tevent_req_done(req
);
1964 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1966 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1967 struct tstream_bsd_writev_state
);
1970 ret
= tsocket_simple_int_recv(req
, perrno
);
1975 tevent_req_received(req
);
1979 struct tstream_bsd_disconnect_state
{
1983 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1984 struct tevent_context
*ev
,
1985 struct tstream_context
*stream
)
1987 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1988 struct tevent_req
*req
;
1989 struct tstream_bsd_disconnect_state
*state
;
1994 req
= tevent_req_create(mem_ctx
, &state
,
1995 struct tstream_bsd_disconnect_state
);
2000 if (bsds
->fd
== -1) {
2001 tevent_req_error(req
, ENOTCONN
);
2005 TALLOC_FREE(bsds
->fde
);
2006 ret
= close(bsds
->fd
);
2008 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
2009 if (tevent_req_error(req
, err
)) {
2013 tevent_req_done(req
);
2015 tevent_req_post(req
, ev
);
2019 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
2024 ret
= tsocket_simple_int_recv(req
, perrno
);
2026 tevent_req_received(req
);
2030 static const struct tstream_context_ops tstream_bsd_ops
= {
2033 .pending_bytes
= tstream_bsd_pending_bytes
,
2035 .readv_send
= tstream_bsd_readv_send
,
2036 .readv_recv
= tstream_bsd_readv_recv
,
2038 .writev_send
= tstream_bsd_writev_send
,
2039 .writev_recv
= tstream_bsd_writev_recv
,
2041 .disconnect_send
= tstream_bsd_disconnect_send
,
2042 .disconnect_recv
= tstream_bsd_disconnect_recv
,
2045 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
2047 TALLOC_FREE(bsds
->fde
);
2048 if (bsds
->fd
!= -1) {
2055 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
2057 struct tstream_context
**_stream
,
2058 const char *location
)
2060 struct tstream_context
*stream
;
2061 struct tstream_bsd
*bsds
;
2063 stream
= tstream_context_create(mem_ctx
,
2073 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
2079 struct tstream_bsd_connect_state
{
2081 struct tevent_fd
*fde
;
2082 struct tstream_conext
*stream
;
2083 struct tsocket_address
*local
;
2086 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
2088 TALLOC_FREE(state
->fde
);
2089 if (state
->fd
!= -1) {
2097 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2098 struct tevent_fd
*fde
,
2100 void *private_data
);
2102 static struct tevent_req
*tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
2103 struct tevent_context
*ev
,
2105 const struct tsocket_address
*local
,
2106 const struct tsocket_address
*remote
)
2108 struct tevent_req
*req
;
2109 struct tstream_bsd_connect_state
*state
;
2110 struct tsocket_address_bsd
*lbsda
=
2111 talloc_get_type_abort(local
->private_data
,
2112 struct tsocket_address_bsd
);
2113 struct tsocket_address_bsd
*lrbsda
= NULL
;
2114 struct tsocket_address_bsd
*rbsda
=
2115 talloc_get_type_abort(remote
->private_data
,
2116 struct tsocket_address_bsd
);
2120 bool do_bind
= false;
2121 bool do_reuseaddr
= false;
2122 bool do_ipv6only
= false;
2123 bool is_inet
= false;
2124 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2126 req
= tevent_req_create(mem_ctx
, &state
,
2127 struct tstream_bsd_connect_state
);
2134 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2136 /* give the wrappers a chance to report an error */
2137 if (sys_errno
!= 0) {
2138 tevent_req_error(req
, sys_errno
);
2142 switch (lbsda
->u
.sa
.sa_family
) {
2144 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2145 do_reuseaddr
= true;
2150 if (lbsda
->u
.in
.sin_port
!= 0) {
2151 do_reuseaddr
= true;
2154 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2161 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2162 do_reuseaddr
= true;
2165 if (memcmp(&in6addr_any
,
2166 &lbsda
->u
.in6
.sin6_addr
,
2167 sizeof(in6addr_any
)) != 0) {
2175 tevent_req_error(req
, EINVAL
);
2179 if (!do_bind
&& is_inet
) {
2180 sa_fam
= rbsda
->u
.sa
.sa_family
;
2183 do_ipv6only
= false;
2194 state
->local
= tsocket_address_create(state
,
2195 &tsocket_address_bsd_ops
,
2197 struct tsocket_address_bsd
,
2198 __location__
"bsd_connect");
2199 if (tevent_req_nomem(state
->local
, req
)) {
2203 ZERO_STRUCTP(lrbsda
);
2204 lrbsda
->sa_socklen
= sizeof(lrbsda
->u
.ss
);
2205 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2206 lrbsda
->u
.sa
.sa_len
= lrbsda
->sa_socklen
;
2210 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2211 if (state
->fd
== -1) {
2212 tevent_req_error(req
, errno
);
2216 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2217 if (state
->fd
== -1) {
2218 tevent_req_error(req
, errno
);
2226 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2227 (const void *)&val
, sizeof(val
));
2229 tevent_req_error(req
, errno
);
2238 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2239 (const void *)&val
, sizeof(val
));
2241 tevent_req_error(req
, errno
);
2247 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2249 tevent_req_error(req
, errno
);
2254 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2255 tevent_req_error(req
, EINVAL
);
2259 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2260 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2265 if (tevent_req_error(req
, err
)) {
2269 if (!state
->local
) {
2270 tevent_req_done(req
);
2274 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2276 tevent_req_error(req
, errno
);
2280 tevent_req_done(req
);
2284 state
->fde
= tevent_add_fd(ev
, state
,
2286 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2287 tstream_bsd_connect_fde_handler
,
2289 if (tevent_req_nomem(state
->fde
, req
)) {
2296 tevent_req_post(req
, ev
);
2300 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2301 struct tevent_fd
*fde
,
2305 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2307 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2308 struct tstream_bsd_connect_state
);
2309 struct tsocket_address_bsd
*lrbsda
= NULL
;
2312 socklen_t len
= sizeof(error
);
2316 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2323 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2328 if (tevent_req_error(req
, err
)) {
2332 if (!state
->local
) {
2333 tevent_req_done(req
);
2337 lrbsda
= talloc_get_type_abort(state
->local
->private_data
,
2338 struct tsocket_address_bsd
);
2340 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2342 tevent_req_error(req
, errno
);
2346 tevent_req_done(req
);
2349 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2351 TALLOC_CTX
*mem_ctx
,
2352 struct tstream_context
**stream
,
2353 struct tsocket_address
**local
,
2354 const char *location
)
2356 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2357 struct tstream_bsd_connect_state
);
2360 ret
= tsocket_simple_int_recv(req
, perrno
);
2362 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2370 TALLOC_FREE(state
->fde
);
2374 *local
= talloc_move(mem_ctx
, &state
->local
);
2379 tevent_req_received(req
);
2383 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2384 struct tevent_context
*ev
,
2385 const struct tsocket_address
*local
,
2386 const struct tsocket_address
*remote
)
2388 struct tsocket_address_bsd
*lbsda
=
2389 talloc_get_type_abort(local
->private_data
,
2390 struct tsocket_address_bsd
);
2391 struct tevent_req
*req
;
2394 switch (lbsda
->u
.sa
.sa_family
) {
2406 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2411 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2413 TALLOC_CTX
*mem_ctx
,
2414 struct tstream_context
**stream
,
2415 struct tsocket_address
**local
,
2416 const char *location
)
2418 return tstream_bsd_connect_recv(req
, perrno
,
2419 mem_ctx
, stream
, local
,
2423 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2424 struct tevent_context
*ev
,
2425 const struct tsocket_address
*local
,
2426 const struct tsocket_address
*remote
)
2428 struct tsocket_address_bsd
*lbsda
=
2429 talloc_get_type_abort(local
->private_data
,
2430 struct tsocket_address_bsd
);
2431 struct tevent_req
*req
;
2434 switch (lbsda
->u
.sa
.sa_family
) {
2442 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2447 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2449 TALLOC_CTX
*mem_ctx
,
2450 struct tstream_context
**stream
,
2451 const char *location
)
2453 return tstream_bsd_connect_recv(req
, perrno
,
2454 mem_ctx
, stream
, NULL
,
2458 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2459 struct tstream_context
**_stream1
,
2460 TALLOC_CTX
*mem_ctx2
,
2461 struct tstream_context
**_stream2
,
2462 const char *location
)
2468 struct tstream_context
*stream1
= NULL
;
2469 struct tstream_context
*stream2
= NULL
;
2471 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2478 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2480 int sys_errno
= errno
;
2486 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2488 int sys_errno
= errno
;
2494 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2499 int sys_errno
= errno
;
2506 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2511 int sys_errno
= errno
;
2512 talloc_free(stream1
);
2518 *_stream1
= stream1
;
2519 *_stream2
= stream2
;