r11671: - make sure req is initialized
[gd/samba-autobuild/.git] / source4 / libcli / smb2 / transport.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client transport context management functions
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "lib/socket/socket.h"
27 #include "lib/events/events.h"
28 #include "lib/stream/packet.h"
29 #include "include/dlinklist.h"
30
31
32 /*
33   an event has happened on the socket
34 */
35 static void smb2_transport_event_handler(struct event_context *ev, 
36                                          struct fd_event *fde, 
37                                          uint16_t flags, void *private)
38 {
39         struct smb2_transport *transport = talloc_get_type(private,
40                                                            struct smb2_transport);
41         if (flags & EVENT_FD_READ) {
42                 packet_recv(transport->packet);
43                 return;
44         }
45         if (flags & EVENT_FD_WRITE) {
46                 packet_queue_run(transport->packet);
47         }
48 }
49
50 /*
51   destroy a transport
52  */
53 static int transport_destructor(void *ptr)
54 {
55         struct smb2_transport *transport = ptr;
56         smb2_transport_dead(transport);
57         return 0;
58 }
59
60
61 /*
62   handle receive errors
63 */
64 static void smb2_transport_error(void *private, NTSTATUS status)
65 {
66         struct smb2_transport *transport = talloc_get_type(private, 
67                                                            struct smb2_transport);
68         smb2_transport_dead(transport);
69 }
70
71 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob);
72
73 /*
74   create a transport structure based on an established socket
75 */
76 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
77                                            TALLOC_CTX *parent_ctx)
78 {
79         struct smb2_transport *transport;
80
81         transport = talloc_zero(parent_ctx, struct smb2_transport);
82         if (!transport) return NULL;
83
84         transport->socket = talloc_steal(transport, sock);
85
86         /* setup the stream -> packet parser */
87         transport->packet = packet_init(transport);
88         if (transport->packet == NULL) {
89                 talloc_free(transport);
90                 return NULL;
91         }
92         packet_set_private(transport->packet, transport);
93         packet_set_socket(transport->packet, transport->socket->sock);
94         packet_set_callback(transport->packet, smb2_transport_finish_recv);
95         packet_set_full_request(transport->packet, packet_full_request_nbt);
96         packet_set_error_handler(transport->packet, smb2_transport_error);
97         packet_set_event_context(transport->packet, transport->socket->event.ctx);
98         packet_set_nofree(transport->packet);
99
100         /* take over event handling from the socket layer - it only
101            handles events up until we are connected */
102         talloc_free(transport->socket->event.fde);
103         transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
104                                                     transport->socket,
105                                                     socket_get_fd(transport->socket->sock),
106                                                     EVENT_FD_READ,
107                                                     smb2_transport_event_handler,
108                                                     transport);
109
110         packet_set_serialise(transport->packet, transport->socket->event.fde);
111
112         talloc_set_destructor(transport, transport_destructor);
113
114         transport->options.timeout = 30;
115
116         return transport;
117 }
118
119 /*
120   mark the transport as dead
121 */
122 void smb2_transport_dead(struct smb2_transport *transport)
123 {
124         smbcli_sock_dead(transport->socket);
125
126         /* kill all pending receives */
127         while (transport->pending_recv) {
128                 struct smb2_request *req = transport->pending_recv;
129                 req->state = SMB2_REQUEST_ERROR;
130                 req->status = NT_STATUS_NET_WRITE_FAULT;
131                 DLIST_REMOVE(transport->pending_recv, req);
132                 if (req->async.fn) {
133                         req->async.fn(req);
134                 }
135         }
136 }
137
138 /*
139   we have a full request in our receive buffer - match it to a pending request
140   and process
141  */
142 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob)
143 {
144         struct smb2_transport *transport = talloc_get_type(private, 
145                                                              struct smb2_transport);
146         uint8_t *buffer, *hdr;
147         int len;
148         struct smb2_request *req = NULL;
149         uint64_t seqnum;
150
151         buffer = blob.data;
152         len = blob.length;
153
154         hdr = buffer+NBT_HDR_SIZE;
155
156         if (len < SMB2_MIN_SIZE) {
157                 DEBUG(1,("Discarding smb2 reply of size %d\n", len));
158                 goto error;
159         }
160
161         seqnum = BVAL(hdr, SMB2_HDR_SEQNUM);
162
163         /* match the incoming request against the list of pending requests */
164         for (req=transport->pending_recv; req; req=req->next) {
165                 if (req->seqnum == seqnum) break;
166         }
167
168         if (!req) {
169                 DEBUG(1,("Discarding unmatched reply with seqnum 0x%llx op %d\n", 
170                          seqnum, SVAL(hdr, SMB2_HDR_OPCODE)));
171                 goto error;
172         }
173
174         /* fill in the 'in' portion of the matching request */
175         req->in.buffer = buffer;
176         talloc_steal(req, buffer);
177         req->in.size = len;
178         req->in.allocated = req->in.size;
179
180         req->in.hdr       = hdr;
181         req->in.body      = hdr+SMB2_HDR_BODY;
182         req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
183         req->in.ptr       = req->in.body;
184         req->status       = NT_STATUS(IVAL(hdr, SMB2_HDR_STATUS));
185
186         DEBUG(2, ("SMB2 RECV seqnum=0x%llx\n", req->seqnum));
187         dump_data(2, req->in.body, req->in.body_size);
188
189         /* if this request has an async handler then call that to
190            notify that the reply has been received. This might destroy
191            the request so it must happen last */
192         DLIST_REMOVE(transport->pending_recv, req);
193         req->state = SMB2_REQUEST_DONE;
194         if (req->async.fn) {
195                 req->async.fn(req);
196         }
197         return NT_STATUS_OK;
198
199 error:
200         dump_data(2, buffer, len);
201         if (req) {
202                 DLIST_REMOVE(transport->pending_recv, req);
203                 req->state = SMB2_REQUEST_ERROR;
204                 if (req->async.fn) {
205                         req->async.fn(req);
206                 }
207         } else {
208                 talloc_free(buffer);
209         }
210         return NT_STATUS_UNSUCCESSFUL;
211 }
212
213 /*
214   handle timeouts of individual smb requests
215 */
216 static void smb2_timeout_handler(struct event_context *ev, struct timed_event *te, 
217                                  struct timeval t, void *private)
218 {
219         struct smb2_request *req = talloc_get_type(private, struct smb2_request);
220
221         if (req->state == SMB2_REQUEST_RECV) {
222                 DLIST_REMOVE(req->transport->pending_recv, req);
223         }
224         req->status = NT_STATUS_IO_TIMEOUT;
225         req->state = SMB2_REQUEST_ERROR;
226         if (req->async.fn) {
227                 req->async.fn(req);
228         }
229 }
230
231
232 /*
233   destroy a request
234 */
235 static int smb2_request_destructor(void *ptr)
236 {
237         struct smb2_request *req = talloc_get_type(ptr, struct smb2_request);
238         if (req->state == SMB2_REQUEST_RECV) {
239                 DLIST_REMOVE(req->transport->pending_recv, req);
240         }
241         return 0;
242 }
243
244
245 /*
246   put a request into the send queue
247 */
248 void smb2_transport_send(struct smb2_request *req)
249 {
250         DATA_BLOB blob;
251         NTSTATUS status;
252
253         _smb_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
254
255         DEBUG(2, ("SMB2 send seqnum=0x%llx\n", req->seqnum));
256         dump_data(2, req->out.body, req->out.body_size);
257
258         /* check if the transport is dead */
259         if (req->transport->socket->sock == NULL) {
260                 req->state = SMB2_REQUEST_ERROR;
261                 req->status = NT_STATUS_NET_WRITE_FAULT;
262                 return;
263         }
264
265         blob = data_blob_const(req->out.buffer, req->out.size);
266         status = packet_send(req->transport->packet, blob);
267         if (!NT_STATUS_IS_OK(status)) {
268                 req->state = SMB2_REQUEST_ERROR;
269                 req->status = status;
270                 return;
271         }
272
273         req->state = SMB2_REQUEST_RECV;
274         DLIST_ADD(req->transport->pending_recv, req);
275
276         /* add a timeout */
277         if (req->transport->options.timeout) {
278                 event_add_timed(req->transport->socket->event.ctx, req, 
279                                 timeval_current_ofs(req->transport->options.timeout, 0), 
280                                 smb2_timeout_handler, req);
281         }
282
283         talloc_set_destructor(req, smb2_request_destructor);
284 }