Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / libcli / raw / clitransport.c
index 15959ce272c17a35dc92626e66a964735bef204d..7a3993c79bf5083ddddb3a9709e9f3266c96e02d 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 "libcli/raw/libcliraw.h"
+#include "libcli/raw/raw_proto.h"
 #include "lib/socket/socket.h"
-#include "dlinklist.h"
+#include "../lib/util/dlinklist.h"
 #include "lib/events/events.h"
 #include "lib/stream/packet.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
+#include "../libcli/nbt/libnbt.h"
 
 
-static void smbcli_transport_process_send(struct smbcli_transport *transport);
-
 /*
   an event has happened on the socket
 */
-static void smbcli_transport_event_handler(struct event_context *ev, 
-                                          struct fd_event *fde, 
-                                          uint16_t flags, void *private)
+static void smbcli_transport_event_handler(struct tevent_context *ev, 
+                                          struct tevent_fd *fde, 
+                                          uint16_t flags, void *private_data)
 {
-       struct smbcli_transport *transport = talloc_get_type(private,
+       struct smbcli_transport *transport = talloc_get_type(private_data,
                                                             struct smbcli_transport);
        if (flags & EVENT_FD_READ) {
                packet_recv(transport->packet);
                return;
        }
        if (flags & EVENT_FD_WRITE) {
-               smbcli_transport_process_send(transport);
+               packet_queue_run(transport->packet);
        }
 }
 
 /*
   destroy a transport
  */
-static int transport_destructor(void *ptr)
+static int transport_destructor(struct smbcli_transport *transport)
 {
-       struct smbcli_transport *transport = ptr;
-
-       smbcli_transport_dead(transport);
+       smbcli_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
        return 0;
 }
 
@@ -63,19 +61,21 @@ static int transport_destructor(void *ptr)
 /*
   handle receive errors
 */
-static void smbcli_transport_error(void *private, NTSTATUS status)
+static void smbcli_transport_error(void *private_data, NTSTATUS status)
 {
-       struct smbcli_transport *transport = talloc_get_type(private, struct smbcli_transport);
-       smbcli_transport_dead(transport);
+       struct smbcli_transport *transport = talloc_get_type(private_data, struct smbcli_transport);
+       smbcli_transport_dead(transport, status);
 }
 
-static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob);
+static NTSTATUS smbcli_transport_finish_recv(void *private_data, DATA_BLOB blob);
 
 /*
   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)
+                                              TALLOC_CTX *parent_ctx, 
+                                              bool primary, 
+                                              struct smbcli_options *options)
 {
        struct smbcli_transport *transport;
 
@@ -88,11 +88,7 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
                transport->socket = talloc_reference(transport, sock);
        }
        transport->negotiate.protocol = PROTOCOL_NT1;
-       transport->options.use_spnego = lp_use_spnego() && lp_nt_status_support();
-       transport->options.max_xmit = lp_max_xmit();
-       transport->options.max_mux = lp_maxmux();
-       transport->options.request_timeout = SMB_REQUEST_TIMEOUT;
-
+       transport->options = *options;
        transport->negotiate.max_xmit = transport->options.max_xmit;
 
        /* setup the stream -> packet parser */
@@ -107,6 +103,8 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
        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);
+       packet_set_initial_read(transport->packet, 4);
 
        smbcli_init_signing(transport);
 
@@ -116,12 +114,14 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
           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,
+                                                   transport->socket->sock,
                                                    socket_get_fd(transport->socket->sock),
                                                    EVENT_FD_READ,
                                                    smbcli_transport_event_handler,
                                                    transport);
 
+       packet_set_fde(transport->packet, transport->socket->event.fde);
+       packet_set_serialise(transport->packet);
        talloc_set_destructor(transport, transport_destructor);
 
        return transport;
@@ -130,26 +130,22 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
 /*
   mark the transport as dead
 */
