2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Volker Lendecke 2019
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "tstream_u32_read.h"
23 #include "lib/util/byteorder.h"
24 #include "lib/util/tevent_unix.h"
26 struct tstream_u32_read_state
{
32 static int tstream_u32_read_next_vector(struct tstream_context
*stream
,
35 struct iovec
**_vector
,
37 static void tstream_u32_read_done(struct tevent_req
*subreq
);
39 struct tevent_req
*tstream_u32_read_send(
41 struct tevent_context
*ev
,
43 struct tstream_context
*stream
)
45 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
46 struct tstream_u32_read_state
*state
= NULL
;
48 req
= tevent_req_create(
49 mem_ctx
, &state
, struct tstream_u32_read_state
);
53 state
->max_msglen
= max_msglen
;
55 subreq
= tstream_readv_pdu_send(
59 tstream_u32_read_next_vector
,
61 if (tevent_req_nomem(subreq
, req
)) {
62 return tevent_req_post(req
, ev
);
64 tevent_req_set_callback(subreq
, tstream_u32_read_done
, req
);
68 static int tstream_u32_read_next_vector(struct tstream_context
*stream
,
71 struct iovec
**_vector
,
74 struct tstream_u32_read_state
*state
= talloc_get_type_abort(
75 private_data
, struct tstream_u32_read_state
);
76 size_t buflen
= talloc_get_size(state
->buf
);
84 state
->buf
= talloc_array(state
, uint8_t, msg_len
);
85 if (state
->buf
== NULL
) {
88 } else if (buflen
== 4) {
92 msg_len
= RIVAL(state
->buf
, 0);
93 if ((msg_len
== 0) || (msg_len
> state
->max_msglen
)) {
103 state
->buf
= talloc_realloc(
104 state
, state
->buf
, uint8_t, msg_len
);
105 if (state
->buf
== NULL
) {
114 vector
= talloc(mem_ctx
, struct iovec
);
115 if (vector
== NULL
) {
118 *vector
= (struct iovec
) {
119 .iov_base
= state
->buf
+ ofs
, .iov_len
= msg_len
- ofs
,
128 static void tstream_u32_read_done(struct tevent_req
*subreq
)
130 struct tevent_req
*req
= tevent_req_callback_data(
131 subreq
, struct tevent_req
);
134 ret
= tstream_readv_pdu_recv(subreq
, &err
);
137 tevent_req_error(req
, err
);
140 tevent_req_done(req
);
143 int tstream_u32_read_recv(
144 struct tevent_req
*req
,
149 struct tstream_u32_read_state
*state
= tevent_req_data(
150 req
, struct tstream_u32_read_state
);
153 if (tevent_req_is_unix_error(req
, &err
)) {
156 *buflen
= talloc_get_size(state
->buf
);
157 *buf
= talloc_move(mem_ctx
, &state
->buf
);