STEP:smbXcli_base: HACK smbXcli_base => smb_transport
authorStefan Metzmacher <metze@samba.org>
Thu, 27 Sep 2012 08:28:41 +0000 (10:28 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 1 Jun 2018 12:35:02 +0000 (14:35 +0200)
libcli/smb/smbXcli_base.c

index ad1b67b8476e62ff758ae4d481872876243193e3..f93dc137dbf3f1a9e9ad0b59fabc94f8f4b5b912 100644 (file)
@@ -37,6 +37,7 @@
 #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;
@@ -45,11 +46,12 @@ 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;
@@ -327,6 +329,8 @@ 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 = 0x1FFFF;
+       NTSTATUS status;
 
        conn = talloc_zero(mem_ctx, struct smbXcli_conn);
        if (!conn) {
@@ -343,23 +347,23 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        ss = (void *)&conn->local_ss;
        sa = (struct sockaddr *)ss;
        sa_length = sizeof(conn->local_ss);
-       ret = getsockname(fd, sa, &sa_length);
+       ret = 0;//getsockname(fd, sa, &sa_length);
        if (ret == -1) {
                goto error;
        }
        ss = (void *)&conn->remote_ss;
        sa = (struct sockaddr *)ss;
        sa_length = sizeof(conn->remote_ss);
-       ret = getpeername(fd, sa, &sa_length);
+       ret = 0;//getpeername(fd, sa, &sa_length);
        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;
@@ -431,6 +435,22 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        conn->smb2.cc_chunk_len = 1024 * 1024;
        conn->smb2.cc_max_chunks = 16;
 
+#if 1
+       conn->transport = (struct smb_transport *)(void *)(uintptr_t)fd;
+       talloc_get_type_abort(conn->transport, struct smb_transport);
+       talloc_steal(conn, conn->transport);
+       conn->sock_fd = -1;
+#else
+#error
+       conn->sock_fd = fd;
+       status = smb_transport_tcp_existing(conn, fd,
+                                           max_pdu_size,
+                                           &conn->transport);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto error;
+       }
+#endif
+
        talloc_set_destructor(conn, smbXcli_conn_destructor);
        return conn;
 
@@ -445,7 +465,7 @@ bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
                return false;
        }
 
-       if (conn->sock_fd == -1) {
+       if (conn->transport == NULL) {
                return false;
        }
 
@@ -494,7 +514,9 @@ bool smbXcli_conn_support_passthrough(struct smbXcli_conn *conn)
 
 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
 {
-       set_socket_options(conn->sock_fd, options);
+       if (conn->sock_fd != -1) {
+               set_socket_options(conn->sock_fd, options);
+       }
 }
 
 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
@@ -565,7 +587,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[9];
+       uint8_t buf[5];
        struct tevent_req *write_req;
 };
 
@@ -587,9 +609,8 @@ struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        state->conn = conn;
-       SIVAL(state->buf, 4, 0x74697865);
-       SCVAL(state->buf, 8, exitcode);
-       _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
+       SIVAL(state->buf, 0, 0x74697865);
+       SCVAL(state->buf, 4, exitcode);
 
        if (conn->suicide_req != NULL) {
                tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -599,8 +620,9 @@ 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 = writev_send(state, ev, conn->outgoing, conn->sock_fd,
-                            false, &state->iov, 1);
+       subreq = smb_transport_write_pdu_send(state, ev,
+                                             state->conn->transport,
+                                             &state->iov, 1);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -644,16 +666,13 @@ 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);
-       ssize_t nwritten;
-       int err;
+       NTSTATUS status;
 
        state->write_req = NULL;
 
-       nwritten = writev_recv(subreq, &err);
+       status = smb_transport_write_pdu_recv(subreq);
        TALLOC_FREE(subreq);
