s3-libsmb: move protos to libsmb/proto.h
[Samba/vl.git] / source3 / libsmb / clireadwrite.c
blobb7fd2bfc971c50f38d6dc051e4e2c0fb91a2add7
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 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
32 && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
33 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
35 if (cli->capabilities & CAP_LARGE_READX) {
36 return cli->is_samba
37 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
38 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
40 return (cli->max_xmit - (smb_size+32)) & ~1023;
43 /****************************************************************************
44 Calculate the recommended write buffer size
45 ****************************************************************************/
46 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
48 if (write_mode == 0 &&
49 !client_is_signing_on(cli) &&
50 !cli_encryption_on(cli) &&
51 (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
52 (cli->capabilities & CAP_LARGE_FILES)) {
53 /* Only do massive writes if we can do them direct
54 * with no signing or encrypting - not on a pipe. */
55 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
58 if (cli->is_samba) {
59 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
62 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
63 || client_is_signing_on(cli)
64 || strequal(cli->dev, "LPT1:")) {
67 * Printer devices are restricted to max_xmit writesize in
68 * Vista and XPSP3 as are signing connections.
71 return (cli->max_xmit - (smb_size+32)) & ~1023;
74 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
77 struct cli_read_andx_state {
78 size_t size;
79 uint16_t vwv[12];
80 NTSTATUS status;
81 size_t received;
82 uint8_t *buf;
85 static void cli_read_andx_done(struct tevent_req *subreq);
87 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
88 struct event_context *ev,
89 struct cli_state *cli, uint16_t fnum,
90 off_t offset, size_t size,
91 struct tevent_req **psmbreq)
93 struct tevent_req *req, *subreq;
94 struct cli_read_andx_state *state;
95 uint8_t wct = 10;
97 if (size > cli_read_max_bufsize(cli)) {
98 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
99 "size=%d\n", (int)size,
100 (int)cli_read_max_bufsize(cli)));
101 return NULL;
104 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
105 if (req == NULL) {
106 return NULL;
108 state->size = size;
110 SCVAL(state->vwv + 0, 0, 0xFF);
111 SCVAL(state->vwv + 0, 1, 0);
112 SSVAL(state->vwv + 1, 0, 0);
113 SSVAL(state->vwv + 2, 0, fnum);
114 SIVAL(state->vwv + 3, 0, offset);
115 SSVAL(state->vwv + 5, 0, size);
116 SSVAL(state->vwv + 6, 0, size);
117 SSVAL(state->vwv + 7, 0, (size >> 16));
118 SSVAL(state->vwv + 8, 0, 0);
119 SSVAL(state->vwv + 9, 0, 0);
121 if ((uint64_t)offset >> 32) {
122 SIVAL(state->vwv + 10, 0,
123 (((uint64_t)offset)>>32) & 0xffffffff);
124 wct += 2;
127 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
128 state->vwv, 0, NULL);
129 if (subreq == NULL) {
130 TALLOC_FREE(req);
131 return NULL;
133 tevent_req_set_callback(subreq, cli_read_andx_done, req);
134 *psmbreq = subreq;
135 return req;
138 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
139 struct event_context *ev,
140 struct cli_state *cli, uint16_t fnum,
141 off_t offset, size_t size)
143 struct tevent_req *req, *subreq;
144 NTSTATUS status;
146 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
147 &subreq);
148 if (req == NULL) {
149 return NULL;
152 status = cli_smb_req_send(subreq);
153 if (tevent_req_nterror(req, status)) {
154 return tevent_req_post(req, ev);
156 return req;
159 static void cli_read_andx_done(struct tevent_req *subreq)
161 struct tevent_req *req = tevent_req_callback_data(
162 subreq, struct tevent_req);
163 struct cli_read_andx_state *state = tevent_req_data(
164 req, struct cli_read_andx_state);
165 uint8_t *inbuf;
166 uint8_t wct;
167 uint16_t *vwv;
168 uint32_t num_bytes;
169 uint8_t *bytes;
171 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
172 &num_bytes, &bytes);
173 TALLOC_FREE(subreq);
174 if (NT_STATUS_IS_ERR(state->status)) {
175 tevent_req_nterror(req, state->status);
176 return;
179 /* size is the number of bytes the server returned.
180 * Might be zero. */
181 state->received = SVAL(vwv + 5, 0);
182 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
184 if (state->received > state->size) {
185 DEBUG(5,("server returned more than we wanted!\n"));
186 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
187 return;
191 * bcc field must be valid for small reads, for large reads the 16-bit
192 * bcc field can't be correct.
195 if ((state->received < 0xffff) && (state->received > num_bytes)) {
196 DEBUG(5, ("server announced more bytes than sent\n"));
197 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
198 return;
201 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
203 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
204 || ((state->received != 0) && (state->buf < bytes))) {
205 DEBUG(5, ("server returned invalid read&x data offset\n"));
206 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
207 return;
209 tevent_req_done(req);
213 * Pull the data out of a finished async read_and_x request. rcvbuf is
214 * talloced from the request, so better make sure that you copy it away before
215 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
216 * talloc_move it!
219 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
220 uint8_t **rcvbuf)
222 struct cli_read_andx_state *state = tevent_req_data(
223 req, struct cli_read_andx_state);
224 NTSTATUS status;
226 if (tevent_req_is_nterror(req, &status)) {
227 return status;
229 *received = state->received;
230 *rcvbuf = state->buf;
231 return NT_STATUS_OK;
234 struct cli_readall_state {
235 struct tevent_context *ev;
236 struct cli_state *cli;
237 uint16_t fnum;
238 off_t start_offset;
239 size_t size;
240 size_t received;
241 uint8_t *buf;
244 static void cli_readall_done(struct tevent_req *subreq);
246 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
247 struct event_context *ev,
248 struct cli_state *cli,
249 uint16_t fnum,
250 off_t offset, size_t size)
252 struct tevent_req *req, *subreq;
253 struct cli_readall_state *state;
255 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
256 if (req == NULL) {
257 return NULL;
259 state->ev = ev;
260 state->cli = cli;
261 state->fnum = fnum;
262 state->start_offset = offset;
263 state->size = size;
264 state->received = 0;
265 state->buf = NULL;
267 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
268 if (tevent_req_nomem(subreq, req)) {
269 return tevent_req_post(req, ev);
271 tevent_req_set_callback(subreq, cli_readall_done, req);
272 return req;
275 static void cli_readall_done(struct tevent_req *subreq)
277 struct tevent_req *req = tevent_req_callback_data(
278 subreq, struct tevent_req);
279 struct cli_readall_state *state = tevent_req_data(
280 req, struct cli_readall_state);
281 ssize_t received;
282 uint8_t *buf;
283 NTSTATUS status;
285 status = cli_read_andx_recv(subreq, &received, &buf);
286 if (tevent_req_nterror(req, status)) {
287 return;
290 if (received == 0) {
291 /* EOF */
292 tevent_req_done(req);
293 return;
296 if ((state->received == 0) && (received == state->size)) {
297 /* Ideal case: Got it all in one run */
298 state->buf = buf;
299 state->received += received;
300 tevent_req_done(req);
301 return;
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)) {
316 return;
319 memcpy(state->buf + state->received, buf, received);
320 state->received += received;
322 TALLOC_FREE(subreq);
324 if (state->received >= state->size) {
325 tevent_req_done(req);
326 return;
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)) {
333 return;
335 tevent_req_set_callback(subreq, cli_readall_done, req);
338 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
339 uint8_t **rcvbuf)
341 struct cli_readall_state *state = tevent_req_data(
342 req, struct cli_readall_state);
343 NTSTATUS status;
345 if (tevent_req_is_nterror(req, &status)) {
346 return status;
348 *received = state->received;
349 *rcvbuf = state->buf;
350 return NT_STATUS_OK;
353 struct cli_pull_subreq {
354 struct tevent_req *req;
355 ssize_t received;
356 uint8_t *buf;
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;
372 uint16_t fnum;
373 off_t start_offset;
374 SMB_OFF_T size;
376 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
377 void *priv;
379 size_t chunk_size;
382 * Outstanding requests
384 int num_reqs;
385 struct cli_pull_subreq *reqs;
388 * For how many bytes did we send requests already?
390 SMB_OFF_T requested;
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".
398 int top_req;
401 * How many bytes did we push into "sink"?
404 SMB_OFF_T pushed;
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);
411 char *result;
413 result = tevent_req_default_print(req, mem_ctx);
414 if (result == NULL) {
415 return 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,
435 void *priv),
436 void *priv)
438 struct tevent_req *req;
439 struct cli_pull_state *state;
440 int i;
442 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
443 if (req == NULL) {
444 return NULL;
446 tevent_req_set_print_fn(req, cli_pull_print);
447 state->req = req;
449 state->cli = cli;
450 state->ev = ev;
451 state->fnum = fnum;
452 state->start_offset = start_offset;
453 state->size = size;
454 state->sink = sink;
455 state->priv = priv;
457 state->pushed = 0;
458 state->top_req = 0;
460 if (size == 0) {
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,
471 state->num_reqs);
472 if (state->reqs == NULL) {
473 goto failed;
476 state->requested = 0;
478 for (i=0; i<state->num_reqs; i++) {
479 struct cli_pull_subreq *subreq = &state->reqs[i];
480 SMB_OFF_T size_left;
481 size_t request_thistime;
483 if (state->requested >= size) {
484 state->num_reqs = i;
485 break;
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,
494 request_thistime);
496 if (subreq->req == NULL) {
497 goto failed;
499 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
500 state->requested += request_thistime;
502 return req;
504 failed:
505 TALLOC_FREE(req);
506 return NULL;
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;
521 NTSTATUS status;
522 int i;
524 for (i = 0; i < state->num_reqs; i++) {
525 pull_subreq = &state->reqs[i];
526 if (subreq == pull_subreq->req) {
527 break;
530 if (i == state->num_reqs) {
531 /* Huh -- received something we did not send?? */
532 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
533 return;
536 status = cli_readall_recv(subreq, &pull_subreq->received,
537 &pull_subreq->buf);
538 if (!NT_STATUS_IS_OK(status)) {
539 tevent_req_nterror(state->req, status);
540 return;
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
549 * requests.
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",
556 state->top_req));
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 "
562 "done\n"));
563 return;
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 (tevent_req_nterror(state->req, status)) {
573 return;
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;
581 SMB_OFF_T size_left;
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
591 + state->requested),
592 state->top_req));
594 new_req = cli_readall_send(
595 state->reqs, state->ev, state->cli,
596 state->fnum,
597 state->start_offset + state->requested,
598 request_thistime);
600 if (tevent_req_nomem(new_req, state->req)) {
601 return;
603 tevent_req_set_callback(new_req, cli_pull_read_done,
604 req);
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);
620 NTSTATUS status;
622 if (tevent_req_is_nterror(req, &status)) {
623 return status;
625 *received = state->pushed;
626 return NT_STATUS_OK;
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;
644 goto fail;
647 ev = event_context_init(frame);
648 if (ev == NULL) {
649 status = NT_STATUS_NO_MEMORY;
650 goto fail;
653 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
654 window_size, sink, priv);
655 if (req == NULL) {
656 status = NT_STATUS_NO_MEMORY;
657 goto fail;
660 if (!tevent_req_poll(req, ev)) {
661 status = map_nt_error_from_unix(errno);
662 goto fail;
665 status = cli_pull_recv(req, received);
666 fail:
667 TALLOC_FREE(frame);
668 if (!NT_STATUS_IS_OK(status)) {
669 cli_set_error(cli, status);
671 return status;
674 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
676 char **pbuf = (char **)priv;
677 memcpy(*pbuf, buf, n);
678 *pbuf += n;
679 return NT_STATUS_OK;
682 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
683 off_t offset, size_t size)
685 NTSTATUS status;
686 SMB_OFF_T ret;
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);
692 return -1;
694 return ret;
697 /****************************************************************************
698 write to a file using a SMBwrite and not bypassing 0 byte writes
699 ****************************************************************************/
701 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
702 off_t offset, size_t size1, size_t *ptotal)
704 uint8_t *bytes;
705 ssize_t total = 0;
708 * 3 bytes prefix
711 bytes = TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
712 if (bytes == NULL) {
713 return NT_STATUS_NO_MEMORY;
715 bytes[0] = 1;
717 do {
718 size_t size = MIN(size1, cli->max_xmit - 48);
719 struct tevent_req *req;
720 uint16_t vwv[5];
721 uint16_t *ret_vwv;
722 NTSTATUS status;
724 SSVAL(vwv+0, 0, fnum);
725 SSVAL(vwv+1, 0, size);
726 SIVAL(vwv+2, 0, offset);
727 SSVAL(vwv+4, 0, 0);
729 bytes = TALLOC_REALLOC_ARRAY(talloc_tos(), bytes, uint8_t,
730 size+3);
731 if (bytes == NULL) {
732 return NT_STATUS_NO_MEMORY;
734 SSVAL(bytes, 1, size);
735 memcpy(bytes + 3, buf + total, size);
737 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
738 size+3, bytes, &req, 1, NULL, &ret_vwv,
739 NULL, NULL);
740 if (!NT_STATUS_IS_OK(status)) {
741 TALLOC_FREE(bytes);
742 return status;
745 size = SVAL(ret_vwv+0, 0);
746 TALLOC_FREE(req);
747 if (size == 0) {
748 break;
750 size1 -= size;
751 total += size;
752 offset += size;
754 } while (size1);
756 TALLOC_FREE(bytes);
758 if (ptotal != NULL) {
759 *ptotal = total;
761 return NT_STATUS_OK;
765 * Send a write&x request
768 struct cli_write_andx_state {
769 size_t size;
770 uint16_t vwv[14];
771 size_t written;
772 uint8_t pad;
773 struct iovec iov[2];
776 static void cli_write_andx_done(struct tevent_req *subreq);
778 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
779 struct event_context *ev,
780 struct cli_state *cli, uint16_t fnum,
781 uint16_t mode, const uint8_t *buf,
782 off_t offset, size_t size,
783 struct tevent_req **reqs_before,
784 int num_reqs_before,
785 struct tevent_req **psmbreq)
787 struct tevent_req *req, *subreq;
788 struct cli_write_andx_state *state;
789 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
790 uint8_t wct = bigoffset ? 14 : 12;
791 size_t max_write = cli_write_max_bufsize(cli, mode);
792 uint16_t *vwv;
794 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
795 if (req == NULL) {
796 return NULL;
799 size = MIN(size, max_write);
801 vwv = state->vwv;
803 SCVAL(vwv+0, 0, 0xFF);
804 SCVAL(vwv+0, 1, 0);
805 SSVAL(vwv+1, 0, 0);
806 SSVAL(vwv+2, 0, fnum);
807 SIVAL(vwv+3, 0, offset);
808 SIVAL(vwv+5, 0, 0);
809 SSVAL(vwv+7, 0, mode);
810 SSVAL(vwv+8, 0, 0);
811 SSVAL(vwv+9, 0, (size>>16));
812 SSVAL(vwv+10, 0, size);
814 SSVAL(vwv+11, 0,
815 cli_smb_wct_ofs(reqs_before, num_reqs_before)
816 + 1 /* the wct field */
817 + wct * 2 /* vwv */
818 + 2 /* num_bytes field */
819 + 1 /* pad */);
821 if (bigoffset) {
822 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
825 state->pad = 0;
826 state->iov[0].iov_base = (void *)&state->pad;
827 state->iov[0].iov_len = 1;
828 state->iov[1].iov_base = discard_const_p(void, buf);
829 state->iov[1].iov_len = size;
831 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
832 2, state->iov);
833 if (tevent_req_nomem(subreq, req)) {
834 return tevent_req_post(req, ev);
836 tevent_req_set_callback(subreq, cli_write_andx_done, req);
837 *psmbreq = subreq;
838 return req;
841 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
842 struct event_context *ev,
843 struct cli_state *cli, uint16_t fnum,
844 uint16_t mode, const uint8_t *buf,
845 off_t offset, size_t size)
847 struct tevent_req *req, *subreq;
848 NTSTATUS status;
850 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
851 size, NULL, 0, &subreq);
852 if (req == NULL) {
853 return NULL;
856 status = cli_smb_req_send(subreq);
857 if (tevent_req_nterror(req, status)) {
858 return tevent_req_post(req, ev);
860 return req;
863 static void cli_write_andx_done(struct tevent_req *subreq)
865 struct tevent_req *req = tevent_req_callback_data(
866 subreq, struct tevent_req);
867 struct cli_write_andx_state *state = tevent_req_data(
868 req, struct cli_write_andx_state);
869 uint8_t wct;
870 uint16_t *vwv;
871 uint8_t *inbuf;
872 NTSTATUS status;
874 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
875 NULL, NULL);
876 TALLOC_FREE(subreq);
877 if (NT_STATUS_IS_ERR(status)) {
878 tevent_req_nterror(req, status);
879 return;
881 state->written = SVAL(vwv+2, 0);
882 state->written |= SVAL(vwv+4, 0)<<16;
883 tevent_req_done(req);
886 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
888 struct cli_write_andx_state *state = tevent_req_data(
889 req, struct cli_write_andx_state);
890 NTSTATUS status;
892 if (tevent_req_is_nterror(req, &status)) {
893 return status;
895 *pwritten = state->written;
896 return NT_STATUS_OK;
899 struct cli_writeall_state {
900 struct event_context *ev;
901 struct cli_state *cli;
902 uint16_t fnum;
903 uint16_t mode;
904 const uint8_t *buf;
905 off_t offset;
906 size_t size;
907 size_t written;
910 static void cli_writeall_written(struct tevent_req *req);
912 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
913 struct event_context *ev,
914 struct cli_state *cli,
915 uint16_t fnum,
916 uint16_t mode,
917 const uint8_t *buf,
918 off_t offset, size_t size)
920 struct tevent_req *req, *subreq;
921 struct cli_writeall_state *state;
923 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
924 if (req == NULL) {
925 return NULL;
927 state->ev = ev;
928 state->cli = cli;
929 state->fnum = fnum;
930 state->mode = mode;
931 state->buf = buf;
932 state->offset = offset;
933 state->size = size;
934 state->written = 0;
936 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
937 state->mode, state->buf, state->offset,
938 state->size);
939 if (tevent_req_nomem(subreq, req)) {
940 return tevent_req_post(req, ev);
942 tevent_req_set_callback(subreq, cli_writeall_written, req);
943 return req;
946 static void cli_writeall_written(struct tevent_req *subreq)
948 struct tevent_req *req = tevent_req_callback_data(
949 subreq, struct tevent_req);
950 struct cli_writeall_state *state = tevent_req_data(
951 req, struct cli_writeall_state);
952 NTSTATUS status;
953 size_t written, to_write;
955 status = cli_write_andx_recv(subreq, &written);
956 TALLOC_FREE(subreq);
957 if (tevent_req_nterror(req, status)) {
958 return;
961 state->written += written;
963 if (state->written > state->size) {
964 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
965 return;
968 to_write = state->size - state->written;
970 if (to_write == 0) {
971 tevent_req_done(req);
972 return;
975 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
976 state->mode,
977 state->buf + state->written,
978 state->offset + state->written, to_write);
979 if (tevent_req_nomem(subreq, req)) {
980 return;
982 tevent_req_set_callback(subreq, cli_writeall_written, req);
985 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
986 size_t *pwritten)
988 struct cli_writeall_state *state = tevent_req_data(
989 req, struct cli_writeall_state);
990 NTSTATUS status;
992 if (tevent_req_is_nterror(req, &status)) {
993 return status;
995 if (pwritten != NULL) {
996 *pwritten = state->written;
998 return NT_STATUS_OK;
1001 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1002 const uint8_t *buf, off_t offset, size_t size,
1003 size_t *pwritten)
1005 TALLOC_CTX *frame = talloc_stackframe();
1006 struct event_context *ev;
1007 struct tevent_req *req;
1008 NTSTATUS status = NT_STATUS_NO_MEMORY;
1010 if (cli_has_async_calls(cli)) {
1012 * Can't use sync call while an async call is in flight
1014 status = NT_STATUS_INVALID_PARAMETER;
1015 goto fail;
1017 ev = event_context_init(frame);
1018 if (ev == NULL) {
1019 goto fail;
1021 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1022 if (req == NULL) {
1023 goto fail;
1025 if (!tevent_req_poll(req, ev)) {
1026 status = map_nt_error_from_unix(errno);
1027 goto fail;
1029 status = cli_writeall_recv(req, pwritten);
1030 fail:
1031 TALLOC_FREE(frame);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 cli_set_error(cli, status);
1035 return status;
1038 struct cli_push_write_state {
1039 struct tevent_req *req;/* This is the main request! Not the subreq */
1040 uint32_t idx;
1041 off_t ofs;
1042 uint8_t *buf;
1043 size_t size;
1046 struct cli_push_state {
1047 struct event_context *ev;
1048 struct cli_state *cli;
1049 uint16_t fnum;
1050 uint16_t mode;
1051 off_t start_offset;
1052 size_t window_size;
1054 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1055 void *priv;
1057 bool eof;
1059 size_t chunk_size;
1060 off_t next_offset;
1063 * Outstanding requests
1065 uint32_t pending;
1066 uint32_t num_reqs;
1067 struct cli_push_write_state **reqs;
1070 static void cli_push_written(struct tevent_req *req);
1072 static bool cli_push_write_setup(struct tevent_req *req,
1073 struct cli_push_state *state,
1074 uint32_t idx)
1076 struct cli_push_write_state *substate;
1077 struct tevent_req *subreq;
1079 substate = talloc(state->reqs, struct cli_push_write_state);
1080 if (!substate) {
1081 return false;
1083 substate->req = req;
1084 substate->idx = idx;
1085 substate->ofs = state->next_offset;
1086 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1087 if (!substate->buf) {
1088 talloc_free(substate);
1089 return false;
1091 substate->size = state->source(substate->buf,
1092 state->chunk_size,
1093 state->priv);
1094 if (substate->size == 0) {
1095 state->eof = true;
1096 /* nothing to send */
1097 talloc_free(substate);
1098 return true;
1101 subreq = cli_writeall_send(substate,
1102 state->ev, state->cli,
1103 state->fnum, state->mode,
1104 substate->buf,
1105 substate->ofs,
1106 substate->size);
1107 if (!subreq) {
1108 talloc_free(substate);
1109 return false;
1111 tevent_req_set_callback(subreq, cli_push_written, substate);
1113 state->reqs[idx] = substate;
1114 state->pending += 1;
1115 state->next_offset += substate->size;
1117 return true;
1120 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1121 struct cli_state *cli,
1122 uint16_t fnum, uint16_t mode,
1123 off_t start_offset, size_t window_size,
1124 size_t (*source)(uint8_t *buf, size_t n,
1125 void *priv),
1126 void *priv)
1128 struct tevent_req *req;
1129 struct cli_push_state *state;
1130 uint32_t i;
1132 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1133 if (req == NULL) {
1134 return NULL;
1136 state->cli = cli;
1137 state->ev = ev;
1138 state->fnum = fnum;
1139 state->start_offset = start_offset;
1140 state->mode = mode;
1141 state->source = source;
1142 state->priv = priv;
1143 state->eof = false;
1144 state->pending = 0;
1145 state->next_offset = start_offset;
1147 state->chunk_size = cli_write_max_bufsize(cli, mode);
1149 if (window_size == 0) {
1150 window_size = cli->max_mux * state->chunk_size;
1152 state->num_reqs = window_size/state->chunk_size;
1153 if ((window_size % state->chunk_size) > 0) {
1154 state->num_reqs += 1;
1156 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1157 state->num_reqs = MAX(state->num_reqs, 1);
1159 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1160 state->num_reqs);
1161 if (state->reqs == NULL) {
1162 goto failed;
1165 for (i=0; i<state->num_reqs; i++) {
1166 if (!cli_push_write_setup(req, state, i)) {
1167 goto failed;
1170 if (state->eof) {
1171 break;
1175 if (state->pending == 0) {
1176 tevent_req_done(req);
1177 return tevent_req_post(req, ev);
1180 return req;
1182 failed:
1183 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1184 return tevent_req_post(req, ev);
1187 static void cli_push_written(struct tevent_req *subreq)
1189 struct cli_push_write_state *substate = tevent_req_callback_data(
1190 subreq, struct cli_push_write_state);
1191 struct tevent_req *req = substate->req;
1192 struct cli_push_state *state = tevent_req_data(
1193 req, struct cli_push_state);
1194 NTSTATUS status;
1195 uint32_t idx = substate->idx;
1197 state->reqs[idx] = NULL;
1198 state->pending -= 1;
1200 status = cli_writeall_recv(subreq, NULL);
1201 TALLOC_FREE(subreq);
1202 TALLOC_FREE(substate);
1203 if (tevent_req_nterror(req, status)) {
1204 return;
1207 if (!state->eof) {
1208 if (!cli_push_write_setup(req, state, idx)) {
1209 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1210 return;
1214 if (state->pending == 0) {
1215 tevent_req_done(req);
1216 return;
1220 NTSTATUS cli_push_recv(struct tevent_req *req)
1222 return tevent_req_simple_recv_ntstatus(req);
1225 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1226 off_t start_offset, size_t window_size,
1227 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1228 void *priv)
1230 TALLOC_CTX *frame = talloc_stackframe();
1231 struct event_context *ev;
1232 struct tevent_req *req;
1233 NTSTATUS status = NT_STATUS_OK;
1235 if (cli_has_async_calls(cli)) {
1237 * Can't use sync call while an async call is in flight
1239 status = NT_STATUS_INVALID_PARAMETER;
1240 goto fail;
1243 ev = event_context_init(frame);
1244 if (ev == NULL) {
1245 status = NT_STATUS_NO_MEMORY;
1246 goto fail;
1249 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1250 window_size, source, priv);
1251 if (req == NULL) {
1252 status = NT_STATUS_NO_MEMORY;
1253 goto fail;
1256 if (!tevent_req_poll(req, ev)) {
1257 status = map_nt_error_from_unix(errno);
1258 goto fail;
1261 status = cli_push_recv(req);
1262 fail:
1263 TALLOC_FREE(frame);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 cli_set_error(cli, status);
1267 return status;