s4:selftest: skip flakey samba4.drs.repl_schema.python for now
[Samba/vl.git] / source3 / libsmb / smb2cli_read.c
blobe45a75f6cadd8a6369a1800709b29beecc329314
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 "smb2cli_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 cli_state *cli,
41 uint32_t length,
42 uint64_t offset,
43 uint64_t fid_persistent,
44 uint64_t fid_volatile,
45 uint64_t minimum_count,
46 uint64_t remaining_bytes)
48 struct tevent_req *req, *subreq;
49 struct smb2cli_read_state *state;
50 uint8_t *fixed;
52 req = tevent_req_create(mem_ctx, &state,
53 struct smb2cli_read_state);
54 if (req == NULL) {
55 return NULL;
58 fixed = state->fixed;
60 SSVAL(fixed, 0, 49);
61 SIVAL(fixed, 4, length);
62 SBVAL(fixed, 8, offset);
63 SBVAL(fixed, 16, fid_persistent);
64 SBVAL(fixed, 24, fid_volatile);
65 SBVAL(fixed, 32, minimum_count);
66 SBVAL(fixed, 40, remaining_bytes);
68 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_READ,
69 0, 0, /* flags */
70 cli->smb2.pid,
71 cli->smb2.tid,
72 cli->smb2.uid,
73 state->fixed, sizeof(state->fixed),
74 state->dyn_pad, sizeof(state->dyn_pad));
75 if (tevent_req_nomem(subreq, req)) {
76 return tevent_req_post(req, ev);
78 tevent_req_set_callback(subreq, smb2cli_read_done, req);
79 return req;
82 static void smb2cli_read_done(struct tevent_req *subreq)
84 struct tevent_req *req = tevent_req_callback_data(
85 subreq, struct tevent_req);
86 struct smb2cli_read_state *state =
87 tevent_req_data(req,
88 struct smb2cli_read_state);
89 NTSTATUS status;
90 struct iovec *iov;
91 uint8_t data_offset;
93 status = smb2cli_req_recv(subreq, state, &iov, 17);
94 if (tevent_req_nterror(req, status)) {
95 return;
98 data_offset = CVAL(iov[1].iov_base, 2);
99 state->data_length = IVAL(iov[1].iov_base, 4);
101 if ((data_offset != SMB2_HDR_BODY + 16) ||
102 (state->data_length > iov[2].iov_len)) {
103 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
104 return;
107 state->recv_iov = iov;
108 state->data = (uint8_t *)iov[2].iov_base;
109 tevent_req_done(req);
112 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
113 uint8_t **data, uint32_t *data_length)
115 struct smb2cli_read_state *state =
116 tevent_req_data(req,
117 struct smb2cli_read_state);
118 NTSTATUS status;
120 if (tevent_req_is_nterror(req, &status)) {
121 return status;
123 talloc_steal(mem_ctx, state->recv_iov);
124 *data_length = state->data_length;
125 *data = state->data;
126 return NT_STATUS_OK;
129 NTSTATUS smb2cli_read(struct cli_state *cli,
130 uint32_t length,
131 uint64_t offset,
132 uint64_t fid_persistent,
133 uint64_t fid_volatile,
134 uint64_t minimum_count,
135 uint64_t remaining_bytes,
136 TALLOC_CTX *mem_ctx,
137 uint8_t **data,
138 uint32_t *data_length)
140 TALLOC_CTX *frame = talloc_stackframe();
141 struct event_context *ev;
142 struct tevent_req *req;
143 NTSTATUS status = NT_STATUS_NO_MEMORY;
145 if (cli_has_async_calls(cli)) {
147 * Can't use sync call while an async call is in flight
149 status = NT_STATUS_INVALID_PARAMETER;
150 goto fail;
152 ev = event_context_init(frame);
153 if (ev == NULL) {
154 goto fail;
156 req = smb2cli_read_send(frame, ev, cli, length, offset,
157 fid_persistent, fid_volatile,
158 minimum_count, remaining_bytes);
159 if (req == NULL) {
160 goto fail;
162 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
163 goto fail;
165 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
166 fail:
167 TALLOC_FREE(frame);
168 return status;