1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Connect to 445 and 139/nbsesssetup
|
---|
4 | Copyright (C) Volker Lendecke 2010
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "../lib/util/tevent_ntstatus.h"
|
---|
22 | #include "client.h"
|
---|
23 | #include "async_smb.h"
|
---|
24 | #include "libsmb/nmblib.h"
|
---|
25 |
|
---|
26 | struct nb_connect_state {
|
---|
27 | struct tevent_context *ev;
|
---|
28 | const struct sockaddr_storage *addr;
|
---|
29 | const char *called_name;
|
---|
30 | int sock;
|
---|
31 |
|
---|
32 | struct nmb_name called;
|
---|
33 | struct nmb_name calling;
|
---|
34 | };
|
---|
35 |
|
---|
36 | static int nb_connect_state_destructor(struct nb_connect_state *state);
|
---|
37 | static void nb_connect_connected(struct tevent_req *subreq);
|
---|
38 | static void nb_connect_done(struct tevent_req *subreq);
|
---|
39 |
|
---|
40 | static struct tevent_req *nb_connect_send(TALLOC_CTX *mem_ctx,
|
---|
41 | struct tevent_context *ev,
|
---|
42 | const struct sockaddr_storage *addr,
|
---|
43 | const char *called_name,
|
---|
44 | int called_type,
|
---|
45 | const char *calling_name,
|
---|
46 | int calling_type)
|
---|
47 | {
|
---|
48 | struct tevent_req *req, *subreq;
|
---|
49 | struct nb_connect_state *state;
|
---|
50 |
|
---|
51 | req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
|
---|
52 | if (req == NULL) {
|
---|
53 | return NULL;
|
---|
54 | }
|
---|
55 | state->ev = ev;
|
---|
56 | state->called_name = called_name;
|
---|
57 | state->addr = addr;
|
---|
58 |
|
---|
59 | state->sock = -1;
|
---|
60 | make_nmb_name(&state->called, called_name, called_type);
|
---|
61 | make_nmb_name(&state->calling, calling_name, calling_type);
|
---|
62 |
|
---|
63 | talloc_set_destructor(state, nb_connect_state_destructor);
|
---|
64 |
|
---|
65 | subreq = open_socket_out_send(state, ev, addr, 139, 5000);
|
---|
66 | if (tevent_req_nomem(subreq, req)) {
|
---|
67 | return tevent_req_post(req, ev);
|
---|
68 | }
|
---|
69 | tevent_req_set_callback(subreq, nb_connect_connected, req);
|
---|
70 | return req;
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int nb_connect_state_destructor(struct nb_connect_state *state)
|
---|
74 | {
|
---|
75 | if (state->sock != -1) {
|
---|
76 | close(state->sock);
|
---|
77 | }
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static void nb_connect_connected(struct tevent_req *subreq)
|
---|
82 | {
|
---|
83 | struct tevent_req *req = tevent_req_callback_data(
|
---|
84 | subreq, struct tevent_req);
|
---|
85 | struct nb_connect_state *state = tevent_req_data(
|
---|
86 | req, struct nb_connect_state);
|
---|
87 | NTSTATUS status;
|
---|
88 |
|
---|
89 | status = open_socket_out_recv(subreq, &state->sock);
|
---|
90 | TALLOC_FREE(subreq);
|
---|
91 | if (!NT_STATUS_IS_OK(status)) {
|
---|
92 | tevent_req_nterror(req, status);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 | subreq = cli_session_request_send(state, state->ev, state->sock,
|
---|
96 | &state->called, &state->calling);
|
---|
97 | if (tevent_req_nomem(subreq, req)) {
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | tevent_req_set_callback(subreq, nb_connect_done, req);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void nb_connect_done(struct tevent_req *subreq)
|
---|
104 | {
|
---|
105 | struct tevent_req *req = tevent_req_callback_data(
|
---|
106 | subreq, struct tevent_req);
|
---|
107 | struct nb_connect_state *state = tevent_req_data(
|
---|
108 | req, struct nb_connect_state);
|
---|
109 | bool ret;
|
---|
110 | int err;
|
---|
111 | uint8_t resp;
|
---|
112 |
|
---|
113 | ret = cli_session_request_recv(subreq, &err, &resp);
|
---|
114 | TALLOC_FREE(subreq);
|
---|
115 | if (!ret) {
|
---|
116 | tevent_req_nterror(req, map_nt_error_from_unix(err));
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
|
---|
122 | */
|
---|
123 |
|
---|
124 | if (resp != 0x82) {
|
---|
125 | /*
|
---|
126 | * The server did not like our session request
|
---|
127 | */
|
---|
128 | close(state->sock);
|
---|
129 | state->sock = -1;
|
---|
130 |
|
---|
131 | if (strequal(state->called_name, "*SMBSERVER")) {
|
---|
132 | /*
|
---|
133 | * Here we could try a name status request and
|
---|
134 | * use the first 0x20 type name.
|
---|
135 | */
|
---|
136 | tevent_req_nterror(
|
---|
137 | req, NT_STATUS_RESOURCE_NAME_NOT_FOUND);
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * We could be subtle and distinguish between
|
---|
143 | * different failure modes, but what we do here
|
---|
144 | * instead is just retry with *SMBSERVER type 0x20.
|
---|
145 | */
|
---|
146 | state->called_name = "*SMBSERVER";
|
---|
147 | make_nmb_name(&state->called, state->called_name, 0x20);
|
---|
148 |
|
---|
149 | subreq = open_socket_out_send(state, state->ev, state->addr,
|
---|
150 | 139, 5000);
|
---|
151 | if (tevent_req_nomem(subreq, req)) {
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | tevent_req_set_callback(subreq, nb_connect_connected, req);
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | tevent_req_done(req);
|
---|
159 | return;
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | static NTSTATUS nb_connect_recv(struct tevent_req *req, int *sock)
|
---|
164 | {
|
---|
165 | struct nb_connect_state *state = tevent_req_data(
|
---|
166 | req, struct nb_connect_state);
|
---|
167 | NTSTATUS status;
|
---|
168 |
|
---|
169 | if (tevent_req_is_nterror(req, &status)) {
|
---|
170 | return status;
|
---|
171 | }
|
---|
172 | *sock = state->sock;
|
---|
173 | state->sock = -1;
|
---|
174 | return NT_STATUS_OK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | struct smbsock_connect_state {
|
---|
178 | struct tevent_context *ev;
|
---|
179 | const struct sockaddr_storage *addr;
|
---|
180 | const char *called_name;
|
---|
181 | uint8_t called_type;
|
---|
182 | const char *calling_name;
|
---|
183 | uint8_t calling_type;
|
---|
184 | struct tevent_req *req_139;
|
---|
185 | struct tevent_req *req_445;
|
---|
186 | int sock;
|
---|
187 | uint16_t port;
|
---|
188 | };
|
---|
189 |
|
---|
190 | static int smbsock_connect_state_destructor(
|
---|
191 | struct smbsock_connect_state *state);
|
---|
192 | static void smbsock_connect_connected(struct tevent_req *subreq);
|
---|
193 | static void smbsock_connect_do_139(struct tevent_req *subreq);
|
---|
194 |
|
---|
195 | struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx,
|
---|
196 | struct tevent_context *ev,
|
---|
197 | const struct sockaddr_storage *addr,
|
---|
198 | uint16_t port,
|
---|
199 | const char *called_name,
|
---|
200 | int called_type,
|
---|
201 | const char *calling_name,
|
---|
202 | int calling_type)
|
---|
203 | {
|
---|
204 | struct tevent_req *req, *subreq;
|
---|
205 | struct smbsock_connect_state *state;
|
---|
206 |
|
---|
207 | req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state);
|
---|
208 | if (req == NULL) {
|
---|
209 | return NULL;
|
---|
210 | }
|
---|
211 | state->ev = ev;
|
---|
212 | state->addr = addr;
|
---|
213 | state->sock = -1;
|
---|
214 | state->called_name =
|
---|
215 | (called_name != NULL) ? called_name : "*SMBSERVER";
|
---|
216 | state->called_type =
|
---|
217 | (called_type != -1) ? called_type : 0x20;
|
---|
218 | state->calling_name =
|
---|
219 | (calling_name != NULL) ? calling_name : global_myname();
|
---|
220 | state->calling_type =
|
---|
221 | (calling_type != -1) ? calling_type : 0x00;
|
---|
222 |
|
---|
223 | talloc_set_destructor(state, smbsock_connect_state_destructor);
|
---|
224 |
|
---|
225 | if (port == 139) {
|
---|
226 | subreq = tevent_wakeup_send(state, ev, timeval_set(0, 0));
|
---|
227 | if (tevent_req_nomem(subreq, req)) {
|
---|
228 | return tevent_req_post(req, ev);
|
---|
229 | }
|
---|
230 | tevent_req_set_callback(subreq, smbsock_connect_do_139, req);
|
---|
231 | return req;
|
---|
232 | }
|
---|
233 | if (port != 0) {
|
---|
234 | state->req_445 = open_socket_out_send(state, ev, addr, port,
|
---|
235 | 5000);
|
---|
236 | if (tevent_req_nomem(state->req_445, req)) {
|
---|
237 | return tevent_req_post(req, ev);
|
---|
238 | }
|
---|
239 | tevent_req_set_callback(
|
---|
240 | state->req_445, smbsock_connect_connected, req);
|
---|
241 | return req;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * port==0, try both
|
---|
246 | */
|
---|
247 |
|
---|
248 | state->req_445 = open_socket_out_send(state, ev, addr, 445, 5000);
|
---|
249 | if (tevent_req_nomem(state->req_445, req)) {
|
---|
250 | return tevent_req_post(req, ev);
|
---|
251 | }
|
---|
252 | tevent_req_set_callback(state->req_445, smbsock_connect_connected,
|
---|
253 | req);
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * After 5 msecs, fire the 139 request
|
---|
257 | */
|
---|
258 | state->req_139 = tevent_wakeup_send(
|
---|
259 | state, ev, timeval_current_ofs(0, 5000));
|
---|
260 | if (tevent_req_nomem(state->req_139, req)) {
|
---|
261 | TALLOC_FREE(state->req_445);
|
---|
262 | return tevent_req_post(req, ev);
|
---|
263 | }
|
---|
264 | tevent_req_set_callback(state->req_139, smbsock_connect_do_139,
|
---|
265 | req);
|
---|
266 | return req;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int smbsock_connect_state_destructor(
|
---|
270 | struct smbsock_connect_state *state)
|
---|
271 | {
|
---|
272 | if (state->sock != -1) {
|
---|
273 | close(state->sock);
|
---|
274 | state->sock = -1;
|
---|
275 | }
|
---|
276 | return 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static void smbsock_connect_do_139(struct tevent_req *subreq)
|
---|
280 | {
|
---|
281 | struct tevent_req *req = tevent_req_callback_data(
|
---|
282 | subreq, struct tevent_req);
|
---|
283 | struct smbsock_connect_state *state = tevent_req_data(
|
---|
284 | req, struct smbsock_connect_state);
|
---|
285 | bool ret;
|
---|
286 |
|
---|
287 | ret = tevent_wakeup_recv(subreq);
|
---|
288 | TALLOC_FREE(subreq);
|
---|
289 | if (!ret) {
|
---|
290 | tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
|
---|
291 | return;
|
---|
292 | }
|
---|
293 | state->req_139 = nb_connect_send(state, state->ev, state->addr,
|
---|
294 | state->called_name,
|
---|
295 | state->called_type,
|
---|
296 | state->calling_name,
|
---|
297 | state->calling_type);
|
---|
298 | if (tevent_req_nomem(state->req_139, req)) {
|
---|
299 | return;
|
---|
300 | }
|
---|
301 | tevent_req_set_callback(state->req_139, smbsock_connect_connected,
|
---|
302 | req);
|
---|
303 | }
|
---|
304 |
|
---|
305 | static void smbsock_connect_connected(struct tevent_req *subreq)
|
---|
306 | {
|
---|
307 | struct tevent_req *req = tevent_req_callback_data(
|
---|
308 | subreq, struct tevent_req);
|
---|
309 | struct smbsock_connect_state *state = tevent_req_data(
|
---|
310 | req, struct smbsock_connect_state);
|
---|
311 | struct tevent_req *unfinished_req;
|
---|
312 | NTSTATUS status;
|
---|
313 |
|
---|
314 | if (subreq == state->req_445) {
|
---|
315 |
|
---|
316 | status = open_socket_out_recv(subreq, &state->sock);
|
---|
317 | TALLOC_FREE(state->req_445);
|
---|
318 | unfinished_req = state->req_139;
|
---|
319 | state->port = 445;
|
---|
320 |
|
---|
321 | } else if (subreq == state->req_139) {
|
---|
322 |
|
---|
323 | status = nb_connect_recv(subreq, &state->sock);
|
---|
324 | TALLOC_FREE(state->req_139);
|
---|
325 | unfinished_req = state->req_445;
|
---|
326 | state->port = 139;
|
---|
327 |
|
---|
328 | } else {
|
---|
329 | tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
|
---|
330 | return;
|
---|
331 | }
|
---|
332 |
|
---|
333 | if (NT_STATUS_IS_OK(status)) {
|
---|
334 | TALLOC_FREE(unfinished_req);
|
---|
335 | state->req_139 = NULL;
|
---|
336 | state->req_445 = NULL;
|
---|
337 | tevent_req_done(req);
|
---|
338 | return;
|
---|
339 | }
|
---|
340 | if (unfinished_req == NULL) {
|
---|
341 | /*
|
---|
342 | * Both requests failed
|
---|
343 | */
|
---|
344 | tevent_req_nterror(req, status);
|
---|
345 | return;
|
---|
346 | }
|
---|
347 | /*
|
---|
348 | * Do nothing, wait for the second request to come here.
|
---|
349 | */
|
---|
350 | }
|
---|
351 |
|
---|
352 | NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
|
---|
353 | uint16_t *ret_port)
|
---|
354 | {
|
---|
355 | struct smbsock_connect_state *state = tevent_req_data(
|
---|
356 | req, struct smbsock_connect_state);
|
---|
357 | NTSTATUS status;
|
---|
358 |
|
---|
359 | if (tevent_req_is_nterror(req, &status)) {
|
---|
360 | return status;
|
---|
361 | }
|
---|
362 | *sock = state->sock;
|
---|
363 | state->sock = -1;
|
---|
364 | if (ret_port != NULL) {
|
---|
365 | *ret_port = state->port;
|
---|
366 | }
|
---|
367 | return NT_STATUS_OK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, uint16_t port,
|
---|
371 | const char *called_name, int called_type,
|
---|
372 | const char *calling_name, int calling_type,
|
---|
373 | int *pfd, uint16_t *ret_port, int sec_timeout)
|
---|
374 | {
|
---|
375 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
376 | struct event_context *ev;
|
---|
377 | struct tevent_req *req;
|
---|
378 | NTSTATUS status = NT_STATUS_NO_MEMORY;
|
---|
379 |
|
---|
380 | ev = event_context_init(frame);
|
---|
381 | if (ev == NULL) {
|
---|
382 | goto fail;
|
---|
383 | }
|
---|
384 | req = smbsock_connect_send(frame, ev, addr, port,
|
---|
385 | called_name, called_type,
|
---|
386 | calling_name, calling_type);
|
---|
387 | if (req == NULL) {
|
---|
388 | goto fail;
|
---|
389 | }
|
---|
390 | if ((sec_timeout != 0) &&
|
---|
391 | !tevent_req_set_endtime(
|
---|
392 | req, ev, timeval_current_ofs(sec_timeout, 0))) {
|
---|
393 | goto fail;
|
---|
394 | }
|
---|
395 | if (!tevent_req_poll_ntstatus(req, ev, &status)) {
|
---|
396 | goto fail;
|
---|
397 | }
|
---|
398 | status = smbsock_connect_recv(req, pfd, ret_port);
|
---|
399 | fail:
|
---|
400 | TALLOC_FREE(frame);
|
---|
401 | return status;
|
---|
402 | }
|
---|
403 |
|
---|
404 | struct smbsock_any_connect_state {
|
---|
405 | struct tevent_context *ev;
|
---|
406 | const struct sockaddr_storage *addrs;
|
---|
407 | const char **called_names;
|
---|
408 | int *called_types;
|
---|
409 | const char **calling_names;
|
---|
410 | int *calling_types;
|
---|
411 | size_t num_addrs;
|
---|
412 | uint16_t port;
|
---|
413 |
|
---|
414 | struct tevent_req **requests;
|
---|
415 | size_t num_sent;
|
---|
416 | size_t num_received;
|
---|
417 |
|
---|
418 | int fd;
|
---|
419 | uint16_t chosen_port;
|
---|
420 | size_t chosen_index;
|
---|
421 | };
|
---|
422 |
|
---|
423 | static bool smbsock_any_connect_send_next(
|
---|
424 | struct tevent_req *req, struct smbsock_any_connect_state *state);
|
---|
425 | static void smbsock_any_connect_trynext(struct tevent_req *subreq);
|
---|
426 | static void smbsock_any_connect_connected(struct tevent_req *subreq);
|
---|
427 |
|
---|
428 | struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
|
---|
429 | struct tevent_context *ev,
|
---|
430 | const struct sockaddr_storage *addrs,
|
---|
431 | const char **called_names,
|
---|
432 | int *called_types,
|
---|
433 | const char **calling_names,
|
---|
434 | int *calling_types,
|
---|
435 | size_t num_addrs, uint16_t port)
|
---|
436 | {
|
---|
437 | struct tevent_req *req, *subreq;
|
---|
438 | struct smbsock_any_connect_state *state;
|
---|
439 |
|
---|
440 | req = tevent_req_create(mem_ctx, &state,
|
---|
441 | struct smbsock_any_connect_state);
|
---|
442 | if (req == NULL) {
|
---|
443 | return NULL;
|
---|
444 | }
|
---|
445 | state->ev = ev;
|
---|
446 | state->addrs = addrs;
|
---|
447 | state->num_addrs = num_addrs;
|
---|
448 | state->called_names = called_names;
|
---|
449 | state->called_types = called_types;
|
---|
450 | state->calling_names = calling_names;
|
---|
451 | state->calling_types = calling_types;
|
---|
452 | state->port = port;
|
---|
453 |
|
---|
454 | if (num_addrs == 0) {
|
---|
455 | tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
|
---|
456 | return tevent_req_post(req, ev);
|
---|
457 | }
|
---|
458 |
|
---|
459 | state->requests = talloc_zero_array(state, struct tevent_req *,
|
---|
460 | num_addrs);
|
---|
461 | if (tevent_req_nomem(state->requests, req)) {
|
---|
462 | return tevent_req_post(req, ev);
|
---|
463 | }
|
---|
464 | if (!smbsock_any_connect_send_next(req, state)) {
|
---|
465 | return tevent_req_post(req, ev);
|
---|
466 | }
|
---|
467 | if (state->num_sent >= state->num_addrs) {
|
---|
468 | return req;
|
---|
469 | }
|
---|
470 | subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 10000));
|
---|
471 | if (tevent_req_nomem(subreq, req)) {
|
---|
472 | return tevent_req_post(req, ev);
|
---|
473 | }
|
---|
474 | tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
|
---|
475 | return req;
|
---|
476 | }
|
---|
477 |
|
---|
478 | static void smbsock_any_connect_trynext(struct tevent_req *subreq)
|
---|
479 | {
|
---|
480 | struct tevent_req *req = tevent_req_callback_data(
|
---|
481 | subreq, struct tevent_req);
|
---|
482 | struct smbsock_any_connect_state *state = tevent_req_data(
|
---|
483 | req, struct smbsock_any_connect_state);
|
---|
484 | bool ret;
|
---|
485 |
|
---|
486 | ret = tevent_wakeup_recv(subreq);
|
---|
487 | TALLOC_FREE(subreq);
|
---|
488 | if (!ret) {
|
---|
489 | tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
|
---|
490 | return;
|
---|
491 | }
|
---|
492 | if (!smbsock_any_connect_send_next(req, state)) {
|
---|
493 | return;
|
---|
494 | }
|
---|
495 | if (state->num_sent >= state->num_addrs) {
|
---|
496 | return;
|
---|
497 | }
|
---|
498 | subreq = tevent_wakeup_send(state, state->ev,
|
---|
499 | tevent_timeval_set(0, 10000));
|
---|
500 | if (tevent_req_nomem(subreq, req)) {
|
---|
501 | return;
|
---|
502 | }
|
---|
503 | tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
|
---|
504 | }
|
---|
505 |
|
---|
506 | static bool smbsock_any_connect_send_next(
|
---|
507 | struct tevent_req *req, struct smbsock_any_connect_state *state)
|
---|
508 | {
|
---|
509 | struct tevent_req *subreq;
|
---|
510 |
|
---|
511 | if (state->num_sent >= state->num_addrs) {
|
---|
512 | tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
|
---|
513 | return false;
|
---|
514 | }
|
---|
515 | subreq = smbsock_connect_send(
|
---|
516 | state->requests, state->ev, &state->addrs[state->num_sent],
|
---|
517 | state->port,
|
---|
518 | (state->called_names != NULL)
|
---|
519 | ? state->called_names[state->num_sent] : NULL,
|
---|
520 | (state->called_types != NULL)
|
---|
521 | ? state->called_types[state->num_sent] : -1,
|
---|
522 | (state->calling_names != NULL)
|
---|
523 | ? state->calling_names[state->num_sent] : NULL,
|
---|
524 | (state->calling_types != NULL)
|
---|
525 | ? state->calling_types[state->num_sent] : -1);
|
---|
526 | if (tevent_req_nomem(subreq, req)) {
|
---|
527 | return false;
|
---|
528 | }
|
---|
529 | tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
|
---|
530 |
|
---|
531 | state->requests[state->num_sent] = subreq;
|
---|
532 | state->num_sent += 1;
|
---|
533 |
|
---|
534 | return true;
|
---|
535 | }
|
---|
536 |
|
---|
537 | static void smbsock_any_connect_connected(struct tevent_req *subreq)
|
---|
538 | {
|
---|
539 | struct tevent_req *req = tevent_req_callback_data(
|
---|
540 | subreq, struct tevent_req);
|
---|
541 | struct smbsock_any_connect_state *state = tevent_req_data(
|
---|
542 | req, struct smbsock_any_connect_state);
|
---|
543 | NTSTATUS status;
|
---|
544 | int fd;
|
---|
545 | uint16_t chosen_port;
|
---|
546 | size_t i;
|
---|
547 | size_t chosen_index = 0;
|
---|
548 |
|
---|
549 | for (i=0; i<state->num_sent; i++) {
|
---|
550 | if (state->requests[i] == subreq) {
|
---|
551 | chosen_index = i;
|
---|
552 | break;
|
---|
553 | }
|
---|
554 | }
|
---|
555 | if (i == state->num_sent) {
|
---|
556 | tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
|
---|
557 | return;
|
---|
558 | }
|
---|
559 |
|
---|
560 | status = smbsock_connect_recv(subreq, &fd, &chosen_port);
|
---|
561 |
|
---|
562 | TALLOC_FREE(subreq);
|
---|
563 | state->requests[chosen_index] = NULL;
|
---|
564 |
|
---|
565 | if (NT_STATUS_IS_OK(status)) {
|
---|
566 | /*
|
---|
567 | * This will kill all the other requests
|
---|
568 | */
|
---|
569 | TALLOC_FREE(state->requests);
|
---|
570 | state->fd = fd;
|
---|
571 | state->chosen_port = chosen_port;
|
---|
572 | state->chosen_index = chosen_index;
|
---|
573 | tevent_req_done(req);
|
---|
574 | return;
|
---|
575 | }
|
---|
576 |
|
---|
577 | state->num_received += 1;
|
---|
578 | if (state->num_received <= state->num_addrs) {
|
---|
579 | /*
|
---|
580 | * More addrs pending, wait for the others
|
---|
581 | */
|
---|
582 | return;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * This is the last response, none succeeded.
|
---|
587 | */
|
---|
588 | tevent_req_nterror(req, status);
|
---|
589 | return;
|
---|
590 | }
|
---|
591 |
|
---|
592 | NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
|
---|
593 | size_t *chosen_index,
|
---|
594 | uint16_t *chosen_port)
|
---|
595 | {
|
---|
596 | struct smbsock_any_connect_state *state = tevent_req_data(
|
---|
597 | req, struct smbsock_any_connect_state);
|
---|
598 | NTSTATUS status;
|
---|
599 |
|
---|
600 | if (tevent_req_is_nterror(req, &status)) {
|
---|
601 | return status;
|
---|
602 | }
|
---|
603 | *pfd = state->fd;
|
---|
604 | if (chosen_index != NULL) {
|
---|
605 | *chosen_index = state->chosen_index;
|
---|
606 | }
|
---|
607 | if (chosen_port != NULL) {
|
---|
608 | *chosen_port = state->chosen_port;
|
---|
609 | }
|
---|
610 | return NT_STATUS_OK;
|
---|
611 | }
|
---|
612 |
|
---|
613 | NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
|
---|
614 | const char **called_names,
|
---|
615 | int *called_types,
|
---|
616 | const char **calling_names,
|
---|
617 | int *calling_types,
|
---|
618 | size_t num_addrs,
|
---|
619 | uint16_t port,
|
---|
620 | int sec_timeout,
|
---|
621 | int *pfd, size_t *chosen_index,
|
---|
622 | uint16_t *chosen_port)
|
---|
623 | {
|
---|
624 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
625 | struct event_context *ev;
|
---|
626 | struct tevent_req *req;
|
---|
627 | NTSTATUS status = NT_STATUS_NO_MEMORY;
|
---|
628 |
|
---|
629 | ev = event_context_init(frame);
|
---|
630 | if (ev == NULL) {
|
---|
631 | goto fail;
|
---|
632 | }
|
---|
633 | req = smbsock_any_connect_send(frame, ev, addrs,
|
---|
634 | called_names, called_types,
|
---|
635 | calling_names, calling_types,
|
---|
636 | num_addrs, port);
|
---|
637 | if (req == NULL) {
|
---|
638 | goto fail;
|
---|
639 | }
|
---|
640 | if ((sec_timeout != 0) &&
|
---|
641 | !tevent_req_set_endtime(
|
---|
642 | req, ev, timeval_current_ofs(sec_timeout, 0))) {
|
---|
643 | goto fail;
|
---|
644 | }
|
---|
645 | if (!tevent_req_poll_ntstatus(req, ev, &status)) {
|
---|
646 | goto fail;
|
---|
647 | }
|
---|
648 | status = smbsock_any_connect_recv(req, pfd, chosen_index, chosen_port);
|
---|
649 | fail:
|
---|
650 | TALLOC_FREE(frame);
|
---|
651 | return status;
|
---|
652 | }
|
---|