struct async_req doesn't really need to carry an event_context
[Samba.git] / source3 / libsmb / clireadwrite.c
blob1d5582e61d0e9810be779d5a24dc569a0d4467f4
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"
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) {
32 return cli->is_samba
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;
54 if (cli->is_samba) {
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;
75 * Send a read&x request
78 struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
79 struct event_context *ev,
80 struct cli_state *cli, int fnum,
81 off_t offset, size_t size)
83 struct async_req *result;
84 struct cli_request *req;
85 bool bigoffset = False;
87 uint16_t vwv[12];
88 uint8_t wct = 10;
90 if (size > cli_read_max_bufsize(cli)) {
91 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
92 "size=%d\n", (int)size,
93 (int)cli_read_max_bufsize(cli)));
94 return NULL;
97 SCVAL(vwv + 0, 0, 0xFF);
98 SCVAL(vwv + 0, 1, 0);
99 SSVAL(vwv + 1, 0, 0);
100 SSVAL(vwv + 2, 0, fnum);
101 SIVAL(vwv + 3, 0, offset);
102 SSVAL(vwv + 5, 0, size);
103 SSVAL(vwv + 6, 0, size);
104 SSVAL(vwv + 7, 0, (size >> 16));
105 SSVAL(vwv + 8, 0, 0);
106 SSVAL(vwv + 9, 0, 0);
108 if ((uint64_t)offset >> 32) {
109 bigoffset = True;
110 SIVAL(vwv + 10, 0,
111 (((uint64_t)offset)>>32) & 0xffffffff);
112 wct += 2;
115 result = cli_request_send(mem_ctx, ev, cli, SMBreadX, 0, wct, vwv, 0,
116 0, NULL);
117 if (result == NULL) {
118 return NULL;
121 req = talloc_get_type_abort(result->private_data, struct cli_request);
123 req->data.read.ofs = offset;
124 req->data.read.size = size;
125 req->data.read.received = 0;
126 req->data.read.rcvbuf = NULL;
128 return result;
132 * Pull the data out of a finished async read_and_x request. rcvbuf is
133 * talloced from the request, so better make sure that you copy it away before
134 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
135 * talloc_move it!
138 NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
139 uint8_t **rcvbuf)
141 struct cli_request *cli_req = talloc_get_type_abort(
142 req->private_data, struct cli_request);
143 uint8_t wct;
144 uint16_t *vwv;
145 uint16_t num_bytes;
146 uint8_t *bytes;
147 uint8_t *buf;
148 NTSTATUS status;
149 size_t size;
151 if (async_req_is_error(req, &status)) {
152 return status;
155 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
157 if (NT_STATUS_IS_ERR(status)) {
158 return status;
161 if (wct < 12) {
162 return NT_STATUS_INVALID_NETWORK_RESPONSE;
165 /* size is the number of bytes the server returned.
166 * Might be zero. */
167 size = SVAL(vwv + 5, 0);
168 size |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
170 if (size > cli_req->data.read.size) {
171 DEBUG(5,("server returned more than we wanted!\n"));
172 return NT_STATUS_UNEXPECTED_IO_ERROR;
176 * bcc field must be valid for small reads, for large reads the 16-bit
177 * bcc field can't be correct.
180 if ((size < 0xffff) && (size > num_bytes)) {
181 DEBUG(5, ("server announced more bytes than sent\n"));
182 return NT_STATUS_INVALID_NETWORK_RESPONSE;
185 buf = (uint8_t *)smb_base(cli_req->inbuf) + SVAL(vwv+6, 0);
187 if (trans_oob(smb_len(cli_req->inbuf), SVAL(vwv+6, 0), size)
188 || (buf < bytes)) {
189 DEBUG(5, ("server returned invalid read&x data offset\n"));
190 return NT_STATUS_INVALID_NETWORK_RESPONSE;
193 *rcvbuf = (uint8_t *)(smb_base(cli_req->inbuf) + SVAL(vwv + 6, 0));
194 *received = size;
195 return NT_STATUS_OK;
199 * Parallel read support.
201 * cli_pull sends as many read&x requests as the server would allow via
202 * max_mux at a time. When replies flow back in, the data is written into
203 * the callback function "sink" in the right order.
206 struct cli_pull_state {
207 struct async_req *req;
209 struct event_context *ev;
210 struct cli_state *cli;
211 uint16_t fnum;
212 off_t start_offset;
213 SMB_OFF_T size;
215 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
216 void *priv;
218 size_t chunk_size;
221 * Outstanding requests
223 int num_reqs;
224 struct async_req **reqs;
227 * For how many bytes did we send requests already?
229 SMB_OFF_T requested;
232 * Next request index to push into "sink". This walks around the "req"
233 * array, taking care that the requests are pushed to "sink" in the
234 * right order. If necessary (i.e. replies don't come in in the right
235 * order), replies are held back in "reqs".
237 int top_req;
240 * How many bytes did we push into "sink"?
243 SMB_OFF_T pushed;
246 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
248 struct cli_pull_state *state = talloc_get_type_abort(
249 req->private_data, struct cli_pull_state);
250 char *result;
252 result = async_req_print(mem_ctx, req);
253 if (result == NULL) {
254 return NULL;
257 return talloc_asprintf_append_buffer(
258 result, "num_reqs=%d, top_req=%d",
259 state->num_reqs, state->top_req);
262 static void cli_pull_read_done(struct async_req *read_req);
265 * Prepare an async pull request
268 struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
269 struct event_context *ev,
270 struct cli_state *cli,
271 uint16_t fnum, off_t start_offset,
272 SMB_OFF_T size, size_t window_size,
273 NTSTATUS (*sink)(char *buf, size_t n,
274 void *priv),
275 void *priv)
277 struct async_req *result;
278 struct cli_pull_state *state;
279 int i;
281 result = async_req_new(mem_ctx);
282 if (result == NULL) {
283 goto failed;
285 state = talloc(result, struct cli_pull_state);
286 if (state == NULL) {
287 goto failed;
289 result->private_data = state;
290 result->print = cli_pull_print;
291 state->req = result;
293 state->cli = cli;
294 state->ev = ev;
295 state->fnum = fnum;
296 state->start_offset = start_offset;
297 state->size = size;
298 state->sink = sink;
299 state->priv = priv;
301 state->pushed = 0;
302 state->top_req = 0;
304 if (size == 0) {
305 if (!async_post_status(result, ev, NT_STATUS_OK)) {
306 goto failed;
308 return result;
311 state->chunk_size = cli_read_max_bufsize(cli);
313 state->num_reqs = MAX(window_size/state->chunk_size, 1);
314 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
316 state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
317 state->num_reqs);
318 if (state->reqs == NULL) {
319 goto failed;
322 state->requested = 0;
324 for (i=0; i<state->num_reqs; i++) {
325 SMB_OFF_T size_left;
326 size_t request_thistime;
328 if (state->requested >= size) {
329 state->num_reqs = i;
330 break;
333 size_left = size - state->requested;
334 request_thistime = MIN(size_left, state->chunk_size);
336 state->reqs[i] = cli_read_andx_send(
337 state->reqs, ev, cli, fnum,
338 state->start_offset + state->requested,
339 request_thistime);
341 if (state->reqs[i] == NULL) {
342 goto failed;
345 state->reqs[i]->async.fn = cli_pull_read_done;
346 state->reqs[i]->async.priv = result;
348 state->requested += request_thistime;
350 return result;
352 failed:
353 TALLOC_FREE(result);
354 return NULL;
358 * Handle incoming read replies, push the data into sink and send out new
359 * requests if necessary.
362 static void cli_pull_read_done(struct async_req *read_req)
364 struct async_req *pull_req = talloc_get_type_abort(
365 read_req->async.priv, struct async_req);
366 struct cli_pull_state *state = talloc_get_type_abort(
367 pull_req->private_data, struct cli_pull_state);
368 struct cli_request *read_state = talloc_get_type_abort(
369 read_req->private_data, struct cli_request);
370 NTSTATUS status;
372 status = cli_read_andx_recv(read_req, &read_state->data.read.received,
373 &read_state->data.read.rcvbuf);
374 if (!NT_STATUS_IS_OK(status)) {
375 async_req_error(state->req, status);
376 return;
380 * This loop is the one to take care of out-of-order replies. All
381 * pending requests are in state->reqs, state->reqs[top_req] is the
382 * one that is to be pushed next. If however a request later than
383 * top_req is replied to, then we can't push yet. If top_req is
384 * replied to at a later point then, we need to push all the finished
385 * requests.
388 while (state->reqs[state->top_req] != NULL) {
389 struct cli_request *top_read;
391 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
392 state->top_req));
394 if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
395 DEBUG(11, ("cli_pull_read_done: top request not yet "
396 "done\n"));
397 return;
400 top_read = talloc_get_type_abort(
401 state->reqs[state->top_req]->private_data,
402 struct cli_request);
404 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
405 "pushed\n", (int)top_read->data.read.received,
406 (int)state->pushed));
408 status = state->sink((char *)top_read->data.read.rcvbuf,
409 top_read->data.read.received,
410 state->priv);
411 if (!NT_STATUS_IS_OK(status)) {
412 async_req_error(state->req, status);
413 return;
415 state->pushed += top_read->data.read.received;
417 TALLOC_FREE(state->reqs[state->top_req]);
419 if (state->requested < state->size) {
420 struct async_req *new_req;
421 SMB_OFF_T size_left;
422 size_t request_thistime;
424 size_left = state->size - state->requested;
425 request_thistime = MIN(size_left, state->chunk_size);
427 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
428 "at %d, position %d\n",
429 (int)request_thistime,
430 (int)(state->start_offset
431 + state->requested),
432 state->top_req));
434 new_req = cli_read_andx_send(
435 state->reqs, state->ev, state->cli,
436 state->fnum,
437 state->start_offset + state->requested,
438 request_thistime);
440 if (async_req_nomem(new_req, state->req)) {
441 return;
444 new_req->async.fn = cli_pull_read_done;
445 new_req->async.priv = pull_req;
447 state->reqs[state->top_req] = new_req;
448 state->requested += request_thistime;
451 state->top_req = (state->top_req+1) % state->num_reqs;
454 async_req_done(pull_req);
457 NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
459 struct cli_pull_state *state = talloc_get_type_abort(
460 req->private_data, struct cli_pull_state);
461 NTSTATUS status;
463 if (async_req_is_error(req, &status)) {
464 return status;
466 *received = state->pushed;
467 return NT_STATUS_OK;
470 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
471 off_t start_offset, SMB_OFF_T size, size_t window_size,
472 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
473 void *priv, SMB_OFF_T *received)
475 TALLOC_CTX *frame = talloc_stackframe();
476 struct event_context *ev;
477 struct async_req *req;
478 NTSTATUS result = NT_STATUS_NO_MEMORY;
480 if (cli->fd_event != NULL) {
482 * Can't use sync call while an async call is in flight
484 return NT_STATUS_INVALID_PARAMETER;
487 ev = event_context_init(frame);
488 if (ev == NULL) {
489 goto nomem;
492 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
493 window_size, sink, priv);
494 if (req == NULL) {
495 goto nomem;
498 while (req->state < ASYNC_REQ_DONE) {
499 event_loop_once(ev);
502 result = cli_pull_recv(req, received);
503 nomem:
504 TALLOC_FREE(frame);
505 return result;
508 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
510 char **pbuf = (char **)priv;
511 memcpy(*pbuf, buf, n);
512 *pbuf += n;
513 return NT_STATUS_OK;
516 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
517 off_t offset, size_t size)
519 NTSTATUS status;
520 SMB_OFF_T ret;
522 status = cli_pull(cli, fnum, offset, size, size,
523 cli_read_sink, &buf, &ret);
524 if (!NT_STATUS_IS_OK(status)) {
525 cli_set_error(cli, status);
526 return -1;
528 return ret;
531 /****************************************************************************
532 Issue a single SMBwrite and don't wait for a reply.
533 ****************************************************************************/
535 static bool cli_issue_write(struct cli_state *cli,
536 int fnum,
537 off_t offset,
538 uint16 mode,
539 const char *buf,
540 size_t size,
541 int i)
543 char *p;
544 bool large_writex = false;
545 /* We can only do direct writes if not signing and not encrypting. */
546 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
548 if (!direct_writes && size + 1 > cli->bufsize) {
549 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
550 if (!cli->outbuf) {
551 return False;
553 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
554 if (cli->inbuf == NULL) {
555 SAFE_FREE(cli->outbuf);
556 return False;
558 cli->bufsize = size + 1024;
561 memset(cli->outbuf,'\0',smb_size);
562 memset(cli->inbuf,'\0',smb_size);
564 if (cli->capabilities & CAP_LARGE_FILES) {
565 large_writex = True;
568 if (large_writex) {
569 cli_set_message(cli->outbuf,14,0,True);
570 } else {
571 cli_set_message(cli->outbuf,12,0,True);
574 SCVAL(cli->outbuf,smb_com,SMBwriteX);
575 SSVAL(cli->outbuf,smb_tid,cli->cnum);
576 cli_setup_packet(cli);
578 SCVAL(cli->outbuf,smb_vwv0,0xFF);
579 SSVAL(cli->outbuf,smb_vwv2,fnum);
581 SIVAL(cli->outbuf,smb_vwv3,offset);
582 SIVAL(cli->outbuf,smb_vwv5,0);
583 SSVAL(cli->outbuf,smb_vwv7,mode);
585 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
587 * According to CIFS-TR-1p00, this following field should only
588 * be set if CAP_LARGE_WRITEX is set. We should check this
589 * locally. However, this check might already have been
590 * done by our callers.
592 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
593 SSVAL(cli->outbuf,smb_vwv10,size);
594 /* +1 is pad byte. */
595 SSVAL(cli->outbuf,smb_vwv11,
596 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
598 if (large_writex) {
599 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
602 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
603 *p++ = '\0'; /* pad byte. */
604 if (!direct_writes) {
605 memcpy(p, buf, size);
607 if (size > 0x1FFFF) {
608 /* This is a POSIX 14 word large write. */
609 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
610 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
611 } else {
612 cli_setup_bcc(cli, p+size);
615 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
617 show_msg(cli->outbuf);
618 if (direct_writes) {
619 /* For direct writes we now need to write the data
620 * directly out of buf. */
621 return cli_send_smb_direct_writeX(cli, buf, size);
622 } else {
623 return cli_send_smb(cli);
627 /****************************************************************************
628 write to a file
629 write_mode: 0x0001 disallow write cacheing
630 0x0002 return bytes remaining
631 0x0004 use raw named pipe protocol
632 0x0008 start of message mode named pipe protocol
633 ****************************************************************************/
635 ssize_t cli_write(struct cli_state *cli,
636 int fnum, uint16 write_mode,
637 const char *buf, off_t offset, size_t size)
639 ssize_t bwritten = 0;
640 unsigned int issued = 0;
641 unsigned int received = 0;
642 int mpx = 1;
643 size_t writesize;
644 int blocks;
646 if(cli->max_mux > 1) {
647 mpx = cli->max_mux-1;
648 } else {
649 mpx = 1;
652 writesize = cli_write_max_bufsize(cli, write_mode);
654 blocks = (size + (writesize-1)) / writesize;
656 while (received < blocks) {
658 while ((issued - received < mpx) && (issued < blocks)) {
659 ssize_t bsent = issued * writesize;
660 ssize_t size1 = MIN(writesize, size - bsent);
662 if (!cli_issue_write(cli, fnum, offset + bsent,
663 write_mode,
664 buf + bsent,
665 size1, issued))
666 return -1;
667 issued++;
670 if (!cli_receive_smb(cli)) {
671 return bwritten;
674 received++;
676 if (cli_is_error(cli))
677 break;
679 bwritten += SVAL(cli->inbuf, smb_vwv2);
680 if (writesize > 0xFFFF) {
681 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
685 while (received < issued && cli_receive_smb(cli)) {
686 received++;
689 return bwritten;
692 /****************************************************************************
693 write to a file using a SMBwrite and not bypassing 0 byte writes
694 ****************************************************************************/
696 ssize_t cli_smbwrite(struct cli_state *cli,
697 int fnum, char *buf, off_t offset, size_t size1)
699 char *p;
700 ssize_t total = 0;
702 do {
703 size_t size = MIN(size1, cli->max_xmit - 48);
705 memset(cli->outbuf,'\0',smb_size);
706 memset(cli->inbuf,'\0',smb_size);
708 cli_set_message(cli->outbuf,5, 0,True);
710 SCVAL(cli->outbuf,smb_com,SMBwrite);
711 SSVAL(cli->outbuf,smb_tid,cli->cnum);
712 cli_setup_packet(cli);
714 SSVAL(cli->outbuf,smb_vwv0,fnum);
715 SSVAL(cli->outbuf,smb_vwv1,size);
716 SIVAL(cli->outbuf,smb_vwv2,offset);
717 SSVAL(cli->outbuf,smb_vwv4,0);
719 p = smb_buf(cli->outbuf);
720 *p++ = 1;
721 SSVAL(p, 0, size); p += 2;
722 memcpy(p, buf + total, size); p += size;
724 cli_setup_bcc(cli, p);
726 if (!cli_send_smb(cli))
727 return -1;
729 if (!cli_receive_smb(cli))
730 return -1;
732 if (cli_is_error(cli))
733 return -1;
735 size = SVAL(cli->inbuf,smb_vwv0);
736 if (size == 0)
737 break;
739 size1 -= size;
740 total += size;
741 offset += size;
743 } while (size1);
745 return total;
749 * Send a write&x request
752 struct async_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
753 struct event_context *ev,
754 struct cli_state *cli, uint16_t fnum,
755 uint16_t mode, const uint8_t *buf,
756 off_t offset, size_t size)
758 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
759 uint8_t wct = bigoffset ? 14 : 12;
760 size_t max_write = cli_write_max_bufsize(cli, mode);
761 uint16_t vwv[14];
763 size = MIN(size, max_write);
765 SCVAL(vwv+0, 0, 0xFF);
766 SCVAL(vwv+0, 1, 0);
767 SSVAL(vwv+1, 0, 0);
768 SSVAL(vwv+2, 0, fnum);
769 SIVAL(vwv+3, 0, offset);
770 SIVAL(vwv+5, 0, 0);
771 SSVAL(vwv+7, 0, mode);
772 SSVAL(vwv+8, 0, 0);
773 SSVAL(vwv+9, 0, (size>>16));
774 SSVAL(vwv+10, 0, size);
776 SSVAL(vwv+11, 0,
777 cli_wct_ofs(cli)
778 + 1 /* the wct field */
779 + wct * 2 /* vwv */
780 + 2 /* num_bytes field */
781 + 1 /* pad */);
783 if (bigoffset) {
784 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
787 return cli_request_send(mem_ctx, ev, cli, SMBwriteX, 0, wct, vwv,
788 2, size, buf);
791 NTSTATUS cli_write_andx_recv(struct async_req *req, size_t *pwritten)
793 uint8_t wct;
794 uint16_t *vwv;
795 uint16_t num_bytes;
796 uint8_t *bytes;
797 NTSTATUS status;
798 size_t written;
800 if (async_req_is_error(req, &status)) {
801 return status;
804 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
806 if (NT_STATUS_IS_ERR(status)) {
807 return status;
810 if (wct < 6) {
811 return NT_STATUS_INVALID_NETWORK_RESPONSE;
814 written = SVAL(vwv+2, 0);
815 written |= SVAL(vwv+4, 0)<<16;
816 *pwritten = written;
818 return NT_STATUS_OK;
821 struct cli_writeall_state {
822 struct event_context *ev;
823 struct cli_state *cli;
824 uint16_t fnum;
825 uint16_t mode;
826 const uint8_t *buf;
827 off_t offset;
828 size_t size;
829 size_t written;
832 static void cli_writeall_written(struct async_req *req);
834 static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
835 struct event_context *ev,
836 struct cli_state *cli,
837 uint16_t fnum,
838 uint16_t mode,
839 const uint8_t *buf,
840 off_t offset, size_t size)
842 struct async_req *result;
843 struct async_req *subreq;
844 struct cli_writeall_state *state;
846 result = async_req_new(mem_ctx);
847 if (result == NULL) {
848 goto fail;
850 state = talloc(result, struct cli_writeall_state);
851 if (state == NULL) {
852 goto fail;
854 result->private_data = state;
856 state->ev = ev;
857 state->cli = cli;
858 state->fnum = fnum;
859 state->mode = mode;
860 state->buf = buf;
861 state->offset = offset;
862 state->size = size;
863 state->written = 0;
865 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
866 state->mode, state->buf, state->offset,
867 state->size);
868 if (subreq == NULL) {
869 goto fail;
872 subreq->async.fn = cli_writeall_written;
873 subreq->async.priv = result;
874 return result;
876 fail:
877 TALLOC_FREE(result);
878 return NULL;
881 static void cli_writeall_written(struct async_req *subreq)
883 struct async_req *req = talloc_get_type_abort(
884 subreq->async.priv, struct async_req);
885 struct cli_writeall_state *state = talloc_get_type_abort(
886 req->private_data, struct cli_writeall_state);
887 NTSTATUS status;
888 size_t written, to_write;
890 status = cli_write_andx_recv(subreq, &written);
891 TALLOC_FREE(subreq);
892 if (!NT_STATUS_IS_OK(status)) {
893 async_req_error(req, status);
894 return;
897 state->written += written;
899 if (state->written > state->size) {
900 async_req_error(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
901 return;
904 to_write = state->size - state->written;
906 if (to_write == 0) {
907 async_req_done(req);
908 return;
911 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
912 state->mode,
913 state->buf + state->written,
914 state->offset + state->written, to_write);
915 if (subreq == NULL) {
916 async_req_error(req, NT_STATUS_NO_MEMORY);
917 return;
920 subreq->async.fn = cli_writeall_written;
921 subreq->async.priv = req;
924 static NTSTATUS cli_writeall_recv(struct async_req *req)
926 return async_req_simple_recv(req);
929 struct cli_push_state {
930 struct async_req *req;
932 struct event_context *ev;
933 struct cli_state *cli;
934 uint16_t fnum;
935 uint16_t mode;
936 off_t start_offset;
937 size_t window_size;
939 size_t (*source)(uint8_t *buf, size_t n, void *priv);
940 void *priv;
942 size_t chunk_size;
944 size_t sent;
945 bool eof;
948 * Outstanding requests
950 int num_reqs;
951 struct async_req **reqs;
953 int pending;
955 uint8_t *buf;
958 static void cli_push_written(struct async_req *req);
960 struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
961 struct cli_state *cli,
962 uint16_t fnum, uint16_t mode,
963 off_t start_offset, size_t window_size,
964 size_t (*source)(uint8_t *buf, size_t n,
965 void *priv),
966 void *priv)
968 struct async_req *result;
969 struct cli_push_state *state;
970 int i;
972 result = async_req_new(mem_ctx);
973 if (result == NULL) {
974 goto failed;
976 state = talloc(result, struct cli_push_state);
977 if (state == NULL) {
978 goto failed;
980 result->private_data = state;
981 state->req = result;
983 state->cli = cli;
984 state->ev = ev;
985 state->fnum = fnum;
986 state->start_offset = start_offset;
987 state->mode = mode;
988 state->source = source;
989 state->priv = priv;
990 state->eof = false;
991 state->sent = 0;
992 state->pending = 0;
994 state->chunk_size = cli_write_max_bufsize(cli, mode);
996 state->num_reqs = MAX(window_size/state->chunk_size, 1);
997 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
999 state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
1000 state->num_reqs);
1001 if (state->reqs == NULL) {
1002 goto failed;
1005 state->buf = TALLOC_ARRAY(
1006 state, uint8_t, state->chunk_size * state->num_reqs);
1007 if (state->buf == NULL) {
1008 goto failed;
1011 for (i=0; i<state->num_reqs; i++) {
1012 size_t to_write;
1013 uint8_t *buf = state->buf + i*state->chunk_size;
1015 to_write = state->source(buf, state->chunk_size, state->priv);
1016 if (to_write == 0) {
1017 state->eof = true;
1018 break;
1021 state->reqs[i] = cli_writeall_send(
1022 state->reqs, state->ev, state->cli, state->fnum,
1023 state->mode, buf, state->start_offset + state->sent,
1024 to_write);
1025 if (state->reqs[i] == NULL) {
1026 goto failed;
1029 state->reqs[i]->async.fn = cli_push_written;
1030 state->reqs[i]->async.priv = state;
1032 state->sent += to_write;
1033 state->pending += 1;
1036 if (i == 0) {
1037 if (!async_post_status(result, ev, NT_STATUS_OK)) {
1038 goto failed;
1040 return result;
1043 return result;
1045 failed:
1046 TALLOC_FREE(result);
1047 return NULL;
1050 static void cli_push_written(struct async_req *req)
1052 struct cli_push_state *state = talloc_get_type_abort(
1053 req->async.priv, struct cli_push_state);
1054 NTSTATUS status;
1055 int i;
1056 uint8_t *buf;
1057 size_t to_write;
1059 for (i=0; i<state->num_reqs; i++) {
1060 if (state->reqs[i] == req) {
1061 break;
1065 if (i == state->num_reqs) {
1066 async_req_error(state->req, NT_STATUS_INTERNAL_ERROR);
1067 return;
1070 status = cli_writeall_recv(req);
1071 TALLOC_FREE(state->reqs[i]);
1072 req = NULL;
1073 if (!NT_STATUS_IS_OK(status)) {
1074 async_req_error(state->req, status);
1075 return;
1078 state->pending -= 1;
1079 if (state->pending == 0) {
1080 async_req_done(state->req);
1081 return;
1084 if (state->eof) {
1085 return;
1088 buf = state->buf + i * state->chunk_size;
1090 to_write = state->source(buf, state->chunk_size, state->priv);
1091 if (to_write == 0) {
1092 state->eof = true;
1093 return;
1096 state->reqs[i] = cli_writeall_send(
1097 state->reqs, state->ev, state->cli, state->fnum,
1098 state->mode, buf, state->start_offset + state->sent, to_write);
1099 if (state->reqs[i] == NULL) {
1100 async_req_error(state->req, NT_STATUS_NO_MEMORY);
1101 return;
1104 state->reqs[i]->async.fn = cli_push_written;
1105 state->reqs[i]->async.priv = state;
1107 state->sent += to_write;
1108 state->pending += 1;
1111 NTSTATUS cli_push_recv(struct async_req *req)
1113 return async_req_simple_recv(req);
1116 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1117 off_t start_offset, size_t window_size,
1118 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1119 void *priv)
1121 TALLOC_CTX *frame = talloc_stackframe();
1122 struct event_context *ev;
1123 struct async_req *req;
1124 NTSTATUS result = NT_STATUS_NO_MEMORY;
1126 if (cli->fd_event != NULL) {
1128 * Can't use sync call while an async call is in flight
1130 return NT_STATUS_INVALID_PARAMETER;
1133 ev = event_context_init(frame);
1134 if (ev == NULL) {
1135 goto nomem;
1138 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1139 window_size, source, priv);
1140 if (req == NULL) {
1141 goto nomem;
1144 while (req->state < ASYNC_REQ_DONE) {
1145 event_loop_once(ev);
1148 result = cli_push_recv(req);
1149 nomem:
1150 TALLOC_FREE(frame);
1151 return result;