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
{
193 socklen_t sa_socklen
;
196 struct sockaddr_in in
;
198 struct sockaddr_in6 in6
;
200 struct sockaddr_un un
;
201 struct sockaddr_storage ss
;
205 int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX
*mem_ctx
,
208 struct tsocket_address
**_addr
,
209 const char *location
)
211 struct tsocket_address
*addr
;
212 struct tsocket_address_bsd
*bsda
;
214 if (sa_socklen
< sizeof(sa
->sa_family
)) {
219 switch (sa
->sa_family
) {
221 if (sa_socklen
> sizeof(struct sockaddr_un
)) {
222 sa_socklen
= sizeof(struct sockaddr_un
);
226 if (sa_socklen
< sizeof(struct sockaddr_in
)) {
230 sa_socklen
= sizeof(struct sockaddr_in
);
234 if (sa_socklen
< sizeof(struct sockaddr_in6
)) {
238 sa_socklen
= sizeof(struct sockaddr_in6
);
242 errno
= EAFNOSUPPORT
;
246 if (sa_socklen
> sizeof(struct sockaddr_storage
)) {
251 addr
= tsocket_address_create(mem_ctx
,
252 &tsocket_address_bsd_ops
,
254 struct tsocket_address_bsd
,
263 memcpy(&bsda
->u
.ss
, sa
, sa_socklen
);
265 bsda
->sa_socklen
= sa_socklen
;
271 ssize_t
tsocket_address_bsd_sockaddr(const struct tsocket_address
*addr
,
275 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
276 struct tsocket_address_bsd
);
283 if (sa_socklen
< bsda
->sa_socklen
) {
288 if (sa_socklen
> bsda
->sa_socklen
) {
289 memset(sa
, 0, sa_socklen
);
290 sa_socklen
= bsda
->sa_socklen
;
293 memcpy(sa
, &bsda
->u
.ss
, sa_socklen
);
297 bool tsocket_address_is_inet(const struct tsocket_address
*addr
, const char *fam
)
299 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
300 struct tsocket_address_bsd
);
306 switch (bsda
->u
.sa
.sa_family
) {
308 if (strcasecmp(fam
, "ip") == 0) {
312 if (strcasecmp(fam
, "ipv4") == 0) {
319 if (strcasecmp(fam
, "ip") == 0) {
323 if (strcasecmp(fam
, "ipv6") == 0) {
334 int _tsocket_address_inet_from_strings(TALLOC_CTX
*mem_ctx
,
338 struct tsocket_address
**_addr
,
339 const char *location
)
341 struct addrinfo hints
;
342 struct addrinfo
*result
= NULL
;
348 * we use SOCKET_STREAM here to get just one result
349 * back from getaddrinfo().
351 hints
.ai_socktype
= SOCK_STREAM
;
352 hints
.ai_flags
= AI_NUMERICHOST
| AI_NUMERICSERV
;
354 if (strcasecmp(fam
, "ip") == 0) {
355 hints
.ai_family
= AF_UNSPEC
;
363 } else if (strcasecmp(fam
, "ipv4") == 0) {
364 hints
.ai_family
= AF_INET
;
369 } else if (strcasecmp(fam
, "ipv6") == 0) {
370 hints
.ai_family
= AF_INET6
;
376 errno
= EAFNOSUPPORT
;
380 snprintf(port_str
, sizeof(port_str
) - 1, "%u", port
);
382 ret
= getaddrinfo(addr
, port_str
, &hints
, &result
);
393 if (result
->ai_socktype
!= SOCK_STREAM
) {
399 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
407 freeaddrinfo(result
);
412 char *tsocket_address_inet_addr_string(const struct tsocket_address
*addr
,
415 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
416 struct tsocket_address_bsd
);
417 char addr_str
[INET6_ADDRSTRLEN
+1];
425 switch (bsda
->u
.sa
.sa_family
) {
427 str
= inet_ntop(bsda
->u
.in
.sin_family
,
428 &bsda
->u
.in
.sin_addr
,
429 addr_str
, sizeof(addr_str
));
433 str
= inet_ntop(bsda
->u
.in6
.sin6_family
,
434 &bsda
->u
.in6
.sin6_addr
,
435 addr_str
, sizeof(addr_str
));
447 return talloc_strdup(mem_ctx
, str
);
450 uint16_t tsocket_address_inet_port(const struct tsocket_address
*addr
)
452 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
453 struct tsocket_address_bsd
);
461 switch (bsda
->u
.sa
.sa_family
) {
463 port
= ntohs(bsda
->u
.in
.sin_port
);
467 port
= ntohs(bsda
->u
.in6
.sin6_port
);
478 int tsocket_address_inet_set_port(struct tsocket_address
*addr
,
481 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
482 struct tsocket_address_bsd
);
489 switch (bsda
->u
.sa
.sa_family
) {
491 bsda
->u
.in
.sin_port
= htons(port
);
495 bsda
->u
.in6
.sin6_port
= htons(port
);
506 bool tsocket_address_is_unix(const struct tsocket_address
*addr
)
508 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
509 struct tsocket_address_bsd
);
515 switch (bsda
->u
.sa
.sa_family
) {
523 int _tsocket_address_unix_from_path(TALLOC_CTX
*mem_ctx
,
525 struct tsocket_address
**_addr
,
526 const char *location
)
528 struct sockaddr_un un
;
536 if (strlen(path
) > sizeof(un
.sun_path
)-1) {
537 errno
= ENAMETOOLONG
;
542 un
.sun_family
= AF_UNIX
;
543 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
)-1);
545 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
546 (struct sockaddr
*)p
,
554 char *tsocket_address_unix_path(const struct tsocket_address
*addr
,
557 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
558 struct tsocket_address_bsd
);
566 switch (bsda
->u
.sa
.sa_family
) {
568 str
= bsda
->u
.un
.sun_path
;
575 return talloc_strdup(mem_ctx
, str
);
578 static char *tsocket_address_bsd_string(const struct tsocket_address
*addr
,
581 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
582 struct tsocket_address_bsd
);
585 const char *prefix
= NULL
;
588 switch (bsda
->u
.sa
.sa_family
) {
590 return talloc_asprintf(mem_ctx
, "unix:%s",
591 bsda
->u
.un
.sun_path
);
605 addr_str
= tsocket_address_inet_addr_string(addr
, mem_ctx
);
610 port
= tsocket_address_inet_port(addr
);
612 str
= talloc_asprintf(mem_ctx
, "%s:%s:%u",
613 prefix
, addr_str
, port
);
614 talloc_free(addr_str
);
619 static struct tsocket_address
*tsocket_address_bsd_copy(const struct tsocket_address
*addr
,
621 const char *location
)
623 struct tsocket_address_bsd
*bsda
= talloc_get_type(addr
->private_data
,
624 struct tsocket_address_bsd
);
625 struct tsocket_address
*copy
;
628 ret
= _tsocket_address_bsd_from_sockaddr(mem_ctx
,
640 static const struct tsocket_address_ops tsocket_address_bsd_ops
= {
642 .string
= tsocket_address_bsd_string
,
643 .copy
= tsocket_address_bsd_copy
,
650 struct tevent_fd
*fde
;
652 void *readable_private
;
653 void (*readable_handler
)(void *private_data
);
654 void *writeable_private
;
655 void (*writeable_handler
)(void *private_data
);
658 static void tdgram_bsd_fde_handler(struct tevent_context
*ev
,
659 struct tevent_fd
*fde
,
663 struct tdgram_bsd
*bsds
= talloc_get_type_abort(private_data
,
666 if (flags
& TEVENT_FD_WRITE
) {
667 bsds
->writeable_handler(bsds
->writeable_private
);
670 if (flags
& TEVENT_FD_READ
) {
671 if (!bsds
->readable_handler
) {
672 TEVENT_FD_NOT_READABLE(bsds
->fde
);
675 bsds
->readable_handler(bsds
->readable_private
);
680 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd
*bsds
,
681 struct tevent_context
*ev
,
682 void (*handler
)(void *private_data
),
690 if (!bsds
->readable_handler
) {
693 bsds
->readable_handler
= NULL
;
694 bsds
->readable_private
= NULL
;
699 /* read and write must use the same tevent_context */
700 if (bsds
->event_ptr
!= ev
) {
701 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
705 bsds
->event_ptr
= NULL
;
706 TALLOC_FREE(bsds
->fde
);
709 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
710 TALLOC_FREE(bsds
->fde
);
712 bsds
->fde
= tevent_add_fd(ev
, bsds
,
713 bsds
->fd
, TEVENT_FD_READ
,
714 tdgram_bsd_fde_handler
,
721 /* cache the event context we're running on */
722 bsds
->event_ptr
= ev
;
723 } else if (!bsds
->readable_handler
) {
724 TEVENT_FD_READABLE(bsds
->fde
);
727 bsds
->readable_handler
= handler
;
728 bsds
->readable_private
= private_data
;
733 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd
*bsds
,
734 struct tevent_context
*ev
,
735 void (*handler
)(void *private_data
),
743 if (!bsds
->writeable_handler
) {
746 bsds
->writeable_handler
= NULL
;
747 bsds
->writeable_private
= NULL
;
748 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
753 /* read and write must use the same tevent_context */
754 if (bsds
->event_ptr
!= ev
) {
755 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
759 bsds
->event_ptr
= NULL
;
760 TALLOC_FREE(bsds
->fde
);
763 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
764 TALLOC_FREE(bsds
->fde
);
766 bsds
->fde
= tevent_add_fd(ev
, bsds
,
767 bsds
->fd
, TEVENT_FD_WRITE
,
768 tdgram_bsd_fde_handler
,
775 /* cache the event context we're running on */
776 bsds
->event_ptr
= ev
;
777 } else if (!bsds
->writeable_handler
) {
778 TEVENT_FD_WRITEABLE(bsds
->fde
);
781 bsds
->writeable_handler
= handler
;
782 bsds
->writeable_private
= private_data
;
787 struct tdgram_bsd_recvfrom_state
{
788 struct tdgram_context
*dgram
;
792 struct tsocket_address
*src
;
795 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state
*state
)
797 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
800 tdgram_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
805 static void tdgram_bsd_recvfrom_handler(void *private_data
);
807 static struct tevent_req
*tdgram_bsd_recvfrom_send(TALLOC_CTX
*mem_ctx
,
808 struct tevent_context
*ev
,
809 struct tdgram_context
*dgram
)
811 struct tevent_req
*req
;
812 struct tdgram_bsd_recvfrom_state
*state
;
813 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
816 req
= tevent_req_create(mem_ctx
, &state
,
817 struct tdgram_bsd_recvfrom_state
);
822 state
->dgram
= dgram
;
827 talloc_set_destructor(state
, tdgram_bsd_recvfrom_destructor
);
829 if (bsds
->fd
== -1) {
830 tevent_req_error(req
, ENOTCONN
);
835 * this is a fast path, not waiting for the
836 * socket to become explicit readable gains
837 * about 10%-20% performance in benchmark tests.
839 tdgram_bsd_recvfrom_handler(req
);
840 if (!tevent_req_is_in_progress(req
)) {
844 ret
= tdgram_bsd_set_readable_handler(bsds
, ev
,
845 tdgram_bsd_recvfrom_handler
,
848 tevent_req_error(req
, errno
);
855 tevent_req_post(req
, ev
);
859 static void tdgram_bsd_recvfrom_handler(void *private_data
)
861 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
863 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
864 struct tdgram_bsd_recvfrom_state
);
865 struct tdgram_context
*dgram
= state
->dgram
;
866 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
867 struct tsocket_address_bsd
*bsda
;
872 ret
= tsocket_bsd_pending(bsds
->fd
);
877 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
882 if (tevent_req_error(req
, err
)) {
886 state
->buf
= talloc_array(state
, uint8_t, ret
);
887 if (tevent_req_nomem(state
->buf
, req
)) {
892 state
->src
= tsocket_address_create(state
,
893 &tsocket_address_bsd_ops
,
895 struct tsocket_address_bsd
,
896 __location__
"bsd_recvfrom");
897 if (tevent_req_nomem(state
->src
, req
)) {
902 bsda
->sa_socklen
= sizeof(bsda
->u
.ss
);
904 ret
= recvfrom(bsds
->fd
, state
->buf
, state
->len
, 0,
905 &bsda
->u
.sa
, &bsda
->sa_socklen
);
906 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
911 if (tevent_req_error(req
, err
)) {
916 * Some systems (FreeBSD, see bug #7115) return too much
917 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
918 * the return value includes some IP/UDP header bytes,
919 * while recvfrom() just returns the payload.
921 state
->buf
= talloc_realloc(state
, state
->buf
, uint8_t, ret
);
922 if (tevent_req_nomem(state
->buf
, req
)) {
927 tevent_req_done(req
);
930 static ssize_t
tdgram_bsd_recvfrom_recv(struct tevent_req
*req
,
934 struct tsocket_address
**src
)
936 struct tdgram_bsd_recvfrom_state
*state
= tevent_req_data(req
,
937 struct tdgram_bsd_recvfrom_state
);
940 ret
= tsocket_simple_int_recv(req
, perrno
);
942 *buf
= talloc_move(mem_ctx
, &state
->buf
);
945 *src
= talloc_move(mem_ctx
, &state
->src
);
949 tevent_req_received(req
);
953 struct tdgram_bsd_sendto_state
{
954 struct tdgram_context
*dgram
;
958 const struct tsocket_address
*dst
;
963 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state
*state
)
965 struct tdgram_bsd
*bsds
= tdgram_context_data(state
->dgram
,
968 tdgram_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
973 static void tdgram_bsd_sendto_handler(void *private_data
);
975 static struct tevent_req
*tdgram_bsd_sendto_send(TALLOC_CTX
*mem_ctx
,
976 struct tevent_context
*ev
,
977 struct tdgram_context
*dgram
,
980 const struct tsocket_address
*dst
)
982 struct tevent_req
*req
;
983 struct tdgram_bsd_sendto_state
*state
;
984 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
987 req
= tevent_req_create(mem_ctx
, &state
,
988 struct tdgram_bsd_sendto_state
);
993 state
->dgram
= dgram
;
999 talloc_set_destructor(state
, tdgram_bsd_sendto_destructor
);
1001 if (bsds
->fd
== -1) {
1002 tevent_req_error(req
, ENOTCONN
);
1007 * this is a fast path, not waiting for the
1008 * socket to become explicit writeable gains
1009 * about 10%-20% performance in benchmark tests.
1011 tdgram_bsd_sendto_handler(req
);
1012 if (!tevent_req_is_in_progress(req
)) {
1016 ret
= tdgram_bsd_set_writeable_handler(bsds
, ev
,
1017 tdgram_bsd_sendto_handler
,
1020 tevent_req_error(req
, errno
);
1027 tevent_req_post(req
, ev
);
1031 static void tdgram_bsd_sendto_handler(void *private_data
)
1033 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1035 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1036 struct tdgram_bsd_sendto_state
);
1037 struct tdgram_context
*dgram
= state
->dgram
;
1038 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1039 struct sockaddr
*sa
= NULL
;
1040 socklen_t sa_socklen
= 0;
1046 struct tsocket_address_bsd
*bsda
=
1047 talloc_get_type(state
->dst
->private_data
,
1048 struct tsocket_address_bsd
);
1051 sa_socklen
= bsda
->sa_socklen
;
1054 ret
= sendto(bsds
->fd
, state
->buf
, state
->len
, 0, sa
, sa_socklen
);
1055 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1060 if (tevent_req_error(req
, err
)) {
1066 tevent_req_done(req
);
1069 static ssize_t
tdgram_bsd_sendto_recv(struct tevent_req
*req
, int *perrno
)
1071 struct tdgram_bsd_sendto_state
*state
= tevent_req_data(req
,
1072 struct tdgram_bsd_sendto_state
);
1075 ret
= tsocket_simple_int_recv(req
, perrno
);
1080 tevent_req_received(req
);
1084 struct tdgram_bsd_disconnect_state
{
1088 static struct tevent_req
*tdgram_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1089 struct tevent_context
*ev
,
1090 struct tdgram_context
*dgram
)
1092 struct tdgram_bsd
*bsds
= tdgram_context_data(dgram
, struct tdgram_bsd
);
1093 struct tevent_req
*req
;
1094 struct tdgram_bsd_disconnect_state
*state
;
1099 req
= tevent_req_create(mem_ctx
, &state
,
1100 struct tdgram_bsd_disconnect_state
);
1105 if (bsds
->fd
== -1) {
1106 tevent_req_error(req
, ENOTCONN
);
1110 ret
= close(bsds
->fd
);
1112 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1113 if (tevent_req_error(req
, err
)) {
1117 tevent_req_done(req
);
1119 tevent_req_post(req
, ev
);
1123 static int tdgram_bsd_disconnect_recv(struct tevent_req
*req
,
1128 ret
= tsocket_simple_int_recv(req
, perrno
);
1130 tevent_req_received(req
);
1134 static const struct tdgram_context_ops tdgram_bsd_ops
= {
1137 .recvfrom_send
= tdgram_bsd_recvfrom_send
,
1138 .recvfrom_recv
= tdgram_bsd_recvfrom_recv
,
1140 .sendto_send
= tdgram_bsd_sendto_send
,
1141 .sendto_recv
= tdgram_bsd_sendto_recv
,
1143 .disconnect_send
= tdgram_bsd_disconnect_send
,
1144 .disconnect_recv
= tdgram_bsd_disconnect_recv
,
1147 static int tdgram_bsd_destructor(struct tdgram_bsd
*bsds
)
1149 TALLOC_FREE(bsds
->fde
);
1150 if (bsds
->fd
!= -1) {
1157 static int tdgram_bsd_dgram_socket(const struct tsocket_address
*local
,
1158 const struct tsocket_address
*remote
,
1160 TALLOC_CTX
*mem_ctx
,
1161 struct tdgram_context
**_dgram
,
1162 const char *location
)
1164 struct tsocket_address_bsd
*lbsda
=
1165 talloc_get_type_abort(local
->private_data
,
1166 struct tsocket_address_bsd
);
1167 struct tsocket_address_bsd
*rbsda
= NULL
;
1168 struct tdgram_context
*dgram
;
1169 struct tdgram_bsd
*bsds
;
1172 bool do_bind
= false;
1173 bool do_reuseaddr
= false;
1174 bool do_ipv6only
= false;
1175 bool is_inet
= false;
1176 int sa_fam
= lbsda
->u
.sa
.sa_family
;
1179 rbsda
= talloc_get_type_abort(remote
->private_data
,
1180 struct tsocket_address_bsd
);
1183 switch (lbsda
->u
.sa
.sa_family
) {
1189 if (lbsda
->u
.un
.sun_path
[0] != 0) {
1190 do_reuseaddr
= true;
1195 if (lbsda
->u
.in
.sin_port
!= 0) {
1196 do_reuseaddr
= true;
1199 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
1206 if (lbsda
->u
.in6
.sin6_port
!= 0) {
1207 do_reuseaddr
= true;
1210 if (memcmp(&in6addr_any
,
1211 &lbsda
->u
.in6
.sin6_addr
,
1212 sizeof(in6addr_any
)) != 0) {
1224 if (!do_bind
&& is_inet
&& rbsda
) {
1225 sa_fam
= rbsda
->u
.sa
.sa_family
;
1228 do_ipv6only
= false;
1238 fd
= socket(sa_fam
, SOCK_DGRAM
, 0);
1243 fd
= tsocket_bsd_common_prepare_fd(fd
, true);
1248 dgram
= tdgram_context_create(mem_ctx
,
1254 int saved_errno
= errno
;
1256 errno
= saved_errno
;
1261 talloc_set_destructor(bsds
, tdgram_bsd_destructor
);
1267 ret
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
1268 (const void *)&val
, sizeof(val
));
1270 int saved_errno
= errno
;
1272 errno
= saved_errno
;
1281 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
,
1282 (const void *)&val
, sizeof(val
));
1284 int saved_errno
= errno
;
1286 errno
= saved_errno
;
1294 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1295 (const void *)&val
, sizeof(val
));
1297 int saved_errno
= errno
;
1299 errno
= saved_errno
;
1305 ret
= bind(fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
1307 int saved_errno
= errno
;
1309 errno
= saved_errno
;
1315 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
1321 ret
= connect(fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
1323 int saved_errno
= errno
;
1325 errno
= saved_errno
;
1334 int _tdgram_inet_udp_socket(const struct tsocket_address
*local
,
1335 const struct tsocket_address
*remote
,
1336 TALLOC_CTX
*mem_ctx
,
1337 struct tdgram_context
**dgram
,
1338 const char *location
)
1340 struct tsocket_address_bsd
*lbsda
=
1341 talloc_get_type_abort(local
->private_data
,
1342 struct tsocket_address_bsd
);
1345 switch (lbsda
->u
.sa
.sa_family
) {
1357 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1358 mem_ctx
, dgram
, location
);
1363 int _tdgram_unix_socket(const struct tsocket_address
*local
,
1364 const struct tsocket_address
*remote
,
1365 TALLOC_CTX
*mem_ctx
,
1366 struct tdgram_context
**dgram
,
1367 const char *location
)
1369 struct tsocket_address_bsd
*lbsda
=
1370 talloc_get_type_abort(local
->private_data
,
1371 struct tsocket_address_bsd
);
1374 switch (lbsda
->u
.sa
.sa_family
) {
1382 ret
= tdgram_bsd_dgram_socket(local
, remote
, false,
1383 mem_ctx
, dgram
, location
);
1388 struct tstream_bsd
{
1392 struct tevent_fd
*fde
;
1394 void *readable_private
;
1395 void (*readable_handler
)(void *private_data
);
1396 void *writeable_private
;
1397 void (*writeable_handler
)(void *private_data
);
1400 static void tstream_bsd_fde_handler(struct tevent_context
*ev
,
1401 struct tevent_fd
*fde
,
1405 struct tstream_bsd
*bsds
= talloc_get_type_abort(private_data
,
1406 struct tstream_bsd
);
1408 if (flags
& TEVENT_FD_WRITE
) {
1409 bsds
->writeable_handler(bsds
->writeable_private
);
1412 if (flags
& TEVENT_FD_READ
) {
1413 if (!bsds
->readable_handler
) {
1414 if (bsds
->writeable_handler
) {
1415 bsds
->writeable_handler(bsds
->writeable_private
);
1418 TEVENT_FD_NOT_READABLE(bsds
->fde
);
1421 bsds
->readable_handler(bsds
->readable_private
);
1426 static int tstream_bsd_set_readable_handler(struct tstream_bsd
*bsds
,
1427 struct tevent_context
*ev
,
1428 void (*handler
)(void *private_data
),
1436 if (!bsds
->readable_handler
) {
1439 bsds
->readable_handler
= NULL
;
1440 bsds
->readable_private
= NULL
;
1445 /* read and write must use the same tevent_context */
1446 if (bsds
->event_ptr
!= ev
) {
1447 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1451 bsds
->event_ptr
= NULL
;
1452 TALLOC_FREE(bsds
->fde
);
1455 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1456 TALLOC_FREE(bsds
->fde
);
1458 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1459 bsds
->fd
, TEVENT_FD_READ
,
1460 tstream_bsd_fde_handler
,
1467 /* cache the event context we're running on */
1468 bsds
->event_ptr
= ev
;
1469 } else if (!bsds
->readable_handler
) {
1470 TEVENT_FD_READABLE(bsds
->fde
);
1473 bsds
->readable_handler
= handler
;
1474 bsds
->readable_private
= private_data
;
1479 static int tstream_bsd_set_writeable_handler(struct tstream_bsd
*bsds
,
1480 struct tevent_context
*ev
,
1481 void (*handler
)(void *private_data
),
1489 if (!bsds
->writeable_handler
) {
1492 bsds
->writeable_handler
= NULL
;
1493 bsds
->writeable_private
= NULL
;
1494 TEVENT_FD_NOT_WRITEABLE(bsds
->fde
);
1499 /* read and write must use the same tevent_context */
1500 if (bsds
->event_ptr
!= ev
) {
1501 if (bsds
->readable_handler
|| bsds
->writeable_handler
) {
1505 bsds
->event_ptr
= NULL
;
1506 TALLOC_FREE(bsds
->fde
);
1509 if (tevent_fd_get_flags(bsds
->fde
) == 0) {
1510 TALLOC_FREE(bsds
->fde
);
1512 bsds
->fde
= tevent_add_fd(ev
, bsds
,
1514 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
1515 tstream_bsd_fde_handler
,
1522 /* cache the event context we're running on */
1523 bsds
->event_ptr
= ev
;
1524 } else if (!bsds
->writeable_handler
) {
1525 uint16_t flags
= tevent_fd_get_flags(bsds
->fde
);
1526 flags
|= TEVENT_FD_READ
| TEVENT_FD_WRITE
;
1527 tevent_fd_set_flags(bsds
->fde
, flags
);
1530 bsds
->writeable_handler
= handler
;
1531 bsds
->writeable_private
= private_data
;
1536 static ssize_t
tstream_bsd_pending_bytes(struct tstream_context
*stream
)
1538 struct tstream_bsd
*bsds
= tstream_context_data(stream
,
1539 struct tstream_bsd
);
1542 if (bsds
->fd
== -1) {
1547 ret
= tsocket_bsd_pending(bsds
->fd
);
1552 struct tstream_bsd_readv_state
{
1553 struct tstream_context
*stream
;
1555 struct iovec
*vector
;
1561 static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state
*state
)
1563 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1564 struct tstream_bsd
);
1566 tstream_bsd_set_readable_handler(bsds
, NULL
, NULL
, NULL
);
1571 static void tstream_bsd_readv_handler(void *private_data
);
1573 static struct tevent_req
*tstream_bsd_readv_send(TALLOC_CTX
*mem_ctx
,
1574 struct tevent_context
*ev
,
1575 struct tstream_context
*stream
,
1576 struct iovec
*vector
,
1579 struct tevent_req
*req
;
1580 struct tstream_bsd_readv_state
*state
;
1581 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1584 req
= tevent_req_create(mem_ctx
, &state
,
1585 struct tstream_bsd_readv_state
);
1590 state
->stream
= stream
;
1591 /* we make a copy of the vector so that we can modify it */
1592 state
->vector
= talloc_array(state
, struct iovec
, count
);
1593 if (tevent_req_nomem(state
->vector
, req
)) {
1596 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1597 state
->count
= count
;
1600 talloc_set_destructor(state
, tstream_bsd_readv_destructor
);
1602 if (bsds
->fd
== -1) {
1603 tevent_req_error(req
, ENOTCONN
);
1608 * this is a fast path, not waiting for the
1609 * socket to become explicit readable gains
1610 * about 10%-20% performance in benchmark tests.
1612 tstream_bsd_readv_handler(req
);
1613 if (!tevent_req_is_in_progress(req
)) {
1617 ret
= tstream_bsd_set_readable_handler(bsds
, ev
,
1618 tstream_bsd_readv_handler
,
1621 tevent_req_error(req
, errno
);
1628 tevent_req_post(req
, ev
);
1632 static void tstream_bsd_readv_handler(void *private_data
)
1634 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1636 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1637 struct tstream_bsd_readv_state
);
1638 struct tstream_context
*stream
= state
->stream
;
1639 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1644 ret
= readv(bsds
->fd
, state
->vector
, state
->count
);
1646 /* propagate end of file */
1647 tevent_req_error(req
, EPIPE
);
1650 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1655 if (tevent_req_error(req
, err
)) {
1662 if (ret
< state
->vector
[0].iov_len
) {
1664 base
= (uint8_t *)state
->vector
[0].iov_base
;
1666 state
->vector
[0].iov_base
= base
;
1667 state
->vector
[0].iov_len
-= ret
;
1670 ret
-= state
->vector
[0].iov_len
;
1676 * there're maybe some empty vectors at the end
1677 * which we need to skip, otherwise we would get
1678 * ret == 0 from the readv() call and return EPIPE
1680 while (state
->count
> 0) {
1681 if (state
->vector
[0].iov_len
> 0) {
1688 if (state
->count
> 0) {
1689 /* we have more to read */
1693 tevent_req_done(req
);
1696 static int tstream_bsd_readv_recv(struct tevent_req
*req
,
1699 struct tstream_bsd_readv_state
*state
= tevent_req_data(req
,
1700 struct tstream_bsd_readv_state
);
1703 ret
= tsocket_simple_int_recv(req
, perrno
);
1708 tevent_req_received(req
);
1712 struct tstream_bsd_writev_state
{
1713 struct tstream_context
*stream
;
1715 struct iovec
*vector
;
1721 static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state
*state
)
1723 struct tstream_bsd
*bsds
= tstream_context_data(state
->stream
,
1724 struct tstream_bsd
);
1726 tstream_bsd_set_writeable_handler(bsds
, NULL
, NULL
, NULL
);
1731 static void tstream_bsd_writev_handler(void *private_data
);
1733 static struct tevent_req
*tstream_bsd_writev_send(TALLOC_CTX
*mem_ctx
,
1734 struct tevent_context
*ev
,
1735 struct tstream_context
*stream
,
1736 const struct iovec
*vector
,
1739 struct tevent_req
*req
;
1740 struct tstream_bsd_writev_state
*state
;
1741 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1744 req
= tevent_req_create(mem_ctx
, &state
,
1745 struct tstream_bsd_writev_state
);
1750 state
->stream
= stream
;
1751 /* we make a copy of the vector so that we can modify it */
1752 state
->vector
= talloc_array(state
, struct iovec
, count
);
1753 if (tevent_req_nomem(state
->vector
, req
)) {
1756 memcpy(state
->vector
, vector
, sizeof(struct iovec
)*count
);
1757 state
->count
= count
;
1760 talloc_set_destructor(state
, tstream_bsd_writev_destructor
);
1762 if (bsds
->fd
== -1) {
1763 tevent_req_error(req
, ENOTCONN
);
1768 * this is a fast path, not waiting for the
1769 * socket to become explicit writeable gains
1770 * about 10%-20% performance in benchmark tests.
1772 tstream_bsd_writev_handler(req
);
1773 if (!tevent_req_is_in_progress(req
)) {
1777 ret
= tstream_bsd_set_writeable_handler(bsds
, ev
,
1778 tstream_bsd_writev_handler
,
1781 tevent_req_error(req
, errno
);
1788 tevent_req_post(req
, ev
);
1792 static void tstream_bsd_writev_handler(void *private_data
)
1794 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
1796 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1797 struct tstream_bsd_writev_state
);
1798 struct tstream_context
*stream
= state
->stream
;
1799 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1804 ret
= writev(bsds
->fd
, state
->vector
, state
->count
);
1806 /* propagate end of file */
1807 tevent_req_error(req
, EPIPE
);
1810 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
1815 if (tevent_req_error(req
, err
)) {
1822 if (ret
< state
->vector
[0].iov_len
) {
1824 base
= (uint8_t *)state
->vector
[0].iov_base
;
1826 state
->vector
[0].iov_base
= base
;
1827 state
->vector
[0].iov_len
-= ret
;
1830 ret
-= state
->vector
[0].iov_len
;
1836 * there're maybe some empty vectors at the end
1837 * which we need to skip, otherwise we would get
1838 * ret == 0 from the writev() call and return EPIPE
1840 while (state
->count
> 0) {
1841 if (state
->vector
[0].iov_len
> 0) {
1848 if (state
->count
> 0) {
1849 /* we have more to read */
1853 tevent_req_done(req
);
1856 static int tstream_bsd_writev_recv(struct tevent_req
*req
, int *perrno
)
1858 struct tstream_bsd_writev_state
*state
= tevent_req_data(req
,
1859 struct tstream_bsd_writev_state
);
1862 ret
= tsocket_simple_int_recv(req
, perrno
);
1867 tevent_req_received(req
);
1871 struct tstream_bsd_disconnect_state
{
1875 static struct tevent_req
*tstream_bsd_disconnect_send(TALLOC_CTX
*mem_ctx
,
1876 struct tevent_context
*ev
,
1877 struct tstream_context
*stream
)
1879 struct tstream_bsd
*bsds
= tstream_context_data(stream
, struct tstream_bsd
);
1880 struct tevent_req
*req
;
1881 struct tstream_bsd_disconnect_state
*state
;
1886 req
= tevent_req_create(mem_ctx
, &state
,
1887 struct tstream_bsd_disconnect_state
);
1892 if (bsds
->fd
== -1) {
1893 tevent_req_error(req
, ENOTCONN
);
1897 ret
= close(bsds
->fd
);
1899 err
= tsocket_bsd_error_from_errno(ret
, errno
, &dummy
);
1900 if (tevent_req_error(req
, err
)) {
1904 tevent_req_done(req
);
1906 tevent_req_post(req
, ev
);
1910 static int tstream_bsd_disconnect_recv(struct tevent_req
*req
,
1915 ret
= tsocket_simple_int_recv(req
, perrno
);
1917 tevent_req_received(req
);
1921 static const struct tstream_context_ops tstream_bsd_ops
= {
1924 .pending_bytes
= tstream_bsd_pending_bytes
,
1926 .readv_send
= tstream_bsd_readv_send
,
1927 .readv_recv
= tstream_bsd_readv_recv
,
1929 .writev_send
= tstream_bsd_writev_send
,
1930 .writev_recv
= tstream_bsd_writev_recv
,
1932 .disconnect_send
= tstream_bsd_disconnect_send
,
1933 .disconnect_recv
= tstream_bsd_disconnect_recv
,
1936 static int tstream_bsd_destructor(struct tstream_bsd
*bsds
)
1938 TALLOC_FREE(bsds
->fde
);
1939 if (bsds
->fd
!= -1) {
1946 int _tstream_bsd_existing_socket(TALLOC_CTX
*mem_ctx
,
1948 struct tstream_context
**_stream
,
1949 const char *location
)
1951 struct tstream_context
*stream
;
1952 struct tstream_bsd
*bsds
;
1954 stream
= tstream_context_create(mem_ctx
,
1964 talloc_set_destructor(bsds
, tstream_bsd_destructor
);
1970 struct tstream_bsd_connect_state
{
1972 struct tevent_fd
*fde
;
1973 struct tstream_conext
*stream
;
1976 static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state
*state
)
1978 TALLOC_FREE(state
->fde
);
1979 if (state
->fd
!= -1) {
1987 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
1988 struct tevent_fd
*fde
,
1990 void *private_data
);
1992 static struct tevent_req
* tstream_bsd_connect_send(TALLOC_CTX
*mem_ctx
,
1993 struct tevent_context
*ev
,
1995 const struct tsocket_address
*local
,
1996 const struct tsocket_address
*remote
)
1998 struct tevent_req
*req
;
1999 struct tstream_bsd_connect_state
*state
;
2000 struct tsocket_address_bsd
*lbsda
=
2001 talloc_get_type_abort(local
->private_data
,
2002 struct tsocket_address_bsd
);
2003 struct tsocket_address_bsd
*rbsda
=
2004 talloc_get_type_abort(remote
->private_data
,
2005 struct tsocket_address_bsd
);
2009 bool do_bind
= false;
2010 bool do_reuseaddr
= false;
2011 bool do_ipv6only
= false;
2012 bool is_inet
= false;
2013 int sa_fam
= lbsda
->u
.sa
.sa_family
;
2015 req
= tevent_req_create(mem_ctx
, &state
,
2016 struct tstream_bsd_connect_state
);
2023 talloc_set_destructor(state
, tstream_bsd_connect_destructor
);
2025 /* give the wrappers a chance to report an error */
2026 if (sys_errno
!= 0) {
2027 tevent_req_error(req
, sys_errno
);
2031 switch (lbsda
->u
.sa
.sa_family
) {
2033 if (lbsda
->u
.un
.sun_path
[0] != 0) {
2034 do_reuseaddr
= true;
2039 if (lbsda
->u
.in
.sin_port
!= 0) {
2040 do_reuseaddr
= true;
2043 if (lbsda
->u
.in
.sin_addr
.s_addr
!= INADDR_ANY
) {
2050 if (lbsda
->u
.in6
.sin6_port
!= 0) {
2051 do_reuseaddr
= true;
2054 if (memcmp(&in6addr_any
,
2055 &lbsda
->u
.in6
.sin6_addr
,
2056 sizeof(in6addr_any
)) != 0) {
2064 tevent_req_error(req
, EINVAL
);
2068 if (!do_bind
&& is_inet
) {
2069 sa_fam
= rbsda
->u
.sa
.sa_family
;
2072 do_ipv6only
= false;
2082 state
->fd
= socket(sa_fam
, SOCK_STREAM
, 0);
2083 if (state
->fd
== -1) {
2084 tevent_req_error(req
, errno
);
2088 state
->fd
= tsocket_bsd_common_prepare_fd(state
->fd
, true);
2089 if (state
->fd
== -1) {
2090 tevent_req_error(req
, errno
);
2098 ret
= setsockopt(state
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
2099 (const void *)&val
, sizeof(val
));
2101 tevent_req_error(req
, errno
);
2110 ret
= setsockopt(state
->fd
, SOL_SOCKET
, SO_REUSEADDR
,
2111 (const void *)&val
, sizeof(val
));
2113 tevent_req_error(req
, errno
);
2119 ret
= bind(state
->fd
, &lbsda
->u
.sa
, lbsda
->sa_socklen
);
2121 tevent_req_error(req
, errno
);
2126 if (rbsda
->u
.sa
.sa_family
!= sa_fam
) {
2127 tevent_req_error(req
, EINVAL
);
2131 ret
= connect(state
->fd
, &rbsda
->u
.sa
, rbsda
->sa_socklen
);
2132 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2137 if (tevent_req_error(req
, err
)) {
2141 tevent_req_done(req
);
2145 state
->fde
= tevent_add_fd(ev
, state
,
2147 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
2148 tstream_bsd_connect_fde_handler
,
2150 if (tevent_req_nomem(state
->fde
, req
)) {
2157 tevent_req_post(req
, ev
);
2161 static void tstream_bsd_connect_fde_handler(struct tevent_context
*ev
,
2162 struct tevent_fd
*fde
,
2166 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
2168 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2169 struct tstream_bsd_connect_state
);
2172 socklen_t len
= sizeof(error
);
2176 ret
= getsockopt(state
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &len
);
2183 err
= tsocket_bsd_error_from_errno(ret
, errno
, &retry
);
2188 if (tevent_req_error(req
, err
)) {
2192 tevent_req_done(req
);
2195 static int tstream_bsd_connect_recv(struct tevent_req
*req
,
2197 TALLOC_CTX
*mem_ctx
,
2198 struct tstream_context
**stream
,
2199 const char *location
)
2201 struct tstream_bsd_connect_state
*state
= tevent_req_data(req
,
2202 struct tstream_bsd_connect_state
);
2205 ret
= tsocket_simple_int_recv(req
, perrno
);
2207 ret
= _tstream_bsd_existing_socket(mem_ctx
,
2215 TALLOC_FREE(state
->fde
);
2220 tevent_req_received(req
);
2224 struct tevent_req
* tstream_inet_tcp_connect_send(TALLOC_CTX
*mem_ctx
,
2225 struct tevent_context
*ev
,
2226 const struct tsocket_address
*local
,
2227 const struct tsocket_address
*remote
)
2229 struct tsocket_address_bsd
*lbsda
=
2230 talloc_get_type_abort(local
->private_data
,
2231 struct tsocket_address_bsd
);
2232 struct tevent_req
*req
;
2235 switch (lbsda
->u
.sa
.sa_family
) {
2247 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2252 int _tstream_inet_tcp_connect_recv(struct tevent_req
*req
,
2254 TALLOC_CTX
*mem_ctx
,
2255 struct tstream_context
**stream
,
2256 const char *location
)
2258 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2261 struct tevent_req
* tstream_unix_connect_send(TALLOC_CTX
*mem_ctx
,
2262 struct tevent_context
*ev
,
2263 const struct tsocket_address
*local
,
2264 const struct tsocket_address
*remote
)
2266 struct tsocket_address_bsd
*lbsda
=
2267 talloc_get_type_abort(local
->private_data
,
2268 struct tsocket_address_bsd
);
2269 struct tevent_req
*req
;
2272 switch (lbsda
->u
.sa
.sa_family
) {
2280 req
= tstream_bsd_connect_send(mem_ctx
, ev
, sys_errno
, local
, remote
);
2285 int _tstream_unix_connect_recv(struct tevent_req
*req
,
2287 TALLOC_CTX
*mem_ctx
,
2288 struct tstream_context
**stream
,
2289 const char *location
)
2291 return tstream_bsd_connect_recv(req
, perrno
, mem_ctx
, stream
, location
);
2294 int _tstream_unix_socketpair(TALLOC_CTX
*mem_ctx1
,
2295 struct tstream_context
**_stream1
,
2296 TALLOC_CTX
*mem_ctx2
,
2297 struct tstream_context
**_stream2
,
2298 const char *location
)
2304 struct tstream_context
*stream1
= NULL
;
2305 struct tstream_context
*stream2
= NULL
;
2307 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
2314 fd1
= tsocket_bsd_common_prepare_fd(fd1
, true);
2316 int sys_errno
= errno
;
2322 fd2
= tsocket_bsd_common_prepare_fd(fd2
, true);
2324 int sys_errno
= errno
;
2330 ret
= _tstream_bsd_existing_socket(mem_ctx1
,
2335 int sys_errno
= errno
;
2342 ret
= _tstream_bsd_existing_socket(mem_ctx2
,
2347 int sys_errno
= errno
;
2348 talloc_free(stream1
);
2354 *_stream1
= stream1
;
2355 *_stream2
= stream2
;