revert libcli/smb/smbXcli_base.c
authorStefan Metzmacher <metze@samba.org>
Thu, 29 Sep 2016 10:53:28 +0000 (12:53 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 1 Jun 2018 12:35:07 +0000 (14:35 +0200)
libcli/smb/smbXcli_base.c

index 267c6c5404923bcf20fb81d0ced66e242395ce2b..ad1b67b8476e62ff758ae4d481872876243193e3 100644 (file)
@@ -37,7 +37,6 @@
 #include "lib/crypto/aes.h"
 #include "lib/crypto/aes_ccm_128.h"
 #include "lib/crypto/aes_gcm_128.h"
-#include "../libcli/smb/smb_transport.h"
 
 struct smbXcli_conn;
 struct smbXcli_req;
@@ -46,12 +45,11 @@ struct smbXcli_tcon;
 
 struct smbXcli_conn {
        int sock_fd;
-       struct smb_transport *transport;
        struct sockaddr_storage local_ss;
        struct sockaddr_storage remote_ss;
        const char *remote_name;
 
-       //struct tevent_queue *outgoing;
+       struct tevent_queue *outgoing;
        struct tevent_req **pending;
        struct tevent_req *read_smb_req;
        struct tevent_req *suicide_req;
@@ -316,9 +314,6 @@ static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
        return 0;
 }
 
-//int smb_direct_transport_fd = -1;
-//const struct smb_transport *smb_direct_transport_ptr;
-
 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
                                         int fd,
                                         const char *remote_name,
@@ -332,8 +327,6 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        struct sockaddr *sa = NULL;
        socklen_t sa_length;
        int ret;
-       size_t max_pdu_size = 0xFFFFFF;//0x1FFFF;
-       NTSTATUS status;
 
        conn = talloc_zero(mem_ctx, struct smbXcli_conn);
        if (!conn) {
@@ -347,7 +340,6 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
                goto error;
        }
 
-       if (0) {
        ss = (void *)&conn->local_ss;
        sa = (struct sockaddr *)ss;
        sa_length = sizeof(conn->local_ss);
@@ -362,12 +354,12 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        if (ret == -1) {
                goto error;
        }
-       }
-       /*conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
+
+       conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
        if (conn->outgoing == NULL) {
                goto error;
        }
-*/     conn->pending = NULL;
+       conn->pending = NULL;
 
        conn->min_protocol = PROTOCOL_NONE;
        conn->max_protocol = PROTOCOL_NONE;
@@ -439,29 +431,6 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        conn->smb2.cc_chunk_len = 1024 * 1024;
        conn->smb2.cc_max_chunks = 16;
 
-//     if (fd == smb_direct_transport_fd) {
-//             void *ptr = (void *)(uintptr_t)fd;
-//             conn->transport = discard_const_p(struct smb_transport, smb_direct_transport_ptr);
-//             smb_direct_transport_ptr = NULL;
-//             smb_direct_transport_fd = -1;
-//
-//     DEBUG(0,("%s: %s: conn->transport [%p] ptr [%p] sock [%d]\n",
-//              __location__, __func__, conn->transport, ptr, fd));
-//
-//     talloc_get_type_abort(conn->transport, struct smb_transport);
-//     talloc_steal(conn, conn->transport);
-//     conn->sock_fd = -1;
-//     } else {
-//     smb_panic(__location__);
-       conn->sock_fd = fd;
-       status = smb_transport_tcp_existing(conn, fd,
-                                           max_pdu_size,
-                                           &conn->transport);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto error;
-       }
-       //}
-
        talloc_set_destructor(conn, smbXcli_conn_destructor);
        return conn;
 
@@ -476,7 +445,7 @@ bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
                return false;
        }
 
-       if (conn->transport == NULL) {
+       if (conn->sock_fd == -1) {
                return false;
        }
 
@@ -525,9 +494,7 @@ bool smbXcli_conn_support_passthrough(struct smbXcli_conn *conn)
 
 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
 {
-       if (conn->sock_fd != -1) {
-               set_socket_options(conn->sock_fd, options);
-       }
+       set_socket_options(conn->sock_fd, options);
 }
 
 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
@@ -598,7 +565,7 @@ void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn *conn,
 struct smbXcli_conn_samba_suicide_state {
        struct smbXcli_conn *conn;
        struct iovec iov;
-       uint8_t buf[5];
+       uint8_t buf[9];
        struct tevent_req *write_req;
 };
 
@@ -620,8 +587,9 @@ struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        state->conn = conn;
-       SIVAL(state->buf, 0, 0x74697865);
-       SCVAL(state->buf, 4, exitcode);
+       SIVAL(state->buf, 4, 0x74697865);
+       SCVAL(state->buf, 8, exitcode);
+       _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
 
        if (conn->suicide_req != NULL) {
                tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -631,9 +599,8 @@ struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
        state->iov.iov_base = state->buf;
        state->iov.iov_len = sizeof(state->buf);
 
-       subreq = smb_transport_write_pdu_send(state, ev,
-                                             state->conn->transport,
-                                             &state->iov, 1);
+       subreq = writev_send(state, ev, conn->outgoing, conn->sock_fd,
+                            false, &state->iov, 1);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -677,13 +644,16 @@ static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
                subreq, struct tevent_req);
        struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
                req, struct smbXcli_conn_samba_suicide_state);
