2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
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 static 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_len
< sizeof(struct sockaddr_un
)) {
221 if (sa_len
< sizeof(struct sockaddr_in
)) {
228 if (sa_len
< sizeof(struct sockaddr_in6
)) {
235 errno
= EAFNOSUPPORT
;
239 if (sa_len
> 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_len
);
262 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
266 struct tsocket_address
**_addr
,
267 const char *location
)
269 struct addrinfo hints
;
270 struct addrinfo
*result
= NULL
;
276 * we use SOCKET_STREAM here to get just one result
277 * back from getaddrinfo().
279 hints
.ai_socktype
= SOCK_STREAM
;
280 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
282 if (strcasecmp(fam
, "ip") == 0) {
283 hints
.ai_family
= AF_UNSPEC
;
291 } else if (strcasecmp(fam
, "ipv4") == 0) {
292 hints
.ai_family
= AF_INET
;
297 } else if (strcasecmp(fam
, "ipv6") == 0) {
298 hints
.ai_family
= AF_INET6
;
304 errno
= EAFNOSUPPORT
;
308 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
310 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
321 if (result
->ai_socktype
!= SOCK_STREAM
) {
327 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
335 freeaddrinfo(result
);
340 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
343 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
344 struct tsocket_address_bsd
);
345 char addr_str
[INET6_ADDRSTRLEN
+1];
353 switch (bsda
->u
.sa
.sa_family
) {
355 str
= inet_ntop(bsda
->u
.in
.sin_family
,
356 &bsda
->u
.in
.sin_addr
,
357 addr_str
, sizeof(addr_str
));
361 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
362 &bsda
->u
.in6
.sin6_addr
,
363 addr_str
, sizeof(addr_str
));
375 return talloc_strdup(mem_ctx
, str
);
378 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
380 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
381 struct tsocket_address_bsd
);
389 switch (bsda
->u
.sa
.sa_family
) {
391 port
= ntohs(bsda
->u
.in
.sin_port
);
395 port
= ntohs(bsda
->u
.in6
.sin6_port
);
406 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
409 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
410 struct tsocket_address_bsd
);
417 switch (bsda
->u
.sa
.sa_family
) {
419 bsda
->u
.in
.sin_port
= htons(port
);
423 bsda
->u
.in6
.sin6_port
= htons(port
);
434 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
436 struct tsocket_address
**_addr
,
437 const char *location
)
439 struct sockaddr_un un
;
448 un
.sun_family
= AF_UNIX
;
449 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
));
451 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
452 (struct sockaddr
*)p
,
460 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
463 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
464 struct tsocket_address_bsd
);
472 switch (bsda
->u
.sa
.sa_family
) {
474 str
= bsda
->u
.un
.sun_path
;
481 return talloc_strdup(mem_ctx
, str
);
484 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
487 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
488 struct tsocket_address_bsd
);
491 const char *prefix
= NULL
;
494 switch (bsda
->u
.sa
.sa_family
) {
496 return talloc_asprintf(mem_ctx
, "unix:%s",
497 bsda
->u
.un
.sun_path
);
511 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
516 port
= tsocket_address_inet_port(addr
);
518 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
519 prefix
, addr_str
, port
);
520 talloc_free(addr_str
);
525 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
527 const char *location
)
529 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
530 struct tsocket_address_bsd
);
531 struct tsocket_address
*copy
;
534 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
546 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
548 .string
= tsocket_address_bsd_string
,
549 .copy
= tsocket_address_bsd_copy
,
556 struct tevent_fd
*fde
;
558 void *readable_private
;
559 void (*readable_handler
)(void *private_data
);
560 void *writeable_private
;
561 void (*writeable_handler
)(void *private_data
);
564 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
565 struct tevent_fd
*fde
,
569 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
572 if (flags
& TEVENT_FD_WRITE
) {
573 bsds
->writeable_handler(bsds
->writeable_private
);
576 if (flags
& TEVENT_FD_READ
) {
577 if (!bsds
->readable_handler
) {
578 TEVENT_FD_NOT_READABLE(bsds
->fde
);
581 bsds
->readable_handler(bsds
->readable_private
);
586 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
587 struct tevent_context
*ev
,
588 void (*handler
)(void *private_data
),
596 if (!bsds
->readable_handler
) {
599 bsds
->readable_handler
= NULL
;
600 bsds
->readable_private
= NULL
;
605 /* read and write must use the same tevent_context */
606 if (bsds
->event_ptr
!= ev
) {
607 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
611 bsds
->event_ptr
= NULL
;
612 TALLOC_FREE(bsds
->fde
);
615 if (bsds
->fde
== NULL
) {
616 bsds
->fde
= tevent_add_fd(ev
, bsds
,
617 bsds
->fd
, TEVENT_FD_READ
,
618 tdgram_bsd_fde_handler
,
625 /* cache the event context we're running on */
626 bsds
->event_ptr
= ev
;
627 } else if (!bsds
->readable_handler
) {
628 TEVENT_FD_READABLE(bsds
->fde
);
631 bsds
->readable_handler
= handler
;
632 bsds
->readable_private
= private_data
;
637 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
638 struct tevent_context
*ev
,
639 void (*handler
)(void *private_data
),
647 if (!bsds
->writeable_handler
) {
650 bsds
->writeable_handler
= NULL
;
651 bsds
->writeable_private
= NULL
;
652 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
657 /* read and write must use the same tevent_context */
658 if (bsds
->event_ptr
!= ev
) {
659 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
663 bsds
->event_ptr
= NULL
;
664 TALLOC_FREE(bsds
->fde
);
667 if (bsds
->fde
== NULL
) {
668 bsds
->fde
= tevent_add_fd(ev
, bsds
,
669 bsds
->fd
, TEVENT_FD_WRITE
,
670 tdgram_bsd_fde_handler
,
677 /* cache the event context we're running on */
678 bsds
->event_ptr
= ev
;
679 } else if (!bsds
->writeable_handler
) {
680 TEVENT_FD_WRITEABLE(bsds
->fde
);
683 bsds
->writeable_handler
= handler
;
684 bsds
->writeable_private
= private_data
;
689 struct tdgram_bsd_recvfrom_state
{
690 struct tdgram_context
*dgram
;
694 struct tsocket_address
*src
;
697 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
699 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
702 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
707 static void tdgram_bsd_recvfrom_handler(void *private_data
);
709 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
710 struct tevent_context
*ev
,
711 struct tdgram_context
*dgram
)
713 struct tevent_req
*req
;
714 struct tdgram_bsd_recvfrom_state
*state
;
715 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
718 req
= tevent_req_create(mem_ctx
, &state
,
719 struct tdgram_bsd_recvfrom_state
);
724 state
->dgram
= dgram
;
729 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
731 if (bsds
->fd
== -1) {
732 tevent_req_error(req
, ENOTCONN
);
737 * this is a fast path, not waiting for the
738 * socket to become explicit readable gains
739 * about 10%-20% performance in benchmark tests.
741 tdgram_bsd_recvfrom_handler(req
);
742 if (!tevent_req_is_in_progress(req
)) {
746 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
747 tdgram_bsd_recvfrom_handler
,
750 tevent_req_error(req
, errno
);
757 tevent_req_post(req
, ev
);
761 static void tdgram_bsd_recvfrom_handler(void *private_data
)
763 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
765 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
766 struct tdgram_bsd_recvfrom_state
);
767 struct tdgram_context
*dgram
= state
->dgram
;
768 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
769 struct tsocket_address_bsd
*bsda
;
771 struct sockaddr
*sa
= NULL
;
772 socklen_t sa_len
= 0;
776 ret
= tsocket_bsd_pending(bsds
->fd
);
781 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
786 if (tevent_req_error(req
, err
)) {
790 state
->buf
= talloc_array(state
, uint8_t, ret
);
791 if (tevent_req_nomem(state
->buf
, req
)) {
796 state
->src
= tsocket_address_create(state
,
797 &tsocket_address_bsd_ops
,
799 struct tsocket_address_bsd
,
800 __location__
"bsd_recvfrom");
801 if (tevent_req_nomem(state
->src
, req
)) {
808 sa_len
= sizeof(bsda
->u
.ss
);
810 * for unix sockets we can't use the size of sockaddr_storage
811 * we would get EINVAL
813 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
814 sa_len
= sizeof(bsda
->u
.un
);
817 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, &sa_len
);
818 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
823 if (tevent_req_error(req
, err
)) {
827 if (ret
!= state
->len
) {
828 tevent_req_error(req
, EIO
);
832 tevent_req_done(req
);
835 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
839 struct tsocket_address
**src
)
841 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
842 struct tdgram_bsd_recvfrom_state
);
845 ret
= tsocket_simple_int_recv(req
, perrno
);
847 *buf
= talloc_move(mem_ctx
, &state
->buf
);
850 *src
= talloc_move(mem_ctx
, &state
->src
);
854 tevent_req_received(req
);
858 struct tdgram_bsd_sendto_state
{
859 struct tdgram_context
*dgram
;
863 const struct tsocket_address
*dst
;
868 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
870 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
873 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
878 static void tdgram_bsd_sendto_handler(void *private_data
);
880 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
881 struct tevent_context
*ev
,
882 struct tdgram_context
*dgram
,
885 const struct tsocket_address
*dst
)
887 struct tevent_req
*req
;
888 struct tdgram_bsd_sendto_state
*state
;
889 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
892 req
= tevent_req_create(mem_ctx
, &state
,
893 struct tdgram_bsd_sendto_state
);
898 state
->dgram
= dgram
;
904 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
906 if (bsds
->fd
== -1) {
907 tevent_req_error(req
, ENOTCONN
);
912 * this is a fast path, not waiting for the
913 * socket to become explicit writeable gains
914 * about 10%-20% performance in benchmark tests.
916 tdgram_bsd_sendto_handler(req
);
917 if (!tevent_req_is_in_progress(req
)) {
921 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
922 tdgram_bsd_sendto_handler
,
925 tevent_req_error(req
, errno
);
932 tevent_req_post(req
, ev
);
936 static void tdgram_bsd_sendto_handler(void *private_data
)
938 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
940 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
941 struct tdgram_bsd_sendto_state
);
942 struct tdgram_context
*dgram
= state
->dgram
;
943 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
944 struct sockaddr
*sa
= NULL
;
945 socklen_t sa_len
= 0;
951 struct tsocket_address_bsd
*bsda
=
952 talloc_get_type(state
->dst
->private_data
,
953 struct tsocket_address_bsd
);
956 sa_len
= sizeof(bsda
->u
.ss
);
958 * for unix sockets we can't use the size of sockaddr_storage
959 * we would get EINVAL
961 if (bsda
->u
.sa
.sa_family
== AF_UNIX
) {
962 sa_len
= sizeof(bsda
->u
.un
);
966 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_len
);
967 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
972 if (tevent_req_error(req
, err
)) {
978 tevent_req_done(req
);
981 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
983 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
984 struct tdgram_bsd_sendto_state
);
987 ret
= tsocket_simple_int_recv(req
, perrno
);
992 tevent_req_received(req
);
996 struct tdgram_bsd_disconnect_state
{
1000 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1001 struct tevent_context
*ev
,
1002 struct tdgram_context
*dgram
)
1004 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1005 struct tevent_req
*req
;
1006 struct tdgram_bsd_disconnect_state
*state
;
1011 req
= tevent_req_create(mem_ctx
, &state
,
1012 struct tdgram_bsd_disconnect_state
);
1017 if (bsds
->fd
== -1) {
1018 tevent_req_error(req
, ENOTCONN
);
1022 ret
= close(bsds
->fd
);
1024 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1025 if (tevent_req_error(req
, err
)) {
1029 tevent_req_done(req
);
1031 tevent_req_post(req
, ev
);
1035 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1040 ret
= tsocket_simple_int_recv(req
, perrno
);
1042 tevent_req_received(req
);
1046 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1049 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1050 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1052 .sendto_send
= tdgram_bsd_sendto_send
,
1053 .sendto_recv
= tdgram_bsd_sendto_recv
,
1055 .disconnect_send
= tdgram_bsd_disconnect_send
,
1056 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1059 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1061 TALLOC_FREE(bsds
->fde
);
1062 if (bsds
->fd
!= -1) {
1069 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1070 const struct tsocket_address
*remote
,
1072 TALLOC_CTX
*mem_ctx
,
1073 struct tdgram_context
**_dgram
,
1074 const char *location
)
1076 struct tsocket_address_bsd
*lbsda
=
1077 talloc_get_type_abort(local
->private_data
,
1078 struct tsocket_address_bsd
);
1079 struct tsocket_address_bsd
*rbsda
= NULL
;
1080 struct tdgram_context
*dgram
;
1081 struct tdgram_bsd
*bsds
;
1084 bool do_bind
= false;
1085 bool do_reuseaddr
= false;
1086 socklen_t sa_len
= sizeof(lbsda
->u
.ss
);
1089 rbsda
= talloc_get_type_abort(remote
->private_data
,
1090 struct tsocket_address_bsd
);
1093 switch (lbsda
->u
.sa
.sa_family
) {
1099 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1100 do_reuseaddr
= true;
1104 * for unix sockets we can't use the size of sockaddr_storage
1105 * we would get EINVAL
1107 sa_len
= sizeof(lbsda
->u
.un
);
1110 if (lbsda
->u
.in
.sin_port
!= 0) {
1111 do_reuseaddr
= true;
1114 if (lbsda
->u
.in
.sin_addr
.s_addr
== INADDR_ANY
) {
1120 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1121 do_reuseaddr
= true;
1124 if (memcmp(&in6addr_any
,
1125 &lbsda
->u
.in6
.sin6_addr
,
1126 sizeof(in6addr_any
)) != 0) {
1136 fd
= socket(lbsda
->u
.sa
.sa_family
, SOCK_DGRAM
, 0);
1141 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1146 dgram
= tdgram_context_create(mem_ctx
,
1152 int saved_errno
= errno
;
1154 errno
= saved_errno
;
1159 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1164 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1165 (const void *)&val
, sizeof(val
));
1167 int saved_errno
= errno
;
1169 errno
= saved_errno
;
1177 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1178 (const void *)&val
, sizeof(val
));
1180 int saved_errno
= errno
;
1182 errno
= saved_errno
;
1188 ret
= bind(fd
, &lbsda
->u
.sa
, sa_len
);
1190 int saved_errno
= errno
;
1192 errno
= saved_errno
;
1198 ret
= connect(fd
, &rbsda
->u
.sa
, sa_len
);
1200 int saved_errno
= errno
;
1202 errno
= saved_errno
;
1211 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1212 const struct tsocket_address
*remote
,
1213 TALLOC_CTX
*mem_ctx
,
1214 struct tdgram_context
**dgram
,
1215 const char *location
)
1217 struct tsocket_address_bsd
*lbsda
=
1218 talloc_get_type_abort(local
->private_data
,
1219 struct tsocket_address_bsd
);
1222 switch (lbsda
->u
.sa
.sa_family
) {
1234 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1235 mem_ctx
, dgram
, location
);
1240 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1241 const struct tsocket_address
*remote
,
1242 TALLOC_CTX
*mem_ctx
,
1243 struct tdgram_context
**dgram
,
1244 const char *location
)
1246 struct tsocket_address_bsd
*lbsda
=
1247 talloc_get_type_abort(local
->private_data
,
1248 struct tsocket_address_bsd
);
1251 switch (lbsda
->u
.sa
.sa_family
) {
1259 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1260 mem_ctx
, dgram
, location
);
1265 struct tstream_bsd
{
1269 struct tevent_fd
*fde
;
1271 void *readable_private
;
1272 void (*readable_handler
)(void *private_data
);
1273 void *writeable_private
;
1274 void (*writeable_handler
)(void *private_data
);
1277 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1278 struct tevent_fd
*fde
,
1282 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1283 struct tstream_bsd
);
1285 if (flags
& TEVENT_FD_WRITE
) {
1286 bsds
->writeable_handler(bsds
->writeable_private
);
1289 if (flags
& TEVENT_FD_READ
) {
1290 if (!bsds
->readable_handler
) {
1291 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1294 bsds
->readable_handler(bsds
->readable_private
);
1299 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1300 struct tevent_context
*ev
,
1301 void (*handler
)(void *private_data
),
1309 if (!bsds
->readable_handler
) {
1312 bsds
->readable_handler
= NULL
;
1313 bsds
->readable_private
= NULL
;
1318 /* read and write must use the same tevent_context */
1319 if (bsds
->event_ptr
!= ev
) {
1320 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1324 bsds
->event_ptr
= NULL
;
1325 TALLOC_FREE(bsds
->fde
);
1328 if (bsds
->fde
== NULL
) {
1329 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1330 bsds
->fd
, TEVENT_FD_READ
,
1331 tstream_bsd_fde_handler
,
1338 /* cache the event context we're running on */
1339 bsds
->event_ptr
= ev
;
1340 } else if (!bsds
->readable_handler
) {
1341 TEVENT_FD_READABLE(bsds
->fde
);
1344 bsds
->readable_handler
= handler
;
1345 bsds
->readable_private
= private_data
;
1350 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1351 struct tevent_context
*ev
,
1352 void (*handler
)(void *private_data
),
1360 if (!bsds
->writeable_handler
) {
1363 bsds
->writeable_handler
= NULL
;
1364 bsds
->writeable_private
= NULL
;
1365 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1370 /* read and write must use the same tevent_context */
1371 if (bsds
->event_ptr
!= ev
) {
1372 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1376 bsds
->event_ptr
= NULL
;
1377 TALLOC_FREE(bsds
->fde
);
1380 if (bsds
->fde
== NULL
) {
1381 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1382 bsds
->fd
, TEVENT_FD_WRITE
,
1383 tstream_bsd_fde_handler
,
1390 /* cache the event context we're running on */
1391 bsds
->event_ptr
= ev
;
1392 } else if (!bsds
->writeable_handler
) {
1393 TEVENT_FD_WRITEABLE(bsds
->fde
);
1396 bsds
->writeable_handler
= handler
;
1397 bsds
->writeable_private
= private_data
;
1402 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1404 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1405 struct tstream_bsd
);
1408 if (bsds
->fd
== -1) {
1413 ret
= tsocket_bsd_pending(bsds
->fd
);
1418 struct tstream_bsd_readv_state
{
1419 struct tstream_context
*stream
;
1421 struct iovec
*vector
;
1427 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1429 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1430 struct tstream_bsd
);
1432 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1437 static void tstream_bsd_readv_handler(void *private_data
);
1439 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1440 struct tevent_context
*ev
,
1441 struct tstream_context
*stream
,
1442 struct iovec
*vector
,
1445 struct tevent_req
*req
;
1446 struct tstream_bsd_readv_state
*state
;
1447 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1450 req
= tevent_req_create(mem_ctx
, &state
,
1451 struct tstream_bsd_readv_state
);
1456 state
->stream
= stream
;
1457 /* we make a copy of the vector so that we can modify it */
1458 state
->vector
= talloc_array(state
, struct iovec
, count
);
1459 if (tevent_req_nomem(state
->vector
, req
)) {
1462 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1463 state
->count
= count
;
1466 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1468 if (bsds
->fd
== -1) {
1469 tevent_req_error(req
, ENOTCONN
);
1474 * this is a fast path, not waiting for the
1475 * socket to become explicit readable gains
1476 * about 10%-20% performance in benchmark tests.
1478 tstream_bsd_readv_handler(req
);
1479 if (!tevent_req_is_in_progress(req
)) {
1483 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1484 tstream_bsd_readv_handler
,
1487 tevent_req_error(req
, errno
);
1494 tevent_req_post(req
, ev
);
1498 static void tstream_bsd_readv_handler(void *private_data
)
1500 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1502 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1503 struct tstream_bsd_readv_state
);
1504 struct tstream_context
*stream
= state
->stream
;
1505 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1510 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1512 /* propagate end of file */
1513 tevent_req_error(req
, EPIPE
);
1516 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1521 if (tevent_req_error(req
, err
)) {
1528 if (ret
< state
->vector
[0].iov_len
) {
1530 base
= (uint8_t *)state
->vector
[0].iov_base
;
1532 state
->vector
[0].iov_base
= base
;
1533 state
->vector
[0].iov_len
-= ret
;
1536 ret
-= state
->vector
[0].iov_len
;
1542 * there're maybe some empty vectors at the end
1543 * which we need to skip, otherwise we would get
1544 * ret == 0 from the readv() call and return EPIPE
1546 while (state
->count
> 0) {
1547 if (state
->vector
[0].iov_len
> 0) {
1554 if (state
->count
> 0) {
1555 /* we have more to read */
1559 tevent_req_done(req
);
1562 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1565 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1566 struct tstream_bsd_readv_state
);
1569 ret
= tsocket_simple_int_recv(req
, perrno
);
1574 tevent_req_received(req
);
1578 struct tstream_bsd_writev_state
{
1579 struct tstream_context
*stream
;
1581 struct iovec
*vector
;
1587 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1589 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1590 struct tstream_bsd
);
1592 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1597 static void tstream_bsd_writev_handler(void *private_data
);
1599 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1600 struct tevent_context
*ev
,
1601 struct tstream_context
*stream
,
1602 const struct iovec
*vector
,
1605 struct tevent_req
*req
;
1606 struct tstream_bsd_writev_state
*state
;
1607 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1610 req
= tevent_req_create(mem_ctx
, &state
,
1611 struct tstream_bsd_writev_state
);
1616 state
->stream
= stream
;
1617 /* we make a copy of the vector so that we can modify it */
1618 state
->vector
= talloc_array(state
, struct iovec
, count
);
1619 if (tevent_req_nomem(state
->vector
, req
)) {
1622 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1623 state
->count
= count
;
1626 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1628 if (bsds
->fd
== -1) {
1629 tevent_req_error(req
, ENOTCONN
);
1634 * this is a fast path, not waiting for the
1635 * socket to become explicit writeable gains
1636 * about 10%-20% performance in benchmark tests.
1638 tstream_bsd_writev_handler(req
);
1639 if (!tevent_req_is_in_progress(req
)) {
1643 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1644 tstream_bsd_writev_handler
,
1647 tevent_req_error(req
, errno
);
1654 tevent_req_post(req
, ev
);
1658 static void tstream_bsd_writev_handler(void *private_data
)
1660 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1662 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1663 struct tstream_bsd_writev_state
);
1664 struct tstream_context
*stream
= state
->stream
;
1665 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1670 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1672 /* propagate end of file */
1673 tevent_req_error(req
, EPIPE
);
1676 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1681 if (tevent_req_error(req
, err
)) {
1688 if (ret
< state
->vector
[0].iov_len
) {
1690 base
= (uint8_t *)state
->vector
[0].iov_base
;
1692 state
->vector
[0].iov_base
= base
;
1693 state
->vector
[0].iov_len
-= ret
;
1696 ret
-= state
->vector
[0].iov_len
;
1702 * there're maybe some empty vectors at the end
1703 * which we need to skip, otherwise we would get
1704 * ret == 0 from the writev() call and return EPIPE
1706 while (state
->count
> 0) {
1707 if (state
->vector
[0].iov_len
> 0) {
1714 if (state
->count
> 0) {
1715 /* we have more to read */
1719 tevent_req_done(req
);
1722 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1724 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1725 struct tstream_bsd_writev_state
);
1728 ret
= tsocket_simple_int_recv(req
, perrno
);
1733 tevent_req_received(req
);
1737 struct tstream_bsd_disconnect_state
{
1741 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1742 struct tevent_context
*ev
,
1743 struct tstream_context
*stream
)
1745 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1746 struct tevent_req
*req
;
1747 struct tstream_bsd_disconnect_state
*state
;
1752 req
= tevent_req_create(mem_ctx
, &state
,
1753 struct tstream_bsd_disconnect_state
);
1758 if (bsds
->fd
== -1) {
1759 tevent_req_error(req
, ENOTCONN
);
1763 ret
= close(bsds
->fd
);
1765 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1766 if (tevent_req_error(req
, err
)) {
1770 tevent_req_done(req
);
1772 tevent_req_post(req
, ev
);
1776 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1781 ret
= tsocket_simple_int_recv(req
, perrno
);
1783 tevent_req_received(req
);
1787 static const struct tstream_context_ops tstream_bsd_ops
= {
1790 .pending_bytes
= tstream_bsd_pending_bytes
,
1792 .readv_send
= tstream_bsd_readv_send
,
1793 .readv_recv
= tstream_bsd_readv_recv
,
1795 .writev_send
= tstream_bsd_writev_send
,
1796 .writev_recv
= tstream_bsd_writev_recv
,
1798 .disconnect_send
= tstream_bsd_disconnect_send
,
1799 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1802 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1804 TALLOC_FREE(bsds
->fde
);
1805 if (bsds
->fd
!= -1) {
1812 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1814 struct tstream_context
**_stream
,
1815 const char *location
)
1817 struct tstream_context
*stream
;
1818 struct tstream_bsd
*bsds
;
1820 stream
= tstream_context_create(mem_ctx
,
1830 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1836 struct tstream_bsd_connect_state
{
1838 struct tevent_fd
*fde
;
1839 struct tstream_conext
*stream
;
1842 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1844 TALLOC_FREE(state
->fde
);
1845 if (state
->fd
!= -1) {
1853 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1854 struct tevent_fd
*fde
,
1856 void *private_data
);
1858 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1859 struct tevent_context
*ev
,
1861 const struct tsocket_address
*local
,
1862 const struct tsocket_address
*remote
)
1864 struct tevent_req
*req
;
1865 struct tstream_bsd_connect_state
*state
;
1866 struct tsocket_address_bsd
*lbsda
=
1867 talloc_get_type_abort(local
->private_data
,
1868 struct tsocket_address_bsd
);
1869 struct tsocket_address_bsd
*rbsda
=
1870 talloc_get_type_abort(remote
->private_data
,
1871 struct tsocket_address_bsd
);
1875 bool do_bind
= false;
1876 bool do_reuseaddr
= false;
1877 socklen_t sa_len
= sizeof(rbsda
->u
.ss
);
1879 req
= tevent_req_create(mem_ctx
, &state
,
1880 struct tstream_bsd_connect_state
);
1887 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
1889 /* give the wrappers a chance to report an error */
1890 if (sys_errno
!= 0) {
1891 tevent_req_error(req
, sys_errno
);
1895 switch (lbsda
->u
.sa
.sa_family
) {
1897 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1898 do_reuseaddr
= true;
1902 * for unix sockets we can't use the size of sockaddr_storage
1903 * we would get EINVAL
1905 sa_len
= sizeof(rbsda
->u
.un
);
1908 if (lbsda
->u
.in
.sin_port
!= 0) {
1909 do_reuseaddr
= true;
1912 if (lbsda
->u
.in
.sin_addr
.s_addr
== INADDR_ANY
) {
1918 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1919 do_reuseaddr
= true;
1922 if (memcmp(&in6addr_any
,
1923 &lbsda
->u
.in6
.sin6_addr
,
1924 sizeof(in6addr_any
)) != 0) {
1930 tevent_req_error(req
, EINVAL
);
1934 state
->fd
= socket(lbsda
->u
.sa
.sa_family
, SOCK_STREAM
, 0);
1935 if (state
->fd
== -1) {
1936 tevent_req_error(req
, errno
);
1940 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
1941 if (state
->fd
== -1) {
1942 tevent_req_error(req
, errno
);
1949 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
1950 (const void *)&val
, sizeof(val
));
1952 tevent_req_error(req
, errno
);
1958 ret
= bind(state
->fd
, &lbsda
->u
.sa
, sizeof(lbsda
->u
.ss
));
1960 tevent_req_error(req
, errno
);
1965 ret
= connect(state
->fd
, &rbsda
->u
.sa
, sa_len
);
1966 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1971 if (tevent_req_error(req
, err
)) {
1975 tevent_req_done(req
);
1979 state
->fde
= tevent_add_fd(ev
, state
,
1981 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1982 tstream_bsd_connect_fde_handler
,
1984 if (tevent_req_nomem(state
->fde
, req
)) {
1991 tevent_req_post(req
, ev
);
1995 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1996 struct tevent_fd
*fde
,
2000 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2002 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2003 struct tstream_bsd_connect_state
);
2006 socklen_t len
= sizeof(error
);
2010 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2017 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2022 if (tevent_req_error(req
, err
)) {
2026 tevent_req_done(req
);
2029 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2031 TALLOC_CTX
*mem_ctx
,
2032 struct tstream_context
**stream
,
2033 const char *location
)
2035 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2036 struct tstream_bsd_connect_state
);
2039 ret
= tsocket_simple_int_recv(req
, perrno
);
2041 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2049 TALLOC_FREE(state
->fde
);
2054 tevent_req_received(req
);
2058 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2059 struct tevent_context
*ev
,
2060 const struct tsocket_address
*local
,
2061 const struct tsocket_address
*remote
)
2063 struct tsocket_address_bsd
*lbsda
=
2064 talloc_get_type_abort(local
->private_data
,
2065 struct tsocket_address_bsd
);
2066 struct tevent_req
*req
;
2069 switch (lbsda
->u
.sa
.sa_family
) {
2081 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2086 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2088 TALLOC_CTX
*mem_ctx
,
2089 struct tstream_context
**stream
,
2090 const char *location
)
2092 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2095 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2096 struct tevent_context
*ev
,
2097 const struct tsocket_address
*local
,
2098 const struct tsocket_address
*remote
)
2100 struct tsocket_address_bsd
*lbsda
=
2101 talloc_get_type_abort(local
->private_data
,
2102 struct tsocket_address_bsd
);
2103 struct tevent_req
*req
;
2106 switch (lbsda
->u
.sa
.sa_family
) {
2114 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2119 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2121 TALLOC_CTX
*mem_ctx
,
2122 struct tstream_context
**stream
,
2123 const char *location
)
2125 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2128 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2129 struct tstream_context
**_stream1
,
2130 TALLOC_CTX
*mem_ctx2
,
2131 struct tstream_context
**_stream2
,
2132 const char *location
)
2138 struct tstream_context
*stream1
= NULL
;
2139 struct tstream_context
*stream2
= NULL
;
2141 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2148 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2150 int sys_errno
= errno
;
2156 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2158 int sys_errno
= errno
;
2164 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2169 int sys_errno
= errno
;
2176 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2181 int sys_errno
= errno
;
2182 talloc_free(stream1
);
2188 *_stream1
= stream1
;
2189 *_stream2
= stream2
;