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 prefer 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_inet_udp_broadcast_socket(const struct tsocket_address
*local
,
1445 TALLOC_CTX
*mem_ctx
,
1446 struct tdgram_context
**dgram
,
1447 const char *location
)
1449 struct tsocket_address_bsd
*lbsda
=
1450 talloc_get_type_abort(local
->private_data
,
1451 struct tsocket_address_bsd
);
1454 switch (lbsda
->u
.sa
.sa_family
) {
1468 ret
= tdgram_bsd_dgram_socket(local
, NULL
, true,
1469 mem_ctx
, dgram
, location
);
1474 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1475 const struct tsocket_address
*remote
,
1476 TALLOC_CTX
*mem_ctx
,
1477 struct tdgram_context
**dgram
,
1478 const char *location
)
1480 struct tsocket_address_bsd
*lbsda
=
1481 talloc_get_type_abort(local
->private_data
,
1482 struct tsocket_address_bsd
);
1485 switch (lbsda
->u
.sa
.sa_family
) {
1493 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1494 mem_ctx
, dgram
, location
);
1499 struct tstream_bsd
{
1503 struct tevent_fd
*fde
;
1504 bool optimize_readv
;
1506 void *readable_private
;
1507 void (*readable_handler
)(void *private_data
);
1508 void *writeable_private
;
1509 void (*writeable_handler
)(void *private_data
);
1512 bool tstream_bsd_optimize_readv(struct tstream_context
*stream
,
1515 struct tstream_bsd
*bsds
=
1516 talloc_get_type(_tstream_context_data(stream
),
1517 struct tstream_bsd
);
1521 /* not a bsd socket */
1525 old
= bsds
->optimize_readv
;
1526 bsds
->optimize_readv
= on
;
1531 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1532 struct tevent_fd
*fde
,
1536 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1537 struct tstream_bsd
);
1539 if (flags
& TEVENT_FD_WRITE
) {
1540 bsds
->writeable_handler(bsds
->writeable_private
);
1543 if (flags
& TEVENT_FD_READ
) {
1544 if (!bsds
->readable_handler
) {
1545 if (bsds
->writeable_handler
) {
1546 bsds
->writeable_handler(bsds
->writeable_private
);
1549 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1552 bsds
->readable_handler(bsds
->readable_private
);
1557 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1558 struct tevent_context
*ev
,
1559 void (*handler
)(void *private_data
),
1567 if (!bsds
->readable_handler
) {
1570 bsds
->readable_handler
= NULL
;
1571 bsds
->readable_private
= NULL
;
1576 /* read and write must use the same tevent_context */
1577 if (bsds
->event_ptr
!= ev
) {
1578 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1582 bsds
->event_ptr
= NULL
;
1583 TALLOC_FREE(bsds
->fde
);
1586 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1587 TALLOC_FREE(bsds
->fde
);
1589 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1590 bsds
->fd
, TEVENT_FD_READ
,
1591 tstream_bsd_fde_handler
,
1598 /* cache the event context we're running on */
1599 bsds
->event_ptr
= ev
;
1600 } else if (!bsds
->readable_handler
) {
1601 TEVENT_FD_READABLE(bsds
->fde
);
1604 bsds
->readable_handler
= handler
;
1605 bsds
->readable_private
= private_data
;
1610 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1611 struct tevent_context
*ev
,
1612 void (*handler
)(void *private_data
),
1620 if (!bsds
->writeable_handler
) {
1623 bsds
->writeable_handler
= NULL
;
1624 bsds
->writeable_private
= NULL
;
1625 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1630 /* read and write must use the same tevent_context */
1631 if (bsds
->event_ptr
!= ev
) {
1632 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1636 bsds
->event_ptr
= NULL
;
1637 TALLOC_FREE(bsds
->fde
);
1640 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1641 TALLOC_FREE(bsds
->fde
);
1643 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1645 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1646 tstream_bsd_fde_handler
,
1653 /* cache the event context we're running on */
1654 bsds
->event_ptr
= ev
;
1655 } else if (!bsds
->writeable_handler
) {
1656 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1657 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1658 tevent_fd_set_flags(bsds
->fde
, flags
);
1661 bsds
->writeable_handler
= handler
;
1662 bsds
->writeable_private
= private_data
;
1667 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1669 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1670 struct tstream_bsd
);
1673 if (bsds
->fd
== -1) {
1678 ret
= tsocket_bsd_pending(bsds
->fd
);
1683 struct tstream_bsd_readv_state
{
1684 struct tstream_context
*stream
;
1686 struct iovec
*vector
;
1692 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1694 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1695 struct tstream_bsd
);
1697 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1702 static void tstream_bsd_readv_handler(void *private_data
);
1704 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1705 struct tevent_context
*ev
,
1706 struct tstream_context
*stream
,
1707 struct iovec
*vector
,
1710 struct tevent_req
*req
;
1711 struct tstream_bsd_readv_state
*state
;
1712 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1715 req
= tevent_req_create(mem_ctx
, &state
,
1716 struct tstream_bsd_readv_state
);
1721 state
->stream
= stream
;
1722 /* we make a copy of the vector so that we can modify it */
1723 state
->vector
= talloc_array(state
, struct iovec
, count
);
1724 if (tevent_req_nomem(state
->vector
, req
)) {
1727 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1728 state
->count
= count
;
1731 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1733 if (bsds
->fd
== -1) {
1734 tevent_req_error(req
, ENOTCONN
);
1739 * this is a fast path, not waiting for the
1740 * socket to become explicit readable gains
1741 * about 10%-20% performance in benchmark tests.
1743 if (bsds
->optimize_readv
) {
1745 * We only do the optimization on
1746 * readv if the caller asked for it.
1748 * This is needed because in most cases
1749 * we prefer to flush send buffers before
1750 * receiving incoming requests.
1752 tstream_bsd_readv_handler(req
);
1753 if (!tevent_req_is_in_progress(req
)) {
1758 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1759 tstream_bsd_readv_handler
,
1762 tevent_req_error(req
, errno
);
1769 tevent_req_post(req
, ev
);
1773 static void tstream_bsd_readv_handler(void *private_data
)
1775 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1777 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1778 struct tstream_bsd_readv_state
);
1779 struct tstream_context
*stream
= state
->stream
;
1780 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1786 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1788 /* propagate end of file */
1789 tevent_req_error(req
, EPIPE
);
1792 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1797 if (tevent_req_error(req
, err
)) {
1803 _count
= state
->count
; /* tstream has size_t count, readv has int */
1804 ok
= iov_advance(&state
->vector
, &_count
, ret
);
1805 state
->count
= _count
;
1808 tevent_req_error(req
, EINVAL
);
1812 if (state
->count
> 0) {
1813 /* we have more to read */
1817 tevent_req_done(req
);
1820 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1823 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1824 struct tstream_bsd_readv_state
);
1827 ret
= tsocket_simple_int_recv(req
, perrno
);
1832 tevent_req_received(req
);
1836 struct tstream_bsd_writev_state
{
1837 struct tstream_context
*stream
;
1839 struct iovec
*vector
;
1845 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1847 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1848 struct tstream_bsd
);
1850 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1855 static void tstream_bsd_writev_handler(void *private_data
);
1857 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1858 struct tevent_context
*ev
,
1859 struct tstream_context
*stream
,
1860 const struct iovec
*vector
,
1863 struct tevent_req
*req
;
1864 struct tstream_bsd_writev_state
*state
;
1865 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1868 req
= tevent_req_create(mem_ctx
, &state
,
1869 struct tstream_bsd_writev_state
);
1874 state
->stream
= stream
;
1875 /* we make a copy of the vector so that we can modify it */
1876 state
->vector
= talloc_array(state
, struct iovec
, count
);
1877 if (tevent_req_nomem(state
->vector
, req
)) {
1880 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1881 state
->count
= count
;
1884 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1886 if (bsds
->fd
== -1) {
1887 tevent_req_error(req
, ENOTCONN
);
1892 * this is a fast path, not waiting for the
1893 * socket to become explicit writeable gains
1894 * about 10%-20% performance in benchmark tests.
1896 tstream_bsd_writev_handler(req
);
1897 if (!tevent_req_is_in_progress(req
)) {
1901 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1902 tstream_bsd_writev_handler
,
1905 tevent_req_error(req
, errno
);
1912 tevent_req_post(req
, ev
);
1916 static void tstream_bsd_writev_handler(void *private_data
)
1918 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1920 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1921 struct tstream_bsd_writev_state
);
1922 struct tstream_context
*stream
= state
->stream
;
1923 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1929 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1931 /* propagate end of file */
1932 tevent_req_error(req
, EPIPE
);
1935 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1940 if (tevent_req_error(req
, err
)) {
1946 _count
= state
->count
; /* tstream has size_t count, writev has int */
1947 ok
= iov_advance(&state
->vector
, &_count
, ret
);
1948 state
->count
= _count
;
1951 tevent_req_error(req
, EINVAL
);
1955 if (state
->count
> 0) {
1956 /* we have more to read */
1960 tevent_req_done(req
);
1963 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1965 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1966 struct tstream_bsd_writev_state
);
1969 ret
= tsocket_simple_int_recv(req
, perrno
);
1974 tevent_req_received(req
);
1978 struct tstream_bsd_disconnect_state
{
1982 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1983 struct tevent_context
*ev
,
1984 struct tstream_context
*stream
)
1986 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1987 struct tevent_req
*req
;
1988 struct tstream_bsd_disconnect_state
*state
;
1993 req
= tevent_req_create(mem_ctx
, &state
,
1994 struct tstream_bsd_disconnect_state
);
1999 if (bsds
->fd
== -1) {
2000 tevent_req_error(req
, ENOTCONN
);
2004 TALLOC_FREE(bsds
->fde
);
2005 ret
= close(bsds
->fd
);
2007 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
2008 if (tevent_req_error(req
, err
)) {
2012 tevent_req_done(req
);
2014 tevent_req_post(req
, ev
);
2018 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
2023 ret
= tsocket_simple_int_recv(req
, perrno
);
2025 tevent_req_received(req
);
2029 static const struct tstream_context_ops tstream_bsd_ops
= {
2032 .pending_bytes
= tstream_bsd_pending_bytes
,
2034 .readv_send
= tstream_bsd_readv_send
,
2035 .readv_recv
= tstream_bsd_readv_recv
,
2037 .writev_send
= tstream_bsd_writev_send
,
2038 .writev_recv
= tstream_bsd_writev_recv
,
2040 .disconnect_send
= tstream_bsd_disconnect_send
,
2041 .disconnect_recv
= tstream_bsd_disconnect_recv
,
2044 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
2046 TALLOC_FREE(bsds
->fde
);
2047 if (bsds
->fd
!= -1) {
2054 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
2056 struct tstream_context
**_stream
,
2057 const char *location
)
2059 struct tstream_context
*stream
;
2060 struct tstream_bsd
*bsds
;
2062 stream
= tstream_context_create(mem_ctx
,
2072 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
2078 struct tstream_bsd_connect_state
{
2080 struct tevent_fd
*fde
;
2081 struct tstream_conext
*stream
;
2082 struct tsocket_address
*local
;
2085 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
2087 TALLOC_FREE(state
->fde
);
2088 if (state
->fd
!= -1) {
2096 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2097 struct tevent_fd
*fde
,
2099 void *private_data
);
2101 static struct tevent_req
*tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
2102 struct tevent_context
*ev
,
2104 const struct tsocket_address
*local
,
2105 const struct tsocket_address
*remote
)
2107 struct tevent_req
*req
;
2108 struct tstream_bsd_connect_state
*state
;
2109 struct tsocket_address_bsd
*lbsda
=
2110 talloc_get_type_abort(local
->private_data
,
2111 struct tsocket_address_bsd
);
2112 struct tsocket_address_bsd
*lrbsda
= NULL
;
2113 struct tsocket_address_bsd
*rbsda
=
2114 talloc_get_type_abort(remote
->private_data
,
2115 struct tsocket_address_bsd
);
2119 bool do_bind
= false;
2120 bool do_reuseaddr
= false;
2121 bool do_ipv6only
= false;
2122 bool is_inet
= false;
2123 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2125 req
= tevent_req_create(mem_ctx
, &state
,
2126 struct tstream_bsd_connect_state
);
2133 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2135 /* give the wrappers a chance to report an error */
2136 if (sys_errno
!= 0) {
2137 tevent_req_error(req
, sys_errno
);
2141 switch (lbsda
->u
.sa
.sa_family
) {
2143 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2144 do_reuseaddr
= true;
2149 if (lbsda
->u
.in
.sin_port
!= 0) {
2150 do_reuseaddr
= true;
2153 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2160 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2161 do_reuseaddr
= true;
2164 if (memcmp(&in6addr_any
,
2165 &lbsda
->u
.in6
.sin6_addr
,
2166 sizeof(in6addr_any
)) != 0) {
2174 tevent_req_error(req
, EINVAL
);
2178 if (!do_bind
&& is_inet
) {
2179 sa_fam
= rbsda
->u
.sa
.sa_family
;
2182 do_ipv6only
= false;
2193 state
->local
= tsocket_address_create(state
,
2194 &tsocket_address_bsd_ops
,
2196 struct tsocket_address_bsd
,
2197 __location__
"bsd_connect");
2198 if (tevent_req_nomem(state
->local
, req
)) {
2202 ZERO_STRUCTP(lrbsda
);
2203 lrbsda
->sa_socklen
= sizeof(lrbsda
->u
.ss
);
2204 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2205 lrbsda
->u
.sa
.sa_len
= lrbsda
->sa_socklen
;
2209 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2210 if (state
->fd
== -1) {
2211 tevent_req_error(req
, errno
);
2215 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2216 if (state
->fd
== -1) {
2217 tevent_req_error(req
, errno
);
2225 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2226 (const void *)&val
, sizeof(val
));
2228 tevent_req_error(req
, errno
);
2237 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2238 (const void *)&val
, sizeof(val
));
2240 tevent_req_error(req
, errno
);
2246 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2248 tevent_req_error(req
, errno
);
2253 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2254 tevent_req_error(req
, EINVAL
);
2258 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2259 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2264 if (tevent_req_error(req
, err
)) {
2268 if (!state
->local
) {
2269 tevent_req_done(req
);
2273 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2275 tevent_req_error(req
, errno
);
2279 tevent_req_done(req
);
2283 state
->fde
= tevent_add_fd(ev
, state
,
2285 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2286 tstream_bsd_connect_fde_handler
,
2288 if (tevent_req_nomem(state
->fde
, req
)) {
2295 tevent_req_post(req
, ev
);
2299 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2300 struct tevent_fd
*fde
,
2304 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2306 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2307 struct tstream_bsd_connect_state
);
2308 struct tsocket_address_bsd
*lrbsda
= NULL
;
2311 socklen_t len
= sizeof(error
);
2315 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2322 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2327 if (tevent_req_error(req
, err
)) {
2331 if (!state
->local
) {
2332 tevent_req_done(req
);
2336 lrbsda
= talloc_get_type_abort(state
->local
->private_data
,
2337 struct tsocket_address_bsd
);
2339 ret
= getsockname(state
->fd
, &lrbsda
->u
.sa
, &lrbsda
->sa_socklen
);
2341 tevent_req_error(req
, errno
);
2345 tevent_req_done(req
);
2348 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2350 TALLOC_CTX
*mem_ctx
,
2351 struct tstream_context
**stream
,
2352 struct tsocket_address
**local
,
2353 const char *location
)
2355 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2356 struct tstream_bsd_connect_state
);
2359 ret
= tsocket_simple_int_recv(req
, perrno
);
2361 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2369 TALLOC_FREE(state
->fde
);
2373 *local
= talloc_move(mem_ctx
, &state
->local
);
2378 tevent_req_received(req
);
2382 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2383 struct tevent_context
*ev
,
2384 const struct tsocket_address
*local
,
2385 const struct tsocket_address
*remote
)
2387 struct tsocket_address_bsd
*lbsda
=
2388 talloc_get_type_abort(local
->private_data
,
2389 struct tsocket_address_bsd
);
2390 struct tevent_req
*req
;
2393 switch (lbsda
->u
.sa
.sa_family
) {
2405 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2410 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2412 TALLOC_CTX
*mem_ctx
,
2413 struct tstream_context
**stream
,
2414 struct tsocket_address
**local
,
2415 const char *location
)
2417 return tstream_bsd_connect_recv(req
, perrno
,
2418 mem_ctx
, stream
, local
,
2422 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2423 struct tevent_context
*ev
,
2424 const struct tsocket_address
*local
,
2425 const struct tsocket_address
*remote
)
2427 struct tsocket_address_bsd
*lbsda
=
2428 talloc_get_type_abort(local
->private_data
,
2429 struct tsocket_address_bsd
);
2430 struct tevent_req
*req
;
2433 switch (lbsda
->u
.sa
.sa_family
) {
2441 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2446 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2448 TALLOC_CTX
*mem_ctx
,
2449 struct tstream_context
**stream
,
2450 const char *location
)
2452 return tstream_bsd_connect_recv(req
, perrno
,
2453 mem_ctx
, stream
, NULL
,
2457 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2458 struct tstream_context
**_stream1
,
2459 TALLOC_CTX
*mem_ctx2
,
2460 struct tstream_context
**_stream2
,
2461 const char *location
)
2467 struct tstream_context
*stream1
= NULL
;
2468 struct tstream_context
*stream2
= NULL
;
2470 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2477 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2479 int sys_errno
= errno
;
2485 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2487 int sys_errno
= errno
;
2493 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2498 int sys_errno
= errno
;
2505 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2510 int sys_errno
= errno
;
2511 talloc_free(stream1
);
2517 *_stream1
= stream1
;
2518 *_stream2
= stream2
;