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"
47 struct socket_address **addrs;
51 struct event_context *event_ctx;
55 kill off a wayward child if needed. This allows us to stop an async
56 name resolution without leaving a potentially blocking call running
59 static int dns_ex_destructor(struct dns_ex_state *state)
63 kill(state->child, SIGTERM);
64 close(state->child_fd);
65 if (waitpid(state->child, &status, WNOHANG) == 0) {
66 kill(state->child, SIGKILL);
67 waitpid(state->child, &status, 0);
76 static void run_child_dns_lookup(struct dns_ex_state *state, int fd)
78 struct dns_reply *reply;
79 struct resource_record *rr;
81 uint32_t srv_valid = 0;
82 struct resource_record **srv_rr;
83 uint32_t addrs_valid = 0;
84 struct resource_record **addrs_rr;
89 /* this is the blocking call we are going to lots of trouble
90 to avoid in the parent */
91 reply = dns_lookup(state->name.name, state->do_srv?"SRV":"A");
100 /* Loop over all returned records and pick the "srv" records */
101 for (rr=reply->head; rr; rr=rr->next) {
102 /* we are only interested in the IN class */
103 if (rr->class != C_IN) {
108 /* we are only interested in SRV records */
109 if (rr->type != T_SRV) {
113 /* verify we actually have a SRV record here */
118 /* Verify we got a port */
119 if (rr->u.srv->port == 0) {
123 /* we are only interested in A records */
124 /* TODO: add AAAA support */
125 if (rr->type != T_A) {
129 /* verify we actually have a A record here */
141 srv_rr = talloc_zero_array(state,
142 struct resource_record *,
148 addrs_rr = talloc_zero_array(state,
149 struct resource_record *,
155 /* Loop over all returned records and pick the records */
156 for (rr=reply->head;rr;rr=rr->next) {
157 /* we are only interested in the IN class */
158 if (rr->class != C_IN) {
163 /* we are only interested in SRV records */
164 if (rr->type != T_SRV) {
168 /* verify we actually have a srv record here */
173 /* Verify we got a port */
174 if (rr->u.srv->port == 0) {
178 srv_rr[srv_valid] = rr;
181 /* we are only interested in A records */
182 /* TODO: add AAAA support */
183 if (rr->type != T_A) {
187 /* verify we actually have a A record here */
192 addrs_rr[addrs_valid] = rr;
197 for (i=0; i < srv_valid; i++) {
198 for (rr=reply->head;rr;rr=rr->next) {
200 if (rr->class != C_IN) {
204 /* we are only interested in SRV records */
205 if (rr->type != T_A) {
209 /* verify we actually have a srv record here */
210 if (strcmp(&srv_rr[i]->u.srv->target[0], rr->domain) != 0) {
220 if (addrs_valid == 0) {
224 addrs = talloc_strdup(state, "");
229 for (i=0; i < count; i++) {
233 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u",
235 inet_ntoa(*addrs_rr[i]->u.a),
236 srv_rr[i]?srv_rr[i]->u.srv->port:0);
244 write(fd, addrs, talloc_get_size(addrs));
254 static void run_child_getaddrinfo(struct dns_ex_state *state, int fd)
257 struct addrinfo hints;
258 struct addrinfo *res;
259 struct addrinfo *res_list = NULL;
264 hints.ai_socktype = SOCK_STREAM;
265 hints.ai_family = AF_INET;/* TODO: add AF_INET6 support */
266 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
268 ret = getaddrinfo(state->name.name, "0", &hints, &res_list);
269 if (ret == EAI_NODATA && state->do_fallback) {
270 /* getaddrinfo() doesn't handle CNAME records */
271 run_child_dns_lookup(state, fd);
278 addrs = talloc_strdup(state, "");
283 for (res = res_list; res; res = res->ai_next) {
284 struct sockaddr_in *in;
286 if (res->ai_family != AF_INET) {
289 in = (struct sockaddr_in *)res->ai_addr;
291 addrs = talloc_asprintf_append_buffer(addrs, "%s%s:%u",
293 inet_ntoa(in->sin_addr),
302 write(fd, addrs, talloc_get_size(addrs));
306 freeaddrinfo(res_list);
312 handle a read event on the pipe
314 static void pipe_handler(struct event_context *ev, struct fd_event *fde,
315 uint16_t flags, void *private_data)
317 struct composite_context *c = talloc_get_type(private_data, struct composite_context);
318 struct dns_ex_state *state = talloc_get_type(c->private_data,
319 struct dns_ex_state);
321 uint32_t num_addrs, i;
326 /* if we get any event from the child then we know that we
327 won't need to kill it off */
328 talloc_set_destructor(state, NULL);
330 /* yes, we don't care about EAGAIN or other niceities
331 here. They just can't happen with this parent/child
332 relationship, and even if they did then giving an error is
333 the right thing to do */
334 ret = read(state->child_fd, address, sizeof(address)-1);
335 close(state->child_fd);
336 if (waitpid(state->child, &status, WNOHANG) == 0) {
337 kill(state->child, SIGKILL);
338 waitpid(state->child, &status, 0);
342 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
346 /* enusre the address looks good */
349 addrs = str_list_make(state, address, ",");
350 if (composite_nomem(addrs, c)) return;
352 num_addrs = str_list_length((const char * const *)addrs);
354 state->addrs = talloc_array(state, struct socket_address *,
356 if (composite_nomem(state->addrs, c)) return;
358 for (i=0; i < num_addrs; i++) {
360 char *p = strrchr(addrs[i], ':');
363 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
370 if (strcmp(addrs[i], "0.0.0.0") == 0 ||
371 inet_addr(addrs[i]) == INADDR_NONE) {
372 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
375 port = strtoul(p, NULL, 10);
376 if (port > UINT16_MAX) {
379 state->addrs[i] = socket_address_from_strings(state->addrs,
383 if (composite_nomem(state->addrs[i], c)) return;
385 state->addrs[i] = NULL;
391 getaddrinfo() or dns_lookup() name resolution method - async send
393 struct composite_context *resolve_name_dns_ex_send(TALLOC_CTX *mem_ctx,
394 struct event_context *event_ctx,
396 struct nbt_name *name,
401 struct composite_context *c;
402 struct dns_ex_state *state;
403 int fd[2] = { -1, -1 };
406 c = composite_create(mem_ctx, event_ctx);
407 if (c == NULL) return NULL;
409 if (composite_nomem(c->event_ctx, c)) return c;
411 state = talloc_zero(c, struct dns_ex_state);
412 if (composite_nomem(state, c)) return c;
413 c->private_data = state;
415 c->status = nbt_name_dup(state, name, &state->name);
416 if (!composite_is_ok(c)) return c;
418 /* setup a pipe to chat to our child */
421 composite_error(c, map_nt_error_from_unix(errno));
425 state->do_getaddrinfo = do_getaddrinfo;
426 state->do_fallback = do_fallback;
427 state->do_srv = do_srv;
429 state->child_fd = fd[0];
430 state->event_ctx = c->event_ctx;
432 /* we need to put the child in our event context so
433 we know when the dns_lookup() has finished */
434 state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ,
436 if (composite_nomem(state->fde, c)) {
442 state->child = fork();
443 if (state->child == (pid_t)-1) {
444 composite_error(c, map_nt_error_from_unix(errno));
448 if (state->child == 0) {
450 if (state->do_getaddrinfo) {
451 run_child_getaddrinfo(state, fd[1]);
453 run_child_dns_lookup(state, fd[1]);
459 /* cleanup wayward children */
460 talloc_set_destructor(state, dns_ex_destructor);
466 getaddrinfo() or dns_lookup() name resolution method - recv side
468 NTSTATUS resolve_name_dns_ex_recv(struct composite_context *c,
470 struct socket_address ***addrs)
474 status = composite_wait(c);
476 if (NT_STATUS_IS_OK(status)) {
477 struct dns_ex_state *state = talloc_get_type(c->private_data,
478 struct dns_ex_state);
479 *addrs = talloc_steal(mem_ctx, state->addrs);