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,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
56 return cli_send_smb(cli);
59 /****************************************************************************
60 Read size bytes at offset offset using SMBreadX.
61 ****************************************************************************/
63 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
74 * Set readsize to the maximum size we can handle in one readX,
75 * rounded down to a multiple of 1024.
78 if (cli->capabilities & CAP_LARGE_READX) {
79 readsize = CLI_MAX_LARGE_READX_SIZE;
81 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
84 while (total < size) {
85 readsize = MIN(readsize, size-total);
87 /* Issue a read and receive a reply */
89 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
92 if (!cli_receive_smb(cli))
95 /* Check for error. Make sure to check for DOS and NT
98 if (cli_is_error(cli)) {
99 BOOL recoverable_error = False;
100 NTSTATUS status = NT_STATUS_OK;
104 if (cli_is_nt_error(cli))
105 status = cli_nt_error(cli);
107 cli_dos_error(cli, &eclass, &ecode);
110 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
111 * recoverable error, plus we have valid data in the
112 * packet so don't error out here.
115 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
116 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
117 recoverable_error = True;
119 if (!recoverable_error)
123 size2 = SVAL(cli->inbuf, smb_vwv5);
124 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
126 if (size2 > readsize) {
127 DEBUG(5,("server returned more than we wanted!\n"));
129 } else if (size2 < 0) {
130 DEBUG(5,("read return < 0!\n"));
134 /* Copy data into buffer */
136 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
137 memcpy(buf + total, p, size2);
143 * If the server returned less than we asked for we're at EOF.
146 if (size2 < readsize)
153 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
155 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
156 you must fix ensure you don't attempt to sign the packets - data
157 *will* be currupted */
159 /****************************************************************************
160 Issue a single SMBreadraw and don't wait for a reply.
161 ****************************************************************************/
163 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
167 if (!cli->sign_info.use_smb_signing) {
168 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
172 memset(cli->outbuf,'\0',smb_size);
173 memset(cli->inbuf,'\0',smb_size);
175 set_message(cli->outbuf,10,0,True);
177 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
178 SSVAL(cli->outbuf,smb_tid,cli->cnum);
179 cli_setup_packet(cli);
181 SSVAL(cli->outbuf,smb_vwv0,fnum);
182 SIVAL(cli->outbuf,smb_vwv1,offset);
183 SSVAL(cli->outbuf,smb_vwv2,size);
184 SSVAL(cli->outbuf,smb_vwv3,size);
185 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
187 return cli_send_smb(cli);
190 /****************************************************************************
191 Tester for the readraw call.
192 ****************************************************************************/
194 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
205 * Set readsize to the maximum size we can handle in one readraw.
210 while (total < size) {
211 readsize = MIN(readsize, size-total);
213 /* Issue a read and receive a reply */
215 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
218 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
221 size2 = smb_len(cli->inbuf);
223 if (size2 > readsize) {
224 DEBUG(5,("server returned more than we wanted!\n"));
226 } else if (size2 < 0) {
227 DEBUG(5,("read return < 0!\n"));
231 /* Copy data into buffer */
235 memcpy(buf + total, p, size2);
242 * If the server returned less than we asked for we're at EOF.
245 if (size2 < readsize)
252 /****************************************************************************
253 issue a single SMBwrite and don't wait for a reply
254 ****************************************************************************/
256 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
257 uint16 mode, const char *buf,
261 BOOL large_writex = False;
263 if (size > cli->bufsize) {
264 cli->outbuf = SMB_REALLOC(cli->outbuf, size + 1024);
265 cli->inbuf = SMB_REALLOC(cli->inbuf, size + 1024);
266 if (cli->outbuf == NULL || cli->inbuf == NULL)
268 cli->bufsize = size + 1024;
271 memset(cli->outbuf,'\0',smb_size);
272 memset(cli->inbuf,'\0',smb_size);
274 if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
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,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
310 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
311 memcpy(p, buf, size);
312 cli_setup_bcc(cli, p+size);
314 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
316 show_msg(cli->outbuf);
317 return cli_send_smb(cli);
320 /****************************************************************************
322 write_mode: 0x0001 disallow write cacheing
323 0x0002 return bytes remaining
324 0x0004 use raw named pipe protocol
325 0x0008 start of message mode named pipe protocol
326 ****************************************************************************/
328 ssize_t cli_write(struct cli_state *cli,
329 int fnum, uint16 write_mode,
330 const char *buf, off_t offset, size_t size)
332 ssize_t bwritten = 0;
333 unsigned int issued = 0;
334 unsigned int received = 0;
336 int block = cli->max_xmit - (smb_size+32);
337 int blocks = (size + (block-1)) / block;
339 if(cli->max_mux > 1) {
340 mpx = cli->max_mux-1;
345 while (received < blocks) {
347 while ((issued - received < mpx) && (issued < blocks)) {
348 ssize_t bsent = issued * block;
349 ssize_t size1 = MIN(block, size - bsent);
351 if (!cli_issue_write(cli, fnum, offset + bsent,
359 if (!cli_receive_smb(cli))
364 if (cli_is_error(cli))
367 bwritten += SVAL(cli->inbuf, smb_vwv2);
368 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
371 while (received < issued && cli_receive_smb(cli))
377 /****************************************************************************
378 write to a file using a SMBwrite and not bypassing 0 byte writes
379 ****************************************************************************/
381 ssize_t cli_smbwrite(struct cli_state *cli,
382 int fnum, char *buf, off_t offset, size_t size1)
388 size_t size = MIN(size1, cli->max_xmit - 48);
390 memset(cli->outbuf,'\0',smb_size);
391 memset(cli->inbuf,'\0',smb_size);
393 set_message(cli->outbuf,5, 0,True);
395 SCVAL(cli->outbuf,smb_com,SMBwrite);
396 SSVAL(cli->outbuf,smb_tid,cli->cnum);
397 cli_setup_packet(cli);
399 SSVAL(cli->outbuf,smb_vwv0,fnum);
400 SSVAL(cli->outbuf,smb_vwv1,size);
401 SIVAL(cli->outbuf,smb_vwv2,offset);
402 SSVAL(cli->outbuf,smb_vwv4,0);
404 p = smb_buf(cli->outbuf);
406 SSVAL(p, 0, size); p += 2;
407 memcpy(p, buf, size); p += size;
409 cli_setup_bcc(cli, p);
411 if (!cli_send_smb(cli))
414 if (!cli_receive_smb(cli))
417 if (cli_is_error(cli))
420 size = SVAL(cli->inbuf,smb_vwv0);