-       NTSTATUS status;
+       ssize_t nwritten;
+       int err;
 
        state->write_req = NULL;
 
-       status = smb_transport_write_pdu_recv(subreq);
+       nwritten = writev_recv(subreq, &err);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (nwritten == -1) {
+               /* here, we need to notify all pending requests */
+               NTSTATUS status = map_nt_error_from_unix_common(err);
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }
@@ -1204,9 +1174,9 @@ static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
         * We're the first ones, add the read_smb request that waits for the
         * answer from the server
         */
-       conn->read_smb_req = smb_transport_read_pdu_send(conn->pending,
-                                                        state->ev,
-                                                        conn->transport);
+       conn->read_smb_req = read_smb_send(conn->pending,
+                                          state->ev,
+                                          conn->sock_fd);
        if (conn->read_smb_req == NULL) {
                return false;
        }
@@ -1219,9 +1189,9 @@ void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
        struct smbXcli_session *session;
        int sock_fd = conn->sock_fd;
 
-       //tevent_queue_stop(conn->outgoing);
+       tevent_queue_stop(conn->outgoing);
 
-       TALLOC_FREE(conn->transport);
+       conn->sock_fd = -1;
 
        session = conn->sessions;
        if (talloc_array_length(conn->pending) == 0) {
@@ -1787,10 +1757,8 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
                        return NT_STATUS_NO_MEMORY;
                }
                iov[0].iov_base = (void *)buf;
-               iov[0].iov_len = 4;
-               iov[1].iov_base = (void *)(buf+4);
-               iov[1].iov_len = talloc_get_size(buf) - 4;
-               iov_count = 2;
+               iov[0].iov_len = talloc_get_size(buf);
+               iov_count = 1;
        }
 
        if (state->conn->dispatch_incoming == NULL) {
@@ -1803,9 +1771,8 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
 
        tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
 
-       subreq = smb_transport_write_pdu_send(state, state->ev,
-                                             state->conn->transport,
-                                             &iov[1], iov_count-1);
+       subreq = writev_send(state, state->ev, state->conn->outgoing,
+                            state->conn->sock_fd, false, iov, iov_count);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1865,13 +1832,16 @@ static void smb1cli_req_writev_done(struct tevent_req *subreq)
        struct smbXcli_req_state *state =
                tevent_req_data(req,
                struct smbXcli_req_state);
-       NTSTATUS status;
+       ssize_t nwritten;
+       int err;
 
        state->write_req = NULL;
 
-       status = smb_transport_write_pdu_recv(subreq);
+       nwritten = writev_recv(subreq, &err);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (nwritten == -1) {
+               /* here, we need to notify all pending requests */
+               NTSTATUS status = map_nt_error_from_unix_common(err);
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }
@@ -1891,7 +1861,8 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        uint8_t *inbuf;
-       struct iovec iov;
+       ssize_t received;
+       int err;
 
        if (subreq != conn->read_smb_req) {
                DEBUG(1, ("Internal error: cli_smb_received called with "
@@ -1902,14 +1873,14 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
        }
        conn->read_smb_req = NULL;
 
-       status = smb_transport_read_pdu_recv(subreq, frame, &iov);
+       received = read_smb_recv(subreq, frame, &inbuf, &err);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (received == -1) {
+               status = map_nt_error_from_unix_common(err);
                smbXcli_conn_disconnect(conn, status);
                TALLOC_FREE(frame);
                return;
        }
-       inbuf = (uint8_t *)iov.iov_base;
 
        status = conn->dispatch_incoming(conn, frame, inbuf);
        TALLOC_FREE(frame);
@@ -2761,11 +2732,8 @@ NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
 
 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
 {
-       return (talloc_array_length(conn->pending) != 0);
-/*
        return ((tevent_queue_length(conn->outgoing) != 0)
                || (talloc_array_length(conn->pending) != 0));
-*/
 }
 
 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
@@ -3402,9 +3370,8 @@ skip_credits:
                state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
        }
 
-       subreq = smb_transport_write_pdu_send(state, state->ev,
-                                             state->conn->transport,
-                                             &iov[1], num_iov - 1);
+       subreq = writev_send(state, state->ev, state->conn->outgoing,
+                            state->conn->sock_fd, false, iov, num_iov);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -3469,14 +3436,16 @@ static void smb2cli_req_writev_done(struct tevent_req *subreq)
        struct smbXcli_req_state *state =
                tevent_req_data(req,
                struct smbXcli_req_state);
-       NTSTATUS status;
+       ssize_t nwritten;
+       int err;
 
        state->write_req = NULL;
 
-       status = smb_transport_write_pdu_recv(subreq);
+       nwritten = writev_recv(subreq, &err);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (nwritten == -1) {
                /* here, we need to notify all pending requests */
+               NTSTATUS status = map_nt_error_from_unix_common(err);
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }