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.
23 /****************************************************************************
24 Issue a single SMBread and don't wait for a reply.
25 ****************************************************************************/
27 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
30 BOOL bigoffset = False;
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 if ((SMB_BIG_UINT)offset >> 32)
38 set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
40 SCVAL(cli->outbuf,smb_com,SMBreadX);
41 SSVAL(cli->outbuf,smb_tid,cli->cnum);
42 cli_setup_packet(cli);
44 SCVAL(cli->outbuf,smb_vwv0,0xFF);
45 SSVAL(cli->outbuf,smb_vwv2,fnum);
46 SIVAL(cli->outbuf,smb_vwv3,offset);
47 SSVAL(cli->outbuf,smb_vwv5,size);
48 SSVAL(cli->outbuf,smb_vwv6,size);
49 SSVAL(cli->outbuf,smb_vwv7,((size >> 16) & 1));
50 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
53 SIVAL(cli->outbuf,smb_vwv10,(offset>>32) & 0xffffffff);
55 return cli_send_smb(cli);
58 /****************************************************************************
59 Read size bytes at offset offset using SMBreadX.
60 ****************************************************************************/
62 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
73 * Set readsize to the maximum size we can handle in one readX,
74 * rounded down to a multiple of 1024.
77 if (cli->capabilities & CAP_LARGE_READX) {
78 readsize = CLI_MAX_LARGE_READX_SIZE;
80 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
83 while (total < size) {
84 readsize = MIN(readsize, size-total);
86 /* Issue a read and receive a reply */
88 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
91 if (!cli_receive_smb(cli))
94 /* Check for error. Make sure to check for DOS and NT
97 if (cli_is_error(cli)) {
98 BOOL recoverable_error = False;
99 NTSTATUS status = NT_STATUS_OK;
103 if (cli_is_nt_error(cli))
104 status = cli_nt_error(cli);
106 cli_dos_error(cli, &eclass, &ecode);
109 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
110 * recoverable error, plus we have valid data in the
111 * packet so don't error out here.
114 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
115 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
116 recoverable_error = True;
118 if (!recoverable_error)
122 size2 = SVAL(cli->inbuf, smb_vwv5);
123 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
125 if (size2 > readsize) {
126 DEBUG(5,("server returned more than we wanted!\n"));
128 } else if (size2 < 0) {
129 DEBUG(5,("read return < 0!\n"));
133 /* Copy data into buffer */
135 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
136 memcpy(buf + total, p, size2);
142 * If the server returned less than we asked for we're at EOF.
145 if (size2 < readsize)
152 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
154 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
155 you must fix ensure you don't attempt to sign the packets - data
156 *will* be currupted */
158 /****************************************************************************
159 Issue a single SMBreadraw and don't wait for a reply.
160 ****************************************************************************/
162 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
166 if (!cli->sign_info.use_smb_signing) {
167 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
171 memset(cli->outbuf,'\0',smb_size);
172 memset(cli->inbuf,'\0',smb_size);
174 set_message(cli->outbuf,10,0,True);
176 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
177 SSVAL(cli->outbuf,smb_tid,cli->cnum);
178 cli_setup_packet(cli);
180 SSVAL(cli->outbuf,smb_vwv0,fnum);
181 SIVAL(cli->outbuf,smb_vwv1,offset);
182 SSVAL(cli->outbuf,smb_vwv2,size);
183 SSVAL(cli->outbuf,smb_vwv3,size);
184 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
186 return cli_send_smb(cli);
189 /****************************************************************************
190 Tester for the readraw call.
191 ****************************************************************************/
193 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
204 * Set readsize to the maximum size we can handle in one readraw.
209 while (total < size) {
210 readsize = MIN(readsize, size-total);
212 /* Issue a read and receive a reply */
214 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
217 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
220 size2 = smb_len(cli->inbuf);
222 if (size2 > readsize) {
223 DEBUG(5,("server returned more than we wanted!\n"));
225 } else if (size2 < 0) {
226 DEBUG(5,("read return < 0!\n"));
230 /* Copy data into buffer */
234 memcpy(buf + total, p, size2);
241 * If the server returned less than we asked for we're at EOF.
244 if (size2 < readsize)
251 /****************************************************************************
252 issue a single SMBwrite and don't wait for a reply
253 ****************************************************************************/
255 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
256 uint16 mode, const char *buf,
260 BOOL large_writex = False;
262 if (size > cli->bufsize) {
263 cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
264 cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
265 if (cli->outbuf == NULL || cli->inbuf == NULL)
267 cli->bufsize = size + 1024;
270 memset(cli->outbuf,'\0',smb_size);
271 memset(cli->inbuf,'\0',smb_size);
273 if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
278 set_message(cli->outbuf,14,0,True);
280 set_message(cli->outbuf,12,0,True);
282 SCVAL(cli->outbuf,smb_com,SMBwriteX);
283 SSVAL(cli->outbuf,smb_tid,cli->cnum);
284 cli_setup_packet(cli);
286 SCVAL(cli->outbuf,smb_vwv0,0xFF);
287 SSVAL(cli->outbuf,smb_vwv2,fnum);
289 SIVAL(cli->outbuf,smb_vwv3,offset);
290 SIVAL(cli->outbuf,smb_vwv5,0);
291 SSVAL(cli->outbuf,smb_vwv7,mode);
293 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
295 * According to CIFS-TR-1p00, this following field should only
296 * be set if CAP_LARGE_WRITEX is set. We should check this
297 * locally. However, this check might already have been
298 * done by our callers.
300 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
301 SSVAL(cli->outbuf,smb_vwv10,size);
302 SSVAL(cli->outbuf,smb_vwv11,
303 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
306 SIVAL(cli->outbuf,smb_vwv12,(offset>>32) & 0xffffffff);
308 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
309 memcpy(p, buf, size);
310 cli_setup_bcc(cli, p+size);
312 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
314 show_msg(cli->outbuf);
315 return cli_send_smb(cli);
318 /****************************************************************************
320 write_mode: 0x0001 disallow write cacheing
321 0x0002 return bytes remaining
322 0x0004 use raw named pipe protocol
323 0x0008 start of message mode named pipe protocol
324 ****************************************************************************/
326 size_t cli_write(struct cli_state *cli,
327 int fnum, uint16 write_mode,
328 const char *buf, off_t offset, size_t size)
334 int block = cli->max_xmit - (smb_size+32);
335 int blocks = (size + (block-1)) / block;
337 if(cli->max_mux > 1) {
338 mpx = cli->max_mux-1;
343 while (received < blocks) {
345 while ((issued - received < mpx) && (issued < blocks)) {
346 int bsent = issued * block;
347 int size1 = MIN(block, size - bsent);
349 if (!cli_issue_write(cli, fnum, offset + bsent,
357 if (!cli_receive_smb(cli))
362 if (cli_is_error(cli))
365 bwritten += SVAL(cli->inbuf, smb_vwv2);
366 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
369 while (received < issued && cli_receive_smb(cli))
375 /****************************************************************************
376 write to a file using a SMBwrite and not bypassing 0 byte writes
377 ****************************************************************************/
379 ssize_t cli_smbwrite(struct cli_state *cli,
380 int fnum, char *buf, off_t offset, size_t size1)
386 size_t size = MIN(size1, cli->max_xmit - 48);
388 memset(cli->outbuf,'\0',smb_size);
389 memset(cli->inbuf,'\0',smb_size);
391 set_message(cli->outbuf,5, 0,True);
393 SCVAL(cli->outbuf,smb_com,SMBwrite);
394 SSVAL(cli->outbuf,smb_tid,cli->cnum);
395 cli_setup_packet(cli);
397 SSVAL(cli->outbuf,smb_vwv0,fnum);
398 SSVAL(cli->outbuf,smb_vwv1,size);
399 SIVAL(cli->outbuf,smb_vwv2,offset);
400 SSVAL(cli->outbuf,smb_vwv4,0);
402 p = smb_buf(cli->outbuf);
404 SSVAL(p, 0, size); p += 2;
405 memcpy(p, buf, size); p += size;
407 cli_setup_bcc(cli, p);
409 if (!cli_send_smb(cli))
412 if (!cli_receive_smb(cli))
415 if (cli_is_error(cli))
418 size = SVAL(cli->inbuf,smb_vwv0);