s3:libsmb: s/\<event_context/tevent_context/gc in smb2cli_session.c
[samba.git] / source3 / libsmb / smb2cli_write.c
1 /*
2    Unix SMB/CIFS implementation.
3    smb2 lib
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
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"
27
28 struct smb2cli_write_state {
29         uint8_t fixed[48];
30         uint8_t dyn_pad[1];
31 };
32
33 static void smb2cli_write_done(struct tevent_req *subreq);
34
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)
48 {
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;
54
55         req = tevent_req_create(mem_ctx, &state,
56                                 struct smb2cli_write_state);
57         if (req == NULL) {
58                 return NULL;
59         }
60
61         fixed = state->fixed;
62
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);
71
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);
78         }
79
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);
90         }
91         tevent_req_set_callback(subreq, smb2cli_write_done, req);
92         return req;
93 }
94
95 static void smb2cli_write_done(struct tevent_req *subreq)
96 {
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[] = {
102         {
103                 .status = NT_STATUS_OK,
104                 .body_size = 0x11
105         }
106         };
107
108         status = smb2cli_req_recv(subreq, NULL, NULL,
109                                   expected, ARRAY_SIZE(expected));
110         if (tevent_req_nterror(req, status)) {
111                 return;
112         }
113         tevent_req_done(req);
114 }
115
116 NTSTATUS smb2cli_write_recv(struct tevent_req *req)
117 {
118         return tevent_req_simple_recv_ntstatus(req);
119 }
120
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)
132 {
133         TALLOC_CTX *frame = talloc_stackframe();
134         struct event_context *ev;
135         struct tevent_req *req;
136         NTSTATUS status = NT_STATUS_NO_MEMORY;
137
138         if (smbXcli_conn_has_async_calls(conn)) {
139                 /*
140                  * Can't use sync call while an async call is in flight
141                  */
142                 status = NT_STATUS_INVALID_PARAMETER;
143                 goto fail;
144         }
145         ev = event_context_init(frame);
146         if (ev == NULL) {
147                 goto fail;
148         }
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;
155         }
156         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
157                 goto fail;
158         }
159         status = smb2cli_write_recv(req);
160  fail:
161         TALLOC_FREE(frame);
162         return status;
163 }