2 Unix SMB/Netbios implementation.
4 client file read/write routines
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /****************************************************************************
27 Issue a single SMBread and don't wait for a reply.
28 ****************************************************************************/
30 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
33 memset(cli->outbuf,'\0',smb_size);
34 memset(cli->inbuf,'\0',smb_size);
36 set_message(cli->outbuf,10,0,True);
38 CVAL(cli->outbuf,smb_com) = SMBreadX;
39 SSVAL(cli->outbuf,smb_tid,cli->cnum);
40 cli_setup_packet(cli);
42 CVAL(cli->outbuf,smb_vwv0) = 0xFF;
43 SSVAL(cli->outbuf,smb_vwv2,fnum);
44 SIVAL(cli->outbuf,smb_vwv3,offset);
45 SSVAL(cli->outbuf,smb_vwv5,size);
46 SSVAL(cli->outbuf,smb_vwv6,size);
47 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
49 return cli_send_smb(cli);
52 /****************************************************************************
53 Issue a single SMBreadraw and don't wait for a reply.
54 ****************************************************************************/
56 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
59 memset(cli->outbuf,'\0',smb_size);
60 memset(cli->inbuf,'\0',smb_size);
62 set_message(cli->outbuf,10,0,True);
64 CVAL(cli->outbuf,smb_com) = SMBreadbraw;
65 SSVAL(cli->outbuf,smb_tid,cli->cnum);
66 cli_setup_packet(cli);
68 SSVAL(cli->outbuf,smb_vwv0,fnum);
69 SIVAL(cli->outbuf,smb_vwv1,offset);
70 SSVAL(cli->outbuf,smb_vwv2,size);
71 SSVAL(cli->outbuf,smb_vwv3,size);
72 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
74 return cli_send_smb(cli);
77 /****************************************************************************
78 Read size bytes at offset offset using SMBreadX.
79 ****************************************************************************/
81 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
92 * Set readsize to the maximum size we can handle in one readX,
93 * rounded down to a multiple of 1024.
96 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
98 while (total < size) {
99 readsize = MIN(readsize, size-total);
101 /* Issue a read and receive a reply */
103 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
106 if (!cli_receive_smb(cli))
109 /* Check for error. Make sure to check for DOS and NT
112 if (cli_is_error(cli)) {
116 if (cli_is_nt_error(cli))
117 status = cli_nt_error(cli);
119 cli_dos_error(cli, &eclass, &status);
121 if ((eclass == ERRDOS && status == ERRmoredata) ||
122 status == STATUS_MORE_ENTRIES)
126 size2 = SVAL(cli->inbuf, smb_vwv5);
128 if (size2 > readsize) {
129 DEBUG(5,("server returned more than we wanted!\n"));
131 } else if (size2 < 0) {
132 DEBUG(5,("read return < 0!\n"));
136 /* Copy data into buffer */
138 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
139 memcpy(buf + total, p, size2);
145 * If the server returned less than we asked for we're at EOF.
148 if (size2 < readsize)
155 /****************************************************************************
156 Tester for the readraw call.
157 ****************************************************************************/
159 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
170 * Set readsize to the maximum size we can handle in one readraw.
175 while (total < size) {
176 readsize = MIN(readsize, size-total);
178 /* Issue a read and receive a reply */
180 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
183 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
186 size2 = smb_len(cli->inbuf);
188 if (size2 > readsize) {
189 DEBUG(5,("server returned more than we wanted!\n"));
191 } else if (size2 < 0) {
192 DEBUG(5,("read return < 0!\n"));
196 /* Copy data into buffer */
200 memcpy(buf + total, p, size2);
207 * If the server returned less than we asked for we're at EOF.
210 if (size2 < readsize)
217 /****************************************************************************
218 issue a single SMBwrite and don't wait for a reply
219 ****************************************************************************/
221 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
226 memset(cli->outbuf,'\0',smb_size);
227 memset(cli->inbuf,'\0',smb_size);
230 set_message(cli->outbuf,14,0,True);
232 set_message(cli->outbuf,12,0,True);
234 CVAL(cli->outbuf,smb_com) = SMBwriteX;
235 SSVAL(cli->outbuf,smb_tid,cli->cnum);
236 cli_setup_packet(cli);
238 CVAL(cli->outbuf,smb_vwv0) = 0xFF;
239 SSVAL(cli->outbuf,smb_vwv2,fnum);
241 SIVAL(cli->outbuf,smb_vwv3,offset);
242 SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
243 SSVAL(cli->outbuf,smb_vwv7,mode);
245 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
246 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
247 SSVAL(cli->outbuf,smb_vwv10,size);
248 SSVAL(cli->outbuf,smb_vwv11,
249 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
251 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
252 memcpy(p, buf, size);
253 cli_setup_bcc(cli, p+size);
255 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
257 show_msg(cli->outbuf);
258 return cli_send_smb(cli);
261 /****************************************************************************
263 write_mode: 0x0001 disallow write cacheing
264 0x0002 return bytes remaining
265 0x0004 use raw named pipe protocol
266 0x0008 start of message mode named pipe protocol
267 ****************************************************************************/
269 ssize_t cli_write(struct cli_state *cli,
270 int fnum, uint16 write_mode,
271 char *buf, off_t offset, size_t size)
276 int mpx = MAX(cli->max_mux-1, 1);
277 int block = (cli->max_xmit - (smb_size+32)) & ~1023;
278 int blocks = (size + (block-1)) / block;
280 while (received < blocks) {
282 while ((issued - received < mpx) && (issued < blocks)) {
283 int bsent = issued * block;
284 int size1 = MIN(block, size - bsent);
286 if (!cli_issue_write(cli, fnum, offset + bsent,
294 if (!cli_receive_smb(cli))
299 if (CVAL(cli->inbuf,smb_rcls) != 0)
302 bwritten += SVAL(cli->inbuf, smb_vwv2);
305 while (received < issued && cli_receive_smb(cli))
311 /****************************************************************************
312 write to a file using a SMBwrite and not bypassing 0 byte writes
313 ****************************************************************************/
315 ssize_t cli_smbwrite(struct cli_state *cli,
316 int fnum, char *buf, off_t offset, size_t size1)
322 size_t size = MIN(size1, cli->max_xmit - 48);
324 memset(cli->outbuf,'\0',smb_size);
325 memset(cli->inbuf,'\0',smb_size);
327 set_message(cli->outbuf,5, 0,True);
329 CVAL(cli->outbuf,smb_com) = SMBwrite;
330 SSVAL(cli->outbuf,smb_tid,cli->cnum);
331 cli_setup_packet(cli);
333 SSVAL(cli->outbuf,smb_vwv0,fnum);
334 SSVAL(cli->outbuf,smb_vwv1,size);
335 SIVAL(cli->outbuf,smb_vwv2,offset);
336 SSVAL(cli->outbuf,smb_vwv4,0);
338 p = smb_buf(cli->outbuf);
340 SSVAL(p, 0, size); p += 2;
341 memcpy(p, buf, size); p += size;
343 cli_setup_bcc(cli, p);
345 if (!cli_send_smb(cli))
348 if (!cli_receive_smb(cli))
351 if (CVAL(cli->inbuf,smb_rcls) != 0)
354 size = SVAL(cli->inbuf,smb_vwv0);