-void smbcli_transport_dead(struct smbcli_transport *transport)
+void smbcli_transport_dead(struct smbcli_transport *transport, NTSTATUS status)
 {
        smbcli_sock_dead(transport->socket);
 
-       /* all pending sends become errors */
-       while (transport->pending_send) {
-               struct smbcli_request *req = transport->pending_send;
-               req->state = SMBCLI_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) {
+       /* 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 = NT_STATUS_NET_WRITE_FAULT;
+               req->status = status;
                DLIST_REMOVE(transport->pending_recv, req);
                if (req->async.fn) {
                        req->async.fn(req);
@@ -158,24 +154,6 @@ void smbcli_transport_dead(struct smbcli_transport *transport)
 }
 
 
-/*
-  enable select for write on a transport
-*/
-static void smbcli_transport_write_enable(struct smbcli_transport *transport)
-{
-       struct fd_event *fde = transport->socket->event.fde;
-       EVENT_FD_WRITEABLE(fde);
-}
-
-/*
-  disable select for write on a transport
-*/
-static void smbcli_transport_write_disable(struct smbcli_transport *transport)
-{
-       struct fd_event *fde = transport->socket->event.fde;
-       EVENT_FD_NOT_WRITEABLE(fde);
-}
-
 /*
   send a session request
 */
@@ -212,7 +190,7 @@ struct smbcli_request *smbcli_transport_connect_send(struct smbcli_transport *tr
        memcpy(p, calling_blob.data, calling_blob.length);
        p += calling_blob.length;
 
-       _smb_setlen(req->out.buffer, PTR_DIFF(p, req->out.buffer)-4);
+       _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)) {
@@ -282,7 +260,7 @@ NTSTATUS smbcli_transport_connect_recv(struct smbcli_request *req)
 /*
   send a session request (if needed)
 */
-BOOL smbcli_transport_connect(struct smbcli_transport *transport,
+bool smbcli_transport_connect(struct smbcli_transport *transport,
                              struct nbt_name *calling, 
                              struct nbt_name *called)
 {
@@ -290,7 +268,7 @@ BOOL smbcli_transport_connect(struct smbcli_transport *transport,
        NTSTATUS status;
 
        if (transport->socket->port == 445) {
-               return True;
+               return true;
        }
 
        req = smbcli_transport_connect_send(transport, 
@@ -328,30 +306,30 @@ again:
        return mid;
 }
 
-static void idle_handler(struct event_context *ev, 
-                        struct timed_event *te, struct timeval t, void *private)
+static void idle_handler(struct tevent_context *ev, 
+                        struct tevent_timer *te, struct timeval t, void *private_data)
 {
-       struct smbcli_transport *transport = talloc_get_type(private,
+       struct smbcli_transport *transport = talloc_get_type(private_data,
                                                             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);
+       transport->idle.func(transport, transport->idle.private_data);
 }
 
 /*
   setup the idle handler for a transport
   the period is in microseconds
 */
-void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
+_PUBLIC_ void smbcli_transport_idle_handler(struct smbcli_transport *transport, 
                                   void (*idle_func)(struct smbcli_transport *, void *),
                                   uint64_t period,
-                                  void *private)
+                                  void *private_data)
 {
        transport->idle.func = idle_func;
-       transport->idle.private = private;
+       transport->idle.private_data = private_data;
        transport->idle.period = period;
 
        if (transport->socket->event.te != NULL) {
@@ -364,56 +342,18 @@ void smbcli_transport_idle_handler(struct smbcli_transport *transport,
                                                      idle_handler, transport);
 }
 
-/*
-  process some pending sends
-*/
-static void smbcli_transport_process_send(struct smbcli_transport *transport)
-{
-       while (transport->pending_send) {
-               struct smbcli_request *req = transport->pending_send;
-               NTSTATUS status;
-               size_t nwritten;
-
-               status = smbcli_sock_write(transport->socket, req->out.buffer, 
-                                          req->out.size, &nwritten);
-               if (NT_STATUS_IS_ERR(status)) {
-                       smbcli_transport_dead(transport);
-                       return;
-               }
-               if (!NT_STATUS_IS_OK(status)) {
-                       return;
-               }
-               req->out.buffer += nwritten;
-               req->out.size -= nwritten;
-               if (req->out.size == 0) {
-                       DLIST_REMOVE(transport->pending_send, req);
-                       if (req->one_way_request) {
-                               req->state = SMBCLI_REQUEST_DONE;
-                               smbcli_request_destroy(req);
-                       } else {
-                               req->state = SMBCLI_REQUEST_RECV;
-                               DLIST_ADD(transport->pending_recv, req);
-                       }
-               }
-       }
-
-       /* we're out of requests to send, so don't wait for write
-          events any more */
-       smbcli_transport_write_disable(transport);
-}
-
 /*
   we have a full request in our receive buffer - match it to a pending request
   and process
  */
-static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
+static NTSTATUS smbcli_transport_finish_recv(void *private_data, DATA_BLOB blob)
 {
-       struct smbcli_transport *transport = talloc_get_type(private
+       struct smbcli_transport *transport = talloc_get_type(private_data,
                                                             struct smbcli_transport);
        uint8_t *buffer, *hdr, *vwv;
        int len;
        uint16_t wct=0, mid = 0, op = 0;
-       struct smbcli_request *req;
+       struct smbcli_request *req = NULL;
 
        buffer = blob.data;
        len = blob.length;
@@ -422,7 +362,7 @@ static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
        vwv = hdr + HDR_VWV;
 
        /* see if it could be an oplock break request */
-       if (handle_oplock_break(transport, len, hdr, vwv)) {
+       if (smbcli_handle_oplock_break(transport, len, hdr, vwv)) {
                talloc_free(buffer);
                return NT_STATUS_OK;
        }
@@ -457,6 +397,9 @@ static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
                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 op %d\n", mid, op));
                goto error;
@@ -503,13 +446,15 @@ static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
        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)) {
-               int class = CVAL(req->in.hdr,HDR_RCLS);
+               int eclass = CVAL(req->in.hdr,HDR_RCLS);
                int code = SVAL(req->in.hdr,HDR_ERR);
-               if (class == 0 && code == 0) {
+               if (eclass == 0 && code == 0) {
                        transport->error.e.nt_status = NT_STATUS_OK;
                } else {
-                       transport->error.e.nt_status = NT_STATUS_DOS(class, code);
+                       transport->error.e.nt_status = NT_STATUS_DOS(eclass, code);
                }
        } else {
                transport->error.e.nt_status = NT_STATUS(IVAL(req->in.hdr, HDR_RCLS));
@@ -534,8 +479,22 @@ async:
        /* if this request has an async handler then call that to
           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 = SMBCLI_REQUEST_DONE;
+
+       if (req->recv_helper.fn) {
+               /*
+                * let the recv helper decide in
+                * what state the request really is
+                */
+               req->state = req->recv_helper.fn(req);
+
+               /* if more parts are needed, wait for them */
+               if (req->state <= SMBCLI_REQUEST_RECV) {
+                       return NT_STATUS_OK;
+               }
+       }
+       DLIST_REMOVE(transport->pending_recv, req);
        if (req->async.fn) {
                req->async.fn(req);
        }
@@ -545,22 +504,27 @@ error:
        if (req) {
                DLIST_REMOVE(transport->pending_recv, req);
                req->state = SMBCLI_REQUEST_ERROR;
+               if (req->async.fn) {
+                       req->async.fn(req);
+               }
+       } else {
+               talloc_free(buffer);
        }
        return NT_STATUS_OK;
 }
 
 /*
   process some read/write requests that are pending
-  return False if the socket is dead
+  return false if the socket is dead
 */
-BOOL smbcli_transport_process(struct smbcli_transport *transport)
+_PUBLIC_ bool smbcli_transport_process(struct smbcli_transport *transport)
 {
        NTSTATUS status;
        size_t npending;
 
-       smbcli_transport_process_send(transport);
+       packet_queue_run(transport->packet);
        if (transport->socket->sock == NULL) {
-               return False;
+               return false;
        }
 
        status = socket_pending(transport->socket->sock, &npending);
@@ -568,22 +532,19 @@ BOOL smbcli_transport_process(struct smbcli_transport *transport)
                packet_recv(transport->packet);
        }
        if (transport->socket->sock == NULL) {
-               return False;
+               return false;
        }
-       return True;
+       return true;
 }
 
 /*
   handle timeouts of individual smb requests
 */
-static void smbcli_timeout_handler(struct event_context *ev, struct timed_event *te, 
-                                  struct timeval t, void *private)
+static void smbcli_timeout_handler(struct tevent_context *ev, struct tevent_timer *te, 
+                                  struct timeval t, void *private_data)
 {
-       struct smbcli_request *req = talloc_get_type(private, struct smbcli_request);
+       struct smbcli_request *req = talloc_get_type(private_data, struct smbcli_request);
 
-       if (req->state == SMBCLI_REQUEST_SEND) {
-               DLIST_REMOVE(req->transport->pending_send, req);
-       }
        if (req->state == SMBCLI_REQUEST_RECV) {
                DLIST_REMOVE(req->transport->pending_recv, req);
        }
@@ -598,12 +559,8 @@ static void smbcli_timeout_handler(struct event_context *ev, struct timed_event
 /*
   destroy a request
 */
-static int smbcli_request_destructor(void *ptr)
+static int smbcli_request_destructor(struct smbcli_request *req)
 {
-       struct smbcli_request *req = talloc_get_type(ptr, struct smbcli_request);
-       if (req->state == SMBCLI_REQUEST_SEND) {
-               DLIST_REMOVE(req->transport->pending_send, req);
-       }
        if (req->state == SMBCLI_REQUEST_RECV) {
                DLIST_REMOVE(req->transport->pending_recv, req);
        }
@@ -616,6 +573,9 @@ static int smbcli_request_destructor(void *ptr)
 */
 void smbcli_transport_send(struct smbcli_request *req)
 {
+       DATA_BLOB blob;
+       NTSTATUS status;
+
        /* check if the transport is dead */
        if (req->transport->socket->sock == NULL) {
                req->state = SMBCLI_REQUEST_ERROR;
@@ -623,12 +583,29 @@ void smbcli_transport_send(struct smbcli_request *req)
                return;
        }
 
-       /* put it on the outgoing socket queue */
-       req->state = SMBCLI_REQUEST_SEND;
-       DLIST_ADD_END(req->transport->pending_send, req, struct smbcli_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;
+       }
+
+       packet_queue_run(req->transport->packet);
+       if (req->transport->socket->sock == NULL) {
+               req->state = SMBCLI_REQUEST_ERROR;
+               req->status = NT_STATUS_NET_WRITE_FAULT;
+               return;
+       }
+
+       if (req->one_way_request) {
+               req->state = SMBCLI_REQUEST_DONE;
+               smbcli_request_destroy(req);
+               return;
+       }
 
-       /* make sure we look for write events */
-       smbcli_transport_write_enable(req->transport);
+       req->state = SMBCLI_REQUEST_RECV;
+       DLIST_ADD(req->transport->pending_recv, req);
 
        /* add a timeout */
        if (req->transport->options.request_timeout) {
@@ -639,3 +616,71 @@ void smbcli_transport_send(struct smbcli_request *req)
 
        talloc_set_destructor(req, smbcli_request_destructor);
 }
+
+
+/****************************************************************************
+ Send an SMBecho (async send)
+*****************************************************************************/
+_PUBLIC_ 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);
+}