r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / lib / socket / connect_multi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Fire connect requests to a host and a number of ports, with a timeout
5    between the connect request. Return if the first connect comes back
6    successfully or return the last error.
7
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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/socket/socket.h"
27 #include "lib/events/events.h"
28 #include "libcli/composite/composite.h"
29
30 #define MULTI_PORT_DELAY 2000 /* microseconds */
31
32 /*
33   overall state
34 */
35 struct connect_multi_state {
36         const char *server_address;
37         int num_ports;
38         uint16_t *ports;
39
40         struct socket_context *sock;
41         uint16_t result_port;
42
43         int num_connects_sent, num_connects_recv;
44 };
45
46 /*
47   state of an individual socket_connect_send() call
48 */
49 struct connect_one_state {
50         struct composite_context *result;
51         struct socket_context *sock;
52         uint16_t port;
53 };
54
55 static void continue_resolve_name(struct composite_context *creq);
56 static void connect_multi_timer(struct event_context *ev,
57                                     struct timed_event *te,
58                                     struct timeval tv, void *p);
59 static void connect_multi_next_socket(struct composite_context *result);
60 static void continue_one(struct composite_context *creq);
61
62 /*
63   setup an async socket_connect, with multiple ports
64 */
65 struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
66                                                     const char *server_address,
67                                                     int num_server_ports,
68                                                     uint16_t *server_ports,
69                                                     struct event_context *event_ctx)
70 {
71         struct composite_context *result;
72         struct connect_multi_state *multi;
73         int i;
74
75         result = talloc_zero(mem_ctx, struct composite_context);
76         if (result == NULL) return NULL;
77         result->state = COMPOSITE_STATE_IN_PROGRESS;
78         result->event_ctx = event_ctx;
79
80         multi = talloc_zero(result, struct connect_multi_state);
81         if (composite_nomem(multi, result)) goto failed;
82         result->private_data = multi;
83
84         multi->server_address = talloc_strdup(multi, server_address);
85         if (composite_nomem(multi->server_address, result)) goto failed;
86
87         multi->num_ports = num_server_ports;
88         multi->ports = talloc_array(multi, uint16_t, multi->num_ports);
89         if (composite_nomem(multi->ports, result)) goto failed;
90
91         for (i=0; i<multi->num_ports; i++) {
92                 multi->ports[i] = server_ports[i];
93         }
94
95         if (!is_ipaddress(server_address)) {
96                 /*  
97                     we don't want to do the name resolution separately
98                     for each port, so start it now, then only start on
99                     the real sockets once we have an IP
100                  */
101                 struct nbt_name name;
102                 struct composite_context *creq;
103                 make_nbt_name_client(&name, server_address);
104                 creq = resolve_name_send(&name, result->event_ctx,
105                                          lp_name_resolve_order());
106                 if (composite_nomem(creq, result)) goto failed;
107                 composite_continue(result, creq, continue_resolve_name, result);
108                 return result;
109         }
110
111         /* now we've setup the state we can process the first socket */
112         connect_multi_next_socket(result);
113
114         if (!NT_STATUS_IS_OK(result->status)) {
115                 goto failed;
116         }
117
118         return result;
119
120  failed:
121         composite_error(result, result->status);
122         return result;
123 }
124
125 /*
126   start connecting to the next socket/port in the list
127 */
128 static void connect_multi_next_socket(struct composite_context *result)
129 {
130         struct connect_multi_state *multi = talloc_get_type(result->private_data, 
131                                                             struct connect_multi_state);
132         struct connect_one_state *state;
133         struct composite_context *creq;
134         int next = multi->num_connects_sent;
135
136         if (next == multi->num_ports) {
137                 /* don't do anything, just wait for the existing ones to finish */
138                 return;
139         }
140
141         multi->num_connects_sent += 1;
142
143         state = talloc(multi, struct connect_one_state);
144         if (composite_nomem(state, result)) return;
145
146         state->result = result;
147         state->port = multi->ports[next];
148
149         result->status = socket_create("ipv4", SOCKET_TYPE_STREAM, &state->sock, 0);
150         if (!composite_is_ok(result)) return;
151
152         talloc_steal(state, state->sock);
153
154         creq = socket_connect_send(state->sock, NULL, 0, 
155                                    multi->server_address, state->port, 0, result->event_ctx);
156         if (composite_nomem(creq, result)) return;
157         talloc_steal(state, creq);
158
159         composite_continue(result, creq, continue_one, state);
160
161         /* if there are more ports to go then setup a timer to fire when we have waited
162            for a couple of milli-seconds, when that goes off we try the next port regardless
163            of whether this port has completed */
164         if (multi->num_ports > multi->num_connects_sent) {
165                 /* note that this timer is a child of the single
166                    connect attempt state, so it will go away when this
167                    request completes */
168                 event_add_timed(result->event_ctx, state,
169                                 timeval_current_ofs(0, MULTI_PORT_DELAY),
170                                 connect_multi_timer, result);
171         }
172 }
173
174 /*
175   a timer has gone off telling us that we should try the next port
176 */
177 static void connect_multi_timer(struct event_context *ev,
178                                 struct timed_event *te,
179                                 struct timeval tv, void *p)
180 {
181         struct composite_context *result = talloc_get_type(p, struct composite_context);
182         connect_multi_next_socket(result);
183 }
184
185
186 /*
187   recv name resolution reply then send the next connect
188 */
189 static void continue_resolve_name(struct composite_context *creq)
190 {
191         struct composite_context *result = talloc_get_type(creq->async.private_data, 
192                                                            struct composite_context);
193         struct connect_multi_state *multi = talloc_get_type(result->private_data, 
194                                                             struct connect_multi_state);
195         const char *addr;
196
197         result->status = resolve_name_recv(creq, multi, &addr);
198         if (!composite_is_ok(result)) return;
199
200         multi->server_address = addr;
201
202         connect_multi_next_socket(result);
203 }
204
205 /*
206   one of our socket_connect_send() calls hash finished. If it got a
207   connection or there are none left then we are done
208 */
209 static void continue_one(struct composite_context *creq)
210 {
211         struct connect_one_state *state = talloc_get_type(creq->async.private_data, 
212                                                           struct connect_one_state);
213         struct composite_context *result = state->result;
214         struct connect_multi_state *multi = talloc_get_type(result->private_data, 
215                                                             struct connect_multi_state);
216         NTSTATUS status;
217         multi->num_connects_recv++;
218
219         status = socket_connect_recv(creq);
220
221         if (NT_STATUS_IS_OK(status)) {
222                 multi->sock = talloc_steal(multi, state->sock);
223                 multi->result_port = state->port;
224         }
225
226         talloc_free(state);
227
228         if (NT_STATUS_IS_OK(status) || 
229             multi->num_connects_recv == multi->num_ports) {
230                 result->status = status;
231                 composite_done(result);
232                 return;
233         }
234
235         /* try the next port */
236         connect_multi_next_socket(result);
237 }
238
239 /*
240   async recv routine for socket_connect_multi()
241  */
242 NTSTATUS socket_connect_multi_recv(struct composite_context *ctx,
243                                    TALLOC_CTX *mem_ctx,
244                                    struct socket_context **sock,
245                                    uint16_t *port)
246 {
247         NTSTATUS status = composite_wait(ctx);
248         if (NT_STATUS_IS_OK(status)) {
249                 struct connect_multi_state *multi =
250                         talloc_get_type(ctx->private_data,
251                                         struct connect_multi_state);
252                 *sock = talloc_steal(mem_ctx, multi->sock);
253                 *port = multi->result_port;
254         }
255         talloc_free(ctx);
256         return status;
257 }
258
259 NTSTATUS socket_connect_multi(TALLOC_CTX *mem_ctx,
260                               const char *server_address,
261                               int num_server_ports, uint16_t *server_ports,
262                               struct event_context *event_ctx,
263                               struct socket_context **result,
264                               uint16_t *result_port)
265 {
266         struct composite_context *ctx =
267                 socket_connect_multi_send(mem_ctx, server_address,
268                                           num_server_ports, server_ports,
269                                           event_ctx);
270         return socket_connect_multi_recv(ctx, mem_ctx, result, result_port);
271 }