-       if (nwritten == -1) {
-               /* here, we need to notify all pending requests */
-               NTSTATUS status = map_nt_error_from_unix_common(err);
+       if (!NT_STATUS_IS_OK(status)) {
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }
@@ -1174,9 +1193,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 = read_smb_send(conn->pending,
-                                          state->ev,
-                                          conn->sock_fd);
+       conn->read_smb_req = smb_transport_read_pdu_send(conn->pending,
+                                                        state->ev,
+                                                        conn->transport);
        if (conn->read_smb_req == NULL) {
                return false;
        }
@@ -1189,9 +1208,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);
 
-       conn->sock_fd = -1;
+       TALLOC_FREE(conn->transport);
 
        session = conn->sessions;
        if (talloc_array_length(conn->pending) == 0) {
@@ -1757,8 +1776,10 @@ 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 = talloc_get_size(buf);
-               iov_count = 1;
+               iov[0].iov_len = 4;
+               iov[1].iov_base = (void *)(buf+4);
+               iov[1].iov_len = talloc_get_size(buf) - 4;
+               iov_count = 2;
        }
 
        if (state->conn->dispatch_incoming == NULL) {
@@ -1771,8 +1792,9 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
 
        tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
 
-       subreq = writev_send(state, state->ev, state->conn->outgoing,
-                            state->conn->sock_fd, false, iov, iov_count);
+       subreq = smb_transport_write_pdu_send(state, state->ev,
+                                             state->conn->transport,
+                                             &iov[1], iov_count-1);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1832,16 +1854,13 @@ static void smb1cli_req_writev_done(struct tevent_req *subreq)
        struct smbXcli_req_state *state =
                tevent_req_data(req,
                struct smbXcli_req_state);
-       ssize_t nwritten;
-       int err;
+       NTSTATUS status;
 
        state->write_req = NULL;
 
-       nwritten = writev_recv(subreq, &err);
+       status = smb_transport_write_pdu_recv(subreq);
        TALLOC_FREE(subreq);
-       if (nwritten == -1) {
-               /* here, we need to notify all pending requests */
-               NTSTATUS status = map_nt_error_from_unix_common(err);
+       if (!NT_STATUS_IS_OK(status)) {
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }
@@ -1861,8 +1880,7 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        uint8_t *inbuf;
-       ssize_t received;
-       int err;
+       struct iovec iov;
 
        if (subreq != conn->read_smb_req) {
                DEBUG(1, ("Internal error: cli_smb_received called with "
@@ -1873,14 +1891,14 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
        }
        conn->read_smb_req = NULL;
 
-       received = read_smb_recv(subreq, frame, &inbuf, &err);
+       status = smb_transport_read_pdu_recv(subreq, frame, &iov);
        TALLOC_FREE(subreq);
-       if (received == -1) {
-               status = map_nt_error_from_unix_common(err);
+       if (!NT_STATUS_IS_OK(status)) {
                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);
@@ -2732,8 +2750,11 @@ 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)
@@ -3370,8 +3391,9 @@ skip_credits:
                state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
        }
 
-       subreq = writev_send(state, state->ev, state->conn->outgoing,
-                            state->conn->sock_fd, false, iov, num_iov);
+       subreq = smb_transport_write_pdu_send(state, state->ev,
+                                             state->conn->transport,
+                                             &iov[1], num_iov - 1);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -3436,16 +3458,14 @@ static void smb2cli_req_writev_done(struct tevent_req *subreq)
        struct smbXcli_req_state *state =
                tevent_req_data(req,
                struct smbXcli_req_state);
-       ssize_t nwritten;
-       int err;
+       NTSTATUS status;
 
        state->write_req = NULL;
 
-       nwritten = writev_recv(subreq, &err);
+       status = smb_transport_write_pdu_recv(subreq);
        TALLOC_FREE(subreq);
-       if (nwritten == -1) {
+       if (!NT_STATUS_IS_OK(status)) {
                /* here, we need to notify all pending requests */
-               NTSTATUS status = map_nt_error_from_unix_common(err);
                smbXcli_conn_disconnect(state->conn, status);
                return;
        }