smbdotconf: mark "dfree command" with substitution="1"
[Samba.git] / libcli / smb / smb2cli_read.c
blob8110b65d432259b0e50d6799ef4ddedebb9c4628
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;
32 bool out_valid;
35 static void smb2cli_read_done(struct tevent_req *subreq);
37 struct tevent_req *smb2cli_read_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct smbXcli_conn *conn,
40 uint32_t timeout_msec,
41 struct smbXcli_session *session,
42 struct smbXcli_tcon *tcon,
43 uint32_t length,
44 uint64_t offset,
45 uint64_t fid_persistent,
46 uint64_t fid_volatile,
47 uint64_t minimum_count,
48 uint64_t remaining_bytes)
50 struct tevent_req *req, *subreq;
51 struct smb2cli_read_state *state;
52 uint8_t *fixed;
54 req = tevent_req_create(mem_ctx, &state,
55 struct smb2cli_read_state);
56 if (req == NULL) {
57 return NULL;
60 fixed = state->fixed;
62 SSVAL(fixed, 0, 49);
63 SIVAL(fixed, 4, length);
64 SBVAL(fixed, 8, offset);
65 SBVAL(fixed, 16, fid_persistent);
66 SBVAL(fixed, 24, fid_volatile);
67 SBVAL(fixed, 32, minimum_count);
68 SBVAL(fixed, 40, remaining_bytes);
70 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_READ,
71 0, 0, /* flags */
72 timeout_msec,
73 tcon,
74 session,
75 state->fixed, sizeof(state->fixed),
76 state->dyn_pad, sizeof(state->dyn_pad),
77 length); /* max_dyn_len */
78 if (tevent_req_nomem(subreq, req)) {
79 return tevent_req_post(req, ev);
81 tevent_req_set_callback(subreq, smb2cli_read_done, req);
82 return req;
85 static void smb2cli_read_done(struct tevent_req *subreq)
87 struct tevent_req *req = tevent_req_callback_data(
88 subreq, struct tevent_req);
89 struct smb2cli_read_state *state =
90 tevent_req_data(req,
91 struct smb2cli_read_state);
92 NTSTATUS status;
93 struct iovec *iov;
94 uint8_t data_offset;
95 static const struct smb2cli_req_expected_response expected[] = {
97 .status = STATUS_BUFFER_OVERFLOW,
98 .body_size = 0x11
101 .status = NT_STATUS_OK,
102 .body_size = 0x11
106 status = smb2cli_req_recv(subreq, state, &iov,
107 expected, ARRAY_SIZE(expected));
108 TALLOC_FREE(subreq);
109 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
110 /* no error */
111 } else {
112 if (tevent_req_nterror(req, status)) {
113 return;
117 data_offset = CVAL(iov[1].iov_base, 2);
118 state->data_length = IVAL(iov[1].iov_base, 4);
120 if ((data_offset != SMB2_HDR_BODY + 16) ||
121 (state->data_length > iov[2].iov_len)) {
122 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
123 return;
126 state->recv_iov = iov;
127 state->data = (uint8_t *)iov[2].iov_base;
129 state->out_valid = true;
131 if (tevent_req_nterror(req, status)) {
132 return;
135 tevent_req_done(req);
138 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
139 uint8_t **data, uint32_t *data_length)
141 struct smb2cli_read_state *state =
142 tevent_req_data(req,
143 struct smb2cli_read_state);
144 NTSTATUS status = NT_STATUS_OK;
146 if (tevent_req_is_nterror(req, &status) && !state->out_valid) {
147 *data_length = 0;
148 *data = NULL;
149 tevent_req_received(req);
150 return status;
152 talloc_steal(mem_ctx, state->recv_iov);
153 *data_length = state->data_length;
154 *data = state->data;
155 tevent_req_received(req);
156 return status;
159 NTSTATUS smb2cli_read(struct smbXcli_conn *conn,
160 uint32_t timeout_msec,
161 struct smbXcli_session *session,
162 struct smbXcli_tcon *tcon,
163 uint32_t length,
164 uint64_t offset,
165 uint64_t fid_persistent,
166 uint64_t fid_volatile,
167 uint64_t minimum_count,
168 uint64_t remaining_bytes,
169 TALLOC_CTX *mem_ctx,
170 uint8_t **data,
171 uint32_t *data_length)
173 TALLOC_CTX *frame = talloc_stackframe();
174 struct tevent_context *ev;
175 struct tevent_req *req;
176 NTSTATUS status = NT_STATUS_NO_MEMORY;
178 if (smbXcli_conn_has_async_calls(conn)) {
180 * Can't use sync call while an async call is in flight
182 status = NT_STATUS_INVALID_PARAMETER;
183 goto fail;
185 ev = samba_tevent_context_init(frame);
186 if (ev == NULL) {
187 goto fail;
189 req = smb2cli_read_send(frame, ev,
190 conn, timeout_msec, session, tcon,
191 length, offset,
192 fid_persistent, fid_volatile,
193 minimum_count, remaining_bytes);
194 if (req == NULL) {
195 goto fail;
197 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
198 goto fail;
200 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
201 fail:
202 TALLOC_FREE(frame);
203 return status;