2 Unix SMB/CIFS mplementation.
4 helper layer for breaking up streams into discrete requests
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.
26 #include "dlinklist.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "lib/tls/tls.h"
30 #include "lib/stream/packet.h"
33 struct packet_context {
34 packet_callback_fn_t callback;
35 packet_full_request_fn_t full_request;
36 packet_error_handler_fn_t error_handler;
39 uint32_t initial_read;
40 struct tls_context *tls;
41 struct socket_context *sock;
42 struct event_context *ev;
52 BOOL destructor_called;
55 struct send_element *next, *prev;
62 a destructor used when we are processing packets to prevent freeing of this
63 context while it is being used
65 static int packet_destructor(void *p)
67 struct packet_context *pc = talloc_get_type(p, struct packet_context);
70 pc->destructor_called = True;
71 /* now we refuse the talloc_free() request. The free will
72 happen again in the packet_recv() code */
81 initialise a packet receiver
83 struct packet_context *packet_init(TALLOC_CTX *mem_ctx)
85 struct packet_context *pc = talloc_zero(mem_ctx, struct packet_context);
87 talloc_set_destructor(pc, packet_destructor);
94 set the request callback, called when a full request is ready
96 void packet_set_callback(struct packet_context *pc, packet_callback_fn_t callback)
98 pc->callback = callback;
102 set the error handler
104 void packet_set_error_handler(struct packet_context *pc, packet_error_handler_fn_t handler)
106 pc->error_handler = handler;
110 set the private pointer passed to the callback functions
112 void packet_set_private(struct packet_context *pc, void *private)
114 pc->private = private;
118 set the full request callback. Should return as follows:
119 NT_STATUS_OK == blob is a full request.
120 STATUS_MORE_ENTRIES == blob is not complete yet
121 any error == blob is not a valid
123 void packet_set_full_request(struct packet_context *pc, packet_full_request_fn_t callback)
125 pc->full_request = callback;
129 set a tls context to use. You must either set a tls_context or a socket_context
131 void packet_set_tls(struct packet_context *pc, struct tls_context *tls)
137 set a socket context to use. You must either set a tls_context or a socket_context
139 void packet_set_socket(struct packet_context *pc, struct socket_context *sock)
145 set an event context. If this is set then the code will ensure that
146 packets arrive with separate events, by creating a immediate event
147 for any secondary packets when more than one packet is read at one
148 time on a socket. This can matter for code that relies on not
149 getting more than one packet per event
151 void packet_set_event_context(struct packet_context *pc, struct event_context *ev)
157 tell the packet layer the fde for the socket
159 void packet_set_fde(struct packet_context *pc, struct fd_event *fde)
165 tell the packet layer to serialise requests, so we don't process two
166 requests at once on one connection. You must have set the
167 event_context and fde
169 void packet_set_serialise(struct packet_context *pc)
171 pc->serialise = True;
175 tell the packet layer how much to read when starting a new packet
176 this ensures it doesn't overread
178 void packet_set_initial_read(struct packet_context *pc, uint32_t initial_read)
180 pc->initial_read = initial_read;
184 tell the packet system not to steal/free blobs given to packet_send()
186 void packet_set_nofree(struct packet_context *pc)
193 tell the caller we have an error
195 static void packet_error(struct packet_context *pc, NTSTATUS status)
199 if (pc->error_handler) {
200 pc->error_handler(pc->private, status);
203 /* default error handler is to free the callers private pointer */
204 if (!NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
205 DEBUG(0,("packet_error on %s - %s\n",
206 talloc_get_name(pc->private), nt_errstr(status)));
208 talloc_free(pc->private);
214 tell the caller we have EOF
216 static void packet_eof(struct packet_context *pc)
218 packet_error(pc, NT_STATUS_END_OF_FILE);
223 used to put packets on event boundaries
225 static void packet_next_event(struct event_context *ev, struct timed_event *te,
226 struct timeval t, void *private)
228 struct packet_context *pc = talloc_get_type(private, struct packet_context);
229 if (pc->num_read != 0 && pc->packet_size != 0 &&
230 pc->packet_size <= pc->num_read) {
237 call this when the socket becomes readable to kick off the whole
238 stream parsing process
240 void packet_recv(struct packet_context *pc)
247 if (pc->processing) {
248 EVENT_FD_NOT_READABLE(pc->fde);
253 if (pc->recv_disable) {
254 EVENT_FD_NOT_READABLE(pc->fde);
258 if (pc->packet_size != 0 && pc->num_read >= pc->packet_size) {
262 if (pc->packet_size != 0) {
263 /* we've already worked out how long this next packet is, so skip the
264 socket_pending() call */
265 npending = pc->packet_size - pc->num_read;
266 } else if (pc->initial_read != 0) {
267 npending = pc->initial_read - pc->num_read;
270 status = tls_socket_pending(pc->tls, &npending);
271 } else if (pc->sock) {
272 status = socket_pending(pc->sock, &npending);
274 status = NT_STATUS_CONNECTION_DISCONNECTED;
276 if (!NT_STATUS_IS_OK(status)) {
277 packet_error(pc, status);
287 /* possibly expand the partial packet buffer */
288 if (npending + pc->num_read > pc->partial.length) {
289 status = data_blob_realloc(pc, &pc->partial, npending+pc->num_read);
290 if (!NT_STATUS_IS_OK(status)) {
291 packet_error(pc, status);
297 status = tls_socket_recv(pc->tls, pc->partial.data + pc->num_read,
300 status = socket_recv(pc->sock, pc->partial.data + pc->num_read,
301 npending, &nread, 0);
303 if (NT_STATUS_IS_ERR(status)) {
304 packet_error(pc, status);
307 if (!NT_STATUS_IS_OK(status)) {
316 pc->num_read += nread;
319 if (pc->partial.length != pc->num_read) {
320 status = data_blob_realloc(pc, &pc->partial, pc->num_read);
321 if (!NT_STATUS_IS_OK(status)) {
322 packet_error(pc, status);
327 /* see if its a full request */
329 blob.length = pc->num_read;
330 status = pc->full_request(pc->private, blob, &pc->packet_size);
331 if (NT_STATUS_IS_ERR(status)) {
332 packet_error(pc, status);
335 if (!NT_STATUS_IS_OK(status)) {
339 if (pc->packet_size > pc->num_read) {
340 /* the caller made an error */
341 DEBUG(0,("Invalid packet_size %lu greater than num_read %lu\n",
342 (long)pc->packet_size, (long)pc->num_read));
343 packet_error(pc, NT_STATUS_INVALID_PARAMETER);
347 /* it is a full request - give it to the caller */
349 blob.length = pc->num_read;
351 if (pc->packet_size < pc->num_read) {
352 pc->partial = data_blob_talloc(pc, blob.data + pc->packet_size,
353 pc->num_read - pc->packet_size);
354 if (pc->partial.data == NULL) {
355 packet_error(pc, NT_STATUS_NO_MEMORY);
358 status = data_blob_realloc(pc, &blob, pc->packet_size);
359 if (!NT_STATUS_IS_OK(status)) {
360 packet_error(pc, status);
364 pc->partial = data_blob(NULL, 0);
366 pc->num_read -= pc->packet_size;
375 status = pc->callback(pc->private, blob);
379 if (pc->destructor_called) {
384 if (pc->processing) {
385 if (pc->processing > 1) {
386 EVENT_FD_READABLE(pc->fde);
391 if (!NT_STATUS_IS_OK(status)) {
392 packet_error(pc, status);
396 if (pc->partial.length == 0) {
400 /* we got multiple packets in one tcp read */
401 if (pc->ev == NULL) {
406 blob.length = pc->num_read;
408 status = pc->full_request(pc->private, blob, &pc->packet_size);
409 if (NT_STATUS_IS_ERR(status)) {
410 packet_error(pc, status);
414 if (!NT_STATUS_IS_OK(status)) {
418 event_add_timed(pc->ev, pc, timeval_zero(), packet_next_event, pc);
423 temporarily disable receiving
425 void packet_recv_disable(struct packet_context *pc)
427 EVENT_FD_NOT_READABLE(pc->fde);
428 pc->recv_disable = True;
434 void packet_recv_enable(struct packet_context *pc)
436 EVENT_FD_READABLE(pc->fde);
437 pc->recv_disable = False;
438 if (pc->num_read != 0 && pc->packet_size >= pc->num_read) {
439 event_add_timed(pc->ev, pc, timeval_zero(), packet_next_event, pc);
444 trigger a run of the send queue
446 void packet_queue_run(struct packet_context *pc)
448 while (pc->send_queue) {
449 struct send_element *el = pc->send_queue;
452 DATA_BLOB blob = data_blob_const(el->blob.data + el->nsent,
453 el->blob.length - el->nsent);
456 status = tls_socket_send(pc->tls, &blob, &nwritten);
458 status = socket_send(pc->sock, &blob, &nwritten, 0);
460 if (NT_STATUS_IS_ERR(status)) {
461 packet_error(pc, NT_STATUS_NET_WRITE_FAULT);
464 if (!NT_STATUS_IS_OK(status)) {
467 el->nsent += nwritten;
468 if (el->nsent == el->blob.length) {
469 DLIST_REMOVE(pc->send_queue, el);
474 /* we're out of requests to send, so don't wait for write
476 EVENT_FD_NOT_WRITEABLE(pc->fde);
480 put a packet in the send queue
482 NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
484 struct send_element *el;
485 el = talloc(pc, struct send_element);
486 NT_STATUS_HAVE_NO_MEMORY(el);
488 DLIST_ADD_END(pc->send_queue, el, struct send_element *);
492 /* if we aren't going to free the packet then we must reference it
493 to ensure it doesn't disappear before going out */
495 if (!talloc_reference(el, blob.data)) {
496 return NT_STATUS_NO_MEMORY;
499 talloc_steal(el, blob.data);
502 EVENT_FD_WRITEABLE(pc->fde);
509 a full request checker for NBT formatted packets (first 3 bytes are length)
511 NTSTATUS packet_full_request_nbt(void *private, DATA_BLOB blob, size_t *size)
513 if (blob.length < 4) {
514 return STATUS_MORE_ENTRIES;
516 *size = 4 + smb_len(blob.data);
517 if (*size > blob.length) {
518 return STATUS_MORE_ENTRIES;
525 work out if a packet is complete for protocols that use a 32 bit network byte
528 NTSTATUS packet_full_request_u32(void *private, DATA_BLOB blob, size_t *size)
530 if (blob.length < 4) {
531 return STATUS_MORE_ENTRIES;
533 *size = 4 + RIVAL(blob.data, 0);
534 if (*size > blob.length) {
535 return STATUS_MORE_ENTRIES;