1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
Unix SMB/CIFS implementation.
Fire connect requests to a host and a number of ports, with a timeout
between the connect request. Return if the first connect comes back
successfully or return the last error.
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "lib/socket/socket.h"
#include "lib/events/events.h"
#include "libcli/composite/composite.h"
#include "libcli/resolve/resolve.h"
#define MULTI_PORT_DELAY 2000 /* microseconds */
/*
overall state
*/
struct connect_multi_state {
struct socket_address **server_address;
unsigned num_address, current_address, current_port;
int num_ports;
uint16_t *ports;
struct socket_context *sock;
uint16_t result_port;
int num_connects_sent, num_connects_recv;
struct socket_connect_multi_ex *ex;
};
/*
state of an individual socket_connect_send() call
*/
struct connect_one_state {
struct composite_context *result;
struct socket_context *sock;
struct socket_address *addr;
};
static void continue_resolve_name(struct composite_context *creq);
static void connect_multi_timer(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval tv, void *p);
static void connect_multi_next_socket(struct composite_context *result);
static void continue_one(struct composite_context *creq);
static void continue_one_ex(struct tevent_req *subreq);
/*
setup an async socket_connect, with multiple ports
*/
_PUBLIC_ struct composite_context *socket_connect_multi_ex_send(
TALLOC_CTX *mem_ctx,
const char *server_name,
int num_server_ports,
uint16_t *server_ports,
struct resolve_context *resolve_ctx,
struct tevent_context *event_ctx,
struct socket_connect_multi_ex *ex)
{
struct composite_context *result;
struct connect_multi_state *multi;
int i;
struct nbt_name name;
struct composite_context *creq;
result = talloc_zero(mem_ctx, struct composite_context);
if (result == NULL) return NULL;
result->state = COMPOSITE_STATE_IN_PROGRESS;
result->event_ctx = event_ctx;
multi = talloc_zero(result, struct connect_multi_state);
if (composite_nomem(multi, result)) goto failed;
result->private_data = multi;
multi->num_ports = num_server_ports;
multi->ports = talloc_array(multi, uint16_t, multi->num_ports);
if (composite_nomem(multi->ports, result)) goto failed;
for (i=0; i<multi->num_ports; i++) {
multi->ports[i] = server_ports[i];
}
multi->ex = ex;
/*
we don't want to do the name resolution separately
for each port, so start it now, then only start on
the real sockets once we have an IP
*/
make_nbt_name_server(&name, server_name);
creq = resolve_name_all_send(resolve_ctx, multi, 0, multi->ports[0], &name, result->event_ctx);
if (composite_nomem(creq, result)) goto failed;
composite_continue(result, creq, continue_resolve_name, result);
return result;
failed:
composite_error(result, result->status);
return result;
}
/*
start connecting to the next socket/port in the list
*/
static void connect_multi_next_socket(struct composite_context *result)
{
struct connect_multi_state *multi = talloc_get_type(result->private_data,
struct connect_multi_state);
struct connect_one_state *state;
struct composite_context *creq;
int next = multi->num_connects_sent;
if (next == multi->num_address * multi->num_ports) {
/* don't do anything, just wait for the existing ones to finish */
return;
}
if (multi->current_address == multi->num_address) {
multi->current_address = 0;
multi->current_port += 1;
}
multi->num_connects_sent += 1;
if (multi->server_address == NULL || multi->server_address[multi->current_address] == NULL) {
composite_error(result, NT_STATUS_OBJECT_NAME_NOT_FOUND);
return;
}
state = talloc(multi, struct connect_one_state);
if (composite_nomem(state, result)) return;
state->result = result;
result->status = socket_create(
state, multi->server_address[multi->current_address]->family,
SOCKET_TYPE_STREAM, &state->sock, 0);
if (!composite_is_ok(result)) return;
state->addr = socket_address_copy(state, multi->server_address[multi->current_address]);
if (composite_nomem(state->addr, result)) return;
socket_address_set_port(state->addr, multi->ports[multi->current_port]);
creq = socket_connect_send(state->sock, NULL,
state->addr, 0,
result->event_ctx);
if (composite_nomem(creq, result)) return;
talloc_steal(state, creq);
multi->current_address++;
composite_continue(result, creq, continue_one, state);
/* if there are more ports / addresses to go then setup a timer to fire when we have waited
for a couple of milli-seconds, when that goes off we try the next port regardless
of whether this port has completed */
if (multi->num_ports * multi->num_address > multi->num_connects_sent) {
/* note that this timer is a child of the single
connect attempt state, so it will go away when this
request completes */
tevent_add_timer(result->event_ctx, state,
timeval_current_ofs_usec(MULTI_PORT_DELAY),
connect_multi_timer, result);
}
}
/*
a timer has gone off telling us that we should try the next port
*/
static void connect_multi_timer(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval tv, void *p)
{
struct composite_context *result = talloc_get_type(p, struct composite_context);
connect_multi_next_socket(result);
}
/*
recv name resolution reply then send the next connect
*/
static void continue_resolve_name(struct composite_context *creq)
{
struct composite_context *result = talloc_get_type(creq->async.private_data,
struct composite_context);
struct connect_multi_state *multi = talloc_get_type(result->private_data,
struct connect_multi_state);
struct socket_address **addr;
unsigned i;
result->status = resolve_name_all_recv(creq, multi, &addr, NULL);
if (!composite_is_ok(result)) return;
for(i=0; addr[i]; i++);
multi->num_address = i;
multi->server_address = talloc_steal(multi, addr);
connect_multi_next_socket(result);
}
/*
one of our socket_connect_send() calls hash finished. If it got a
connection or there are none left then we are done
*/
static void continue_one(struct composite_context *creq)
{
struct connect_one_state *state = talloc_get_type(creq->async.private_data,
struct connect_one_state);
struct composite_context *result = state->result;
struct connect_multi_state *multi = talloc_get_type(result->private_data,
struct connect_multi_state);
NTSTATUS status;
status = socket_connect_recv(creq);
if (multi->ex) {
struct tevent_req *subreq;
subreq = multi->ex->establish_send(state,
result->event_ctx,
state->sock,
state->addr,
multi->ex->private_data);
if (composite_nomem(subreq, result)) return;
tevent_req_set_callback(subreq, continue_one_ex, state);
return;
}
multi->num_connects_recv++;
if (NT_STATUS_IS_OK(status)) {
multi->sock = talloc_steal(multi, state->sock);
multi->result_port = state->addr->port;
}
talloc_free(state);
if (NT_STATUS_IS_OK(status) ||
multi->num_connects_recv == (multi->num_address * multi->num_ports)) {
result->status = status;
composite_done(result);
return;
}
/* try the next port */
connect_multi_next_socket(result);
}
/*
one of our multi->ex->establish_send() calls hash finished. If it got a
connection or there are none left then we are done
*/
static void continue_one_ex(struct tevent_req *subreq)
{
struct connect_one_state *state =
tevent_req_callback_data(subreq,
struct connect_one_state);
struct composite_context *result = state->result;
struct connect_multi_state *multi =
talloc_get_type_abort(result->private_data,
struct connect_multi_state);
NTSTATUS status;
multi->num_connects_recv++;
status = multi->ex->establish_recv(subreq);
TALLOC_FREE(subreq);
if (NT_STATUS_IS_OK(status)) {
multi->sock = talloc_steal(multi, state->sock);
multi->result_port = state->addr->port;
}
talloc_free(state);
if (NT_STATUS_IS_OK(status) ||
multi->num_connects_recv == (multi->num_address * multi->num_ports)) {
result->status = status;
composite_done(result);
return;
}
/* try the next port */
connect_multi_next_socket(result);
}
/*
async recv routine for socket_connect_multi()
*/
_PUBLIC_ NTSTATUS socket_connect_multi_ex_recv(struct composite_context *ctx,
TALLOC_CTX *mem_ctx,
struct socket_context **sock,
uint16_t *port)
{
NTSTATUS status = composite_wait(ctx);
if (NT_STATUS_IS_OK(status)) {
struct connect_multi_state *multi =
talloc_get_type(ctx->private_data,
struct connect_multi_state);
*sock = talloc_steal(mem_ctx, multi->sock);
*port = multi->result_port;
}
talloc_free(ctx);
return status;
}
NTSTATUS socket_connect_multi_ex(TALLOC_CTX *mem_ctx,
const char *server_address,
int num_server_ports, uint16_t *server_ports,
struct resolve_context *resolve_ctx,
struct tevent_context *event_ctx,
struct socket_connect_multi_ex *ex,
struct socket_context **result,
uint16_t *result_port)
{
struct composite_context *ctx =
socket_connect_multi_ex_send(mem_ctx, server_address,
num_server_ports, server_ports,
resolve_ctx,
event_ctx,
ex);
return socket_connect_multi_ex_recv(ctx, mem_ctx, result, result_port);
}
/*
setup an async socket_connect, with multiple ports
*/
_PUBLIC_ struct composite_context *socket_connect_multi_send(
TALLOC_CTX *mem_ctx,
const char *server_name,
int num_server_ports,
uint16_t *server_ports,
struct resolve_context *resolve_ctx,
struct tevent_context *event_ctx)
{
return socket_connect_multi_ex_send(mem_ctx,
server_name,
num_server_ports,
server_ports,
resolve_ctx,
event_ctx,
NULL); /* ex */
}
/*
async recv routine for socket_connect_multi()
*/
_PUBLIC_ NTSTATUS socket_connect_multi_recv(struct composite_context *ctx,
TALLOC_CTX *mem_ctx,
struct socket_context **sock,
uint16_t *port)
{
return socket_connect_multi_ex_recv(ctx, mem_ctx, sock, port);
}
NTSTATUS socket_connect_multi(TALLOC_CTX *mem_ctx,
const char *server_address,
int num_server_ports, uint16_t *server_ports,
struct resolve_context *resolve_ctx,
struct tevent_context *event_ctx,
struct socket_context **result,
uint16_t *result_port)
{
return socket_connect_multi_ex(mem_ctx,
server_address,
num_server_ports,
server_ports,
resolve_ctx,
event_ctx,
NULL, /* ex */
result,
result_port);
}
|