r9702: r9680@blu: tridge | 2005-08-27 18:45:08 +1000
[ira/wip.git] / source4 / lib / socket / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implements a non-blocking connect operation that is aware of the samba4 events
5    system
6
7    Copyright (C) Andrew Tridgell 2005
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/nbt.h"
28
29
30 /*
31   async name resolution handler for socket_connect_ev, returnes either
32   an IP address or 'localhost' (which is specially recognised)
33 */
34 static NTSTATUS connect_resolve(TALLOC_CTX *mem_ctx, const char *address, 
35                                 struct event_context *ev, const char **ret_address)
36 {
37         struct nbt_name name;
38
39         if (is_ipaddress(address) || strcasecmp("localhost", address) == 0) {
40                 *ret_address = address;
41                 return NT_STATUS_OK;
42         }
43
44         name.name = address;
45         name.scope = NULL;
46         name.type = NBT_NAME_CLIENT;
47
48         return resolve_name(&name, mem_ctx, ret_address, ev);
49 }
50
51
52 /*
53   handle write events on connect completion
54 */
55 static void socket_connect_handler(struct event_context *ev, struct fd_event *fde, 
56                                    uint16_t flags, void *private)
57 {
58         NTSTATUS *status = (NTSTATUS *)private;
59         *status = NT_STATUS_OK;
60 }
61
62
63 /*
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
68   properly async
69  */
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)
74 {
75         TALLOC_CTX *tmp_ctx = talloc_new(sock);
76         NTSTATUS status;
77         
78         /*
79           we resolve separately to ensure that the name resolutions happens
80           asynchronously
81         */
82         status = connect_resolve(tmp_ctx, server_address, ev, &server_address);
83         if (!NT_STATUS_IS_OK(status)) {
84                 talloc_free(tmp_ctx);
85                 return status;
86         }
87
88         set_blocking(socket_get_fd(sock), False);
89
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)) {
94                 return status;
95         }
96
97         event_add_fd(ev, tmp_ctx, socket_get_fd(sock), EVENT_FD_WRITE, 
98                      socket_connect_handler, &status);
99
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;
104                 }
105         }
106
107         status = socket_connect_complete(sock, flags);
108
109         talloc_free(tmp_ctx);
110         return status;
111 }