libcli/smb: move smb2cli_close.c from source3 to the toplevel
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_read.c
blobc5e40b7c57a4bcb44f326c00942e907370a842d4
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 "client.h"
22 #include "async_smb.h"
23 #include "../libcli/smb/smbXcli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_read_state {
29 uint8_t fixed[48];
30 uint8_t dyn_pad[1];
31 struct iovec *recv_iov;
32 uint8_t *data;
33 uint32_t data_length;
36 static void smb2cli_read_done(struct tevent_req *subreq);
38 struct tevent_req *smb2cli_read_send(TALLOC_CTX *mem_ctx,
39 struct tevent_context *ev,
40 struct smbXcli_conn *conn,
41 uint32_t timeout_msec,
42 struct smbXcli_session *session,
43 uint32_t tcon_id,
44 uint32_t length,
45 uint64_t offset,
46 uint64_t fid_persistent,
47 uint64_t fid_volatile,
48 uint64_t minimum_count,
49 uint64_t remaining_bytes)
51 struct tevent_req *req, *subreq;
52 struct smb2cli_read_state *state;
53 uint8_t *fixed;
55 req = tevent_req_create(mem_ctx, &state,
56 struct smb2cli_read_state);
57 if (req == NULL) {
58 return NULL;
61 fixed = state->fixed;
63 SSVAL(fixed, 0, 49);
64 SIVAL(fixed, 4, length);
65 SBVAL(fixed, 8, offset);
66 SBVAL(fixed, 16, fid_persistent);
67 SBVAL(fixed, 24, fid_volatile);
68 SBVAL(fixed, 32, minimum_count);
69 SBVAL(fixed, 40, remaining_bytes);
71 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_READ,
72 0, 0, /* flags */
73 timeout_msec,
74 0xFEFF, /* pid */
75 tcon_id,
76 session,
77 state->fixed, sizeof(state->fixed),
78 state->dyn_pad, sizeof(state->dyn_pad));
79 if (tevent_req_nomem(subreq, req)) {
80 return tevent_req_post(req, ev);
82 tevent_req_set_callback(subreq, smb2cli_read_done, req);
83 return req;
86 static void smb2cli_read_done(struct tevent_req *subreq)
88 struct tevent_req *req = tevent_req_callback_data(
89 subreq, struct tevent_req);
90 struct smb2cli_read_state *state =
91 tevent_req_data(req,
92 struct smb2cli_read_state);
93 NTSTATUS status;
94 struct iovec *iov;
95 uint8_t data_offset;
96 static const struct smb2cli_req_expected_response expected[] = {
98 .status = STATUS_BUFFER_OVERFLOW,
99 .body_size = 0x11
102 .status = NT_STATUS_OK,
103 .body_size = 0x11
107 status = smb2cli_req_recv(subreq, state, &iov,
108 expected, ARRAY_SIZE(expected));
109 if (tevent_req_nterror(req, status)) {
110 return;
113 data_offset = CVAL(iov[1].iov_base, 2);
114 state->data_length = IVAL(iov[1].iov_base, 4);
116 if ((data_offset != SMB2_HDR_BODY + 16) ||
117 (state->data_length > iov[2].iov_len)) {
118 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
119 return;
122 state->recv_iov = iov;
123 state->data = (uint8_t *)iov[2].iov_base;
124 tevent_req_done(req);
127 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
128 uint8_t **data, uint32_t *data_length)
130 struct smb2cli_read_state *state =
131 tevent_req_data(req,
132 struct smb2cli_read_state);
133 NTSTATUS status;
135 if (tevent_req_is_nterror(req, &status)) {
136 return status;
138 talloc_steal(mem_ctx, state->recv_iov);
139 *data_length = state->data_length;
140 *data = state->data;
141 return NT_STATUS_OK;
144 NTSTATUS smb2cli_read(struct smbXcli_conn *conn,
145 uint32_t timeout_msec,
146 struct smbXcli_session *session,
147 uint32_t tcon_id,
148 uint32_t length,
149 uint64_t offset,
150 uint64_t fid_persistent,
151 uint64_t fid_volatile,
152 uint64_t minimum_count,
153 uint64_t remaining_bytes,
154 TALLOC_CTX *mem_ctx,
155 uint8_t **data,
156 uint32_t *data_length)
158 TALLOC_CTX *frame = talloc_stackframe();
159 struct tevent_context *ev;
160 struct tevent_req *req;
161 NTSTATUS status = NT_STATUS_NO_MEMORY;
163 if (smbXcli_conn_has_async_calls(conn)) {
165 * Can't use sync call while an async call is in flight
167 status = NT_STATUS_INVALID_PARAMETER;
168 goto fail;
170 ev = tevent_context_init(frame);
171 if (ev == NULL) {
172 goto fail;
174 req = smb2cli_read_send(frame, ev,
175 conn, timeout_msec, session, tcon_id,
176 length, offset,
177 fid_persistent, fid_volatile,
178 minimum_count, remaining_bytes);
179 if (req == NULL) {
180 goto fail;
182 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
183 goto fail;
185 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
186 fail:
187 TALLOC_FREE(frame);
188 return status;