2 Unix SMB/CIFS implementation.
4 SMB2 client transport context management functions
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
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"
33 an event has happened on the socket
35 static void smb2_transport_event_handler(struct event_context *ev,
37 uint16_t flags, void *private)
39 struct smb2_transport *transport = talloc_get_type(private,
40 struct smb2_transport);
41 if (flags & EVENT_FD_READ) {
42 packet_recv(transport->packet);
45 if (flags & EVENT_FD_WRITE) {
46 packet_queue_run(transport->packet);
53 static int transport_destructor(void *ptr)
55 struct smb2_transport *transport = ptr;
56 smb2_transport_dead(transport);
64 static void smb2_transport_error(void *private, NTSTATUS status)
66 struct smb2_transport *transport = talloc_get_type(private,
67 struct smb2_transport);
68 smb2_transport_dead(transport);
71 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob);
74 create a transport structure based on an established socket
76 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
77 TALLOC_CTX *parent_ctx)
79 struct smb2_transport *transport;
81 transport = talloc_zero(parent_ctx, struct smb2_transport);
82 if (!transport) return NULL;
84 transport->socket = talloc_steal(transport, sock);
86 /* setup the stream -> packet parser */
87 transport->packet = packet_init(transport);
88 if (transport->packet == NULL) {
89 talloc_free(transport);
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);
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,
105 socket_get_fd(transport->socket->sock),
107 smb2_transport_event_handler,
110 packet_set_fde(transport->packet, transport->socket->event.fde);
111 packet_set_serialise(transport->packet);
113 talloc_set_destructor(transport, transport_destructor);
115 transport->options.timeout = 30;
121 mark the transport as dead
123 void smb2_transport_dead(struct smb2_transport *transport)
125 smbcli_sock_dead(transport->socket);
127 /* kill all pending receives */
128 while (transport->pending_recv) {
129 struct smb2_request *req = transport->pending_recv;
130 req->state = SMB2_REQUEST_ERROR;
131 req->status = NT_STATUS_NET_WRITE_FAULT;
132 DLIST_REMOVE(transport->pending_recv, req);
140 we have a full request in our receive buffer - match it to a pending request
143 static NTSTATUS smb2_transport_finish_recv(void *private, DATA_BLOB blob)
145 struct smb2_transport *transport = talloc_get_type(private,
146 struct smb2_transport);
147 uint8_t *buffer, *hdr;
149 struct smb2_request *req = NULL;
155 hdr = buffer+NBT_HDR_SIZE;
157 if (len < SMB2_MIN_SIZE) {
158 DEBUG(1,("Discarding smb2 reply of size %d\n", len));
162 seqnum = BVAL(hdr, SMB2_HDR_SEQNUM);
164 /* match the incoming request against the list of pending requests */
165 for (req=transport->pending_recv; req; req=req->next) {
166 if (req->seqnum == seqnum) break;
170 DEBUG(1,("Discarding unmatched reply with seqnum 0x%llx op %d\n",
171 seqnum, SVAL(hdr, SMB2_HDR_OPCODE)));
175 /* fill in the 'in' portion of the matching request */
176 req->in.buffer = buffer;
177 talloc_steal(req, buffer);
179 req->in.allocated = req->in.size;
182 req->in.body = hdr+SMB2_HDR_BODY;
183 req->in.body_size = req->in.size - (SMB2_HDR_BODY+NBT_HDR_SIZE);
184 req->in.ptr = req->in.body;
185 req->status = NT_STATUS(IVAL(hdr, SMB2_HDR_STATUS));
187 DEBUG(2, ("SMB2 RECV seqnum=0x%llx\n", req->seqnum));
188 dump_data(5, req->in.body, req->in.body_size);
190 /* if this request has an async handler then call that to
191 notify that the reply has been received. This might destroy
192 the request so it must happen last */
193 DLIST_REMOVE(transport->pending_recv, req);
194 req->state = SMB2_REQUEST_DONE;
201 dump_data(5, buffer, len);
203 DLIST_REMOVE(transport->pending_recv, req);
204 req->state = SMB2_REQUEST_ERROR;
211 return NT_STATUS_UNSUCCESSFUL;
215 handle timeouts of individual smb requests
217 static void smb2_timeout_handler(struct event_context *ev, struct timed_event *te,
218 struct timeval t, void *private)
220 struct smb2_request *req = talloc_get_type(private, struct smb2_request);
222 if (req->state == SMB2_REQUEST_RECV) {
223 DLIST_REMOVE(req->transport->pending_recv, req);
225 req->status = NT_STATUS_IO_TIMEOUT;
226 req->state = SMB2_REQUEST_ERROR;
236 static int smb2_request_destructor(void *ptr)
238 struct smb2_request *req = talloc_get_type(ptr, struct smb2_request);
239 if (req->state == SMB2_REQUEST_RECV) {
240 DLIST_REMOVE(req->transport->pending_recv, req);
247 put a request into the send queue
249 void smb2_transport_send(struct smb2_request *req)
254 _smb_setlen2(req->out.buffer, req->out.size - NBT_HDR_SIZE);
256 DEBUG(2, ("SMB2 send seqnum=0x%llx\n", req->seqnum));
257 dump_data(5, req->out.body, req->out.body_size);
259 /* check if the transport is dead */
260 if (req->transport->socket->sock == NULL) {
261 req->state = SMB2_REQUEST_ERROR;
262 req->status = NT_STATUS_NET_WRITE_FAULT;
266 blob = data_blob_const(req->out.buffer, req->out.size);
267 status = packet_send(req->transport->packet, blob);
268 if (!NT_STATUS_IS_OK(status)) {
269 req->state = SMB2_REQUEST_ERROR;
270 req->status = status;
274 req->state = SMB2_REQUEST_RECV;
275 DLIST_ADD(req->transport->pending_recv, req);
278 if (req->transport->options.timeout) {
279 event_add_timed(req->transport->socket->event.ctx, req,
280 timeval_current_ofs(req->transport->options.timeout, 0),
281 smb2_timeout_handler, req);
284 talloc_set_destructor(req, smb2_request_destructor);