r12608: Remove some unused #include lines.
[bbaumbach/samba-autobuild/.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/composite/composite.h"
37
38 struct host_state {
39         struct nbt_name name;
40         const char *reply_addr;
41         pid_t child;
42         int child_fd;
43         struct fd_event *fde;
44         struct event_context *event_ctx;
45 };
46
47
48 /*
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
51   in a child
52 */
53 static int host_destructor(void *ptr)
54 {
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);
59         }
60         return 0;
61 }
62
63 /*
64   the blocking child
65 */
66 static void run_child(struct composite_context *c, int fd)
67 {
68         struct host_state *state = talloc_get_type(c->private_data, struct host_state);
69         struct ipv4_addr ip;
70         const char *address;
71
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);
75
76         address = sys_inet_ntoa(ip);
77         if (address != NULL) {
78                 write(fd, address, strlen(address)+1);
79         }
80 }
81
82 /*
83   handle a read event on the pipe
84 */
85 static void pipe_handler(struct event_context *ev, struct fd_event *fde, 
86                          uint16_t flags, void *private_data)
87 {
88         struct composite_context *c = talloc_get_type(private_data, struct composite_context);
89         struct host_state *state = talloc_get_type(c->private_data, struct host_state);
90         char address[128];
91         int ret;
92
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;
96
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;
103
104         /* enusre the address looks good */
105         address[ret] = 0;
106         if (strcmp(address, "0.0.0.0") == 0 ||
107             inet_addr(address) == INADDR_NONE) {
108                 goto failed;
109         }
110
111         state->reply_addr = talloc_strdup(state, address);
112         if (state->reply_addr == NULL) goto failed;
113
114         c->status = NT_STATUS_OK;
115         c->state = COMPOSITE_STATE_DONE;
116         if (c->async.fn) {
117                 c->async.fn(c);
118         }
119         return;
120
121 failed:
122         c->status = NT_STATUS_BAD_NETWORK_NAME;
123         c->state = COMPOSITE_STATE_ERROR;
124         if (c->async.fn) {
125                 c->async.fn(c);
126         }
127 }
128
129 /*
130   gethostbyname name resolution method - async send
131  */
132 struct composite_context *resolve_name_host_send(struct nbt_name *name, 
133                                                 struct event_context *event_ctx)
134 {
135         struct composite_context *c;
136         struct host_state *state;
137         NTSTATUS status;
138         int fd[2] = { -1, -1 };
139         int ret;
140
141         c = talloc_zero(NULL, struct composite_context);
142         if (c == NULL) goto failed;
143
144         state = talloc(c, struct host_state);
145         if (state == NULL) goto failed;
146
147         status = nbt_name_dup(state, name, &state->name);
148         if (!NT_STATUS_IS_OK(status)) goto failed;
149
150         c->state = COMPOSITE_STATE_IN_PROGRESS;
151         c->private_data = state;
152         c->event_ctx = talloc_reference(c, event_ctx);
153
154         /* setup a pipe to chat to our child */
155         ret = pipe(fd);
156         if (ret == -1) goto failed;
157
158         state->child_fd = fd[0];
159         state->event_ctx = c->event_ctx;
160
161         /* we need to put the child in our event context so
162            we know when the gethostbyname() has finished */
163         state->fde = event_add_fd(c->event_ctx, c, state->child_fd, EVENT_FD_READ, 
164                                   pipe_handler, c);
165         if (state->fde == NULL) {
166                 close(fd[0]);
167                 close(fd[1]);
168                 goto failed;
169         }
170
171         /* signal handling in posix really sucks - doing this in a library
172            affects the whole app, but what else to do?? */
173         signal(SIGCHLD, SIG_IGN);
174
175         state->child = fork();
176         if (state->child == (pid_t)-1) {
177                 goto failed;
178         }
179
180         if (state->child == 0) {
181                 close(fd[0]);
182                 run_child(c, fd[1]);
183                 _exit(0);
184         }
185         close(fd[1]);
186
187         /* cleanup wayward children */
188         talloc_set_destructor(state, host_destructor);
189
190         return c;       
191
192 failed:
193         talloc_free(c);
194         return NULL;
195 }
196
197 /*
198   gethostbyname name resolution method - recv side
199 */
200 NTSTATUS resolve_name_host_recv(struct composite_context *c, 
201                                  TALLOC_CTX *mem_ctx, const char **reply_addr)
202 {
203         NTSTATUS status;
204
205         status = composite_wait(c);
206
207         if (NT_STATUS_IS_OK(status)) {
208                 struct host_state *state = talloc_get_type(c->private_data, struct host_state);
209                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
210         }
211
212         talloc_free(c);
213         return status;
214 }
215
216 /*
217   gethostbyname name resolution method - sync call
218  */
219 NTSTATUS resolve_name_host(struct nbt_name *name, 
220                             TALLOC_CTX *mem_ctx,
221                             const char **reply_addr)
222 {
223         struct composite_context *c = resolve_name_host_send(name, NULL);
224         return resolve_name_host_recv(c, mem_ctx, reply_addr);
225 }
226