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