s4-torture: smb2 copychunk data integrity checks
[Samba/gbeck.git] / source3 / libsmb / clireadwrite.c
blobe8c901755d22b2e75048599713a08dc3ff63e8d3
1 /*
2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
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 "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
24 #include "trans2.h"
26 /****************************************************************************
27 Calculate the recommended read buffer size
28 ****************************************************************************/
29 static size_t cli_read_max_bufsize(struct cli_state *cli)
31 uint8_t wct = 12;
32 uint32_t min_space;
33 uint32_t data_offset;
34 uint32_t useable_space = 0;
36 data_offset = HDR_VWV;
37 data_offset += wct * sizeof(uint16_t);
38 data_offset += sizeof(uint16_t); /* byte count */
39 data_offset += 1; /* pad */
41 min_space = cli_state_available_size(cli, data_offset);
43 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
44 useable_space = 0xFFFFFF - data_offset;
46 if (client_is_signing_on(cli)) {
47 return min_space;
50 if (cli_state_encryption_on(cli)) {
51 return min_space;
54 return useable_space;
55 } else if (cli_state_capabilities(cli) & CAP_LARGE_READX) {
57 * Note: CAP_LARGE_READX also works with signing
59 useable_space = 0x1FFFF - data_offset;
61 useable_space = MIN(useable_space, UINT16_MAX);
63 return useable_space;
66 return min_space;
69 /****************************************************************************
70 Calculate the recommended write buffer size
71 ****************************************************************************/
72 static size_t cli_write_max_bufsize(struct cli_state *cli,
73 uint16_t write_mode,
74 uint8_t wct)
76 uint32_t min_space;
77 uint32_t data_offset;
78 uint32_t useable_space = 0;
80 data_offset = HDR_VWV;
81 data_offset += wct * sizeof(uint16_t);
82 data_offset += sizeof(uint16_t); /* byte count */
83 data_offset += 1; /* pad */
85 min_space = cli_state_available_size(cli, data_offset);
87 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) {
88 useable_space = 0xFFFFFF - data_offset;
89 } else if (cli_state_capabilities(cli) & CAP_LARGE_WRITEX) {
90 useable_space = 0x1FFFF - data_offset;
91 } else {
92 return min_space;
95 if (write_mode != 0) {
96 return min_space;
99 if (client_is_signing_on(cli)) {
100 return min_space;
103 if (cli_state_encryption_on(cli)) {
104 return min_space;
107 if (strequal(cli->dev, "LPT1:")) {
108 return min_space;
111 return useable_space;
114 struct cli_read_andx_state {
115 size_t size;
116 uint16_t vwv[12];
117 NTSTATUS status;
118 size_t received;
119 uint8_t *buf;
122 static void cli_read_andx_done(struct tevent_req *subreq);
124 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
125 struct event_context *ev,
126 struct cli_state *cli, uint16_t fnum,
127 off_t offset, size_t size,
128 struct tevent_req **psmbreq)
130 struct tevent_req *req, *subreq;
131 struct cli_read_andx_state *state;
132 uint8_t wct = 10;
134 if (size > cli_read_max_bufsize(cli)) {
135 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
136 "size=%d\n", (int)size,
137 (int)cli_read_max_bufsize(cli)));
138 return NULL;
141 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
142 if (req == NULL) {
143 return NULL;
145 state->size = size;
147 SCVAL(state->vwv + 0, 0, 0xFF);
148 SCVAL(state->vwv + 0, 1, 0);
149 SSVAL(state->vwv + 1, 0, 0);
150 SSVAL(state->vwv + 2, 0, fnum);
151 SIVAL(state->vwv + 3, 0, offset);
152 SSVAL(state->vwv + 5, 0, size);
153 SSVAL(state->vwv + 6, 0, size);
154 SSVAL(state->vwv + 7, 0, (size >> 16));
155 SSVAL(state->vwv + 8, 0, 0);
156 SSVAL(state->vwv + 9, 0, 0);
158 if (cli_state_capabilities(cli) & CAP_LARGE_FILES) {
159 SIVAL(state->vwv + 10, 0,
160 (((uint64_t)offset)>>32) & 0xffffffff);
161 wct = 12;
162 } else {
163 if ((((uint64_t)offset) & 0xffffffff00000000LL) != 0) {
164 DEBUG(10, ("cli_read_andx_send got large offset where "
165 "the server does not support it\n"));
166 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
167 return tevent_req_post(req, ev);
171 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
172 state->vwv, 0, NULL);
173 if (subreq == NULL) {
174 TALLOC_FREE(req);
175 return NULL;
177 tevent_req_set_callback(subreq, cli_read_andx_done, req);
178 *psmbreq = subreq;
179 return req;
182 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
183 struct event_context *ev,
184 struct cli_state *cli, uint16_t fnum,
185 off_t offset, size_t size)
187 struct tevent_req *req, *subreq;
188 NTSTATUS status;
190 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
191 &subreq);
192 if (req == NULL) {
193 return NULL;
196 status = cli_smb_req_send(subreq);
197 if (tevent_req_nterror(req, status)) {
198 return tevent_req_post(req, ev);
200 return req;
203 static void cli_read_andx_done(struct tevent_req *subreq)
205 struct tevent_req *req = tevent_req_callback_data(
206 subreq, struct tevent_req);
207 struct cli_read_andx_state *state = tevent_req_data(
208 req, struct cli_read_andx_state);
209 uint8_t *inbuf;
210 uint8_t wct;
211 uint16_t *vwv;
212 uint32_t num_bytes;
213 uint8_t *bytes;
215 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
216 &num_bytes, &bytes);
217 TALLOC_FREE(subreq);
218 if (NT_STATUS_IS_ERR(state->status)) {
219 tevent_req_nterror(req, state->status);
220 return;
223 /* size is the number of bytes the server returned.
224 * Might be zero. */
225 state->received = SVAL(vwv + 5, 0);
226 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
228 if (state->received > state->size) {
229 DEBUG(5,("server returned more than we wanted!\n"));
230 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
231 return;
235 * bcc field must be valid for small reads, for large reads the 16-bit
236 * bcc field can't be correct.
239 if ((state->received < 0xffff) && (state->received > num_bytes)) {
240 DEBUG(5, ("server announced more bytes than sent\n"));
241 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
242 return;
245 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
247 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
248 || ((state->received != 0) && (state->buf < bytes))) {
249 DEBUG(5, ("server returned invalid read&x data offset\n"));
250 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
251 return;
253 tevent_req_done(req);
257 * Pull the data out of a finished async read_and_x request. rcvbuf is
258 * talloced from the request, so better make sure that you copy it away before
259 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
260 * talloc_move it!
263 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
264 uint8_t **rcvbuf)
266 struct cli_read_andx_state *state = tevent_req_data(
267 req, struct cli_read_andx_state);
268 NTSTATUS status;
270 if (tevent_req_is_nterror(req, &status)) {
271 return status;
273 *received = state->received;
274 *rcvbuf = state->buf;
275 return NT_STATUS_OK;
278 struct cli_readall_state {
279 struct tevent_context *ev;
280 struct cli_state *cli;
281 uint16_t fnum;
282 off_t start_offset;
283 size_t size;
284 size_t received;
285 uint8_t *buf;
288 static void cli_readall_done(struct tevent_req *subreq);
290 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
291 struct event_context *ev,
292 struct cli_state *cli,
293 uint16_t fnum,
294 off_t offset, size_t size)
296 struct tevent_req *req, *subreq;
297 struct cli_readall_state *state;
299 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
300 if (req == NULL) {
301 return NULL;
303 state->ev = ev;
304 state->cli = cli;
305 state->fnum = fnum;
306 state->start_offset = offset;
307 state->size = size;
308 state->received = 0;
309 state->buf = NULL;
311 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
312 if (tevent_req_nomem(subreq, req)) {
313 return tevent_req_post(req, ev);
315 tevent_req_set_callback(subreq, cli_readall_done, req);
316 return req;
319 static void cli_readall_done(struct tevent_req *subreq)
321 struct tevent_req *req = tevent_req_callback_data(
322 subreq, struct tevent_req);
323 struct cli_readall_state *state = tevent_req_data(
324 req, struct cli_readall_state);
325 ssize_t received;
326 uint8_t *buf;
327 NTSTATUS status;
329 status = cli_read_andx_recv(subreq, &received, &buf);
330 if (tevent_req_nterror(req, status)) {
331 return;
334 if (received == 0) {
335 /* EOF */
336 tevent_req_done(req);
337 return;
340 if ((state->received == 0) && (received == state->size)) {
341 /* Ideal case: Got it all in one run */
342 state->buf = buf;
343 state->received += received;
344 tevent_req_done(req);
345 return;
349 * We got a short read, issue a read for the
350 * rest. Unfortunately we have to allocate the buffer
351 * ourselves now, as our caller expects to receive a single
352 * buffer. cli_read_andx does it from the buffer received from
353 * the net, but with a short read we have to put it together
354 * from several reads.
357 if (state->buf == NULL) {
358 state->buf = talloc_array(state, uint8_t, state->size);
359 if (tevent_req_nomem(state->buf, req)) {
360 return;
363 memcpy(state->buf + state->received, buf, received);
364 state->received += received;
366 TALLOC_FREE(subreq);
368 if (state->received >= state->size) {
369 tevent_req_done(req);
370 return;
373 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
374 state->start_offset + state->received,
375 state->size - state->received);
376 if (tevent_req_nomem(subreq, req)) {
377 return;
379 tevent_req_set_callback(subreq, cli_readall_done, req);
382 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
383 uint8_t **rcvbuf)
385 struct cli_readall_state *state = tevent_req_data(
386 req, struct cli_readall_state);
387 NTSTATUS status;
389 if (tevent_req_is_nterror(req, &status)) {
390 return status;
392 *received = state->received;
393 *rcvbuf = state->buf;
394 return NT_STATUS_OK;
397 struct cli_pull_subreq {
398 struct tevent_req *req;
399 ssize_t received;
400 uint8_t *buf;
404 * Parallel read support.
406 * cli_pull sends as many read&x requests as the server would allow via
407 * max_mux at a time. When replies flow back in, the data is written into
408 * the callback function "sink" in the right order.
411 struct cli_pull_state {
412 struct tevent_req *req;
414 struct event_context *ev;
415 struct cli_state *cli;
416 uint16_t fnum;
417 off_t start_offset;
418 SMB_OFF_T size;
420 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
421 void *priv;
423 size_t chunk_size;
426 * Outstanding requests
428 uint16_t max_reqs;
429 int num_reqs;
430 struct cli_pull_subreq *reqs;
433 * For how many bytes did we send requests already?
435 SMB_OFF_T requested;
438 * Next request index to push into "sink". This walks around the "req"
439 * array, taking care that the requests are pushed to "sink" in the
440 * right order. If necessary (i.e. replies don't come in in the right
441 * order), replies are held back in "reqs".
443 int top_req;
446 * How many bytes did we push into "sink"?
449 SMB_OFF_T pushed;
452 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
454 struct cli_pull_state *state = tevent_req_data(
455 req, struct cli_pull_state);
456 char *result;
458 result = tevent_req_default_print(req, mem_ctx);
459 if (result == NULL) {
460 return NULL;
463 return talloc_asprintf_append_buffer(
464 result, "num_reqs=%d, top_req=%d",
465 state->num_reqs, state->top_req);
468 static void cli_pull_read_done(struct tevent_req *read_req);
471 * Prepare an async pull request
474 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
475 struct event_context *ev,
476 struct cli_state *cli,
477 uint16_t fnum, off_t start_offset,
478 SMB_OFF_T size, size_t window_size,
479 NTSTATUS (*sink)(char *buf, size_t n,
480 void *priv),
481 void *priv)
483 struct tevent_req *req;
484 struct cli_pull_state *state;
485 int i;
486 size_t page_size = 1024;
488 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
489 if (req == NULL) {
490 return NULL;
492 tevent_req_set_print_fn(req, cli_pull_print);
493 state->req = req;
495 state->cli = cli;
496 state->ev = ev;
497 state->fnum = fnum;
498 state->start_offset = start_offset;
499 state->size = size;
500 state->sink = sink;
501 state->priv = priv;
503 state->pushed = 0;
504 state->top_req = 0;
506 if (size == 0) {
507 tevent_req_done(req);
508 return tevent_req_post(req, ev);
511 state->chunk_size = cli_read_max_bufsize(cli);
512 if (state->chunk_size > page_size) {
513 state->chunk_size &= ~(page_size - 1);
516 state->max_reqs = cli_state_max_requests(cli);
518 state->num_reqs = MAX(window_size/state->chunk_size, 1);
519 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
521 state->reqs = talloc_zero_array(state, struct cli_pull_subreq,
522 state->num_reqs);
523 if (state->reqs == NULL) {
524 goto failed;
527 state->requested = 0;
529 for (i=0; i<state->num_reqs; i++) {
530 struct cli_pull_subreq *subreq = &state->reqs[i];
531 SMB_OFF_T size_left;
532 size_t request_thistime;
534 if (state->requested >= size) {
535 state->num_reqs = i;
536 break;
539 size_left = size - state->requested;
540 request_thistime = MIN(size_left, state->chunk_size);
542 subreq->req = cli_readall_send(
543 state->reqs, ev, cli, fnum,
544 state->start_offset + state->requested,
545 request_thistime);
547 if (subreq->req == NULL) {
548 goto failed;
550 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
551 state->requested += request_thistime;
553 return req;
555 failed:
556 TALLOC_FREE(req);
557 return NULL;
561 * Handle incoming read replies, push the data into sink and send out new
562 * requests if necessary.
565 static void cli_pull_read_done(struct tevent_req *subreq)
567 struct tevent_req *req = tevent_req_callback_data(
568 subreq, struct tevent_req);
569 struct cli_pull_state *state = tevent_req_data(
570 req, struct cli_pull_state);
571 struct cli_pull_subreq *pull_subreq = NULL;
572 NTSTATUS status;
573 int i;
575 for (i = 0; i < state->num_reqs; i++) {
576 pull_subreq = &state->reqs[i];
577 if (subreq == pull_subreq->req) {
578 break;
581 if (i == state->num_reqs) {
582 /* Huh -- received something we did not send?? */
583 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
584 return;
587 status = cli_readall_recv(subreq, &pull_subreq->received,
588 &pull_subreq->buf);
589 if (!NT_STATUS_IS_OK(status)) {
590 tevent_req_nterror(state->req, status);
591 return;
595 * This loop is the one to take care of out-of-order replies. All
596 * pending requests are in state->reqs, state->reqs[top_req] is the
597 * one that is to be pushed next. If however a request later than
598 * top_req is replied to, then we can't push yet. If top_req is
599 * replied to at a later point then, we need to push all the finished
600 * requests.
603 while (state->reqs[state->top_req].req != NULL) {
604 struct cli_pull_subreq *top_subreq;
606 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
607 state->top_req));
609 top_subreq = &state->reqs[state->top_req];
611 if (tevent_req_is_in_progress(top_subreq->req)) {
612 DEBUG(11, ("cli_pull_read_done: top request not yet "
613 "done\n"));
614 return;
617 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
618 "pushed\n", (int)top_subreq->received,
619 (int)state->pushed));
621 status = state->sink((char *)top_subreq->buf,
622 top_subreq->received, state->priv);
623 if (tevent_req_nterror(state->req, status)) {
624 return;
626 state->pushed += top_subreq->received;
628 TALLOC_FREE(state->reqs[state->top_req].req);
630 if (state->requested < state->size) {
631 struct tevent_req *new_req;
632 SMB_OFF_T size_left;
633 size_t request_thistime;
635 size_left = state->size - state->requested;
636 request_thistime = MIN(size_left, state->chunk_size);
638 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
639 "at %d, position %d\n",
640 (int)request_thistime,
641 (int)(state->start_offset
642 + state->requested),
643 state->top_req));
645 new_req = cli_readall_send(
646 state->reqs, state->ev, state->cli,
647 state->fnum,
648 state->start_offset + state->requested,
649 request_thistime);
651 if (tevent_req_nomem(new_req, state->req)) {
652 return;
654 tevent_req_set_callback(new_req, cli_pull_read_done,
655 req);
657 state->reqs[state->top_req].req = new_req;
658 state->requested += request_thistime;
661 state->top_req = (state->top_req+1) % state->num_reqs;
664 tevent_req_done(req);
667 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
669 struct cli_pull_state *state = tevent_req_data(
670 req, struct cli_pull_state);
671 NTSTATUS status;
673 if (tevent_req_is_nterror(req, &status)) {
674 return status;
676 *received = state->pushed;
677 return NT_STATUS_OK;
680 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
681 off_t start_offset, SMB_OFF_T size, size_t window_size,
682 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
683 void *priv, SMB_OFF_T *received)
685 TALLOC_CTX *frame = talloc_stackframe();
686 struct event_context *ev;
687 struct tevent_req *req;
688 NTSTATUS status = NT_STATUS_OK;
690 if (cli_has_async_calls(cli)) {
692 * Can't use sync call while an async call is in flight
694 status = NT_STATUS_INVALID_PARAMETER;
695 goto fail;
698 ev = event_context_init(frame);
699 if (ev == NULL) {
700 status = NT_STATUS_NO_MEMORY;
701 goto fail;
704 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
705 window_size, sink, priv);
706 if (req == NULL) {
707 status = NT_STATUS_NO_MEMORY;
708 goto fail;
711 if (!tevent_req_poll(req, ev)) {
712 status = map_nt_error_from_unix(errno);
713 goto fail;
716 status = cli_pull_recv(req, received);
717 fail:
718 TALLOC_FREE(frame);
719 return status;
722 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
724 char **pbuf = (char **)priv;
725 memcpy(*pbuf, buf, n);
726 *pbuf += n;
727 return NT_STATUS_OK;
730 NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
731 char *buf, off_t offset, size_t size,
732 size_t *nread)
734 NTSTATUS status;
735 SMB_OFF_T ret;
737 status = cli_pull(cli, fnum, offset, size, size,
738 cli_read_sink, &buf, &ret);
739 if (!NT_STATUS_IS_OK(status)) {
740 return status;
743 if (nread) {
744 *nread = ret;
747 return NT_STATUS_OK;
750 /****************************************************************************
751 write to a file using a SMBwrite and not bypassing 0 byte writes
752 ****************************************************************************/
754 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
755 off_t offset, size_t size1, size_t *ptotal)
757 uint8_t *bytes;
758 ssize_t total = 0;
761 * 3 bytes prefix
764 bytes = talloc_array(talloc_tos(), uint8_t, 3);
765 if (bytes == NULL) {
766 return NT_STATUS_NO_MEMORY;
768 bytes[0] = 1;
770 do {
771 uint32_t usable_space = cli_state_available_size(cli, 48);
772 size_t size = MIN(size1, usable_space);
773 struct tevent_req *req;
774 uint16_t vwv[5];
775 uint16_t *ret_vwv;
776 NTSTATUS status;
778 SSVAL(vwv+0, 0, fnum);
779 SSVAL(vwv+1, 0, size);
780 SIVAL(vwv+2, 0, offset);
781 SSVAL(vwv+4, 0, 0);
783 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
784 size+3);
785 if (bytes == NULL) {
786 return NT_STATUS_NO_MEMORY;
788 SSVAL(bytes, 1, size);
789 memcpy(bytes + 3, buf + total, size);
791 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
792 size+3, bytes, &req, 1, NULL, &ret_vwv,
793 NULL, NULL);
794 if (!NT_STATUS_IS_OK(status)) {
795 TALLOC_FREE(bytes);
796 return status;
799 size = SVAL(ret_vwv+0, 0);
800 TALLOC_FREE(req);
801 if (size == 0) {
802 break;
804 size1 -= size;
805 total += size;
806 offset += size;
808 } while (size1);
810 TALLOC_FREE(bytes);
812 if (ptotal != NULL) {
813 *ptotal = total;
815 return NT_STATUS_OK;
819 * Send a write&x request
822 struct cli_write_andx_state {
823 size_t size;
824 uint16_t vwv[14];
825 size_t written;
826 uint8_t pad;
827 struct iovec iov[2];
830 static void cli_write_andx_done(struct tevent_req *subreq);
832 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
833 struct event_context *ev,
834 struct cli_state *cli, uint16_t fnum,
835 uint16_t mode, const uint8_t *buf,
836 off_t offset, size_t size,
837 struct tevent_req **reqs_before,
838 int num_reqs_before,
839 struct tevent_req **psmbreq)
841 struct tevent_req *req, *subreq;
842 struct cli_write_andx_state *state;
843 bool bigoffset = ((cli_state_capabilities(cli) & CAP_LARGE_FILES) != 0);
844 uint8_t wct = bigoffset ? 14 : 12;
845 size_t max_write = cli_write_max_bufsize(cli, mode, wct);
846 uint16_t *vwv;
848 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
849 if (req == NULL) {
850 return NULL;
853 size = MIN(size, max_write);
855 vwv = state->vwv;
857 SCVAL(vwv+0, 0, 0xFF);
858 SCVAL(vwv+0, 1, 0);
859 SSVAL(vwv+1, 0, 0);
860 SSVAL(vwv+2, 0, fnum);
861 SIVAL(vwv+3, 0, offset);
862 SIVAL(vwv+5, 0, 0);
863 SSVAL(vwv+7, 0, mode);
864 SSVAL(vwv+8, 0, 0);
865 SSVAL(vwv+9, 0, (size>>16));
866 SSVAL(vwv+10, 0, size);
868 SSVAL(vwv+11, 0,
869 cli_smb_wct_ofs(reqs_before, num_reqs_before)
870 + 1 /* the wct field */
871 + wct * 2 /* vwv */
872 + 2 /* num_bytes field */
873 + 1 /* pad */);
875 if (bigoffset) {
876 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
879 state->pad = 0;
880 state->iov[0].iov_base = (void *)&state->pad;
881 state->iov[0].iov_len = 1;
882 state->iov[1].iov_base = discard_const_p(void, buf);
883 state->iov[1].iov_len = size;
885 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
886 2, state->iov);
887 if (tevent_req_nomem(subreq, req)) {
888 return tevent_req_post(req, ev);
890 tevent_req_set_callback(subreq, cli_write_andx_done, req);
891 *psmbreq = subreq;
892 return req;
895 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
896 struct event_context *ev,
897 struct cli_state *cli, uint16_t fnum,
898 uint16_t mode, const uint8_t *buf,
899 off_t offset, size_t size)
901 struct tevent_req *req, *subreq;
902 NTSTATUS status;
904 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
905 size, NULL, 0, &subreq);
906 if (req == NULL) {
907 return NULL;
910 status = cli_smb_req_send(subreq);
911 if (tevent_req_nterror(req, status)) {
912 return tevent_req_post(req, ev);
914 return req;
917 static void cli_write_andx_done(struct tevent_req *subreq)
919 struct tevent_req *req = tevent_req_callback_data(
920 subreq, struct tevent_req);
921 struct cli_write_andx_state *state = tevent_req_data(
922 req, struct cli_write_andx_state);
923 uint8_t wct;
924 uint16_t *vwv;
925 uint8_t *inbuf;
926 NTSTATUS status;
928 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
929 NULL, NULL);
930 TALLOC_FREE(subreq);
931 if (NT_STATUS_IS_ERR(status)) {
932 tevent_req_nterror(req, status);
933 return;
935 state->written = SVAL(vwv+2, 0);
936 state->written |= SVAL(vwv+4, 0)<<16;
937 tevent_req_done(req);
940 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
942 struct cli_write_andx_state *state = tevent_req_data(
943 req, struct cli_write_andx_state);
944 NTSTATUS status;
946 if (tevent_req_is_nterror(req, &status)) {
947 return status;
949 if (pwritten != 0) {
950 *pwritten = state->written;
952 return NT_STATUS_OK;
955 struct cli_writeall_state {
956 struct event_context *ev;
957 struct cli_state *cli;
958 uint16_t fnum;
959 uint16_t mode;
960 const uint8_t *buf;
961 off_t offset;
962 size_t size;
963 size_t written;
966 static void cli_writeall_written(struct tevent_req *req);
968 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
969 struct event_context *ev,
970 struct cli_state *cli,
971 uint16_t fnum,
972 uint16_t mode,
973 const uint8_t *buf,
974 off_t offset, size_t size)
976 struct tevent_req *req, *subreq;
977 struct cli_writeall_state *state;
979 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
980 if (req == NULL) {
981 return NULL;
983 state->ev = ev;
984 state->cli = cli;
985 state->fnum = fnum;
986 state->mode = mode;
987 state->buf = buf;
988 state->offset = offset;
989 state->size = size;
990 state->written = 0;
992 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
993 state->mode, state->buf, state->offset,
994 state->size);
995 if (tevent_req_nomem(subreq, req)) {
996 return tevent_req_post(req, ev);
998 tevent_req_set_callback(subreq, cli_writeall_written, req);
999 return req;
1002 static void cli_writeall_written(struct tevent_req *subreq)
1004 struct tevent_req *req = tevent_req_callback_data(
1005 subreq, struct tevent_req);
1006 struct cli_writeall_state *state = tevent_req_data(
1007 req, struct cli_writeall_state);
1008 NTSTATUS status;
1009 size_t written, to_write;
1011 status = cli_write_andx_recv(subreq, &written);
1012 TALLOC_FREE(subreq);
1013 if (tevent_req_nterror(req, status)) {
1014 return;
1017 state->written += written;
1019 if (state->written > state->size) {
1020 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1021 return;
1024 to_write = state->size - state->written;
1026 if (to_write == 0) {
1027 tevent_req_done(req);
1028 return;
1031 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1032 state->mode,
1033 state->buf + state->written,
1034 state->offset + state->written, to_write);
1035 if (tevent_req_nomem(subreq, req)) {
1036 return;
1038 tevent_req_set_callback(subreq, cli_writeall_written, req);
1041 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
1042 size_t *pwritten)
1044 struct cli_writeall_state *state = tevent_req_data(
1045 req, struct cli_writeall_state);
1046 NTSTATUS status;
1048 if (tevent_req_is_nterror(req, &status)) {
1049 return status;
1051 if (pwritten != NULL) {
1052 *pwritten = state->written;
1054 return NT_STATUS_OK;
1057 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1058 const uint8_t *buf, off_t offset, size_t size,
1059 size_t *pwritten)
1061 TALLOC_CTX *frame = talloc_stackframe();
1062 struct event_context *ev;
1063 struct tevent_req *req;
1064 NTSTATUS status = NT_STATUS_NO_MEMORY;
1066 if (cli_has_async_calls(cli)) {
1068 * Can't use sync call while an async call is in flight
1070 status = NT_STATUS_INVALID_PARAMETER;
1071 goto fail;
1073 ev = event_context_init(frame);
1074 if (ev == NULL) {
1075 goto fail;
1077 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1078 if (req == NULL) {
1079 goto fail;
1081 if (!tevent_req_poll(req, ev)) {
1082 status = map_nt_error_from_unix(errno);
1083 goto fail;
1085 status = cli_writeall_recv(req, pwritten);
1086 fail:
1087 TALLOC_FREE(frame);
1088 return status;
1091 struct cli_push_write_state {
1092 struct tevent_req *req;/* This is the main request! Not the subreq */
1093 uint32_t idx;
1094 off_t ofs;
1095 uint8_t *buf;
1096 size_t size;
1099 struct cli_push_state {
1100 struct event_context *ev;
1101 struct cli_state *cli;
1102 uint16_t fnum;
1103 uint16_t mode;
1104 off_t start_offset;
1105 size_t window_size;
1107 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1108 void *priv;
1110 bool eof;
1112 size_t chunk_size;
1113 off_t next_offset;
1116 * Outstanding requests
1118 uint32_t pending;
1119 uint16_t max_reqs;
1120 uint32_t num_reqs;
1121 struct cli_push_write_state **reqs;
1124 static void cli_push_written(struct tevent_req *req);
1126 static bool cli_push_write_setup(struct tevent_req *req,
1127 struct cli_push_state *state,
1128 uint32_t idx)
1130 struct cli_push_write_state *substate;
1131 struct tevent_req *subreq;
1133 substate = talloc(state->reqs, struct cli_push_write_state);
1134 if (!substate) {
1135 return false;
1137 substate->req = req;
1138 substate->idx = idx;
1139 substate->ofs = state->next_offset;
1140 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1141 if (!substate->buf) {
1142 talloc_free(substate);
1143 return false;
1145 substate->size = state->source(substate->buf,
1146 state->chunk_size,
1147 state->priv);
1148 if (substate->size == 0) {
1149 state->eof = true;
1150 /* nothing to send */
1151 talloc_free(substate);
1152 return true;
1155 subreq = cli_writeall_send(substate,
1156 state->ev, state->cli,
1157 state->fnum, state->mode,
1158 substate->buf,
1159 substate->ofs,
1160 substate->size);
1161 if (!subreq) {
1162 talloc_free(substate);
1163 return false;
1165 tevent_req_set_callback(subreq, cli_push_written, substate);
1167 state->reqs[idx] = substate;
1168 state->pending += 1;
1169 state->next_offset += substate->size;
1171 return true;
1174 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1175 struct cli_state *cli,
1176 uint16_t fnum, uint16_t mode,
1177 off_t start_offset, size_t window_size,
1178 size_t (*source)(uint8_t *buf, size_t n,
1179 void *priv),
1180 void *priv)
1182 struct tevent_req *req;
1183 struct cli_push_state *state;
1184 uint32_t i;
1185 size_t page_size = 1024;
1187 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1188 if (req == NULL) {
1189 return NULL;
1191 state->cli = cli;
1192 state->ev = ev;
1193 state->fnum = fnum;
1194 state->start_offset = start_offset;
1195 state->mode = mode;
1196 state->source = source;
1197 state->priv = priv;
1198 state->eof = false;
1199 state->pending = 0;
1200 state->next_offset = start_offset;
1202 state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
1203 if (state->chunk_size > page_size) {
1204 state->chunk_size &= ~(page_size - 1);
1207 state->max_reqs = cli_state_max_requests(cli);
1209 if (window_size == 0) {
1210 window_size = state->max_reqs * state->chunk_size;
1212 state->num_reqs = window_size/state->chunk_size;
1213 if ((window_size % state->chunk_size) > 0) {
1214 state->num_reqs += 1;
1216 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
1217 state->num_reqs = MAX(state->num_reqs, 1);
1219 state->reqs = talloc_zero_array(state, struct cli_push_write_state *,
1220 state->num_reqs);
1221 if (state->reqs == NULL) {
1222 goto failed;
1225 for (i=0; i<state->num_reqs; i++) {
1226 if (!cli_push_write_setup(req, state, i)) {
1227 goto failed;
1230 if (state->eof) {
1231 break;
1235 if (state->pending == 0) {
1236 tevent_req_done(req);
1237 return tevent_req_post(req, ev);
1240 return req;
1242 failed:
1243 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1244 return tevent_req_post(req, ev);
1247 static void cli_push_written(struct tevent_req *subreq)
1249 struct cli_push_write_state *substate = tevent_req_callback_data(
1250 subreq, struct cli_push_write_state);
1251 struct tevent_req *req = substate->req;
1252 struct cli_push_state *state = tevent_req_data(
1253 req, struct cli_push_state);
1254 NTSTATUS status;
1255 uint32_t idx = substate->idx;
1257 state->reqs[idx] = NULL;
1258 state->pending -= 1;
1260 status = cli_writeall_recv(subreq, NULL);
1261 TALLOC_FREE(subreq);
1262 TALLOC_FREE(substate);
1263 if (tevent_req_nterror(req, status)) {
1264 return;
1267 if (!state->eof) {
1268 if (!cli_push_write_setup(req, state, idx)) {
1269 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1270 return;
1274 if (state->pending == 0) {
1275 tevent_req_done(req);
1276 return;
1280 NTSTATUS cli_push_recv(struct tevent_req *req)
1282 return tevent_req_simple_recv_ntstatus(req);
1285 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1286 off_t start_offset, size_t window_size,
1287 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1288 void *priv)
1290 TALLOC_CTX *frame = talloc_stackframe();
1291 struct event_context *ev;
1292 struct tevent_req *req;
1293 NTSTATUS status = NT_STATUS_OK;
1295 if (cli_has_async_calls(cli)) {
1297 * Can't use sync call while an async call is in flight
1299 status = NT_STATUS_INVALID_PARAMETER;
1300 goto fail;
1303 ev = event_context_init(frame);
1304 if (ev == NULL) {
1305 status = NT_STATUS_NO_MEMORY;
1306 goto fail;
1309 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1310 window_size, source, priv);
1311 if (req == NULL) {
1312 status = NT_STATUS_NO_MEMORY;
1313 goto fail;
1316 if (!tevent_req_poll(req, ev)) {
1317 status = map_nt_error_from_unix(errno);
1318 goto fail;
1321 status = cli_push_recv(req);
1322 fail:
1323 TALLOC_FREE(frame);
1324 return status;