dd2440b0a81b1b3cdc73149b65325ef418477693
[gd/samba-autobuild/.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
5    events system
6
7    Copyright (C) Andrew Tridgell 2005
8    Copyright (C) Volker Lendecke 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "lib/socket/socket.h"
26 #include "lib/events/events.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/resolve/resolve.h"
29
30
31 struct connect_state {
32         struct socket_context *sock;
33         const struct socket_address *my_address;
34         const struct socket_address *server_address;
35         uint32_t flags;
36 };
37
38 static void socket_connect_handler(struct event_context *ev,
39                                    struct fd_event *fde, 
40                                    uint16_t flags, void *private);
41 static void continue_resolve_name(struct composite_context *ctx);
42 static void continue_socket_connect(struct composite_context *creq);
43
44 /*
45   call the real socket_connect() call, and setup event handler
46 */
47 static void socket_send_connect(struct composite_context *result)
48 {
49         struct composite_context *creq;
50         struct fd_event *fde;
51         struct connect_state *state = talloc_get_type(result->private_data, 
52                                                       struct connect_state);
53
54         creq = talloc_zero(state, struct composite_context);
55         if (composite_nomem(creq, result)) return;
56         creq->state = COMPOSITE_STATE_IN_PROGRESS;
57         creq->event_ctx = result->event_ctx;
58         creq->async.fn = continue_socket_connect;
59         creq->async.private_data = result;
60
61         result->status = socket_connect(state->sock,
62                                         state->my_address,
63                                         state->server_address,
64                                         state->flags);
65         if (NT_STATUS_IS_ERR(result->status) && 
66             !NT_STATUS_EQUAL(result->status,
67                              NT_STATUS_MORE_PROCESSING_REQUIRED)) {
68                 composite_error(result, result->status);
69                 return;
70         }
71
72         fde = event_add_fd(result->event_ctx, result,
73                            socket_get_fd(state->sock),
74                            EVENT_FD_READ|EVENT_FD_WRITE, 
75                            socket_connect_handler, result);
76         composite_nomem(fde, result);
77 }
78
79
80 /*
81   send a socket connect, potentially doing some name resolution first
82 */
83 struct composite_context *socket_connect_send(struct socket_context *sock,
84                                               struct socket_address *my_address,
85                                               struct socket_address *server_address, 
86                                               uint32_t flags,
87                                               struct event_context *event_ctx)
88 {
89         struct composite_context *result;
90         struct connect_state *state;
91
92         result = talloc_zero(sock, struct composite_context);
93         if (result == NULL) return NULL;
94         result->state = COMPOSITE_STATE_IN_PROGRESS;
95         result->event_ctx = event_ctx;
96
97         state = talloc_zero(result, struct connect_state);
98         if (composite_nomem(state, result)) return result;
99         result->private_data = state;
100
101         state->sock = talloc_reference(state, sock);
102         if (composite_nomem(state->sock, result)) return result;
103
104         if (my_address) {
105                 void *ref = talloc_reference(state, my_address);
106                 if (composite_nomem(ref, result)) {
107                         return result;
108                 }
109                 state->my_address = my_address;
110         }
111
112         {
113                 void *ref = talloc_reference(state, server_address);
114                 if (composite_nomem(ref, result)) {
115                         return result;
116                 }
117                 state->server_address = server_address;
118         }
119
120         state->flags = flags;
121
122         set_blocking(socket_get_fd(sock), False);
123
124         if (server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
125                 struct nbt_name name;
126                 struct composite_context *creq;
127                 make_nbt_name_client(&name, server_address->addr);
128                 creq = resolve_name_send(&name, result->event_ctx,
129                                          lp_name_resolve_order());
130                 if (composite_nomem(creq, result)) return result;
131                 composite_continue(result, creq, continue_resolve_name, result);
132                 return result;
133         }
134
135         socket_send_connect(result);
136
137         return result;
138 }
139
140 /*
141   handle write events on connect completion
142 */
143 static void socket_connect_handler(struct event_context *ev,
144                                    struct fd_event *fde, 
145                                    uint16_t flags, void *private)
146 {
147         struct composite_context *result =
148                 talloc_get_type(private, struct composite_context);
149         struct connect_state *state = talloc_get_type(result->private_data,
150                                                       struct connect_state);
151
152         result->status = socket_connect_complete(state->sock, state->flags);
153         if (!composite_is_ok(result)) return;
154
155         composite_done(result);
156 }
157
158 /*
159   recv name resolution reply then send the connect
160 */
161 static void continue_resolve_name(struct composite_context *creq)
162 {
163         struct composite_context *result = talloc_get_type(creq->async.private_data, 
164                                                            struct composite_context);
165         struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
166         const char *addr;
167
168         result->status = resolve_name_recv(creq, state, &addr);
169         if (!composite_is_ok(result)) return;
170
171         state->server_address = socket_address_from_strings(state, state->sock->backend_name,
172                                                             addr, state->server_address->port);
173         if (composite_nomem(state->server_address, result)) return;
174
175         socket_send_connect(result);
176 }
177
178 /*
179   called when a connect has finished. Complete the top level composite context
180 */
181 static void continue_socket_connect(struct composite_context *creq)
182 {
183         struct composite_context *result = talloc_get_type(creq->async.private_data, 
184                                                            struct composite_context);
185         result->status = creq->status;
186         if (!composite_is_ok(result)) return;
187         composite_done(result);
188 }
189
190
191 /*
192   wait for a socket_connect_send() to finish
193 */
194 NTSTATUS socket_connect_recv(struct composite_context *result)
195 {
196         NTSTATUS status = composite_wait(result);
197         talloc_free(result);
198         return status;
199 }
200
201
202 /*
203   like socket_connect() but takes an event context, doing a semi-async connect
204 */
205 NTSTATUS socket_connect_ev(struct socket_context *sock,
206                            struct socket_address *my_address,
207                            struct socket_address *server_address, 
208                            uint32_t flags, struct event_context *ev)
209 {
210         struct composite_context *ctx;
211         ctx = socket_connect_send(sock, my_address, 
212                                   server_address, flags, ev);
213         return socket_connect_recv(ctx);
214 }