r12542: Move some more prototypes out to seperate headers
[abartlet/samba.git/.git] / source4 / libcli / resolve / host.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    async gethostbyname() name resolution module
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 /*
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.
30 */
31
32 #include "includes.h"
33 #include "lib/events/events.h"
34 #include "system/network.h"
35 #include "system/filesys.h"
36 #include "libcli/raw/libcliraw.h"
37 #include "libcli/composite/composite.h"
38 #include "libcli/nbt/libnbt.h"
39
40 struct host_state {
41         struct nbt_name name;
42         const char *reply_addr;
43         pid_t child;
44         int child_fd;
45         struct fd_event *fde;
46         struct event_context *event_ctx;
47 };
48
49
50 /*
51   kill off a wayward child if needed. This allows us to stop an async
52   name resolution without leaving a potentially blocking call running
53   in a child
54 */
55 static int host_destructor(void *ptr)
56 {
57         struct host_state *state = talloc_get_type(ptr, struct host_state);
58         close(state->child_fd);
59         if (state->child != (pid_t)-1) {
60                 kill(state->child, SIGTERM);
61         }
62         return 0;
63 }
64
65 /*
66   the blocking child
67 */
68 static void run_child(struct composite_context *c, int fd)
69 {
70         struct host_state *state = talloc_get_type(c->private_data, struct host_state);
71         struct ipv4_addr ip;
72         const char *address;
73
74         /* this is the blocking call we are going to lots of trouble
75            to avoid in the parent */
76         ip = interpret_addr2(state->name.name);
77
78         address = sys_inet_ntoa(ip);
79         if (address != NULL) {
80                 write(fd, address, strlen(address)+1);
81         }
82 }
83
84 /*
85   handle a read event on the pipe
86 */
87 static void pipe_handler(struct event_context *ev, struct fd_event *fde, 
88                          uint16_t flags, void *private_data)
89 {
90         struct composite_context *c = talloc_get_type(private_data, struct composite_context);
91         struct host_state *state = talloc_get_type(c->private_data, struct host_state);
92         char address[128];
93         int ret;
94
95         /* if we get any event from the child then we know that we
96            won't need to kill it off */
97         state->child = (pid_t)-1;
98
99         /* yes, we don't care about EAGAIN or other niceities
100            here. They just can't happen with this parent/child
101            relationship, and even if they did then giving an error is
102            the right thing to do */
103         ret = read(state->child_fd, address, sizeof(address)-1);
104         if (ret <= 0) goto failed;
105
106         /* enusre the address looks good */
107         address[ret] = 0;
108         if (strcmp(address, "0.0.0.0") == 0 ||
109             inet_addr(address) == INADDR_NONE) {
110                 goto failed;
111         }
112
113         state->reply_addr = talloc_strdup(state, address);
114         if (state->reply_addr == NULL) goto failed;
115
116         c->status = NT_STATUS_OK;
117         c->state = COMPOSITE_STATE_DONE;
118         if (c->async.fn) {
119                 c->async.fn(c);
120         }
121         return;
122
123 failed:
124         c->status = NT_STATUS_BAD_NETWORK_NAME;
125         c->state = COMPOSITE_STATE_ERROR;
126         if (c->async.fn) {
127                 c->async.fn(c);
128         }
129 }
130
131 /*
132   gethostbyname name resolution method - async send
133  */
134 struct composite_context *resolve_name_host_send(struct nbt_name *name, 
135                                                 struct event_context *event_ctx)
136 {
137         struct composite_context *c;
138         struct host_state *state;
139         NTSTATUS status;
140         int fd[2] = { -1, -1 };
141         int ret;
142
143         c = talloc_zero(NULL, struct composite_context);
144         if (c == NULL) goto failed;
145
146         state = talloc(c, struct host_state);
147         if (state == NULL) goto failed;
148
149         status = nbt_name_dup(state, name, &state->name);
150         if (!NT_STATUS_IS_OK(status)) goto failed;
151
152         c->state = COMPOSITE_STATE_IN_PROGRESS;
153         c->private_data = state;
154         c->event_ctx = talloc_reference(c, event_ctx);
155
156         /* setup a pipe to chat to our child */
157         ret = pipe(fd);
158         if (ret == -1) goto failed;
159
160         state->child_fd = fd[0];
161         state->event_ctx = c->event_ctx;
162
163         /* we need to put the child in our event context so
164            we know when the gethostbyname() has finished */
165         state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ, 
166                                   pipe_handler, c);
167         if (state->fde == NULL) {
168                 close(fd[0]);
169                 close(fd[1]);
170                 goto failed;
171         }
172
173         /* signal handling in posix really sucks - doing this in a library
174            affects the whole app, but what else to do?? */
175         signal(SIGCHLD, SIG_IGN);
176
177         state->child = fork();
178         if (state->child == (pid_t)-1) {
179                 goto failed;
180         }
181
182         if (state->child == 0) {
183                 close(fd[0]);
184                 run_child(c, fd[1]);
185                 _exit(0);
186         }
187         close(fd[1]);
188
189         /* cleanup wayward children */
190         talloc_set_destructor(state, host_destructor);
191
192         return c;       
193
194 failed:
195         talloc_free(c);
196         return NULL;
197 }
198
199 /*
200   gethostbyname name resolution method - recv side
201 */
202 NTSTATUS resolve_name_host_recv(struct composite_context *c, 
203                                  TALLOC_CTX *mem_ctx, const char **reply_addr)
204 {
205         NTSTATUS status;
206
207         status = composite_wait(c);
208
209         if (NT_STATUS_IS_OK(status)) {
210                 struct host_state *state = talloc_get_type(c->private_data, struct host_state);
211                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
212         }
213
214         talloc_free(c);
215         return status;
216 }
217
218 /*
219   gethostbyname name resolution method - sync call
220  */
221 NTSTATUS resolve_name_host(struct nbt_name *name, 
222                             TALLOC_CTX *mem_ctx,
223                             const char **reply_addr)
224 {
225         struct composite_context *c = resolve_name_host_send(name, NULL);
226         return resolve_name_host_recv(c, mem_ctx, reply_addr);
227 }
228