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/>.
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_read_state
{
30 struct iovec
*recv_iov
;
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 cli_state
*cli
,
42 uint64_t fid_persistent
,
43 uint64_t fid_volatile
,
44 uint64_t minimum_count
,
45 uint64_t remaining_bytes
)
47 struct tevent_req
*req
, *subreq
;
48 struct smb2cli_read_state
*state
;
51 req
= tevent_req_create(mem_ctx
, &state
,
52 struct smb2cli_read_state
);
60 SIVAL(fixed
, 4, length
);
61 SBVAL(fixed
, 8, offset
);
62 SBVAL(fixed
, 16, fid_persistent
);
63 SBVAL(fixed
, 24, fid_volatile
);
64 SBVAL(fixed
, 32, minimum_count
);
65 SBVAL(fixed
, 40, remaining_bytes
);
67 subreq
= smb2cli_req_send(state
, ev
, cli
, SMB2_OP_READ
, 0,
68 state
->fixed
, sizeof(state
->fixed
),
70 if (tevent_req_nomem(subreq
, req
)) {
71 return tevent_req_post(req
, ev
);
73 tevent_req_set_callback(subreq
, smb2cli_read_done
, req
);
77 static void smb2cli_read_done(struct tevent_req
*subreq
)
79 struct tevent_req
*req
= tevent_req_callback_data(
80 subreq
, struct tevent_req
);
81 struct smb2cli_read_state
*state
=
83 struct smb2cli_read_state
);
88 status
= smb2cli_req_recv(subreq
, state
, &iov
, 17);
89 if (tevent_req_nterror(req
, status
)) {
93 data_offset
= CVAL(iov
[1].iov_base
, 2);
94 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
96 if ((data_offset
!= SMB2_HDR_BODY
+ 16) ||
97 (state
->data_length
> iov
[2].iov_len
)) {
98 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
102 state
->recv_iov
= iov
;
103 state
->data
= (uint8_t *)iov
[2].iov_base
;
104 tevent_req_done(req
);
107 NTSTATUS
smb2cli_read_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
108 uint8_t **data
, uint32_t *data_length
)
110 struct smb2cli_read_state
*state
=
112 struct smb2cli_read_state
);
115 if (tevent_req_is_nterror(req
, &status
)) {
118 talloc_steal(mem_ctx
, state
->recv_iov
);
119 *data_length
= state
->data_length
;
124 NTSTATUS
smb2cli_read(struct cli_state
*cli
,
127 uint64_t fid_persistent
,
128 uint64_t fid_volatile
,
129 uint64_t minimum_count
,
130 uint64_t remaining_bytes
,
133 uint32_t *data_length
)
135 TALLOC_CTX
*frame
= talloc_stackframe();
136 struct event_context
*ev
;
137 struct tevent_req
*req
;
138 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
140 if (cli_has_async_calls(cli
)) {
142 * Can't use sync call while an async call is in flight
144 status
= NT_STATUS_INVALID_PARAMETER
;
147 ev
= event_context_init(frame
);
151 req
= smb2cli_read_send(frame
, ev
, cli
, length
, offset
,
152 fid_persistent
, fid_volatile
,
153 minimum_count
, remaining_bytes
);
157 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
160 status
= smb2cli_read_recv(req
, mem_ctx
, data
, data_length
);