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/>.
22 /****************************************************************************
23 Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state *cli)
27 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
28 && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
29 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
31 if (cli->capabilities & CAP_LARGE_READX) {
33 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
34 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
36 return (cli->max_xmit - (smb_size+32)) & ~1023;
39 /****************************************************************************
40 Calculate the recommended write buffer size
41 ****************************************************************************/
42 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
44 if (write_mode == 0 &&
45 !client_is_signing_on(cli) &&
46 !cli_encryption_on(cli) &&
47 (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
48 (cli->capabilities & CAP_LARGE_FILES)) {
49 /* Only do massive writes if we can do them direct
50 * with no signing or encrypting - not on a pipe. */
51 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
55 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
58 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
59 || client_is_signing_on(cli)
60 || strequal(cli->dev, "LPT1:")) {
63 * Printer devices are restricted to max_xmit writesize in
64 * Vista and XPSP3 as are signing connections.
67 return (cli->max_xmit - (smb_size+32)) & ~1023;
70 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
73 struct cli_read_andx_state {
81 static void cli_read_andx_done(struct tevent_req *subreq);
83 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
84 struct event_context *ev,
85 struct cli_state *cli, uint16_t fnum,
86 off_t offset, size_t size,
87 struct tevent_req **psmbreq)
89 struct tevent_req *req, *subreq;
90 struct cli_read_andx_state *state;
91 bool bigoffset = False;
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) {
120 SIVAL(state->vwv + 10, 0,
121 (((uint64_t)offset)>>32) & 0xffffffff);
125 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
126 state->vwv, 0, NULL);
127 if (subreq == NULL) {
131 tevent_req_set_callback(subreq, cli_read_andx_done, req);
136 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
137 struct event_context *ev,
138 struct cli_state *cli, uint16_t fnum,
139 off_t offset, size_t size)
141 struct tevent_req *req, *subreq;
144 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
150 status = cli_smb_req_send(subreq);
151 if (!NT_STATUS_IS_OK(status)) {
152 tevent_req_nterror(req, status);
153 return tevent_req_post(req, ev);
158 static void cli_read_andx_done(struct tevent_req *subreq)
160 struct tevent_req *req = tevent_req_callback_data(
161 subreq, struct tevent_req);
162 struct cli_read_andx_state *state = tevent_req_data(
163 req, struct cli_read_andx_state);
170 state->status = cli_smb_recv(subreq, 12, &wct, &vwv, &num_bytes,
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 inbuf = cli_smb_inbuf(subreq);
200 state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
202 if (trans_oob(smb_len_large(inbuf), SVAL(vwv+6, 0), state->received)
203 || ((state->received != 0) && (state->buf < bytes))) {
204 DEBUG(5, ("server returned invalid read&x data offset\n"));
205 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
208 tevent_req_done(req);
212 * Pull the data out of a finished async read_and_x request. rcvbuf is
213 * talloced from the request, so better make sure that you copy it away before
214 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
218 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
221 struct cli_read_andx_state *state = tevent_req_data(
222 req, struct cli_read_andx_state);
225 if (tevent_req_is_nterror(req, &status)) {
228 *received = state->received;
229 *rcvbuf = state->buf;
233 struct cli_readall_state {
234 struct tevent_context *ev;
235 struct cli_state *cli;
243 static void cli_readall_done(struct tevent_req *subreq);
245 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
246 struct event_context *ev,
247 struct cli_state *cli,
249 off_t offset, size_t size)
251 struct tevent_req *req, *subreq;
252 struct cli_readall_state *state;
254 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
261 state->start_offset = offset;
266 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
267 if (tevent_req_nomem(subreq, req)) {
268 return tevent_req_post(req, ev);
270 tevent_req_set_callback(subreq, cli_readall_done, req);
274 static void cli_readall_done(struct tevent_req *subreq)
276 struct tevent_req *req = tevent_req_callback_data(
277 subreq, struct tevent_req);
278 struct cli_readall_state *state = tevent_req_data(
279 req, struct cli_readall_state);
284 status = cli_read_andx_recv(subreq, &received, &buf);
285 if (!NT_STATUS_IS_OK(status)) {
286 tevent_req_nterror(req, status);
292 tevent_req_done(req);
296 if ((state->received == 0) && (received == state->size)) {
297 /* Ideal case: Got it all in one run */
299 state->received += received;
300 tevent_req_done(req);
305 * We got a short read, issue a read for the
306 * rest. Unfortunately we have to allocate the buffer
307 * ourselves now, as our caller expects to receive a single
308 * buffer. cli_read_andx does it from the buffer received from
309 * the net, but with a short read we have to put it together
310 * from several reads.
313 if (state->buf == NULL) {
314 state->buf = talloc_array(state, uint8_t, state->size);
315 if (tevent_req_nomem(state->buf, req)) {
319 memcpy(state->buf + state->received, buf, received);
320 state->received += received;
324 if (state->received >= state->size) {
325 tevent_req_done(req);
329 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
330 state->start_offset + state->received,
331 state->size - state->received);
332 if (tevent_req_nomem(subreq, req)) {
335 tevent_req_set_callback(subreq, cli_readall_done, req);
338 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
341 struct cli_readall_state *state = tevent_req_data(
342 req, struct cli_readall_state);
345 if (tevent_req_is_nterror(req, &status)) {
348 *received = state->received;
349 *rcvbuf = state->buf;
353 struct cli_pull_subreq {
354 struct tevent_req *req;
360 * Parallel read support.
362 * cli_pull sends as many read&x requests as the server would allow via
363 * max_mux at a time. When replies flow back in, the data is written into
364 * the callback function "sink" in the right order.
367 struct cli_pull_state {
368 struct tevent_req *req;
370 struct event_context *ev;
371 struct cli_state *cli;
376 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
382 * Outstanding requests
385 struct cli_pull_subreq *reqs;
388 * For how many bytes did we send requests already?
393 * Next request index to push into "sink". This walks around the "req"
394 * array, taking care that the requests are pushed to "sink" in the
395 * right order. If necessary (i.e. replies don't come in in the right
396 * order), replies are held back in "reqs".
401 * How many bytes did we push into "sink"?
407 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
409 struct cli_pull_state *state = tevent_req_data(
410 req, struct cli_pull_state);
413 result = tevent_req_print(mem_ctx, req);
414 if (result == NULL) {
418 return talloc_asprintf_append_buffer(
419 result, "num_reqs=%d, top_req=%d",
420 state->num_reqs, state->top_req);
423 static void cli_pull_read_done(struct tevent_req *read_req);
426 * Prepare an async pull request
429 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
430 struct event_context *ev,
431 struct cli_state *cli,
432 uint16_t fnum, off_t start_offset,
433 SMB_OFF_T size, size_t window_size,
434 NTSTATUS (*sink)(char *buf, size_t n,
438 struct tevent_req *req;
439 struct cli_pull_state *state;
442 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
446 tevent_req_set_print_fn(req, cli_pull_print);
452 state->start_offset = start_offset;
461 tevent_req_done(req);
462 return tevent_req_post(req, ev);
465 state->chunk_size = cli_read_max_bufsize(cli);
467 state->num_reqs = MAX(window_size/state->chunk_size, 1);
468 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
470 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
472 if (state->reqs == NULL) {
476 state->requested = 0;
478 for (i=0; i<state->num_reqs; i++) {
479 struct cli_pull_subreq *subreq = &state->reqs[i];
481 size_t request_thistime;
483 if (state->requested >= size) {
488 size_left = size - state->requested;
489 request_thistime = MIN(size_left, state->chunk_size);
491 subreq->req = cli_readall_send(
492 state->reqs, ev, cli, fnum,
493 state->start_offset + state->requested,
496 if (subreq->req == NULL) {
499 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
500 state->requested += request_thistime;
510 * Handle incoming read replies, push the data into sink and send out new
511 * requests if necessary.
514 static void cli_pull_read_done(struct tevent_req *subreq)
516 struct tevent_req *req = tevent_req_callback_data(
517 subreq, struct tevent_req);
518 struct cli_pull_state *state = tevent_req_data(
519 req, struct cli_pull_state);
520 struct cli_pull_subreq *pull_subreq = NULL;
524 for (i = 0; i < state->num_reqs; i++) {
525 pull_subreq = &state->reqs[i];
526 if (subreq == pull_subreq->req) {
530 if (i == state->num_reqs) {
531 /* Huh -- received something we did not send?? */
532 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
536 status = cli_readall_recv(subreq, &pull_subreq->received,
538 if (!NT_STATUS_IS_OK(status)) {
539 tevent_req_nterror(state->req, status);
544 * This loop is the one to take care of out-of-order replies. All
545 * pending requests are in state->reqs, state->reqs[top_req] is the
546 * one that is to be pushed next. If however a request later than
547 * top_req is replied to, then we can't push yet. If top_req is
548 * replied to at a later point then, we need to push all the finished
552 while (state->reqs[state->top_req].req != NULL) {
553 struct cli_pull_subreq *top_subreq;
555 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
558 top_subreq = &state->reqs[state->top_req];
560 if (tevent_req_is_in_progress(top_subreq->req)) {
561 DEBUG(11, ("cli_pull_read_done: top request not yet "
566 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
567 "pushed\n", (int)top_subreq->received,
568 (int)state->pushed));
570 status = state->sink((char *)top_subreq->buf,
571 top_subreq->received, state->priv);
572 if (!NT_STATUS_IS_OK(status)) {
573 tevent_req_nterror(state->req, status);
576 state->pushed += top_subreq->received;
578 TALLOC_FREE(state->reqs[state->top_req].req);
580 if (state->requested < state->size) {
581 struct tevent_req *new_req;
583 size_t request_thistime;
585 size_left = state->size - state->requested;
586 request_thistime = MIN(size_left, state->chunk_size);
588 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
589 "at %d, position %d\n",
590 (int)request_thistime,
591 (int)(state->start_offset
595 new_req = cli_readall_send(
596 state->reqs, state->ev, state->cli,
598 state->start_offset + state->requested,
601 if (tevent_req_nomem(new_req, state->req)) {
604 tevent_req_set_callback(new_req, cli_pull_read_done,
607 state->reqs[state->top_req].req = new_req;
608 state->requested += request_thistime;
611 state->top_req = (state->top_req+1) % state->num_reqs;
614 tevent_req_done(req);
617 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
619 struct cli_pull_state *state = tevent_req_data(
620 req, struct cli_pull_state);
623 if (tevent_req_is_nterror(req, &status)) {
626 *received = state->pushed;
630 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
631 off_t start_offset, SMB_OFF_T size, size_t window_size,
632 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
633 void *priv, SMB_OFF_T *received)
635 TALLOC_CTX *frame = talloc_stackframe();
636 struct event_context *ev;
637 struct tevent_req *req;
638 NTSTATUS status = NT_STATUS_OK;
640 if (cli_has_async_calls(cli)) {
642 * Can't use sync call while an async call is in flight
644 status = NT_STATUS_INVALID_PARAMETER;
648 ev = event_context_init(frame);
650 status = NT_STATUS_NO_MEMORY;
654 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
655 window_size, sink, priv);
657 status = NT_STATUS_NO_MEMORY;
661 if (!tevent_req_poll(req, ev)) {
662 status = map_nt_error_from_unix(errno);
666 status = cli_pull_recv(req, received);
669 if (!NT_STATUS_IS_OK(status)) {
670 cli_set_error(cli, status);
675 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
677 char **pbuf = (char **)priv;
678 memcpy(*pbuf, buf, n);
683 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
684 off_t offset, size_t size)
689 status = cli_pull(cli, fnum, offset, size, size,
690 cli_read_sink, &buf, &ret);
691 if (!NT_STATUS_IS_OK(status)) {
692 cli_set_error(cli, status);
698 /****************************************************************************
699 Issue a single SMBwrite and don't wait for a reply.
700 ****************************************************************************/
702 static bool cli_issue_write(struct cli_state *cli,
710 bool large_writex = false;
711 /* We can only do direct writes if not signing and not encrypting. */
712 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
714 if (!direct_writes && size + 1 > cli->bufsize) {
715 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
719 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
720 if (cli->inbuf == NULL) {
721 SAFE_FREE(cli->outbuf);
724 cli->bufsize = size + 1024;
727 memset(cli->outbuf,'\0',smb_size);
728 memset(cli->inbuf,'\0',smb_size);
730 if (cli->capabilities & CAP_LARGE_FILES) {
735 cli_set_message(cli->outbuf,14,0,True);
737 cli_set_message(cli->outbuf,12,0,True);
740 SCVAL(cli->outbuf,smb_com,SMBwriteX);
741 SSVAL(cli->outbuf,smb_tid,cli->cnum);
742 cli_setup_packet(cli);
744 SCVAL(cli->outbuf,smb_vwv0,0xFF);
745 SSVAL(cli->outbuf,smb_vwv2,fnum);
747 SIVAL(cli->outbuf,smb_vwv3,offset);
748 SIVAL(cli->outbuf,smb_vwv5,0);
749 SSVAL(cli->outbuf,smb_vwv7,mode);
751 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
753 * According to CIFS-TR-1p00, this following field should only
754 * be set if CAP_LARGE_WRITEX is set. We should check this
755 * locally. However, this check might already have been
756 * done by our callers.
758 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
759 SSVAL(cli->outbuf,smb_vwv10,size);
760 /* +1 is pad byte. */
761 SSVAL(cli->outbuf,smb_vwv11,
762 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
765 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
768 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
769 *p++ = '\0'; /* pad byte. */
770 if (!direct_writes) {
771 memcpy(p, buf, size);
773 if (size > 0x1FFFF) {
774 /* This is a POSIX 14 word large write. */
775 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
776 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
778 cli_setup_bcc(cli, p+size);
781 show_msg(cli->outbuf);
783 /* For direct writes we now need to write the data
784 * directly out of buf. */
785 return cli_send_smb_direct_writeX(cli, buf, size);
787 return cli_send_smb(cli);
791 /****************************************************************************
793 write_mode: 0x0001 disallow write cacheing
794 0x0002 return bytes remaining
795 0x0004 use raw named pipe protocol
796 0x0008 start of message mode named pipe protocol
797 ****************************************************************************/
799 ssize_t cli_write(struct cli_state *cli,
800 uint16_t fnum, uint16 write_mode,
801 const char *buf, off_t offset, size_t size)
803 ssize_t bwritten = 0;
804 unsigned int issued = 0;
805 unsigned int received = 0;
810 if(cli->max_mux > 1) {
811 mpx = cli->max_mux-1;
816 writesize = cli_write_max_bufsize(cli, write_mode);
818 blocks = (size + (writesize-1)) / writesize;
820 while (received < blocks) {
822 while ((issued - received < mpx) && (issued < blocks)) {
823 ssize_t bsent = issued * writesize;
824 ssize_t size1 = MIN(writesize, size - bsent);
826 if (!cli_issue_write(cli, fnum, offset + bsent,
834 if (!cli_receive_smb(cli)) {
840 if (cli_is_error(cli))
843 bwritten += SVAL(cli->inbuf, smb_vwv2);
844 if (writesize > 0xFFFF) {
845 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
849 while (received < issued && cli_receive_smb(cli)) {
856 /****************************************************************************
857 write to a file using a SMBwrite and not bypassing 0 byte writes
858 ****************************************************************************/
860 ssize_t cli_smbwrite(struct cli_state *cli,
861 uint16_t fnum, char *buf, off_t offset, size_t size1)
867 size_t size = MIN(size1, cli->max_xmit - 48);
869 memset(cli->outbuf,'\0',smb_size);
870 memset(cli->inbuf,'\0',smb_size);
872 cli_set_message(cli->outbuf,5, 0,True);
874 SCVAL(cli->outbuf,smb_com,SMBwrite);
875 SSVAL(cli->outbuf,smb_tid,cli->cnum);
876 cli_setup_packet(cli);
878 SSVAL(cli->outbuf,smb_vwv0,fnum);
879 SSVAL(cli->outbuf,smb_vwv1,size);
880 SIVAL(cli->outbuf,smb_vwv2,offset);
881 SSVAL(cli->outbuf,smb_vwv4,0);
883 p = smb_buf(cli->outbuf);
885 SSVAL(p, 0, size); p += 2;
886 memcpy(p, buf + total, size); p += size;
888 cli_setup_bcc(cli, p);
890 if (!cli_send_smb(cli))
893 if (!cli_receive_smb(cli))
896 if (cli_is_error(cli))
899 size = SVAL(cli->inbuf,smb_vwv0);
913 * Send a write&x request
916 struct cli_write_andx_state {
924 static void cli_write_andx_done(struct tevent_req *subreq);
926 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
927 struct event_context *ev,
928 struct cli_state *cli, uint16_t fnum,
929 uint16_t mode, const uint8_t *buf,
930 off_t offset, size_t size,
931 struct tevent_req **reqs_before,
933 struct tevent_req **psmbreq)
935 struct tevent_req *req, *subreq;
936 struct cli_write_andx_state *state;
937 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
938 uint8_t wct = bigoffset ? 14 : 12;
939 size_t max_write = cli_write_max_bufsize(cli, mode);
942 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
947 state->size = MIN(size, max_write);
951 SCVAL(vwv+0, 0, 0xFF);
954 SSVAL(vwv+2, 0, fnum);
955 SIVAL(vwv+3, 0, offset);
957 SSVAL(vwv+7, 0, mode);
959 SSVAL(vwv+9, 0, (state->size>>16));
960 SSVAL(vwv+10, 0, state->size);
963 cli_smb_wct_ofs(reqs_before, num_reqs_before)
964 + 1 /* the wct field */
966 + 2 /* num_bytes field */
970 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
974 state->iov[0].iov_base = (void *)&state->pad;
975 state->iov[0].iov_len = 1;
976 state->iov[1].iov_base = CONST_DISCARD(void *, buf);
977 state->iov[1].iov_len = state->size;
979 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
981 if (tevent_req_nomem(subreq, req)) {
982 return tevent_req_post(req, ev);
984 tevent_req_set_callback(subreq, cli_write_andx_done, req);
989 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
990 struct event_context *ev,
991 struct cli_state *cli, uint16_t fnum,
992 uint16_t mode, const uint8_t *buf,
993 off_t offset, size_t size)
995 struct tevent_req *req, *subreq;
998 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
999 size, NULL, 0, &subreq);
1004 status = cli_smb_req_send(subreq);
1005 if (!NT_STATUS_IS_OK(status)) {
1006 tevent_req_nterror(req, status);
1007 return tevent_req_post(req, ev);
1012 static void cli_write_andx_done(struct tevent_req *subreq)
1014 struct tevent_req *req = tevent_req_callback_data(
1015 subreq, struct tevent_req);
1016 struct cli_write_andx_state *state = tevent_req_data(
1017 req, struct cli_write_andx_state);
1022 status = cli_smb_recv(subreq, 6, &wct, &vwv, NULL, NULL);
1023 if (NT_STATUS_IS_ERR(status)) {
1024 TALLOC_FREE(subreq);
1025 tevent_req_nterror(req, status);
1028 state->written = SVAL(vwv+2, 0);
1029 if (state->size > UINT16_MAX) {
1031 * It is important that we only set the
1032 * high bits only if we asked for a large write.
1034 * OS/2 print shares get this wrong and may send
1039 state->written |= SVAL(vwv+4, 0)<<16;
1041 tevent_req_done(req);
1044 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
1046 struct cli_write_andx_state *state = tevent_req_data(
1047 req, struct cli_write_andx_state);
1050 if (tevent_req_is_nterror(req, &status)) {
1053 *pwritten = state->written;
1054 return NT_STATUS_OK;
1057 struct cli_writeall_state {
1058 struct event_context *ev;
1059 struct cli_state *cli;
1068 static void cli_writeall_written(struct tevent_req *req);
1070 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
1071 struct event_context *ev,
1072 struct cli_state *cli,
1076 off_t offset, size_t size)
1078 struct tevent_req *req, *subreq;
1079 struct cli_writeall_state *state;
1081 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
1090 state->offset = offset;
1094 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1095 state->mode, state->buf, state->offset,
1097 if (tevent_req_nomem(subreq, req)) {
1098 return tevent_req_post(req, ev);
1100 tevent_req_set_callback(subreq, cli_writeall_written, req);
1104 static void cli_writeall_written(struct tevent_req *subreq)
1106 struct tevent_req *req = tevent_req_callback_data(
1107 subreq, struct tevent_req);
1108 struct cli_writeall_state *state = tevent_req_data(
1109 req, struct cli_writeall_state);
1111 size_t written, to_write;
1113 status = cli_write_andx_recv(subreq, &written);
1114 TALLOC_FREE(subreq);
1115 if (!NT_STATUS_IS_OK(status)) {
1116 tevent_req_nterror(req, status);
1120 state->written += written;
1122 if (state->written > state->size) {
1123 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1127 to_write = state->size - state->written;
1129 if (to_write == 0) {
1130 tevent_req_done(req);
1134 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1136 state->buf + state->written,
1137 state->offset + state->written, to_write);
1138 if (tevent_req_nomem(subreq, req)) {
1141 tevent_req_set_callback(subreq, cli_writeall_written, req);
1144 static NTSTATUS cli_writeall_recv(struct tevent_req *req)
1146 return tevent_req_simple_recv_ntstatus(req);
1149 struct cli_push_write_state {
1150 struct tevent_req *req;/* This is the main request! Not the subreq */
1157 struct cli_push_state {
1158 struct event_context *ev;
1159 struct cli_state *cli;
1165 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1174 * Outstanding requests
1178 struct cli_push_write_state **reqs;
1181 static void cli_push_written(struct tevent_req *req);
1183 static bool cli_push_write_setup(struct tevent_req *req,
1184 struct cli_push_state *state,
1187 struct cli_push_write_state *substate;
1188 struct tevent_req *subreq;
1190 substate = talloc(state->reqs, struct cli_push_write_state);
1194 substate->req = req;
1195 substate->idx = idx;
1196 substate->ofs = state->next_offset;
1197 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1198 if (!substate->buf) {
1199 talloc_free(substate);
1202 substate->size = state->source(substate->buf,
1205 if (substate->size == 0) {
1207 /* nothing to send */
1208 talloc_free(substate);
1212 subreq = cli_writeall_send(substate,
1213 state->ev, state->cli,
1214 state->fnum, state->mode,
1219 talloc_free(substate);
1222 tevent_req_set_callback(subreq, cli_push_written, substate);
1224 state->reqs[idx] = substate;
1225 state->pending += 1;
1226 state->next_offset += substate->size;
1231 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1232 struct cli_state *cli,
1233 uint16_t fnum, uint16_t mode,
1234 off_t start_offset, size_t window_size,
1235 size_t (*source)(uint8_t *buf, size_t n,
1239 struct tevent_req *req;
1240 struct cli_push_state *state;
1243 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1250 state->start_offset = start_offset;
1252 state->source = source;
1256 state->next_offset = start_offset;
1258 state->chunk_size = cli_write_max_bufsize(cli, mode);
1260 if (window_size == 0) {
1261 window_size = cli->max_mux * state->chunk_size;
1263 state->num_reqs = window_size/state->chunk_size;
1264 if ((window_size % state->chunk_size) > 0) {
1265 state->num_reqs += 1;
1267 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1268 state->num_reqs = MAX(state->num_reqs, 1);
1270 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1272 if (state->reqs == NULL) {
1276 for (i=0; i<state->num_reqs; i++) {
1277 if (!cli_push_write_setup(req, state, i)) {
1286 if (state->pending == 0) {
1287 tevent_req_done(req);
1288 return tevent_req_post(req, ev);
1294 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1295 return tevent_req_post(req, ev);
1298 static void cli_push_written(struct tevent_req *subreq)
1300 struct cli_push_write_state *substate = tevent_req_callback_data(
1301 subreq, struct cli_push_write_state);
1302 struct tevent_req *req = substate->req;
1303 struct cli_push_state *state = tevent_req_data(
1304 req, struct cli_push_state);
1306 uint32_t idx = substate->idx;
1308 state->reqs[idx] = NULL;
1309 state->pending -= 1;
1311 status = cli_writeall_recv(subreq);
1312 TALLOC_FREE(subreq);
1313 TALLOC_FREE(substate);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 tevent_req_nterror(req, status);
1320 if (!cli_push_write_setup(req, state, idx)) {
1321 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1326 if (state->pending == 0) {
1327 tevent_req_done(req);
1332 NTSTATUS cli_push_recv(struct tevent_req *req)
1334 return tevent_req_simple_recv_ntstatus(req);
1337 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1338 off_t start_offset, size_t window_size,
1339 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1342 TALLOC_CTX *frame = talloc_stackframe();
1343 struct event_context *ev;
1344 struct tevent_req *req;
1345 NTSTATUS status = NT_STATUS_OK;
1347 if (cli_has_async_calls(cli)) {
1349 * Can't use sync call while an async call is in flight
1351 status = NT_STATUS_INVALID_PARAMETER;
1355 ev = event_context_init(frame);
1357 status = NT_STATUS_NO_MEMORY;
1361 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1362 window_size, source, priv);
1364 status = NT_STATUS_NO_MEMORY;
1368 if (!tevent_req_poll(req, ev)) {
1369 status = map_nt_error_from_unix(errno);
1373 status = cli_push_recv(req);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 cli_set_error(cli, status);