2 Unix SMB/CIFS implementation.
4 async getaddrinfo()/dns_lookup() name resolution module
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2008
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this module uses a fork() per getaddrinfo() or dns_looup() call.
25 At first that might seem crazy, but it is actually very fast,
26 and solves many of the tricky problems of keeping a child
27 hanging around in a librar (like what happens when the parent forks).
28 We use a talloc destructor to ensure that the child is cleaned up
29 when we have finished with this name resolution.
33 #include "lib/events/events.h"
34 #include "system/network.h"
35 #include "system/filesys.h"
36 #include "lib/socket/socket.h"
37 #include "libcli/composite/composite.h"
38 #include "librpc/gen_ndr/ndr_nbt.h"
39 #include "libcli/resolve/resolve.h"
40 #include "heimdal/lib/roken/resolve.h"
48 struct socket_address **addrs;
53 struct event_context *event_ctx;
57 kill off a wayward child if needed. This allows us to stop an async
58 name resolution without leaving a potentially blocking call running
61 static int dns_ex_destructor(struct dns_ex_state *state)
65 kill(state->child, SIGTERM);
66 close(state->child_fd);
67 if (waitpid(state->child, &status, WNOHANG) == 0) {
68 kill(state->child, SIGKILL);
69 waitpid(state->child, &status, 0);
78 static void run_child_dns_lookup(struct dns_ex_state *state, int fd)
80 struct dns_reply *reply;
81 struct resource_record *rr;
83 uint32_t srv_valid = 0;
84 struct resource_record **srv_rr;
85 uint32_t addrs_valid = 0;
86 struct resource_record **addrs_rr;
91 /* this is the blocking call we are going to lots of trouble
92 to avoid in the parent */
93 reply = dns_lookup(state->name.name, state->do_srv?"SRV":"A");
102 /* Loop over all returned records and pick the "srv" records */
103 for (rr=reply->head; rr; rr=rr->next) {
104 /* we are only interested in the IN class */
105 if (rr->class != C_IN) {
110 /* we are only interested in SRV records */
111 if (rr->type != T_SRV) {
115 /* verify we actually have a SRV record here */
120 /* Verify we got a port */
121 if (rr->u.srv->port == 0) {
125 /* we are only interested in A records */
126 /* TODO: add AAAA support */
127 if (rr->type != T_A) {
131 /* verify we actually have a A record here */
143 srv_rr = talloc_zero_array(state,
144 struct resource_record *,
150 addrs_rr = talloc_zero_array(state,
151 struct resource_record *,
157 /* Loop over all returned records and pick the records */
158 for (rr=reply->head;rr;rr=rr->next) {
159 /* we are only interested in the IN class */
160 if (rr->class != C_IN) {
165 /* we are only interested in SRV records */
166 if (rr->type != T_SRV) {
170 /* verify we actually have a srv record here */
175 /* Verify we got a port */
176 if (rr->u.srv->port == 0) {
180 srv_rr[srv_valid] = rr;
183 /* we are only interested in A records */
184 /* TODO: add AAAA support */
185 if (rr->type != T_A) {
189 /* verify we actually have a A record here */
194 addrs_rr[addrs_valid] = rr;
199 for (i=0; i < srv_valid; i++) {
200 for (rr=reply->head;rr;rr=rr->next) {
202 if (rr->class != C_IN) {
206 /* we are only interested in SRV records */
207 if (rr->type != T_A) {
211 /* verify we actually have a srv record here */
212 if (strcmp(&srv_rr[i]->u.srv->target[0], rr->domain) != 0) {
222 if (addrs_valid == 0) {
226 addrs = talloc_strdup(state, "");
231 for (i=0; i < count; i++) {
235 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
237 inet_ntoa(*addrs_rr[i]->u.a),
238 srv_rr[i]?srv_rr[i]->u.srv->port:0,
239 addrs_rr[i]->domain);
247 write(fd, addrs, talloc_get_size(addrs));
257 static void run_child_getaddrinfo(struct dns_ex_state *state, int fd)
260 struct addrinfo hints;
261 struct addrinfo *res;
262 struct addrinfo *res_list = NULL;
267 hints.ai_socktype = SOCK_STREAM;
268 hints.ai_family = AF_INET;/* TODO: add AF_INET6 support */
269 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
271 ret = getaddrinfo(state->name.name, "0", &hints, &res_list);
272 if (ret == EAI_NODATA && state->do_fallback) {
273 /* getaddrinfo() doesn't handle CNAME records */
274 run_child_dns_lookup(state, fd);
281 addrs = talloc_strdup(state, "");
286 for (res = res_list; res; res = res->ai_next) {
287 struct sockaddr_in *in;
289 if (res->ai_family != AF_INET) {
292 in = (struct sockaddr_in *)res->ai_addr;
294 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u/%s",
296 inet_ntoa(in->sin_addr),
297 0, state->name.name);
305 write(fd, addrs, talloc_get_size(addrs));
309 freeaddrinfo(res_list);
315 handle a read event on the pipe
317 static void pipe_handler(struct event_context *ev, struct fd_event *fde,
318 uint16_t flags, void *private_data)
320 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
321 struct dns_ex_state *state = talloc_get_type(c->private_data,
322 struct dns_ex_state);
324 uint32_t num_addrs, i;
330 /* if we get any event from the child then we know that we
331 won't need to kill it off */
332 talloc_set_destructor(state, NULL);
334 if (ioctl(state->child_fd, FIONREAD, &value) != 0) {
338 address = talloc_array(state, char, value+1);
340 /* yes, we don't care about EAGAIN or other niceities
341 here. They just can't happen with this parent/child
342 relationship, and even if they did then giving an error is
343 the right thing to do */
344 ret = read(state->child_fd, address, value);
348 close(state->child_fd);
349 if (waitpid(state->child, &status, WNOHANG) == 0) {
350 kill(state->child, SIGKILL);
351 waitpid(state->child, &status, 0);
355 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
359 /* enusre the address looks good */
362 addrs = str_list_make(state, address, ",");
363 if (composite_nomem(addrs, c)) return;
365 num_addrs = str_list_length((const char * const *)addrs);
367 state->addrs = talloc_array(state, struct socket_address *,
369 if (composite_nomem(state->addrs, c)) return;
371 state->names = talloc_array(state, char *, num_addrs+1);
372 if (composite_nomem(state->names, c)) return;
374 for (i=0; i < num_addrs; i++) {
376 char *p = strrchr(addrs[i], ':');
380 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
389 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
396 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
397 inet_addr(addrs[i]) == INADDR_NONE) {
398 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
401 port = strtoul(p, NULL, 10);
402 if (port > UINT16_MAX) {
405 state->addrs[i] = socket_address_from_strings(state->addrs,
409 if (composite_nomem(state->addrs[i], c)) return;
411 state->names[i] = talloc_strdup(state->names, n);
412 if (composite_nomem(state->names[i], c)) return;
414 state->addrs[i] = NULL;
415 state->names[i] = NULL;
421 getaddrinfo() or dns_lookup() name resolution method - async send
423 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
424 struct event_context *event_ctx,
427 struct nbt_name *name,
432 struct composite_context *c;
433 struct dns_ex_state *state;
434 int fd[2] = { -1, -1 };
437 c = composite_create(mem_ctx, event_ctx);
438 if (c == NULL) return NULL;
440 if (composite_nomem(c->event_ctx, c)) return c;
442 state = talloc_zero(c, struct dns_ex_state);
443 if (composite_nomem(state, c)) return c;
444 c->private_data = state;
446 c->status = nbt_name_dup(state, name, &state->name);
447 if (!composite_is_ok(c)) return c;
449 /* setup a pipe to chat to our child */
452 composite_error(c, map_nt_error_from_unix(errno));
456 state->do_getaddrinfo = do_getaddrinfo;
457 state->do_fallback = do_fallback;
458 state->do_srv = do_srv;
459 state->flags = flags;
461 state->child_fd = fd[0];
462 state->event_ctx = c->event_ctx;
464 /* we need to put the child in our event context so
465 we know when the dns_lookup() has finished */
466 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
468 if (composite_nomem(state->fde, c)) {
474 state->child = fork();
475 if (state->child == (pid_t)-1) {
476 composite_error(c, map_nt_error_from_unix(errno));
480 if (state->child == 0) {
482 if (state->do_getaddrinfo) {
483 run_child_getaddrinfo(state, fd[1]);
485 run_child_dns_lookup(state, fd[1]);
491 /* cleanup wayward children */
492 talloc_set_destructor(state, dns_ex_destructor);
498 getaddrinfo() or dns_lookup() name resolution method - recv side
500 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
502 struct socket_address ***addrs,
507 status = composite_wait(c);
509 if (NT_STATUS_IS_OK(status)) {
510 struct dns_ex_state *state = talloc_get_type(c->private_data,
511 struct dns_ex_state);
512 *addrs = talloc_steal(mem_ctx, state->addrs);
514 *names = talloc_steal(mem_ctx, state->names);