r23792: convert Samba4 to GPLv3
[samba.git] / source4 / libcli / raw / clitransport.c
index f579457252ccae848497b7fa0ead00c502be65cf..98f5042d9990790106407d3768400516bf676c2e 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 "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"
 
 
 /*
@@ -49,11 +49,9 @@ static void smbcli_transport_event_handler(struct event_context *ev,
 /*
   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;
 }
 
@@ -64,7 +62,7 @@ static int transport_destructor(void *ptr)
 static void smbcli_transport_error(void *private, NTSTATUS status)
 {
        struct smbcli_transport *transport = talloc_get_type(private, struct smbcli_transport);
-       smbcli_transport_dead(transport);
+       smbcli_transport_dead(transport, status);
 }
 
 static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob);
@@ -115,14 +113,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_serialise(transport->packet, transport->socket->event.fde);
-
+       packet_set_fde(transport->packet, transport->socket->event.fde);
+       packet_set_serialise(transport->packet);
        talloc_set_destructor(transport, transport_destructor);
 
        return transport;
@@ -131,15 +129,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);
 
-       /* kill all pending receives */
-       while (transport->pending_recv) {
+       if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
+               status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
+       }
+
+       /* 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);
@@ -184,7 +189,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)) {
@@ -347,7 +352,7 @@ static NTSTATUS smbcli_transport_finish_recv(void *private, DATA_BLOB blob)
        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;
@@ -391,6 +396,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;
@@ -479,6 +487,9 @@ 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);
        }
@@ -517,9 +528,6 @@ static void smbcli_timeout_handler(struct event_context *ev, struct timed_event
 {
        struct smbcli_request *req = talloc_get_type(private, 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);
        }
@@ -534,12 +542,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);
        }
@@ -588,3 +592,71 @@ void smbcli_transport_send(struct smbcli_request *req)
 
        talloc_set_destructor(req, smbcli_request_destructor);
 }
+
+
+/****************************************************************************
+ 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_size(mem_ctx, p->out.size);
+       NT_STATUS_HAVE_NO_MEMORY(p->out.data);
+
+       if (!smbcli_raw_pull_data(req, 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);
+}