s4:selftest: skip flakey samba4.drs.repl_schema.python for now
[Samba/vl.git] / source3 / libsmb / clireadwrite.c
blob1ee2196b76b3becdb07c746919de3c3f9ade15a9
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 ((uint64_t)offset >> 32) {
159 SIVAL(state->vwv + 10, 0,
160 (((uint64_t)offset)>>32) & 0xffffffff);
161 wct += 2;
164 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
165 state->vwv, 0, NULL);
166 if (subreq == NULL) {
167 TALLOC_FREE(req);
168 return NULL;
170 tevent_req_set_callback(subreq, cli_read_andx_done, req);
171 *psmbreq = subreq;
172 return req;
175 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
176 struct event_context *ev,
177 struct cli_state *cli, uint16_t fnum,
178 off_t offset, size_t size)
180 struct tevent_req *req, *subreq;
181 NTSTATUS status;
183 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
184 &subreq);
185 if (req == NULL) {
186 return NULL;
189 status = cli_smb_req_send(subreq);
190 if (tevent_req_nterror(req, status)) {
191 return tevent_req_post(req, ev);
193 return req;
196 static void cli_read_andx_done(struct tevent_req *subreq)
198 struct tevent_req *req = tevent_req_callback_data(
199 subreq, struct tevent_req);
200 struct cli_read_andx_state *state = tevent_req_data(
201 req, struct cli_read_andx_state);
202 uint8_t *inbuf;
203 uint8_t wct;
204 uint16_t *vwv;
205 uint32_t num_bytes;
206 uint8_t *bytes;
208 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
209 &num_bytes, &bytes);
210 TALLOC_FREE(subreq);
211 if (NT_STATUS_IS_ERR(state->status)) {
212 tevent_req_nterror(req, state->status);
213 return;
216 /* size is the number of bytes the server returned.
217 * Might be zero. */
218 state->received = SVAL(vwv + 5, 0);
219 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
221 if (state->received > state->size) {
222 DEBUG(5,("server returned more than we wanted!\n"));
223 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
224 return;
228 * bcc field must be valid for small reads, for large reads the 16-bit
229 * bcc field can't be correct.
232 if ((state->received < 0xffff) && (state->received > num_bytes)) {
233 DEBUG(5, ("server announced more bytes than sent\n"));
234 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
235 return;
238 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
240 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
241 || ((state->received != 0) && (state->buf < bytes))) {
242 DEBUG(5, ("server returned invalid read&x data offset\n"));
243 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
244 return;
246 tevent_req_done(req);
250 * Pull the data out of a finished async read_and_x request. rcvbuf is
251 * talloced from the request, so better make sure that you copy it away before
252 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
253 * talloc_move it!
256 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
257 uint8_t **rcvbuf)
259 struct cli_read_andx_state *state = tevent_req_data(
260 req, struct cli_read_andx_state);
261 NTSTATUS status;
263 if (tevent_req_is_nterror(req, &status)) {
264 return status;
266 *received = state->received;
267 *rcvbuf = state->buf;
268 return NT_STATUS_OK;
271 struct cli_readall_state {
272 struct tevent_context *ev;
273 struct cli_state *cli;
274 uint16_t fnum;
275 off_t start_offset;
276 size_t size;
277 size_t received;
278 uint8_t *buf;
281 static void cli_readall_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
284 struct event_context *ev,
285 struct cli_state *cli,
286 uint16_t fnum,
287 off_t offset, size_t size)
289 struct tevent_req *req, *subreq;
290 struct cli_readall_state *state;
292 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
293 if (req == NULL) {
294 return NULL;
296 state->ev = ev;
297 state->cli = cli;
298 state->fnum = fnum;
299 state->start_offset = offset;
300 state->size = size;
301 state->received = 0;
302 state->buf = NULL;
304 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
305 if (tevent_req_nomem(subreq, req)) {
306 return tevent_req_post(req, ev);
308 tevent_req_set_callback(subreq, cli_readall_done, req);
309 return req;
312 static void cli_readall_done(struct tevent_req *subreq)
314 struct tevent_req *req = tevent_req_callback_data(
315 subreq, struct tevent_req);
316 struct cli_readall_state *state = tevent_req_data(
317 req, struct cli_readall_state);
318 ssize_t received;
319 uint8_t *buf;
320 NTSTATUS status;
322 status = cli_read_andx_recv(subreq, &received, &buf);
323 if (tevent_req_nterror(req, status)) {
324 return;
327 if (received == 0) {
328 /* EOF */
329 tevent_req_done(req);
330 return;
333 if ((state->received == 0) && (received == state->size)) {
334 /* Ideal case: Got it all in one run */
335 state->buf = buf;
336 state->received += received;
337 tevent_req_done(req);
338 return;
342 * We got a short read, issue a read for the
343 * rest. Unfortunately we have to allocate the buffer
344 * ourselves now, as our caller expects to receive a single
345 * buffer. cli_read_andx does it from the buffer received from
346 * the net, but with a short read we have to put it together
347 * from several reads.
350 if (state->buf == NULL) {
351 state->buf = talloc_array(state, uint8_t, state->size);
352 if (tevent_req_nomem(state->buf, req)) {
353 return;
356 memcpy(state->buf + state->received, buf, received);
357 state->received += received;
359 TALLOC_FREE(subreq);
361 if (state->received >= state->size) {
362 tevent_req_done(req);
363 return;
366 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
367 state->start_offset + state->received,
368 state->size - state->received);
369 if (tevent_req_nomem(subreq, req)) {
370 return;
372 tevent_req_set_callback(subreq, cli_readall_done, req);
375 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
376 uint8_t **rcvbuf)
378 struct cli_readall_state *state = tevent_req_data(
379 req, struct cli_readall_state);
380 NTSTATUS status;
382 if (tevent_req_is_nterror(req, &status)) {
383 return status;
385 *received = state->received;
386 *rcvbuf = state->buf;
387 return NT_STATUS_OK;
390 struct cli_pull_subreq {
391 struct tevent_req *req;
392 ssize_t received;
393 uint8_t *buf;
397 * Parallel read support.
399 * cli_pull sends as many read&x requests as the server would allow via
400 * max_mux at a time. When replies flow back in, the data is written into
401 * the callback function "sink" in the right order.
404 struct cli_pull_state {
405 struct tevent_req *req;
407 struct event_context *ev;
408 struct cli_state *cli;
409 uint16_t fnum;
410 off_t start_offset;
411 SMB_OFF_T size;
413 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
414 void *priv;
416 size_t chunk_size;
419 * Outstanding requests
421 uint16_t max_reqs;
422 int num_reqs;
423 struct cli_pull_subreq *reqs;
426 * For how many bytes did we send requests already?
428 SMB_OFF_T requested;
431 * Next request index to push into "sink". This walks around the "req"
432 * array, taking care that the requests are pushed to "sink" in the
433 * right order. If necessary (i.e. replies don't come in in the right
434 * order), replies are held back in "reqs".
436 int top_req;
439 * How many bytes did we push into "sink"?
442 SMB_OFF_T pushed;
445 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
447 struct cli_pull_state *state = tevent_req_data(
448 req, struct cli_pull_state);
449 char *result;
451 result = tevent_req_default_print(req, mem_ctx);
452 if (result == NULL) {
453 return NULL;
456 return talloc_asprintf_append_buffer(
457 result, "num_reqs=%d, top_req=%d",
458 state->num_reqs, state->top_req);
461 static void cli_pull_read_done(struct tevent_req *read_req);
464 * Prepare an async pull request
467 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
468 struct event_context *ev,
469 struct cli_state *cli,
470 uint16_t fnum, off_t start_offset,
471 SMB_OFF_T size, size_t window_size,
472 NTSTATUS (*sink)(char *buf, size_t n,
473 void *priv),
474 void *priv)
476 struct tevent_req *req;
477 struct cli_pull_state *state;
478 int i;
479 size_t page_size = 1024;
481 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
482 if (req == NULL) {
483 return NULL;
485 tevent_req_set_print_fn(req, cli_pull_print);
486 state->req = req;
488 state->cli = cli;
489 state->ev = ev;
490 state->fnum = fnum;
491 state->start_offset = start_offset;
492 state->size = size;
493 state->sink = sink;
494 state->priv = priv;
496 state->pushed = 0;
497 state->top_req = 0;
499 if (size == 0) {
500 tevent_req_done(req);
501 return tevent_req_post(req, ev);
504 state->chunk_size = cli_read_max_bufsize(cli);
505 if (state->chunk_size > page_size) {
506 state->chunk_size &= ~(page_size - 1);
509 state->max_reqs = cli_state_max_requests(cli);
511 state->num_reqs = MAX(window_size/state->chunk_size, 1);
512 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
514 state->reqs = talloc_zero_array(state, struct cli_pull_subreq,
515 state->num_reqs);
516 if (state->reqs == NULL) {
517 goto failed;
520 state->requested = 0;
522 for (i=0; i<state->num_reqs; i++) {
523 struct cli_pull_subreq *subreq = &state->reqs[i];
524 SMB_OFF_T size_left;
525 size_t request_thistime;
527 if (state->requested >= size) {
528 state->num_reqs = i;
529 break;
532 size_left = size - state->requested;
533 request_thistime = MIN(size_left, state->chunk_size);
535 subreq->req = cli_readall_send(
536 state->reqs, ev, cli, fnum,
537 state->start_offset + state->requested,
538 request_thistime);
540 if (subreq->req == NULL) {
541 goto failed;
543 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
544 state->requested += request_thistime;
546 return req;
548 failed:
549 TALLOC_FREE(req);
550 return NULL;
554 * Handle incoming read replies, push the data into sink and send out new
555 * requests if necessary.
558 static void cli_pull_read_done(struct tevent_req *subreq)
560 struct tevent_req *req = tevent_req_callback_data(
561 subreq, struct tevent_req);
562 struct cli_pull_state *state = tevent_req_data(
563 req, struct cli_pull_state);
564 struct cli_pull_subreq *pull_subreq = NULL;
565 NTSTATUS status;
566 int i;
568 for (i = 0; i < state->num_reqs; i++) {
569 pull_subreq = &state->reqs[i];
570 if (subreq == pull_subreq->req) {
571 break;
574 if (i == state->num_reqs) {
575 /* Huh -- received something we did not send?? */
576 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
577 return;
580 status = cli_readall_recv(subreq, &pull_subreq->received,
581 &pull_subreq->buf);
582 if (!NT_STATUS_IS_OK(status)) {
583 tevent_req_nterror(state->req, status);
584 return;
588 * This loop is the one to take care of out-of-order replies. All
589 * pending requests are in state->reqs, state->reqs[top_req] is the
590 * one that is to be pushed next. If however a request later than
591 * top_req is replied to, then we can't push yet. If top_req is
592 * replied to at a later point then, we need to push all the finished
593 * requests.
596 while (state->reqs[state->top_req].req != NULL) {
597 struct cli_pull_subreq *top_subreq;
599 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
600 state->top_req));
602 top_subreq = &state->reqs[state->top_req];
604 if (tevent_req_is_in_progress(top_subreq->req)) {
605 DEBUG(11, ("cli_pull_read_done: top request not yet "
606 "done\n"));
607 return;
610 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
611 "pushed\n", (int)top_subreq->received,
612 (int)state->pushed));
614 status = state->sink((char *)top_subreq->buf,
615 top_subreq->received, state->priv);
616 if (tevent_req_nterror(state->req, status)) {
617 return;
619 state->pushed += top_subreq->received;
621 TALLOC_FREE(state->reqs[state->top_req].req);
623 if (state->requested < state->size) {
624 struct tevent_req *new_req;
625 SMB_OFF_T size_left;
626 size_t request_thistime;
628 size_left = state->size - state->requested;
629 request_thistime = MIN(size_left, state->chunk_size);
631 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
632 "at %d, position %d\n",
633 (int)request_thistime,
634 (int)(state->start_offset
635 + state->requested),
636 state->top_req));
638 new_req = cli_readall_send(
639 state->reqs, state->ev, state->cli,
640 state->fnum,
641 state->start_offset + state->requested,
642 request_thistime);
644 if (tevent_req_nomem(new_req, state->req)) {
645 return;
647 tevent_req_set_callback(new_req, cli_pull_read_done,
648 req);
650 state->reqs[state->top_req].req = new_req;
651 state->requested += request_thistime;
654 state->top_req = (state->top_req+1) % state->num_reqs;
657 tevent_req_done(req);
660 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
662 struct cli_pull_state *state = tevent_req_data(
663 req, struct cli_pull_state);
664 NTSTATUS status;
666 if (tevent_req_is_nterror(req, &status)) {
667 return status;
669 *received = state->pushed;
670 return NT_STATUS_OK;
673 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
674 off_t start_offset, SMB_OFF_T size, size_t window_size,
675 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
676 void *priv, SMB_OFF_T *received)
678 TALLOC_CTX *frame = talloc_stackframe();
679 struct event_context *ev;
680 struct tevent_req *req;
681 NTSTATUS status = NT_STATUS_OK;
683 if (cli_has_async_calls(cli)) {
685 * Can't use sync call while an async call is in flight
687 status = NT_STATUS_INVALID_PARAMETER;
688 goto fail;
691 ev = event_context_init(frame);
692 if (ev == NULL) {
693 status = NT_STATUS_NO_MEMORY;
694 goto fail;
697 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
698 window_size, sink, priv);
699 if (req == NULL) {
700 status = NT_STATUS_NO_MEMORY;
701 goto fail;
704 if (!tevent_req_poll(req, ev)) {
705 status = map_nt_error_from_unix(errno);
706 goto fail;
709 status = cli_pull_recv(req, received);
710 fail:
711 TALLOC_FREE(frame);
712 return status;
715 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
717 char **pbuf = (char **)priv;
718 memcpy(*pbuf, buf, n);
719 *pbuf += n;
720 return NT_STATUS_OK;
723 NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
724 char *buf, off_t offset, size_t size,
725 size_t *nread)
727 NTSTATUS status;
728 SMB_OFF_T ret;
730 status = cli_pull(cli, fnum, offset, size, size,
731 cli_read_sink, &buf, &ret);
732 if (!NT_STATUS_IS_OK(status)) {
733 return status;
736 if (nread) {
737 *nread = ret;
740 return NT_STATUS_OK;
743 /****************************************************************************
744 write to a file using a SMBwrite and not bypassing 0 byte writes
745 ****************************************************************************/
747 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
748 off_t offset, size_t size1, size_t *ptotal)
750 uint8_t *bytes;
751 ssize_t total = 0;
754 * 3 bytes prefix
757 bytes = talloc_array(talloc_tos(), uint8_t, 3);
758 if (bytes == NULL) {
759 return NT_STATUS_NO_MEMORY;
761 bytes[0] = 1;
763 do {
764 uint32_t usable_space = cli_state_available_size(cli, 48);
765 size_t size = MIN(size1, usable_space);
766 struct tevent_req *req;
767 uint16_t vwv[5];
768 uint16_t *ret_vwv;
769 NTSTATUS status;
771 SSVAL(vwv+0, 0, fnum);
772 SSVAL(vwv+1, 0, size);
773 SIVAL(vwv+2, 0, offset);
774 SSVAL(vwv+4, 0, 0);
776 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
777 size+3);
778 if (bytes == NULL) {
779 return NT_STATUS_NO_MEMORY;
781 SSVAL(bytes, 1, size);
782 memcpy(bytes + 3, buf + total, size);
784 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
785 size+3, bytes, &req, 1, NULL, &ret_vwv,
786 NULL, NULL);
787 if (!NT_STATUS_IS_OK(status)) {
788 TALLOC_FREE(bytes);
789 return status;
792 size = SVAL(ret_vwv+0, 0);
793 TALLOC_FREE(req);
794 if (size == 0) {
795 break;
797 size1 -= size;
798 total += size;
799 offset += size;
801 } while (size1);
803 TALLOC_FREE(bytes);
805 if (ptotal != NULL) {
806 *ptotal = total;
808 return NT_STATUS_OK;
812 * Send a write&x request
815 struct cli_write_andx_state {
816 size_t size;
817 uint16_t vwv[14];
818 size_t written;
819 uint8_t pad;
820 struct iovec iov[2];
823 static void cli_write_andx_done(struct tevent_req *subreq);
825 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
826 struct event_context *ev,
827 struct cli_state *cli, uint16_t fnum,
828 uint16_t mode, const uint8_t *buf,
829 off_t offset, size_t size,
830 struct tevent_req **reqs_before,
831 int num_reqs_before,
832 struct tevent_req **psmbreq)
834 struct tevent_req *req, *subreq;
835 struct cli_write_andx_state *state;
836 bool bigoffset = ((cli_state_capabilities(cli) & CAP_LARGE_FILES) != 0);
837 uint8_t wct = bigoffset ? 14 : 12;
838 size_t max_write = cli_write_max_bufsize(cli, mode, wct);
839 uint16_t *vwv;
841 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
842 if (req == NULL) {
843 return NULL;
846 size = MIN(size, max_write);
848 vwv = state->vwv;
850 SCVAL(vwv+0, 0, 0xFF);
851 SCVAL(vwv+0, 1, 0);
852 SSVAL(vwv+1, 0, 0);
853 SSVAL(vwv+2, 0, fnum);
854 SIVAL(vwv+3, 0, offset);
855 SIVAL(vwv+5, 0, 0);
856 SSVAL(vwv+7, 0, mode);
857 SSVAL(vwv+8, 0, 0);
858 SSVAL(vwv+9, 0, (size>>16));
859 SSVAL(vwv+10, 0, size);
861 SSVAL(vwv+11, 0,
862 cli_smb_wct_ofs(reqs_before, num_reqs_before)
863 + 1 /* the wct field */
864 + wct * 2 /* vwv */
865 + 2 /* num_bytes field */
866 + 1 /* pad */);
868 if (bigoffset) {
869 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
872 state->pad = 0;
873 state->iov[0].iov_base = (void *)&state->pad;
874 state->iov[0].iov_len = 1;
875 state->iov[1].iov_base = discard_const_p(void, buf);
876 state->iov[1].iov_len = size;
878 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
879 2, state->iov);
880 if (tevent_req_nomem(subreq, req)) {
881 return tevent_req_post(req, ev);
883 tevent_req_set_callback(subreq, cli_write_andx_done, req);
884 *psmbreq = subreq;
885 return req;
888 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
889 struct event_context *ev,
890 struct cli_state *cli, uint16_t fnum,
891 uint16_t mode, const uint8_t *buf,
892 off_t offset, size_t size)
894 struct tevent_req *req, *subreq;
895 NTSTATUS status;
897 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
898 size, NULL, 0, &subreq);
899 if (req == NULL) {
900 return NULL;
903 status = cli_smb_req_send(subreq);
904 if (tevent_req_nterror(req, status)) {
905 return tevent_req_post(req, ev);
907 return req;
910 static void cli_write_andx_done(struct tevent_req *subreq)
912 struct tevent_req *req = tevent_req_callback_data(
913 subreq, struct tevent_req);
914 struct cli_write_andx_state *state = tevent_req_data(
915 req, struct cli_write_andx_state);
916 uint8_t wct;
917 uint16_t *vwv;
918 uint8_t *inbuf;
919 NTSTATUS status;
921 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
922 NULL, NULL);
923 TALLOC_FREE(subreq);
924 if (NT_STATUS_IS_ERR(status)) {
925 tevent_req_nterror(req, status);
926 return;
928 state->written = SVAL(vwv+2, 0);
929 state->written |= SVAL(vwv+4, 0)<<16;
930 tevent_req_done(req);
933 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
935 struct cli_write_andx_state *state = tevent_req_data(
936 req, struct cli_write_andx_state);
937 NTSTATUS status;
939 if (tevent_req_is_nterror(req, &status)) {
940 return status;
942 if (pwritten != 0) {
943 *pwritten = state->written;
945 return NT_STATUS_OK;
948 struct cli_writeall_state {
949 struct event_context *ev;
950 struct cli_state *cli;
951 uint16_t fnum;
952 uint16_t mode;
953 const uint8_t *buf;
954 off_t offset;
955 size_t size;
956 size_t written;
959 static void cli_writeall_written(struct tevent_req *req);
961 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
962 struct event_context *ev,
963 struct cli_state *cli,
964 uint16_t fnum,
965 uint16_t mode,
966 const uint8_t *buf,
967 off_t offset, size_t size)
969 struct tevent_req *req, *subreq;
970 struct cli_writeall_state *state;
972 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
973 if (req == NULL) {
974 return NULL;
976 state->ev = ev;
977 state->cli = cli;
978 state->fnum = fnum;
979 state->mode = mode;
980 state->buf = buf;
981 state->offset = offset;
982 state->size = size;
983 state->written = 0;
985 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
986 state->mode, state->buf, state->offset,
987 state->size);
988 if (tevent_req_nomem(subreq, req)) {
989 return tevent_req_post(req, ev);
991 tevent_req_set_callback(subreq, cli_writeall_written, req);
992 return req;
995 static void cli_writeall_written(struct tevent_req *subreq)
997 struct tevent_req *req = tevent_req_callback_data(
998 subreq, struct tevent_req);
999 struct cli_writeall_state *state = tevent_req_data(
1000 req, struct cli_writeall_state);
1001 NTSTATUS status;
1002 size_t written, to_write;
1004 status = cli_write_andx_recv(subreq, &written);
1005 TALLOC_FREE(subreq);
1006 if (tevent_req_nterror(req, status)) {
1007 return;
1010 state->written += written;
1012 if (state->written > state->size) {
1013 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1014 return;
1017 to_write = state->size - state->written;
1019 if (to_write == 0) {
1020 tevent_req_done(req);
1021 return;
1024 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1025 state->mode,
1026 state->buf + state->written,
1027 state->offset + state->written, to_write);
1028 if (tevent_req_nomem(subreq, req)) {
1029 return;
1031 tevent_req_set_callback(subreq, cli_writeall_written, req);
1034 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
1035 size_t *pwritten)
1037 struct cli_writeall_state *state = tevent_req_data(
1038 req, struct cli_writeall_state);
1039 NTSTATUS status;
1041 if (tevent_req_is_nterror(req, &status)) {
1042 return status;
1044 if (pwritten != NULL) {
1045 *pwritten = state->written;
1047 return NT_STATUS_OK;
1050 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1051 const uint8_t *buf, off_t offset, size_t size,
1052 size_t *pwritten)
1054 TALLOC_CTX *frame = talloc_stackframe();
1055 struct event_context *ev;
1056 struct tevent_req *req;
1057 NTSTATUS status = NT_STATUS_NO_MEMORY;
1059 if (cli_has_async_calls(cli)) {
1061 * Can't use sync call while an async call is in flight
1063 status = NT_STATUS_INVALID_PARAMETER;
1064 goto fail;
1066 ev = event_context_init(frame);
1067 if (ev == NULL) {
1068 goto fail;
1070 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1071 if (req == NULL) {
1072 goto fail;
1074 if (!tevent_req_poll(req, ev)) {
1075 status = map_nt_error_from_unix(errno);
1076 goto fail;
1078 status = cli_writeall_recv(req, pwritten);
1079 fail:
1080 TALLOC_FREE(frame);
1081 return status;
1084 struct cli_push_write_state {
1085 struct tevent_req *req;/* This is the main request! Not the subreq */
1086 uint32_t idx;
1087 off_t ofs;
1088 uint8_t *buf;
1089 size_t size;
1092 struct cli_push_state {
1093 struct event_context *ev;
1094 struct cli_state *cli;
1095 uint16_t fnum;
1096 uint16_t mode;
1097 off_t start_offset;
1098 size_t window_size;
1100 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1101 void *priv;
1103 bool eof;
1105 size_t chunk_size;
1106 off_t next_offset;
1109 * Outstanding requests
1111 uint32_t pending;
1112 uint16_t max_reqs;
1113 uint32_t num_reqs;
1114 struct cli_push_write_state **reqs;
1117 static void cli_push_written(struct tevent_req *req);
1119 static bool cli_push_write_setup(struct tevent_req *req,
1120 struct cli_push_state *state,
1121 uint32_t idx)
1123 struct cli_push_write_state *substate;
1124 struct tevent_req *subreq;
1126 substate = talloc(state->reqs, struct cli_push_write_state);
1127 if (!substate) {
1128 return false;
1130 substate->req = req;
1131 substate->idx = idx;
1132 substate->ofs = state->next_offset;
1133 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1134 if (!substate->buf) {
1135 talloc_free(substate);
1136 return false;
1138 substate->size = state->source(substate->buf,
1139 state->chunk_size,
1140 state->priv);
1141 if (substate->size == 0) {
1142 state->eof = true;
1143 /* nothing to send */
1144 talloc_free(substate);
1145 return true;
1148 subreq = cli_writeall_send(substate,
1149 state->ev, state->cli,
1150 state->fnum, state->mode,
1151 substate->buf,
1152 substate->ofs,
1153 substate->size);
1154 if (!subreq) {
1155 talloc_free(substate);
1156 return false;
1158 tevent_req_set_callback(subreq, cli_push_written, substate);
1160 state->reqs[idx] = substate;
1161 state->pending += 1;
1162 state->next_offset += substate->size;
1164 return true;
1167 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1168 struct cli_state *cli,
1169 uint16_t fnum, uint16_t mode,
1170 off_t start_offset, size_t window_size,
1171 size_t (*source)(uint8_t *buf, size_t n,
1172 void *priv),
1173 void *priv)
1175 struct tevent_req *req;
1176 struct cli_push_state *state;
1177 uint32_t i;
1178 size_t page_size = 1024;
1180 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1181 if (req == NULL) {
1182 return NULL;
1184 state->cli = cli;
1185 state->ev = ev;
1186 state->fnum = fnum;
1187 state->start_offset = start_offset;
1188 state->mode = mode;
1189 state->source = source;
1190 state->priv = priv;
1191 state->eof = false;
1192 state->pending = 0;
1193 state->next_offset = start_offset;
1195 state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
1196 if (state->chunk_size > page_size) {
1197 state->chunk_size &= ~(page_size - 1);
1200 state->max_reqs = cli_state_max_requests(cli);
1202 if (window_size == 0) {
1203 window_size = state->max_reqs * state->chunk_size;
1205 state->num_reqs = window_size/state->chunk_size;
1206 if ((window_size % state->chunk_size) > 0) {
1207 state->num_reqs += 1;
1209 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
1210 state->num_reqs = MAX(state->num_reqs, 1);
1212 state->reqs = talloc_zero_array(state, struct cli_push_write_state *,
1213 state->num_reqs);
1214 if (state->reqs == NULL) {
1215 goto failed;
1218 for (i=0; i<state->num_reqs; i++) {
1219 if (!cli_push_write_setup(req, state, i)) {
1220 goto failed;
1223 if (state->eof) {
1224 break;
1228 if (state->pending == 0) {
1229 tevent_req_done(req);
1230 return tevent_req_post(req, ev);
1233 return req;
1235 failed:
1236 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1237 return tevent_req_post(req, ev);
1240 static void cli_push_written(struct tevent_req *subreq)
1242 struct cli_push_write_state *substate = tevent_req_callback_data(
1243 subreq, struct cli_push_write_state);
1244 struct tevent_req *req = substate->req;
1245 struct cli_push_state *state = tevent_req_data(
1246 req, struct cli_push_state);
1247 NTSTATUS status;
1248 uint32_t idx = substate->idx;
1250 state->reqs[idx] = NULL;
1251 state->pending -= 1;
1253 status = cli_writeall_recv(subreq, NULL);
1254 TALLOC_FREE(subreq);
1255 TALLOC_FREE(substate);
1256 if (tevent_req_nterror(req, status)) {
1257 return;
1260 if (!state->eof) {
1261 if (!cli_push_write_setup(req, state, idx)) {
1262 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1263 return;
1267 if (state->pending == 0) {
1268 tevent_req_done(req);
1269 return;
1273 NTSTATUS cli_push_recv(struct tevent_req *req)
1275 return tevent_req_simple_recv_ntstatus(req);
1278 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1279 off_t start_offset, size_t window_size,
1280 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1281 void *priv)
1283 TALLOC_CTX *frame = talloc_stackframe();
1284 struct event_context *ev;
1285 struct tevent_req *req;
1286 NTSTATUS status = NT_STATUS_OK;
1288 if (cli_has_async_calls(cli)) {
1290 * Can't use sync call while an async call is in flight
1292 status = NT_STATUS_INVALID_PARAMETER;
1293 goto fail;
1296 ev = event_context_init(frame);
1297 if (ev == NULL) {
1298 status = NT_STATUS_NO_MEMORY;
1299 goto fail;
1302 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1303 window_size, source, priv);
1304 if (req == NULL) {
1305 status = NT_STATUS_NO_MEMORY;
1306 goto fail;
1309 if (!tevent_req_poll(req, ev)) {
1310 status = map_nt_error_from_unix(errno);
1311 goto fail;
1314 status = cli_push_recv(req);
1315 fail:
1316 TALLOC_FREE(frame);
1317 return status;