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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
32 BOOL bigoffset = False;
34 memset(cli->outbuf,'\0',smb_size);
35 memset(cli->inbuf,'\0',smb_size);
37 if ((SMB_BIG_UINT)offset >> 32)
40 set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
42 SCVAL(cli->outbuf,smb_com,SMBreadX);
43 SSVAL(cli->outbuf,smb_tid,cli->cnum);
44 cli_setup_packet(cli);
46 SCVAL(cli->outbuf,smb_vwv0,0xFF);
47 SSVAL(cli->outbuf,smb_vwv2,fnum);
48 SIVAL(cli->outbuf,smb_vwv3,offset);
49 SSVAL(cli->outbuf,smb_vwv5,size);
50 SSVAL(cli->outbuf,smb_vwv6,size);
51 SSVAL(cli->outbuf,smb_vwv7,((size >> 16) & 1));
52 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
55 SIVAL(cli->outbuf,smb_vwv10,(offset>>32) & 0xffffffff);
57 return cli_send_smb(cli);
60 /****************************************************************************
61 Read size bytes at offset offset using SMBreadX.
62 ****************************************************************************/
64 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
75 * Set readsize to the maximum size we can handle in one readX,
76 * rounded down to a multiple of 1024.
79 if (cli->capabilities & CAP_LARGE_READX) {
80 readsize = CLI_MAX_LARGE_READX_SIZE;
82 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
85 while (total < size) {
86 readsize = MIN(readsize, size-total);
88 /* Issue a read and receive a reply */
90 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
93 if (!cli_receive_smb(cli))
96 /* Check for error. Make sure to check for DOS and NT
99 if (cli_is_error(cli)) {
100 BOOL recoverable_error = False;
101 NTSTATUS status = NT_STATUS_OK;
105 if (cli_is_nt_error(cli))
106 status = cli_nt_error(cli);
108 cli_dos_error(cli, &eclass, &ecode);
111 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
112 * recoverable error, plus we have valid data in the
113 * packet so don't error out here.
116 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
117 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
118 recoverable_error = True;
120 if (!recoverable_error)
124 size2 = SVAL(cli->inbuf, smb_vwv5);
125 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
127 if (size2 > readsize) {
128 DEBUG(5,("server returned more than we wanted!\n"));
130 } else if (size2 < 0) {
131 DEBUG(5,("read return < 0!\n"));
135 /* Copy data into buffer */
137 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
138 memcpy(buf + total, p, size2);
144 * If the server returned less than we asked for we're at EOF.
147 if (size2 < readsize)
154 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
156 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
157 you must fix ensure you don't attempt to sign the packets - data
158 *will* be currupted */
160 /****************************************************************************
161 Issue a single SMBreadraw and don't wait for a reply.
162 ****************************************************************************/
164 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
168 if (!cli->sign_info.use_smb_signing) {
169 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
173 memset(cli->outbuf,'\0',smb_size);
174 memset(cli->inbuf,'\0',smb_size);
176 set_message(cli->outbuf,10,0,True);
178 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
179 SSVAL(cli->outbuf,smb_tid,cli->cnum);
180 cli_setup_packet(cli);
182 SSVAL(cli->outbuf,smb_vwv0,fnum);
183 SIVAL(cli->outbuf,smb_vwv1,offset);
184 SSVAL(cli->outbuf,smb_vwv2,size);
185 SSVAL(cli->outbuf,smb_vwv3,size);
186 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
188 return cli_send_smb(cli);
191 /****************************************************************************
192 Tester for the readraw call.
193 ****************************************************************************/
195 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
206 * Set readsize to the maximum size we can handle in one readraw.
211 while (total < size) {
212 readsize = MIN(readsize, size-total);
214 /* Issue a read and receive a reply */
216 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
219 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
222 size2 = smb_len(cli->inbuf);
224 if (size2 > readsize) {
225 DEBUG(5,("server returned more than we wanted!\n"));
227 } else if (size2 < 0) {
228 DEBUG(5,("read return < 0!\n"));
232 /* Copy data into buffer */
236 memcpy(buf + total, p, size2);
243 * If the server returned less than we asked for we're at EOF.
246 if (size2 < readsize)
253 /****************************************************************************
254 issue a single SMBwrite and don't wait for a reply
255 ****************************************************************************/
257 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
258 uint16 mode, const char *buf,
262 BOOL bigoffset = False;
264 if (size > cli->bufsize) {
265 cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
266 cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
267 if (cli->outbuf == NULL || cli->inbuf == NULL)
269 cli->bufsize = size + 1024;
272 memset(cli->outbuf,'\0',smb_size);
273 memset(cli->inbuf,'\0',smb_size);
275 if ((SMB_BIG_UINT)offset >> 32)
279 set_message(cli->outbuf,14,0,True);
281 set_message(cli->outbuf,12,0,True);
283 SCVAL(cli->outbuf,smb_com,SMBwriteX);
284 SSVAL(cli->outbuf,smb_tid,cli->cnum);
285 cli_setup_packet(cli);
287 SCVAL(cli->outbuf,smb_vwv0,0xFF);
288 SSVAL(cli->outbuf,smb_vwv2,fnum);
290 SIVAL(cli->outbuf,smb_vwv3,offset);
291 SIVAL(cli->outbuf,smb_vwv5,0);
292 SSVAL(cli->outbuf,smb_vwv7,mode);
294 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
296 * According to CIFS-TR-1p00, this following field should only
297 * be set if CAP_LARGE_WRITEX is set. We should check this
298 * locally. However, this check might already have been
299 * done by our callers.
301 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
302 SSVAL(cli->outbuf,smb_vwv10,size);
303 SSVAL(cli->outbuf,smb_vwv11,
304 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
307 SIVAL(cli->outbuf,smb_vwv12,(offset>>32) & 0xffffffff);
309 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
310 memcpy(p, buf, size);
311 cli_setup_bcc(cli, p+size);
313 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
315 show_msg(cli->outbuf);
316 return cli_send_smb(cli);
319 /****************************************************************************
321 write_mode: 0x0001 disallow write cacheing
322 0x0002 return bytes remaining
323 0x0004 use raw named pipe protocol
324 0x0008 start of message mode named pipe protocol
325 ****************************************************************************/
327 size_t cli_write(struct cli_state *cli,
328 int fnum, uint16 write_mode,
329 const char *buf, off_t offset, size_t size)
335 int block = cli->max_xmit - (smb_size+32);
336 int blocks = (size + (block-1)) / block;
338 if(cli->max_mux > 1) {
339 mpx = cli->max_mux-1;
344 while (received < blocks) {
346 while ((issued - received < mpx) && (issued < blocks)) {
347 int bsent = issued * block;
348 int size1 = MIN(block, size - bsent);
350 if (!cli_issue_write(cli, fnum, offset + bsent,
358 if (!cli_receive_smb(cli))
363 if (cli_is_error(cli))
366 bwritten += SVAL(cli->inbuf, smb_vwv2);
367 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
370 while (received < issued && cli_receive_smb(cli))
376 /****************************************************************************
377 write to a file using a SMBwrite and not bypassing 0 byte writes
378 ****************************************************************************/
380 ssize_t cli_smbwrite(struct cli_state *cli,
381 int fnum, char *buf, off_t offset, size_t size1)
387 size_t size = MIN(size1, cli->max_xmit - 48);
389 memset(cli->outbuf,'\0',smb_size);
390 memset(cli->inbuf,'\0',smb_size);
392 set_message(cli->outbuf,5, 0,True);
394 SCVAL(cli->outbuf,smb_com,SMBwrite);
395 SSVAL(cli->outbuf,smb_tid,cli->cnum);
396 cli_setup_packet(cli);
398 SSVAL(cli->outbuf,smb_vwv0,fnum);
399 SSVAL(cli->outbuf,smb_vwv1,size);
400 SIVAL(cli->outbuf,smb_vwv2,offset);
401 SSVAL(cli->outbuf,smb_vwv4,0);
403 p = smb_buf(cli->outbuf);
405 SSVAL(p, 0, size); p += 2;
406 memcpy(p, buf, size); p += size;
408 cli_setup_bcc(cli, p);
410 if (!cli_send_smb(cli))
413 if (!cli_receive_smb(cli))
416 if (cli_is_error(cli))
419 size = SVAL(cli->inbuf,smb_vwv0);