r14173: change smb interface structures to always use
[gd/samba-autobuild/.git] / source4 / libcli / raw / rawreadwrite.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) James Myers 2003
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 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24
25 #define SETUP_REQUEST(cmd, wct, buflen) do { \
26         req = smbcli_request_setup(tree, cmd, wct, buflen); \
27         if (!req) return NULL; \
28 } while (0)
29
30 /****************************************************************************
31  low level read operation (async send)
32 ****************************************************************************/
33 struct smbcli_request *smb_raw_read_send(struct smbcli_tree *tree, union smb_read *parms)
34 {
35         BOOL bigoffset = False;
36         struct smbcli_request *req = NULL; 
37
38         switch (parms->generic.level) {
39         case RAW_READ_READBRAW:
40                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
41                         bigoffset = True;
42                 }
43                 SETUP_REQUEST(SMBreadbraw, bigoffset? 10:8, 0);
44                 SSVAL(req->out.vwv, VWV(0), parms->readbraw.file.fnum);
45                 SIVAL(req->out.vwv, VWV(1), parms->readbraw.in.offset);
46                 SSVAL(req->out.vwv, VWV(3), parms->readbraw.in.maxcnt);
47                 SSVAL(req->out.vwv, VWV(4), parms->readbraw.in.mincnt);
48                 SIVAL(req->out.vwv, VWV(5), parms->readbraw.in.timeout);
49                 SSVAL(req->out.vwv, VWV(7), 0); /* reserved */
50                 if (bigoffset) {
51                         SIVAL(req->out.vwv, VWV(8),parms->readbraw.in.offset>>32);
52                 }
53                 break;
54
55         case RAW_READ_LOCKREAD:
56                 SETUP_REQUEST(SMBlockread, 5, 0);
57                 SSVAL(req->out.vwv, VWV(0), parms->lockread.file.fnum);
58                 SSVAL(req->out.vwv, VWV(1), parms->lockread.in.count);
59                 SIVAL(req->out.vwv, VWV(2), parms->lockread.in.offset);
60                 SSVAL(req->out.vwv, VWV(4), parms->lockread.in.remaining);
61                 break;
62
63         case RAW_READ_READ:
64                 SETUP_REQUEST(SMBread, 5, 0);
65                 SSVAL(req->out.vwv, VWV(0), parms->read.file.fnum);
66                 SSVAL(req->out.vwv, VWV(1), parms->read.in.count);
67                 SIVAL(req->out.vwv, VWV(2), parms->read.in.offset);
68                 SSVAL(req->out.vwv, VWV(4), parms->read.in.remaining);
69                 break;
70
71         case RAW_READ_READX:
72                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
73                         bigoffset = True;
74                 }
75                 SETUP_REQUEST(SMBreadX, bigoffset ? 12 : 10, 0);
76                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
77                 SSVAL(req->out.vwv, VWV(1), 0);
78                 SSVAL(req->out.vwv, VWV(2), parms->readx.file.fnum);
79                 SIVAL(req->out.vwv, VWV(3), parms->readx.in.offset);
80                 SSVAL(req->out.vwv, VWV(5), parms->readx.in.maxcnt & 0xFFFF);
81                 SSVAL(req->out.vwv, VWV(6), parms->readx.in.mincnt);
82                 SIVAL(req->out.vwv, VWV(7), parms->readx.in.maxcnt >> 16);
83                 SSVAL(req->out.vwv, VWV(9), parms->readx.in.remaining);
84                 if (bigoffset) {
85                         SIVAL(req->out.vwv, VWV(10),parms->readx.in.offset>>32);
86                 }
87                 break;
88         }
89
90         if (!smbcli_request_send(req)) {
91                 smbcli_request_destroy(req);
92                 return NULL;
93         }
94
95         /* the transport layer needs to know that a readbraw is pending
96            and handle receives a little differently */
97         if (parms->generic.level == RAW_READ_READBRAW) {
98                 tree->session->transport->readbraw_pending = 1;
99         }
100
101         return req;
102 }
103
104 /****************************************************************************
105  low level read operation (async recv)
106 ****************************************************************************/
107 NTSTATUS smb_raw_read_recv(struct smbcli_request *req, union smb_read *parms)
108 {
109         if (!smbcli_request_receive(req) ||
110             smbcli_request_is_error(req)) {
111                 goto failed;
112         }
113
114         switch (parms->generic.level) {
115         case RAW_READ_READBRAW:
116                 parms->readbraw.out.nread = req->in.size - NBT_HDR_SIZE;
117                 if (parms->readbraw.out.nread > 
118                     MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt)) {
119                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
120                         goto failed;
121                 }
122                 memcpy(parms->readbraw.out.data, req->in.buffer + NBT_HDR_SIZE, parms->readbraw.out.nread);
123                 break;
124                 
125         case RAW_READ_LOCKREAD:
126                 SMBCLI_CHECK_WCT(req, 5);
127                 parms->lockread.out.nread = SVAL(req->in.vwv, VWV(0));
128                 if (parms->lockread.out.nread > parms->lockread.in.count ||
129                     !smbcli_raw_pull_data(req, req->in.data+3, 
130                                        parms->lockread.out.nread, parms->lockread.out.data)) {
131                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
132                 }
133                 break;
134
135         case RAW_READ_READ:
136                 /* there are 4 reserved words in the reply */
137                 SMBCLI_CHECK_WCT(req, 5);
138                 parms->read.out.nread = SVAL(req->in.vwv, VWV(0));
139                 if (parms->read.out.nread > parms->read.in.count ||
140                     !smbcli_raw_pull_data(req, req->in.data+3, 
141                                        parms->read.out.nread, parms->read.out.data)) {
142                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
143                 }
144                 break;
145
146         case RAW_READ_READX:
147                 /* there are 5 reserved words in the reply */
148                 SMBCLI_CHECK_WCT(req, 12);
149                 parms->readx.out.remaining       = SVAL(req->in.vwv, VWV(2));
150                 parms->readx.out.compaction_mode = SVAL(req->in.vwv, VWV(3));
151                 parms->readx.out.nread = SVAL(req->in.vwv, VWV(5));
152                 if (parms->readx.out.nread > MAX(parms->readx.in.mincnt, parms->readx.in.maxcnt) ||
153                     !smbcli_raw_pull_data(req, req->in.hdr + SVAL(req->in.vwv, VWV(6)), 
154                                        parms->readx.out.nread, 
155                                        parms->readx.out.data)) {
156                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
157                 }
158                 break;
159         }
160
161 failed:
162         return smbcli_request_destroy(req);
163 }
164
165 /****************************************************************************
166  low level read operation (sync interface)
167 ****************************************************************************/
168 NTSTATUS smb_raw_read(struct smbcli_tree *tree, union smb_read *parms)
169 {
170         struct smbcli_request *req = smb_raw_read_send(tree, parms);
171         return smb_raw_read_recv(req, parms);
172 }
173
174
175 /****************************************************************************
176  raw write interface (async send)
177 ****************************************************************************/
178 struct smbcli_request *smb_raw_write_send(struct smbcli_tree *tree, union smb_write *parms)
179 {
180         BOOL bigoffset = False;
181         struct smbcli_request *req = NULL; 
182
183         switch (parms->generic.level) {
184         case RAW_WRITE_WRITEUNLOCK:
185                 SETUP_REQUEST(SMBwriteunlock, 5, 3 + parms->writeunlock.in.count);
186                 SSVAL(req->out.vwv, VWV(0), parms->writeunlock.file.fnum);
187                 SSVAL(req->out.vwv, VWV(1), parms->writeunlock.in.count);
188                 SIVAL(req->out.vwv, VWV(2), parms->writeunlock.in.offset);
189                 SSVAL(req->out.vwv, VWV(4), parms->writeunlock.in.remaining);
190                 SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
191                 SSVAL(req->out.data, 1, parms->writeunlock.in.count);
192                 if (parms->writeunlock.in.count > 0) {
193                         memcpy(req->out.data+3, parms->writeunlock.in.data, 
194                                parms->writeunlock.in.count);
195                 }
196                 break;
197
198         case RAW_WRITE_WRITE:
199                 SETUP_REQUEST(SMBwrite, 5,  3 + parms->write.in.count);
200                 SSVAL(req->out.vwv, VWV(0), parms->write.file.fnum);
201                 SSVAL(req->out.vwv, VWV(1), parms->write.in.count);
202                 SIVAL(req->out.vwv, VWV(2), parms->write.in.offset);
203                 SSVAL(req->out.vwv, VWV(4), parms->write.in.remaining);
204                 SCVAL(req->out.data, 0, SMB_DATA_BLOCK);
205                 SSVAL(req->out.data, 1, parms->write.in.count);
206                 if (parms->write.in.count > 0) {
207                         memcpy(req->out.data+3, parms->write.in.data, parms->write.in.count);
208                 }
209                 break;
210
211         case RAW_WRITE_WRITECLOSE:
212                 SETUP_REQUEST(SMBwriteclose, 6, 1 + parms->writeclose.in.count);
213                 SSVAL(req->out.vwv, VWV(0), parms->writeclose.file.fnum);
214                 SSVAL(req->out.vwv, VWV(1), parms->writeclose.in.count);
215                 SIVAL(req->out.vwv, VWV(2), parms->writeclose.in.offset);
216                 raw_push_dos_date3(tree->session->transport,
217                                   req->out.vwv, VWV(4), parms->writeclose.in.mtime);
218                 SCVAL(req->out.data, 0, 0);
219                 if (parms->writeclose.in.count > 0) {
220                         memcpy(req->out.data+1, parms->writeclose.in.data, 
221                                parms->writeclose.in.count);
222                 }
223                 break;
224
225         case RAW_WRITE_WRITEX:
226                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
227                         bigoffset = True;
228                 }
229                 SETUP_REQUEST(SMBwriteX, bigoffset ? 14 : 12, parms->writex.in.count);
230                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
231                 SSVAL(req->out.vwv, VWV(1), 0);
232                 SSVAL(req->out.vwv, VWV(2), parms->writex.file.fnum);
233                 SIVAL(req->out.vwv, VWV(3), parms->writex.in.offset);
234                 SIVAL(req->out.vwv, VWV(5), 0); /* reserved */
235                 SSVAL(req->out.vwv, VWV(7), parms->writex.in.wmode);
236                 SSVAL(req->out.vwv, VWV(8), parms->writex.in.remaining);
237                 SSVAL(req->out.vwv, VWV(9), parms->writex.in.count>>16);
238                 SSVAL(req->out.vwv, VWV(10), parms->writex.in.count);
239                 SSVAL(req->out.vwv, VWV(11), PTR_DIFF(req->out.data, req->out.hdr));
240                 if (bigoffset) {
241                         SIVAL(req->out.vwv,VWV(12),parms->writex.in.offset>>32);
242                 }
243                 if (parms->writex.in.count > 0) {
244                         memcpy(req->out.data, parms->writex.in.data, parms->writex.in.count);
245                 }
246                 break;
247
248         case RAW_WRITE_SPLWRITE:
249                 SETUP_REQUEST(SMBsplwr, 1, parms->splwrite.in.count);
250                 SSVAL(req->out.vwv, VWV(0), parms->splwrite.file.fnum);
251                 if (parms->splwrite.in.count > 0) {
252                         memcpy(req->out.data, parms->splwrite.in.data, parms->splwrite.in.count);
253                 }
254                 break;
255         }
256
257         if (!smbcli_request_send(req)) {
258                 smbcli_request_destroy(req);
259                 return NULL;
260         }
261
262         return req;
263 }
264
265
266 /****************************************************************************
267  raw write interface (async recv)
268 ****************************************************************************/
269 NTSTATUS smb_raw_write_recv(struct smbcli_request *req, union smb_write *parms)
270 {
271         if (!smbcli_request_receive(req) ||
272             smbcli_request_is_error(req)) {
273                 goto failed;
274         }
275
276         switch (parms->generic.level) {
277         case RAW_WRITE_WRITEUNLOCK:
278                 SMBCLI_CHECK_WCT(req, 1);               
279                 parms->writeunlock.out.nwritten = SVAL(req->in.vwv, VWV(0));
280                 break;
281         case RAW_WRITE_WRITE:
282                 SMBCLI_CHECK_WCT(req, 1);
283                 parms->write.out.nwritten = SVAL(req->in.vwv, VWV(0));
284                 break;
285         case RAW_WRITE_WRITECLOSE:
286                 SMBCLI_CHECK_WCT(req, 1);
287                 parms->writeclose.out.nwritten = SVAL(req->in.vwv, VWV(0));
288                 break;
289         case RAW_WRITE_WRITEX:
290                 SMBCLI_CHECK_WCT(req, 6);
291                 parms->writex.out.nwritten  = SVAL(req->in.vwv, VWV(2));
292                 parms->writex.out.nwritten += (CVAL(req->in.vwv, VWV(4)) << 16);
293                 parms->writex.out.remaining = SVAL(req->in.vwv, VWV(3));
294                 break;
295         case RAW_WRITE_SPLWRITE:
296                 break;
297         }
298
299 failed:
300         return smbcli_request_destroy(req);
301 }
302
303 /****************************************************************************
304  raw write interface (sync interface)
305 ****************************************************************************/
306 NTSTATUS smb_raw_write(struct smbcli_tree *tree, union smb_write *parms)
307 {
308         struct smbcli_request *req = smb_raw_write_send(tree, parms);
309         return smb_raw_write_recv(req, parms);
310 }