]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s4:libcli: let struct smbcli_socket hold struct smbXcli_transport instead of sockfd
authorStefan Metzmacher <metze@samba.org>
Wed, 7 May 2025 14:46:55 +0000 (16:46 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 18 Jun 2025 17:52:37 +0000 (17:52 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
source4/libcli/raw/clisocket.c
source4/libcli/raw/clitransport.c
source4/libcli/raw/libcliraw.h
source4/libcli/smb2/transport.c
source4/libcli/smb_composite/connect_nego.c

index a7779e4e0b566c2739dd2cebd2413b5283f1161d..27814617d2ec8dabfde5d07af38c4cda5369b091 100644 (file)
@@ -30,6 +30,7 @@
 #include "lib/socket/socket.h"
 #include "libcli/resolve/resolve.h"
 #include "param/param.h"
+#include "../libcli/smb/smbXcli_base.h"
 #include "libcli/raw/raw_proto.h"
 #include "../libcli/smb/read_smb.h"
 #include "lib/util/util_net.h"
@@ -141,10 +142,7 @@ failed:
 
 static int smbcli_socket_destructor(struct smbcli_socket *sock)
 {
-       if (sock->sockfd != -1) {
-               close(sock->sockfd);
-               sock->sockfd = -1;
-       }
+       TALLOC_FREE(sock->transport);
 
        return 0;
 }
@@ -237,6 +235,7 @@ static void smbcli_sock_connect_recv_conn(struct tevent_req *subreq)
        struct sock_connect_state *state =
                tevent_req_callback_data(subreq,
                struct sock_connect_state);
+       struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, };
        int sockfd = -1;
 
        state->ctx->status = smbsock_any_connect_recv(subreq,
@@ -253,7 +252,14 @@ static void smbcli_sock_connect_recv_conn(struct tevent_req *subreq)
                return;
        }
 
-       state->result->sockfd = sockfd;
+       state->result->transport = smbXcli_transport_bsd(state->result,
+                                                        sockfd,
+                                                        &tp);
+       if (composite_nomem(state->result->transport, state->ctx)) {
+               close(sockfd);
+               return;
+       }
+
        state->result->hostname = talloc_steal(state->result, state->host_name);
 
        talloc_set_destructor(state->result, smbcli_socket_destructor);
index 0388499004d09a2eb367d255db6f68670e3ae98b..b774cce3f5b596d628706843bde7119262a4519f 100644 (file)
@@ -50,8 +50,6 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
                                               struct smbcli_options *options)
 {
        struct smbcli_transport *transport;
-       struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, };
-       struct smbXcli_transport *xtp = NULL;
        uint32_t smb1_capabilities;
 
        transport = talloc_zero(parent_ctx, struct smbcli_transport);
@@ -92,25 +90,16 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock,
                smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
        }
 
-       xtp = smbXcli_transport_bsd(transport, sock->sockfd, &tp);
-       if (xtp == NULL) {
-               TALLOC_FREE(sock);
-               TALLOC_FREE(transport);
-               return NULL;
-       }
-       sock->sockfd = -1;
-       TALLOC_FREE(sock);
-
        transport->conn = smbXcli_conn_create(transport,
-                                             &xtp,
+                                             &sock->transport,
                                              sock->hostname,
                                              options->signing,
                                              smb1_capabilities,
                                              NULL, /* client_guid */
                                              0, /* smb2_capabilities */
                                              NULL); /* smb3_ciphers */
+       TALLOC_FREE(sock);
        if (transport->conn == NULL) {
-               TALLOC_FREE(xtp);
                TALLOC_FREE(transport);
                return NULL;
        }
index a8c2891b08c206c150c93534102e48678b7fd022..417939cd8abc154f2d4e5ff084a93242132bc13e 100644 (file)
@@ -70,7 +70,7 @@ struct smbcli_negotiate {
        
 /* this is the context for a SMB socket associated with the socket itself */
 struct smbcli_socket {
-       int sockfd;
+       struct smbXcli_transport *transport;
 
        /* the hostname we connected to */
        const char *hostname;
index 47d309bda8e4d79a516036bab708f40146941c10..402b894babe4e47ddcecdddef1be3097cfdec1e8 100644 (file)
@@ -48,8 +48,6 @@ struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
                                           struct smbcli_options *options)
 {
        struct smb2_transport *transport;
-       struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, };
-       struct smbXcli_transport *xtp = NULL;
 
        transport = talloc_zero(parent_ctx, struct smb2_transport);
        if (!transport) return NULL;
@@ -65,25 +63,16 @@ struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
                transport->options.max_protocol = PROTOCOL_LATEST;
        }
 
-       xtp = smbXcli_transport_bsd(transport, sock->sockfd, &tp);
-       if (xtp == NULL) {
-               TALLOC_FREE(sock);
-               TALLOC_FREE(transport);
-               return NULL;
-       }
-       sock->sockfd = -1;
-       TALLOC_FREE(sock);
-
        transport->conn = smbXcli_conn_create(transport,
-                                             &xtp,
+                                             &sock->transport,
                                              sock->hostname,
                                              options->signing,
                                              0, /* smb1_capabilities */
                                              &options->client_guid,
                                              options->smb2_capabilities,
                                              &options->smb3_capabilities);
+       TALLOC_FREE(sock);
        if (transport->conn == NULL) {
-               TALLOC_FREE(xtp);
                TALLOC_FREE(transport);
                return NULL;
        }
index ca1718db8ea326870d1b07ccc8d1cf756b8f49b0..1cb97eccb4f4d137787ea02473bd2f408963db06 100644 (file)
@@ -109,8 +109,6 @@ static void smb_connect_nego_connect_done(struct composite_context *creq)
                struct smb_connect_nego_state);
        struct tevent_req *subreq = NULL;
        struct smbcli_socket *sock = NULL;
-       struct smb_transport tp = { .type = SMB_TRANSPORT_TYPE_UNKNOWN, };
-       struct smbXcli_transport *xtp = NULL;
        uint32_t smb1_capabilities;
        uint32_t timeout_msec = state->options.request_timeout * 1000;
        NTSTATUS status;
@@ -145,21 +143,15 @@ static void smb_connect_nego_connect_done(struct composite_context *creq)
                smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
        }
 
-       xtp = smbXcli_transport_bsd(state, sock->sockfd, &tp);
-       if (tevent_req_nomem(xtp, req)) {
-               return;
-       }
-       sock->sockfd = -1;
-       TALLOC_FREE(sock);
-
        state->conn = smbXcli_conn_create(state,
-                                         &xtp,
+                                         &sock->transport,
                                          state->target_hostname,
                                          state->options.signing,
                                          smb1_capabilities,
                                          &state->options.client_guid,
                                          state->options.smb2_capabilities,
                                          &state->options.smb3_capabilities);
+       TALLOC_FREE(sock);
        if (tevent_req_nomem(state->conn, req)) {
                return;
        }