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
;
657 bool optimize_recvfrom
;
659 void *readable_private
;
660 void (*readable_handler
)(void *private_data
);
661 void *writeable_private
;
662 void (*writeable_handler
)(void *private_data
);
665 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context
*dgram
,
668 struct tdgram_bsd
*bsds
=
669 talloc_get_type(_tdgram_context_data(dgram
),
674 /* not a bsd socket */
678 old
= bsds
->optimize_recvfrom
;
679 bsds
->optimize_recvfrom
= on
;
684 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
685 struct tevent_fd
*fde
,
689 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
692 if (flags
& TEVENT_FD_WRITE
) {
693 bsds
->writeable_handler(bsds
->writeable_private
);
696 if (flags
& TEVENT_FD_READ
) {
697 if (!bsds
->readable_handler
) {
698 TEVENT_FD_NOT_READABLE(bsds
->fde
);
701 bsds
->readable_handler(bsds
->readable_private
);
706 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
707 struct tevent_context
*ev
,
708 void (*handler
)(void *private_data
),
716 if (!bsds
->readable_handler
) {
719 bsds
->readable_handler
= NULL
;
720 bsds
->readable_private
= NULL
;
725 /* read and write must use the same tevent_context */
726 if (bsds
->event_ptr
!= ev
) {
727 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
731 bsds
->event_ptr
= NULL
;
732 TALLOC_FREE(bsds
->fde
);
735 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
736 TALLOC_FREE(bsds
->fde
);
738 bsds
->fde
= tevent_add_fd(ev
, bsds
,
739 bsds
->fd
, TEVENT_FD_READ
,
740 tdgram_bsd_fde_handler
,
747 /* cache the event context we're running on */
748 bsds
->event_ptr
= ev
;
749 } else if (!bsds
->readable_handler
) {
750 TEVENT_FD_READABLE(bsds
->fde
);
753 bsds
->readable_handler
= handler
;
754 bsds
->readable_private
= private_data
;
759 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
760 struct tevent_context
*ev
,
761 void (*handler
)(void *private_data
),
769 if (!bsds
->writeable_handler
) {
772 bsds
->writeable_handler
= NULL
;
773 bsds
->writeable_private
= NULL
;
774 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
779 /* read and write must use the same tevent_context */
780 if (bsds
->event_ptr
!= ev
) {
781 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
785 bsds
->event_ptr
= NULL
;
786 TALLOC_FREE(bsds
->fde
);
789 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
790 TALLOC_FREE(bsds
->fde
);
792 bsds
->fde
= tevent_add_fd(ev
, bsds
,
793 bsds
->fd
, TEVENT_FD_WRITE
,
794 tdgram_bsd_fde_handler
,
801 /* cache the event context we're running on */
802 bsds
->event_ptr
= ev
;
803 } else if (!bsds
->writeable_handler
) {
804 TEVENT_FD_WRITEABLE(bsds
->fde
);
807 bsds
->writeable_handler
= handler
;
808 bsds
->writeable_private
= private_data
;
813 struct tdgram_bsd_recvfrom_state
{
814 struct tdgram_context
*dgram
;
818 struct tsocket_address
*src
;
821 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
823 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
826 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
831 static void tdgram_bsd_recvfrom_handler(void *private_data
);
833 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
834 struct tevent_context
*ev
,
835 struct tdgram_context
*dgram
)
837 struct tevent_req
*req
;
838 struct tdgram_bsd_recvfrom_state
*state
;
839 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
842 req
= tevent_req_create(mem_ctx
, &state
,
843 struct tdgram_bsd_recvfrom_state
);
848 state
->dgram
= dgram
;
849 state
->first_try
= true;
854 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
856 if (bsds
->fd
== -1) {
857 tevent_req_error(req
, ENOTCONN
);
863 * this is a fast path, not waiting for the
864 * socket to become explicit readable gains
865 * about 10%-20% performance in benchmark tests.
867 if (bsds
->optimize_recvfrom
) {
869 * We only do the optimization on
870 * recvfrom if the caller asked for it.
872 * This is needed because in most cases
873 * we preferr to flush send buffers before
874 * receiving incoming requests.
876 tdgram_bsd_recvfrom_handler(req
);
877 if (!tevent_req_is_in_progress(req
)) {
882 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
883 tdgram_bsd_recvfrom_handler
,
886 tevent_req_error(req
, errno
);
893 tevent_req_post(req
, ev
);
897 static void tdgram_bsd_recvfrom_handler(void *private_data
)
899 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
901 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
902 struct tdgram_bsd_recvfrom_state
);
903 struct tdgram_context
*dgram
= state
->dgram
;
904 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
905 struct tsocket_address_bsd
*bsda
;
910 ret
= tsocket_bsd_pending(bsds
->fd
);
911 if (state
->first_try
&& ret
== 0) {
912 state
->first_try
= false;
916 state
->first_try
= false;
918 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
923 if (tevent_req_error(req
, err
)) {
927 /* note that 'ret' can be 0 here */
928 state
->buf
= talloc_array(state
, uint8_t, ret
);
929 if (tevent_req_nomem(state
->buf
, req
)) {
934 state
->src
= tsocket_address_create(state
,
935 &tsocket_address_bsd_ops
,
937 struct tsocket_address_bsd
,
938 __location__
"bsd_recvfrom");
939 if (tevent_req_nomem(state
->src
, req
)) {
944 bsda
->sa_socklen
= sizeof(bsda
->u
.ss
);
945 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
946 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
949 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0,
950 &bsda
->u
.sa
, &bsda
->sa_socklen
);
951 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
956 if (tevent_req_error(req
, err
)) {
961 * Some systems (FreeBSD, see bug #7115) return too much
962 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
963 * the return value includes some IP/UDP header bytes,
964 * while recvfrom() just returns the payload.
966 state
->buf
= talloc_realloc(state
, state
->buf
, uint8_t, ret
);
967 if (tevent_req_nomem(state
->buf
, req
)) {
972 tevent_req_done(req
);
975 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
979 struct tsocket_address
**src
)
981 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
982 struct tdgram_bsd_recvfrom_state
);
985 ret
= tsocket_simple_int_recv(req
, perrno
);
987 *buf
= talloc_move(mem_ctx
, &state
->buf
);
990 *src
= talloc_move(mem_ctx
, &state
->src
);
994 tevent_req_received(req
);
998 struct tdgram_bsd_sendto_state
{
999 struct tdgram_context
*dgram
;
1003 const struct tsocket_address
*dst
;
1008 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
1010 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
1013 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1018 static void tdgram_bsd_sendto_handler(void *private_data
);
1020 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
1021 struct tevent_context
*ev
,
1022 struct tdgram_context
*dgram
,
1025 const struct tsocket_address
*dst
)
1027 struct tevent_req
*req
;
1028 struct tdgram_bsd_sendto_state
*state
;
1029 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1032 req
= tevent_req_create(mem_ctx
, &state
,
1033 struct tdgram_bsd_sendto_state
);
1038 state
->dgram
= dgram
;
1044 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
1046 if (bsds
->fd
== -1) {
1047 tevent_req_error(req
, ENOTCONN
);
1052 * this is a fast path, not waiting for the
1053 * socket to become explicit writeable gains
1054 * about 10%-20% performance in benchmark tests.
1056 tdgram_bsd_sendto_handler(req
);
1057 if (!tevent_req_is_in_progress(req
)) {
1061 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
1062 tdgram_bsd_sendto_handler
,
1065 tevent_req_error(req
, errno
);
1072 tevent_req_post(req
, ev
);
1076 static void tdgram_bsd_sendto_handler(void *private_data
)
1078 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1080 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1081 struct tdgram_bsd_sendto_state
);
1082 struct tdgram_context
*dgram
= state
->dgram
;
1083 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1084 struct sockaddr
*sa
= NULL
;
1085 socklen_t sa_socklen
= 0;
1091 struct tsocket_address_bsd
*bsda
=
1092 talloc_get_type(state
->dst
->private_data
,
1093 struct tsocket_address_bsd
);
1096 sa_socklen
= bsda
->sa_socklen
;
1099 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1100 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1105 if (tevent_req_error(req
, err
)) {
1111 tevent_req_done(req
);
1114 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1116 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1117 struct tdgram_bsd_sendto_state
);
1120 ret
= tsocket_simple_int_recv(req
, perrno
);
1125 tevent_req_received(req
);
1129 struct tdgram_bsd_disconnect_state
{
1133 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1134 struct tevent_context
*ev
,
1135 struct tdgram_context
*dgram
)
1137 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1138 struct tevent_req
*req
;
1139 struct tdgram_bsd_disconnect_state
*state
;
1144 req
= tevent_req_create(mem_ctx
, &state
,
1145 struct tdgram_bsd_disconnect_state
);
1150 if (bsds
->fd
== -1) {
1151 tevent_req_error(req
, ENOTCONN
);
1155 TALLOC_FREE(bsds
->fde
);
1156 ret
= close(bsds
->fd
);
1158 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1159 if (tevent_req_error(req
, err
)) {
1163 tevent_req_done(req
);
1165 tevent_req_post(req
, ev
);
1169 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1174 ret
= tsocket_simple_int_recv(req
, perrno
);
1176 tevent_req_received(req
);
1180 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1183 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1184 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1186 .sendto_send
= tdgram_bsd_sendto_send
,
1187 .sendto_recv
= tdgram_bsd_sendto_recv
,
1189 .disconnect_send
= tdgram_bsd_disconnect_send
,
1190 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1193 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1195 TALLOC_FREE(bsds
->fde
);
1196 if (bsds
->fd
!= -1) {
1203 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1204 const struct tsocket_address
*remote
,
1206 TALLOC_CTX
*mem_ctx
,
1207 struct tdgram_context
**_dgram
,
1208 const char *location
)
1210 struct tsocket_address_bsd
*lbsda
=
1211 talloc_get_type_abort(local
->private_data
,
1212 struct tsocket_address_bsd
);
1213 struct tsocket_address_bsd
*rbsda
= NULL
;
1214 struct tdgram_context
*dgram
;
1215 struct tdgram_bsd
*bsds
;
1218 bool do_bind
= false;
1219 bool do_reuseaddr
= false;
1220 bool do_ipv6only
= false;
1221 bool is_inet
= false;
1222 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1225 rbsda
= talloc_get_type_abort(remote
->private_data
,
1226 struct tsocket_address_bsd
);
1229 switch (lbsda
->u
.sa
.sa_family
) {
1235 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1236 do_reuseaddr
= true;
1241 if (lbsda
->u
.in
.sin_port
!= 0) {
1242 do_reuseaddr
= true;
1245 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1252 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1253 do_reuseaddr
= true;
1256 if (memcmp(&in6addr_any
,
1257 &lbsda
->u
.in6
.sin6_addr
,
1258 sizeof(in6addr_any
)) != 0) {
1270 if (!do_bind
&& is_inet
&& rbsda
) {
1271 sa_fam
= rbsda
->u
.sa
.sa_family
;
1274 do_ipv6only
= false;
1284 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1289 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1294 dgram
= tdgram_context_create(mem_ctx
,
1300 int saved_errno
= errno
;
1302 errno
= saved_errno
;
1307 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1313 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1314 (const void *)&val
, sizeof(val
));
1316 int saved_errno
= errno
;
1318 errno
= saved_errno
;
1327 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1328 (const void *)&val
, sizeof(val
));
1330 int saved_errno
= errno
;
1332 errno
= saved_errno
;
1340 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1341 (const void *)&val
, sizeof(val
));
1343 int saved_errno
= errno
;
1345 errno
= saved_errno
;
1351 ret
= bind(fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
1353 int saved_errno
= errno
;
1355 errno
= saved_errno
;
1361 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1367 ret
= connect(fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
1369 int saved_errno
= errno
;
1371 errno
= saved_errno
;
1380 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1381 const struct tsocket_address
*remote
,
1382 TALLOC_CTX
*mem_ctx
,
1383 struct tdgram_context
**dgram
,
1384 const char *location
)
1386 struct tsocket_address_bsd
*lbsda
=
1387 talloc_get_type_abort(local
->private_data
,
1388 struct tsocket_address_bsd
);
1391 switch (lbsda
->u
.sa
.sa_family
) {
1403 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1404 mem_ctx
, dgram
, location
);
1409 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1410 const struct tsocket_address
*remote
,
1411 TALLOC_CTX
*mem_ctx
,
1412 struct tdgram_context
**dgram
,
1413 const char *location
)
1415 struct tsocket_address_bsd
*lbsda
=
1416 talloc_get_type_abort(local
->private_data
,
1417 struct tsocket_address_bsd
);
1420 switch (lbsda
->u
.sa
.sa_family
) {
1428 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1429 mem_ctx
, dgram
, location
);
1434 struct tstream_bsd
{
1438 struct tevent_fd
*fde
;
1439 bool optimize_readv
;
1441 void *readable_private
;
1442 void (*readable_handler
)(void *private_data
);
1443 void *writeable_private
;
1444 void (*writeable_handler
)(void *private_data
);
1447 bool tstream_bsd_optimize_readv(struct tstream_context
*stream
,
1450 struct tstream_bsd
*bsds
=
1451 talloc_get_type(_tstream_context_data(stream
),
1452 struct tstream_bsd
);
1456 /* not a bsd socket */
1460 old
= bsds
->optimize_readv
;
1461 bsds
->optimize_readv
= on
;
1466 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1467 struct tevent_fd
*fde
,
1471 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1472 struct tstream_bsd
);
1474 if (flags
& TEVENT_FD_WRITE
) {
1475 bsds
->writeable_handler(bsds
->writeable_private
);
1478 if (flags
& TEVENT_FD_READ
) {
1479 if (!bsds
->readable_handler
) {
1480 if (bsds
->writeable_handler
) {
1481 bsds
->writeable_handler(bsds
->writeable_private
);
1484 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1487 bsds
->readable_handler(bsds
->readable_private
);
1492 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1493 struct tevent_context
*ev
,
1494 void (*handler
)(void *private_data
),
1502 if (!bsds
->readable_handler
) {
1505 bsds
->readable_handler
= NULL
;
1506 bsds
->readable_private
= NULL
;
1511 /* read and write must use the same tevent_context */
1512 if (bsds
->event_ptr
!= ev
) {
1513 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1517 bsds
->event_ptr
= NULL
;
1518 TALLOC_FREE(bsds
->fde
);
1521 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1522 TALLOC_FREE(bsds
->fde
);
1524 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1525 bsds
->fd
, TEVENT_FD_READ
,
1526 tstream_bsd_fde_handler
,
1533 /* cache the event context we're running on */
1534 bsds
->event_ptr
= ev
;
1535 } else if (!bsds
->readable_handler
) {
1536 TEVENT_FD_READABLE(bsds
->fde
);
1539 bsds
->readable_handler
= handler
;
1540 bsds
->readable_private
= private_data
;
1545 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1546 struct tevent_context
*ev
,
1547 void (*handler
)(void *private_data
),
1555 if (!bsds
->writeable_handler
) {
1558 bsds
->writeable_handler
= NULL
;
1559 bsds
->writeable_private
= NULL
;
1560 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1565 /* read and write must use the same tevent_context */
1566 if (bsds
->event_ptr
!= ev
) {
1567 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1571 bsds
->event_ptr
= NULL
;
1572 TALLOC_FREE(bsds
->fde
);
1575 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1576 TALLOC_FREE(bsds
->fde
);
1578 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1580 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1581 tstream_bsd_fde_handler
,
1588 /* cache the event context we're running on */
1589 bsds
->event_ptr
= ev
;
1590 } else if (!bsds
->writeable_handler
) {
1591 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1592 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1593 tevent_fd_set_flags(bsds
->fde
, flags
);
1596 bsds
->writeable_handler
= handler
;
1597 bsds
->writeable_private
= private_data
;
1602 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1604 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1605 struct tstream_bsd
);
1608 if (bsds
->fd
== -1) {
1613 ret
= tsocket_bsd_pending(bsds
->fd
);
1618 struct tstream_bsd_readv_state
{
1619 struct tstream_context
*stream
;
1621 struct iovec
*vector
;
1627 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1629 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1630 struct tstream_bsd
);
1632 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1637 static void tstream_bsd_readv_handler(void *private_data
);
1639 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1640 struct tevent_context
*ev
,
1641 struct tstream_context
*stream
,
1642 struct iovec
*vector
,
1645 struct tevent_req
*req
;
1646 struct tstream_bsd_readv_state
*state
;
1647 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1650 req
= tevent_req_create(mem_ctx
, &state
,
1651 struct tstream_bsd_readv_state
);
1656 state
->stream
= stream
;
1657 /* we make a copy of the vector so that we can modify it */
1658 state
->vector
= talloc_array(state
, struct iovec
, count
);
1659 if (tevent_req_nomem(state
->vector
, req
)) {
1662 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1663 state
->count
= count
;
1666 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1668 if (bsds
->fd
== -1) {
1669 tevent_req_error(req
, ENOTCONN
);
1674 * this is a fast path, not waiting for the
1675 * socket to become explicit readable gains
1676 * about 10%-20% performance in benchmark tests.
1678 if (bsds
->optimize_readv
) {
1680 * We only do the optimization on
1681 * readv if the caller asked for it.
1683 * This is needed because in most cases
1684 * we preferr to flush send buffers before
1685 * receiving incoming requests.
1687 tstream_bsd_readv_handler(req
);
1688 if (!tevent_req_is_in_progress(req
)) {
1693 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1694 tstream_bsd_readv_handler
,
1697 tevent_req_error(req
, errno
);
1704 tevent_req_post(req
, ev
);
1708 static void tstream_bsd_readv_handler(void *private_data
)
1710 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1712 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1713 struct tstream_bsd_readv_state
);
1714 struct tstream_context
*stream
= state
->stream
;
1715 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1720 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1722 /* propagate end of file */
1723 tevent_req_error(req
, EPIPE
);
1726 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1731 if (tevent_req_error(req
, err
)) {
1738 if (ret
< state
->vector
[0].iov_len
) {
1740 base
= (uint8_t *)state
->vector
[0].iov_base
;
1742 state
->vector
[0].iov_base
= (void *)base
;
1743 state
->vector
[0].iov_len
-= ret
;
1746 ret
-= state
->vector
[0].iov_len
;
1752 * there're maybe some empty vectors at the end
1753 * which we need to skip, otherwise we would get
1754 * ret == 0 from the readv() call and return EPIPE
1756 while (state
->count
> 0) {
1757 if (state
->vector
[0].iov_len
> 0) {
1764 if (state
->count
> 0) {
1765 /* we have more to read */
1769 tevent_req_done(req
);
1772 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1775 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1776 struct tstream_bsd_readv_state
);
1779 ret
= tsocket_simple_int_recv(req
, perrno
);
1784 tevent_req_received(req
);
1788 struct tstream_bsd_writev_state
{
1789 struct tstream_context
*stream
;
1791 struct iovec
*vector
;
1797 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1799 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1800 struct tstream_bsd
);
1802 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1807 static void tstream_bsd_writev_handler(void *private_data
);
1809 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1810 struct tevent_context
*ev
,
1811 struct tstream_context
*stream
,
1812 const struct iovec
*vector
,
1815 struct tevent_req
*req
;
1816 struct tstream_bsd_writev_state
*state
;
1817 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1820 req
= tevent_req_create(mem_ctx
, &state
,
1821 struct tstream_bsd_writev_state
);
1826 state
->stream
= stream
;
1827 /* we make a copy of the vector so that we can modify it */
1828 state
->vector
= talloc_array(state
, struct iovec
, count
);
1829 if (tevent_req_nomem(state
->vector
, req
)) {
1832 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1833 state
->count
= count
;
1836 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1838 if (bsds
->fd
== -1) {
1839 tevent_req_error(req
, ENOTCONN
);
1844 * this is a fast path, not waiting for the
1845 * socket to become explicit writeable gains
1846 * about 10%-20% performance in benchmark tests.
1848 tstream_bsd_writev_handler(req
);
1849 if (!tevent_req_is_in_progress(req
)) {
1853 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1854 tstream_bsd_writev_handler
,
1857 tevent_req_error(req
, errno
);
1864 tevent_req_post(req
, ev
);
1868 static void tstream_bsd_writev_handler(void *private_data
)
1870 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1872 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1873 struct tstream_bsd_writev_state
);
1874 struct tstream_context
*stream
= state
->stream
;
1875 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1880 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1882 /* propagate end of file */
1883 tevent_req_error(req
, EPIPE
);
1886 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1891 if (tevent_req_error(req
, err
)) {
1898 if (ret
< state
->vector
[0].iov_len
) {
1900 base
= (uint8_t *)state
->vector
[0].iov_base
;
1902 state
->vector
[0].iov_base
= (void *)base
;
1903 state
->vector
[0].iov_len
-= ret
;
1906 ret
-= state
->vector
[0].iov_len
;
1912 * there're maybe some empty vectors at the end
1913 * which we need to skip, otherwise we would get
1914 * ret == 0 from the writev() call and return EPIPE
1916 while (state
->count
> 0) {
1917 if (state
->vector
[0].iov_len
> 0) {
1924 if (state
->count
> 0) {
1925 /* we have more to read */
1929 tevent_req_done(req
);
1932 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1934 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1935 struct tstream_bsd_writev_state
);
1938 ret
= tsocket_simple_int_recv(req
, perrno
);
1943 tevent_req_received(req
);
1947 struct tstream_bsd_disconnect_state
{
1951 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1952 struct tevent_context
*ev
,
1953 struct tstream_context
*stream
)
1955 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1956 struct tevent_req
*req
;
1957 struct tstream_bsd_disconnect_state
*state
;
1962 req
= tevent_req_create(mem_ctx
, &state
,
1963 struct tstream_bsd_disconnect_state
);
1968 if (bsds
->fd
== -1) {
1969 tevent_req_error(req
, ENOTCONN
);
1973 TALLOC_FREE(bsds
->fde
);
1974 ret
= close(bsds
->fd
);
1976 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1977 if (tevent_req_error(req
, err
)) {
1981 tevent_req_done(req
);
1983 tevent_req_post(req
, ev
);
1987 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1992 ret
= tsocket_simple_int_recv(req
, perrno
);
1994 tevent_req_received(req
);
1998 static const struct tstream_context_ops tstream_bsd_ops
= {
2001 .pending_bytes
= tstream_bsd_pending_bytes
,
2003 .readv_send
= tstream_bsd_readv_send
,
2004 .readv_recv
= tstream_bsd_readv_recv
,
2006 .writev_send
= tstream_bsd_writev_send
,
2007 .writev_recv
= tstream_bsd_writev_recv
,
2009 .disconnect_send
= tstream_bsd_disconnect_send
,
2010 .disconnect_recv
= tstream_bsd_disconnect_recv
,
2013 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
2015 TALLOC_FREE(bsds
->fde
);
2016 if (bsds
->fd
!= -1) {
2023 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
2025 struct tstream_context
**_stream
,
2026 const char *location
)
2028 struct tstream_context
*stream
;
2029 struct tstream_bsd
*bsds
;
2031 stream
= tstream_context_create(mem_ctx
,
2041 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
2047 struct tstream_bsd_connect_state
{
2049 struct tevent_fd
*fde
;
2050 struct tstream_conext
*stream
;
2051 struct tsocket_address
*local
;
2054 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
2056 TALLOC_FREE(state
->fde
);
2057 if (state
->fd
!= -1) {
2065 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2066 struct tevent_fd
*fde
,
2068 void *private_data
);
2070 static struct tevent_req
*tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
2071 struct tevent_context
*ev
,
2073 const struct tsocket_address
*local
,
2074 const struct tsocket_address
*remote
)
2076 struct tevent_req
*req
;
2077 struct tstream_bsd_connect_state
*state
;
2078 struct tsocket_address_bsd
*lbsda
=
2079 talloc_get_type_abort(local
->private_data
,
2080 struct tsocket_address_bsd
);
2081 struct tsocket_address_bsd
*lrbsda
= NULL
;
2082 struct tsocket_address_bsd
*rbsda
=
2083 talloc_get_type_abort(remote
->private_data
,
2084 struct tsocket_address_bsd
);
2088 bool do_bind
= false;
2089 bool do_reuseaddr
= false;
2090 bool do_ipv6only
= false;
2091 bool is_inet
= false;
2092 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2094 req
= tevent_req_create(mem_ctx
, &state
,
2095 struct tstream_bsd_connect_state
);
2102 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2104 /* give the wrappers a chance to report an error */
2105 if (sys_errno
!= 0) {
2106 tevent_req_error(req
, sys_errno
);
2110 switch (lbsda
->u
.sa
.sa_family
) {
2112 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2113 do_reuseaddr
= true;
2118 if (lbsda
->u
.in
.sin_port
!= 0) {
2119 do_reuseaddr
= true;
2122 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2129 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2130 do_reuseaddr
= true;
2133 if (memcmp(&in6addr_any
,
2134 &lbsda
->u
.in6
.sin6_addr
,
2135 sizeof(in6addr_any
)) != 0) {
2143 tevent_req_error(req
, EINVAL
);
2147 if (!do_bind
&& is_inet
) {
2148 sa_fam
= rbsda
->u
.sa
.sa_family
;
2151 do_ipv6only
= false;
2162 state
->local
= tsocket_address_create(state
,
2163 &tsocket_address_bsd_ops
,
2165 struct tsocket_address_bsd
,
2166 __location__
"bsd_connect");
2167 if (tevent_req_nomem(state
->local
, req
)) {
2171 ZERO_STRUCTP(lrbsda
);
2172 lrbsda
->sa_socklen
= sizeof(lrbsda
->u
.ss
);
2173 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2174 lrbsda
->u
.sa
.sa_len
= lrbsda
->sa_socklen
;
2178 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2179 if (state
->fd
== -1) {
2180 tevent_req_error(req
, errno
);
2184 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2185 if (state
->fd
== -1) {
2186 tevent_req_error(req
, errno
);
2194 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2195 (const void *)&val
, sizeof(val
));
2197 tevent_req_error(req
, errno
);
2206 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2207 (const void *)&val
, sizeof(val
));
2209 tevent_req_error(req
, errno
);
2215 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2217 tevent_req_error(req
, errno
);
2222 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2223 tevent_req_error(req
, EINVAL
);
2227 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2228 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2233 if (tevent_req_error(req
, err
)) {
2237 if (!state
->local
) {
2238 tevent_req_done(req
);
2242 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2244 tevent_req_error(req
, errno
);
2248 tevent_req_done(req
);
2252 state
->fde
= tevent_add_fd(ev
, state
,
2254 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2255 tstream_bsd_connect_fde_handler
,
2257 if (tevent_req_nomem(state
->fde
, req
)) {
2264 tevent_req_post(req
, ev
);
2268 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2269 struct tevent_fd
*fde
,
2273 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2275 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2276 struct tstream_bsd_connect_state
);
2277 struct tsocket_address_bsd
*lrbsda
= NULL
;
2280 socklen_t len
= sizeof(error
);
2284 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2291 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2296 if (tevent_req_error(req
, err
)) {
2300 if (!state
->local
) {
2301 tevent_req_done(req
);
2305 lrbsda
= talloc_get_type_abort(state
->local
->private_data
,
2306 struct tsocket_address_bsd
);
2308 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2310 tevent_req_error(req
, errno
);
2314 tevent_req_done(req
);
2317 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2319 TALLOC_CTX
*mem_ctx
,
2320 struct tstream_context
**stream
,
2321 struct tsocket_address
**local
,
2322 const char *location
)
2324 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2325 struct tstream_bsd_connect_state
);
2328 ret
= tsocket_simple_int_recv(req
, perrno
);
2330 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2338 TALLOC_FREE(state
->fde
);
2342 *local
= talloc_move(mem_ctx
, &state
->local
);
2347 tevent_req_received(req
);
2351 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2352 struct tevent_context
*ev
,
2353 const struct tsocket_address
*local
,
2354 const struct tsocket_address
*remote
)
2356 struct tsocket_address_bsd
*lbsda
=
2357 talloc_get_type_abort(local
->private_data
,
2358 struct tsocket_address_bsd
);
2359 struct tevent_req
*req
;
2362 switch (lbsda
->u
.sa
.sa_family
) {
2374 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2379 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2381 TALLOC_CTX
*mem_ctx
,
2382 struct tstream_context
**stream
,
2383 struct tsocket_address
**local
,
2384 const char *location
)
2386 return tstream_bsd_connect_recv(req
, perrno
,
2387 mem_ctx
, stream
, local
,
2391 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2392 struct tevent_context
*ev
,
2393 const struct tsocket_address
*local
,
2394 const struct tsocket_address
*remote
)
2396 struct tsocket_address_bsd
*lbsda
=
2397 talloc_get_type_abort(local
->private_data
,
2398 struct tsocket_address_bsd
);
2399 struct tevent_req
*req
;
2402 switch (lbsda
->u
.sa
.sa_family
) {
2410 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2415 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2417 TALLOC_CTX
*mem_ctx
,
2418 struct tstream_context
**stream
,
2419 const char *location
)
2421 return tstream_bsd_connect_recv(req
, perrno
,
2422 mem_ctx
, stream
, NULL
,
2426 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2427 struct tstream_context
**_stream1
,
2428 TALLOC_CTX
*mem_ctx2
,
2429 struct tstream_context
**_stream2
,
2430 const char *location
)
2436 struct tstream_context
*stream1
= NULL
;
2437 struct tstream_context
*stream2
= NULL
;
2439 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2446 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2448 int sys_errno
= errno
;
2454 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2456 int sys_errno
= errno
;
2462 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2467 int sys_errno
= errno
;
2474 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2479 int sys_errno
= errno
;
2480 talloc_free(stream1
);
2486 *_stream1
= stream1
;
2487 *_stream2
= stream2
;