2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Stefan Metzmacher 2009
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/>.
22 #include "system/filesys.h"
23 #include "../lib/tsocket/tsocket.h"
24 #include "../libcli/util/tstream.h"
25 #include "../lib/util/tevent_ntstatus.h"
27 struct tstream_read_pdu_blob_state {
28 /* this structs are owned by the caller */
30 struct tevent_context *ev;
31 struct tstream_context *stream;
32 NTSTATUS (*full_fn)(void *private_data,
39 struct iovec tmp_vector;
42 static void tstream_read_pdu_blob_done(struct tevent_req *subreq);
44 struct tevent_req *tstream_read_pdu_blob_send(TALLOC_CTX *mem_ctx,
45 struct tevent_context *ev,
46 struct tstream_context *stream,
47 size_t initial_read_size,
48 NTSTATUS (*full_fn)(void *private_data,
53 struct tevent_req *req;
54 struct tstream_read_pdu_blob_state *state;
55 struct tevent_req *subreq;
58 req = tevent_req_create(mem_ctx, &state,
59 struct tstream_read_pdu_blob_state);
64 state->caller.ev = ev;
65 state->caller.stream = stream;
66 state->caller.full_fn = full_fn;
67 state->caller.full_private = full_private;
69 if (initial_read_size == 0) {
70 tevent_req_error(req, EINVAL);
71 return tevent_req_post(req, ev);
74 buf = talloc_array(state, uint8_t, initial_read_size);
75 if (tevent_req_nomem(buf, req)) {
76 return tevent_req_post(req, ev);
78 state->pdu_blob.data = buf;
79 state->pdu_blob.length = initial_read_size;
81 state->tmp_vector.iov_base = buf;
82 state->tmp_vector.iov_len = initial_read_size;
84 subreq = tstream_readv_send(state, ev, stream, &state->tmp_vector, 1);
85 if (tevent_req_nomem(subreq, req)) {
86 return tevent_req_post(req, ev);
88 tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
93 static void tstream_read_pdu_blob_done(struct tevent_req *subreq)
95 struct tevent_req *req =
96 tevent_req_callback_data(subreq,
98 struct tstream_read_pdu_blob_state *state =
100 struct tstream_read_pdu_blob_state);
107 ret = tstream_readv_recv(subreq, &sys_errno);
110 status = map_nt_error_from_unix(sys_errno);
111 tevent_req_nterror(req, status);
115 status = state->caller.full_fn(state->caller.full_private,
116 state->pdu_blob, &pdu_size);
117 if (NT_STATUS_IS_OK(status)) {
118 tevent_req_done(req);
120 } else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
122 } else if (!NT_STATUS_IS_OK(status)) {
123 tevent_req_nterror(req, status);
127 buf = talloc_realloc(state, state->pdu_blob.data, uint8_t, pdu_size);
128 if (tevent_req_nomem(buf, req)) {
131 state->pdu_blob.data = buf;
132 state->pdu_blob.length = pdu_size;
134 state->tmp_vector.iov_base = buf + state->tmp_vector.iov_len;
135 state->tmp_vector.iov_len = pdu_size - state->tmp_vector.iov_len;
137 subreq = tstream_readv_send(state,
139 state->caller.stream,
142 if (tevent_req_nomem(subreq, req)) {
145 tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
148 NTSTATUS tstream_read_pdu_blob_recv(struct tevent_req *req,
152 struct tstream_read_pdu_blob_state *state = tevent_req_data(req,
153 struct tstream_read_pdu_blob_state);
156 if (tevent_req_is_nterror(req, &status)) {
157 tevent_req_received(req);
161 *pdu_blob = state->pdu_blob;
162 talloc_steal(mem_ctx, pdu_blob->data);
164 tevent_req_received(req);