source: branches/samba-3.0/source/nmbd/asyncdns.c

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 10.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 a async DNS handler
4 Copyright (C) Andrew Tridgell 1997-1998
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include "includes.h"
22
23/***************************************************************************
24 Add a DNS result to the name cache.
25****************************************************************************/
26
27static struct name_record *add_dns_result(struct nmb_name *question, struct in_addr addr)
28{
29 int name_type = question->name_type;
30 unstring qname;
31
32 pull_ascii_nstring(qname, sizeof(qname), question->name);
33
34 if (!addr.s_addr) {
35 /* add the fail to WINS cache of names. give it 1 hour in the cache */
36 DEBUG(3,("add_dns_result: Negative DNS answer for %s\n", qname));
37 add_name_to_subnet( wins_server_subnet, qname, name_type,
38 NB_ACTIVE, 60*60, DNSFAIL_NAME, 1, &addr );
39 return NULL;
40 }
41
42 /* add it to our WINS cache of names. give it 2 hours in the cache */
43 DEBUG(3,("add_dns_result: DNS gave answer for %s of %s\n", qname, inet_ntoa(addr)));
44
45 add_name_to_subnet( wins_server_subnet, qname, name_type,
46 NB_ACTIVE, 2*60*60, DNS_NAME, 1, &addr);
47
48 return find_name_on_subnet(wins_server_subnet, question, FIND_ANY_NAME);
49}
50
51#ifndef SYNC_DNS
52
53static int fd_in = -1, fd_out = -1;
54static pid_t child_pid = -1;
55static int in_dns;
56
57/* this is the structure that is passed between the parent and child */
58struct query_record {
59 struct nmb_name name;
60 struct in_addr result;
61};
62
63/* a queue of pending requests waiting to be sent to the DNS child */
64static struct packet_struct *dns_queue;
65
66/* the packet currently being processed by the dns child */
67static struct packet_struct *dns_current;
68
69
70/***************************************************************************
71 return the fd used to gather async dns replies. This is added to the select
72 loop
73 ****************************************************************************/
74
75int asyncdns_fd(void)
76{
77 return fd_in;
78}
79
80/***************************************************************************
81 handle DNS queries arriving from the parent
82 ****************************************************************************/
83static void asyncdns_process(void)
84{
85 struct query_record r;
86 unstring qname;
87
88 DEBUGLEVEL = -1;
89
90 while (1) {
91 if (read_data(fd_in, (char *)&r, sizeof(r)) != sizeof(r))
92 break;
93
94 pull_ascii_nstring( qname, sizeof(qname), r.name.name);
95 r.result.s_addr = interpret_addr(qname);
96
97 if (write_data(fd_out, (char *)&r, sizeof(r)) != sizeof(r))
98 break;
99 }
100
101 _exit(0);
102}
103
104/**************************************************************************** **
105 catch a sigterm (in the child process - the parent has a different handler
106 see nmbd.c for details).
107 We need a separate term handler here so we don't release any
108 names that our parent is going to release, or overwrite a
109 WINS db that our parent is going to write.
110 **************************************************************************** */
111
112static void sig_term(int sig)
113{
114 _exit(0);
115}
116
117/***************************************************************************
118 Called by the parent process when it receives a SIGTERM - also kills the
119 child so we don't get child async dns processes lying around, causing trouble.
120 ****************************************************************************/
121
122void kill_async_dns_child(void)
123{
124 if (child_pid > 0) {
125 kill(child_pid, SIGTERM);
126 child_pid = -1;
127 }
128}
129
130/***************************************************************************
131 create a child process to handle DNS lookups
132 ****************************************************************************/
133void start_async_dns(void)
134{
135 int fd1[2], fd2[2];
136
137 CatchChild();
138
139#ifdef __OS2__
140// cant use pipes here, because select() from innotek lib does work only for sockets.
141 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd1) < 0)
142 {
143 DEBUG(0,("can't create asyncdns socketpair1 %d\n", errno));
144 return;
145 }
146 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd2) < 0)
147 {
148 DEBUG(0,("can't create asyncdns socketpair2 %d\n", errno));
149 close(fd1[0]);
150 close(fd1[1]);
151 return;
152 }
153DEBUG(9,("!!!!!!PIPES %d %d %d %d\n", fd1[0], fd1[1], fd2[0], fd2[1]));
154#else
155 if (pipe(fd1) || pipe(fd2)) {
156 DEBUG(0,("can't create asyncdns pipes\n"));
157 return;
158 }
159#endif /* __OS2__ */
160 child_pid = sys_fork();
161
162 if (child_pid) {
163 fd_in = fd1[0];
164 fd_out = fd2[1];
165 close(fd1[1]);
166 close(fd2[0]);
167 DEBUG(0,("started asyncdns process %d\n", (int)child_pid));
168 return;
169 }
170
171 fd_in = fd2[0];
172 fd_out = fd1[1];
173
174 CatchSignal(SIGUSR2, SIG_IGN);
175 CatchSignal(SIGUSR1, SIG_IGN);
176 CatchSignal(SIGHUP, SIG_IGN);
177 CatchSignal(SIGTERM, SIGNAL_CAST sig_term );
178
179 asyncdns_process();
180}
181
182
183/***************************************************************************
184check if a particular name is already being queried
185 ****************************************************************************/
186static BOOL query_current(struct query_record *r)
187{
188 return dns_current &&
189 nmb_name_equal(&r->name,
190 &dns_current->packet.nmb.question.question_name);
191}
192
193
194/***************************************************************************
195 write a query to the child process
196 ****************************************************************************/
197static BOOL write_child(struct packet_struct *p)
198{
199 struct query_record r;
200
201 r.name = p->packet.nmb.question.question_name;
202
203 return write_data(fd_out, (char *)&r, sizeof(r)) == sizeof(r);
204}
205
206/***************************************************************************
207 check the DNS queue
208 ****************************************************************************/
209void run_dns_queue(void)
210{
211 struct query_record r;
212 struct packet_struct *p, *p2;
213 struct name_record *namerec;
214 int size;
215
216 if (fd_in == -1)
217 return;
218
219 /* Allow SIGTERM to kill us. */
220 BlockSignals(False, SIGTERM);
221
222 if (!process_exists_by_pid(child_pid)) {
223 close(fd_in);
224 close(fd_out);
225 start_async_dns();
226 }
227
228 if ((size=read_data(fd_in, (char *)&r, sizeof(r))) != sizeof(r)) {
229 if (size) {
230 DEBUG(0,("Incomplete DNS answer from child!\n"));
231 fd_in = -1;
232 }
233 BlockSignals(True, SIGTERM);
234 return;
235 }
236
237 BlockSignals(True, SIGTERM);
238
239 namerec = add_dns_result(&r.name, r.result);
240
241 if (dns_current) {
242 if (query_current(&r)) {
243 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
244 in_dns = 1;
245 if(namerec == NULL)
246 send_wins_name_query_response(NAM_ERR, dns_current, NULL);
247 else
248 send_wins_name_query_response(0,dns_current,namerec);
249 in_dns = 0;
250 }
251
252 dns_current->locked = False;
253 free_packet(dns_current);
254 dns_current = NULL;
255 }
256
257 /* loop over the whole dns queue looking for entries that
258 match the result we just got */
259 for (p = dns_queue; p;) {
260 struct nmb_packet *nmb = &p->packet.nmb;
261 struct nmb_name *question = &nmb->question.question_name;
262
263 if (nmb_name_equal(question, &r.name)) {
264 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
265 in_dns = 1;
266 if(namerec == NULL)
267 send_wins_name_query_response(NAM_ERR, p, NULL);
268 else
269 send_wins_name_query_response(0,p,namerec);
270 in_dns = 0;
271 p->locked = False;
272
273 if (p->prev)
274 p->prev->next = p->next;
275 else
276 dns_queue = p->next;
277 if (p->next)
278 p->next->prev = p->prev;
279 p2 = p->next;
280 free_packet(p);
281 p = p2;
282 } else {
283 p = p->next;
284 }
285 }
286
287 if (dns_queue) {
288 dns_current = dns_queue;
289 dns_queue = dns_queue->next;
290 if (dns_queue)
291 dns_queue->prev = NULL;
292 dns_current->next = NULL;
293
294 if (!write_child(dns_current)) {
295 DEBUG(3,("failed to send DNS query to child!\n"));
296 return;
297 }
298 }
299}
300
301/***************************************************************************
302queue a DNS query
303 ****************************************************************************/
304
305BOOL queue_dns_query(struct packet_struct *p,struct nmb_name *question)
306{
307 if (in_dns || fd_in == -1)
308 return False;
309
310 if (!dns_current) {
311 if (!write_child(p)) {
312 DEBUG(3,("failed to send DNS query to child!\n"));
313 return False;
314 }
315 dns_current = p;
316 p->locked = True;
317 } else {
318 p->locked = True;
319 p->next = dns_queue;
320 p->prev = NULL;
321 if (p->next)
322 p->next->prev = p;
323 dns_queue = p;
324 }
325
326 DEBUG(3,("added DNS query for %s\n", nmb_namestr(question)));
327 return True;
328}
329
330#else
331
332
333/***************************************************************************
334 we use this when we can't do async DNS lookups
335 ****************************************************************************/
336
337BOOL queue_dns_query(struct packet_struct *p,struct nmb_name *question)
338{
339 struct name_record *namerec = NULL;
340 struct in_addr dns_ip;
341 unstring qname;
342
343 pull_ascii_nstring(qname, sizeof(qname), question->name);
344
345 DEBUG(3,("DNS search for %s - ", nmb_namestr(question)));
346
347 /* Unblock TERM signal so we can be killed in DNS lookup. */
348 BlockSignals(False, SIGTERM);
349
350 dns_ip.s_addr = interpret_addr(qname);
351
352 /* Re-block TERM signal. */
353 BlockSignals(True, SIGTERM);
354
355 namerec = add_dns_result(question, dns_ip);
356 if(namerec == NULL) {
357 send_wins_name_query_response(NAM_ERR, p, NULL);
358 } else {
359 send_wins_name_query_response(0, p, namerec);
360 }
361 return False;
362}
363
364/***************************************************************************
365 With sync dns there is no child to kill on SIGTERM.
366 ****************************************************************************/
367
368void kill_async_dns_child(void)
369{
370 return;
371}
372#endif
Note: See TracBrowser for help on using the repository browser.