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/>.
21 #include "async_smb.h"
23 /****************************************************************************
24 Calculate the recommended read buffer size
25 ****************************************************************************/
26 static size_t cli_read_max_bufsize(struct cli_state *cli)
28 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
29 && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
30 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
32 if (cli->capabilities & CAP_LARGE_READX) {
34 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
35 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
37 return (cli->max_xmit - (smb_size+32)) & ~1023;
40 /****************************************************************************
41 Calculate the recommended write buffer size
42 ****************************************************************************/
43 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
45 if (write_mode == 0 &&
46 !client_is_signing_on(cli) &&
47 !cli_encryption_on(cli) &&
48 (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
49 (cli->capabilities & CAP_LARGE_FILES)) {
50 /* Only do massive writes if we can do them direct
51 * with no signing or encrypting - not on a pipe. */
52 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
56 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
59 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
60 || client_is_signing_on(cli)
61 || strequal(cli->dev, "LPT1:")) {
64 * Printer devices are restricted to max_xmit writesize in
65 * Vista and XPSP3 as are signing connections.
68 return (cli->max_xmit - (smb_size+32)) & ~1023;
71 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
74 struct cli_read_andx_state {
82 static void cli_read_andx_done(struct tevent_req *subreq);
84 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
85 struct event_context *ev,
86 struct cli_state *cli, uint16_t fnum,
87 off_t offset, size_t size,
88 struct tevent_req **psmbreq)
90 struct tevent_req *req, *subreq;
91 struct cli_read_andx_state *state;
94 if (size > cli_read_max_bufsize(cli)) {
95 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
96 "size=%d\n", (int)size,
97 (int)cli_read_max_bufsize(cli)));
101 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
107 SCVAL(state->vwv + 0, 0, 0xFF);
108 SCVAL(state->vwv + 0, 1, 0);
109 SSVAL(state->vwv + 1, 0, 0);
110 SSVAL(state->vwv + 2, 0, fnum);
111 SIVAL(state->vwv + 3, 0, offset);
112 SSVAL(state->vwv + 5, 0, size);
113 SSVAL(state->vwv + 6, 0, size);
114 SSVAL(state->vwv + 7, 0, (size >> 16));
115 SSVAL(state->vwv + 8, 0, 0);
116 SSVAL(state->vwv + 9, 0, 0);
118 if ((uint64_t)offset >> 32) {
119 SIVAL(state->vwv + 10, 0,
120 (((uint64_t)offset)>>32) & 0xffffffff);
124 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
125 state->vwv, 0, NULL);
126 if (subreq == NULL) {
130 tevent_req_set_callback(subreq, cli_read_andx_done, req);
135 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
136 struct event_context *ev,
137 struct cli_state *cli, uint16_t fnum,
138 off_t offset, size_t size)
140 struct tevent_req *req, *subreq;
143 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
149 status = cli_smb_req_send(subreq);
150 if (!NT_STATUS_IS_OK(status)) {
151 tevent_req_nterror(req, status);
152 return tevent_req_post(req, ev);
157 static void cli_read_andx_done(struct tevent_req *subreq)
159 struct tevent_req *req = tevent_req_callback_data(
160 subreq, struct tevent_req);
161 struct cli_read_andx_state *state = tevent_req_data(
162 req, struct cli_read_andx_state);
169 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
172 if (NT_STATUS_IS_ERR(state->status)) {
173 tevent_req_nterror(req, state->status);
177 /* size is the number of bytes the server returned.
179 state->received = SVAL(vwv + 5, 0);
180 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
182 if (state->received > state->size) {
183 DEBUG(5,("server returned more than we wanted!\n"));
184 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
189 * bcc field must be valid for small reads, for large reads the 16-bit
190 * bcc field can't be correct.
193 if ((state->received < 0xffff) && (state->received > num_bytes)) {
194 DEBUG(5, ("server announced more bytes than sent\n"));
195 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
199 state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
201 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
202 || ((state->received != 0) && (state->buf < bytes))) {
203 DEBUG(5, ("server returned invalid read&x data offset\n"));
204 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
207 tevent_req_done(req);
211 * Pull the data out of a finished async read_and_x request. rcvbuf is
212 * talloced from the request, so better make sure that you copy it away before
213 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
217 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
220 struct cli_read_andx_state *state = tevent_req_data(
221 req, struct cli_read_andx_state);
224 if (tevent_req_is_nterror(req, &status)) {
227 *received = state->received;
228 *rcvbuf = state->buf;
232 struct cli_readall_state {
233 struct tevent_context *ev;
234 struct cli_state *cli;
242 static void cli_readall_done(struct tevent_req *subreq);
244 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
245 struct event_context *ev,
246 struct cli_state *cli,
248 off_t offset, size_t size)
250 struct tevent_req *req, *subreq;
251 struct cli_readall_state *state;
253 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
260 state->start_offset = offset;
265 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
266 if (tevent_req_nomem(subreq, req)) {
267 return tevent_req_post(req, ev);
269 tevent_req_set_callback(subreq, cli_readall_done, req);
273 static void cli_readall_done(struct tevent_req *subreq)
275 struct tevent_req *req = tevent_req_callback_data(
276 subreq, struct tevent_req);
277 struct cli_readall_state *state = tevent_req_data(
278 req, struct cli_readall_state);
283 status = cli_read_andx_recv(subreq, &received, &buf);
284 if (!NT_STATUS_IS_OK(status)) {
285 tevent_req_nterror(req, status);
291 tevent_req_done(req);
295 if ((state->received == 0) && (received == state->size)) {
296 /* Ideal case: Got it all in one run */
298 state->received += received;
299 tevent_req_done(req);
304 * We got a short read, issue a read for the
305 * rest. Unfortunately we have to allocate the buffer
306 * ourselves now, as our caller expects to receive a single
307 * buffer. cli_read_andx does it from the buffer received from
308 * the net, but with a short read we have to put it together
309 * from several reads.
312 if (state->buf == NULL) {
313 state->buf = talloc_array(state, uint8_t, state->size);
314 if (tevent_req_nomem(state->buf, req)) {
318 memcpy(state->buf + state->received, buf, received);
319 state->received += received;
323 if (state->received >= state->size) {
324 tevent_req_done(req);
328 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
329 state->start_offset + state->received,
330 state->size - state->received);
331 if (tevent_req_nomem(subreq, req)) {
334 tevent_req_set_callback(subreq, cli_readall_done, req);
337 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
340 struct cli_readall_state *state = tevent_req_data(
341 req, struct cli_readall_state);
344 if (tevent_req_is_nterror(req, &status)) {
347 *received = state->received;
348 *rcvbuf = state->buf;
352 struct cli_pull_subreq {
353 struct tevent_req *req;
359 * Parallel read support.
361 * cli_pull sends as many read&x requests as the server would allow via
362 * max_mux at a time. When replies flow back in, the data is written into
363 * the callback function "sink" in the right order.
366 struct cli_pull_state {
367 struct tevent_req *req;
369 struct event_context *ev;
370 struct cli_state *cli;
375 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
381 * Outstanding requests
384 struct cli_pull_subreq *reqs;
387 * For how many bytes did we send requests already?
392 * Next request index to push into "sink". This walks around the "req"
393 * array, taking care that the requests are pushed to "sink" in the
394 * right order. If necessary (i.e. replies don't come in in the right
395 * order), replies are held back in "reqs".
400 * How many bytes did we push into "sink"?
406 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
408 struct cli_pull_state *state = tevent_req_data(
409 req, struct cli_pull_state);
412 result = tevent_req_default_print(req, mem_ctx);
413 if (result == NULL) {
417 return talloc_asprintf_append_buffer(
418 result, "num_reqs=%d, top_req=%d",
419 state->num_reqs, state->top_req);
422 static void cli_pull_read_done(struct tevent_req *read_req);
425 * Prepare an async pull request
428 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
429 struct event_context *ev,
430 struct cli_state *cli,
431 uint16_t fnum, off_t start_offset,
432 SMB_OFF_T size, size_t window_size,
433 NTSTATUS (*sink)(char *buf, size_t n,
437 struct tevent_req *req;
438 struct cli_pull_state *state;
441 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
445 tevent_req_set_print_fn(req, cli_pull_print);
451 state->start_offset = start_offset;
460 tevent_req_done(req);
461 return tevent_req_post(req, ev);
464 state->chunk_size = cli_read_max_bufsize(cli);
466 state->num_reqs = MAX(window_size/state->chunk_size, 1);
467 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
469 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
471 if (state->reqs == NULL) {
475 state->requested = 0;
477 for (i=0; i<state->num_reqs; i++) {
478 struct cli_pull_subreq *subreq = &state->reqs[i];
480 size_t request_thistime;
482 if (state->requested >= size) {
487 size_left = size - state->requested;
488 request_thistime = MIN(size_left, state->chunk_size);
490 subreq->req = cli_readall_send(
491 state->reqs, ev, cli, fnum,
492 state->start_offset + state->requested,
495 if (subreq->req == NULL) {
498 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
499 state->requested += request_thistime;
509 * Handle incoming read replies, push the data into sink and send out new
510 * requests if necessary.
513 static void cli_pull_read_done(struct tevent_req *subreq)
515 struct tevent_req *req = tevent_req_callback_data(
516 subreq, struct tevent_req);
517 struct cli_pull_state *state = tevent_req_data(
518 req, struct cli_pull_state);
519 struct cli_pull_subreq *pull_subreq = NULL;
523 for (i = 0; i < state->num_reqs; i++) {
524 pull_subreq = &state->reqs[i];
525 if (subreq == pull_subreq->req) {
529 if (i == state->num_reqs) {
530 /* Huh -- received something we did not send?? */
531 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
535 status = cli_readall_recv(subreq, &pull_subreq->received,
537 if (!NT_STATUS_IS_OK(status)) {
538 tevent_req_nterror(state->req, status);
543 * This loop is the one to take care of out-of-order replies. All
544 * pending requests are in state->reqs, state->reqs[top_req] is the
545 * one that is to be pushed next. If however a request later than
546 * top_req is replied to, then we can't push yet. If top_req is
547 * replied to at a later point then, we need to push all the finished
551 while (state->reqs[state->top_req].req != NULL) {
552 struct cli_pull_subreq *top_subreq;
554 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
557 top_subreq = &state->reqs[state->top_req];
559 if (tevent_req_is_in_progress(top_subreq->req)) {
560 DEBUG(11, ("cli_pull_read_done: top request not yet "
565 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
566 "pushed\n", (int)top_subreq->received,
567 (int)state->pushed));
569 status = state->sink((char *)top_subreq->buf,
570 top_subreq->received, state->priv);
571 if (!NT_STATUS_IS_OK(status)) {
572 tevent_req_nterror(state->req, status);
575 state->pushed += top_subreq->received;
577 TALLOC_FREE(state->reqs[state->top_req].req);
579 if (state->requested < state->size) {
580 struct tevent_req *new_req;
582 size_t request_thistime;
584 size_left = state->size - state->requested;
585 request_thistime = MIN(size_left, state->chunk_size);
587 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
588 "at %d, position %d\n",
589 (int)request_thistime,
590 (int)(state->start_offset
594 new_req = cli_readall_send(
595 state->reqs, state->ev, state->cli,
597 state->start_offset + state->requested,
600 if (tevent_req_nomem(new_req, state->req)) {
603 tevent_req_set_callback(new_req, cli_pull_read_done,
606 state->reqs[state->top_req].req = new_req;
607 state->requested += request_thistime;
610 state->top_req = (state->top_req+1) % state->num_reqs;
613 tevent_req_done(req);
616 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
618 struct cli_pull_state *state = tevent_req_data(
619 req, struct cli_pull_state);
622 if (tevent_req_is_nterror(req, &status)) {
625 *received = state->pushed;
629 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
630 off_t start_offset, SMB_OFF_T size, size_t window_size,
631 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
632 void *priv, SMB_OFF_T *received)
634 TALLOC_CTX *frame = talloc_stackframe();
635 struct event_context *ev;
636 struct tevent_req *req;
637 NTSTATUS status = NT_STATUS_OK;
639 if (cli_has_async_calls(cli)) {
641 * Can't use sync call while an async call is in flight
643 status = NT_STATUS_INVALID_PARAMETER;
647 ev = event_context_init(frame);
649 status = NT_STATUS_NO_MEMORY;
653 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
654 window_size, sink, priv);
656 status = NT_STATUS_NO_MEMORY;
660 if (!tevent_req_poll(req, ev)) {
661 status = map_nt_error_from_unix(errno);
665 status = cli_pull_recv(req, received);
668 if (!NT_STATUS_IS_OK(status)) {
669 cli_set_error(cli, status);
674 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
676 char **pbuf = (char **)priv;
677 memcpy(*pbuf, buf, n);
682 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
683 off_t offset, size_t size)
688 status = cli_pull(cli, fnum, offset, size, size,
689 cli_read_sink, &buf, &ret);
690 if (!NT_STATUS_IS_OK(status)) {
691 cli_set_error(cli, status);
697 /****************************************************************************
698 Issue a single SMBwrite and don't wait for a reply.
699 ****************************************************************************/
701 static bool cli_issue_write(struct cli_state *cli,
709 bool large_writex = false;
710 /* We can only do direct writes if not signing and not encrypting. */
711 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
713 if (!direct_writes && size + 1 > cli->bufsize) {
714 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
718 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
719 if (cli->inbuf == NULL) {
720 SAFE_FREE(cli->outbuf);
723 cli->bufsize = size + 1024;
726 memset(cli->outbuf,'\0',smb_size);
727 memset(cli->inbuf,'\0',smb_size);
729 if (cli->capabilities & CAP_LARGE_FILES) {
734 cli_set_message(cli->outbuf,14,0,True);
736 cli_set_message(cli->outbuf,12,0,True);
739 SCVAL(cli->outbuf,smb_com,SMBwriteX);
740 SSVAL(cli->outbuf,smb_tid,cli->cnum);
741 cli_setup_packet(cli);
743 SCVAL(cli->outbuf,smb_vwv0,0xFF);
744 SSVAL(cli->outbuf,smb_vwv2,fnum);
746 SIVAL(cli->outbuf,smb_vwv3,offset);
747 SIVAL(cli->outbuf,smb_vwv5,0);
748 SSVAL(cli->outbuf,smb_vwv7,mode);
750 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
752 * According to CIFS-TR-1p00, this following field should only
753 * be set if CAP_LARGE_WRITEX is set. We should check this
754 * locally. However, this check might already have been
755 * done by our callers.
757 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
758 SSVAL(cli->outbuf,smb_vwv10,size);
759 /* +1 is pad byte. */
760 SSVAL(cli->outbuf,smb_vwv11,
761 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
764 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
767 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
768 *p++ = '\0'; /* pad byte. */
769 if (!direct_writes) {
770 memcpy(p, buf, size);
772 if (size > 0x1FFFF) {
773 /* This is a POSIX 14 word large write. */
774 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
775 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
777 cli_setup_bcc(cli, p+size);
780 show_msg(cli->outbuf);
782 /* For direct writes we now need to write the data
783 * directly out of buf. */
784 return cli_send_smb_direct_writeX(cli, buf, size);
786 return cli_send_smb(cli);
790 /****************************************************************************
792 write_mode: 0x0001 disallow write cacheing
793 0x0002 return bytes remaining
794 0x0004 use raw named pipe protocol
795 0x0008 start of message mode named pipe protocol
796 ****************************************************************************/
798 ssize_t cli_write(struct cli_state *cli,
799 uint16_t fnum, uint16 write_mode,
800 const char *buf, off_t offset, size_t size)
802 ssize_t bwritten = 0;
803 unsigned int issued = 0;
804 unsigned int received = 0;
809 if(cli->max_mux > 1) {
810 mpx = cli->max_mux-1;
815 writesize = cli_write_max_bufsize(cli, write_mode);
817 blocks = (size + (writesize-1)) / writesize;
819 while (received < blocks) {
821 while ((issued - received < mpx) && (issued < blocks)) {
822 ssize_t bsent = issued * writesize;
823 ssize_t size1 = MIN(writesize, size - bsent);
825 if (!cli_issue_write(cli, fnum, offset + bsent,
833 if (!cli_receive_smb(cli)) {
839 if (cli_is_error(cli))
842 bwritten += SVAL(cli->inbuf, smb_vwv2);
843 if (writesize > 0xFFFF) {
844 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
848 while (received < issued && cli_receive_smb(cli)) {
855 /****************************************************************************
856 write to a file using a SMBwrite and not bypassing 0 byte writes
857 ****************************************************************************/
859 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
860 off_t offset, size_t size1, size_t *ptotal)
869 bytes = TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
871 return NT_STATUS_NO_MEMORY;
876 size_t size = MIN(size1, cli->max_xmit - 48);
877 struct tevent_req *req;
882 SSVAL(vwv+0, 0, fnum);
883 SSVAL(vwv+1, 0, size);
884 SIVAL(vwv+2, 0, offset);
887 bytes = TALLOC_REALLOC_ARRAY(talloc_tos(), bytes, uint8_t,
890 return NT_STATUS_NO_MEMORY;
892 SSVAL(bytes, 1, size);
893 memcpy(bytes + 3, buf + total, size);
895 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
896 size+3, bytes, &req, 1, NULL, &ret_vwv,
898 if (!NT_STATUS_IS_OK(status)) {
903 size = SVAL(ret_vwv+0, 0);
916 if (ptotal != NULL) {
923 * Send a write&x request
926 struct cli_write_andx_state {
934 static void cli_write_andx_done(struct tevent_req *subreq);
936 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
937 struct event_context *ev,
938 struct cli_state *cli, uint16_t fnum,
939 uint16_t mode, const uint8_t *buf,
940 off_t offset, size_t size,
941 struct tevent_req **reqs_before,
943 struct tevent_req **psmbreq)
945 struct tevent_req *req, *subreq;
946 struct cli_write_andx_state *state;
947 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
948 uint8_t wct = bigoffset ? 14 : 12;
949 size_t max_write = cli_write_max_bufsize(cli, mode);
952 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
957 size = MIN(size, max_write);
961 SCVAL(vwv+0, 0, 0xFF);
964 SSVAL(vwv+2, 0, fnum);
965 SIVAL(vwv+3, 0, offset);
967 SSVAL(vwv+7, 0, mode);
969 SSVAL(vwv+9, 0, (size>>16));
970 SSVAL(vwv+10, 0, size);
973 cli_smb_wct_ofs(reqs_before, num_reqs_before)
974 + 1 /* the wct field */
976 + 2 /* num_bytes field */
980 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
984 state->iov[0].iov_base = (void *)&state->pad;
985 state->iov[0].iov_len = 1;
986 state->iov[1].iov_base = CONST_DISCARD(void *, buf);
987 state->iov[1].iov_len = size;
989 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
991 if (tevent_req_nomem(subreq, req)) {
992 return tevent_req_post(req, ev);
994 tevent_req_set_callback(subreq, cli_write_andx_done, req);
999 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
1000 struct event_context *ev,
1001 struct cli_state *cli, uint16_t fnum,
1002 uint16_t mode, const uint8_t *buf,
1003 off_t offset, size_t size)
1005 struct tevent_req *req, *subreq;
1008 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
1009 size, NULL, 0, &subreq);
1014 status = cli_smb_req_send(subreq);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 tevent_req_nterror(req, status);
1017 return tevent_req_post(req, ev);
1022 static void cli_write_andx_done(struct tevent_req *subreq)
1024 struct tevent_req *req = tevent_req_callback_data(
1025 subreq, struct tevent_req);
1026 struct cli_write_andx_state *state = tevent_req_data(
1027 req, struct cli_write_andx_state);
1033 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
1035 TALLOC_FREE(subreq);
1036 if (NT_STATUS_IS_ERR(status)) {
1037 tevent_req_nterror(req, status);
1040 state->written = SVAL(vwv+2, 0);
1041 state->written |= SVAL(vwv+4, 0)<<16;
1042 tevent_req_done(req);
1045 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
1047 struct cli_write_andx_state *state = tevent_req_data(
1048 req, struct cli_write_andx_state);
1051 if (tevent_req_is_nterror(req, &status)) {
1054 *pwritten = state->written;
1055 return NT_STATUS_OK;
1058 struct cli_writeall_state {
1059 struct event_context *ev;
1060 struct cli_state *cli;
1069 static void cli_writeall_written(struct tevent_req *req);
1071 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
1072 struct event_context *ev,
1073 struct cli_state *cli,
1077 off_t offset, size_t size)
1079 struct tevent_req *req, *subreq;
1080 struct cli_writeall_state *state;
1082 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
1091 state->offset = offset;
1095 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1096 state->mode, state->buf, state->offset,
1098 if (tevent_req_nomem(subreq, req)) {
1099 return tevent_req_post(req, ev);
1101 tevent_req_set_callback(subreq, cli_writeall_written, req);
1105 static void cli_writeall_written(struct tevent_req *subreq)
1107 struct tevent_req *req = tevent_req_callback_data(
1108 subreq, struct tevent_req);
1109 struct cli_writeall_state *state = tevent_req_data(
1110 req, struct cli_writeall_state);
1112 size_t written, to_write;
1114 status = cli_write_andx_recv(subreq, &written);
1115 TALLOC_FREE(subreq);
1116 if (!NT_STATUS_IS_OK(status)) {
1117 tevent_req_nterror(req, status);
1121 state->written += written;
1123 if (state->written > state->size) {
1124 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1128 to_write = state->size - state->written;
1130 if (to_write == 0) {
1131 tevent_req_done(req);
1135 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1137 state->buf + state->written,
1138 state->offset + state->written, to_write);
1139 if (tevent_req_nomem(subreq, req)) {
1142 tevent_req_set_callback(subreq, cli_writeall_written, req);
1145 static NTSTATUS cli_writeall_recv(struct tevent_req *req)
1147 return tevent_req_simple_recv_ntstatus(req);
1150 struct cli_push_write_state {
1151 struct tevent_req *req;/* This is the main request! Not the subreq */
1158 struct cli_push_state {
1159 struct event_context *ev;
1160 struct cli_state *cli;
1166 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1175 * Outstanding requests
1179 struct cli_push_write_state **reqs;
1182 static void cli_push_written(struct tevent_req *req);
1184 static bool cli_push_write_setup(struct tevent_req *req,
1185 struct cli_push_state *state,
1188 struct cli_push_write_state *substate;
1189 struct tevent_req *subreq;
1191 substate = talloc(state->reqs, struct cli_push_write_state);
1195 substate->req = req;
1196 substate->idx = idx;
1197 substate->ofs = state->next_offset;
1198 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1199 if (!substate->buf) {
1200 talloc_free(substate);
1203 substate->size = state->source(substate->buf,
1206 if (substate->size == 0) {
1208 /* nothing to send */
1209 talloc_free(substate);
1213 subreq = cli_writeall_send(substate,
1214 state->ev, state->cli,
1215 state->fnum, state->mode,
1220 talloc_free(substate);
1223 tevent_req_set_callback(subreq, cli_push_written, substate);
1225 state->reqs[idx] = substate;
1226 state->pending += 1;
1227 state->next_offset += substate->size;
1232 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1233 struct cli_state *cli,
1234 uint16_t fnum, uint16_t mode,
1235 off_t start_offset, size_t window_size,
1236 size_t (*source)(uint8_t *buf, size_t n,
1240 struct tevent_req *req;
1241 struct cli_push_state *state;
1244 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1251 state->start_offset = start_offset;
1253 state->source = source;
1257 state->next_offset = start_offset;
1259 state->chunk_size = cli_write_max_bufsize(cli, mode);
1261 if (window_size == 0) {
1262 window_size = cli->max_mux * state->chunk_size;
1264 state->num_reqs = window_size/state->chunk_size;
1265 if ((window_size % state->chunk_size) > 0) {
1266 state->num_reqs += 1;
1268 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1269 state->num_reqs = MAX(state->num_reqs, 1);
1271 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1273 if (state->reqs == NULL) {
1277 for (i=0; i<state->num_reqs; i++) {
1278 if (!cli_push_write_setup(req, state, i)) {
1287 if (state->pending == 0) {
1288 tevent_req_done(req);
1289 return tevent_req_post(req, ev);
1295 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1296 return tevent_req_post(req, ev);
1299 static void cli_push_written(struct tevent_req *subreq)
1301 struct cli_push_write_state *substate = tevent_req_callback_data(
1302 subreq, struct cli_push_write_state);
1303 struct tevent_req *req = substate->req;
1304 struct cli_push_state *state = tevent_req_data(
1305 req, struct cli_push_state);
1307 uint32_t idx = substate->idx;
1309 state->reqs[idx] = NULL;
1310 state->pending -= 1;
1312 status = cli_writeall_recv(subreq);
1313 TALLOC_FREE(subreq);
1314 TALLOC_FREE(substate);
1315 if (!NT_STATUS_IS_OK(status)) {
1316 tevent_req_nterror(req, status);
1321 if (!cli_push_write_setup(req, state, idx)) {
1322 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1327 if (state->pending == 0) {
1328 tevent_req_done(req);
1333 NTSTATUS cli_push_recv(struct tevent_req *req)
1335 return tevent_req_simple_recv_ntstatus(req);
1338 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1339 off_t start_offset, size_t window_size,
1340 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1343 TALLOC_CTX *frame = talloc_stackframe();
1344 struct event_context *ev;
1345 struct tevent_req *req;
1346 NTSTATUS status = NT_STATUS_OK;
1348 if (cli_has_async_calls(cli)) {
1350 * Can't use sync call while an async call is in flight
1352 status = NT_STATUS_INVALID_PARAMETER;
1356 ev = event_context_init(frame);
1358 status = NT_STATUS_NO_MEMORY;
1362 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1363 window_size, source, priv);
1365 status = NT_STATUS_NO_MEMORY;
1369 if (!tevent_req_poll(req, ev)) {
1370 status = map_nt_error_from_unix(errno);
1374 status = cli_push_recv(req);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 cli_set_error(cli, status);