2 Unix SMB/CIFS implementation.
4 async gethostbyname() name resolution module
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 this module uses a fork() per gethostbyname() call. At first that
25 might seem crazy, but it is actually very fast, and solves many of
26 the tricky problems of keeping a child hanging around in a library
27 (like what happens when the parent forks). We use a talloc
28 destructor to ensure that the child is cleaned up when we have
29 finished with this name resolution.
34 #include "system/network.h"
35 #include "libcli/raw/libcliraw.h"
36 #include "libcli/composite/composite.h"
40 const char *reply_addr;
44 struct event_context *event_ctx;
49 kill off a wayward child if needed. This allows us to stop an async
50 name resolution without leaving a potentially blocking call running
53 static int host_destructor(void *ptr)
55 struct host_state *state = talloc_get_type(ptr, struct host_state);
56 close(state->child_fd);
57 if (state->child != (pid_t)-1) {
58 kill(state->child, SIGTERM);
66 static void run_child(struct composite_context *c, int fd)
68 struct host_state *state = talloc_get_type(c->private, struct host_state);
72 /* this is the blocking call we are going to lots of trouble
73 to avoid in the parent */
74 ip = interpret_addr2(state->name.name);
76 address = sys_inet_ntoa(ip);
77 if (address != NULL) {
78 write(fd, address, strlen(address)+1);
83 handle a read event on the pipe
85 static void pipe_handler(struct event_context *ev, struct fd_event *fde,
86 struct timeval t, uint16_t flags)
88 struct composite_context *c = talloc_get_type(fde->private, struct composite_context);
89 struct host_state *state = talloc_get_type(c->private, struct host_state);
93 /* if we get any event from the child then we know that we
94 won't need to kill it off */
95 state->child = (pid_t)-1;
97 /* yes, we don't care about EAGAIN or other niceities
98 here. They just can't happen with this parent/child
99 relationship, and even if they did then giving an error is
100 the right thing to do */
101 ret = read(state->child_fd, address, sizeof(address)-1);
102 if (ret <= 0) goto failed;
104 /* enusre the address looks good */
106 if (strcmp(address, "0.0.0.0") == 0 ||
107 sys_inet_addr(address) == INADDR_NONE) {
111 state->reply_addr = talloc_strdup(state, address);
112 if (state->reply_addr == NULL) goto failed;
114 c->status = NT_STATUS_OK;
115 c->state = SMBCLI_REQUEST_DONE;
122 c->status = NT_STATUS_BAD_NETWORK_NAME;
123 c->state = SMBCLI_REQUEST_ERROR;
130 gethostbyname name resolution method - async send
132 struct composite_context *resolve_name_host_send(struct nbt_name *name,
133 struct event_context *event_ctx)
135 struct composite_context *c;
136 struct host_state *state;
138 int fd[2] = { -1, -1 };
142 c = talloc_zero(NULL, struct composite_context);
143 if (c == NULL) goto failed;
145 state = talloc(c, struct host_state);
146 if (state == NULL) goto failed;
148 status = nbt_name_dup(state, name, &state->name);
149 if (!NT_STATUS_IS_OK(status)) goto failed;
151 c->state = SMBCLI_REQUEST_SEND;
153 c->event_ctx = talloc_reference(c, event_ctx);
155 /* setup a pipe to chat to our child */
157 if (ret == -1) goto failed;
159 state->child_fd = fd[0];
160 state->event_ctx = c->event_ctx;
162 /* we need to put the child in our event context so
163 we know when the gethostbyname() has finished */
164 fde.fd = state->child_fd;
165 fde.flags = EVENT_FD_READ;
166 fde.handler = pipe_handler;
168 state->fde = event_add_fd(c->event_ctx, &fde, state);
169 if (state->fde == NULL) {
175 /* signal handling in posix really sucks - doing this in a library
176 affects the whole app, but what else to do?? */
177 signal(SIGCHLD, SIG_IGN);
179 state->child = fork();
180 if (state->child == (pid_t)-1) {
184 if (state->child == 0) {
191 /* cleanup wayward children */
192 talloc_set_destructor(state, host_destructor);
202 gethostbyname name resolution method - recv side
204 NTSTATUS resolve_name_host_recv(struct composite_context *c,
205 TALLOC_CTX *mem_ctx, const char **reply_addr)
209 status = composite_wait(c);
211 if (NT_STATUS_IS_OK(status)) {
212 struct host_state *state = talloc_get_type(c->private, struct host_state);
213 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
221 gethostbyname name resolution method - sync call
223 NTSTATUS resolve_name_host(struct nbt_name *name,
225 const char **reply_addr)
227 struct composite_context *c = resolve_name_host_send(name, NULL);
228 return resolve_name_host_recv(c, mem_ctx, reply_addr);