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.
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
28 #include "lib/tls/tls.h"
29 #include "lib/stream/packet.h"
32 struct packet_context {
33 packet_callback_fn_t callback;
34 packet_full_request_fn_t full_request;
35 packet_error_handler_fn_t error_handler;
38 uint32_t initial_read;
39 struct tls_context *tls;
40 struct socket_context *sock;
41 struct event_context *ev;
51 struct send_element *next, *prev;
58 initialise a packet receiver
60 struct packet_context *packet_init(TALLOC_CTX *mem_ctx)
62 return talloc_zero(mem_ctx, struct packet_context);
67 set the request callback, called when a full request is ready
69 void packet_set_callback(struct packet_context *pc, packet_callback_fn_t callback)
71 pc->callback = callback;
77 void packet_set_error_handler(struct packet_context *pc, packet_error_handler_fn_t handler)
79 pc->error_handler = handler;
83 set the private pointer passed to the callback functions
85 void packet_set_private(struct packet_context *pc, void *private)
87 pc->private = private;
91 set the full request callback. Should return as follows:
92 NT_STATUS_OK == blob is a full request.
93 STATUS_MORE_ENTRIES == blob is not complete yet
94 any error == blob is not a valid
96 void packet_set_full_request(struct packet_context *pc, packet_full_request_fn_t callback)
98 pc->full_request = callback;
102 set a tls context to use. You must either set a tls_context or a socket_context
104 void packet_set_tls(struct packet_context *pc, struct tls_context *tls)
110 set a socket context to use. You must either set a tls_context or a socket_context
112 void packet_set_socket(struct packet_context *pc, struct socket_context *sock)
118 set an event context. If this is set then the code will ensure that
119 packets arrive with separate events, by creating a immediate event
120 for any secondary packets when more than one packet is read at one
121 time on a socket. This can matter for code that relies on not
122 getting more than one packet per event
124 void packet_set_event_context(struct packet_context *pc, struct event_context *ev)
130 tell the packet layer to serialise requests, so we don't process two requests at once on
131 one connection. You must have set the event_context
133 void packet_set_serialise(struct packet_context *pc, struct fd_event *fde)
135 pc->serialise = True;
140 tell the packet layer how much to read when starting a new packet
141 this ensures it doesn't overread
143 void packet_set_initial_read(struct packet_context *pc, uint32_t initial_read)
145 pc->initial_read = initial_read;
149 tell the packet system not to steal/free blobs given to packet_send()
151 void packet_set_nofree(struct packet_context *pc)
158 tell the caller we have an error
160 static void packet_error(struct packet_context *pc, NTSTATUS status)
164 if (pc->error_handler) {
165 pc->error_handler(pc->private, status);
168 /* default error handler is to free the callers private pointer */
169 if (!NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
170 DEBUG(0,("packet_error on %s - %s\n",
171 talloc_get_name(pc->private), nt_errstr(status)));
173 talloc_free(pc->private);
179 tell the caller we have EOF
181 static void packet_eof(struct packet_context *pc)
183 packet_error(pc, NT_STATUS_END_OF_FILE);
188 used to put packets on event boundaries
190 static void packet_next_event(struct event_context *ev, struct timed_event *te,
191 struct timeval t, void *private)
193 struct packet_context *pc = talloc_get_type(private, struct packet_context);
194 if (pc->num_read != 0 && pc->packet_size != 0 &&
195 pc->packet_size <= pc->num_read) {
201 call this when the socket becomes readable to kick off the whole
202 stream parsing process
204 void packet_recv(struct packet_context *pc)
211 if (pc->processing) {
215 if (pc->recv_disable) {
216 EVENT_FD_NOT_READABLE(pc->fde);
220 if (pc->packet_size != 0 && pc->num_read >= pc->packet_size) {
224 if (pc->packet_size != 0) {
225 /* we've already worked out how long this next packet is, so skip the
226 socket_pending() call */
227 npending = pc->packet_size - pc->num_read;
228 } else if (pc->initial_read != 0) {
229 npending = pc->initial_read - pc->num_read;
232 status = tls_socket_pending(pc->tls, &npending);
233 } else if (pc->sock) {
234 status = socket_pending(pc->sock, &npending);
236 status = NT_STATUS_CONNECTION_DISCONNECTED;
238 if (!NT_STATUS_IS_OK(status)) {
239 packet_error(pc, status);
249 /* possibly expand the partial packet buffer */
250 if (npending + pc->num_read > pc->partial.length) {
251 status = data_blob_realloc(pc, &pc->partial, npending+pc->num_read);
252 if (!NT_STATUS_IS_OK(status)) {
253 packet_error(pc, status);
259 status = tls_socket_recv(pc->tls, pc->partial.data + pc->num_read,
262 status = socket_recv(pc->sock, pc->partial.data + pc->num_read,
263 npending, &nread, 0);
265 if (NT_STATUS_IS_ERR(status)) {
266 packet_error(pc, status);
269 if (!NT_STATUS_IS_OK(status)) {
278 pc->num_read += nread;
281 if (pc->partial.length != pc->num_read) {
282 status = data_blob_realloc(pc, &pc->partial, pc->num_read);
283 if (!NT_STATUS_IS_OK(status)) {
284 packet_error(pc, status);
289 /* see if its a full request */
291 blob.length = pc->num_read;
292 status = pc->full_request(pc->private, blob, &pc->packet_size);
293 if (NT_STATUS_IS_ERR(status)) {
294 packet_error(pc, status);
297 if (!NT_STATUS_IS_OK(status)) {
301 if (pc->packet_size > pc->num_read) {
302 /* the caller made an error */
303 DEBUG(0,("Invalid packet_size %u greater than num_read %u\n",
304 pc->packet_size, pc->num_read));
305 packet_error(pc, NT_STATUS_INVALID_PARAMETER);
309 /* it is a full request - give it to the caller */
311 blob.length = pc->num_read;
313 if (pc->packet_size < pc->num_read) {
314 pc->partial = data_blob_talloc(pc, blob.data + pc->packet_size,
315 pc->num_read - pc->packet_size);
316 if (pc->partial.data == NULL) {
317 packet_error(pc, NT_STATUS_NO_MEMORY);
320 status = data_blob_realloc(pc, &blob, pc->packet_size);
321 if (!NT_STATUS_IS_OK(status)) {
322 packet_error(pc, status);
326 pc->partial = data_blob(NULL, 0);
328 pc->num_read -= pc->packet_size;
332 EVENT_FD_NOT_READABLE(pc->fde);
333 pc->processing = True;
336 status = pc->callback(pc->private, blob);
339 EVENT_FD_READABLE(pc->fde);
340 pc->processing = False;
343 if (!NT_STATUS_IS_OK(status)) {
344 packet_error(pc, status);
348 if (pc->partial.length == 0) {
352 /* we got multiple packets in one tcp read */
353 if (pc->ev == NULL) {
358 blob.length = pc->num_read;
360 status = pc->full_request(pc->private, blob, &pc->packet_size);
361 if (NT_STATUS_IS_ERR(status)) {
362 packet_error(pc, status);
366 if (!NT_STATUS_IS_OK(status)) {
370 event_add_timed(pc->ev, pc, timeval_zero(), packet_next_event, pc);
375 temporarily disable receiving
377 void packet_recv_disable(struct packet_context *pc)
379 EVENT_FD_NOT_READABLE(pc->fde);
380 pc->recv_disable = True;
386 void packet_recv_enable(struct packet_context *pc)
388 EVENT_FD_READABLE(pc->fde);
389 pc->recv_disable = False;
390 if (pc->num_read != 0 && pc->packet_size >= pc->num_read) {
391 event_add_timed(pc->ev, pc, timeval_zero(), packet_next_event, pc);
396 trigger a run of the send queue
398 void packet_queue_run(struct packet_context *pc)
400 while (pc->send_queue) {
401 struct send_element *el = pc->send_queue;
404 DATA_BLOB blob = data_blob_const(el->blob.data + el->nsent,
405 el->blob.length - el->nsent);
408 status = tls_socket_send(pc->tls, &blob, &nwritten);
410 status = socket_send(pc->sock, &blob, &nwritten, 0);
412 if (NT_STATUS_IS_ERR(status)) {
413 packet_error(pc, NT_STATUS_NET_WRITE_FAULT);
416 if (!NT_STATUS_IS_OK(status)) {
419 el->nsent += nwritten;
420 if (el->nsent == el->blob.length) {
421 DLIST_REMOVE(pc->send_queue, el);
426 /* we're out of requests to send, so don't wait for write
428 EVENT_FD_NOT_WRITEABLE(pc->fde);
432 put a packet in the send queue
434 NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
436 struct send_element *el;
437 el = talloc(pc, struct send_element);
438 NT_STATUS_HAVE_NO_MEMORY(el);
440 DLIST_ADD_END(pc->send_queue, el, struct send_element *);
444 /* if we aren't going to free the packet then we must reference it
445 to ensure it doesn't disappear before going out */
447 if (!talloc_reference(el, blob.data)) {
448 return NT_STATUS_NO_MEMORY;
451 talloc_steal(el, blob.data);
454 EVENT_FD_WRITEABLE(pc->fde);
461 a full request checker for NBT formatted packets (first 3 bytes are length)
463 NTSTATUS packet_full_request_nbt(void *private, DATA_BLOB blob, size_t *size)
465 if (blob.length < 4) {
466 return STATUS_MORE_ENTRIES;
468 *size = 4 + smb_len(blob.data);
469 if (*size > blob.length) {
470 return STATUS_MORE_ENTRIES;
477 work out if a packet is complete for protocols that use a 32 bit network byte
480 NTSTATUS packet_full_request_u32(void *private, DATA_BLOB blob, size_t *size)
482 if (blob.length < 4) {
483 return STATUS_MORE_ENTRIES;
485 *size = 4 + RIVAL(blob.data, 0);
486 if (*size > blob.length) {
487 return STATUS_MORE_ENTRIES;