2 Unix SMB/CIFS implementation.
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/>.
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
{
29 struct iovec
*recv_iov
;
33 struct tevent_req
*subreq
;
38 static bool smb2cli_read_cancel(struct tevent_req
*req
);
39 static void smb2cli_read_done(struct tevent_req
*subreq
);
41 struct tevent_req
*smb2cli_read_send(TALLOC_CTX
*mem_ctx
,
42 struct tevent_context
*ev
,
43 struct smbXcli_conn
*conn
,
44 uint32_t timeout_msec
,
45 struct smbXcli_session
*session
,
46 struct smbXcli_tcon
*tcon
,
49 uint64_t fid_persistent
,
50 uint64_t fid_volatile
,
51 uint64_t minimum_count
,
52 uint64_t remaining_bytes
)
54 struct tevent_req
*req
, *subreq
;
55 struct smb2cli_read_state
*state
;
58 req
= tevent_req_create(mem_ctx
, &state
,
59 struct smb2cli_read_state
);
67 SIVAL(fixed
, 4, length
);
68 SBVAL(fixed
, 8, offset
);
69 SBVAL(fixed
, 16, fid_persistent
);
70 SBVAL(fixed
, 24, fid_volatile
);
71 SBVAL(fixed
, 32, minimum_count
);
72 SBVAL(fixed
, 40, remaining_bytes
);
74 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_READ
,
79 state
->fixed
, sizeof(state
->fixed
),
80 state
->dyn_pad
, sizeof(state
->dyn_pad
),
81 length
); /* max_dyn_len */
82 if (tevent_req_nomem(subreq
, req
)) {
83 return tevent_req_post(req
, ev
);
85 tevent_req_set_callback(subreq
, smb2cli_read_done
, req
);
86 state
->subreq
= subreq
;
87 tevent_req_set_cancel_fn(req
, smb2cli_read_cancel
);
92 static bool smb2cli_read_cancel(struct tevent_req
*req
)
94 struct smb2cli_read_state
*state
= tevent_req_data(
95 req
, struct smb2cli_read_state
);
98 ok
= tevent_req_cancel(state
->subreq
);
102 void smb2cli_read_set_notify_async(struct tevent_req
*req
)
104 struct smb2cli_read_state
*state
=
106 struct smb2cli_read_state
);
108 smb2cli_req_set_notify_async(state
->subreq
);
109 state
->notify_async
= true;
112 static void smb2cli_read_done(struct tevent_req
*subreq
)
114 struct tevent_req
*req
= tevent_req_callback_data(
115 subreq
, struct tevent_req
);
116 struct smb2cli_read_state
*state
=
118 struct smb2cli_read_state
);
122 const uint8_t dyn_ofs
= SMB2_HDR_BODY
+ 0x10;
123 DATA_BLOB dyn_buffer
= data_blob_null
;
125 DATA_BLOB data_buffer
= data_blob_null
;
126 uint32_t next_offset
= 0; /* this variable is completely ignored */
127 static const struct smb2cli_req_expected_response expected
[] = {
129 .status
= STATUS_BUFFER_OVERFLOW
,
133 .status
= NT_STATUS_OK
,
138 SMB_ASSERT(state
->subreq
== subreq
);
140 status
= smb2cli_req_recv(subreq
, state
, &iov
,
141 expected
, ARRAY_SIZE(expected
));
142 if (NT_STATUS_EQUAL(status
, NT_STATUS_PENDING
) && state
->notify_async
) {
143 state
->notify_async
= false;
144 state
->report_pending
= true;
145 tevent_req_notify_callback(req
);
148 state
->notify_async
= false;
149 state
->report_pending
= false;
150 state
->subreq
= NULL
;
152 if (NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
155 if (tevent_req_nterror(req
, status
)) {
160 data_offset
= CVAL(iov
[1].iov_base
, 2);
161 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
163 dyn_buffer
= data_blob_const((uint8_t *)iov
[2].iov_base
,
166 error
= smb2cli_parse_dyn_buffer(dyn_ofs
,
168 dyn_ofs
, /* min_offset */
171 dyn_buffer
.length
, /* max_length */
174 if (tevent_req_nterror(req
, error
)) {
178 state
->recv_iov
= iov
;
179 state
->data
= data_buffer
.data
;
181 state
->out_valid
= true;
183 if (tevent_req_nterror(req
, status
)) {
187 tevent_req_done(req
);
190 NTSTATUS
smb2cli_read_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
191 uint8_t **data
, uint32_t *data_length
)
193 struct smb2cli_read_state
*state
=
195 struct smb2cli_read_state
);
196 NTSTATUS status
= NT_STATUS_OK
;
198 if (state
->report_pending
) {
201 return NT_STATUS_PENDING
;
204 if (tevent_req_is_nterror(req
, &status
) && !state
->out_valid
) {
207 tevent_req_received(req
);
210 talloc_steal(mem_ctx
, state
->recv_iov
);
211 *data_length
= state
->data_length
;
213 tevent_req_received(req
);
217 NTSTATUS
smb2cli_read(struct smbXcli_conn
*conn
,
218 uint32_t timeout_msec
,
219 struct smbXcli_session
*session
,
220 struct smbXcli_tcon
*tcon
,
223 uint64_t fid_persistent
,
224 uint64_t fid_volatile
,
225 uint64_t minimum_count
,
226 uint64_t remaining_bytes
,
229 uint32_t *data_length
)
231 TALLOC_CTX
*frame
= talloc_stackframe();
232 struct tevent_context
*ev
;
233 struct tevent_req
*req
;
234 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
236 if (smbXcli_conn_has_async_calls(conn
)) {
238 * Can't use sync call while an async call is in flight
240 status
= NT_STATUS_INVALID_PARAMETER
;
243 ev
= samba_tevent_context_init(frame
);
247 req
= smb2cli_read_send(frame
, ev
,
248 conn
, timeout_msec
, session
, tcon
,
250 fid_persistent
, fid_volatile
,
251 minimum_count
, remaining_bytes
);
255 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
258 status
= smb2cli_read_recv(req
, mem_ctx
, data
, data_length
);