fixed several places that unnecessarily take a reference to the event context
[kai/samba-autobuild/.git] / source4 / libcli / raw / clisocket.c
index c09104e256b00b2d66c3aa09f086e5786195ba98..02da4917e31b0836ea4b15c27ef551b2d7ed2134 100644 (file)
 #include "lib/socket/socket.h"
 #include "libcli/resolve/resolve.h"
 #include "param/param.h"
+#include "libcli/raw/raw_proto.h"
 
 struct sock_connect_state {
        struct composite_context *ctx;
        const char *host_name;
        int num_ports;
        uint16_t *ports;
+       const char *socket_options;
        struct smbcli_socket *result;
 };
 
@@ -45,23 +47,21 @@ static void smbcli_sock_connect_recv_conn(struct composite_context *ctx);
 
 struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
                                                   const char *host_addr,
-                                                  int port,
+                                                  const char **ports,
                                                   const char *host_name,
-                                                  struct event_context *event_ctx)
+                                                  struct resolve_context *resolve_ctx,
+                                                  struct tevent_context *event_ctx,
+                                                  const char *socket_options)
 {
        struct composite_context *result, *ctx;
        struct sock_connect_state *state;
+       int i;
 
        result = talloc_zero(mem_ctx, struct composite_context);
        if (result == NULL) goto failed;
        result->state = COMPOSITE_STATE_IN_PROGRESS;
 
-       if (event_ctx != NULL) {
-               result->event_ctx = talloc_reference(result, event_ctx);
-       } else {
-               result->event_ctx = event_context_init(result);
-       }
-
+       result->event_ctx = event_ctx;
        if (result->event_ctx == NULL) goto failed;
 
        state = talloc(result, struct sock_connect_state);
@@ -72,31 +72,17 @@ struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
        state->host_name = talloc_strdup(state, host_name);
        if (state->host_name == NULL) goto failed;
 
-       if (port == 0) {
-               const char **ports = lp_smb_ports(global_loadparm);
-               int i;
-
-               for (i=0;ports[i];i++) /* noop */ ;
-               if (i == 0) {
-                       DEBUG(3, ("no smb ports defined\n"));
-                       goto failed;
-               }
-               state->num_ports = i;
-               state->ports = talloc_array(state, uint16_t, i);
-               if (state->ports == NULL) goto failed;
-               for (i=0;ports[i];i++) {
-                       state->ports[i] = atoi(ports[i]);
-               }
-       } else {
-               state->ports = talloc_array(state, uint16_t, 1);
-               if (state->ports == NULL) goto failed;
-               state->num_ports = 1;
-               state->ports[0] = port;
+       state->num_ports = str_list_length(ports);
+       state->ports = talloc_array(state, uint16_t, state->num_ports);
+       if (state->ports == NULL) goto failed;
+       for (i=0;ports[i];i++) {
+               state->ports[i] = atoi(ports[i]);
        }
+       state->socket_options = talloc_reference(state, socket_options);
 
        ctx = socket_connect_multi_send(state, host_addr,
                                        state->num_ports, state->ports,
-                                       lp_resolve_context(global_loadparm),
+                                       resolve_ctx,
                                        state->ctx->event_ctx);
        if (ctx == NULL) goto failed;
        ctx->async.fn = smbcli_sock_connect_recv_conn;
@@ -121,7 +107,7 @@ static void smbcli_sock_connect_recv_conn(struct composite_context *ctx)
        if (!composite_is_ok(state->ctx)) return;
 
        state->ctx->status =
-               socket_set_option(sock, lp_socket_options(global_loadparm), NULL);
+               socket_set_option(sock, state->socket_options, NULL);
        if (!composite_is_ok(state->ctx)) return;
 
 
@@ -132,8 +118,7 @@ static void smbcli_sock_connect_recv_conn(struct composite_context *ctx)
        state->result->port = port;
        state->result->hostname = talloc_steal(sock, state->host_name);
 
-       state->result->event.ctx =
-               talloc_reference(state->result, state->ctx->event_ctx);
+       state->result->event.ctx = state->ctx->event_ctx;
        if (composite_nomem(state->result->event.ctx, state->ctx)) return;
 
        composite_done(state->ctx);
@@ -164,14 +149,17 @@ NTSTATUS smbcli_sock_connect_recv(struct composite_context *c,
   sync version of the function
 */
 NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
-                            const char *host_addr, int port,
+                            const char *host_addr, const char **ports,
                             const char *host_name,
-                            struct event_context *event_ctx,
+                            struct resolve_context *resolve_ctx,
+                            struct tevent_context *event_ctx,
+                                const char *socket_options,
                             struct smbcli_socket **result)
 {
        struct composite_context *c =
-               smbcli_sock_connect_send(mem_ctx, host_addr, port, host_name,
-                                        event_ctx);
+               smbcli_sock_connect_send(mem_ctx, host_addr, ports, host_name,
+                                        resolve_ctx,
+                                        event_ctx, socket_options);
        return smbcli_sock_connect_recv(c, mem_ctx, result);
 }
 
@@ -179,7 +167,7 @@ NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
 /****************************************************************************
  mark the socket as dead
 ****************************************************************************/
-void smbcli_sock_dead(struct smbcli_socket *sock)
+_PUBLIC_ void smbcli_sock_dead(struct smbcli_socket *sock)
 {
        talloc_free(sock->event.fde);
        sock->event.fde = NULL;
@@ -198,10 +186,11 @@ void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
 /****************************************************************************
 resolve a hostname and connect 
 ****************************************************************************/
-struct smbcli_socket *smbcli_sock_connect_byname(const char *host, int port,
+_PUBLIC_ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, const char **ports,
                                                 TALLOC_CTX *mem_ctx,
                                                 struct resolve_context *resolve_ctx,
-                                                struct event_context *event_ctx)
+                                                struct tevent_context *event_ctx,
+                                                const char *socket_options)
 {
        int name_type = NBT_NAME_SERVER;
        const char *address;
@@ -211,6 +200,11 @@ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, int port,
        TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
        struct smbcli_socket *result;
 
+       if (event_ctx == NULL) {
+               DEBUG(0, ("Invalid NULL event context passed in as parameter\n"));
+               return NULL;
+       }
+
        if (tmp_ctx == NULL) {
                DEBUG(0, ("talloc_new failed\n"));
                return NULL;
@@ -223,16 +217,6 @@ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, int port,
                return NULL;
        }
 
-       if (event_ctx == NULL) {
-               event_ctx = event_context_init(mem_ctx);
-       }
-
-       if (event_ctx == NULL) {
-               DEBUG(0, ("event_context_init failed\n"));
-               talloc_free(tmp_ctx);
-               return NULL;
-       }
-
        /* allow hostnames of the form NAME#xx and do a netbios lookup */
        if ((p = strchr(name, '#'))) {
                name_type = strtol(p+1, NULL, 16);
@@ -247,8 +231,9 @@ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, int port,
                return NULL;
        }
 
-       status = smbcli_sock_connect(mem_ctx, address, port, name, event_ctx,
-                                    &result);
+       status = smbcli_sock_connect(mem_ctx, address, ports, name, resolve_ctx,
+                                    event_ctx, 
+                                        socket_options, &result);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(9, ("smbcli_sock_connect failed: %s\n",