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
;
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
,
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
;
54 req
= tevent_req_create(mem_ctx
, &state
,
55 struct smb2cli_read_state
);
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
);
71 tcon_id
= smb2cli_tcon_current_id(tcon
);
74 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_READ
,
80 state
->fixed
, sizeof(state
->fixed
),
81 state
->dyn_pad
, sizeof(state
->dyn_pad
));
82 if (tevent_req_nomem(subreq
, req
)) {
83 return tevent_req_post(req
, ev
);
85 tevent_req_set_callback(subreq
, smb2cli_read_done
, req
);
89 static void smb2cli_read_done(struct tevent_req
*subreq
)
91 struct tevent_req
*req
= tevent_req_callback_data(
92 subreq
, struct tevent_req
);
93 struct smb2cli_read_state
*state
=
95 struct smb2cli_read_state
);
99 static const struct smb2cli_req_expected_response expected
[] = {
101 .status
= STATUS_BUFFER_OVERFLOW
,
105 .status
= NT_STATUS_OK
,
110 status
= smb2cli_req_recv(subreq
, state
, &iov
,
111 expected
, ARRAY_SIZE(expected
));
112 if (tevent_req_nterror(req
, status
)) {
116 data_offset
= CVAL(iov
[1].iov_base
, 2);
117 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
119 if ((data_offset
!= SMB2_HDR_BODY
+ 16) ||
120 (state
->data_length
> iov
[2].iov_len
)) {
121 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
125 state
->recv_iov
= iov
;
126 state
->data
= (uint8_t *)iov
[2].iov_base
;
127 tevent_req_done(req
);
130 NTSTATUS
smb2cli_read_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
131 uint8_t **data
, uint32_t *data_length
)
133 struct smb2cli_read_state
*state
=
135 struct smb2cli_read_state
);
138 if (tevent_req_is_nterror(req
, &status
)) {
141 talloc_steal(mem_ctx
, state
->recv_iov
);
142 *data_length
= state
->data_length
;
147 NTSTATUS
smb2cli_read(struct smbXcli_conn
*conn
,
148 uint32_t timeout_msec
,
149 struct smbXcli_session
*session
,
150 struct smbXcli_tcon
*tcon
,
153 uint64_t fid_persistent
,
154 uint64_t fid_volatile
,
155 uint64_t minimum_count
,
156 uint64_t remaining_bytes
,
159 uint32_t *data_length
)
161 TALLOC_CTX
*frame
= talloc_stackframe();
162 struct tevent_context
*ev
;
163 struct tevent_req
*req
;
164 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
166 if (smbXcli_conn_has_async_calls(conn
)) {
168 * Can't use sync call while an async call is in flight
170 status
= NT_STATUS_INVALID_PARAMETER
;
173 ev
= tevent_context_init(frame
);
177 req
= smb2cli_read_send(frame
, ev
,
178 conn
, timeout_msec
, session
, tcon
,
180 fid_persistent
, fid_volatile
,
181 minimum_count
, remaining_bytes
);
185 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
188 status
= smb2cli_read_recv(req
, mem_ctx
, data
, data_length
);