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"
24 /****************************************************************************
25 Calculate the recommended read buffer size
26 ****************************************************************************/
27 static size_t cli_read_max_bufsize(struct cli_state
*cli
)
29 if (!client_is_signing_on(cli
) && !cli_encryption_on(cli
)
30 && (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_READ_CAP
)) {
31 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE
;
33 if (cli
->capabilities
& CAP_LARGE_READX
) {
35 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
36 : CLI_WINDOWS_MAX_LARGE_READX_SIZE
;
38 return (cli
->max_xmit
- (smb_size
+32)) & ~1023;
41 /****************************************************************************
42 Calculate the recommended write buffer size
43 ****************************************************************************/
44 static size_t cli_write_max_bufsize(struct cli_state
*cli
, uint16_t write_mode
)
46 if (write_mode
== 0 &&
47 !client_is_signing_on(cli
) &&
48 !cli_encryption_on(cli
) &&
49 (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_WRITE_CAP
) &&
50 (cli
->capabilities
& CAP_LARGE_FILES
)) {
51 /* Only do massive writes if we can do them direct
52 * with no signing or encrypting - not on a pipe. */
53 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE
;
57 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE
;
60 if (((cli
->capabilities
& CAP_LARGE_WRITEX
) == 0)
61 || client_is_signing_on(cli
)
62 || strequal(cli
->dev
, "LPT1:")) {
65 * Printer devices are restricted to max_xmit writesize in
66 * Vista and XPSP3 as are signing connections.
69 return (cli
->max_xmit
- (smb_size
+32)) & ~1023;
72 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE
;
75 struct cli_read_andx_state
{
83 static void cli_read_andx_done(struct tevent_req
*subreq
);
85 struct tevent_req
*cli_read_andx_create(TALLOC_CTX
*mem_ctx
,
86 struct event_context
*ev
,
87 struct cli_state
*cli
, uint16_t fnum
,
88 off_t offset
, size_t size
,
89 struct tevent_req
**psmbreq
)
91 struct tevent_req
*req
, *subreq
;
92 struct cli_read_andx_state
*state
;
95 if (size
> cli_read_max_bufsize(cli
)) {
96 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
97 "size=%d\n", (int)size
,
98 (int)cli_read_max_bufsize(cli
)));
102 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_andx_state
);
108 SCVAL(state
->vwv
+ 0, 0, 0xFF);
109 SCVAL(state
->vwv
+ 0, 1, 0);
110 SSVAL(state
->vwv
+ 1, 0, 0);
111 SSVAL(state
->vwv
+ 2, 0, fnum
);
112 SIVAL(state
->vwv
+ 3, 0, offset
);
113 SSVAL(state
->vwv
+ 5, 0, size
);
114 SSVAL(state
->vwv
+ 6, 0, size
);
115 SSVAL(state
->vwv
+ 7, 0, (size
>> 16));
116 SSVAL(state
->vwv
+ 8, 0, 0);
117 SSVAL(state
->vwv
+ 9, 0, 0);
119 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 (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 (tevent_req_nterror(req
, status
)) {
290 tevent_req_done(req
);
294 if ((state
->received
== 0) && (received
== state
->size
)) {
295 /* Ideal case: Got it all in one run */
297 state
->received
+= received
;
298 tevent_req_done(req
);
303 * We got a short read, issue a read for the
304 * rest. Unfortunately we have to allocate the buffer
305 * ourselves now, as our caller expects to receive a single
306 * buffer. cli_read_andx does it from the buffer received from
307 * the net, but with a short read we have to put it together
308 * from several reads.
311 if (state
->buf
== NULL
) {
312 state
->buf
= talloc_array(state
, uint8_t, state
->size
);
313 if (tevent_req_nomem(state
->buf
, req
)) {
317 memcpy(state
->buf
+ state
->received
, buf
, received
);
318 state
->received
+= received
;
322 if (state
->received
>= state
->size
) {
323 tevent_req_done(req
);
327 subreq
= cli_read_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
328 state
->start_offset
+ state
->received
,
329 state
->size
- state
->received
);
330 if (tevent_req_nomem(subreq
, req
)) {
333 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
336 static NTSTATUS
cli_readall_recv(struct tevent_req
*req
, ssize_t
*received
,
339 struct cli_readall_state
*state
= tevent_req_data(
340 req
, struct cli_readall_state
);
343 if (tevent_req_is_nterror(req
, &status
)) {
346 *received
= state
->received
;
347 *rcvbuf
= state
->buf
;
351 struct cli_pull_subreq
{
352 struct tevent_req
*req
;
358 * Parallel read support.
360 * cli_pull sends as many read&x requests as the server would allow via
361 * max_mux at a time. When replies flow back in, the data is written into
362 * the callback function "sink" in the right order.
365 struct cli_pull_state
{
366 struct tevent_req
*req
;
368 struct event_context
*ev
;
369 struct cli_state
*cli
;
374 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
);
380 * Outstanding requests
383 struct cli_pull_subreq
*reqs
;
386 * For how many bytes did we send requests already?
391 * Next request index to push into "sink". This walks around the "req"
392 * array, taking care that the requests are pushed to "sink" in the
393 * right order. If necessary (i.e. replies don't come in in the right
394 * order), replies are held back in "reqs".
399 * How many bytes did we push into "sink"?
405 static char *cli_pull_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
407 struct cli_pull_state
*state
= tevent_req_data(
408 req
, struct cli_pull_state
);
411 result
= tevent_req_default_print(req
, mem_ctx
);
412 if (result
== NULL
) {
416 return talloc_asprintf_append_buffer(
417 result
, "num_reqs=%d, top_req=%d",
418 state
->num_reqs
, state
->top_req
);
421 static void cli_pull_read_done(struct tevent_req
*read_req
);
424 * Prepare an async pull request
427 struct tevent_req
*cli_pull_send(TALLOC_CTX
*mem_ctx
,
428 struct event_context
*ev
,
429 struct cli_state
*cli
,
430 uint16_t fnum
, off_t start_offset
,
431 SMB_OFF_T size
, size_t window_size
,
432 NTSTATUS (*sink
)(char *buf
, size_t n
,
436 struct tevent_req
*req
;
437 struct cli_pull_state
*state
;
440 req
= tevent_req_create(mem_ctx
, &state
, struct cli_pull_state
);
444 tevent_req_set_print_fn(req
, cli_pull_print
);
450 state
->start_offset
= start_offset
;
459 tevent_req_done(req
);
460 return tevent_req_post(req
, ev
);
463 state
->chunk_size
= cli_read_max_bufsize(cli
);
465 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
466 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
468 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct cli_pull_subreq
,
470 if (state
->reqs
== NULL
) {
474 state
->requested
= 0;
476 for (i
=0; i
<state
->num_reqs
; i
++) {
477 struct cli_pull_subreq
*subreq
= &state
->reqs
[i
];
479 size_t request_thistime
;
481 if (state
->requested
>= size
) {
486 size_left
= size
- state
->requested
;
487 request_thistime
= MIN(size_left
, state
->chunk_size
);
489 subreq
->req
= cli_readall_send(
490 state
->reqs
, ev
, cli
, fnum
,
491 state
->start_offset
+ state
->requested
,
494 if (subreq
->req
== NULL
) {
497 tevent_req_set_callback(subreq
->req
, cli_pull_read_done
, req
);
498 state
->requested
+= request_thistime
;
508 * Handle incoming read replies, push the data into sink and send out new
509 * requests if necessary.
512 static void cli_pull_read_done(struct tevent_req
*subreq
)
514 struct tevent_req
*req
= tevent_req_callback_data(
515 subreq
, struct tevent_req
);
516 struct cli_pull_state
*state
= tevent_req_data(
517 req
, struct cli_pull_state
);
518 struct cli_pull_subreq
*pull_subreq
= NULL
;
522 for (i
= 0; i
< state
->num_reqs
; i
++) {
523 pull_subreq
= &state
->reqs
[i
];
524 if (subreq
== pull_subreq
->req
) {
528 if (i
== state
->num_reqs
) {
529 /* Huh -- received something we did not send?? */
530 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
534 status
= cli_readall_recv(subreq
, &pull_subreq
->received
,
536 if (!NT_STATUS_IS_OK(status
)) {
537 tevent_req_nterror(state
->req
, status
);
542 * This loop is the one to take care of out-of-order replies. All
543 * pending requests are in state->reqs, state->reqs[top_req] is the
544 * one that is to be pushed next. If however a request later than
545 * top_req is replied to, then we can't push yet. If top_req is
546 * replied to at a later point then, we need to push all the finished
550 while (state
->reqs
[state
->top_req
].req
!= NULL
) {
551 struct cli_pull_subreq
*top_subreq
;
553 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
556 top_subreq
= &state
->reqs
[state
->top_req
];
558 if (tevent_req_is_in_progress(top_subreq
->req
)) {
559 DEBUG(11, ("cli_pull_read_done: top request not yet "
564 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
565 "pushed\n", (int)top_subreq
->received
,
566 (int)state
->pushed
));
568 status
= state
->sink((char *)top_subreq
->buf
,
569 top_subreq
->received
, state
->priv
);
570 if (tevent_req_nterror(state
->req
, status
)) {
573 state
->pushed
+= top_subreq
->received
;
575 TALLOC_FREE(state
->reqs
[state
->top_req
].req
);
577 if (state
->requested
< state
->size
) {
578 struct tevent_req
*new_req
;
580 size_t request_thistime
;
582 size_left
= state
->size
- state
->requested
;
583 request_thistime
= MIN(size_left
, state
->chunk_size
);
585 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
586 "at %d, position %d\n",
587 (int)request_thistime
,
588 (int)(state
->start_offset
592 new_req
= cli_readall_send(
593 state
->reqs
, state
->ev
, state
->cli
,
595 state
->start_offset
+ state
->requested
,
598 if (tevent_req_nomem(new_req
, state
->req
)) {
601 tevent_req_set_callback(new_req
, cli_pull_read_done
,
604 state
->reqs
[state
->top_req
].req
= new_req
;
605 state
->requested
+= request_thistime
;
608 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
611 tevent_req_done(req
);
614 NTSTATUS
cli_pull_recv(struct tevent_req
*req
, SMB_OFF_T
*received
)
616 struct cli_pull_state
*state
= tevent_req_data(
617 req
, struct cli_pull_state
);
620 if (tevent_req_is_nterror(req
, &status
)) {
623 *received
= state
->pushed
;
627 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
628 off_t start_offset
, SMB_OFF_T size
, size_t window_size
,
629 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
630 void *priv
, SMB_OFF_T
*received
)
632 TALLOC_CTX
*frame
= talloc_stackframe();
633 struct event_context
*ev
;
634 struct tevent_req
*req
;
635 NTSTATUS status
= NT_STATUS_OK
;
637 if (cli_has_async_calls(cli
)) {
639 * Can't use sync call while an async call is in flight
641 status
= NT_STATUS_INVALID_PARAMETER
;
645 ev
= event_context_init(frame
);
647 status
= NT_STATUS_NO_MEMORY
;
651 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
652 window_size
, sink
, priv
);
654 status
= NT_STATUS_NO_MEMORY
;
658 if (!tevent_req_poll(req
, ev
)) {
659 status
= map_nt_error_from_unix(errno
);
663 status
= cli_pull_recv(req
, received
);
666 if (!NT_STATUS_IS_OK(status
)) {
667 cli_set_error(cli
, status
);
672 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
674 char **pbuf
= (char **)priv
;
675 memcpy(*pbuf
, buf
, n
);
680 ssize_t
cli_read(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
681 off_t offset
, size_t size
)
686 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
687 cli_read_sink
, &buf
, &ret
);
688 if (!NT_STATUS_IS_OK(status
)) {
689 cli_set_error(cli
, status
);
695 /****************************************************************************
696 Issue a single SMBwrite and don't wait for a reply.
697 ****************************************************************************/
699 static bool cli_issue_write(struct cli_state
*cli
,
707 bool large_writex
= false;
708 /* We can only do direct writes if not signing and not encrypting. */
709 bool direct_writes
= !client_is_signing_on(cli
) && !cli_encryption_on(cli
);
711 if (!direct_writes
&& size
+ 1 > cli
->bufsize
) {
712 cli
->outbuf
= (char *)SMB_REALLOC(cli
->outbuf
, size
+ 1024);
716 cli
->inbuf
= (char *)SMB_REALLOC(cli
->inbuf
, size
+ 1024);
717 if (cli
->inbuf
== NULL
) {
718 SAFE_FREE(cli
->outbuf
);
721 cli
->bufsize
= size
+ 1024;
724 memset(cli
->outbuf
,'\0',smb_size
);
725 memset(cli
->inbuf
,'\0',smb_size
);
727 if (cli
->capabilities
& CAP_LARGE_FILES
) {
732 cli_set_message(cli
->outbuf
,14,0,True
);
734 cli_set_message(cli
->outbuf
,12,0,True
);
737 SCVAL(cli
->outbuf
,smb_com
,SMBwriteX
);
738 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
739 cli_setup_packet(cli
);
741 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
742 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
744 SIVAL(cli
->outbuf
,smb_vwv3
,offset
);
745 SIVAL(cli
->outbuf
,smb_vwv5
,0);
746 SSVAL(cli
->outbuf
,smb_vwv7
,mode
);
748 SSVAL(cli
->outbuf
,smb_vwv8
,(mode
& 0x0008) ? size
: 0);
750 * According to CIFS-TR-1p00, this following field should only
751 * be set if CAP_LARGE_WRITEX is set. We should check this
752 * locally. However, this check might already have been
753 * done by our callers.
755 SSVAL(cli
->outbuf
,smb_vwv9
,(size
>>16));
756 SSVAL(cli
->outbuf
,smb_vwv10
,size
);
757 /* +1 is pad byte. */
758 SSVAL(cli
->outbuf
,smb_vwv11
,
759 smb_buf(cli
->outbuf
) - smb_base(cli
->outbuf
) + 1);
762 SIVAL(cli
->outbuf
,smb_vwv12
,(((uint64_t)offset
)>>32) & 0xffffffff);
765 p
= smb_base(cli
->outbuf
) + SVAL(cli
->outbuf
,smb_vwv11
) -1;
766 *p
++ = '\0'; /* pad byte. */
767 if (!direct_writes
) {
768 memcpy(p
, buf
, size
);
770 if (size
> 0x1FFFF) {
771 /* This is a POSIX 14 word large write. */
772 set_message_bcc(cli
->outbuf
, 0); /* Set bcc to zero. */
773 _smb_setlen_large(cli
->outbuf
,smb_size
+ 28 + 1 /* pad */ + size
- 4);
775 cli_setup_bcc(cli
, p
+size
);
778 show_msg(cli
->outbuf
);
780 /* For direct writes we now need to write the data
781 * directly out of buf. */
782 return cli_send_smb_direct_writeX(cli
, buf
, size
);
784 return cli_send_smb(cli
);
788 /****************************************************************************
790 write_mode: 0x0001 disallow write cacheing
791 0x0002 return bytes remaining
792 0x0004 use raw named pipe protocol
793 0x0008 start of message mode named pipe protocol
794 ****************************************************************************/
796 ssize_t
cli_write(struct cli_state
*cli
,
797 uint16_t fnum
, uint16 write_mode
,
798 const char *buf
, off_t offset
, size_t size
)
800 ssize_t bwritten
= 0;
801 unsigned int issued
= 0;
802 unsigned int received
= 0;
807 if(cli
->max_mux
> 1) {
808 mpx
= cli
->max_mux
-1;
813 writesize
= cli_write_max_bufsize(cli
, write_mode
);
815 blocks
= (size
+ (writesize
-1)) / writesize
;
817 while (received
< blocks
) {
819 while ((issued
- received
< mpx
) && (issued
< blocks
)) {
820 ssize_t bsent
= issued
* writesize
;
821 ssize_t size1
= MIN(writesize
, size
- bsent
);
823 if (!cli_issue_write(cli
, fnum
, offset
+ bsent
,
831 if (!cli_receive_smb(cli
)) {
837 if (cli_is_error(cli
))
840 bwritten
+= SVAL(cli
->inbuf
, smb_vwv2
);
841 if (writesize
> 0xFFFF) {
842 bwritten
+= (((int)(SVAL(cli
->inbuf
, smb_vwv4
)))<<16);
846 while (received
< issued
&& cli_receive_smb(cli
)) {
853 /****************************************************************************
854 write to a file using a SMBwrite and not bypassing 0 byte writes
855 ****************************************************************************/
857 NTSTATUS
cli_smbwrite(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
858 off_t offset
, size_t size1
, size_t *ptotal
)
867 bytes
= TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
869 return NT_STATUS_NO_MEMORY
;
874 size_t size
= MIN(size1
, cli
->max_xmit
- 48);
875 struct tevent_req
*req
;
880 SSVAL(vwv
+0, 0, fnum
);
881 SSVAL(vwv
+1, 0, size
);
882 SIVAL(vwv
+2, 0, offset
);
885 bytes
= TALLOC_REALLOC_ARRAY(talloc_tos(), bytes
, uint8_t,
888 return NT_STATUS_NO_MEMORY
;
890 SSVAL(bytes
, 1, size
);
891 memcpy(bytes
+ 3, buf
+ total
, size
);
893 status
= cli_smb(talloc_tos(), cli
, SMBwrite
, 0, 5, vwv
,
894 size
+3, bytes
, &req
, 1, NULL
, &ret_vwv
,
896 if (!NT_STATUS_IS_OK(status
)) {
901 size
= SVAL(ret_vwv
+0, 0);
914 if (ptotal
!= NULL
) {
921 * Send a write&x request
924 struct cli_write_andx_state
{
932 static void cli_write_andx_done(struct tevent_req
*subreq
);
934 struct tevent_req
*cli_write_andx_create(TALLOC_CTX
*mem_ctx
,
935 struct event_context
*ev
,
936 struct cli_state
*cli
, uint16_t fnum
,
937 uint16_t mode
, const uint8_t *buf
,
938 off_t offset
, size_t size
,
939 struct tevent_req
**reqs_before
,
941 struct tevent_req
**psmbreq
)
943 struct tevent_req
*req
, *subreq
;
944 struct cli_write_andx_state
*state
;
945 bool bigoffset
= ((cli
->capabilities
& CAP_LARGE_FILES
) != 0);
946 uint8_t wct
= bigoffset
? 14 : 12;
947 size_t max_write
= cli_write_max_bufsize(cli
, mode
);
950 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_andx_state
);
955 size
= MIN(size
, max_write
);
959 SCVAL(vwv
+0, 0, 0xFF);
962 SSVAL(vwv
+2, 0, fnum
);
963 SIVAL(vwv
+3, 0, offset
);
965 SSVAL(vwv
+7, 0, mode
);
967 SSVAL(vwv
+9, 0, (size
>>16));
968 SSVAL(vwv
+10, 0, size
);
971 cli_smb_wct_ofs(reqs_before
, num_reqs_before
)
972 + 1 /* the wct field */
974 + 2 /* num_bytes field */
978 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
982 state
->iov
[0].iov_base
= (void *)&state
->pad
;
983 state
->iov
[0].iov_len
= 1;
984 state
->iov
[1].iov_base
= CONST_DISCARD(void *, buf
);
985 state
->iov
[1].iov_len
= size
;
987 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
989 if (tevent_req_nomem(subreq
, req
)) {
990 return tevent_req_post(req
, ev
);
992 tevent_req_set_callback(subreq
, cli_write_andx_done
, req
);
997 struct tevent_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
998 struct event_context
*ev
,
999 struct cli_state
*cli
, uint16_t fnum
,
1000 uint16_t mode
, const uint8_t *buf
,
1001 off_t offset
, size_t size
)
1003 struct tevent_req
*req
, *subreq
;
1006 req
= cli_write_andx_create(mem_ctx
, ev
, cli
, fnum
, mode
, buf
, offset
,
1007 size
, NULL
, 0, &subreq
);
1012 status
= cli_smb_req_send(subreq
);
1013 if (tevent_req_nterror(req
, status
)) {
1014 return tevent_req_post(req
, ev
);
1019 static void cli_write_andx_done(struct tevent_req
*subreq
)
1021 struct tevent_req
*req
= tevent_req_callback_data(
1022 subreq
, struct tevent_req
);
1023 struct cli_write_andx_state
*state
= tevent_req_data(
1024 req
, struct cli_write_andx_state
);
1030 status
= cli_smb_recv(subreq
, state
, &inbuf
, 6, &wct
, &vwv
,
1032 TALLOC_FREE(subreq
);
1033 if (NT_STATUS_IS_ERR(status
)) {
1034 tevent_req_nterror(req
, status
);
1037 state
->written
= SVAL(vwv
+2, 0);
1038 state
->written
|= SVAL(vwv
+4, 0)<<16;
1039 tevent_req_done(req
);
1042 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
1044 struct cli_write_andx_state
*state
= tevent_req_data(
1045 req
, struct cli_write_andx_state
);
1048 if (tevent_req_is_nterror(req
, &status
)) {
1051 *pwritten
= state
->written
;
1052 return NT_STATUS_OK
;
1055 struct cli_writeall_state
{
1056 struct event_context
*ev
;
1057 struct cli_state
*cli
;
1066 static void cli_writeall_written(struct tevent_req
*req
);
1068 static struct tevent_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
1069 struct event_context
*ev
,
1070 struct cli_state
*cli
,
1074 off_t offset
, size_t size
)
1076 struct tevent_req
*req
, *subreq
;
1077 struct cli_writeall_state
*state
;
1079 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
1088 state
->offset
= offset
;
1092 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1093 state
->mode
, state
->buf
, state
->offset
,
1095 if (tevent_req_nomem(subreq
, req
)) {
1096 return tevent_req_post(req
, ev
);
1098 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1102 static void cli_writeall_written(struct tevent_req
*subreq
)
1104 struct tevent_req
*req
= tevent_req_callback_data(
1105 subreq
, struct tevent_req
);
1106 struct cli_writeall_state
*state
= tevent_req_data(
1107 req
, struct cli_writeall_state
);
1109 size_t written
, to_write
;
1111 status
= cli_write_andx_recv(subreq
, &written
);
1112 TALLOC_FREE(subreq
);
1113 if (tevent_req_nterror(req
, status
)) {
1117 state
->written
+= written
;
1119 if (state
->written
> state
->size
) {
1120 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1124 to_write
= state
->size
- state
->written
;
1126 if (to_write
== 0) {
1127 tevent_req_done(req
);
1131 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1133 state
->buf
+ state
->written
,
1134 state
->offset
+ state
->written
, to_write
);
1135 if (tevent_req_nomem(subreq
, req
)) {
1138 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1141 static NTSTATUS
cli_writeall_recv(struct tevent_req
*req
,
1144 struct cli_writeall_state
*state
= tevent_req_data(
1145 req
, struct cli_writeall_state
);
1148 if (tevent_req_is_nterror(req
, &status
)) {
1151 if (pwritten
!= NULL
) {
1152 *pwritten
= state
->written
;
1154 return NT_STATUS_OK
;
1157 NTSTATUS
cli_writeall(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1158 const uint8_t *buf
, off_t offset
, size_t size
,
1161 TALLOC_CTX
*frame
= talloc_stackframe();
1162 struct event_context
*ev
;
1163 struct tevent_req
*req
;
1164 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1166 if (cli_has_async_calls(cli
)) {
1168 * Can't use sync call while an async call is in flight
1170 status
= NT_STATUS_INVALID_PARAMETER
;
1173 ev
= event_context_init(frame
);
1177 req
= cli_writeall_send(frame
, ev
, cli
, fnum
, mode
, buf
, offset
, size
);
1181 if (!tevent_req_poll(req
, ev
)) {
1182 status
= map_nt_error_from_unix(errno
);
1185 status
= cli_writeall_recv(req
, pwritten
);
1188 if (!NT_STATUS_IS_OK(status
)) {
1189 cli_set_error(cli
, status
);
1194 struct cli_push_write_state
{
1195 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1202 struct cli_push_state
{
1203 struct event_context
*ev
;
1204 struct cli_state
*cli
;
1210 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1219 * Outstanding requests
1223 struct cli_push_write_state
**reqs
;
1226 static void cli_push_written(struct tevent_req
*req
);
1228 static bool cli_push_write_setup(struct tevent_req
*req
,
1229 struct cli_push_state
*state
,
1232 struct cli_push_write_state
*substate
;
1233 struct tevent_req
*subreq
;
1235 substate
= talloc(state
->reqs
, struct cli_push_write_state
);
1239 substate
->req
= req
;
1240 substate
->idx
= idx
;
1241 substate
->ofs
= state
->next_offset
;
1242 substate
->buf
= talloc_array(substate
, uint8_t, state
->chunk_size
);
1243 if (!substate
->buf
) {
1244 talloc_free(substate
);
1247 substate
->size
= state
->source(substate
->buf
,
1250 if (substate
->size
== 0) {
1252 /* nothing to send */
1253 talloc_free(substate
);
1257 subreq
= cli_writeall_send(substate
,
1258 state
->ev
, state
->cli
,
1259 state
->fnum
, state
->mode
,
1264 talloc_free(substate
);
1267 tevent_req_set_callback(subreq
, cli_push_written
, substate
);
1269 state
->reqs
[idx
] = substate
;
1270 state
->pending
+= 1;
1271 state
->next_offset
+= substate
->size
;
1276 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1277 struct cli_state
*cli
,
1278 uint16_t fnum
, uint16_t mode
,
1279 off_t start_offset
, size_t window_size
,
1280 size_t (*source
)(uint8_t *buf
, size_t n
,
1284 struct tevent_req
*req
;
1285 struct cli_push_state
*state
;
1288 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1295 state
->start_offset
= start_offset
;
1297 state
->source
= source
;
1301 state
->next_offset
= start_offset
;
1303 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
);
1305 if (window_size
== 0) {
1306 window_size
= cli
->max_mux
* state
->chunk_size
;
1308 state
->num_reqs
= window_size
/state
->chunk_size
;
1309 if ((window_size
% state
->chunk_size
) > 0) {
1310 state
->num_reqs
+= 1;
1312 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
1313 state
->num_reqs
= MAX(state
->num_reqs
, 1);
1315 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct cli_push_write_state
*,
1317 if (state
->reqs
== NULL
) {
1321 for (i
=0; i
<state
->num_reqs
; i
++) {
1322 if (!cli_push_write_setup(req
, state
, i
)) {
1331 if (state
->pending
== 0) {
1332 tevent_req_done(req
);
1333 return tevent_req_post(req
, ev
);
1339 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1340 return tevent_req_post(req
, ev
);
1343 static void cli_push_written(struct tevent_req
*subreq
)
1345 struct cli_push_write_state
*substate
= tevent_req_callback_data(
1346 subreq
, struct cli_push_write_state
);
1347 struct tevent_req
*req
= substate
->req
;
1348 struct cli_push_state
*state
= tevent_req_data(
1349 req
, struct cli_push_state
);
1351 uint32_t idx
= substate
->idx
;
1353 state
->reqs
[idx
] = NULL
;
1354 state
->pending
-= 1;
1356 status
= cli_writeall_recv(subreq
, NULL
);
1357 TALLOC_FREE(subreq
);
1358 TALLOC_FREE(substate
);
1359 if (tevent_req_nterror(req
, status
)) {
1364 if (!cli_push_write_setup(req
, state
, idx
)) {
1365 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1370 if (state
->pending
== 0) {
1371 tevent_req_done(req
);
1376 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1378 return tevent_req_simple_recv_ntstatus(req
);
1381 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1382 off_t start_offset
, size_t window_size
,
1383 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1386 TALLOC_CTX
*frame
= talloc_stackframe();
1387 struct event_context
*ev
;
1388 struct tevent_req
*req
;
1389 NTSTATUS status
= NT_STATUS_OK
;
1391 if (cli_has_async_calls(cli
)) {
1393 * Can't use sync call while an async call is in flight
1395 status
= NT_STATUS_INVALID_PARAMETER
;
1399 ev
= event_context_init(frame
);
1401 status
= NT_STATUS_NO_MEMORY
;
1405 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1406 window_size
, source
, priv
);
1408 status
= NT_STATUS_NO_MEMORY
;
1412 if (!tevent_req_poll(req
, ev
)) {
1413 status
= map_nt_error_from_unix(errno
);
1417 status
= cli_push_recv(req
);
1420 if (!NT_STATUS_IS_OK(status
)) {
1421 cli_set_error(cli
, status
);