Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[jelmer/samba4-debian.git] / source / libcli / raw / clitransport.c
index 3b3c10ed01361b20f77db6a7f26088ab683f17c2..288f0612de61f66103ed6445f26c6b8d8c061e9b 100644 (file)
@@ -1,12 +1,13 @@
 /* 
    Unix SMB/CIFS implementation.
    SMB client transport context management functions
-   Copyright (C) Andrew Tridgell 1994-2003
+
+   Copyright (C) Andrew Tridgell 1994-2005
    Copyright (C) James Myers 2003 <myersjj@samba.org>
    
    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 "libcli/raw/libcliraw.h"
+#include "lib/socket/socket.h"
+#include "lib/util/dlinklist.h"
+#include "lib/events/events.h"
+#include "lib/stream/packet.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
+#include "param/param.h"
+
 
 /*
   an event has happened on the socket
 */
-static void cli_transport_event_handler(struct event_context *ev, struct fd_event *fde, 
-                                       time_t t, uint16_t flags)
+static void smbcli_transport_event_handler(struct event_context *ev, 
+                                          struct fd_event *fde, 
+                                          uint16_t flags, void *private)
 {
-       struct cli_transport *transport = fde->private;
+       struct smbcli_transport *transport = talloc_get_type(private,
+                                                            struct smbcli_transport);
+       if (flags & EVENT_FD_READ) {
+               packet_recv(transport->packet);
+               return;
+       }
+       if (flags & EVENT_FD_WRITE) {
+               packet_queue_run(transport->packet);
+       }
+}
 
-       cli_transport_process(transport);
+/*
+  destroy a transport
+ */
+static int transport_destructor(struct smbcli_transport *transport)
+{
+       smbcli_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
+       return 0;
 }
 
+
 /*
-  create a transport structure based on an established socket
+  handle receive errors
 */
-struct cli_transport *cli_transport_init(struct cli_socket *sock)
+static void smbcli_transport_error(void *private, NTSTATUS status)
 {
-       TALLOC_CTX *mem_ctx;
-       struct cli_transport *transport;
-       struct fd_event fde;
+       struct smbcli_transport *transport = talloc_get_type(private, struct smbcli_transport);
+       smbcli_transport_dead(transport, status);
+}
 
-       mem_ctx = talloc_init("cli_transport");
-       if (!mem_ctx) return NULL;
+static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob);
 
