Merge 2610c05b5b95cc7036b3d6dfb894c6cfbdb68483 as Samba-4.0alpha16
[amitay/samba.git] / source4 / libcli / raw / clisocket.c
index 1dcf2d1c539151a302e7ea0d5de80549ecfd2d77..bf608f1ec1576c090da8b8838a3ce23e01fdeda6 100644 (file)
@@ -27,6 +27,7 @@
 #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;
@@ -49,7 +50,8 @@ struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
                                                   const char **ports,
                                                   const char *host_name,
                                                   struct resolve_context *resolve_ctx,
-                                                  struct event_context *event_ctx)
+                                                  struct tevent_context *event_ctx,
+                                                  const char *socket_options)
 {
        struct composite_context *result, *ctx;
        struct sock_connect_state *state;
@@ -59,12 +61,7 @@ struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
        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);
@@ -81,7 +78,11 @@ struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
        for (i=0;ports[i];i++) {
                state->ports[i] = atoi(ports[i]);
        }
-       state->socket_options = lp_socket_options(global_loadparm);
+       state->socket_options = talloc_reference(state, socket_options);
+
+       if (!host_addr) {
+               host_addr = host_name;
+       }
 
        ctx = socket_connect_multi_send(state, host_addr,
                                        state->num_ports, state->ports,
@@ -121,8 +122,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);
@@ -156,13 +156,14 @@ NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
                             const char *host_addr, const char **ports,
                             const char *host_name,
                             struct resolve_context *resolve_ctx,
-                            struct event_context *event_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, ports, host_name,
                                         resolve_ctx,
-                                        event_ctx);
+                                        event_ctx, socket_options);
        return smbcli_sock_connect_recv(c, mem_ctx, result);
 }
 
@@ -192,7 +193,8 @@ resolve a hostname and connect
 _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;
@@ -202,6 +204,11 @@ _PUBLIC_ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, cons
        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;
@@ -214,16 +221,6 @@ _PUBLIC_ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, cons
                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);
@@ -232,14 +229,15 @@ _PUBLIC_ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, cons
 
        make_nbt_name(&nbt_name, host, name_type);
        
-       status = resolve_name(resolve_ctx, &nbt_name, tmp_ctx, &address, event_ctx);
+       status = resolve_name_ex(resolve_ctx, 0, 0, &nbt_name, tmp_ctx, &address, event_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                talloc_free(tmp_ctx);
                return NULL;
        }
 
        status = smbcli_sock_connect(mem_ctx, address, ports, name, resolve_ctx,
-                                    event_ctx, &result);
+                                    event_ctx, 
+                                        socket_options, &result);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(9, ("smbcli_sock_connect failed: %s\n",