Fix include paths to new location of libutil.
[kai/samba-autobuild/.git] / source4 / lib / stream / packet.c
index 987193c7d7512d73da9932d5b801092af8e5f21b..72a7e6485c708ed1bafd559638762fa7e8d8bc79 100644 (file)
@@ -7,7 +7,7 @@
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
 #include "includes.h"
-#include "smb.h"
-#include "dlinklist.h"
+#include "../lib/util/dlinklist.h"
 #include "lib/events/events.h"
 #include "lib/socket/socket.h"
-#include "lib/tls/tls.h"
 #include "lib/stream/packet.h"
-
+#include "libcli/raw/smb.h"
 
 struct packet_context {
        packet_callback_fn_t callback;
@@ -37,24 +34,25 @@ struct packet_context {
        DATA_BLOB partial;
        uint32_t num_read;
        uint32_t initial_read;
-       struct tls_context *tls;
        struct socket_context *sock;
        struct event_context *ev;
        size_t packet_size;
        void *private;
        struct fd_event *fde;
-       BOOL serialise;
+       bool serialise;
        int processing;
-       BOOL recv_disable;
-       BOOL nofree;
+       bool recv_disable;
+       bool nofree;
 
-       BOOL busy;
-       BOOL destructor_called;
+       bool busy;
+       bool destructor_called;
 
        struct send_element {
                struct send_element *next, *prev;
                DATA_BLOB blob;
                size_t nsent;
+               packet_send_callback_fn_t send_callback;
+               void *send_callback_private;
        } *send_queue;
 };
 
@@ -62,12 +60,10 @@ struct packet_context {
   a destructor used when we are processing packets to prevent freeing of this
   context while it is being used
 */
-static int packet_destructor(void *p)
+static int packet_destructor(struct packet_context *pc)
 {
-       struct packet_context *pc = talloc_get_type(p, struct packet_context);
-
        if (pc->busy) {
-               pc->destructor_called = True;
+               pc->destructor_called = true;
                /* now we refuse the talloc_free() request. The free will
                   happen again in the packet_recv() code */
                return -1;
@@ -126,15 +122,7 @@ _PUBLIC_ void packet_set_full_request(struct packet_context *pc, packet_full_req
 }
 
 /*
-  set a tls context to use. You must either set a tls_context or a socket_context
-*/
-_PUBLIC_ void packet_set_tls(struct packet_context *pc, struct tls_context *tls)
-{
-       pc->tls = tls;
-}
-
-/*
-  set a socket context to use. You must either set a tls_context or a socket_context
+  set a socket context to use. You must set a socket_context
 */
 _PUBLIC_ void packet_set_socket(struct packet_context *pc, struct socket_context *sock)
 {
@@ -168,7 +156,7 @@ _PUBLIC_ void packet_set_fde(struct packet_context *pc, struct fd_event *fde)
 */
 _PUBLIC_ void packet_set_serialise(struct packet_context *pc)
 {
-       pc->serialise = True;
+       pc->serialise = true;
 }
 
 /*
@@ -185,7 +173,7 @@ _PUBLIC_ void packet_set_initial_read(struct packet_context *pc, uint32_t initia
 */
 _PUBLIC_ void packet_set_nofree(struct packet_context *pc)
 {
-       pc->nofree = True;
+       pc->nofree = true;
 }
 
 
@@ -194,7 +182,6 @@ _PUBLIC_ void packet_set_nofree(struct packet_context *pc)
 */
 static void packet_error(struct packet_context *pc, NTSTATUS status)
 {
-       pc->tls = NULL;
        pc->sock = NULL;
        if (pc->error_handler) {
                pc->error_handler(pc->private, status);
@@ -266,9 +253,7 @@ _PUBLIC_ void packet_recv(struct packet_context *pc)
        } else if (pc->initial_read != 0) {
                npending = pc->initial_read - pc->num_read;
        } else {
-               if (pc->tls) {
-                       status = tls_socket_pending(pc->tls, &npending);
-               } else if (pc->sock) {
+               if (pc->sock) {
                        status = socket_pending(pc->sock, &npending);
                } else {
                        status = NT_STATUS_CONNECTION_DISCONNECTED;
@@ -284,22 +269,41 @@ _PUBLIC_ void packet_recv(struct packet_context *pc)
                return;
        }
 
+       if (npending + pc->num_read < npending) {
+               packet_error(pc, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
+
+       if (npending + pc->num_read < pc->num_read) {
+               packet_error(pc, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
+
        /* possibly expand the partial packet buffer */
        if (npending + pc->num_read > pc->partial.length) {
-               status = data_blob_realloc(pc, &pc->partial, npending+pc->num_read);
-               if (!NT_STATUS_IS_OK(status)) {
-                       packet_error(pc, status);
+               if (!data_blob_realloc(pc, &pc->partial, npending+pc->num_read)) {
+                       packet_error(pc, NT_STATUS_NO_MEMORY);
                        return;
                }
        }
 
-       if (pc->tls) {
-               status = tls_socket_recv(pc->tls, pc->partial.data + pc->num_read, 
-                                        npending, &nread);
-       } else {
-               status = socket_recv(pc->sock, pc->partial.data + pc->num_read, 
-                                    npending, &nread, 0);
+       if (pc->partial.length < pc->num_read + npending) {
+               packet_error(pc, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
+
+       if ((uint8_t *)pc->partial.data + pc->num_read < (uint8_t *)pc->partial.data) {
+               packet_error(pc, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
+       if ((uint8_t *)pc->partial.data + pc->num_read + npending < (uint8_t *)pc->partial.data) {
+               packet_error(pc, NT_STATUS_INVALID_PARAMETER);
+               return;
        }
+
+       status = socket_recv(pc->sock, pc->partial.data + pc->num_read, 
+                            npending, &nread);
+
        if (NT_STATUS_IS_ERR(status)) {
                packet_error(pc, status);
                return;
@@ -317,9 +321,8 @@ _PUBLIC_ void packet_recv(struct packet_context *pc)
 
 next_partial:
        if (pc->partial.length != pc->num_read) {
-               status = data_blob_realloc(pc, &pc->partial, pc->num_read);
-               if (!NT_STATUS_IS_OK(status)) {
-                       packet_error(pc, status);
+               if (!data_blob_realloc(pc, &pc->partial, pc->num_read)) {
+                       packet_error(pc, NT_STATUS_NO_MEMORY);
                        return;
                }
        }
@@ -355,9 +358,9 @@ next_partial:
                        packet_error(pc, NT_STATUS_NO_MEMORY);
                        return;
                }
-               status = data_blob_realloc(pc, &blob, pc->packet_size);
-               if (!NT_STATUS_IS_OK(status)) {
-                       packet_error(pc, status);
+               /* Trunate the blob sent to the caller to only the packet length */
+               if (!data_blob_realloc(pc, &blob, pc->packet_size)) {
+                       packet_error(pc, NT_STATUS_NO_MEMORY);
                        return;
                }
        } else {
@@ -370,11 +373,11 @@ next_partial:
                pc->processing = 1;
        }
 
-       pc->busy = True;
+       pc->busy = true;
 
        status = pc->callback(pc->private, blob);
 
-       pc->busy = False;
+       pc->busy = false;
 
        if (pc->destructor_called) {
                talloc_free(pc);
@@ -393,6 +396,7 @@ next_partial:
                return;
        }
 
+       /* Have we consumed the whole buffer yet? */
        if (pc->partial.length == 0) {
                return;
        }
@@ -425,7 +429,7 @@ next_partial:
 _PUBLIC_ void packet_recv_disable(struct packet_context *pc)
 {
        EVENT_FD_NOT_READABLE(pc->fde);
-       pc->recv_disable = True;
+       pc->recv_disable = true;
 }
 
 /*
@@ -434,7 +438,7 @@ _PUBLIC_ void packet_recv_disable(struct packet_context *pc)
 _PUBLIC_ void packet_recv_enable(struct packet_context *pc)
 {
        EVENT_FD_READABLE(pc->fde);
-       pc->recv_disable = False;
+       pc->recv_disable = false;
        if (pc->num_read != 0 && pc->packet_size >= pc->num_read) {
                event_add_timed(pc->ev, pc, timeval_zero(), packet_next_event, pc);
        }
@@ -452,13 +456,10 @@ _PUBLIC_ void packet_queue_run(struct packet_context *pc)
                DATA_BLOB blob = data_blob_const(el->blob.data + el->nsent,
                                                 el->blob.length - el->nsent);
 
-               if (pc->tls) {
-                       status = tls_socket_send(pc->tls, &blob, &nwritten);
-               } else {
-                       status = socket_send(pc->sock, &blob, &nwritten, 0);
-               }
+               status = socket_send(pc->sock, &blob, &nwritten);
+
                if (NT_STATUS_IS_ERR(status)) {
-                       packet_error(pc, NT_STATUS_NET_WRITE_FAULT);
+                       packet_error(pc, status);
                        return;
                }
                if (!NT_STATUS_IS_OK(status)) {
@@ -467,6 +468,15 @@ _PUBLIC_ void packet_queue_run(struct packet_context *pc)
                el->nsent += nwritten;
                if (el->nsent == el->blob.length) {
                        DLIST_REMOVE(pc->send_queue, el);
+                       if (el->send_callback) {
+                               pc->busy = true;
+                               el->send_callback(el->send_callback_private);
+                               pc->busy = false;
+                               if (pc->destructor_called) {
+                                       talloc_free(pc);
+                                       return;
+                               }
+                       }
                        talloc_free(el);
                }
        }
@@ -477,9 +487,15 @@ _PUBLIC_ void packet_queue_run(struct packet_context *pc)
 }
 
 /*
-  put a packet in the send queue
+  put a packet in the send queue.  When the packet is actually sent,
+  call send_callback.  
+
+  Useful for operations that must occour after sending a message, such
+  as the switch to SASL encryption after as sucessful LDAP bind relpy.
 */
-_PUBLIC_ NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
+_PUBLIC_ NTSTATUS packet_send_callback(struct packet_context *pc, DATA_BLOB blob,
+                                      packet_send_callback_fn_t send_callback, 
+                                      void *private)
 {
        struct send_element *el;
        el = talloc(pc, struct send_element);
@@ -488,6 +504,8 @@ _PUBLIC_ NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
        DLIST_ADD_END(pc->send_queue, el, struct send_element *);
        el->blob = blob;
        el->nsent = 0;
+       el->send_callback = send_callback;
+       el->send_callback_private = private;
 
        /* if we aren't going to free the packet then we must reference it
           to ensure it doesn't disappear before going out */
@@ -499,11 +517,23 @@ _PUBLIC_ NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
                talloc_steal(el, blob.data);
        }
 
+       if (private && !talloc_reference(el, private)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        EVENT_FD_WRITEABLE(pc->fde);
 
        return NT_STATUS_OK;
 }
 
+/*
+  put a packet in the send queue
+*/
+_PUBLIC_ NTSTATUS packet_send(struct packet_context *pc, DATA_BLOB blob)
+{
+       return packet_send_callback(pc, blob, NULL, NULL);
+}
+
 
 /*
   a full request checker for NBT formatted packets (first 3 bytes are length)