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"
29 #include "lib/util/iov_buf.h"
30 #include "lib/util/blocking.h"
32 static int tsocket_bsd_error_from_errno(int ret
,
50 if (sys_errno
== EINTR
) {
55 if (sys_errno
== EINPROGRESS
) {
60 if (sys_errno
== EAGAIN
) {
65 /* ENOMEM is retryable on Solaris/illumos, and possibly other systems. */
66 if (sys_errno
== ENOMEM
) {
72 if (sys_errno
== EWOULDBLOCK
) {
81 static int tsocket_bsd_common_prepare_fd(int fd
, bool high_fd
)
95 /* first make a fd >= 3 */
105 for (i
=0; i
<num_fds
; i
++) {
114 result
= set_blocking(fd
, false);
119 ok
= smb_set_close_on_exec(fd
);
135 static ssize_t
tsocket_bsd_pending(int fd
)
141 ret
= ioctl(fd
, FIONREAD
, &value
);
147 /* this should not be reached */
160 * if no data is available check if the socket is in error state. For
161 * dgram sockets it's the way to return ICMP error messages of
162 * connected sockets to the caller.
164 ret
= getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
175 static const struct tsocket_address_ops tsocket_address_bsd_ops
;
177 struct tsocket_address_bsd
{
178 socklen_t sa_socklen
;
181 struct sockaddr_in in
;
183 struct sockaddr_in6 in6
;
185 struct sockaddr_un un
;
186 struct sockaddr_storage ss
;
190 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
191 const struct sockaddr
*sa
,
193 struct tsocket_address
**_addr
,
194 const char *location
)
196 struct tsocket_address
*addr
;
197 struct tsocket_address_bsd
*bsda
;
199 if (sa_socklen
< sizeof(sa
->sa_family
)) {
204 switch (sa
->sa_family
) {
206 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
207 sa_socklen
= sizeof(struct sockaddr_un
);
211 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
215 sa_socklen
= sizeof(struct sockaddr_in
);
219 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
223 sa_socklen
= sizeof(struct sockaddr_in6
);
227 errno
= EAFNOSUPPORT
;
231 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
236 addr
= tsocket_address_create(mem_ctx
,
237 &tsocket_address_bsd_ops
,
239 struct tsocket_address_bsd
,
248 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
250 bsda
->sa_socklen
= sa_socklen
;
251 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
252 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
259 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
263 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
264 struct tsocket_address_bsd
);
271 if (sa_socklen
< bsda
->sa_socklen
) {
276 if (sa_socklen
> bsda
->sa_socklen
) {
277 memset(sa
, 0, sa_socklen
);
278 sa_socklen
= bsda
->sa_socklen
;
281 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
282 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
283 sa
->sa_len
= sa_socklen
;
288 bool tsocket_address_is_inet(const struct tsocket_address
*addr
, const char *fam
)
290 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
291 struct tsocket_address_bsd
);
297 switch (bsda
->u
.sa
.sa_family
) {
299 if (strcasecmp(fam
, "ip") == 0) {
303 if (strcasecmp(fam
, "ipv4") == 0) {
310 if (strcasecmp(fam
, "ip") == 0) {
314 if (strcasecmp(fam
, "ipv6") == 0) {
325 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
329 struct tsocket_address
**_addr
,
330 const char *location
)
332 struct addrinfo hints
;
333 struct addrinfo
*result
= NULL
;
339 * we use SOCKET_STREAM here to get just one result
340 * back from getaddrinfo().
342 hints
.ai_socktype
= SOCK_STREAM
;
343 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
345 if (strcasecmp(fam
, "ip") == 0) {
346 hints
.ai_family
= AF_UNSPEC
;
354 } else if (strcasecmp(fam
, "ipv4") == 0) {
355 hints
.ai_family
= AF_INET
;
360 } else if (strcasecmp(fam
, "ipv6") == 0) {
361 hints
.ai_family
= AF_INET6
;
367 errno
= EAFNOSUPPORT
;
371 snprintf(port_str
, sizeof(port_str
), "%u", port
);
373 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
384 if (result
->ai_socktype
!= SOCK_STREAM
) {
390 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
398 freeaddrinfo(result
);
403 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
406 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
407 struct tsocket_address_bsd
);
408 char addr_str
[INET6_ADDRSTRLEN
+1];
416 switch (bsda
->u
.sa
.sa_family
) {
418 str
= inet_ntop(bsda
->u
.in
.sin_family
,
419 &bsda
->u
.in
.sin_addr
,
420 addr_str
, sizeof(addr_str
));
424 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
425 &bsda
->u
.in6
.sin6_addr
,
426 addr_str
, sizeof(addr_str
));
438 return talloc_strdup(mem_ctx
, str
);
441 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
443 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
444 struct tsocket_address_bsd
);
452 switch (bsda
->u
.sa
.sa_family
) {
454 port
= ntohs(bsda
->u
.in
.sin_port
);
458 port
= ntohs(bsda
->u
.in6
.sin6_port
);
469 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
472 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
473 struct tsocket_address_bsd
);
480 switch (bsda
->u
.sa
.sa_family
) {
482 bsda
->u
.in
.sin_port
= htons(port
);
486 bsda
->u
.in6
.sin6_port
= htons(port
);
497 bool tsocket_address_is_unix(const struct tsocket_address
*addr
)
499 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
500 struct tsocket_address_bsd
);
506 switch (bsda
->u
.sa
.sa_family
) {
514 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
516 struct tsocket_address
**_addr
,
517 const char *location
)
519 struct sockaddr_un un
;
527 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
528 errno
= ENAMETOOLONG
;
533 un
.sun_family
= AF_UNIX
;
534 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
536 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
537 (struct sockaddr
*)p
,
545 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
548 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
549 struct tsocket_address_bsd
);
557 switch (bsda
->u
.sa
.sa_family
) {
559 str
= bsda
->u
.un
.sun_path
;
566 return talloc_strdup(mem_ctx
, str
);
569 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
572 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
573 struct tsocket_address_bsd
);
576 const char *prefix
= NULL
;
579 switch (bsda
->u
.sa
.sa_family
) {
581 return talloc_asprintf(mem_ctx
, "unix:%s",
582 bsda
->u
.un
.sun_path
);
596 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
601 port
= tsocket_address_inet_port(addr
);
603 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
604 prefix
, addr_str
, port
);
605 talloc_free(addr_str
);
610 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
612 const char *location
)
614 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
615 struct tsocket_address_bsd
);
616 struct tsocket_address
*copy
;
619 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
631 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
633 .string
= tsocket_address_bsd_string
,
634 .copy
= tsocket_address_bsd_copy
,
641 struct tevent_fd
*fde
;
642 bool optimize_recvfrom
;
644 void *readable_private
;
645 void (*readable_handler
)(void *private_data
);
646 void *writeable_private
;
647 void (*writeable_handler
)(void *private_data
);
650 bool tdgram_bsd_optimize_recvfrom(struct tdgram_context
*dgram
,
653 struct tdgram_bsd
*bsds
=
654 talloc_get_type(_tdgram_context_data(dgram
),
659 /* not a bsd socket */
663 old
= bsds
->optimize_recvfrom
;
664 bsds
->optimize_recvfrom
= on
;
669 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
670 struct tevent_fd
*fde
,
674 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
677 if (flags
& TEVENT_FD_WRITE
) {
678 bsds
->writeable_handler(bsds
->writeable_private
);
681 if (flags
& TEVENT_FD_READ
) {
682 if (!bsds
->readable_handler
) {
683 TEVENT_FD_NOT_READABLE(bsds
->fde
);
686 bsds
->readable_handler(bsds
->readable_private
);
691 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
692 struct tevent_context
*ev
,
693 void (*handler
)(void *private_data
),
701 if (!bsds
->readable_handler
) {
704 bsds
->readable_handler
= NULL
;
705 bsds
->readable_private
= NULL
;
710 /* read and write must use the same tevent_context */
711 if (bsds
->event_ptr
!= ev
) {
712 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
716 bsds
->event_ptr
= NULL
;
717 TALLOC_FREE(bsds
->fde
);
720 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
721 TALLOC_FREE(bsds
->fde
);
723 bsds
->fde
= tevent_add_fd(ev
, bsds
,
724 bsds
->fd
, TEVENT_FD_READ
,
725 tdgram_bsd_fde_handler
,
732 /* cache the event context we're running on */
733 bsds
->event_ptr
= ev
;
734 } else if (!bsds
->readable_handler
) {
735 TEVENT_FD_READABLE(bsds
->fde
);
738 bsds
->readable_handler
= handler
;
739 bsds
->readable_private
= private_data
;
744 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
745 struct tevent_context
*ev
,
746 void (*handler
)(void *private_data
),
754 if (!bsds
->writeable_handler
) {
757 bsds
->writeable_handler
= NULL
;
758 bsds
->writeable_private
= NULL
;
759 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
764 /* read and write must use the same tevent_context */
765 if (bsds
->event_ptr
!= ev
) {
766 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
770 bsds
->event_ptr
= NULL
;
771 TALLOC_FREE(bsds
->fde
);
774 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
775 TALLOC_FREE(bsds
->fde
);
777 bsds
->fde
= tevent_add_fd(ev
, bsds
,
778 bsds
->fd
, TEVENT_FD_WRITE
,
779 tdgram_bsd_fde_handler
,
786 /* cache the event context we're running on */
787 bsds
->event_ptr
= ev
;
788 } else if (!bsds
->writeable_handler
) {
789 TEVENT_FD_WRITEABLE(bsds
->fde
);
792 bsds
->writeable_handler
= handler
;
793 bsds
->writeable_private
= private_data
;
798 struct tdgram_bsd_recvfrom_state
{
799 struct tdgram_context
*dgram
;
803 struct tsocket_address
*src
;
806 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
808 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
811 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
816 static void tdgram_bsd_recvfrom_handler(void *private_data
);
818 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
819 struct tevent_context
*ev
,
820 struct tdgram_context
*dgram
)
822 struct tevent_req
*req
;
823 struct tdgram_bsd_recvfrom_state
*state
;
824 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
827 req
= tevent_req_create(mem_ctx
, &state
,
828 struct tdgram_bsd_recvfrom_state
);
833 state
->dgram
= dgram
;
834 state
->first_try
= true;
839 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
841 if (bsds
->fd
== -1) {
842 tevent_req_error(req
, ENOTCONN
);
848 * this is a fast path, not waiting for the
849 * socket to become explicit readable gains
850 * about 10%-20% performance in benchmark tests.
852 if (bsds
->optimize_recvfrom
) {
854 * We only do the optimization on
855 * recvfrom if the caller asked for it.
857 * This is needed because in most cases
858 * we preferr to flush send buffers before
859 * receiving incoming requests.
861 tdgram_bsd_recvfrom_handler(req
);
862 if (!tevent_req_is_in_progress(req
)) {
867 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
868 tdgram_bsd_recvfrom_handler
,
871 tevent_req_error(req
, errno
);
878 tevent_req_post(req
, ev
);
882 static void tdgram_bsd_recvfrom_handler(void *private_data
)
884 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
886 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
887 struct tdgram_bsd_recvfrom_state
);
888 struct tdgram_context
*dgram
= state
->dgram
;
889 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
890 struct tsocket_address_bsd
*bsda
;
895 ret
= tsocket_bsd_pending(bsds
->fd
);
896 if (state
->first_try
&& ret
== 0) {
897 state
->first_try
= false;
901 state
->first_try
= false;
903 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
908 if (tevent_req_error(req
, err
)) {
912 /* note that 'ret' can be 0 here */
913 state
->buf
= talloc_array(state
, uint8_t, ret
);
914 if (tevent_req_nomem(state
->buf
, req
)) {
919 state
->src
= tsocket_address_create(state
,
920 &tsocket_address_bsd_ops
,
922 struct tsocket_address_bsd
,
923 __location__
"bsd_recvfrom");
924 if (tevent_req_nomem(state
->src
, req
)) {
929 bsda
->sa_socklen
= sizeof(bsda
->u
.ss
);
930 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
931 bsda
->u
.sa
.sa_len
= bsda
->sa_socklen
;
934 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0,
935 &bsda
->u
.sa
, &bsda
->sa_socklen
);
936 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
941 if (tevent_req_error(req
, err
)) {
946 * Some systems (FreeBSD, see bug #7115) return too much
947 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
948 * the return value includes some IP/UDP header bytes,
949 * while recvfrom() just returns the payload.
951 state
->buf
= talloc_realloc(state
, state
->buf
, uint8_t, ret
);
952 if (tevent_req_nomem(state
->buf
, req
)) {
957 tevent_req_done(req
);
960 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
964 struct tsocket_address
**src
)
966 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
967 struct tdgram_bsd_recvfrom_state
);
970 ret
= tsocket_simple_int_recv(req
, perrno
);
972 *buf
= talloc_move(mem_ctx
, &state
->buf
);
975 *src
= talloc_move(mem_ctx
, &state
->src
);
979 tevent_req_received(req
);
983 struct tdgram_bsd_sendto_state
{
984 struct tdgram_context
*dgram
;
988 const struct tsocket_address
*dst
;
993 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
995 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
998 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1003 static void tdgram_bsd_sendto_handler(void *private_data
);
1005 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
1006 struct tevent_context
*ev
,
1007 struct tdgram_context
*dgram
,
1010 const struct tsocket_address
*dst
)
1012 struct tevent_req
*req
;
1013 struct tdgram_bsd_sendto_state
*state
;
1014 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1017 req
= tevent_req_create(mem_ctx
, &state
,
1018 struct tdgram_bsd_sendto_state
);
1023 state
->dgram
= dgram
;
1029 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
1031 if (bsds
->fd
== -1) {
1032 tevent_req_error(req
, ENOTCONN
);
1037 * this is a fast path, not waiting for the
1038 * socket to become explicit writeable gains
1039 * about 10%-20% performance in benchmark tests.
1041 tdgram_bsd_sendto_handler(req
);
1042 if (!tevent_req_is_in_progress(req
)) {
1046 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
1047 tdgram_bsd_sendto_handler
,
1050 tevent_req_error(req
, errno
);
1057 tevent_req_post(req
, ev
);
1061 static void tdgram_bsd_sendto_handler(void *private_data
)
1063 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1065 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1066 struct tdgram_bsd_sendto_state
);
1067 struct tdgram_context
*dgram
= state
->dgram
;
1068 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1069 struct sockaddr
*sa
= NULL
;
1070 socklen_t sa_socklen
= 0;
1076 struct tsocket_address_bsd
*bsda
=
1077 talloc_get_type(state
->dst
->private_data
,
1078 struct tsocket_address_bsd
);
1081 sa_socklen
= bsda
->sa_socklen
;
1084 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1085 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1091 if (err
== EMSGSIZE
) {
1092 /* round up in 1K increments */
1093 int bufsize
= ((state
->len
+ 1023) & (~1023));
1095 ret
= setsockopt(bsds
->fd
, SOL_SOCKET
, SO_SNDBUF
, &bufsize
,
1099 * We do the retry here, rather then via the
1100 * handler, as we only want to retry once for
1101 * this condition, so if there is a mismatch
1102 * between what setsockopt() accepts and what can
1103 * actually be sent, we do not end up in a
1107 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
,
1109 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1110 if (retry
) { /* retry later */
1116 if (tevent_req_error(req
, err
)) {
1122 tevent_req_done(req
);
1125 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1127 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1128 struct tdgram_bsd_sendto_state
);
1131 ret
= tsocket_simple_int_recv(req
, perrno
);
1136 tevent_req_received(req
);
1140 struct tdgram_bsd_disconnect_state
{
1144 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1145 struct tevent_context
*ev
,
1146 struct tdgram_context
*dgram
)
1148 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1149 struct tevent_req
*req
;
1150 struct tdgram_bsd_disconnect_state
*state
;
1155 req
= tevent_req_create(mem_ctx
, &state
,
1156 struct tdgram_bsd_disconnect_state
);
1161 if (bsds
->fd
== -1) {
1162 tevent_req_error(req
, ENOTCONN
);
1166 TALLOC_FREE(bsds
->fde
);
1167 ret
= close(bsds
->fd
);
1169 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1170 if (tevent_req_error(req
, err
)) {
1174 tevent_req_done(req
);
1176 tevent_req_post(req
, ev
);
1180 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1185 ret
= tsocket_simple_int_recv(req
, perrno
);
1187 tevent_req_received(req
);
1191 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1194 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1195 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1197 .sendto_send
= tdgram_bsd_sendto_send
,
1198 .sendto_recv
= tdgram_bsd_sendto_recv
,
1200 .disconnect_send
= tdgram_bsd_disconnect_send
,
1201 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1204 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1206 TALLOC_FREE(bsds
->fde
);
1207 if (bsds
->fd
!= -1) {
1214 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1215 const struct tsocket_address
*remote
,
1217 TALLOC_CTX
*mem_ctx
,
1218 struct tdgram_context
**_dgram
,
1219 const char *location
)
1221 struct tsocket_address_bsd
*lbsda
=
1222 talloc_get_type_abort(local
->private_data
,
1223 struct tsocket_address_bsd
);
1224 struct tsocket_address_bsd
*rbsda
= NULL
;
1225 struct tdgram_context
*dgram
;
1226 struct tdgram_bsd
*bsds
;
1229 bool do_bind
= false;
1230 bool do_reuseaddr
= false;
1231 bool do_ipv6only
= false;
1232 bool is_inet
= false;
1233 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1236 rbsda
= talloc_get_type_abort(remote
->private_data
,
1237 struct tsocket_address_bsd
);
1240 switch (lbsda
->u
.sa
.sa_family
) {
1246 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1247 do_reuseaddr
= true;
1252 if (lbsda
->u
.in
.sin_port
!= 0) {
1253 do_reuseaddr
= true;
1256 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1263 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1264 do_reuseaddr
= true;
1267 if (memcmp(&in6addr_any
,
1268 &lbsda
->u
.in6
.sin6_addr
,
1269 sizeof(in6addr_any
)) != 0) {
1281 if (!do_bind
&& is_inet
&& rbsda
) {
1282 sa_fam
= rbsda
->u
.sa
.sa_family
;
1285 do_ipv6only
= false;
1295 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1300 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1305 dgram
= tdgram_context_create(mem_ctx
,
1311 int saved_errno
= errno
;
1313 errno
= saved_errno
;
1318 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1324 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1325 (const void *)&val
, sizeof(val
));
1327 int saved_errno
= errno
;
1329 errno
= saved_errno
;
1338 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1339 (const void *)&val
, sizeof(val
));
1341 int saved_errno
= errno
;
1343 errno
= saved_errno
;
1351 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1352 (const void *)&val
, sizeof(val
));
1354 int saved_errno
= errno
;
1356 errno
= saved_errno
;
1362 ret
= bind(fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
1364 int saved_errno
= errno
;
1366 errno
= saved_errno
;
1372 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1378 ret
= connect(fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
1380 int saved_errno
= errno
;
1382 errno
= saved_errno
;
1391 int _tdgram_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1393 struct tdgram_context
**_dgram
,
1394 const char *location
)
1396 struct tdgram_context
*dgram
;
1397 struct tdgram_bsd
*bsds
;
1399 dgram
= tdgram_context_create(mem_ctx
,
1409 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1415 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1416 const struct tsocket_address
*remote
,
1417 TALLOC_CTX
*mem_ctx
,
1418 struct tdgram_context
**dgram
,
1419 const char *location
)
1421 struct tsocket_address_bsd
*lbsda
=
1422 talloc_get_type_abort(local
->private_data
,
1423 struct tsocket_address_bsd
);
1426 switch (lbsda
->u
.sa
.sa_family
) {
1438 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1439 mem_ctx
, dgram
, location
);
1444 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1445 const struct tsocket_address
*remote
,
1446 TALLOC_CTX
*mem_ctx
,
1447 struct tdgram_context
**dgram
,
1448 const char *location
)
1450 struct tsocket_address_bsd
*lbsda
=
1451 talloc_get_type_abort(local
->private_data
,
1452 struct tsocket_address_bsd
);
1455 switch (lbsda
->u
.sa
.sa_family
) {
1463 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1464 mem_ctx
, dgram
, location
);
1469 struct tstream_bsd
{
1473 struct tevent_fd
*fde
;
1474 bool optimize_readv
;
1476 void *readable_private
;
1477 void (*readable_handler
)(void *private_data
);
1478 void *writeable_private
;
1479 void (*writeable_handler
)(void *private_data
);
1482 bool tstream_bsd_optimize_readv(struct tstream_context
*stream
,
1485 struct tstream_bsd
*bsds
=
1486 talloc_get_type(_tstream_context_data(stream
),
1487 struct tstream_bsd
);
1491 /* not a bsd socket */
1495 old
= bsds
->optimize_readv
;
1496 bsds
->optimize_readv
= on
;
1501 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1502 struct tevent_fd
*fde
,
1506 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1507 struct tstream_bsd
);
1509 if (flags
& TEVENT_FD_WRITE
) {
1510 bsds
->writeable_handler(bsds
->writeable_private
);
1513 if (flags
& TEVENT_FD_READ
) {
1514 if (!bsds
->readable_handler
) {
1515 if (bsds
->writeable_handler
) {
1516 bsds
->writeable_handler(bsds
->writeable_private
);
1519 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1522 bsds
->readable_handler(bsds
->readable_private
);
1527 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1528 struct tevent_context
*ev
,
1529 void (*handler
)(void *private_data
),
1537 if (!bsds
->readable_handler
) {
1540 bsds
->readable_handler
= NULL
;
1541 bsds
->readable_private
= NULL
;
1546 /* read and write must use the same tevent_context */
1547 if (bsds
->event_ptr
!= ev
) {
1548 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1552 bsds
->event_ptr
= NULL
;
1553 TALLOC_FREE(bsds
->fde
);
1556 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1557 TALLOC_FREE(bsds
->fde
);
1559 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1560 bsds
->fd
, TEVENT_FD_READ
,
1561 tstream_bsd_fde_handler
,
1568 /* cache the event context we're running on */
1569 bsds
->event_ptr
= ev
;
1570 } else if (!bsds
->readable_handler
) {
1571 TEVENT_FD_READABLE(bsds
->fde
);
1574 bsds
->readable_handler
= handler
;
1575 bsds
->readable_private
= private_data
;
1580 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1581 struct tevent_context
*ev
,
1582 void (*handler
)(void *private_data
),
1590 if (!bsds
->writeable_handler
) {
1593 bsds
->writeable_handler
= NULL
;
1594 bsds
->writeable_private
= NULL
;
1595 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1600 /* read and write must use the same tevent_context */
1601 if (bsds
->event_ptr
!= ev
) {
1602 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1606 bsds
->event_ptr
= NULL
;
1607 TALLOC_FREE(bsds
->fde
);
1610 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1611 TALLOC_FREE(bsds
->fde
);
1613 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1615 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1616 tstream_bsd_fde_handler
,
1623 /* cache the event context we're running on */
1624 bsds
->event_ptr
= ev
;
1625 } else if (!bsds
->writeable_handler
) {
1626 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1627 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1628 tevent_fd_set_flags(bsds
->fde
, flags
);
1631 bsds
->writeable_handler
= handler
;
1632 bsds
->writeable_private
= private_data
;
1637 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1639 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1640 struct tstream_bsd
);
1643 if (bsds
->fd
== -1) {
1648 ret
= tsocket_bsd_pending(bsds
->fd
);
1653 struct tstream_bsd_readv_state
{
1654 struct tstream_context
*stream
;
1656 struct iovec
*vector
;
1662 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1664 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1665 struct tstream_bsd
);
1667 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1672 static void tstream_bsd_readv_handler(void *private_data
);
1674 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1675 struct tevent_context
*ev
,
1676 struct tstream_context
*stream
,
1677 struct iovec
*vector
,
1680 struct tevent_req
*req
;
1681 struct tstream_bsd_readv_state
*state
;
1682 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1685 req
= tevent_req_create(mem_ctx
, &state
,
1686 struct tstream_bsd_readv_state
);
1691 state
->stream
= stream
;
1692 /* we make a copy of the vector so that we can modify it */
1693 state
->vector
= talloc_array(state
, struct iovec
, count
);
1694 if (tevent_req_nomem(state
->vector
, req
)) {
1697 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1698 state
->count
= count
;
1701 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1703 if (bsds
->fd
== -1) {
1704 tevent_req_error(req
, ENOTCONN
);
1709 * this is a fast path, not waiting for the
1710 * socket to become explicit readable gains
1711 * about 10%-20% performance in benchmark tests.
1713 if (bsds
->optimize_readv
) {
1715 * We only do the optimization on
1716 * readv if the caller asked for it.
1718 * This is needed because in most cases
1719 * we preferr to flush send buffers before
1720 * receiving incoming requests.
1722 tstream_bsd_readv_handler(req
);
1723 if (!tevent_req_is_in_progress(req
)) {
1728 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1729 tstream_bsd_readv_handler
,
1732 tevent_req_error(req
, errno
);
1739 tevent_req_post(req
, ev
);
1743 static void tstream_bsd_readv_handler(void *private_data
)
1745 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1747 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1748 struct tstream_bsd_readv_state
);
1749 struct tstream_context
*stream
= state
->stream
;
1750 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1756 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1758 /* propagate end of file */
1759 tevent_req_error(req
, EPIPE
);
1762 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1767 if (tevent_req_error(req
, err
)) {
1773 _count
= state
->count
; /* tstream has size_t count, readv has int */
1774 ok
= iov_advance(&state
->vector
, &_count
, ret
);
1775 state
->count
= _count
;
1778 tevent_req_error(req
, EINVAL
);
1782 if (state
->count
> 0) {
1783 /* we have more to read */
1787 tevent_req_done(req
);
1790 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1793 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1794 struct tstream_bsd_readv_state
);
1797 ret
= tsocket_simple_int_recv(req
, perrno
);
1802 tevent_req_received(req
);
1806 struct tstream_bsd_writev_state
{
1807 struct tstream_context
*stream
;
1809 struct iovec
*vector
;
1815 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1817 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1818 struct tstream_bsd
);
1820 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1825 static void tstream_bsd_writev_handler(void *private_data
);
1827 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1828 struct tevent_context
*ev
,
1829 struct tstream_context
*stream
,
1830 const struct iovec
*vector
,
1833 struct tevent_req
*req
;
1834 struct tstream_bsd_writev_state
*state
;
1835 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1838 req
= tevent_req_create(mem_ctx
, &state
,
1839 struct tstream_bsd_writev_state
);
1844 state
->stream
= stream
;
1845 /* we make a copy of the vector so that we can modify it */
1846 state
->vector
= talloc_array(state
, struct iovec
, count
);
1847 if (tevent_req_nomem(state
->vector
, req
)) {
1850 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1851 state
->count
= count
;
1854 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1856 if (bsds
->fd
== -1) {
1857 tevent_req_error(req
, ENOTCONN
);
1862 * this is a fast path, not waiting for the
1863 * socket to become explicit writeable gains
1864 * about 10%-20% performance in benchmark tests.
1866 tstream_bsd_writev_handler(req
);
1867 if (!tevent_req_is_in_progress(req
)) {
1871 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1872 tstream_bsd_writev_handler
,
1875 tevent_req_error(req
, errno
);
1882 tevent_req_post(req
, ev
);
1886 static void tstream_bsd_writev_handler(void *private_data
)
1888 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1890 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1891 struct tstream_bsd_writev_state
);
1892 struct tstream_context
*stream
= state
->stream
;
1893 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1899 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1901 /* propagate end of file */
1902 tevent_req_error(req
, EPIPE
);
1905 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1910 if (tevent_req_error(req
, err
)) {
1916 _count
= state
->count
; /* tstream has size_t count, writev has int */
1917 ok
= iov_advance(&state
->vector
, &_count
, ret
);
1918 state
->count
= _count
;
1921 tevent_req_error(req
, EINVAL
);
1925 if (state
->count
> 0) {
1926 /* we have more to read */
1930 tevent_req_done(req
);
1933 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1935 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1936 struct tstream_bsd_writev_state
);
1939 ret
= tsocket_simple_int_recv(req
, perrno
);
1944 tevent_req_received(req
);
1948 struct tstream_bsd_disconnect_state
{
1952 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1953 struct tevent_context
*ev
,
1954 struct tstream_context
*stream
)
1956 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1957 struct tevent_req
*req
;
1958 struct tstream_bsd_disconnect_state
*state
;
1963 req
= tevent_req_create(mem_ctx
, &state
,
1964 struct tstream_bsd_disconnect_state
);
1969 if (bsds
->fd
== -1) {
1970 tevent_req_error(req
, ENOTCONN
);
1974 TALLOC_FREE(bsds
->fde
);
1975 ret
= close(bsds
->fd
);
1977 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1978 if (tevent_req_error(req
, err
)) {
1982 tevent_req_done(req
);
1984 tevent_req_post(req
, ev
);
1988 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1993 ret
= tsocket_simple_int_recv(req
, perrno
);
1995 tevent_req_received(req
);
1999 static const struct tstream_context_ops tstream_bsd_ops
= {
2002 .pending_bytes
= tstream_bsd_pending_bytes
,
2004 .readv_send
= tstream_bsd_readv_send
,
2005 .readv_recv
= tstream_bsd_readv_recv
,
2007 .writev_send
= tstream_bsd_writev_send
,
2008 .writev_recv
= tstream_bsd_writev_recv
,
2010 .disconnect_send
= tstream_bsd_disconnect_send
,
2011 .disconnect_recv
= tstream_bsd_disconnect_recv
,
2014 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
2016 TALLOC_FREE(bsds
->fde
);
2017 if (bsds
->fd
!= -1) {
2024 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
2026 struct tstream_context
**_stream
,
2027 const char *location
)
2029 struct tstream_context
*stream
;
2030 struct tstream_bsd
*bsds
;
2032 stream
= tstream_context_create(mem_ctx
,
2042 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
2048 struct tstream_bsd_connect_state
{
2050 struct tevent_fd
*fde
;
2051 struct tstream_conext
*stream
;
2052 struct tsocket_address
*local
;
2055 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
2057 TALLOC_FREE(state
->fde
);
2058 if (state
->fd
!= -1) {
2066 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2067 struct tevent_fd
*fde
,
2069 void *private_data
);
2071 static struct tevent_req
*tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
2072 struct tevent_context
*ev
,
2074 const struct tsocket_address
*local
,
2075 const struct tsocket_address
*remote
)
2077 struct tevent_req
*req
;
2078 struct tstream_bsd_connect_state
*state
;
2079 struct tsocket_address_bsd
*lbsda
=
2080 talloc_get_type_abort(local
->private_data
,
2081 struct tsocket_address_bsd
);
2082 struct tsocket_address_bsd
*lrbsda
= NULL
;
2083 struct tsocket_address_bsd
*rbsda
=
2084 talloc_get_type_abort(remote
->private_data
,
2085 struct tsocket_address_bsd
);
2089 bool do_bind
= false;
2090 bool do_reuseaddr
= false;
2091 bool do_ipv6only
= false;
2092 bool is_inet
= false;
2093 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2095 req
= tevent_req_create(mem_ctx
, &state
,
2096 struct tstream_bsd_connect_state
);
2103 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2105 /* give the wrappers a chance to report an error */
2106 if (sys_errno
!= 0) {
2107 tevent_req_error(req
, sys_errno
);
2111 switch (lbsda
->u
.sa
.sa_family
) {
2113 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2114 do_reuseaddr
= true;
2119 if (lbsda
->u
.in
.sin_port
!= 0) {
2120 do_reuseaddr
= true;
2123 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2130 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2131 do_reuseaddr
= true;
2134 if (memcmp(&in6addr_any
,
2135 &lbsda
->u
.in6
.sin6_addr
,
2136 sizeof(in6addr_any
)) != 0) {
2144 tevent_req_error(req
, EINVAL
);
2148 if (!do_bind
&& is_inet
) {
2149 sa_fam
= rbsda
->u
.sa
.sa_family
;
2152 do_ipv6only
= false;
2163 state
->local
= tsocket_address_create(state
,
2164 &tsocket_address_bsd_ops
,
2166 struct tsocket_address_bsd
,
2167 __location__
"bsd_connect");
2168 if (tevent_req_nomem(state
->local
, req
)) {
2172 ZERO_STRUCTP(lrbsda
);
2173 lrbsda
->sa_socklen
= sizeof(lrbsda
->u
.ss
);
2174 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2175 lrbsda
->u
.sa
.sa_len
= lrbsda
->sa_socklen
;
2179 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2180 if (state
->fd
== -1) {
2181 tevent_req_error(req
, errno
);
2185 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2186 if (state
->fd
== -1) {
2187 tevent_req_error(req
, errno
);
2195 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2196 (const void *)&val
, sizeof(val
));
2198 tevent_req_error(req
, errno
);
2207 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2208 (const void *)&val
, sizeof(val
));
2210 tevent_req_error(req
, errno
);
2216 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2218 tevent_req_error(req
, errno
);
2223 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2224 tevent_req_error(req
, EINVAL
);
2228 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2229 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2234 if (tevent_req_error(req
, err
)) {
2238 if (!state
->local
) {
2239 tevent_req_done(req
);
2243 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2245 tevent_req_error(req
, errno
);
2249 tevent_req_done(req
);
2253 state
->fde
= tevent_add_fd(ev
, state
,
2255 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2256 tstream_bsd_connect_fde_handler
,
2258 if (tevent_req_nomem(state
->fde
, req
)) {
2265 tevent_req_post(req
, ev
);
2269 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2270 struct tevent_fd
*fde
,
2274 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2276 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2277 struct tstream_bsd_connect_state
);
2278 struct tsocket_address_bsd
*lrbsda
= NULL
;
2281 socklen_t len
= sizeof(error
);
2285 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2292 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2297 if (tevent_req_error(req
, err
)) {
2301 if (!state
->local
) {
2302 tevent_req_done(req
);
2306 lrbsda
= talloc_get_type_abort(state
->local
->private_data
,
2307 struct tsocket_address_bsd
);
2309 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2311 tevent_req_error(req
, errno
);
2315 tevent_req_done(req
);
2318 static int tstream_bsd_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 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2326 struct tstream_bsd_connect_state
);
2329 ret
= tsocket_simple_int_recv(req
, perrno
);
2331 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2339 TALLOC_FREE(state
->fde
);
2343 *local
= talloc_move(mem_ctx
, &state
->local
);
2348 tevent_req_received(req
);
2352 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2353 struct tevent_context
*ev
,
2354 const struct tsocket_address
*local
,
2355 const struct tsocket_address
*remote
)
2357 struct tsocket_address_bsd
*lbsda
=
2358 talloc_get_type_abort(local
->private_data
,
2359 struct tsocket_address_bsd
);
2360 struct tevent_req
*req
;
2363 switch (lbsda
->u
.sa
.sa_family
) {
2375 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2380 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2382 TALLOC_CTX
*mem_ctx
,
2383 struct tstream_context
**stream
,
2384 struct tsocket_address
**local
,
2385 const char *location
)
2387 return tstream_bsd_connect_recv(req
, perrno
,
2388 mem_ctx
, stream
, local
,
2392 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2393 struct tevent_context
*ev
,
2394 const struct tsocket_address
*local
,
2395 const struct tsocket_address
*remote
)
2397 struct tsocket_address_bsd
*lbsda
=
2398 talloc_get_type_abort(local
->private_data
,
2399 struct tsocket_address_bsd
);
2400 struct tevent_req
*req
;
2403 switch (lbsda
->u
.sa
.sa_family
) {
2411 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2416 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2418 TALLOC_CTX
*mem_ctx
,
2419 struct tstream_context
**stream
,
2420 const char *location
)
2422 return tstream_bsd_connect_recv(req
, perrno
,
2423 mem_ctx
, stream
, NULL
,
2427 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2428 struct tstream_context
**_stream1
,
2429 TALLOC_CTX
*mem_ctx2
,
2430 struct tstream_context
**_stream2
,
2431 const char *location
)
2437 struct tstream_context
*stream1
= NULL
;
2438 struct tstream_context
*stream2
= NULL
;
2440 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2447 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2449 int sys_errno
= errno
;
2455 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2457 int sys_errno
= errno
;
2463 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2468 int sys_errno
= errno
;
2475 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2480 int sys_errno
= errno
;
2481 talloc_free(stream1
);
2487 *_stream1
= stream1
;
2488 *_stream2
= stream2
;