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
{
195 struct sockaddr_in in
;
197 struct sockaddr_in6 in6
;
199 struct sockaddr_un un
;
200 struct sockaddr_storage ss
;
204 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
207 struct tsocket_address
**_addr
,
208 const char *location
)
210 struct tsocket_address
*addr
;
211 struct tsocket_address_bsd
*bsda
;
213 if (sa_socklen
< sizeof(sa
->sa_family
)) {
218 switch (sa
->sa_family
) {
220 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
221 sa_socklen
= sizeof(struct sockaddr_un
);
225 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
229 sa_socklen
= sizeof(struct sockaddr_in
);
233 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
237 sa_socklen
= sizeof(struct sockaddr_in6
);
241 errno
= EAFNOSUPPORT
;
245 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
250 addr
= tsocket_address_create(mem_ctx
,
251 &tsocket_address_bsd_ops
,
253 struct tsocket_address_bsd
,
262 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
268 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
272 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
273 struct tsocket_address_bsd
);
281 switch (bsda
->u
.sa
.sa_family
) {
283 rlen
= sizeof(struct sockaddr_un
);
286 rlen
= sizeof(struct sockaddr_in
);
290 rlen
= sizeof(struct sockaddr_in6
);
294 errno
= EAFNOSUPPORT
;
298 if (sa_socklen
< rlen
) {
303 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
304 memset(sa
, 0, sa_socklen
);
305 sa_socklen
= sizeof(struct sockaddr_storage
);
308 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
312 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
316 struct tsocket_address
**_addr
,
317 const char *location
)
319 struct addrinfo hints
;
320 struct addrinfo
*result
= NULL
;
326 * we use SOCKET_STREAM here to get just one result
327 * back from getaddrinfo().
329 hints
.ai_socktype
= SOCK_STREAM
;
330 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
332 if (strcasecmp(fam
, "ip") == 0) {
333 hints
.ai_family
= AF_UNSPEC
;
341 } else if (strcasecmp(fam
, "ipv4") == 0) {
342 hints
.ai_family
= AF_INET
;
347 } else if (strcasecmp(fam
, "ipv6") == 0) {
348 hints
.ai_family
= AF_INET6
;
354 errno
= EAFNOSUPPORT
;
358 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
360 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
371 if (result
->ai_socktype
!= SOCK_STREAM
) {
377 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
385 freeaddrinfo(result
);
390 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
393 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
394 struct tsocket_address_bsd
);
395 char addr_str
[INET6_ADDRSTRLEN
+1];
403 switch (bsda
->u
.sa
.sa_family
) {
405 str
= inet_ntop(bsda
->u
.in
.sin_family
,
406 &bsda
->u
.in
.sin_addr
,
407 addr_str
, sizeof(addr_str
));
411 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
412 &bsda
->u
.in6
.sin6_addr
,
413 addr_str
, sizeof(addr_str
));
425 return talloc_strdup(mem_ctx
, str
);
428 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
430 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
431 struct tsocket_address_bsd
);
439 switch (bsda
->u
.sa
.sa_family
) {
441 port
= ntohs(bsda
->u
.in
.sin_port
);
445 port
= ntohs(bsda
->u
.in6
.sin6_port
);
456 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
459 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
460 struct tsocket_address_bsd
);
467 switch (bsda
->u
.sa
.sa_family
) {
469 bsda
->u
.in
.sin_port
= htons(port
);
473 bsda
->u
.in6
.sin6_port
= htons(port
);
484 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
486 struct tsocket_address
**_addr
,
487 const char *location
)
489 struct sockaddr_un un
;
497 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
498 errno
= ENAMETOOLONG
;
503 un
.sun_family
= AF_UNIX
;
504 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
506 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
507 (struct sockaddr
*)p
,
515 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
518 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
519 struct tsocket_address_bsd
);
527 switch (bsda
->u
.sa
.sa_family
) {
529 str
= bsda
->u
.un
.sun_path
;
536 return talloc_strdup(mem_ctx
, str
);
539 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
542 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
543 struct tsocket_address_bsd
);
546 const char *prefix
= NULL
;
549 switch (bsda
->u
.sa
.sa_family
) {
551 return talloc_asprintf(mem_ctx
, "unix:%s",
552 bsda
->u
.un
.sun_path
);
566 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
571 port
= tsocket_address_inet_port(addr
);
573 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
574 prefix
, addr_str
, port
);
575 talloc_free(addr_str
);
580 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
582 const char *location
)
584 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
585 struct tsocket_address_bsd
);
586 struct tsocket_address
*copy
;
589 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
601 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
603 .string
= tsocket_address_bsd_string
,
604 .copy
= tsocket_address_bsd_copy
,
611 struct tevent_fd
*fde
;
613 void *readable_private
;
614 void (*readable_handler
)(void *private_data
);
615 void *writeable_private
;
616 void (*writeable_handler
)(void *private_data
);
619 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
620 struct tevent_fd
*fde
,
624 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
627 if (flags
& TEVENT_FD_WRITE
) {
628 bsds
->writeable_handler(bsds
->writeable_private
);
631 if (flags
& TEVENT_FD_READ
) {
632 if (!bsds
->readable_handler
) {
633 TEVENT_FD_NOT_READABLE(bsds
->fde
);
636 bsds
->readable_handler(bsds
->readable_private
);
641 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
642 struct tevent_context
*ev
,
643 void (*handler
)(void *private_data
),
651 if (!bsds
->readable_handler
) {
654 bsds
->readable_handler
= NULL
;
655 bsds
->readable_private
= NULL
;
660 /* read and write must use the same tevent_context */
661 if (bsds
->event_ptr
!= ev
) {
662 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
666 bsds
->event_ptr
= NULL
;
667 TALLOC_FREE(bsds
->fde
);
670 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
671 TALLOC_FREE(bsds
->fde
);
673 bsds
->fde
= tevent_add_fd(ev
, bsds
,
674 bsds
->fd
, TEVENT_FD_READ
,
675 tdgram_bsd_fde_handler
,
682 /* cache the event context we're running on */
683 bsds
->event_ptr
= ev
;
684 } else if (!bsds
->readable_handler
) {
685 TEVENT_FD_READABLE(bsds
->fde
);
688 bsds
->readable_handler
= handler
;
689 bsds
->readable_private
= private_data
;
694 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
695 struct tevent_context
*ev
,
696 void (*handler
)(void *private_data
),
704 if (!bsds
->writeable_handler
) {
707 bsds
->writeable_handler
= NULL
;
708 bsds
->writeable_private
= NULL
;
709 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
714 /* read and write must use the same tevent_context */
715 if (bsds
->event_ptr
!= ev
) {
716 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
720 bsds
->event_ptr
= NULL
;
721 TALLOC_FREE(bsds
->fde
);
724 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
725 TALLOC_FREE(bsds
->fde
);
727 bsds
->fde
= tevent_add_fd(ev
, bsds
,
728 bsds
->fd
, TEVENT_FD_WRITE
,
729 tdgram_bsd_fde_handler
,
736 /* cache the event context we're running on */
737 bsds
->event_ptr
= ev
;
738 } else if (!bsds
->writeable_handler
) {
739 TEVENT_FD_WRITEABLE(bsds
->fde
);
742 bsds
->writeable_handler
= handler
;
743 bsds
->writeable_private
= private_data
;
748 struct tdgram_bsd_recvfrom_state
{
749 struct tdgram_context
*dgram
;
753 struct tsocket_address
*src
;
756 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
758 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
761 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
766 static void tdgram_bsd_recvfrom_handler(void *private_data
);
768 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
769 struct tevent_context
*ev
,
770 struct tdgram_context
*dgram
)
772 struct tevent_req
*req
;
773 struct tdgram_bsd_recvfrom_state
*state
;
774 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
777 req
= tevent_req_create(mem_ctx
, &state
,
778 struct tdgram_bsd_recvfrom_state
);
783 state
->dgram
= dgram
;
788 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
790 if (bsds
->fd
== -1) {
791 tevent_req_error(req
, ENOTCONN
);
796 * this is a fast path, not waiting for the
797 * socket to become explicit readable gains
798 * about 10%-20% performance in benchmark tests.
800 tdgram_bsd_recvfrom_handler(req
);
801 if (!tevent_req_is_in_progress(req
)) {
805 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
806 tdgram_bsd_recvfrom_handler
,
809 tevent_req_error(req
, errno
);
816 tevent_req_post(req
, ev
);
820 static void tdgram_bsd_recvfrom_handler(void *private_data
)
822 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
824 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
825 struct tdgram_bsd_recvfrom_state
);
826 struct tdgram_context
*dgram
= state
->dgram
;
827 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
828 struct tsocket_address_bsd
*bsda
;
830 struct sockaddr
*sa
= NULL
;
831 socklen_t sa_socklen
= 0;
835 ret
= tsocket_bsd_pending(bsds
->fd
);
840 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
845 if (tevent_req_error(req
, err
)) {
849 state
->buf
= talloc_array(state
, uint8_t, ret
);
850 if (tevent_req_nomem(state
->buf
, req
)) {
855 state
->src
= tsocket_address_create(state
,
856 &tsocket_address_bsd_ops
,
858 struct tsocket_address_bsd
,
859 __location__
"bsd_recvfrom");
860 if (tevent_req_nomem(state
->src
, req
)) {
867 sa_socklen
= sizeof(bsda
->u
.ss
);
869 * for unix sockets we can't use the size of sockaddr_storage
870 * we would get EINVAL
872 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
873 sa_socklen
= sizeof(bsda
->u
.un
);
876 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, &sa_socklen
);
877 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
882 if (tevent_req_error(req
, err
)) {
886 if (ret
!= state
->len
) {
887 tevent_req_error(req
, EIO
);
891 tevent_req_done(req
);
894 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
898 struct tsocket_address
**src
)
900 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
901 struct tdgram_bsd_recvfrom_state
);
904 ret
= tsocket_simple_int_recv(req
, perrno
);
906 *buf
= talloc_move(mem_ctx
, &state
->buf
);
909 *src
= talloc_move(mem_ctx
, &state
->src
);
913 tevent_req_received(req
);
917 struct tdgram_bsd_sendto_state
{
918 struct tdgram_context
*dgram
;
922 const struct tsocket_address
*dst
;
927 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
929 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
932 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
937 static void tdgram_bsd_sendto_handler(void *private_data
);
939 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
940 struct tevent_context
*ev
,
941 struct tdgram_context
*dgram
,
944 const struct tsocket_address
*dst
)
946 struct tevent_req
*req
;
947 struct tdgram_bsd_sendto_state
*state
;
948 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
951 req
= tevent_req_create(mem_ctx
, &state
,
952 struct tdgram_bsd_sendto_state
);
957 state
->dgram
= dgram
;
963 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
965 if (bsds
->fd
== -1) {
966 tevent_req_error(req
, ENOTCONN
);
971 * this is a fast path, not waiting for the
972 * socket to become explicit writeable gains
973 * about 10%-20% performance in benchmark tests.
975 tdgram_bsd_sendto_handler(req
);
976 if (!tevent_req_is_in_progress(req
)) {
980 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
981 tdgram_bsd_sendto_handler
,
984 tevent_req_error(req
, errno
);
991 tevent_req_post(req
, ev
);
995 static void tdgram_bsd_sendto_handler(void *private_data
)
997 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
999 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1000 struct tdgram_bsd_sendto_state
);
1001 struct tdgram_context
*dgram
= state
->dgram
;
1002 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1003 struct sockaddr
*sa
= NULL
;
1004 socklen_t sa_socklen
= 0;
1010 struct tsocket_address_bsd
*bsda
=
1011 talloc_get_type(state
->dst
->private_data
,
1012 struct tsocket_address_bsd
);
1015 sa_socklen
= sizeof(bsda
->u
.ss
);
1017 * for unix sockets we can't use the size of sockaddr_storage
1018 * we would get EINVAL
1020 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
1021 sa_socklen
= sizeof(bsda
->u
.un
);
1025 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1026 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1031 if (tevent_req_error(req
, err
)) {
1037 tevent_req_done(req
);
1040 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1042 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1043 struct tdgram_bsd_sendto_state
);
1046 ret
= tsocket_simple_int_recv(req
, perrno
);
1051 tevent_req_received(req
);
1055 struct tdgram_bsd_disconnect_state
{
1059 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1060 struct tevent_context
*ev
,
1061 struct tdgram_context
*dgram
)
1063 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1064 struct tevent_req
*req
;
1065 struct tdgram_bsd_disconnect_state
*state
;
1070 req
= tevent_req_create(mem_ctx
, &state
,
1071 struct tdgram_bsd_disconnect_state
);
1076 if (bsds
->fd
== -1) {
1077 tevent_req_error(req
, ENOTCONN
);
1081 ret
= close(bsds
->fd
);
1083 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1084 if (tevent_req_error(req
, err
)) {
1088 tevent_req_done(req
);
1090 tevent_req_post(req
, ev
);
1094 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1099 ret
= tsocket_simple_int_recv(req
, perrno
);
1101 tevent_req_received(req
);
1105 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1108 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1109 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1111 .sendto_send
= tdgram_bsd_sendto_send
,
1112 .sendto_recv
= tdgram_bsd_sendto_recv
,
1114 .disconnect_send
= tdgram_bsd_disconnect_send
,
1115 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1118 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1120 TALLOC_FREE(bsds
->fde
);
1121 if (bsds
->fd
!= -1) {
1128 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1129 const struct tsocket_address
*remote
,
1131 TALLOC_CTX
*mem_ctx
,
1132 struct tdgram_context
**_dgram
,
1133 const char *location
)
1135 struct tsocket_address_bsd
*lbsda
=
1136 talloc_get_type_abort(local
->private_data
,
1137 struct tsocket_address_bsd
);
1138 struct tsocket_address_bsd
*rbsda
= NULL
;
1139 struct tdgram_context
*dgram
;
1140 struct tdgram_bsd
*bsds
;
1143 bool do_bind
= false;
1144 bool do_reuseaddr
= false;
1145 bool is_inet
= false;
1146 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1147 socklen_t sa_socklen
= sizeof(lbsda
->u
.ss
);
1150 rbsda
= talloc_get_type_abort(remote
->private_data
,
1151 struct tsocket_address_bsd
);
1154 switch (lbsda
->u
.sa
.sa_family
) {
1160 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1161 do_reuseaddr
= true;
1165 * for unix sockets we can't use the size of sockaddr_storage
1166 * we would get EINVAL
1168 sa_socklen
= sizeof(lbsda
->u
.un
);
1171 if (lbsda
->u
.in
.sin_port
!= 0) {
1172 do_reuseaddr
= true;
1175 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1179 sa_socklen
= sizeof(rbsda
->u
.in
);
1183 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1184 do_reuseaddr
= true;
1187 if (memcmp(&in6addr_any
,
1188 &lbsda
->u
.in6
.sin6_addr
,
1189 sizeof(in6addr_any
)) != 0) {
1193 sa_socklen
= sizeof(rbsda
->u
.in6
);
1201 if (!do_bind
&& is_inet
&& rbsda
) {
1202 sa_fam
= rbsda
->u
.sa
.sa_family
;
1205 sa_socklen
= sizeof(rbsda
->u
.in
);
1209 sa_socklen
= sizeof(rbsda
->u
.in6
);
1215 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1220 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1225 dgram
= tdgram_context_create(mem_ctx
,
1231 int saved_errno
= errno
;
1233 errno
= saved_errno
;
1238 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1243 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1244 (const void *)&val
, sizeof(val
));
1246 int saved_errno
= errno
;
1248 errno
= saved_errno
;
1256 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1257 (const void *)&val
, sizeof(val
));
1259 int saved_errno
= errno
;
1261 errno
= saved_errno
;
1267 ret
= bind(fd
, &lbsda
->u
.sa
, sa_socklen
);
1269 int saved_errno
= errno
;
1271 errno
= saved_errno
;
1277 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1283 ret
= connect(fd
, &rbsda
->u
.sa
, sa_socklen
);
1285 int saved_errno
= errno
;
1287 errno
= saved_errno
;
1296 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1297 const struct tsocket_address
*remote
,
1298 TALLOC_CTX
*mem_ctx
,
1299 struct tdgram_context
**dgram
,
1300 const char *location
)
1302 struct tsocket_address_bsd
*lbsda
=
1303 talloc_get_type_abort(local
->private_data
,
1304 struct tsocket_address_bsd
);
1307 switch (lbsda
->u
.sa
.sa_family
) {
1319 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1320 mem_ctx
, dgram
, location
);
1325 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1326 const struct tsocket_address
*remote
,
1327 TALLOC_CTX
*mem_ctx
,
1328 struct tdgram_context
**dgram
,
1329 const char *location
)
1331 struct tsocket_address_bsd
*lbsda
=
1332 talloc_get_type_abort(local
->private_data
,
1333 struct tsocket_address_bsd
);
1336 switch (lbsda
->u
.sa
.sa_family
) {
1344 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1345 mem_ctx
, dgram
, location
);
1350 struct tstream_bsd
{
1354 struct tevent_fd
*fde
;
1356 void *readable_private
;
1357 void (*readable_handler
)(void *private_data
);
1358 void *writeable_private
;
1359 void (*writeable_handler
)(void *private_data
);
1362 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1363 struct tevent_fd
*fde
,
1367 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1368 struct tstream_bsd
);
1370 if (flags
& TEVENT_FD_WRITE
) {
1371 bsds
->writeable_handler(bsds
->writeable_private
);
1374 if (flags
& TEVENT_FD_READ
) {
1375 if (!bsds
->readable_handler
) {
1376 if (bsds
->writeable_handler
) {
1377 bsds
->writeable_handler(bsds
->writeable_private
);
1380 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1383 bsds
->readable_handler(bsds
->readable_private
);
1388 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1389 struct tevent_context
*ev
,
1390 void (*handler
)(void *private_data
),
1398 if (!bsds
->readable_handler
) {
1401 bsds
->readable_handler
= NULL
;
1402 bsds
->readable_private
= NULL
;
1407 /* read and write must use the same tevent_context */
1408 if (bsds
->event_ptr
!= ev
) {
1409 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1413 bsds
->event_ptr
= NULL
;
1414 TALLOC_FREE(bsds
->fde
);
1417 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1418 TALLOC_FREE(bsds
->fde
);
1420 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1421 bsds
->fd
, TEVENT_FD_READ
,
1422 tstream_bsd_fde_handler
,
1429 /* cache the event context we're running on */
1430 bsds
->event_ptr
= ev
;
1431 } else if (!bsds
->readable_handler
) {
1432 TEVENT_FD_READABLE(bsds
->fde
);
1435 bsds
->readable_handler
= handler
;
1436 bsds
->readable_private
= private_data
;
1441 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1442 struct tevent_context
*ev
,
1443 void (*handler
)(void *private_data
),
1451 if (!bsds
->writeable_handler
) {
1454 bsds
->writeable_handler
= NULL
;
1455 bsds
->writeable_private
= NULL
;
1456 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1461 /* read and write must use the same tevent_context */
1462 if (bsds
->event_ptr
!= ev
) {
1463 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1467 bsds
->event_ptr
= NULL
;
1468 TALLOC_FREE(bsds
->fde
);
1471 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1472 TALLOC_FREE(bsds
->fde
);
1474 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1476 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1477 tstream_bsd_fde_handler
,
1484 /* cache the event context we're running on */
1485 bsds
->event_ptr
= ev
;
1486 } else if (!bsds
->writeable_handler
) {
1487 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1488 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1489 tevent_fd_set_flags(bsds
->fde
, flags
);
1492 bsds
->writeable_handler
= handler
;
1493 bsds
->writeable_private
= private_data
;
1498 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1500 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1501 struct tstream_bsd
);
1504 if (bsds
->fd
== -1) {
1509 ret
= tsocket_bsd_pending(bsds
->fd
);
1514 struct tstream_bsd_readv_state
{
1515 struct tstream_context
*stream
;
1517 struct iovec
*vector
;
1523 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1525 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1526 struct tstream_bsd
);
1528 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1533 static void tstream_bsd_readv_handler(void *private_data
);
1535 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1536 struct tevent_context
*ev
,
1537 struct tstream_context
*stream
,
1538 struct iovec
*vector
,
1541 struct tevent_req
*req
;
1542 struct tstream_bsd_readv_state
*state
;
1543 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1546 req
= tevent_req_create(mem_ctx
, &state
,
1547 struct tstream_bsd_readv_state
);
1552 state
->stream
= stream
;
1553 /* we make a copy of the vector so that we can modify it */
1554 state
->vector
= talloc_array(state
, struct iovec
, count
);
1555 if (tevent_req_nomem(state
->vector
, req
)) {
1558 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1559 state
->count
= count
;
1562 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1564 if (bsds
->fd
== -1) {
1565 tevent_req_error(req
, ENOTCONN
);
1570 * this is a fast path, not waiting for the
1571 * socket to become explicit readable gains
1572 * about 10%-20% performance in benchmark tests.
1574 tstream_bsd_readv_handler(req
);
1575 if (!tevent_req_is_in_progress(req
)) {
1579 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1580 tstream_bsd_readv_handler
,
1583 tevent_req_error(req
, errno
);
1590 tevent_req_post(req
, ev
);
1594 static void tstream_bsd_readv_handler(void *private_data
)
1596 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1598 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1599 struct tstream_bsd_readv_state
);
1600 struct tstream_context
*stream
= state
->stream
;
1601 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1606 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1608 /* propagate end of file */
1609 tevent_req_error(req
, EPIPE
);
1612 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1617 if (tevent_req_error(req
, err
)) {
1624 if (ret
< state
->vector
[0].iov_len
) {
1626 base
= (uint8_t *)state
->vector
[0].iov_base
;
1628 state
->vector
[0].iov_base
= base
;
1629 state
->vector
[0].iov_len
-= ret
;
1632 ret
-= state
->vector
[0].iov_len
;
1638 * there're maybe some empty vectors at the end
1639 * which we need to skip, otherwise we would get
1640 * ret == 0 from the readv() call and return EPIPE
1642 while (state
->count
> 0) {
1643 if (state
->vector
[0].iov_len
> 0) {
1650 if (state
->count
> 0) {
1651 /* we have more to read */
1655 tevent_req_done(req
);
1658 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1661 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1662 struct tstream_bsd_readv_state
);
1665 ret
= tsocket_simple_int_recv(req
, perrno
);
1670 tevent_req_received(req
);
1674 struct tstream_bsd_writev_state
{
1675 struct tstream_context
*stream
;
1677 struct iovec
*vector
;
1683 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1685 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1686 struct tstream_bsd
);
1688 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1693 static void tstream_bsd_writev_handler(void *private_data
);
1695 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1696 struct tevent_context
*ev
,
1697 struct tstream_context
*stream
,
1698 const struct iovec
*vector
,
1701 struct tevent_req
*req
;
1702 struct tstream_bsd_writev_state
*state
;
1703 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1706 req
= tevent_req_create(mem_ctx
, &state
,
1707 struct tstream_bsd_writev_state
);
1712 state
->stream
= stream
;
1713 /* we make a copy of the vector so that we can modify it */
1714 state
->vector
= talloc_array(state
, struct iovec
, count
);
1715 if (tevent_req_nomem(state
->vector
, req
)) {
1718 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1719 state
->count
= count
;
1722 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1724 if (bsds
->fd
== -1) {
1725 tevent_req_error(req
, ENOTCONN
);
1730 * this is a fast path, not waiting for the
1731 * socket to become explicit writeable gains
1732 * about 10%-20% performance in benchmark tests.
1734 tstream_bsd_writev_handler(req
);
1735 if (!tevent_req_is_in_progress(req
)) {
1739 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1740 tstream_bsd_writev_handler
,
1743 tevent_req_error(req
, errno
);
1750 tevent_req_post(req
, ev
);
1754 static void tstream_bsd_writev_handler(void *private_data
)
1756 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1758 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1759 struct tstream_bsd_writev_state
);
1760 struct tstream_context
*stream
= state
->stream
;
1761 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1766 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1768 /* propagate end of file */
1769 tevent_req_error(req
, EPIPE
);
1772 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1777 if (tevent_req_error(req
, err
)) {
1784 if (ret
< state
->vector
[0].iov_len
) {
1786 base
= (uint8_t *)state
->vector
[0].iov_base
;
1788 state
->vector
[0].iov_base
= base
;
1789 state
->vector
[0].iov_len
-= ret
;
1792 ret
-= state
->vector
[0].iov_len
;
1798 * there're maybe some empty vectors at the end
1799 * which we need to skip, otherwise we would get
1800 * ret == 0 from the writev() call and return EPIPE
1802 while (state
->count
> 0) {
1803 if (state
->vector
[0].iov_len
> 0) {
1810 if (state
->count
> 0) {
1811 /* we have more to read */
1815 tevent_req_done(req
);
1818 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1820 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1821 struct tstream_bsd_writev_state
);
1824 ret
= tsocket_simple_int_recv(req
, perrno
);
1829 tevent_req_received(req
);
1833 struct tstream_bsd_disconnect_state
{
1837 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1838 struct tevent_context
*ev
,
1839 struct tstream_context
*stream
)
1841 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1842 struct tevent_req
*req
;
1843 struct tstream_bsd_disconnect_state
*state
;
1848 req
= tevent_req_create(mem_ctx
, &state
,
1849 struct tstream_bsd_disconnect_state
);
1854 if (bsds
->fd
== -1) {
1855 tevent_req_error(req
, ENOTCONN
);
1859 ret
= close(bsds
->fd
);
1861 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1862 if (tevent_req_error(req
, err
)) {
1866 tevent_req_done(req
);
1868 tevent_req_post(req
, ev
);
1872 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1877 ret
= tsocket_simple_int_recv(req
, perrno
);
1879 tevent_req_received(req
);
1883 static const struct tstream_context_ops tstream_bsd_ops
= {
1886 .pending_bytes
= tstream_bsd_pending_bytes
,
1888 .readv_send
= tstream_bsd_readv_send
,
1889 .readv_recv
= tstream_bsd_readv_recv
,
1891 .writev_send
= tstream_bsd_writev_send
,
1892 .writev_recv
= tstream_bsd_writev_recv
,
1894 .disconnect_send
= tstream_bsd_disconnect_send
,
1895 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1898 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1900 TALLOC_FREE(bsds
->fde
);
1901 if (bsds
->fd
!= -1) {
1908 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1910 struct tstream_context
**_stream
,
1911 const char *location
)
1913 struct tstream_context
*stream
;
1914 struct tstream_bsd
*bsds
;
1916 stream
= tstream_context_create(mem_ctx
,
1926 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1932 struct tstream_bsd_connect_state
{
1934 struct tevent_fd
*fde
;
1935 struct tstream_conext
*stream
;
1938 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1940 TALLOC_FREE(state
->fde
);
1941 if (state
->fd
!= -1) {
1949 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1950 struct tevent_fd
*fde
,
1952 void *private_data
);
1954 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1955 struct tevent_context
*ev
,
1957 const struct tsocket_address
*local
,
1958 const struct tsocket_address
*remote
)
1960 struct tevent_req
*req
;
1961 struct tstream_bsd_connect_state
*state
;
1962 struct tsocket_address_bsd
*lbsda
=
1963 talloc_get_type_abort(local
->private_data
,
1964 struct tsocket_address_bsd
);
1965 struct tsocket_address_bsd
*rbsda
=
1966 talloc_get_type_abort(remote
->private_data
,
1967 struct tsocket_address_bsd
);
1971 bool do_bind
= false;
1972 bool do_reuseaddr
= false;
1973 bool is_inet
= false;
1974 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1975 socklen_t sa_socklen
= sizeof(rbsda
->u
.ss
);
1977 req
= tevent_req_create(mem_ctx
, &state
,
1978 struct tstream_bsd_connect_state
);
1985 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
1987 /* give the wrappers a chance to report an error */
1988 if (sys_errno
!= 0) {
1989 tevent_req_error(req
, sys_errno
);
1993 switch (lbsda
->u
.sa
.sa_family
) {
1995 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1996 do_reuseaddr
= true;
2000 * for unix sockets we can't use the size of sockaddr_storage
2001 * we would get EINVAL
2003 sa_socklen
= sizeof(rbsda
->u
.un
);
2006 if (lbsda
->u
.in
.sin_port
!= 0) {
2007 do_reuseaddr
= true;
2010 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2014 sa_socklen
= sizeof(rbsda
->u
.in
);
2018 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2019 do_reuseaddr
= true;
2022 if (memcmp(&in6addr_any
,
2023 &lbsda
->u
.in6
.sin6_addr
,
2024 sizeof(in6addr_any
)) != 0) {
2028 sa_socklen
= sizeof(rbsda
->u
.in6
);
2032 tevent_req_error(req
, EINVAL
);
2036 if (!do_bind
&& is_inet
) {
2037 sa_fam
= rbsda
->u
.sa
.sa_family
;
2040 sa_socklen
= sizeof(rbsda
->u
.in
);
2044 sa_socklen
= sizeof(rbsda
->u
.in6
);
2050 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2051 if (state
->fd
== -1) {
2052 tevent_req_error(req
, errno
);
2056 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2057 if (state
->fd
== -1) {
2058 tevent_req_error(req
, errno
);
2065 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2066 (const void *)&val
, sizeof(val
));
2068 tevent_req_error(req
, errno
);
2074 ret
= bind(state
->fd
, &lbsda
->u
.sa
, sa_socklen
);
2076 tevent_req_error(req
, errno
);
2081 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2082 tevent_req_error(req
, EINVAL
);
2086 ret
= connect(state
->fd
, &rbsda
->u
.sa
, sa_socklen
);
2087 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2092 if (tevent_req_error(req
, err
)) {
2096 tevent_req_done(req
);
2100 state
->fde
= tevent_add_fd(ev
, state
,
2102 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2103 tstream_bsd_connect_fde_handler
,
2105 if (tevent_req_nomem(state
->fde
, req
)) {
2112 tevent_req_post(req
, ev
);
2116 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2117 struct tevent_fd
*fde
,
2121 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2123 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2124 struct tstream_bsd_connect_state
);
2127 socklen_t len
= sizeof(error
);
2131 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2138 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2143 if (tevent_req_error(req
, err
)) {
2147 tevent_req_done(req
);
2150 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2152 TALLOC_CTX
*mem_ctx
,
2153 struct tstream_context
**stream
,
2154 const char *location
)
2156 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2157 struct tstream_bsd_connect_state
);
2160 ret
= tsocket_simple_int_recv(req
, perrno
);
2162 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2170 TALLOC_FREE(state
->fde
);
2175 tevent_req_received(req
);
2179 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2180 struct tevent_context
*ev
,
2181 const struct tsocket_address
*local
,
2182 const struct tsocket_address
*remote
)
2184 struct tsocket_address_bsd
*lbsda
=
2185 talloc_get_type_abort(local
->private_data
,
2186 struct tsocket_address_bsd
);
2187 struct tevent_req
*req
;
2190 switch (lbsda
->u
.sa
.sa_family
) {
2202 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2207 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2209 TALLOC_CTX
*mem_ctx
,
2210 struct tstream_context
**stream
,
2211 const char *location
)
2213 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2216 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2217 struct tevent_context
*ev
,
2218 const struct tsocket_address
*local
,
2219 const struct tsocket_address
*remote
)
2221 struct tsocket_address_bsd
*lbsda
=
2222 talloc_get_type_abort(local
->private_data
,
2223 struct tsocket_address_bsd
);
2224 struct tevent_req
*req
;
2227 switch (lbsda
->u
.sa
.sa_family
) {
2235 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2240 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2242 TALLOC_CTX
*mem_ctx
,
2243 struct tstream_context
**stream
,
2244 const char *location
)
2246 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2249 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2250 struct tstream_context
**_stream1
,
2251 TALLOC_CTX
*mem_ctx2
,
2252 struct tstream_context
**_stream2
,
2253 const char *location
)
2259 struct tstream_context
*stream1
= NULL
;
2260 struct tstream_context
*stream2
= NULL
;
2262 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2269 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2271 int sys_errno
= errno
;
2277 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2279 int sys_errno
= errno
;
2285 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2290 int sys_errno
= errno
;
2297 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2302 int sys_errno
= errno
;
2303 talloc_free(stream1
);
2309 *_stream1
= stream1
;
2310 *_stream2
= stream2
;