2 Unix SMB/CIFS implementation.
4 implements a non-blocking connect operation that is aware of the samba4 events
7 Copyright (C) Andrew Tridgell 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/nbt.h"
31 async name resolution handler for socket_connect_ev, returnes either
32 an IP address or 'localhost' (which is specially recognised)
34 static NTSTATUS connect_resolve(TALLOC_CTX *mem_ctx, const char *address,
35 struct event_context *ev, const char **ret_address)
39 if (is_ipaddress(address) || strcasecmp("localhost", address) == 0) {
40 *ret_address = address;
46 name.type = NBT_NAME_CLIENT;
48 return resolve_name(&name, mem_ctx, ret_address, ev);
53 handle write events on connect completion
55 static void socket_connect_handler(struct event_context *ev, struct fd_event *fde,
56 uint16_t flags, void *private)
58 NTSTATUS *status = (NTSTATUS *)private;
59 *status = NT_STATUS_OK;
64 just like socket_connect() but other events can happen while the
65 connect is ongoing. This isn't as good as making the calling code
66 fully async during its connect phase, but at least it means that any
67 calling code that uses this won't interfere with code that is
70 NTSTATUS socket_connect_ev(struct socket_context *sock,
71 const char *my_address, int my_port,
72 const char *server_address, int server_port,
73 uint32_t flags, struct event_context *ev)
75 TALLOC_CTX *tmp_ctx = talloc_new(sock);
79 we resolve separately to ensure that the name resolutions happens
82 status = connect_resolve(tmp_ctx, server_address, ev, &server_address);
83 if (!NT_STATUS_IS_OK(status)) {
88 set_blocking(socket_get_fd(sock), False);
90 status = socket_connect(sock, my_address, my_port,
91 server_address, server_port, flags);
92 if (NT_STATUS_IS_ERR(status) &&
93 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
97 event_add_fd(ev, tmp_ctx, socket_get_fd(sock), EVENT_FD_WRITE,
98 socket_connect_handler, &status);
100 while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
101 if (event_loop_once(ev) != 0) {
102 talloc_free(tmp_ctx);
103 return NT_STATUS_INTERNAL_ERROR;
107 status = socket_connect_complete(sock, flags);
109 talloc_free(tmp_ctx);