Set correct reply word in large writeX (greater than 64k) replies.
[kai/samba.git] / source3 / libsmb / clireadwrite.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client file read/write routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27 issue a single SMBread and don't wait for a reply
28 ****************************************************************************/
29 static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
30                            size_t size, int i)
31 {
32         memset(cli->outbuf,'\0',smb_size);
33         memset(cli->inbuf,'\0',smb_size);
34
35         set_message(cli->outbuf,10,0,True);
36                 
37         CVAL(cli->outbuf,smb_com) = SMBreadX;
38         SSVAL(cli->outbuf,smb_tid,cli->cnum);
39         cli_setup_packet(cli);
40
41         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
42         SSVAL(cli->outbuf,smb_vwv2,fnum);
43         SIVAL(cli->outbuf,smb_vwv3,offset);
44         SSVAL(cli->outbuf,smb_vwv5,size);
45         SSVAL(cli->outbuf,smb_vwv6,size);
46         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
47
48         cli_send_smb(cli);
49 }
50
51 /****************************************************************************
52   read from a file
53 ****************************************************************************/
54 size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
55 {
56         char *p;
57         int total = -1;
58         int issued=0;
59         int received=0;
60 /*
61  * There is a problem in this code when mpx is more than one.
62  * for some reason files can get corrupted when being read.
63  * Until we understand this fully I am serializing reads (one
64  * read/one reply) for now. JRA.
65  */
66 #if 0
67         int mpx = MAX(cli->max_mux-1, 1); 
68 #else
69         int mpx = 1;
70 #endif
71         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
72         int mid;
73         int blocks = (size + (block-1)) / block;
74
75         if (size == 0) return 0;
76
77         while (received < blocks) {
78                 int size2;
79
80                 while (issued - received < mpx && issued < blocks) {
81                         int size1 = MIN(block, size-issued*block);
82                         cli_issue_read(cli, fnum, offset+issued*block, size1, issued);
83                         issued++;
84                 }
85
86                 if (!cli_receive_smb(cli)) {
87                         return total;
88                 }
89
90                 received++;
91                 mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
92                 size2 = SVAL(cli->inbuf, smb_vwv5);
93
94                 if (CVAL(cli->inbuf,smb_rcls) != 0) {
95                         blocks = MIN(blocks, mid-1);
96                         continue;
97                 }
98
99                 if (size2 <= 0) {
100                         blocks = MIN(blocks, mid-1);
101                         /* this distinguishes EOF from an error */
102                         total = MAX(total, 0);
103                         continue;
104                 }
105
106                 if (size2 > block) {
107                         DEBUG(0,("server returned more than we wanted!\n"));
108                         return -1;
109                 }
110                 if (mid >= issued) {
111                         DEBUG(0,("invalid mid from server!\n"));
112                         return -1;
113                 }
114                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
115
116                 memcpy(buf+mid*block, p, size2);
117
118                 total = MAX(total, mid*block + size2);
119         }
120
121         while (received < issued) {
122                 cli_receive_smb(cli);
123                 received++;
124         }
125         
126         return total;
127 }
128
129
130 /****************************************************************************
131 issue a single SMBwrite and don't wait for a reply
132 ****************************************************************************/
133 static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
134                             size_t size, int i)
135 {
136         char *p;
137
138         memset(cli->outbuf,'\0',smb_size);
139         memset(cli->inbuf,'\0',smb_size);
140
141         if (size > 0xFFFF)
142                 set_message(cli->outbuf,14,size,True);
143         else
144                 set_message(cli->outbuf,12,size,True);
145         
146         CVAL(cli->outbuf,smb_com) = SMBwriteX;
147         SSVAL(cli->outbuf,smb_tid,cli->cnum);
148         cli_setup_packet(cli);
149         
150         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
151         SSVAL(cli->outbuf,smb_vwv2,fnum);
152
153         SIVAL(cli->outbuf,smb_vwv3,offset);
154         SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
155         SSVAL(cli->outbuf,smb_vwv7,mode);
156
157         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
158         SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
159         SSVAL(cli->outbuf,smb_vwv10,size);
160         SSVAL(cli->outbuf,smb_vwv11,
161               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
162         
163         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
164         memcpy(p, buf, size);
165
166         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
167         
168         show_msg(cli->outbuf);
169         cli_send_smb(cli);
170 }
171
172 /****************************************************************************
173   write to a file
174   write_mode: 0x0001 disallow write cacheing
175               0x0002 return bytes remaining
176               0x0004 use raw named pipe protocol
177               0x0008 start of message mode named pipe protocol
178 ****************************************************************************/
179 ssize_t cli_write(struct cli_state *cli,
180                   int fnum, uint16 write_mode,
181                   char *buf, off_t offset, size_t size)
182 {
183         int bwritten = 0;
184         int issued = 0;
185         int received = 0;
186         int mpx = MAX(cli->max_mux-1, 1);
187         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
188         int blocks = (size + (block-1)) / block;
189
190         while (received < blocks) {
191
192                 while ((issued - received < mpx) && (issued < blocks))
193                 {
194                         int bsent = issued * block;
195                         int size1 = MIN(block, size - bsent);
196
197                         cli_issue_write(cli, fnum, offset + bsent,
198                                         write_mode,
199                                         buf + bsent,
200                                         size1, issued);
201                         issued++;
202                 }
203
204                 if (!cli_receive_smb(cli))
205                 {
206                         return bwritten;
207                 }
208
209                 received++;
210
211                 if (CVAL(cli->inbuf,smb_rcls) != 0)
212                 {
213                         break;
214                 }
215
216                 bwritten += SVAL(cli->inbuf, smb_vwv2);
217         }
218
219         while (received < issued && cli_receive_smb(cli))
220         {
221                 received++;
222         }
223         
224         return bwritten;
225 }
226
227
228 /****************************************************************************
229   write to a file using a SMBwrite and not bypassing 0 byte writes
230 ****************************************************************************/
231 ssize_t cli_smbwrite(struct cli_state *cli,
232                      int fnum, char *buf, off_t offset, size_t size1)
233 {
234         char *p;
235         ssize_t total = 0;
236
237         do {
238                 size_t size = MIN(size1, cli->max_xmit - 48);
239                 
240                 memset(cli->outbuf,'\0',smb_size);
241                 memset(cli->inbuf,'\0',smb_size);
242
243                 set_message(cli->outbuf,5, 3 + size,True);
244
245                 CVAL(cli->outbuf,smb_com) = SMBwrite;
246                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
247                 cli_setup_packet(cli);
248                 
249                 SSVAL(cli->outbuf,smb_vwv0,fnum);
250                 SSVAL(cli->outbuf,smb_vwv1,size);
251                 SIVAL(cli->outbuf,smb_vwv2,offset);
252                 SSVAL(cli->outbuf,smb_vwv4,0);
253                 
254                 p = smb_buf(cli->outbuf);
255                 *p++ = 1;
256                 SSVAL(p, 0, size);
257                 memcpy(p+2, buf, size);
258                 
259                 cli_send_smb(cli);
260                 if (!cli_receive_smb(cli)) {
261                         return -1;
262                 }
263                 
264                 if (CVAL(cli->inbuf,smb_rcls) != 0) {
265                         return -1;
266                 }
267
268                 size = SVAL(cli->inbuf,smb_vwv0);
269                 if (size == 0) break;
270
271                 size1 -= size;
272                 total += size;
273         } while (size1);
274
275         return total;
276 }
277