libcli/smb: move smb2cli_close.c from source3 to the toplevel
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_write.c
blobd452ffb8197a9198c458174e6e2d3ca3dd009be9
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 "../libcli/smb/smbXcli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_write_state {
29 uint8_t fixed[48];
30 uint8_t dyn_pad[1];
33 static void smb2cli_write_done(struct tevent_req *subreq);
35 struct tevent_req *smb2cli_write_send(TALLOC_CTX *mem_ctx,
36 struct tevent_context *ev,
37 struct smbXcli_conn *conn,
38 uint32_t timeout_msec,
39 struct smbXcli_session *session,
40 uint32_t tcon_id,
41 uint32_t length,
42 uint64_t offset,
43 uint64_t fid_persistent,
44 uint64_t fid_volatile,
45 uint32_t remaining_bytes,
46 uint32_t flags,
47 const uint8_t *data)
49 struct tevent_req *req, *subreq;
50 struct smb2cli_write_state *state;
51 uint8_t *fixed;
52 const uint8_t *dyn;
53 size_t dyn_len;
55 req = tevent_req_create(mem_ctx, &state,
56 struct smb2cli_write_state);
57 if (req == NULL) {
58 return NULL;
61 fixed = state->fixed;
63 SSVAL(fixed, 0, 49);
64 SSVAL(fixed, 2, SMB2_HDR_BODY + 48);
65 SIVAL(fixed, 4, length);
66 SBVAL(fixed, 8, offset);
67 SBVAL(fixed, 16, fid_persistent);
68 SBVAL(fixed, 24, fid_volatile);
69 SIVAL(fixed, 36, remaining_bytes);
70 SIVAL(fixed, 44, flags);
72 if (length > 0) {
73 dyn = data;
74 dyn_len = length;
75 } else {
76 dyn = state->dyn_pad;;
77 dyn_len = sizeof(state->dyn_pad);
80 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_WRITE,
81 0, 0, /* flags */
82 timeout_msec,
83 0xFEFF, /* pid */
84 tcon_id,
85 session,
86 state->fixed, sizeof(state->fixed),
87 dyn, dyn_len);
88 if (tevent_req_nomem(subreq, req)) {
89 return tevent_req_post(req, ev);
91 tevent_req_set_callback(subreq, smb2cli_write_done, req);
92 return req;
95 static void smb2cli_write_done(struct tevent_req *subreq)
97 struct tevent_req *req =
98 tevent_req_callback_data(subreq,
99 struct tevent_req);
100 NTSTATUS status;
101 static const struct smb2cli_req_expected_response expected[] = {
103 .status = NT_STATUS_OK,
104 .body_size = 0x11
108 status = smb2cli_req_recv(subreq, NULL, NULL,
109 expected, ARRAY_SIZE(expected));
110 if (tevent_req_nterror(req, status)) {
111 return;
113 tevent_req_done(req);
116 NTSTATUS smb2cli_write_recv(struct tevent_req *req)
118 return tevent_req_simple_recv_ntstatus(req);
121 NTSTATUS smb2cli_write(struct smbXcli_conn *conn,
122 uint32_t timeout_msec,
123 struct smbXcli_session *session,
124 uint32_t tcon_id,
125 uint32_t length,
126 uint64_t offset,
127 uint64_t fid_persistent,
128 uint64_t fid_volatile,
129 uint32_t remaining_bytes,
130 uint32_t flags,
131 const uint8_t *data)
133 TALLOC_CTX *frame = talloc_stackframe();
134 struct tevent_context *ev;
135 struct tevent_req *req;
136 NTSTATUS status = NT_STATUS_NO_MEMORY;
138 if (smbXcli_conn_has_async_calls(conn)) {
140 * Can't use sync call while an async call is in flight
142 status = NT_STATUS_INVALID_PARAMETER;
143 goto fail;
145 ev = tevent_context_init(frame);
146 if (ev == NULL) {
147 goto fail;
149 req = smb2cli_write_send(frame, ev, conn, timeout_msec, session,
150 tcon_id, length, offset,
151 fid_persistent, fid_volatile,
152 remaining_bytes, flags, data);
153 if (req == NULL) {
154 goto fail;
156 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
157 goto fail;
159 status = smb2cli_write_recv(req);
160 fail:
161 TALLOC_FREE(frame);
162 return status;