libcli/smb: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / libcli / smb / smb2cli_read.c
blob0adb4037944257d0f747caa9690c2cec4a0e84db
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Volker Lendecke 2011
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/>.
20 #include "includes.h"
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
26 struct smb2cli_read_state {
27 uint8_t fixed[48];
28 uint8_t dyn_pad[1];
29 struct iovec *recv_iov;
30 uint8_t *data;
31 uint32_t data_length;
34 static void smb2cli_read_done(struct tevent_req *subreq);
36 struct tevent_req *smb2cli_read_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct smbXcli_conn *conn,
39 uint32_t timeout_msec,
40 struct smbXcli_session *session,
41 struct smbXcli_tcon *tcon,
42 uint32_t length,
43 uint64_t offset,
44 uint64_t fid_persistent,
45 uint64_t fid_volatile,
46 uint64_t minimum_count,
47 uint64_t remaining_bytes)
49 struct tevent_req *req, *subreq;
50 struct smb2cli_read_state *state;
51 uint8_t *fixed;
53 req = tevent_req_create(mem_ctx, &state,
54 struct smb2cli_read_state);
55 if (req == NULL) {
56 return NULL;
59 fixed = state->fixed;
61 SSVAL(fixed, 0, 49);
62 SIVAL(fixed, 4, length);
63 SBVAL(fixed, 8, offset);
64 SBVAL(fixed, 16, fid_persistent);
65 SBVAL(fixed, 24, fid_volatile);
66 SBVAL(fixed, 32, minimum_count);
67 SBVAL(fixed, 40, remaining_bytes);
69 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_READ,
70 0, 0, /* flags */
71 timeout_msec,
72 tcon,
73 session,
74 state->fixed, sizeof(state->fixed),
75 state->dyn_pad, sizeof(state->dyn_pad));
76 if (tevent_req_nomem(subreq, req)) {
77 return tevent_req_post(req, ev);
79 tevent_req_set_callback(subreq, smb2cli_read_done, req);
80 return req;
83 static void smb2cli_read_done(struct tevent_req *subreq)
85 struct tevent_req *req = tevent_req_callback_data(
86 subreq, struct tevent_req);
87 struct smb2cli_read_state *state =
88 tevent_req_data(req,
89 struct smb2cli_read_state);
90 NTSTATUS status;
91 struct iovec *iov;
92 uint8_t data_offset;
93 static const struct smb2cli_req_expected_response expected[] = {
95 .status = STATUS_BUFFER_OVERFLOW,
96 .body_size = 0x11
99 .status = NT_STATUS_OK,
100 .body_size = 0x11
104 status = smb2cli_req_recv(subreq, state, &iov,
105 expected, ARRAY_SIZE(expected));
106 TALLOC_FREE(subreq);
107 if (tevent_req_nterror(req, status)) {
108 return;
111 data_offset = CVAL(iov[1].iov_base, 2);
112 state->data_length = IVAL(iov[1].iov_base, 4);
114 if ((data_offset != SMB2_HDR_BODY + 16) ||
115 (state->data_length > iov[2].iov_len)) {
116 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
117 return;
120 state->recv_iov = iov;
121 state->data = (uint8_t *)iov[2].iov_base;
122 tevent_req_done(req);
125 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
126 uint8_t **data, uint32_t *data_length)
128 struct smb2cli_read_state *state =
129 tevent_req_data(req,
130 struct smb2cli_read_state);
131 NTSTATUS status;
133 if (tevent_req_is_nterror(req, &status)) {
134 return status;
136 talloc_steal(mem_ctx, state->recv_iov);
137 *data_length = state->data_length;
138 *data = state->data;
139 return NT_STATUS_OK;
142 NTSTATUS smb2cli_read(struct smbXcli_conn *conn,
143 uint32_t timeout_msec,
144 struct smbXcli_session *session,
145 struct smbXcli_tcon *tcon,
146 uint32_t length,
147 uint64_t offset,
148 uint64_t fid_persistent,
149 uint64_t fid_volatile,
150 uint64_t minimum_count,
151 uint64_t remaining_bytes,
152 TALLOC_CTX *mem_ctx,
153 uint8_t **data,
154 uint32_t *data_length)
156 TALLOC_CTX *frame = talloc_stackframe();
157 struct tevent_context *ev;
158 struct tevent_req *req;
159 NTSTATUS status = NT_STATUS_NO_MEMORY;
161 if (smbXcli_conn_has_async_calls(conn)) {
163 * Can't use sync call while an async call is in flight
165 status = NT_STATUS_INVALID_PARAMETER;
166 goto fail;
168 ev = samba_tevent_context_init(frame);
169 if (ev == NULL) {
170 goto fail;
172 req = smb2cli_read_send(frame, ev,
173 conn, timeout_msec, session, tcon,
174 length, offset,
175 fid_persistent, fid_volatile,
176 minimum_count, remaining_bytes);
177 if (req == NULL) {
178 goto fail;
180 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
181 goto fail;
183 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
184 fail:
185 TALLOC_FREE(frame);
186 return status;