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
->server_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
->server_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
;
93 if (size
> cli_read_max_bufsize(cli
)) {
94 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
95 "size=%d\n", (int)size
,
96 (int)cli_read_max_bufsize(cli
)));
100 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_andx_state
);
106 SCVAL(state
->vwv
+ 0, 0, 0xFF);
107 SCVAL(state
->vwv
+ 0, 1, 0);
108 SSVAL(state
->vwv
+ 1, 0, 0);
109 SSVAL(state
->vwv
+ 2, 0, fnum
);
110 SIVAL(state
->vwv
+ 3, 0, offset
);
111 SSVAL(state
->vwv
+ 5, 0, size
);
112 SSVAL(state
->vwv
+ 6, 0, size
);
113 SSVAL(state
->vwv
+ 7, 0, (size
>> 16));
114 SSVAL(state
->vwv
+ 8, 0, 0);
115 SSVAL(state
->vwv
+ 9, 0, 0);
117 if ((uint64_t)offset
>> 32) {
118 SIVAL(state
->vwv
+ 10, 0,
119 (((uint64_t)offset
)>>32) & 0xffffffff);
123 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBreadX
, 0, wct
,
124 state
->vwv
, 0, NULL
);
125 if (subreq
== NULL
) {
129 tevent_req_set_callback(subreq
, cli_read_andx_done
, req
);
134 struct tevent_req
*cli_read_andx_send(TALLOC_CTX
*mem_ctx
,
135 struct event_context
*ev
,
136 struct cli_state
*cli
, uint16_t fnum
,
137 off_t offset
, size_t size
)
139 struct tevent_req
*req
, *subreq
;
142 req
= cli_read_andx_create(mem_ctx
, ev
, cli
, fnum
, offset
, size
,
148 status
= cli_smb_req_send(subreq
);
149 if (!NT_STATUS_IS_OK(status
)) {
150 tevent_req_nterror(req
, status
);
151 return tevent_req_post(req
, ev
);
156 static void cli_read_andx_done(struct tevent_req
*subreq
)
158 struct tevent_req
*req
= tevent_req_callback_data(
159 subreq
, struct tevent_req
);
160 struct cli_read_andx_state
*state
= tevent_req_data(
161 req
, struct cli_read_andx_state
);
168 state
->status
= cli_smb_recv(subreq
, state
, &inbuf
, 12, &wct
, &vwv
,
171 if (NT_STATUS_IS_ERR(state
->status
)) {
172 tevent_req_nterror(req
, state
->status
);
176 /* size is the number of bytes the server returned.
178 state
->received
= SVAL(vwv
+ 5, 0);
179 state
->received
|= (((unsigned int)SVAL(vwv
+ 7, 0)) << 16);
181 if (state
->received
> state
->size
) {
182 DEBUG(5,("server returned more than we wanted!\n"));
183 tevent_req_nterror(req
, NT_STATUS_UNEXPECTED_IO_ERROR
);
188 * bcc field must be valid for small reads, for large reads the 16-bit
189 * bcc field can't be correct.
192 if ((state
->received
< 0xffff) && (state
->received
> num_bytes
)) {
193 DEBUG(5, ("server announced more bytes than sent\n"));
194 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
198 state
->buf
= (uint8_t *)smb_base(inbuf
) + SVAL(vwv
+6, 0);
200 if (trans_oob(smb_len(inbuf
), SVAL(vwv
+6, 0), state
->received
)
201 || ((state
->received
!= 0) && (state
->buf
< bytes
))) {
202 DEBUG(5, ("server returned invalid read&x data offset\n"));
203 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
206 tevent_req_done(req
);
210 * Pull the data out of a finished async read_and_x request. rcvbuf is
211 * talloced from the request, so better make sure that you copy it away before
212 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
216 NTSTATUS
cli_read_andx_recv(struct tevent_req
*req
, ssize_t
*received
,
219 struct cli_read_andx_state
*state
= tevent_req_data(
220 req
, struct cli_read_andx_state
);
223 if (tevent_req_is_nterror(req
, &status
)) {
226 *received
= state
->received
;
227 *rcvbuf
= state
->buf
;
231 struct cli_readall_state
{
232 struct tevent_context
*ev
;
233 struct cli_state
*cli
;
241 static void cli_readall_done(struct tevent_req
*subreq
);
243 static struct tevent_req
*cli_readall_send(TALLOC_CTX
*mem_ctx
,
244 struct event_context
*ev
,
245 struct cli_state
*cli
,
247 off_t offset
, size_t size
)
249 struct tevent_req
*req
, *subreq
;
250 struct cli_readall_state
*state
;
252 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readall_state
);
259 state
->start_offset
= offset
;
264 subreq
= cli_read_andx_send(state
, ev
, cli
, fnum
, offset
, size
);
265 if (tevent_req_nomem(subreq
, req
)) {
266 return tevent_req_post(req
, ev
);
268 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
272 static void cli_readall_done(struct tevent_req
*subreq
)
274 struct tevent_req
*req
= tevent_req_callback_data(
275 subreq
, struct tevent_req
);
276 struct cli_readall_state
*state
= tevent_req_data(
277 req
, struct cli_readall_state
);
282 status
= cli_read_andx_recv(subreq
, &received
, &buf
);
283 if (!NT_STATUS_IS_OK(status
)) {
284 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 (!NT_STATUS_IS_OK(status
)) {
571 tevent_req_nterror(state
->req
, status
);
574 state
->pushed
+= top_subreq
->received
;
576 TALLOC_FREE(state
->reqs
[state
->top_req
].req
);
578 if (state
->requested
< state
->size
) {
579 struct tevent_req
*new_req
;
581 size_t request_thistime
;
583 size_left
= state
->size
- state
->requested
;
584 request_thistime
= MIN(size_left
, state
->chunk_size
);
586 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
587 "at %d, position %d\n",
588 (int)request_thistime
,
589 (int)(state
->start_offset
593 new_req
= cli_readall_send(
594 state
->reqs
, state
->ev
, state
->cli
,
596 state
->start_offset
+ state
->requested
,
599 if (tevent_req_nomem(new_req
, state
->req
)) {
602 tevent_req_set_callback(new_req
, cli_pull_read_done
,
605 state
->reqs
[state
->top_req
].req
= new_req
;
606 state
->requested
+= request_thistime
;
609 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
612 tevent_req_done(req
);
615 NTSTATUS
cli_pull_recv(struct tevent_req
*req
, SMB_OFF_T
*received
)
617 struct cli_pull_state
*state
= tevent_req_data(
618 req
, struct cli_pull_state
);
621 if (tevent_req_is_nterror(req
, &status
)) {
624 *received
= state
->pushed
;
628 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
629 off_t start_offset
, SMB_OFF_T size
, size_t window_size
,
630 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
631 void *priv
, SMB_OFF_T
*received
)
633 TALLOC_CTX
*frame
= talloc_stackframe();
634 struct event_context
*ev
;
635 struct tevent_req
*req
;
636 NTSTATUS status
= NT_STATUS_OK
;
638 if (cli_has_async_calls(cli
)) {
640 * Can't use sync call while an async call is in flight
642 status
= NT_STATUS_INVALID_PARAMETER
;
646 ev
= event_context_init(frame
);
648 status
= NT_STATUS_NO_MEMORY
;
652 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
653 window_size
, sink
, priv
);
655 status
= NT_STATUS_NO_MEMORY
;
659 if (!tevent_req_poll(req
, ev
)) {
660 status
= map_nt_error_from_unix(errno
);
664 status
= cli_pull_recv(req
, received
);
667 if (!NT_STATUS_IS_OK(status
)) {
668 cli_set_error(cli
, status
);
673 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
675 char **pbuf
= (char **)priv
;
676 memcpy(*pbuf
, buf
, n
);
681 ssize_t
cli_read(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
682 off_t offset
, size_t size
)
687 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
688 cli_read_sink
, &buf
, &ret
);
689 if (!NT_STATUS_IS_OK(status
)) {
690 cli_set_error(cli
, status
);
696 /****************************************************************************
697 Issue a single SMBwrite and don't wait for a reply.
698 ****************************************************************************/
700 static bool cli_issue_write(struct cli_state
*cli
,
708 bool large_writex
= false;
709 /* We can only do direct writes if not signing and not encrypting. */
710 bool direct_writes
= !client_is_signing_on(cli
) && !cli_encryption_on(cli
);
712 if (!direct_writes
&& size
+ 1 > cli
->bufsize
) {
713 cli
->outbuf
= (char *)SMB_REALLOC(cli
->outbuf
, size
+ 1024);
717 cli
->inbuf
= (char *)SMB_REALLOC(cli
->inbuf
, size
+ 1024);
718 if (cli
->inbuf
== NULL
) {
719 SAFE_FREE(cli
->outbuf
);
722 cli
->bufsize
= size
+ 1024;
725 memset(cli
->outbuf
,'\0',smb_size
);
726 memset(cli
->inbuf
,'\0',smb_size
);
728 if (cli
->capabilities
& CAP_LARGE_FILES
) {
733 cli_set_message(cli
->outbuf
,14,0,True
);
735 cli_set_message(cli
->outbuf
,12,0,True
);
738 SCVAL(cli
->outbuf
,smb_com
,SMBwriteX
);
739 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
740 cli_setup_packet(cli
);
742 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
743 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
745 SIVAL(cli
->outbuf
,smb_vwv3
,offset
);
746 SIVAL(cli
->outbuf
,smb_vwv5
,0);
747 SSVAL(cli
->outbuf
,smb_vwv7
,mode
);
749 SSVAL(cli
->outbuf
,smb_vwv8
,(mode
& 0x0008) ? size
: 0);
751 * According to CIFS-TR-1p00, this following field should only
752 * be set if CAP_LARGE_WRITEX is set. We should check this
753 * locally. However, this check might already have been
754 * done by our callers.
756 SSVAL(cli
->outbuf
,smb_vwv9
,(size
>>16));
757 SSVAL(cli
->outbuf
,smb_vwv10
,size
);
758 /* +1 is pad byte. */
759 SSVAL(cli
->outbuf
,smb_vwv11
,
760 smb_buf(cli
->outbuf
) - smb_base(cli
->outbuf
) + 1);
763 SIVAL(cli
->outbuf
,smb_vwv12
,(((uint64_t)offset
)>>32) & 0xffffffff);
766 p
= smb_base(cli
->outbuf
) + SVAL(cli
->outbuf
,smb_vwv11
) -1;
767 *p
++ = '\0'; /* pad byte. */
768 if (!direct_writes
) {
769 memcpy(p
, buf
, size
);
771 if (size
> 0x1FFFF) {
772 /* This is a POSIX 14 word large write. */
773 set_message_bcc(cli
->outbuf
, 0); /* Set bcc to zero. */
774 _smb_setlen_large(cli
->outbuf
,smb_size
+ 28 + 1 /* pad */ + size
- 4);
776 cli_setup_bcc(cli
, p
+size
);
779 show_msg(cli
->outbuf
);
781 /* For direct writes we now need to write the data
782 * directly out of buf. */
783 return cli_send_smb_direct_writeX(cli
, buf
, size
);
785 return cli_send_smb(cli
);
789 /****************************************************************************
791 write_mode: 0x0001 disallow write cacheing
792 0x0002 return bytes remaining
793 0x0004 use raw named pipe protocol
794 0x0008 start of message mode named pipe protocol
795 ****************************************************************************/
797 ssize_t
cli_write(struct cli_state
*cli
,
798 uint16_t fnum
, uint16 write_mode
,
799 const char *buf
, off_t offset
, size_t size
)
801 ssize_t bwritten
= 0;
802 unsigned int issued
= 0;
803 unsigned int received
= 0;
808 if(cli
->max_mux
> 1) {
809 mpx
= cli
->max_mux
-1;
814 writesize
= cli_write_max_bufsize(cli
, write_mode
);
816 blocks
= (size
+ (writesize
-1)) / writesize
;
818 while (received
< blocks
) {
820 while ((issued
- received
< mpx
) && (issued
< blocks
)) {
821 ssize_t bsent
= issued
* writesize
;
822 ssize_t size1
= MIN(writesize
, size
- bsent
);
824 if (!cli_issue_write(cli
, fnum
, offset
+ bsent
,
832 if (!cli_receive_smb(cli
)) {
838 if (cli_is_error(cli
))
841 bwritten
+= SVAL(cli
->inbuf
, smb_vwv2
);
842 if (writesize
> 0xFFFF) {
843 bwritten
+= (((int)(SVAL(cli
->inbuf
, smb_vwv4
)))<<16);
847 while (received
< issued
&& cli_receive_smb(cli
)) {
854 /****************************************************************************
855 write to a file using a SMBwrite and not bypassing 0 byte writes
856 ****************************************************************************/
858 ssize_t
cli_smbwrite(struct cli_state
*cli
,
859 uint16_t fnum
, char *buf
, off_t offset
, size_t size1
)
865 size_t size
= MIN(size1
, cli
->max_xmit
- 48);
867 memset(cli
->outbuf
,'\0',smb_size
);
868 memset(cli
->inbuf
,'\0',smb_size
);
870 cli_set_message(cli
->outbuf
,5, 0,True
);
872 SCVAL(cli
->outbuf
,smb_com
,SMBwrite
);
873 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
874 cli_setup_packet(cli
);
876 SSVAL(cli
->outbuf
,smb_vwv0
,fnum
);
877 SSVAL(cli
->outbuf
,smb_vwv1
,size
);
878 SIVAL(cli
->outbuf
,smb_vwv2
,offset
);
879 SSVAL(cli
->outbuf
,smb_vwv4
,0);
881 p
= smb_buf(cli
->outbuf
);
883 SSVAL(p
, 0, size
); p
+= 2;
884 memcpy(p
, buf
+ total
, size
); p
+= size
;
886 cli_setup_bcc(cli
, p
);
888 if (!cli_send_smb(cli
))
891 if (!cli_receive_smb(cli
))
894 if (cli_is_error(cli
))
897 size
= SVAL(cli
->inbuf
,smb_vwv0
);
911 * Send a write&x request
914 struct cli_write_andx_state
{
922 static void cli_write_andx_done(struct tevent_req
*subreq
);
924 struct tevent_req
*cli_write_andx_create(TALLOC_CTX
*mem_ctx
,
925 struct event_context
*ev
,
926 struct cli_state
*cli
, uint16_t fnum
,
927 uint16_t mode
, const uint8_t *buf
,
928 off_t offset
, size_t size
,
929 struct tevent_req
**reqs_before
,
931 struct tevent_req
**psmbreq
)
933 struct tevent_req
*req
, *subreq
;
934 struct cli_write_andx_state
*state
;
935 bool bigoffset
= ((cli
->capabilities
& CAP_LARGE_FILES
) != 0);
936 uint8_t wct
= bigoffset
? 14 : 12;
937 size_t max_write
= cli_write_max_bufsize(cli
, mode
);
940 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_andx_state
);
945 size
= MIN(size
, max_write
);
949 SCVAL(vwv
+0, 0, 0xFF);
952 SSVAL(vwv
+2, 0, fnum
);
953 SIVAL(vwv
+3, 0, offset
);
955 SSVAL(vwv
+7, 0, mode
);
957 SSVAL(vwv
+9, 0, (size
>>16));
958 SSVAL(vwv
+10, 0, size
);
961 cli_smb_wct_ofs(reqs_before
, num_reqs_before
)
962 + 1 /* the wct field */
964 + 2 /* num_bytes field */
968 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
972 state
->iov
[0].iov_base
= (void *)&state
->pad
;
973 state
->iov
[0].iov_len
= 1;
974 state
->iov
[1].iov_base
= CONST_DISCARD(void *, buf
);
975 state
->iov
[1].iov_len
= size
;
977 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
979 if (tevent_req_nomem(subreq
, req
)) {
980 return tevent_req_post(req
, ev
);
982 tevent_req_set_callback(subreq
, cli_write_andx_done
, req
);
987 struct tevent_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
988 struct event_context
*ev
,
989 struct cli_state
*cli
, uint16_t fnum
,
990 uint16_t mode
, const uint8_t *buf
,
991 off_t offset
, size_t size
)
993 struct tevent_req
*req
, *subreq
;
996 req
= cli_write_andx_create(mem_ctx
, ev
, cli
, fnum
, mode
, buf
, offset
,
997 size
, NULL
, 0, &subreq
);
1002 status
= cli_smb_req_send(subreq
);
1003 if (!NT_STATUS_IS_OK(status
)) {
1004 tevent_req_nterror(req
, status
);
1005 return tevent_req_post(req
, ev
);
1010 static void cli_write_andx_done(struct tevent_req
*subreq
)
1012 struct tevent_req
*req
= tevent_req_callback_data(
1013 subreq
, struct tevent_req
);
1014 struct cli_write_andx_state
*state
= tevent_req_data(
1015 req
, struct cli_write_andx_state
);
1021 status
= cli_smb_recv(subreq
, state
, &inbuf
, 6, &wct
, &vwv
,
1023 TALLOC_FREE(subreq
);
1024 if (NT_STATUS_IS_ERR(status
)) {
1025 tevent_req_nterror(req
, status
);
1028 state
->written
= SVAL(vwv
+2, 0);
1029 state
->written
|= SVAL(vwv
+4, 0)<<16;
1030 tevent_req_done(req
);
1033 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
1035 struct cli_write_andx_state
*state
= tevent_req_data(
1036 req
, struct cli_write_andx_state
);
1039 if (tevent_req_is_nterror(req
, &status
)) {
1042 *pwritten
= state
->written
;
1043 return NT_STATUS_OK
;
1046 struct cli_writeall_state
{
1047 struct event_context
*ev
;
1048 struct cli_state
*cli
;
1057 static void cli_writeall_written(struct tevent_req
*req
);
1059 static struct tevent_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
1060 struct event_context
*ev
,
1061 struct cli_state
*cli
,
1065 off_t offset
, size_t size
)
1067 struct tevent_req
*req
, *subreq
;
1068 struct cli_writeall_state
*state
;
1070 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
1079 state
->offset
= offset
;
1083 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1084 state
->mode
, state
->buf
, state
->offset
,
1086 if (tevent_req_nomem(subreq
, req
)) {
1087 return tevent_req_post(req
, ev
);
1089 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1093 static void cli_writeall_written(struct tevent_req
*subreq
)
1095 struct tevent_req
*req
= tevent_req_callback_data(
1096 subreq
, struct tevent_req
);
1097 struct cli_writeall_state
*state
= tevent_req_data(
1098 req
, struct cli_writeall_state
);
1100 size_t written
, to_write
;
1102 status
= cli_write_andx_recv(subreq
, &written
);
1103 TALLOC_FREE(subreq
);
1104 if (!NT_STATUS_IS_OK(status
)) {
1105 tevent_req_nterror(req
, status
);
1109 state
->written
+= written
;
1111 if (state
->written
> state
->size
) {
1112 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1116 to_write
= state
->size
- state
->written
;
1118 if (to_write
== 0) {
1119 tevent_req_done(req
);
1123 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1125 state
->buf
+ state
->written
,
1126 state
->offset
+ state
->written
, to_write
);
1127 if (tevent_req_nomem(subreq
, req
)) {
1130 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1133 static NTSTATUS
cli_writeall_recv(struct tevent_req
*req
)
1135 return tevent_req_simple_recv_ntstatus(req
);
1138 struct cli_push_write_state
{
1139 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1146 struct cli_push_state
{
1147 struct event_context
*ev
;
1148 struct cli_state
*cli
;
1154 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1163 * Outstanding requests
1167 struct cli_push_write_state
**reqs
;
1170 static void cli_push_written(struct tevent_req
*req
);
1172 static bool cli_push_write_setup(struct tevent_req
*req
,
1173 struct cli_push_state
*state
,
1176 struct cli_push_write_state
*substate
;
1177 struct tevent_req
*subreq
;
1179 substate
= talloc(state
->reqs
, struct cli_push_write_state
);
1183 substate
->req
= req
;
1184 substate
->idx
= idx
;
1185 substate
->ofs
= state
->next_offset
;
1186 substate
->buf
= talloc_array(substate
, uint8_t, state
->chunk_size
);
1187 if (!substate
->buf
) {
1188 talloc_free(substate
);
1191 substate
->size
= state
->source(substate
->buf
,
1194 if (substate
->size
== 0) {
1196 /* nothing to send */
1197 talloc_free(substate
);
1201 subreq
= cli_writeall_send(substate
,
1202 state
->ev
, state
->cli
,
1203 state
->fnum
, state
->mode
,
1208 talloc_free(substate
);
1211 tevent_req_set_callback(subreq
, cli_push_written
, substate
);
1213 state
->reqs
[idx
] = substate
;
1214 state
->pending
+= 1;
1215 state
->next_offset
+= substate
->size
;
1220 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1221 struct cli_state
*cli
,
1222 uint16_t fnum
, uint16_t mode
,
1223 off_t start_offset
, size_t window_size
,
1224 size_t (*source
)(uint8_t *buf
, size_t n
,
1228 struct tevent_req
*req
;
1229 struct cli_push_state
*state
;
1232 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1239 state
->start_offset
= start_offset
;
1241 state
->source
= source
;
1245 state
->next_offset
= start_offset
;
1247 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
);
1249 if (window_size
== 0) {
1250 window_size
= cli
->max_mux
* state
->chunk_size
;
1252 state
->num_reqs
= window_size
/state
->chunk_size
;
1253 if ((window_size
% state
->chunk_size
) > 0) {
1254 state
->num_reqs
+= 1;
1256 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
1257 state
->num_reqs
= MAX(state
->num_reqs
, 1);
1259 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct cli_push_write_state
*,
1261 if (state
->reqs
== NULL
) {
1265 for (i
=0; i
<state
->num_reqs
; i
++) {
1266 if (!cli_push_write_setup(req
, state
, i
)) {
1275 if (state
->pending
== 0) {
1276 tevent_req_done(req
);
1277 return tevent_req_post(req
, ev
);
1283 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1284 return tevent_req_post(req
, ev
);
1287 static void cli_push_written(struct tevent_req
*subreq
)
1289 struct cli_push_write_state
*substate
= tevent_req_callback_data(
1290 subreq
, struct cli_push_write_state
);
1291 struct tevent_req
*req
= substate
->req
;
1292 struct cli_push_state
*state
= tevent_req_data(
1293 req
, struct cli_push_state
);
1295 uint32_t idx
= substate
->idx
;
1297 state
->reqs
[idx
] = NULL
;
1298 state
->pending
-= 1;
1300 status
= cli_writeall_recv(subreq
);
1301 TALLOC_FREE(subreq
);
1302 TALLOC_FREE(substate
);
1303 if (!NT_STATUS_IS_OK(status
)) {
1304 tevent_req_nterror(req
, status
);
1309 if (!cli_push_write_setup(req
, state
, idx
)) {
1310 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1315 if (state
->pending
== 0) {
1316 tevent_req_done(req
);
1321 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1323 return tevent_req_simple_recv_ntstatus(req
);
1326 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1327 off_t start_offset
, size_t window_size
,
1328 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1331 TALLOC_CTX
*frame
= talloc_stackframe();
1332 struct event_context
*ev
;
1333 struct tevent_req
*req
;
1334 NTSTATUS status
= NT_STATUS_OK
;
1336 if (cli_has_async_calls(cli
)) {
1338 * Can't use sync call while an async call is in flight
1340 status
= NT_STATUS_INVALID_PARAMETER
;
1344 ev
= event_context_init(frame
);
1346 status
= NT_STATUS_NO_MEMORY
;
1350 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1351 window_size
, source
, priv
);
1353 status
= NT_STATUS_NO_MEMORY
;
1357 if (!tevent_req_poll(req
, ev
)) {
1358 status
= map_nt_error_from_unix(errno
);
1362 status
= cli_push_recv(req
);
1365 if (!NT_STATUS_IS_OK(status
)) {
1366 cli_set_error(cli
, status
);