samldb: Allow automatic generation of mAPIIDs
[sfrench/samba-autobuild/.git] / source4 / lib / socket / connect.c
index 04902442e3eb24ba13f4dfd3a01e63be0f186d42..1da8b41bbd7c056f3bf4850aee9771e1dcf52adb 100644 (file)
@@ -1,14 +1,15 @@
 /* 
    Unix SMB/CIFS implementation.
 
-   implements a non-blocking connect operation that is aware of the samba4 events
-   system
+   implements a non-blocking connect operation that is aware of the samba4
+   events system
 
    Copyright (C) Andrew Tridgell 2005
+   Copyright (C) Volker Lendecke 2005
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/socket/socket.h"
 #include "lib/events/events.h"
-#include "librpc/gen_ndr/nbt.h"
+#include "libcli/composite/composite.h"
 
 
+struct connect_state {
+       struct socket_context *sock;
+       const struct socket_address *my_address;
+       const struct socket_address *server_address;
+       uint32_t flags;
+};
+
+static void socket_connect_handler(struct tevent_context *ev,
+                                  struct tevent_fd *fde, 
+                                  uint16_t flags, void *private_data);
+
 /*
-  async name resolution handler for socket_connect_ev, returnes either
-  an IP address or 'localhost' (which is specially recognised)
+  call the real socket_connect() call, and setup event handler
 */
-static NTSTATUS connect_resolve(TALLOC_CTX *mem_ctx, const char *address, 
-                               struct event_context *ev, const char **ret_address)
+static void socket_send_connect(struct composite_context *result)
 {
-       struct nbt_name name;
-
-       name.name = address;
-       name.scope = NULL;
-       name.type = NBT_NAME_CLIENT;
+       struct tevent_fd *fde;
+       struct connect_state *state = talloc_get_type(result->private_data, 
+                                                     struct connect_state);
+
+       result->status = socket_connect(state->sock,
+                                       state->my_address,
+                                       state->server_address,
+                                       state->flags);
+       if (NT_STATUS_IS_ERR(result->status) && 
+           !NT_STATUS_EQUAL(result->status,
+                            NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               composite_error(result, result->status);
+               return;
+       }
 
-       return resolve_name(&name, mem_ctx, ret_address, ev);
+       fde = tevent_add_fd(result->event_ctx, result,
+                          socket_get_fd(state->sock),
+                          TEVENT_FD_READ|TEVENT_FD_WRITE,
+                          socket_connect_handler, result);
+       composite_nomem(fde, result);
 }
 
 
 /*
-  handle write events on connect completion
+  send a socket connect, potentially doing some name resolution first
 */
-static void socket_connect_handler(struct event_context *ev, struct fd_event *fde, 
-                                  uint16_t flags, void *private)
+struct composite_context *socket_connect_send(struct socket_context *sock,
+                                             struct socket_address *my_address,
+                                             struct socket_address *server_address, 
+                                             uint32_t flags,
+                                             struct tevent_context *event_ctx)
 {
-       NTSTATUS *status = (NTSTATUS *)private;
-       *status = NT_STATUS_OK;
-}
+       struct composite_context *result;
+       struct connect_state *state;
 
+       result = composite_create(sock, event_ctx);
+       if (result == NULL) return NULL;
 
-/*
-  just like socket_connect() but other events can happen while the
-  connect is ongoing. This isn't as good as making the calling code
-  fully async during its connect phase, but at least it means that any
-  calling code that uses this won't interfere with code that is
-  properly async
- */
-NTSTATUS socket_connect_ev(struct socket_context *sock,
-                          const char *my_address, int my_port,
-                          const char *server_address, int server_port,
-                          uint32_t flags, struct event_context *ev)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(sock);
-       NTSTATUS status;
-
-       /*
-         we resolve separately to ensure that the name resolutions happens
-         asynchronously
-
-         the check for ipv4 is ugly, but how to do it better? We don't want
-         to try to resolve unix pathnames here
-       */
-       if (strcmp(sock->backend_name, "ipv4") == 0) {
-               status = connect_resolve(tmp_ctx, server_address, ev, &server_address);
-               if (!NT_STATUS_IS_OK(status)) {
-                       talloc_free(tmp_ctx);
-                       return status;
-               }
-       }
+       state = talloc_zero(result, struct connect_state);
+       if (composite_nomem(state, result)) return result;
+       result->private_data = state;
 
-       set_blocking(socket_get_fd(sock), False);
+       state->sock = talloc_reference(state, sock);
+       if (composite_nomem(state->sock, result)) return result;
 
-       status = socket_connect(sock, my_address, my_port, 
-                               server_address, server_port, flags);
-       if (NT_STATUS_IS_ERR(status) && 
-           !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-               return status;
+       if (my_address) {
+               void *ref = talloc_reference(state, my_address);
+               if (composite_nomem(ref, result)) {
+                       return result;
+               }
+               state->my_address = my_address;
        }
 
-       event_add_fd(ev, tmp_ctx, socket_get_fd(sock), EVENT_FD_WRITE, 
-                    socket_connect_handler, &status);
-
-       while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
-               if (event_loop_once(ev) != 0) {
-                       talloc_free(tmp_ctx);
-                       return NT_STATUS_INTERNAL_ERROR;
+       {
+               void *ref = talloc_reference(state, server_address);
+               if (composite_nomem(ref, result)) {
+                       return result;
                }
+               state->server_address = server_address;
        }
 
-       status = socket_connect_complete(sock, flags);
+       state->flags = flags;
+
+       set_blocking(socket_get_fd(sock), false);
 
-       talloc_free(tmp_ctx);
+       socket_send_connect(result);
+
+       return result;
+}
+
+/*
+  handle write events on connect completion
+*/
+static void socket_connect_handler(struct tevent_context *ev,
+                                  struct tevent_fd *fde, 
+                                  uint16_t flags, void *private_data)
+{
+       struct composite_context *result =
+               talloc_get_type(private_data, struct composite_context);
+       struct connect_state *state = talloc_get_type(result->private_data,
+                                                     struct connect_state);
+
+       result->status = socket_connect_complete(state->sock, state->flags);
+       if (!composite_is_ok(result)) return;
+
+       composite_done(result);
+}
+
+/*
+  wait for a socket_connect_send() to finish
+*/
+NTSTATUS socket_connect_recv(struct composite_context *result)
+{
+       NTSTATUS status = composite_wait(result);
+       talloc_free(result);
        return status;
 }
+
+
+/*
+  like socket_connect() but takes an event context, doing a semi-async connect
+*/
+NTSTATUS socket_connect_ev(struct socket_context *sock,
+                          struct socket_address *my_address,
+                          struct socket_address *server_address, 
+                          uint32_t flags,
+                          struct tevent_context *ev)
+{
+       struct composite_context *ctx;
+       ctx = socket_connect_send(sock, my_address, 
+                                 server_address, flags, ev);
+       return socket_connect_recv(ctx);
+}