split clientgen.c into several parts
[sfrench/samba-autobuild/.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         set_message(cli->outbuf,12,size,True);
142         
143         CVAL(cli->outbuf,smb_com) = SMBwriteX;
144         SSVAL(cli->outbuf,smb_tid,cli->cnum);
145         cli_setup_packet(cli);
146         
147         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
148         SSVAL(cli->outbuf,smb_vwv2,fnum);
149
150         SIVAL(cli->outbuf,smb_vwv3,offset);
151         SIVAL(cli->outbuf,smb_vwv5,IS_BITS_SET_ALL(mode, 0x0008) ? 0xFFFFFFFF : 0);
152         SSVAL(cli->outbuf,smb_vwv7,mode);
153
154         SSVAL(cli->outbuf,smb_vwv8,IS_BITS_SET_ALL(mode, 0x0008) ? size : 0);
155         SSVAL(cli->outbuf,smb_vwv10,size);
156         SSVAL(cli->outbuf,smb_vwv11,
157               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
158         
159         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
160         memcpy(p, buf, size);
161
162         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
163         
164         show_msg(cli->outbuf);
165         cli_send_smb(cli);
166 }
167
168 /****************************************************************************
169   write to a file
170   write_mode: 0x0001 disallow write cacheing
171               0x0002 return bytes remaining
172               0x0004 use raw named pipe protocol
173               0x0008 start of message mode named pipe protocol
174 ****************************************************************************/
175 ssize_t cli_write(struct cli_state *cli,
176                   int fnum, uint16 write_mode,
177                   char *buf, off_t offset, size_t size)
178 {
179         int bwritten = 0;
180         int issued = 0;
181         int received = 0;
182         int mpx = MAX(cli->max_mux-1, 1);
183         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
184         int blocks = (size + (block-1)) / block;
185
186         while (received < blocks) {
187
188                 while ((issued - received < mpx) && (issued < blocks))
189                 {
190                         int bsent = issued * block;
191                         int size1 = MIN(block, size - bsent);
192
193                         cli_issue_write(cli, fnum, offset + bsent,
194                                         write_mode,
195                                         buf + bsent,
196                                         size1, issued);
197                         issued++;
198                 }
199
200                 if (!cli_receive_smb(cli))
201                 {
202                         return bwritten;
203                 }
204
205                 received++;
206
207                 if (CVAL(cli->inbuf,smb_rcls) != 0)
208                 {
209                         break;
210                 }
211
212                 bwritten += SVAL(cli->inbuf, smb_vwv2);
213         }
214
215         while (received < issued && cli_receive_smb(cli))
216         {
217                 received++;
218         }
219         
220         return bwritten;
221 }
222
223
224 /****************************************************************************
225   write to a file using a SMBwrite and not bypassing 0 byte writes
226 ****************************************************************************/
227 ssize_t cli_smbwrite(struct cli_state *cli,
228                      int fnum, char *buf, off_t offset, size_t size1)
229 {
230         char *p;
231         ssize_t total = 0;
232
233         do {
234                 size_t size = MIN(size1, cli->max_xmit - 48);
235                 
236                 memset(cli->outbuf,'\0',smb_size);
237                 memset(cli->inbuf,'\0',smb_size);
238
239                 set_message(cli->outbuf,5, 3 + size,True);
240
241                 CVAL(cli->outbuf,smb_com) = SMBwrite;
242                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
243                 cli_setup_packet(cli);
244                 
245                 SSVAL(cli->outbuf,smb_vwv0,fnum);
246                 SSVAL(cli->outbuf,smb_vwv1,size);
247                 SIVAL(cli->outbuf,smb_vwv2,offset);
248                 SSVAL(cli->outbuf,smb_vwv4,0);
249                 
250                 p = smb_buf(cli->outbuf);
251                 *p++ = 1;
252                 SSVAL(p, 0, size);
253                 memcpy(p+2, buf, size);
254                 
255                 cli_send_smb(cli);
256                 if (!cli_receive_smb(cli)) {
257                         return -1;
258                 }
259                 
260                 if (CVAL(cli->inbuf,smb_rcls) != 0) {
261                         return -1;
262                 }
263
264                 size = SVAL(cli->inbuf,smb_vwv0);
265                 if (size == 0) break;
266
267                 size1 -= size;
268                 total += size;
269         } while (size1);
270
271         return total;
272 }
273