s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / clireadwrite.c
blob540600778474e81be054b6cab71bdefe441617c2
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 size_t data_offset = smb_size - 4;
32 size_t wct = 12;
34 size_t useable_space;
36 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
37 && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
38 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
40 if (cli->capabilities & CAP_LARGE_READX) {
41 return cli->is_samba
42 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
43 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
46 data_offset += wct * sizeof(uint16_t);
47 data_offset += 1; /* pad */
49 useable_space = cli->max_xmit - data_offset;
51 return useable_space;
54 /****************************************************************************
55 Calculate the recommended write buffer size
56 ****************************************************************************/
57 static size_t cli_write_max_bufsize(struct cli_state *cli,
58 uint16_t write_mode,
59 uint8_t wct)
61 if (write_mode == 0 &&
62 !client_is_signing_on(cli) &&
63 !cli_encryption_on(cli) &&
64 (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
65 (cli->capabilities & CAP_LARGE_FILES)) {
66 /* Only do massive writes if we can do them direct
67 * with no signing or encrypting - not on a pipe. */
68 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
71 if (cli->is_samba) {
72 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
75 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
76 || client_is_signing_on(cli)
77 || strequal(cli->dev, "LPT1:")) {
78 size_t data_offset = smb_size - 4;
79 size_t useable_space;
81 data_offset += wct * sizeof(uint16_t);
82 data_offset += 1; /* pad */
84 useable_space = cli->max_xmit - data_offset;
86 return useable_space;
89 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
92 struct cli_read_andx_state {
93 size_t size;
94 uint16_t vwv[12];
95 NTSTATUS status;
96 size_t received;
97 uint8_t *buf;
100 static void cli_read_andx_done(struct tevent_req *subreq);
102 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
103 struct event_context *ev,
104 struct cli_state *cli, uint16_t fnum,
105 off_t offset, size_t size,
106 struct tevent_req **psmbreq)
108 struct tevent_req *req, *subreq;
109 struct cli_read_andx_state *state;
110 uint8_t wct = 10;
112 if (size > cli_read_max_bufsize(cli)) {
113 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
114 "size=%d\n", (int)size,
115 (int)cli_read_max_bufsize(cli)));
116 return NULL;
119 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
120 if (req == NULL) {
121 return NULL;
123 state->size = size;
125 SCVAL(state->vwv + 0, 0, 0xFF);
126 SCVAL(state->vwv + 0, 1, 0);
127 SSVAL(state->vwv + 1, 0, 0);
128 SSVAL(state->vwv + 2, 0, fnum);
129 SIVAL(state->vwv + 3, 0, offset);
130 SSVAL(state->vwv + 5, 0, size);
131 SSVAL(state->vwv + 6, 0, size);
132 SSVAL(state->vwv + 7, 0, (size >> 16));
133 SSVAL(state->vwv + 8, 0, 0);
134 SSVAL(state->vwv + 9, 0, 0);
136 if ((uint64_t)offset >> 32) {
137 SIVAL(state->vwv + 10, 0,
138 (((uint64_t)offset)>>32) & 0xffffffff);
139 wct += 2;
142 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
143 state->vwv, 0, NULL);
144 if (subreq == NULL) {
145 TALLOC_FREE(req);
146 return NULL;
148 tevent_req_set_callback(subreq, cli_read_andx_done, req);
149 *psmbreq = subreq;
150 return req;
153 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
154 struct event_context *ev,
155 struct cli_state *cli, uint16_t fnum,
156 off_t offset, size_t size)
158 struct tevent_req *req, *subreq;
159 NTSTATUS status;
161 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
162 &subreq);
163 if (req == NULL) {
164 return NULL;
167 status = cli_smb_req_send(subreq);
168 if (tevent_req_nterror(req, status)) {
169 return tevent_req_post(req, ev);
171 return req;
174 static void cli_read_andx_done(struct tevent_req *subreq)
176 struct tevent_req *req = tevent_req_callback_data(
177 subreq, struct tevent_req);
178 struct cli_read_andx_state *state = tevent_req_data(
179 req, struct cli_read_andx_state);
180 uint8_t *inbuf;
181 uint8_t wct;
182 uint16_t *vwv;
183 uint32_t num_bytes;
184 uint8_t *bytes;
186 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
187 &num_bytes, &bytes);
188 TALLOC_FREE(subreq);
189 if (NT_STATUS_IS_ERR(state->status)) {
190 tevent_req_nterror(req, state->status);
191 return;
194 /* size is the number of bytes the server returned.
195 * Might be zero. */
196 state->received = SVAL(vwv + 5, 0);
197 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
199 if (state->received > state->size) {
200 DEBUG(5,("server returned more than we wanted!\n"));
201 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
202 return;
206 * bcc field must be valid for small reads, for large reads the 16-bit
207 * bcc field can't be correct.
210 if ((state->received < 0xffff) && (state->received > num_bytes)) {
211 DEBUG(5, ("server announced more bytes than sent\n"));
212 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
213 return;
216 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
218 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
219 || ((state->received != 0) && (state->buf < bytes))) {
220 DEBUG(5, ("server returned invalid read&x data offset\n"));
221 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
222 return;
224 tevent_req_done(req);
228 * Pull the data out of a finished async read_and_x request. rcvbuf is
229 * talloced from the request, so better make sure that you copy it away before
230 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
231 * talloc_move it!
234 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
235 uint8_t **rcvbuf)
237 struct cli_read_andx_state *state = tevent_req_data(
238 req, struct cli_read_andx_state);
239 NTSTATUS status;
241 if (tevent_req_is_nterror(req, &status)) {
242 return status;
244 *received = state->received;
245 *rcvbuf = state->buf;
246 return NT_STATUS_OK;
249 struct cli_readall_state {
250 struct tevent_context *ev;
251 struct cli_state *cli;
252 uint16_t fnum;
253 off_t start_offset;
254 size_t size;
255 size_t received;
256 uint8_t *buf;
259 static void cli_readall_done(struct tevent_req *subreq);
261 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
262 struct event_context *ev,
263 struct cli_state *cli,
264 uint16_t fnum,
265 off_t offset, size_t size)
267 struct tevent_req *req, *subreq;
268 struct cli_readall_state *state;
270 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
271 if (req == NULL) {
272 return NULL;
274 state->ev = ev;
275 state->cli = cli;
276 state->fnum = fnum;
277 state->start_offset = offset;
278 state->size = size;
279 state->received = 0;
280 state->buf = NULL;
282 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
283 if (tevent_req_nomem(subreq, req)) {
284 return tevent_req_post(req, ev);
286 tevent_req_set_callback(subreq, cli_readall_done, req);
287 return req;
290 static void cli_readall_done(struct tevent_req *subreq)
292 struct tevent_req *req = tevent_req_callback_data(
293 subreq, struct tevent_req);
294 struct cli_readall_state *state = tevent_req_data(
295 req, struct cli_readall_state);
296 ssize_t received;
297 uint8_t *buf;
298 NTSTATUS status;
300 status = cli_read_andx_recv(subreq, &received, &buf);
301 if (tevent_req_nterror(req, status)) {
302 return;
305 if (received == 0) {
306 /* EOF */
307 tevent_req_done(req);
308 return;
311 if ((state->received == 0) && (received == state->size)) {
312 /* Ideal case: Got it all in one run */
313 state->buf = buf;
314 state->received += received;
315 tevent_req_done(req);
316 return;
320 * We got a short read, issue a read for the
321 * rest. Unfortunately we have to allocate the buffer
322 * ourselves now, as our caller expects to receive a single
323 * buffer. cli_read_andx does it from the buffer received from
324 * the net, but with a short read we have to put it together
325 * from several reads.
328 if (state->buf == NULL) {
329 state->buf = talloc_array(state, uint8_t, state->size);
330 if (tevent_req_nomem(state->buf, req)) {
331 return;
334 memcpy(state->buf + state->received, buf, received);
335 state->received += received;
337 TALLOC_FREE(subreq);
339 if (state->received >= state->size) {
340 tevent_req_done(req);
341 return;
344 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
345 state->start_offset + state->received,
346 state->size - state->received);
347 if (tevent_req_nomem(subreq, req)) {
348 return;
350 tevent_req_set_callback(subreq, cli_readall_done, req);
353 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
354 uint8_t **rcvbuf)
356 struct cli_readall_state *state = tevent_req_data(
357 req, struct cli_readall_state);
358 NTSTATUS status;
360 if (tevent_req_is_nterror(req, &status)) {
361 return status;
363 *received = state->received;
364 *rcvbuf = state->buf;
365 return NT_STATUS_OK;
368 struct cli_pull_subreq {
369 struct tevent_req *req;
370 ssize_t received;
371 uint8_t *buf;
375 * Parallel read support.
377 * cli_pull sends as many read&x requests as the server would allow via
378 * max_mux at a time. When replies flow back in, the data is written into
379 * the callback function "sink" in the right order.
382 struct cli_pull_state {
383 struct tevent_req *req;
385 struct event_context *ev;
386 struct cli_state *cli;
387 uint16_t fnum;
388 off_t start_offset;
389 SMB_OFF_T size;
391 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
392 void *priv;
394 size_t chunk_size;
397 * Outstanding requests
399 int num_reqs;
400 struct cli_pull_subreq *reqs;
403 * For how many bytes did we send requests already?
405 SMB_OFF_T requested;
408 * Next request index to push into "sink". This walks around the "req"
409 * array, taking care that the requests are pushed to "sink" in the
410 * right order. If necessary (i.e. replies don't come in in the right
411 * order), replies are held back in "reqs".
413 int top_req;
416 * How many bytes did we push into "sink"?
419 SMB_OFF_T pushed;
422 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
424 struct cli_pull_state *state = tevent_req_data(
425 req, struct cli_pull_state);
426 char *result;
428 result = tevent_req_default_print(req, mem_ctx);
429 if (result == NULL) {
430 return NULL;
433 return talloc_asprintf_append_buffer(
434 result, "num_reqs=%d, top_req=%d",
435 state->num_reqs, state->top_req);
438 static void cli_pull_read_done(struct tevent_req *read_req);
441 * Prepare an async pull request
444 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
445 struct event_context *ev,
446 struct cli_state *cli,
447 uint16_t fnum, off_t start_offset,
448 SMB_OFF_T size, size_t window_size,
449 NTSTATUS (*sink)(char *buf, size_t n,
450 void *priv),
451 void *priv)
453 struct tevent_req *req;
454 struct cli_pull_state *state;
455 int i;
457 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
458 if (req == NULL) {
459 return NULL;
461 tevent_req_set_print_fn(req, cli_pull_print);
462 state->req = req;
464 state->cli = cli;
465 state->ev = ev;
466 state->fnum = fnum;
467 state->start_offset = start_offset;
468 state->size = size;
469 state->sink = sink;
470 state->priv = priv;
472 state->pushed = 0;
473 state->top_req = 0;
475 if (size == 0) {
476 tevent_req_done(req);
477 return tevent_req_post(req, ev);
480 state->chunk_size = cli_read_max_bufsize(cli);
482 state->num_reqs = MAX(window_size/state->chunk_size, 1);
483 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
485 state->reqs = talloc_zero_array(state, struct cli_pull_subreq,
486 state->num_reqs);
487 if (state->reqs == NULL) {
488 goto failed;
491 state->requested = 0;
493 for (i=0; i<state->num_reqs; i++) {
494 struct cli_pull_subreq *subreq = &state->reqs[i];
495 SMB_OFF_T size_left;
496 size_t request_thistime;
498 if (state->requested >= size) {
499 state->num_reqs = i;
500 break;
503 size_left = size - state->requested;
504 request_thistime = MIN(size_left, state->chunk_size);
506 subreq->req = cli_readall_send(
507 state->reqs, ev, cli, fnum,
508 state->start_offset + state->requested,
509 request_thistime);
511 if (subreq->req == NULL) {
512 goto failed;
514 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
515 state->requested += request_thistime;
517 return req;
519 failed:
520 TALLOC_FREE(req);
521 return NULL;
525 * Handle incoming read replies, push the data into sink and send out new
526 * requests if necessary.
529 static void cli_pull_read_done(struct tevent_req *subreq)
531 struct tevent_req *req = tevent_req_callback_data(
532 subreq, struct tevent_req);
533 struct cli_pull_state *state = tevent_req_data(
534 req, struct cli_pull_state);
535 struct cli_pull_subreq *pull_subreq = NULL;
536 NTSTATUS status;
537 int i;
539 for (i = 0; i < state->num_reqs; i++) {
540 pull_subreq = &state->reqs[i];
541 if (subreq == pull_subreq->req) {
542 break;
545 if (i == state->num_reqs) {
546 /* Huh -- received something we did not send?? */
547 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
548 return;
551 status = cli_readall_recv(subreq, &pull_subreq->received,
552 &pull_subreq->buf);
553 if (!NT_STATUS_IS_OK(status)) {
554 tevent_req_nterror(state->req, status);
555 return;
559 * This loop is the one to take care of out-of-order replies. All
560 * pending requests are in state->reqs, state->reqs[top_req] is the
561 * one that is to be pushed next. If however a request later than
562 * top_req is replied to, then we can't push yet. If top_req is
563 * replied to at a later point then, we need to push all the finished
564 * requests.
567 while (state->reqs[state->top_req].req != NULL) {
568 struct cli_pull_subreq *top_subreq;
570 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
571 state->top_req));
573 top_subreq = &state->reqs[state->top_req];
575 if (tevent_req_is_in_progress(top_subreq->req)) {
576 DEBUG(11, ("cli_pull_read_done: top request not yet "
577 "done\n"));
578 return;
581 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
582 "pushed\n", (int)top_subreq->received,
583 (int)state->pushed));
585 status = state->sink((char *)top_subreq->buf,
586 top_subreq->received, state->priv);
587 if (tevent_req_nterror(state->req, status)) {
588 return;
590 state->pushed += top_subreq->received;
592 TALLOC_FREE(state->reqs[state->top_req].req);
594 if (state->requested < state->size) {
595 struct tevent_req *new_req;
596 SMB_OFF_T size_left;
597 size_t request_thistime;
599 size_left = state->size - state->requested;
600 request_thistime = MIN(size_left, state->chunk_size);
602 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
603 "at %d, position %d\n",
604 (int)request_thistime,
605 (int)(state->start_offset
606 + state->requested),
607 state->top_req));
609 new_req = cli_readall_send(
610 state->reqs, state->ev, state->cli,
611 state->fnum,
612 state->start_offset + state->requested,
613 request_thistime);
615 if (tevent_req_nomem(new_req, state->req)) {
616 return;
618 tevent_req_set_callback(new_req, cli_pull_read_done,
619 req);
621 state->reqs[state->top_req].req = new_req;
622 state->requested += request_thistime;
625 state->top_req = (state->top_req+1) % state->num_reqs;
628 tevent_req_done(req);
631 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
633 struct cli_pull_state *state = tevent_req_data(
634 req, struct cli_pull_state);
635 NTSTATUS status;
637 if (tevent_req_is_nterror(req, &status)) {
638 return status;
640 *received = state->pushed;
641 return NT_STATUS_OK;
644 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
645 off_t start_offset, SMB_OFF_T size, size_t window_size,
646 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
647 void *priv, SMB_OFF_T *received)
649 TALLOC_CTX *frame = talloc_stackframe();
650 struct event_context *ev;
651 struct tevent_req *req;
652 NTSTATUS status = NT_STATUS_OK;
654 if (cli_has_async_calls(cli)) {
656 * Can't use sync call while an async call is in flight
658 status = NT_STATUS_INVALID_PARAMETER;
659 goto fail;
662 ev = event_context_init(frame);
663 if (ev == NULL) {
664 status = NT_STATUS_NO_MEMORY;
665 goto fail;
668 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
669 window_size, sink, priv);
670 if (req == NULL) {
671 status = NT_STATUS_NO_MEMORY;
672 goto fail;
675 if (!tevent_req_poll(req, ev)) {
676 status = map_nt_error_from_unix(errno);
677 goto fail;
680 status = cli_pull_recv(req, received);
681 fail:
682 TALLOC_FREE(frame);
683 return status;
686 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
688 char **pbuf = (char **)priv;
689 memcpy(*pbuf, buf, n);
690 *pbuf += n;
691 return NT_STATUS_OK;
694 ssize_t cli_read_old(struct cli_state *cli, uint16_t fnum, char *buf,
695 off_t offset, size_t size)
697 NTSTATUS status;
698 SMB_OFF_T ret;
700 status = cli_pull(cli, fnum, offset, size, size,
701 cli_read_sink, &buf, &ret);
702 if (!NT_STATUS_IS_OK(status)) {
703 return -1;
705 return ret;
708 NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
709 char *buf, off_t offset, size_t size,
710 size_t *nread)
712 NTSTATUS status;
713 SMB_OFF_T ret;
715 status = cli_pull(cli, fnum, offset, size, size,
716 cli_read_sink, &buf, &ret);
717 if (!NT_STATUS_IS_OK(status)) {
718 return status;
721 if (nread) {
722 *nread = ret;
725 return NT_STATUS_OK;
728 /****************************************************************************
729 write to a file using a SMBwrite and not bypassing 0 byte writes
730 ****************************************************************************/
732 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
733 off_t offset, size_t size1, size_t *ptotal)
735 uint8_t *bytes;
736 ssize_t total = 0;
739 * 3 bytes prefix
742 bytes = talloc_array(talloc_tos(), uint8_t, 3);
743 if (bytes == NULL) {
744 return NT_STATUS_NO_MEMORY;
746 bytes[0] = 1;
748 do {
749 size_t size = MIN(size1, cli->max_xmit - 48);
750 struct tevent_req *req;
751 uint16_t vwv[5];
752 uint16_t *ret_vwv;
753 NTSTATUS status;
755 SSVAL(vwv+0, 0, fnum);
756 SSVAL(vwv+1, 0, size);
757 SIVAL(vwv+2, 0, offset);
758 SSVAL(vwv+4, 0, 0);
760 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
761 size+3);
762 if (bytes == NULL) {
763 return NT_STATUS_NO_MEMORY;
765 SSVAL(bytes, 1, size);
766 memcpy(bytes + 3, buf + total, size);
768 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
769 size+3, bytes, &req, 1, NULL, &ret_vwv,
770 NULL, NULL);
771 if (!NT_STATUS_IS_OK(status)) {
772 TALLOC_FREE(bytes);
773 return status;
776 size = SVAL(ret_vwv+0, 0);
777 TALLOC_FREE(req);
778 if (size == 0) {
779 break;
781 size1 -= size;
782 total += size;
783 offset += size;
785 } while (size1);
787 TALLOC_FREE(bytes);
789 if (ptotal != NULL) {
790 *ptotal = total;
792 return NT_STATUS_OK;
796 * Send a write&x request
799 struct cli_write_andx_state {
800 size_t size;
801 uint16_t vwv[14];
802 size_t written;
803 uint8_t pad;
804 struct iovec iov[2];
807 static void cli_write_andx_done(struct tevent_req *subreq);
809 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
810 struct event_context *ev,
811 struct cli_state *cli, uint16_t fnum,
812 uint16_t mode, const uint8_t *buf,
813 off_t offset, size_t size,
814 struct tevent_req **reqs_before,
815 int num_reqs_before,
816 struct tevent_req **psmbreq)
818 struct tevent_req *req, *subreq;
819 struct cli_write_andx_state *state;
820 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
821 uint8_t wct = bigoffset ? 14 : 12;
822 size_t max_write = cli_write_max_bufsize(cli, mode, wct);
823 uint16_t *vwv;
825 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
826 if (req == NULL) {
827 return NULL;
830 size = MIN(size, max_write);
832 vwv = state->vwv;
834 SCVAL(vwv+0, 0, 0xFF);
835 SCVAL(vwv+0, 1, 0);
836 SSVAL(vwv+1, 0, 0);
837 SSVAL(vwv+2, 0, fnum);
838 SIVAL(vwv+3, 0, offset);
839 SIVAL(vwv+5, 0, 0);
840 SSVAL(vwv+7, 0, mode);
841 SSVAL(vwv+8, 0, 0);
842 SSVAL(vwv+9, 0, (size>>16));
843 SSVAL(vwv+10, 0, size);
845 SSVAL(vwv+11, 0,
846 cli_smb_wct_ofs(reqs_before, num_reqs_before)
847 + 1 /* the wct field */
848 + wct * 2 /* vwv */
849 + 2 /* num_bytes field */
850 + 1 /* pad */);
852 if (bigoffset) {
853 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
856 state->pad = 0;
857 state->iov[0].iov_base = (void *)&state->pad;
858 state->iov[0].iov_len = 1;
859 state->iov[1].iov_base = discard_const_p(void, buf);
860 state->iov[1].iov_len = size;
862 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
863 2, state->iov);
864 if (tevent_req_nomem(subreq, req)) {
865 return tevent_req_post(req, ev);
867 tevent_req_set_callback(subreq, cli_write_andx_done, req);
868 *psmbreq = subreq;
869 return req;
872 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
873 struct event_context *ev,
874 struct cli_state *cli, uint16_t fnum,
875 uint16_t mode, const uint8_t *buf,
876 off_t offset, size_t size)
878 struct tevent_req *req, *subreq;
879 NTSTATUS status;
881 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
882 size, NULL, 0, &subreq);
883 if (req == NULL) {
884 return NULL;
887 status = cli_smb_req_send(subreq);
888 if (tevent_req_nterror(req, status)) {
889 return tevent_req_post(req, ev);
891 return req;
894 static void cli_write_andx_done(struct tevent_req *subreq)
896 struct tevent_req *req = tevent_req_callback_data(
897 subreq, struct tevent_req);
898 struct cli_write_andx_state *state = tevent_req_data(
899 req, struct cli_write_andx_state);
900 uint8_t wct;
901 uint16_t *vwv;
902 uint8_t *inbuf;
903 NTSTATUS status;
905 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
906 NULL, NULL);
907 TALLOC_FREE(subreq);
908 if (NT_STATUS_IS_ERR(status)) {
909 tevent_req_nterror(req, status);
910 return;
912 state->written = SVAL(vwv+2, 0);
913 state->written |= SVAL(vwv+4, 0)<<16;
914 tevent_req_done(req);
917 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
919 struct cli_write_andx_state *state = tevent_req_data(
920 req, struct cli_write_andx_state);
921 NTSTATUS status;
923 if (tevent_req_is_nterror(req, &status)) {
924 return status;
926 if (pwritten != 0) {
927 *pwritten = state->written;
929 return NT_STATUS_OK;
932 struct cli_writeall_state {
933 struct event_context *ev;
934 struct cli_state *cli;
935 uint16_t fnum;
936 uint16_t mode;
937 const uint8_t *buf;
938 off_t offset;
939 size_t size;
940 size_t written;
943 static void cli_writeall_written(struct tevent_req *req);
945 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
946 struct event_context *ev,
947 struct cli_state *cli,
948 uint16_t fnum,
949 uint16_t mode,
950 const uint8_t *buf,
951 off_t offset, size_t size)
953 struct tevent_req *req, *subreq;
954 struct cli_writeall_state *state;
956 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
957 if (req == NULL) {
958 return NULL;
960 state->ev = ev;
961 state->cli = cli;
962 state->fnum = fnum;
963 state->mode = mode;
964 state->buf = buf;
965 state->offset = offset;
966 state->size = size;
967 state->written = 0;
969 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
970 state->mode, state->buf, state->offset,
971 state->size);
972 if (tevent_req_nomem(subreq, req)) {
973 return tevent_req_post(req, ev);
975 tevent_req_set_callback(subreq, cli_writeall_written, req);
976 return req;
979 static void cli_writeall_written(struct tevent_req *subreq)
981 struct tevent_req *req = tevent_req_callback_data(
982 subreq, struct tevent_req);
983 struct cli_writeall_state *state = tevent_req_data(
984 req, struct cli_writeall_state);
985 NTSTATUS status;
986 size_t written, to_write;
988 status = cli_write_andx_recv(subreq, &written);
989 TALLOC_FREE(subreq);
990 if (tevent_req_nterror(req, status)) {
991 return;
994 state->written += written;
996 if (state->written > state->size) {
997 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
998 return;
1001 to_write = state->size - state->written;
1003 if (to_write == 0) {
1004 tevent_req_done(req);
1005 return;
1008 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1009 state->mode,
1010 state->buf + state->written,
1011 state->offset + state->written, to_write);
1012 if (tevent_req_nomem(subreq, req)) {
1013 return;
1015 tevent_req_set_callback(subreq, cli_writeall_written, req);
1018 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
1019 size_t *pwritten)
1021 struct cli_writeall_state *state = tevent_req_data(
1022 req, struct cli_writeall_state);
1023 NTSTATUS status;
1025 if (tevent_req_is_nterror(req, &status)) {
1026 return status;
1028 if (pwritten != NULL) {
1029 *pwritten = state->written;
1031 return NT_STATUS_OK;
1034 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1035 const uint8_t *buf, off_t offset, size_t size,
1036 size_t *pwritten)
1038 TALLOC_CTX *frame = talloc_stackframe();
1039 struct event_context *ev;
1040 struct tevent_req *req;
1041 NTSTATUS status = NT_STATUS_NO_MEMORY;
1043 if (cli_has_async_calls(cli)) {
1045 * Can't use sync call while an async call is in flight
1047 status = NT_STATUS_INVALID_PARAMETER;
1048 goto fail;
1050 ev = event_context_init(frame);
1051 if (ev == NULL) {
1052 goto fail;
1054 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1055 if (req == NULL) {
1056 goto fail;
1058 if (!tevent_req_poll(req, ev)) {
1059 status = map_nt_error_from_unix(errno);
1060 goto fail;
1062 status = cli_writeall_recv(req, pwritten);
1063 fail:
1064 TALLOC_FREE(frame);
1065 return status;
1068 struct cli_push_write_state {
1069 struct tevent_req *req;/* This is the main request! Not the subreq */
1070 uint32_t idx;
1071 off_t ofs;
1072 uint8_t *buf;
1073 size_t size;
1076 struct cli_push_state {
1077 struct event_context *ev;
1078 struct cli_state *cli;
1079 uint16_t fnum;
1080 uint16_t mode;
1081 off_t start_offset;
1082 size_t window_size;
1084 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1085 void *priv;
1087 bool eof;
1089 size_t chunk_size;
1090 off_t next_offset;
1093 * Outstanding requests
1095 uint32_t pending;
1096 uint32_t num_reqs;
1097 struct cli_push_write_state **reqs;
1100 static void cli_push_written(struct tevent_req *req);
1102 static bool cli_push_write_setup(struct tevent_req *req,
1103 struct cli_push_state *state,
1104 uint32_t idx)
1106 struct cli_push_write_state *substate;
1107 struct tevent_req *subreq;
1109 substate = talloc(state->reqs, struct cli_push_write_state);
1110 if (!substate) {
1111 return false;
1113 substate->req = req;
1114 substate->idx = idx;
1115 substate->ofs = state->next_offset;
1116 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1117 if (!substate->buf) {
1118 talloc_free(substate);
1119 return false;
1121 substate->size = state->source(substate->buf,
1122 state->chunk_size,
1123 state->priv);
1124 if (substate->size == 0) {
1125 state->eof = true;
1126 /* nothing to send */
1127 talloc_free(substate);
1128 return true;
1131 subreq = cli_writeall_send(substate,
1132 state->ev, state->cli,
1133 state->fnum, state->mode,
1134 substate->buf,
1135 substate->ofs,
1136 substate->size);
1137 if (!subreq) {
1138 talloc_free(substate);
1139 return false;
1141 tevent_req_set_callback(subreq, cli_push_written, substate);
1143 state->reqs[idx] = substate;
1144 state->pending += 1;
1145 state->next_offset += substate->size;
1147 return true;
1150 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1151 struct cli_state *cli,
1152 uint16_t fnum, uint16_t mode,
1153 off_t start_offset, size_t window_size,
1154 size_t (*source)(uint8_t *buf, size_t n,
1155 void *priv),
1156 void *priv)
1158 struct tevent_req *req;
1159 struct cli_push_state *state;
1160 uint32_t i;
1162 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1163 if (req == NULL) {
1164 return NULL;
1166 state->cli = cli;
1167 state->ev = ev;
1168 state->fnum = fnum;
1169 state->start_offset = start_offset;
1170 state->mode = mode;
1171 state->source = source;
1172 state->priv = priv;
1173 state->eof = false;
1174 state->pending = 0;
1175 state->next_offset = start_offset;
1177 state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
1179 if (window_size == 0) {
1180 window_size = cli->max_mux * state->chunk_size;
1182 state->num_reqs = window_size/state->chunk_size;
1183 if ((window_size % state->chunk_size) > 0) {
1184 state->num_reqs += 1;
1186 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1187 state->num_reqs = MAX(state->num_reqs, 1);
1189 state->reqs = talloc_zero_array(state, struct cli_push_write_state *,
1190 state->num_reqs);
1191 if (state->reqs == NULL) {
1192 goto failed;
1195 for (i=0; i<state->num_reqs; i++) {
1196 if (!cli_push_write_setup(req, state, i)) {
1197 goto failed;
1200 if (state->eof) {
1201 break;
1205 if (state->pending == 0) {
1206 tevent_req_done(req);
1207 return tevent_req_post(req, ev);
1210 return req;
1212 failed:
1213 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1214 return tevent_req_post(req, ev);
1217 static void cli_push_written(struct tevent_req *subreq)
1219 struct cli_push_write_state *substate = tevent_req_callback_data(
1220 subreq, struct cli_push_write_state);
1221 struct tevent_req *req = substate->req;
1222 struct cli_push_state *state = tevent_req_data(
1223 req, struct cli_push_state);
1224 NTSTATUS status;
1225 uint32_t idx = substate->idx;
1227 state->reqs[idx] = NULL;
1228 state->pending -= 1;
1230 status = cli_writeall_recv(subreq, NULL);
1231 TALLOC_FREE(subreq);
1232 TALLOC_FREE(substate);
1233 if (tevent_req_nterror(req, status)) {
1234 return;
1237 if (!state->eof) {
1238 if (!cli_push_write_setup(req, state, idx)) {
1239 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1240 return;
1244 if (state->pending == 0) {
1245 tevent_req_done(req);
1246 return;
1250 NTSTATUS cli_push_recv(struct tevent_req *req)
1252 return tevent_req_simple_recv_ntstatus(req);
1255 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1256 off_t start_offset, size_t window_size,
1257 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1258 void *priv)
1260 TALLOC_CTX *frame = talloc_stackframe();
1261 struct event_context *ev;
1262 struct tevent_req *req;
1263 NTSTATUS status = NT_STATUS_OK;
1265 if (cli_has_async_calls(cli)) {
1267 * Can't use sync call while an async call is in flight
1269 status = NT_STATUS_INVALID_PARAMETER;
1270 goto fail;
1273 ev = event_context_init(frame);
1274 if (ev == NULL) {
1275 status = NT_STATUS_NO_MEMORY;
1276 goto fail;
1279 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1280 window_size, source, priv);
1281 if (req == NULL) {
1282 status = NT_STATUS_NO_MEMORY;
1283 goto fail;
1286 if (!tevent_req_poll(req, ev)) {
1287 status = map_nt_error_from_unix(errno);
1288 goto fail;
1291 status = cli_push_recv(req);
1292 fail:
1293 TALLOC_FREE(frame);
1294 return status;