-       transport = talloc_zero(mem_ctx, sizeof(*transport));
+/*
+  create a transport structure based on an established socket
+*/
+struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
+                                              TALLOC_CTX *parent_ctx, 
+                                              bool primary, 
+                                              struct smbcli_options *options)
+{
+       struct smbcli_transport *transport;
+
+       transport = talloc_zero(parent_ctx, struct smbcli_transport);
        if (!transport) return NULL;
 
-       transport->event.ctx = event_context_init();
-       if (transport->event.ctx == NULL) {
-               talloc_destroy(mem_ctx);
-               return NULL;
+       if (primary) {
+               transport->socket = talloc_steal(transport, sock);
+       } else {
+               transport->socket = talloc_reference(transport, sock);
        }
-
-       transport->mem_ctx = mem_ctx;
-       transport->socket = sock;
        transport->negotiate.protocol = PROTOCOL_NT1;
-       transport->options.use_spnego = lp_use_spnego();
-       transport->negotiate.max_xmit = ~0;
-       
-       cli_init_signing(transport);
+       transport->options = *options;
+       transport->negotiate.max_xmit = transport->options.max_xmit;
+
+       /* setup the stream -> packet parser */
+       transport->packet = packet_init(transport);
+       if (transport->packet == NULL) {
+               talloc_free(transport);
+               return NULL;
+       }
+       packet_set_private(transport->packet, transport);
+       packet_set_socket(transport->packet, transport->socket->sock);
+       packet_set_callback(transport->packet, smbcli_transport_finish_recv);
+       packet_set_full_request(transport->packet, packet_full_request_nbt);
+       packet_set_error_handler(transport->packet, smbcli_transport_error);
+       packet_set_event_context(transport->packet, transport->socket->event.ctx);
+       packet_set_nofree(transport->packet);
 
-       transport->socket->reference_count++;
+       smbcli_init_signing(transport);
 
        ZERO_STRUCT(transport->called);
 
-       fde.fd = sock->fd;
-       fde.flags = EVENT_FD_READ;
-       fde.handler = cli_transport_event_handler;
-       fde.private = transport;
-       fde.ref_count = 1;
+       /* take over event handling from the socket layer - it only
+          handles events up until we are connected */
+       talloc_free(transport->socket->event.fde);
+       transport->socket->event.fde = event_add_fd(transport->socket->event.ctx,
+                                                   transport->socket->sock,
+                                                   socket_get_fd(transport->socket->sock),
+                                                   EVENT_FD_READ,
+                                                   smbcli_transport_event_handler,
+                                                   transport);
 
-       transport->event.fde = event_add_fd(transport->event.ctx, &fde);
+       packet_set_fde(transport->packet, transport->socket->event.fde);
+       packet_set_serialise(transport->packet);
+       talloc_set_destructor(transport, transport_destructor);
 
        return transport;
 }
 
-/*
-  decrease reference count on a transport, and destroy if it becomes
-  zero
-*/
-void cli_transport_close(struct cli_transport *transport)
-{
-       transport->reference_count--;
-       if (transport->reference_count <= 0) {
-               cli_sock_close(transport->socket);
-               event_remove_fd(transport->event.ctx, transport->event.fde);
-               event_remove_timed(transport->event.ctx, transport->event.te);
-               event_context_destroy(transport->event.ctx);
-               talloc_destroy(transport->mem_ctx);
-       }
-}
-
 /*
   mark the transport as dead
 */
-void cli_transport_dead(struct cli_transport *transport)
+void smbcli_transport_dead(struct smbcli_transport *transport, NTSTATUS status)
 {
-       cli_sock_dead(transport->socket);
+       smbcli_sock_dead(transport->socket);
 
-       /* all pending sends become errors */
-       while (transport->pending_send) {
-               struct cli_request *req = transport->pending_send;
-               req->state = CLI_REQUEST_ERROR;
-               req->status = NT_STATUS_NET_WRITE_FAULT;
-               DLIST_REMOVE(transport->pending_send, req);
-               if (req->async.fn) {
-                       req->async.fn(req);
-               }
+       if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
+               status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
        }
 
-       /* as do all pending receives */
-       while (transport->pending_recv) {
-               struct cli_request *req = transport->pending_recv;
-               req->state = CLI_REQUEST_ERROR;
-               req->status = NT_STATUS_NET_WRITE_FAULT;
+       /* kill only the first pending receive - this is so that if
+        that async function frees the connection we don't die trying
+        to use old memory. The caller has to cope with only one
+        network error */
+       if (transport->pending_recv) {
+               struct smbcli_request *req = transport->pending_recv;
+               req->state = SMBCLI_REQUEST_ERROR;
+               req->status = status;
                DLIST_REMOVE(transport->pending_recv, req);
                if (req->async.fn) {
                        req->async.fn(req);
@@ -124,82 +153,135 @@ void cli_transport_dead(struct cli_transport *transport)
 
 
 /*
-  enable select for write on a transport
+  send a session request
 */
-static void cli_transport_write_enable(struct cli_transport *transport)
+struct smbcli_request *smbcli_transport_connect_send(struct smbcli_transport *transport,
+                                                    struct nbt_name *calling, 
+                                                    struct nbt_name *called)
 {
-       transport->event.fde->flags |= EVENT_FD_WRITE;
+       uint8_t *p;
+       struct smbcli_request *req;
+       DATA_BLOB calling_blob, called_blob;
+       TALLOC_CTX *tmp_ctx = talloc_new(transport);
+       NTSTATUS status;
+
+       status = nbt_name_dup(transport, called, &transport->called);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+       
+       status = nbt_name_to_blob(tmp_ctx, &calling_blob, calling);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+
+       status = nbt_name_to_blob(tmp_ctx, &called_blob, called);
+       if (!NT_STATUS_IS_OK(status)) goto failed;
+
+       /* allocate output buffer */
+       req = smbcli_request_setup_nonsmb(transport, 
+                                         NBT_HDR_SIZE + 
+                                         calling_blob.length + called_blob.length);
+       if (req == NULL) goto failed;
+
+       /* put in the destination name */
+       p = req->out.buffer + NBT_HDR_SIZE;
+       memcpy(p, called_blob.data, called_blob.length);
+       p += called_blob.length;
+
+       memcpy(p, calling_blob.data, calling_blob.length);
+       p += calling_blob.length;
+
+       _smb_setlen(req->out.buffer, PTR_DIFF(p, req->out.buffer) - NBT_HDR_SIZE);
+       SCVAL(req->out.buffer,0,0x81);
+
+       if (!smbcli_request_send(req)) {
+               smbcli_request_destroy(req);
+               goto failed;
+       }
+
+       talloc_free(tmp_ctx);
+       return req;
+
+failed:
+       talloc_free(tmp_ctx);
+       return NULL;
 }
 
 /*
-  disable select for write on a transport
-*/
-static void cli_transport_write_disable(struct cli_transport *transport)
+  map a session request error to a NTSTATUS
+ */
+static NTSTATUS map_session_refused_error(uint8_t error)
 {
-       transport->event.fde->flags &= ~EVENT_FD_WRITE;
+       switch (error) {
+       case 0x80:
+       case 0x81:
+               return NT_STATUS_REMOTE_NOT_LISTENING;
+       case 0x82:
+               return NT_STATUS_RESOURCE_NAME_NOT_FOUND;
+       case 0x83:
+               return NT_STATUS_REMOTE_RESOURCES;
+       }
+       return NT_STATUS_UNEXPECTED_IO_ERROR;
 }
 
-/****************************************************************************
-send a session request (if appropriate)
-****************************************************************************/
-BOOL cli_transport_connect(struct cli_transport *transport,
-                          struct nmb_name *calling, 
-                          struct nmb_name *called)
+
+/*
+  finish a smbcli_transport_connect()
+*/
+NTSTATUS smbcli_transport_connect_recv(struct smbcli_request *req)
 {
-       char *p;
-       int len = NBT_HDR_SIZE;
-       struct cli_request *req;
+       NTSTATUS status;
 
-       if (called) {
-               transport->called = *called;
+       if (!smbcli_request_receive(req)) {
+               smbcli_request_destroy(req);
+               return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
        }
 
-       /* 445 doesn't have session request */
-       if (transport->socket->port == 445) {
-               return True;
+       switch (CVAL(req->in.buffer,0)) {
+       case 0x82:
+               status = NT_STATUS_OK;
+               break;
+       case 0x83:
+               status = map_session_refused_error(CVAL(req->in.buffer,4));
+               break;
+       case 0x84:
+               DEBUG(1,("Warning: session retarget not supported\n"));
+               status = NT_STATUS_NOT_SUPPORTED;
+               break;
+       default:
+               status = NT_STATUS_UNEXPECTED_IO_ERROR;
+               break;
        }
 
-       /* allocate output buffer */
-       req = cli_request_setup_nonsmb(transport, NBT_HDR_SIZE + 2*nbt_mangled_name_len());
-
-       /* put in the destination name */
-       p = req->out.buffer + NBT_HDR_SIZE;
-       name_mangle(called->name, p, called->name_type);
-       len += name_len(p);
+       smbcli_request_destroy(req);
+       return status;
+}
 
-       /* and my name */
-       p = req->out.buffer+len;
-       name_mangle(calling->name, p, calling->name_type);
-       len += name_len(p);
 
-       _smb_setlen(req->out.buffer,len-4);
-       SCVAL(req->out.buffer,0,0x81);
+/*
+  send a session request (if needed)
+*/
+bool smbcli_transport_connect(struct smbcli_transport *transport,
+                             struct nbt_name *calling, 
+                             struct nbt_name *called)
+{
+       struct smbcli_request *req;
+       NTSTATUS status;
 
-       if (!cli_request_send(req) ||
-           !cli_request_receive(req)) {
-               cli_request_destroy(req);
-               return False;
-       }
-       
-       if (CVAL(req->in.buffer,0) != 0x82) {
-               transport->error.etype = ETYPE_NBT;
-               transport->error.e.nbt_error = CVAL(req->in.buffer,4);
-               cli_request_destroy(req);
-               return False;
+       if (transport->socket->port == 445) {
+               return true;
        }
 
-       cli_request_destroy(req);
-       return True;
+       req = smbcli_transport_connect_send(transport, 
+                                           calling, called);
+       status = smbcli_transport_connect_recv(req);
+       return NT_STATUS_IS_OK(status);
 }
 
-
 /****************************************************************************
 get next mid in sequence
 ****************************************************************************/
-uint16_t cli_transport_next_mid(struct cli_transport *transport)
+uint16_t smbcli_transport_next_mid(struct smbcli_transport *transport)
 {
        uint16_t mid;
-       struct cli_request *req;
+       struct smbcli_request *req;
 
        mid = transport->next_mid;
 
@@ -223,94 +305,64 @@ again:
 }
 
 static void idle_handler(struct event_context *ev, 
-                        struct timed_event *te, time_t t)
+                        struct timed_event *te, struct timeval t, void *private)
 {
-       struct cli_transport *transport = te->private;
-       te->next_event = t + transport->idle.period;
+       struct smbcli_transport *transport = talloc_get_type(private,
+                                                            struct smbcli_transport);
+       struct timeval next = timeval_add(&t, 0, transport->idle.period);
+       transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
+                                                     transport,
+                                                     next,
+                                                     idle_handler, transport);
        transport->idle.func(transport, transport->idle.private);
 }
 
 /*
   setup the idle handler for a transport
-  the period is in seconds
+  the period is in microseconds
 */
-void cli_transport_idle_handler(struct cli_transport *transport, 
-                               void (*idle_func)(struct cli_transport *, void *),
-                               uint_t period,
-                               void *private)
+void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
+                                  void (*idle_func)(struct smbcli_transport *, void *),
+                                  uint64_t period,
+                                  void *private)
 {
-       struct timed_event te;
        transport->idle.func = idle_func;
        transport->idle.private = private;
        transport->idle.period = period;
 
-       if (transport->event.te != NULL) {
-               event_remove_timed(transport->event.ctx, transport->event.te);
-       }
-
-       te.next_event = time(NULL) + period;
-       te.handler = idle_handler;
-       te.private = transport;
-       transport->event.te = event_add_timed(transport->event.ctx, &te);
-}
-
-/*
-  process some pending sends
-*/
-static void cli_transport_process_send(struct cli_transport *transport)
-{
-       while (transport->pending_send) {
-               struct cli_request *req = transport->pending_send;
-               ssize_t ret;
-               ret = cli_sock_write(transport->socket, req->out.buffer, req->out.size);
-               if (ret == -1) {
-                       if (errno == EAGAIN || errno == EINTR) {
-                               return;
-                       }
-                       cli_transport_dead(transport);
-               }
-               req->out.buffer += ret;
-               req->out.size -= ret;
-               if (req->out.size == 0) {
-                       DLIST_REMOVE(transport->pending_send, req);
-                       if (req->one_way_request) {
-                               req->state = CLI_REQUEST_DONE;
-                               cli_request_destroy(req);
-                       } else {
-                               req->state = CLI_REQUEST_RECV;
-                               DLIST_ADD(transport->pending_recv, req);
-                       }
-               }
+       if (transport->socket->event.te != NULL) {
+               talloc_free(transport->socket->event.te);
        }
 
-       /* we're out of requests to send, so don't wait for write
-          events any more */
-       cli_transport_write_disable(transport);
+       transport->socket->event.te = event_add_timed(transport->socket->event.ctx, 
+                                                     transport,
+                                                     timeval_current_ofs(0, period),
+                                                     idle_handler, transport);
 }
 
 /*
   we have a full request in our receive buffer - match it to a pending request
   and process
  */
-static void cli_transport_finish_recv(struct cli_transport *transport)
+static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
 {
+       struct smbcli_transport *transport = talloc_get_type(private, 
+                                                            struct smbcli_transport);
        uint8_t *buffer, *hdr, *vwv;
        int len;
-       uint16_t wct, mid = 0;
-       struct cli_request *req;
+       uint16_t wct=0, mid = 0, op = 0;
+       struct smbcli_request *req = NULL;
 
-       buffer = transport->recv_buffer.buffer;
-       len = transport->recv_buffer.req_size;
-
-       ZERO_STRUCT(transport->recv_buffer);
+       buffer = blob.data;
+       len = blob.length;
 
        hdr = buffer+NBT_HDR_SIZE;
        vwv = hdr + HDR_VWV;
 
        /* see if it could be an oplock break request */
-       if (handle_oplock_break(transport, len, hdr, vwv)) {
-               talloc_free(transport->mem_ctx, buffer);
-               return;
+       if (smbcli_handle_oplock_break(transport, len, hdr, vwv)) {
+               talloc_free(buffer);
+               return NT_STATUS_OK;
        }
 
        /* at this point we need to check for a readbraw reply, as
@@ -325,7 +377,7 @@ static void cli_transport_finish_recv(struct cli_transport *transport)
                if (!req) goto error;
 
                req->in.buffer = buffer;
-               talloc_steal(transport->mem_ctx, req->mem_ctx, buffer);
+               talloc_steal(req, buffer);
                req->in.size = len;
                req->in.allocated = req->in.size;
                goto async;
@@ -335,6 +387,7 @@ static void cli_transport_finish_recv(struct cli_transport *transport)
                /* extract the mid for matching to pending requests */
                mid = SVAL(hdr, HDR_MID);
                wct = CVAL(hdr, HDR_WCT);
+               op  = CVAL(hdr, HDR_COM);
        }
 
        /* match the incoming request against the list of pending requests */
@@ -342,27 +395,36 @@ static void cli_transport_finish_recv(struct cli_transport *transport)
                if (req->mid == mid) break;
        }
 
+       /* see if it's a ntcancel reply for the current MID */
+       req = smbcli_handle_ntcancel_reply(req, len, hdr);
+
        if (!req) {
-               DEBUG(1,("Discarding unmatched reply with mid %d\n", mid));
+               DEBUG(1,("Discarding unmatched reply with mid %d op %d\n", mid, op));
                goto error;
        }
 
        /* fill in the 'in' portion of the matching request */
        req->in.buffer = buffer;
-       talloc_steal(transport->mem_ctx, req->mem_ctx, buffer);
+       talloc_steal(req, buffer);
        req->in.size = len;
        req->in.allocated = req->in.size;
 
+       /* handle NBT session replies */
+       if (req->in.size >= 4 && req->in.buffer[0] != 0) {
+               req->status = NT_STATUS_OK;
+               goto async;
+       }
+
        /* handle non-SMB replies */
        if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE) {
-               req->state = CLI_REQUEST_ERROR;
+               req->state = SMBCLI_REQUEST_ERROR;
                goto error;
        }
 
        if (req->in.size < NBT_HDR_SIZE + MIN_SMB_SIZE + VWV(wct)) {
                DEBUG(2,("bad reply size for mid %d\n", mid));
                req->status = NT_STATUS_UNSUCCESSFUL;
-               req->state = CLI_REQUEST_ERROR;
+               req->state = SMBCLI_REQUEST_ERROR;
                goto error;
        }
 
@@ -382,22 +444,32 @@ static void cli_transport_finish_recv(struct cli_transport *transport)
        req->in.ptr = req->in.data;
        req->flags2 = SVAL(req->in.hdr, HDR_FLG2);
 
+       smb_setup_bufinfo(req);
+
        if (!(req->flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
-               transport->error.etype = ETYPE_DOS;
-               transport->error.e.dos.eclass = CVAL(req->in.hdr,HDR_RCLS);
-               transport->error.e.dos.ecode = SVAL(req->in.hdr,HDR_ERR);
-               req->status = dos_to_ntstatus(transport->error.e.dos.eclass, 
-                                             transport->error.e.dos.ecode);
+               int class = CVAL(req->in.hdr,HDR_RCLS);
+               int code = SVAL(req->in.hdr,HDR_ERR);
+               if (class == 0 && code == 0) {
+                       transport->error.e.nt_status = NT_STATUS_OK;
+               } else {
+                       transport->error.e.nt_status = NT_STATUS_DOS(class, code);
+               }
        } else {
-               transport->error.etype = ETYPE_NT;
                transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
-               req->status = transport->error.e.nt_status;
        }
 
-       if (!cli_request_check_sign_mac(req)) {
+       req->status = transport->error.e.nt_status;
+       if (NT_STATUS_IS_OK(req->status)) {
+               transport->error.etype = ETYPE_NONE;
+       } else {
+               transport->error.etype = ETYPE_SMB;
+       }
+
+       if (!smbcli_request_check_sign_mac(req)) {
                transport->error.etype = ETYPE_SOCKET;
                transport->error.e.socket_error = SOCKET_READ_BAD_SIG;
-               req->state = CLI_REQUEST_ERROR;
+               req->state = SMBCLI_REQUEST_ERROR;
+               req->status = NT_STATUS_ACCESS_DENIED;
                goto error;
        };
 
@@ -406,111 +478,186 @@ async:
           notify that the reply has been received. This might destroy
           the request so it must happen last */
        DLIST_REMOVE(transport->pending_recv, req);
-       req->state = CLI_REQUEST_DONE;
+       req->state = SMBCLI_REQUEST_DONE;
        if (req->async.fn) {
                req->async.fn(req);
        }
-       return;
+       return NT_STATUS_OK;
 
 error:
        if (req) {
                DLIST_REMOVE(transport->pending_recv, req);
-               req->state = CLI_REQUEST_ERROR;
+               req->state = SMBCLI_REQUEST_ERROR;
+               if (req->async.fn) {
+                       req->async.fn(req);
+               }
+       } else {
+               talloc_free(buffer);
        }
+       return NT_STATUS_OK;
 }
 
 /*
-  process some pending receives
+  process some read/write requests that are pending
+  return false if the socket is dead
 */
-static void cli_transport_process_recv(struct cli_transport *transport)
+bool smbcli_transport_process(struct smbcli_transport *transport)
 {
-       /* a incoming packet goes through 2 stages - first we read the
-          4 byte header, which tells us how much more is coming. Then
-          we read the rest */
-       if (transport->recv_buffer.received < NBT_HDR_SIZE) {
-               ssize_t ret;
-               ret = cli_sock_read(transport->socket, 
-                                   transport->recv_buffer.header + 
-                                   transport->recv_buffer.received,
-                                   NBT_HDR_SIZE - transport->recv_buffer.received);
-               if (ret == -1) {
-                       if (errno == EINTR || errno == EAGAIN) {
-                               return;
-                       }
-                       cli_transport_dead(transport);
-                       return;
-               }
+       NTSTATUS status;
+       size_t npending;
 
-               transport->recv_buffer.received += ret;
-
-               if (transport->recv_buffer.received == NBT_HDR_SIZE) {
-                       /* we've got a full header */
-                       transport->recv_buffer.req_size = smb_len(transport->recv_buffer.header) + NBT_HDR_SIZE;
-                       transport->recv_buffer.buffer = talloc(transport->mem_ctx,
-                                                              NBT_HDR_SIZE+transport->recv_buffer.req_size);
-                       if (transport->recv_buffer.buffer == NULL) {
-                               cli_transport_dead(transport);
-                               return;
-                       }
-                       memcpy(transport->recv_buffer.buffer, transport->recv_buffer.header, NBT_HDR_SIZE);
-               }
+       packet_queue_run(transport->packet);
+       if (transport->socket->sock == NULL) {
+               return false;
        }
 
-       if (transport->recv_buffer.received < transport->recv_buffer.req_size) {
-               ssize_t ret;
-               ret = cli_sock_read(transport->socket, 
-                                   transport->recv_buffer.buffer + 
-                                   transport->recv_buffer.received,
-                                   transport->recv_buffer.req_size - 
-                                   transport->recv_buffer.received);
-               if (ret == -1) {
-                       if (errno == EINTR || errno == EAGAIN) {
-                               return;
-                       }
-                       cli_transport_dead(transport);
-                       return;
-               }
-               transport->recv_buffer.received += ret;
+       status = socket_pending(transport->socket->sock, &npending);
+       if (NT_STATUS_IS_OK(status) && npending > 0) {
+               packet_recv(transport->packet);
        }
-
-       if (transport->recv_buffer.received != 0 &&
-           transport->recv_buffer.received == transport->recv_buffer.req_size) {
-               cli_transport_finish_recv(transport);
+       if (transport->socket->sock == NULL) {
+               return false;
        }
+       return true;
 }
 
 /*
-  process some read/write requests that are pending
-  return False if the socket is dead
+  handle timeouts of individual smb requests
 */
-BOOL cli_transport_process(struct cli_transport *transport)
+static void smbcli_timeout_handler(struct event_context *ev, struct timed_event *te, 
+                                  struct timeval t, void *private)
 {
-       cli_transport_process_send(transport);
-       cli_transport_process_recv(transport);
-       if (transport->socket->fd == -1) {
-               return False;
+       struct smbcli_request *req = talloc_get_type(private, struct smbcli_request);
+
+       if (req->state == SMBCLI_REQUEST_RECV) {
+               DLIST_REMOVE(req->transport->pending_recv, req);
+       }
+       req->status = NT_STATUS_IO_TIMEOUT;
+       req->state = SMBCLI_REQUEST_ERROR;
+       if (req->async.fn) {
+               req->async.fn(req);
        }
-       return True;
 }
 
 
+/*
+  destroy a request
+*/
+static int smbcli_request_destructor(struct smbcli_request *req)
+{
+       if (req->state == SMBCLI_REQUEST_RECV) {
+               DLIST_REMOVE(req->transport->pending_recv, req);
+       }
+       return 0;
+}
+
 
 /*
   put a request into the send queue
 */
-void cli_transport_send(struct cli_request *req)
+void smbcli_transport_send(struct smbcli_request *req)
 {
+       DATA_BLOB blob;
+       NTSTATUS status;
+
        /* check if the transport is dead */
-       if (req->transport->socket->fd == -1) {
-               req->state = CLI_REQUEST_ERROR;
+       if (req->transport->socket->sock == NULL) {
+               req->state = SMBCLI_REQUEST_ERROR;
                req->status = NT_STATUS_NET_WRITE_FAULT;
                return;
        }
 
-       /* put it on the outgoing socket queue */
-       req->state = CLI_REQUEST_SEND;
-       DLIST_ADD_END(req->transport->pending_send, req, struct cli_request *);
+       blob = data_blob_const(req->out.buffer, req->out.size);
+       status = packet_send(req->transport->packet, blob);
+       if (!NT_STATUS_IS_OK(status)) {
+               req->state = SMBCLI_REQUEST_ERROR;
+               req->status = status;
+               return;
+       }
+
+       if (req->one_way_request) {
+               req->state = SMBCLI_REQUEST_DONE;
+               smbcli_request_destroy(req);
+               return;
+       }
+
+       req->state = SMBCLI_REQUEST_RECV;
+       DLIST_ADD(req->transport->pending_recv, req);
+
+       /* add a timeout */
+       if (req->transport->options.request_timeout) {
+               event_add_timed(req->transport->socket->event.ctx, req, 
+                               timeval_current_ofs(req->transport->options.request_timeout, 0), 
+                               smbcli_timeout_handler, req);
+       }
+
+       talloc_set_destructor(req, smbcli_request_destructor);
+}
+
 
-       /* make sure we look for write events */
-       cli_transport_write_enable(req->transport);
+/****************************************************************************
+ Send an SMBecho (async send)
+*****************************************************************************/
+struct smbcli_request *smb_raw_echo_send(struct smbcli_transport *transport,
+                                        struct smb_echo *p)
+{
+       struct smbcli_request *req;
+
+       req = smbcli_request_setup_transport(transport, SMBecho, 1, p->in.size);
+       if (!req) return NULL;
+
+       SSVAL(req->out.vwv, VWV(0), p->in.repeat_count);
+
+       memcpy(req->out.data, p->in.data, p->in.size);
+
+       ZERO_STRUCT(p->out);
+
+       if (!smbcli_request_send(req)) {
+               smbcli_request_destroy(req);
+               return NULL;
+       }
+
+       return req;
+}
+
+/****************************************************************************
+ raw echo interface (async recv)
+****************************************************************************/
+NTSTATUS smb_raw_echo_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx,
+                          struct smb_echo *p)
+{
+       if (!smbcli_request_receive(req) ||
+           smbcli_request_is_error(req)) {
+               goto failed;
+       }
+
+       SMBCLI_CHECK_WCT(req, 1);
+       p->out.count++;
+       p->out.sequence_number = SVAL(req->in.vwv, VWV(0));
+       p->out.size = req->in.data_size;
+       talloc_free(p->out.data);
+       p->out.data = talloc_array(mem_ctx, uint8_t, p->out.size);
+       NT_STATUS_HAVE_NO_MEMORY(p->out.data);
+
+       if (!smbcli_raw_pull_data(&req->in.bufinfo, req->in.data, p->out.size, p->out.data)) {
+               req->status = NT_STATUS_BUFFER_TOO_SMALL;
+       }
+
+       if (p->out.count == p->in.repeat_count) {
+               return smbcli_request_destroy(req);
+       }
+
+       return NT_STATUS_OK;
+
+failed:
+       return smbcli_request_destroy(req);
+}
+
+/****************************************************************************
+ Send a echo (sync interface)
+*****************************************************************************/
+NTSTATUS smb_raw_echo(struct smbcli_transport *transport, struct smb_echo *p)
+{
+       struct smbcli_request *req = smb_raw_echo_send(transport, p);
+       return smbcli_request_simple_recv(req);
 }