summaryrefslogtreecommitdiff
path: root/source4/libcli
diff options
context:
space:
mode:
Diffstat (limited to 'source4/libcli')
-rw-r--r--source4/libcli/cliconnect.c12
-rw-r--r--source4/libcli/raw/clisession.c50
-rw-r--r--source4/libcli/raw/clisocket.c28
-rw-r--r--source4/libcli/raw/clitransport.c34
-rw-r--r--source4/libcli/raw/clitree.c38
5 files changed, 83 insertions, 79 deletions
diff --git a/source4/libcli/cliconnect.c b/source4/libcli/cliconnect.c
index 27caaa9df9..f772070305 100644
--- a/source4/libcli/cliconnect.c
+++ b/source4/libcli/cliconnect.c
@@ -31,13 +31,13 @@ BOOL smbcli_socket_connect(struct smbcli_state *cli, const char *server)
if (!sock) return False;
if (!smbcli_sock_connect_byname(sock, server, 0)) {
- smbcli_sock_close(sock);
+ talloc_free(sock);
return False;
}
cli->transport = smbcli_transport_init(sock);
if (!cli->transport) {
- smbcli_sock_close(sock);
+ talloc_free(sock);
return False;
}
@@ -112,8 +112,6 @@ NTSTATUS smbcli_send_tconX(struct smbcli_state *cli, const char *sharename,
cli->tree = smbcli_tree_init(cli->session);
if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
- cli->tree->reference_count++;
-
mem_ctx = talloc_init("tcon");
if (!mem_ctx) {
return NT_STATUS_NO_MEMORY;
@@ -188,7 +186,6 @@ NTSTATUS smbcli_full_connection(struct smbcli_state **ret_cli,
(*ret_cli)->tree = tree;
(*ret_cli)->session = tree->session;
(*ret_cli)->transport = tree->session->transport;
- tree->reference_count++;
done:
talloc_free(mem_ctx);
@@ -225,9 +222,6 @@ struct smbcli_state *smbcli_state_init(void)
void smbcli_shutdown(struct smbcli_state *cli)
{
if (!cli) return;
- if (cli->tree) {
- cli->tree->reference_count++;
- smbcli_tree_close(cli->tree);
- }
+ talloc_free(cli->tree);
talloc_free(cli);
}
diff --git a/source4/libcli/raw/clisession.c b/source4/libcli/raw/clisession.c
index 516da2fa2e..0c6c80d94c 100644
--- a/source4/libcli/raw/clisession.c
+++ b/source4/libcli/raw/clisession.c
@@ -26,6 +26,17 @@
if (!req) return NULL; \
} while (0)
+
+/*
+ destroy a smbcli_session
+*/
+static int session_destroy(void *ptr)
+{
+ struct smbcli_session *session = ptr;
+ talloc_free(session->transport);
+ return 0;
+}
+
/****************************************************************************
Initialize the session context
****************************************************************************/
@@ -42,20 +53,10 @@ struct smbcli_session *smbcli_session_init(struct smbcli_transport *transport)
session->transport = transport;
session->pid = (uint16_t)getpid();
session->vuid = UID_FIELD_INVALID;
- session->transport->reference_count++;
- return session;
-}
+ talloc_set_destructor(session, session_destroy);
-/****************************************************************************
-reduce reference_count and destroy is <= 0
-****************************************************************************/
-void smbcli_session_close(struct smbcli_session *session)
-{
- session->reference_count--;
- if (session->reference_count <= 0) {
- smbcli_transport_close(session->transport);
- }
+ return session;
}
/****************************************************************************
@@ -590,16 +591,27 @@ NTSTATUS smb_raw_ulogoff(struct smbcli_session *session)
/****************************************************************************
- Send a SMBexit
-****************************************************************************/
-NTSTATUS smb_raw_exit(struct smbcli_session *session)
+ Send a exit (async send)
+*****************************************************************************/
+struct smbcli_request *smb_raw_exit_send(struct smbcli_session *session)
{
struct smbcli_request *req;
- req = smbcli_request_setup_session(session, SMBexit, 0, 0);
+ SETUP_REQUEST_SESSION(SMBexit, 0, 0);
- if (smbcli_request_send(req)) {
- smbcli_request_receive(req);
+ if (!smbcli_request_send(req)) {
+ smbcli_request_destroy(req);
+ return NULL;
}
- return smbcli_request_destroy(req);
+
+ return req;
+}
+
+/****************************************************************************
+ Send a exit (sync interface)
+*****************************************************************************/
+NTSTATUS smb_raw_exit(struct smbcli_session *session)
+{
+ struct smbcli_request *req = smb_raw_exit_send(session);
+ return smbcli_request_simple_recv(req);
}
diff --git a/source4/libcli/raw/clisocket.c b/source4/libcli/raw/clisocket.c
index 8481bc73e2..37188f4e77 100644
--- a/source4/libcli/raw/clisocket.c
+++ b/source4/libcli/raw/clisocket.c
@@ -21,6 +21,18 @@
#include "includes.h"
+/*
+ destroy a socket
+ */
+static int sock_destructor(void *ptr)
+{
+ struct smbcli_socket *sock = ptr;
+ if (sock->fd != -1) {
+ close(sock->fd);
+ sock->fd = -1;
+ }
+ return 0;
+}
/*
create a smbcli_socket context
@@ -37,11 +49,13 @@ struct smbcli_socket *smbcli_sock_init(void)
ZERO_STRUCTP(sock);
sock->fd = -1;
sock->port = 0;
+
/* 20 second default timeout */
sock->timeout = 20000;
-
sock->hostname = NULL;
+ talloc_set_destructor(sock, sock_destructor);
+
return sock;
}
@@ -97,18 +111,6 @@ void smbcli_sock_dead(struct smbcli_socket *sock)
}
/****************************************************************************
- reduce socket reference count - if it becomes zero then close
-****************************************************************************/
-void smbcli_sock_close(struct smbcli_socket *sock)
-{
- sock->reference_count--;
- if (sock->reference_count <= 0) {
- smbcli_sock_dead(sock);
- talloc_free(sock);
- }
-}
-
-/****************************************************************************
Set socket options on a open connection.
****************************************************************************/
void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
diff --git a/source4/libcli/raw/clitransport.c b/source4/libcli/raw/clitransport.c
index d70ceceab3..eb1d3631ee 100644
--- a/source4/libcli/raw/clitransport.c
+++ b/source4/libcli/raw/clitransport.c
@@ -42,6 +42,21 @@ static void smbcli_transport_event_handler(struct event_context *ev, struct fd_e
}
/*
+ destroy a transport
+ */
+static int transport_destructor(void *ptr)
+{
+ struct smbcli_transport *transport = ptr;
+
+ smbcli_transport_dead(transport);
+ event_remove_fd(transport->event.ctx, transport->event.fde);
+ event_remove_timed(transport->event.ctx, transport->event.te);
+ event_context_destroy(transport->event.ctx);
+ talloc_free(transport->socket);
+ return 0;
+}
+
+/*
create a transport structure based on an established socket
*/
struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
@@ -67,8 +82,6 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
smbcli_init_signing(transport);
- transport->socket->reference_count++;
-
ZERO_STRUCT(transport->called);
fde.fd = sock->fd;
@@ -79,22 +92,9 @@ struct smbcli_transport *smbcli_transport_init(struct smbcli_socket *sock)
transport->event.fde = event_add_fd(transport->event.ctx, &fde);
- return transport;
-}
+ talloc_set_destructor(transport, transport_destructor);
-/*
- decrease reference count on a transport, and destroy if it becomes
- zero
-*/
-void smbcli_transport_close(struct smbcli_transport *transport)
-{
- transport->reference_count--;
- if (transport->reference_count <= 0) {
- event_remove_fd(transport->event.ctx, transport->event.fde);
- event_remove_timed(transport->event.ctx, transport->event.te);
- event_context_destroy(transport->event.ctx);
- smbcli_sock_close(transport->socket);
- }
+ return transport;
}
/*
diff --git a/source4/libcli/raw/clitree.c b/source4/libcli/raw/clitree.c
index e3a1a6d341..e0072a31b1 100644
--- a/source4/libcli/raw/clitree.c
+++ b/source4/libcli/raw/clitree.c
@@ -26,6 +26,15 @@
if (!req) return NULL; \
} while (0)
+/*
+ destroy a smbcli_tree
+*/
+static int tree_destructor(void *ptr)
+{
+ struct smbcli_tree *tree = ptr;
+ talloc_free(tree->session);
+ return 0;
+}
/****************************************************************************
Initialize the tree context
@@ -41,25 +50,12 @@ struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session)
ZERO_STRUCTP(tree);
tree->session = session;
- tree->session->reference_count++;
+ talloc_set_destructor(tree, tree_destructor);
return tree;
}
/****************************************************************************
-reduce reference count on a tree and destroy if <= 0
-****************************************************************************/
-void smbcli_tree_close(struct smbcli_tree *tree)
-{
- if (!tree) return;
- tree->reference_count--;
- if (tree->reference_count <= 0) {
- smbcli_session_close(tree->session);
- }
-}
-
-
-/****************************************************************************
Send a tconX (async send)
****************************************************************************/
struct smbcli_request *smb_tree_connect_send(struct smbcli_tree *tree, union smb_tcon *parms)
@@ -202,7 +198,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
transport = smbcli_transport_init(sock);
if (!transport) {
- smbcli_sock_close(sock);
+ talloc_free(sock);
return NT_STATUS_NO_MEMORY;
}
@@ -211,7 +207,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
choose_called_name(&called, dest_host, 0x20);
if (!smbcli_transport_connect(transport, &calling, &called)) {
- smbcli_transport_close(transport);
+ talloc_free(transport);
return NT_STATUS_UNSUCCESSFUL;
}
@@ -219,13 +215,13 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
/* negotiate protocol options with the server */
status = smb_raw_negotiate(transport);
if (!NT_STATUS_IS_OK(status)) {
- smbcli_transport_close(transport);
+ talloc_free(transport);
return status;
}
session = smbcli_session_init(transport);
if (!session) {
- smbcli_transport_close(transport);
+ talloc_free(transport);
return NT_STATUS_NO_MEMORY;
}
@@ -251,7 +247,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
status = smb_raw_session_setup(session, mem_ctx, &setup);
if (!NT_STATUS_IS_OK(status)) {
- smbcli_session_close(session);
+ talloc_free(session);
talloc_free(mem_ctx);
return status;
}
@@ -260,7 +256,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
tree = smbcli_tree_init(session);
if (!tree) {
- smbcli_session_close(session);
+ talloc_free(session);
talloc_free(mem_ctx);
return NT_STATUS_NO_MEMORY;
}
@@ -284,7 +280,7 @@ NTSTATUS smbcli_tree_full_connection(struct smbcli_tree **ret_tree,
SAFE_FREE(in_path);
if (!NT_STATUS_IS_OK(status)) {
- smbcli_tree_close(tree);
+ talloc_free(tree);
talloc_free(mem_ctx);
return status;
}