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 switch (sa
->sa_family
) {
215 if (sa_socklen
< sizeof(struct sockaddr_un
)) {
221 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
228 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
235 errno
= EAFNOSUPPORT
;
239 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
244 addr
= tsocket_address_create(mem_ctx
,
245 &tsocket_address_bsd_ops
,
247 struct tsocket_address_bsd
,
256 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
262 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
266 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
267 struct tsocket_address_bsd
);
275 switch (bsda
->u
.sa
.sa_family
) {
277 rlen
= sizeof(struct sockaddr_un
);
280 rlen
= sizeof(struct sockaddr_in
);
284 rlen
= sizeof(struct sockaddr_in6
);
288 errno
= EAFNOSUPPORT
;
292 if (sa_socklen
< rlen
) {
297 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
298 memset(sa
, 0, sa_socklen
);
299 sa_socklen
= sizeof(struct sockaddr_storage
);
302 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
306 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
310 struct tsocket_address
**_addr
,
311 const char *location
)
313 struct addrinfo hints
;
314 struct addrinfo
*result
= NULL
;
320 * we use SOCKET_STREAM here to get just one result
321 * back from getaddrinfo().
323 hints
.ai_socktype
= SOCK_STREAM
;
324 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
326 if (strcasecmp(fam
, "ip") == 0) {
327 hints
.ai_family
= AF_UNSPEC
;
335 } else if (strcasecmp(fam
, "ipv4") == 0) {
336 hints
.ai_family
= AF_INET
;
341 } else if (strcasecmp(fam
, "ipv6") == 0) {
342 hints
.ai_family
= AF_INET6
;
348 errno
= EAFNOSUPPORT
;
352 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
354 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
365 if (result
->ai_socktype
!= SOCK_STREAM
) {
371 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
379 freeaddrinfo(result
);
384 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
387 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
388 struct tsocket_address_bsd
);
389 char addr_str
[INET6_ADDRSTRLEN
+1];
397 switch (bsda
->u
.sa
.sa_family
) {
399 str
= inet_ntop(bsda
->u
.in
.sin_family
,
400 &bsda
->u
.in
.sin_addr
,
401 addr_str
, sizeof(addr_str
));
405 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
406 &bsda
->u
.in6
.sin6_addr
,
407 addr_str
, sizeof(addr_str
));
419 return talloc_strdup(mem_ctx
, str
);
422 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
424 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
425 struct tsocket_address_bsd
);
433 switch (bsda
->u
.sa
.sa_family
) {
435 port
= ntohs(bsda
->u
.in
.sin_port
);
439 port
= ntohs(bsda
->u
.in6
.sin6_port
);
450 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
453 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
454 struct tsocket_address_bsd
);
461 switch (bsda
->u
.sa
.sa_family
) {
463 bsda
->u
.in
.sin_port
= htons(port
);
467 bsda
->u
.in6
.sin6_port
= htons(port
);
478 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
480 struct tsocket_address
**_addr
,
481 const char *location
)
483 struct sockaddr_un un
;
491 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
492 errno
= ENAMETOOLONG
;
497 un
.sun_family
= AF_UNIX
;
498 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
500 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
501 (struct sockaddr
*)p
,
509 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
512 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
513 struct tsocket_address_bsd
);
521 switch (bsda
->u
.sa
.sa_family
) {
523 str
= bsda
->u
.un
.sun_path
;
530 return talloc_strdup(mem_ctx
, str
);
533 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
536 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
537 struct tsocket_address_bsd
);
540 const char *prefix
= NULL
;
543 switch (bsda
->u
.sa
.sa_family
) {
545 return talloc_asprintf(mem_ctx
, "unix:%s",
546 bsda
->u
.un
.sun_path
);
560 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
565 port
= tsocket_address_inet_port(addr
);
567 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
568 prefix
, addr_str
, port
);
569 talloc_free(addr_str
);
574 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
576 const char *location
)
578 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
579 struct tsocket_address_bsd
);
580 struct tsocket_address
*copy
;
583 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
595 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
597 .string
= tsocket_address_bsd_string
,
598 .copy
= tsocket_address_bsd_copy
,
605 struct tevent_fd
*fde
;
607 void *readable_private
;
608 void (*readable_handler
)(void *private_data
);
609 void *writeable_private
;
610 void (*writeable_handler
)(void *private_data
);
613 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
614 struct tevent_fd
*fde
,
618 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
621 if (flags
& TEVENT_FD_WRITE
) {
622 bsds
->writeable_handler(bsds
->writeable_private
);
625 if (flags
& TEVENT_FD_READ
) {
626 if (!bsds
->readable_handler
) {
627 TEVENT_FD_NOT_READABLE(bsds
->fde
);
630 bsds
->readable_handler(bsds
->readable_private
);
635 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
636 struct tevent_context
*ev
,
637 void (*handler
)(void *private_data
),
645 if (!bsds
->readable_handler
) {
648 bsds
->readable_handler
= NULL
;
649 bsds
->readable_private
= NULL
;
654 /* read and write must use the same tevent_context */
655 if (bsds
->event_ptr
!= ev
) {
656 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
660 bsds
->event_ptr
= NULL
;
661 TALLOC_FREE(bsds
->fde
);
664 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
665 TALLOC_FREE(bsds
->fde
);
667 bsds
->fde
= tevent_add_fd(ev
, bsds
,
668 bsds
->fd
, TEVENT_FD_READ
,
669 tdgram_bsd_fde_handler
,
676 /* cache the event context we're running on */
677 bsds
->event_ptr
= ev
;
678 } else if (!bsds
->readable_handler
) {
679 TEVENT_FD_READABLE(bsds
->fde
);
682 bsds
->readable_handler
= handler
;
683 bsds
->readable_private
= private_data
;
688 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
689 struct tevent_context
*ev
,
690 void (*handler
)(void *private_data
),
698 if (!bsds
->writeable_handler
) {
701 bsds
->writeable_handler
= NULL
;
702 bsds
->writeable_private
= NULL
;
703 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
708 /* read and write must use the same tevent_context */
709 if (bsds
->event_ptr
!= ev
) {
710 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
714 bsds
->event_ptr
= NULL
;
715 TALLOC_FREE(bsds
->fde
);
718 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
719 TALLOC_FREE(bsds
->fde
);
721 bsds
->fde
= tevent_add_fd(ev
, bsds
,
722 bsds
->fd
, TEVENT_FD_WRITE
,
723 tdgram_bsd_fde_handler
,
730 /* cache the event context we're running on */
731 bsds
->event_ptr
= ev
;
732 } else if (!bsds
->writeable_handler
) {
733 TEVENT_FD_WRITEABLE(bsds
->fde
);
736 bsds
->writeable_handler
= handler
;
737 bsds
->writeable_private
= private_data
;
742 struct tdgram_bsd_recvfrom_state
{
743 struct tdgram_context
*dgram
;
747 struct tsocket_address
*src
;
750 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
752 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
755 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
760 static void tdgram_bsd_recvfrom_handler(void *private_data
);
762 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
763 struct tevent_context
*ev
,
764 struct tdgram_context
*dgram
)
766 struct tevent_req
*req
;
767 struct tdgram_bsd_recvfrom_state
*state
;
768 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
771 req
= tevent_req_create(mem_ctx
, &state
,
772 struct tdgram_bsd_recvfrom_state
);
777 state
->dgram
= dgram
;
782 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
784 if (bsds
->fd
== -1) {
785 tevent_req_error(req
, ENOTCONN
);
790 * this is a fast path, not waiting for the
791 * socket to become explicit readable gains
792 * about 10%-20% performance in benchmark tests.
794 tdgram_bsd_recvfrom_handler(req
);
795 if (!tevent_req_is_in_progress(req
)) {
799 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
800 tdgram_bsd_recvfrom_handler
,
803 tevent_req_error(req
, errno
);
810 tevent_req_post(req
, ev
);
814 static void tdgram_bsd_recvfrom_handler(void *private_data
)
816 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
818 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
819 struct tdgram_bsd_recvfrom_state
);
820 struct tdgram_context
*dgram
= state
->dgram
;
821 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
822 struct tsocket_address_bsd
*bsda
;
824 struct sockaddr
*sa
= NULL
;
825 socklen_t sa_socklen
= 0;
829 ret
= tsocket_bsd_pending(bsds
->fd
);
834 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
839 if (tevent_req_error(req
, err
)) {
843 state
->buf
= talloc_array(state
, uint8_t, ret
);
844 if (tevent_req_nomem(state
->buf
, req
)) {
849 state
->src
= tsocket_address_create(state
,
850 &tsocket_address_bsd_ops
,
852 struct tsocket_address_bsd
,
853 __location__
"bsd_recvfrom");
854 if (tevent_req_nomem(state
->src
, req
)) {
861 sa_socklen
= sizeof(bsda
->u
.ss
);
863 * for unix sockets we can't use the size of sockaddr_storage
864 * we would get EINVAL
866 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
867 sa_socklen
= sizeof(bsda
->u
.un
);
870 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, &sa_socklen
);
871 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
876 if (tevent_req_error(req
, err
)) {
880 if (ret
!= state
->len
) {
881 tevent_req_error(req
, EIO
);
885 tevent_req_done(req
);
888 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
892 struct tsocket_address
**src
)
894 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
895 struct tdgram_bsd_recvfrom_state
);
898 ret
= tsocket_simple_int_recv(req
, perrno
);
900 *buf
= talloc_move(mem_ctx
, &state
->buf
);
903 *src
= talloc_move(mem_ctx
, &state
->src
);
907 tevent_req_received(req
);
911 struct tdgram_bsd_sendto_state
{
912 struct tdgram_context
*dgram
;
916 const struct tsocket_address
*dst
;
921 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
923 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
926 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
931 static void tdgram_bsd_sendto_handler(void *private_data
);
933 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
934 struct tevent_context
*ev
,
935 struct tdgram_context
*dgram
,
938 const struct tsocket_address
*dst
)
940 struct tevent_req
*req
;
941 struct tdgram_bsd_sendto_state
*state
;
942 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
945 req
= tevent_req_create(mem_ctx
, &state
,
946 struct tdgram_bsd_sendto_state
);
951 state
->dgram
= dgram
;
957 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
959 if (bsds
->fd
== -1) {
960 tevent_req_error(req
, ENOTCONN
);
965 * this is a fast path, not waiting for the
966 * socket to become explicit writeable gains
967 * about 10%-20% performance in benchmark tests.
969 tdgram_bsd_sendto_handler(req
);
970 if (!tevent_req_is_in_progress(req
)) {
974 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
975 tdgram_bsd_sendto_handler
,
978 tevent_req_error(req
, errno
);
985 tevent_req_post(req
, ev
);
989 static void tdgram_bsd_sendto_handler(void *private_data
)
991 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
993 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
994 struct tdgram_bsd_sendto_state
);
995 struct tdgram_context
*dgram
= state
->dgram
;
996 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
997 struct sockaddr
*sa
= NULL
;
998 socklen_t sa_socklen
= 0;
1004 struct tsocket_address_bsd
*bsda
=
1005 talloc_get_type(state
->dst
->private_data
,
1006 struct tsocket_address_bsd
);
1009 sa_socklen
= sizeof(bsda
->u
.ss
);
1011 * for unix sockets we can't use the size of sockaddr_storage
1012 * we would get EINVAL
1014 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
1015 sa_socklen
= sizeof(bsda
->u
.un
);
1019 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1020 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1025 if (tevent_req_error(req
, err
)) {
1031 tevent_req_done(req
);
1034 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1036 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1037 struct tdgram_bsd_sendto_state
);
1040 ret
= tsocket_simple_int_recv(req
, perrno
);
1045 tevent_req_received(req
);
1049 struct tdgram_bsd_disconnect_state
{
1053 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1054 struct tevent_context
*ev
,
1055 struct tdgram_context
*dgram
)
1057 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1058 struct tevent_req
*req
;
1059 struct tdgram_bsd_disconnect_state
*state
;
1064 req
= tevent_req_create(mem_ctx
, &state
,
1065 struct tdgram_bsd_disconnect_state
);
1070 if (bsds
->fd
== -1) {
1071 tevent_req_error(req
, ENOTCONN
);
1075 ret
= close(bsds
->fd
);
1077 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1078 if (tevent_req_error(req
, err
)) {
1082 tevent_req_done(req
);
1084 tevent_req_post(req
, ev
);
1088 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1093 ret
= tsocket_simple_int_recv(req
, perrno
);
1095 tevent_req_received(req
);
1099 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1102 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1103 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1105 .sendto_send
= tdgram_bsd_sendto_send
,
1106 .sendto_recv
= tdgram_bsd_sendto_recv
,
1108 .disconnect_send
= tdgram_bsd_disconnect_send
,
1109 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1112 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1114 TALLOC_FREE(bsds
->fde
);
1115 if (bsds
->fd
!= -1) {
1122 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1123 const struct tsocket_address
*remote
,
1125 TALLOC_CTX
*mem_ctx
,
1126 struct tdgram_context
**_dgram
,
1127 const char *location
)
1129 struct tsocket_address_bsd
*lbsda
=
1130 talloc_get_type_abort(local
->private_data
,
1131 struct tsocket_address_bsd
);
1132 struct tsocket_address_bsd
*rbsda
= NULL
;
1133 struct tdgram_context
*dgram
;
1134 struct tdgram_bsd
*bsds
;
1137 bool do_bind
= false;
1138 bool do_reuseaddr
= false;
1139 socklen_t sa_socklen
= sizeof(lbsda
->u
.ss
);
1142 rbsda
= talloc_get_type_abort(remote
->private_data
,
1143 struct tsocket_address_bsd
);
1146 switch (lbsda
->u
.sa
.sa_family
) {
1152 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1153 do_reuseaddr
= true;
1157 * for unix sockets we can't use the size of sockaddr_storage
1158 * we would get EINVAL
1160 sa_socklen
= sizeof(lbsda
->u
.un
);
1163 if (lbsda
->u
.in
.sin_port
!= 0) {
1164 do_reuseaddr
= true;
1167 if (lbsda
->u
.in
.sin_addr
.s_addr
== INADDR_ANY
) {
1173 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1174 do_reuseaddr
= true;
1177 if (memcmp(&in6addr_any
,
1178 &lbsda
->u
.in6
.sin6_addr
,
1179 sizeof(in6addr_any
)) != 0) {
1189 fd
= socket(lbsda
->u
.sa
.sa_family
, SOCK_DGRAM
, 0);
1194 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1199 dgram
= tdgram_context_create(mem_ctx
,
1205 int saved_errno
= errno
;
1207 errno
= saved_errno
;
1212 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1217 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1218 (const void *)&val
, sizeof(val
));
1220 int saved_errno
= errno
;
1222 errno
= saved_errno
;
1230 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1231 (const void *)&val
, sizeof(val
));
1233 int saved_errno
= errno
;
1235 errno
= saved_errno
;
1241 ret
= bind(fd
, &lbsda
->u
.sa
, sa_socklen
);
1243 int saved_errno
= errno
;
1245 errno
= saved_errno
;
1251 ret
= connect(fd
, &rbsda
->u
.sa
, sa_socklen
);
1253 int saved_errno
= errno
;
1255 errno
= saved_errno
;
1264 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1265 const struct tsocket_address
*remote
,
1266 TALLOC_CTX
*mem_ctx
,
1267 struct tdgram_context
**dgram
,
1268 const char *location
)
1270 struct tsocket_address_bsd
*lbsda
=
1271 talloc_get_type_abort(local
->private_data
,
1272 struct tsocket_address_bsd
);
1275 switch (lbsda
->u
.sa
.sa_family
) {
1287 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1288 mem_ctx
, dgram
, location
);
1293 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1294 const struct tsocket_address
*remote
,
1295 TALLOC_CTX
*mem_ctx
,
1296 struct tdgram_context
**dgram
,
1297 const char *location
)
1299 struct tsocket_address_bsd
*lbsda
=
1300 talloc_get_type_abort(local
->private_data
,
1301 struct tsocket_address_bsd
);
1304 switch (lbsda
->u
.sa
.sa_family
) {
1312 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1313 mem_ctx
, dgram
, location
);
1318 struct tstream_bsd
{
1322 struct tevent_fd
*fde
;
1324 void *readable_private
;
1325 void (*readable_handler
)(void *private_data
);
1326 void *writeable_private
;
1327 void (*writeable_handler
)(void *private_data
);
1330 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1331 struct tevent_fd
*fde
,
1335 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1336 struct tstream_bsd
);
1338 if (flags
& TEVENT_FD_WRITE
) {
1339 bsds
->writeable_handler(bsds
->writeable_private
);
1342 if (flags
& TEVENT_FD_READ
) {
1343 if (!bsds
->readable_handler
) {
1344 if (bsds
->writeable_handler
) {
1345 bsds
->writeable_handler(bsds
->writeable_private
);
1348 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1351 bsds
->readable_handler(bsds
->readable_private
);
1356 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1357 struct tevent_context
*ev
,
1358 void (*handler
)(void *private_data
),
1366 if (!bsds
->readable_handler
) {
1369 bsds
->readable_handler
= NULL
;
1370 bsds
->readable_private
= NULL
;
1375 /* read and write must use the same tevent_context */
1376 if (bsds
->event_ptr
!= ev
) {
1377 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1381 bsds
->event_ptr
= NULL
;
1382 TALLOC_FREE(bsds
->fde
);
1385 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1386 TALLOC_FREE(bsds
->fde
);
1388 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1389 bsds
->fd
, TEVENT_FD_READ
,
1390 tstream_bsd_fde_handler
,
1397 /* cache the event context we're running on */
1398 bsds
->event_ptr
= ev
;
1399 } else if (!bsds
->readable_handler
) {
1400 TEVENT_FD_READABLE(bsds
->fde
);
1403 bsds
->readable_handler
= handler
;
1404 bsds
->readable_private
= private_data
;
1409 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1410 struct tevent_context
*ev
,
1411 void (*handler
)(void *private_data
),
1419 if (!bsds
->writeable_handler
) {
1422 bsds
->writeable_handler
= NULL
;
1423 bsds
->writeable_private
= NULL
;
1424 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1429 /* read and write must use the same tevent_context */
1430 if (bsds
->event_ptr
!= ev
) {
1431 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1435 bsds
->event_ptr
= NULL
;
1436 TALLOC_FREE(bsds
->fde
);
1439 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1440 TALLOC_FREE(bsds
->fde
);
1442 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1444 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1445 tstream_bsd_fde_handler
,
1452 /* cache the event context we're running on */
1453 bsds
->event_ptr
= ev
;
1454 } else if (!bsds
->writeable_handler
) {
1455 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1456 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1457 tevent_fd_set_flags(bsds
->fde
, flags
);
1460 bsds
->writeable_handler
= handler
;
1461 bsds
->writeable_private
= private_data
;
1466 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1468 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1469 struct tstream_bsd
);
1472 if (bsds
->fd
== -1) {
1477 ret
= tsocket_bsd_pending(bsds
->fd
);
1482 struct tstream_bsd_readv_state
{
1483 struct tstream_context
*stream
;
1485 struct iovec
*vector
;
1491 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1493 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1494 struct tstream_bsd
);
1496 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1501 static void tstream_bsd_readv_handler(void *private_data
);
1503 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1504 struct tevent_context
*ev
,
1505 struct tstream_context
*stream
,
1506 struct iovec
*vector
,
1509 struct tevent_req
*req
;
1510 struct tstream_bsd_readv_state
*state
;
1511 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1514 req
= tevent_req_create(mem_ctx
, &state
,
1515 struct tstream_bsd_readv_state
);
1520 state
->stream
= stream
;
1521 /* we make a copy of the vector so that we can modify it */
1522 state
->vector
= talloc_array(state
, struct iovec
, count
);
1523 if (tevent_req_nomem(state
->vector
, req
)) {
1526 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1527 state
->count
= count
;
1530 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1532 if (bsds
->fd
== -1) {
1533 tevent_req_error(req
, ENOTCONN
);
1538 * this is a fast path, not waiting for the
1539 * socket to become explicit readable gains
1540 * about 10%-20% performance in benchmark tests.
1542 tstream_bsd_readv_handler(req
);
1543 if (!tevent_req_is_in_progress(req
)) {
1547 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1548 tstream_bsd_readv_handler
,
1551 tevent_req_error(req
, errno
);
1558 tevent_req_post(req
, ev
);
1562 static void tstream_bsd_readv_handler(void *private_data
)
1564 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1566 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1567 struct tstream_bsd_readv_state
);
1568 struct tstream_context
*stream
= state
->stream
;
1569 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1574 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1576 /* propagate end of file */
1577 tevent_req_error(req
, EPIPE
);
1580 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1585 if (tevent_req_error(req
, err
)) {
1592 if (ret
< state
->vector
[0].iov_len
) {
1594 base
= (uint8_t *)state
->vector
[0].iov_base
;
1596 state
->vector
[0].iov_base
= base
;
1597 state
->vector
[0].iov_len
-= ret
;
1600 ret
-= state
->vector
[0].iov_len
;
1606 * there're maybe some empty vectors at the end
1607 * which we need to skip, otherwise we would get
1608 * ret == 0 from the readv() call and return EPIPE
1610 while (state
->count
> 0) {
1611 if (state
->vector
[0].iov_len
> 0) {
1618 if (state
->count
> 0) {
1619 /* we have more to read */
1623 tevent_req_done(req
);
1626 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1629 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1630 struct tstream_bsd_readv_state
);
1633 ret
= tsocket_simple_int_recv(req
, perrno
);
1638 tevent_req_received(req
);
1642 struct tstream_bsd_writev_state
{
1643 struct tstream_context
*stream
;
1645 struct iovec
*vector
;
1651 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1653 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1654 struct tstream_bsd
);
1656 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1661 static void tstream_bsd_writev_handler(void *private_data
);
1663 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1664 struct tevent_context
*ev
,
1665 struct tstream_context
*stream
,
1666 const struct iovec
*vector
,
1669 struct tevent_req
*req
;
1670 struct tstream_bsd_writev_state
*state
;
1671 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1674 req
= tevent_req_create(mem_ctx
, &state
,
1675 struct tstream_bsd_writev_state
);
1680 state
->stream
= stream
;
1681 /* we make a copy of the vector so that we can modify it */
1682 state
->vector
= talloc_array(state
, struct iovec
, count
);
1683 if (tevent_req_nomem(state
->vector
, req
)) {
1686 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1687 state
->count
= count
;
1690 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1692 if (bsds
->fd
== -1) {
1693 tevent_req_error(req
, ENOTCONN
);
1698 * this is a fast path, not waiting for the
1699 * socket to become explicit writeable gains
1700 * about 10%-20% performance in benchmark tests.
1702 tstream_bsd_writev_handler(req
);
1703 if (!tevent_req_is_in_progress(req
)) {
1707 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1708 tstream_bsd_writev_handler
,
1711 tevent_req_error(req
, errno
);
1718 tevent_req_post(req
, ev
);
1722 static void tstream_bsd_writev_handler(void *private_data
)
1724 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1726 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1727 struct tstream_bsd_writev_state
);
1728 struct tstream_context
*stream
= state
->stream
;
1729 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1734 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1736 /* propagate end of file */
1737 tevent_req_error(req
, EPIPE
);
1740 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1745 if (tevent_req_error(req
, err
)) {
1752 if (ret
< state
->vector
[0].iov_len
) {
1754 base
= (uint8_t *)state
->vector
[0].iov_base
;
1756 state
->vector
[0].iov_base
= base
;
1757 state
->vector
[0].iov_len
-= ret
;
1760 ret
-= state
->vector
[0].iov_len
;
1766 * there're maybe some empty vectors at the end
1767 * which we need to skip, otherwise we would get
1768 * ret == 0 from the writev() call and return EPIPE
1770 while (state
->count
> 0) {
1771 if (state
->vector
[0].iov_len
> 0) {
1778 if (state
->count
> 0) {
1779 /* we have more to read */
1783 tevent_req_done(req
);
1786 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1788 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1789 struct tstream_bsd_writev_state
);
1792 ret
= tsocket_simple_int_recv(req
, perrno
);
1797 tevent_req_received(req
);
1801 struct tstream_bsd_disconnect_state
{
1805 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1806 struct tevent_context
*ev
,
1807 struct tstream_context
*stream
)
1809 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1810 struct tevent_req
*req
;
1811 struct tstream_bsd_disconnect_state
*state
;
1816 req
= tevent_req_create(mem_ctx
, &state
,
1817 struct tstream_bsd_disconnect_state
);
1822 if (bsds
->fd
== -1) {
1823 tevent_req_error(req
, ENOTCONN
);
1827 ret
= close(bsds
->fd
);
1829 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1830 if (tevent_req_error(req
, err
)) {
1834 tevent_req_done(req
);
1836 tevent_req_post(req
, ev
);
1840 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1845 ret
= tsocket_simple_int_recv(req
, perrno
);
1847 tevent_req_received(req
);
1851 static const struct tstream_context_ops tstream_bsd_ops
= {
1854 .pending_bytes
= tstream_bsd_pending_bytes
,
1856 .readv_send
= tstream_bsd_readv_send
,
1857 .readv_recv
= tstream_bsd_readv_recv
,
1859 .writev_send
= tstream_bsd_writev_send
,
1860 .writev_recv
= tstream_bsd_writev_recv
,
1862 .disconnect_send
= tstream_bsd_disconnect_send
,
1863 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1866 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1868 TALLOC_FREE(bsds
->fde
);
1869 if (bsds
->fd
!= -1) {
1876 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1878 struct tstream_context
**_stream
,
1879 const char *location
)
1881 struct tstream_context
*stream
;
1882 struct tstream_bsd
*bsds
;
1884 stream
= tstream_context_create(mem_ctx
,
1894 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1900 struct tstream_bsd_connect_state
{
1902 struct tevent_fd
*fde
;
1903 struct tstream_conext
*stream
;
1906 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1908 TALLOC_FREE(state
->fde
);
1909 if (state
->fd
!= -1) {
1917 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1918 struct tevent_fd
*fde
,
1920 void *private_data
);
1922 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1923 struct tevent_context
*ev
,
1925 const struct tsocket_address
*local
,
1926 const struct tsocket_address
*remote
)
1928 struct tevent_req
*req
;
1929 struct tstream_bsd_connect_state
*state
;
1930 struct tsocket_address_bsd
*lbsda
=
1931 talloc_get_type_abort(local
->private_data
,
1932 struct tsocket_address_bsd
);
1933 struct tsocket_address_bsd
*rbsda
=
1934 talloc_get_type_abort(remote
->private_data
,
1935 struct tsocket_address_bsd
);
1939 bool do_bind
= false;
1940 bool do_reuseaddr
= false;
1941 socklen_t sa_socklen
= sizeof(rbsda
->u
.ss
);
1943 req
= tevent_req_create(mem_ctx
, &state
,
1944 struct tstream_bsd_connect_state
);
1951 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
1953 /* give the wrappers a chance to report an error */
1954 if (sys_errno
!= 0) {
1955 tevent_req_error(req
, sys_errno
);
1959 switch (lbsda
->u
.sa
.sa_family
) {
1961 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1962 do_reuseaddr
= true;
1966 * for unix sockets we can't use the size of sockaddr_storage
1967 * we would get EINVAL
1969 sa_socklen
= sizeof(rbsda
->u
.un
);
1972 if (lbsda
->u
.in
.sin_port
!= 0) {
1973 do_reuseaddr
= true;
1976 if (lbsda
->u
.in
.sin_addr
.s_addr
== INADDR_ANY
) {
1982 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1983 do_reuseaddr
= true;
1986 if (memcmp(&in6addr_any
,
1987 &lbsda
->u
.in6
.sin6_addr
,
1988 sizeof(in6addr_any
)) != 0) {
1994 tevent_req_error(req
, EINVAL
);
1998 state
->fd
= socket(lbsda
->u
.sa
.sa_family
, SOCK_STREAM
, 0);
1999 if (state
->fd
== -1) {
2000 tevent_req_error(req
, errno
);
2004 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2005 if (state
->fd
== -1) {
2006 tevent_req_error(req
, errno
);
2013 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2014 (const void *)&val
, sizeof(val
));
2016 tevent_req_error(req
, errno
);
2022 ret
= bind(state
->fd
, &lbsda
->u
.sa
, sizeof(lbsda
->u
.ss
));
2024 tevent_req_error(req
, errno
);
2029 ret
= connect(state
->fd
, &rbsda
->u
.sa
, sa_socklen
);
2030 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2035 if (tevent_req_error(req
, err
)) {
2039 tevent_req_done(req
);
2043 state
->fde
= tevent_add_fd(ev
, state
,
2045 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2046 tstream_bsd_connect_fde_handler
,
2048 if (tevent_req_nomem(state
->fde
, req
)) {
2055 tevent_req_post(req
, ev
);
2059 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2060 struct tevent_fd
*fde
,
2064 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2066 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2067 struct tstream_bsd_connect_state
);
2070 socklen_t len
= sizeof(error
);
2074 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2081 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2086 if (tevent_req_error(req
, err
)) {
2090 tevent_req_done(req
);
2093 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2095 TALLOC_CTX
*mem_ctx
,
2096 struct tstream_context
**stream
,
2097 const char *location
)
2099 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2100 struct tstream_bsd_connect_state
);
2103 ret
= tsocket_simple_int_recv(req
, perrno
);
2105 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2113 TALLOC_FREE(state
->fde
);
2118 tevent_req_received(req
);
2122 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2123 struct tevent_context
*ev
,
2124 const struct tsocket_address
*local
,
2125 const struct tsocket_address
*remote
)
2127 struct tsocket_address_bsd
*lbsda
=
2128 talloc_get_type_abort(local
->private_data
,
2129 struct tsocket_address_bsd
);
2130 struct tevent_req
*req
;
2133 switch (lbsda
->u
.sa
.sa_family
) {
2145 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2150 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2152 TALLOC_CTX
*mem_ctx
,
2153 struct tstream_context
**stream
,
2154 const char *location
)
2156 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2159 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2160 struct tevent_context
*ev
,
2161 const struct tsocket_address
*local
,
2162 const struct tsocket_address
*remote
)
2164 struct tsocket_address_bsd
*lbsda
=
2165 talloc_get_type_abort(local
->private_data
,
2166 struct tsocket_address_bsd
);
2167 struct tevent_req
*req
;
2170 switch (lbsda
->u
.sa
.sa_family
) {
2178 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2183 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2185 TALLOC_CTX
*mem_ctx
,
2186 struct tstream_context
**stream
,
2187 const char *location
)
2189 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2192 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2193 struct tstream_context
**_stream1
,
2194 TALLOC_CTX
*mem_ctx2
,
2195 struct tstream_context
**_stream2
,
2196 const char *location
)
2202 struct tstream_context
*stream1
= NULL
;
2203 struct tstream_context
*stream2
= NULL
;
2205 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2212 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2214 int sys_errno
= errno
;
2220 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2222 int sys_errno
= errno
;
2228 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2233 int sys_errno
= errno
;
2240 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2245 int sys_errno
= errno
;
2246 talloc_free(stream1
);
2252 *_stream1
= stream1
;
2253 *_stream2
= stream2
;