s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_read.c
blobc348c9937c5bae02aff31e1e9de8ee3e6dc2d541
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 struct iovec *recv_iov;
31 uint8_t *data;
32 uint32_t data_length;
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,
40 uint32_t length,
41 uint64_t offset,
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;
49 uint8_t *fixed;
51 req = tevent_req_create(mem_ctx, &state,
52 struct smb2cli_read_state);
53 if (req == NULL) {
54 return NULL;
57 fixed = state->fixed;
59 SSVAL(fixed, 0, 49);
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),
69 NULL, 0);
70 if (tevent_req_nomem(subreq, req)) {
71 return tevent_req_post(req, ev);
73 tevent_req_set_callback(subreq, smb2cli_read_done, req);
74 return 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 =
82 tevent_req_data(req,
83 struct smb2cli_read_state);
84 NTSTATUS status;
85 struct iovec *iov;
86 uint8_t data_offset;
88 status = smb2cli_req_recv(subreq, state, &iov, 17);
89 if (tevent_req_nterror(req, status)) {
90 return;
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);
99 return;
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 =
111 tevent_req_data(req,
112 struct smb2cli_read_state);
113 NTSTATUS status;
115 if (tevent_req_is_nterror(req, &status)) {
116 return status;
118 talloc_steal(mem_ctx, state->recv_iov);
119 *data_length = state->data_length;
120 *data = state->data;
121 return NT_STATUS_OK;
124 NTSTATUS smb2cli_read(struct cli_state *cli,
125 uint32_t length,
126 uint64_t offset,
127 uint64_t fid_persistent,
128 uint64_t fid_volatile,
129 uint64_t minimum_count,
130 uint64_t remaining_bytes,
131 TALLOC_CTX *mem_ctx,
132 uint8_t **data,
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;
145 goto fail;
147 ev = event_context_init(frame);
148 if (ev == NULL) {
149 goto fail;
151 req = smb2cli_read_send(frame, ev, cli, length, offset,
152 fid_persistent, fid_volatile,
153 minimum_count, remaining_bytes);
154 if (req == NULL) {
155 goto fail;
157 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
158 goto fail;
160 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
161 fail:
162 TALLOC_FREE(frame);
163 return status;