ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/res.c
Revision: 8384
Committed: Fri Mar 16 20:06:38 2018 UTC (6 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 19575 byte(s)
Log Message:
- Rip out mempool

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3     *
4 michael 8280 * Copyright (c) 1997-2018 ircd-hybrid development team
5 michael 2916 *
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 michael 4564 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 michael 2916 * USA
20     */
21    
22 michael 3322 /*! \file res.c
23 michael 2916 * \brief ircd resolver functions
24     * \version $Id$
25     */
26    
27     /*
28 michael 4312 * A rewrite of Darren Reed's original res.c As there is nothing
29     * left of Darren's original code, this is now licensed by the hybrid group.
30 adx 30 * (Well, some of the function names are the same, and bits of the structs..)
31     * You can use it where it is useful, free even. Buy us a beer and stuff.
32     *
33     * The authors takes no responsibility for any damage or loss
34     * of property which results from the use of this software.
35     *
36     * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
37     * added callbacks and reference counting of returned hostents.
38     * --Bleep (Thomas Helvey <tomh@inxpress.net>)
39     *
40     * This was all needlessly complicated for irc. Simplified. No more hostent
41 michael 4312 * All we really care about is the IP -> hostname mappings. That's all.
42 adx 30 *
43     * Apr 28, 2003 --cryogen and Dianora
44     */
45    
46     #include "stdinc.h"
47 michael 1011 #include "list.h"
48 adx 30 #include "event.h"
49     #include "irc_string.h"
50     #include "ircd.h"
51 michael 982 #include "rng_mt.h"
52 adx 30 #include "fdlist.h"
53     #include "s_bsd.h"
54 michael 3347 #include "misc.h"
55 michael 3322 #include "res.h"
56     #include "reslib.h"
57 michael 8384 #include "memory.h"
58 adx 30
59     #if (CHAR_BIT != 8)
60 michael 2916 #error this code needs to be able to address individual octets
61 adx 30 #endif
62    
63 michael 4462 static void res_readreply(fde_t *, void *);
64 adx 30
65 michael 4312 #define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */
66     #define AR_TTL 600 /**< TTL in seconds for dns cache entries */
67 adx 30
68 michael 4312 /*
69     * RFC 1104/1105 wasn't very helpful about what these fields
70 adx 30 * should be named, so for now, we'll just name them this way.
71 michael 4312 * We probably should look at what named calls them or something.
72 adx 30 */
73     #define TYPE_SIZE (size_t)2
74     #define CLASS_SIZE (size_t)2
75     #define TTL_SIZE (size_t)4
76     #define RDLENGTH_SIZE (size_t)2
77     #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
78    
79 michael 2916 struct reslist
80 adx 30 {
81 michael 4422 dlink_node node; /**< Doubly linked list node. */
82 michael 7594 unsigned int id; /**< Request ID (from request header). */
83 michael 4422 char type; /**< Current request type. */
84     char retries; /**< Retry counter */
85     unsigned int sends; /**< Number of sends (>1 means resent). */
86 michael 7329 uintmax_t sentat; /**< Timestamp we last sent this request. */
87     uintmax_t timeout; /**< When this request times out. */
88 michael 4422 struct irc_ssaddr addr; /**< Address for this request. */
89     char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */
90     size_t namelength; /**< Actual hostname length. */
91     dns_callback_fnc callback; /**< Callback function on completion. */
92     void *callback_ctx; /**< Context pointer for callback. */
93 adx 30 };
94    
95 michael 8338 static fde_t *ResolverFileDescriptor;
96 michael 3235 static dlink_list request_list;
97 adx 30
98    
99 michael 4312 /*
100     * rem_request - remove a request from the list.
101     * This must also free any memory that has been allocated for
102     * temporary storage of DNS results.
103     */
104     static void
105     rem_request(struct reslist *request)
106     {
107     dlinkDelete(&request->node, &request_list);
108 michael 8384 xfree(request);
109 michael 4312 }
110    
111 adx 30 /*
112 michael 4312 * make_request - Create a DNS request record for the server.
113     */
114     static struct reslist *
115     make_request(dns_callback_fnc callback, void *ctx)
116     {
117 michael 8384 struct reslist *request = xcalloc(sizeof(*request));
118 michael 4312
119 michael 7596 request->sentat = CurrentTime;
120     request->retries = 2;
121     request->timeout = 4; /* Start at 4 and exponential inc. */
122     request->callback = callback;
123 michael 4312 request->callback_ctx = ctx;
124    
125     dlinkAdd(request, &request->node, &request_list);
126     return request;
127     }
128    
129     /*
130 adx 30 * int
131     * res_ourserver(inp)
132     * looks up "inp" in irc_nsaddr_list[]
133     * returns:
134     * 0 : not found
135     * >0 : found
136     * author:
137     * paul vixie, 29may94
138     * revised for ircd, cryogen(stu) may03
139     */
140     static int
141 michael 2916 res_ourserver(const struct irc_ssaddr *inp)
142 adx 30 {
143 michael 992 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
144 michael 2916 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
145 adx 30
146 michael 3297 for (unsigned int i = 0; i < irc_nscount; ++i)
147 adx 30 {
148 michael 3235 const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
149 michael 4475 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)srv;
150     const struct sockaddr_in *v4 = (const struct sockaddr_in *)srv;
151 adx 30
152 michael 4312 /*
153     * Could probably just memcmp(srv, inp, srv.ss_len) here
154 adx 30 * but we'll air on the side of caution - stu
155     */
156     switch (srv->ss.ss_family)
157     {
158     case AF_INET6:
159     if (srv->ss.ss_family == inp->ss.ss_family)
160     if (v6->sin6_port == v6in->sin6_port)
161 michael 1346 if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
162     sizeof(struct in6_addr)))
163 michael 1124 return 1;
164 adx 30 break;
165     case AF_INET:
166     if (srv->ss.ss_family == inp->ss.ss_family)
167     if (v4->sin_port == v4in->sin_port)
168 michael 1346 if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
169 michael 1124 return 1;
170 adx 30 break;
171     default:
172     break;
173     }
174     }
175    
176 michael 1124 return 0;
177 adx 30 }
178    
179     /*
180     * start_resolver - do everything we need to read the resolv.conf file
181     * and initialize the resolver file descriptor if needed
182     */
183     static void
184     start_resolver(void)
185     {
186     irc_res_init();
187    
188 michael 8338 if (!ResolverFileDescriptor)
189 adx 30 {
190 michael 8338 int fd = comm_socket(irc_nsaddr_list[0].ss.ss_family, SOCK_DGRAM, 0);
191     if (fd == -1)
192 adx 30 return;
193    
194 michael 8338 ResolverFileDescriptor = fd_open(fd, 1, "UDP resolver socket");
195    
196 adx 30 /* At the moment, the resolver FD data is global .. */
197 michael 8338 comm_setselect(ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
198 adx 30 }
199     }
200    
201     /*
202     * restart_resolver - reread resolv.conf, reopen socket
203     */
204     void
205     restart_resolver(void)
206     {
207 michael 8362 if (ResolverFileDescriptor)
208     {
209     fd_close(ResolverFileDescriptor);
210     ResolverFileDescriptor = NULL;
211     }
212 michael 8338
213 adx 30 start_resolver();
214     }
215    
216     /*
217 michael 2916 * delete_resolver_queries - cleanup outstanding queries
218 adx 30 * for which there no longer exist clients or conf lines.
219     */
220     void
221 michael 992 delete_resolver_queries(const void *vptr)
222 adx 30 {
223 michael 7913 dlink_node *node, *node_next;
224 adx 30
225 michael 4816 DLINK_FOREACH_SAFE(node, node_next, request_list.head)
226 adx 30 {
227 michael 4816 struct reslist *request = node->data;
228 michael 997
229     if (request->callback_ctx == vptr)
230     rem_request(request);
231 adx 30 }
232     }
233    
234     /*
235     * send_res_msg - sends msg to all nameservers found in the "_res" structure.
236     * This should reflect /etc/resolv.conf. We will get responses
237     * which arent needed but is easier than checking to see if nameserver
238 michael 4299 * isn't present. Returns number of messages successfully sent to
239 adx 30 * nameservers or -1 if no successful sends.
240     */
241 michael 7594 static void
242 michael 4467 send_res_msg(const unsigned char *msg, int len, unsigned int rcount)
243 adx 30 {
244 michael 3297 unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
245 adx 30
246     /* RES_PRIMARY option is not implemented
247     * if (res.options & RES_PRIMARY || 0 == max_queries)
248     */
249     if (max_queries == 0)
250     max_queries = 1;
251    
252 michael 3297 for (unsigned int i = 0; i < max_queries; ++i)
253 michael 8338 sendto(ResolverFileDescriptor->fd, msg, len, 0,
254 michael 7594 (struct sockaddr *)&irc_nsaddr_list[i], irc_nsaddr_list[i].ss_len);
255 adx 30 }
256    
257     /*
258     * find_id - find a dns request id (id is determined by dn_mkquery)
259     */
260     static struct reslist *
261 michael 7594 find_id(unsigned int id)
262 adx 30 {
263 michael 7913 dlink_node *node;
264 adx 30
265 michael 4816 DLINK_FOREACH(node, request_list.head)
266 adx 30 {
267 michael 4816 struct reslist *request = node->data;
268 adx 30
269     if (request->id == id)
270 michael 1124 return request;
271 adx 30 }
272    
273 michael 1124 return NULL;
274 adx 30 }
275    
276 michael 2916 /*
277 michael 4312 * query_name - generate a query based on class, type and name.
278 adx 30 */
279 michael 4312 static void
280 michael 4467 query_name(const char *name, int query_class, int type, struct reslist *request)
281 adx 30 {
282 michael 4467 unsigned char buf[MAXPACKET];
283 michael 4312 int request_len = 0;
284 adx 30
285 michael 4312 memset(buf, 0, sizeof(buf));
286 adx 30
287 michael 4467 if ((request_len = irc_res_mkquery(name, query_class, type, buf, sizeof(buf))) > 0)
288 michael 4312 {
289     HEADER *header = (HEADER *)buf;
290    
291     /*
292     * Generate an unique id.
293     * NOTE: we don't have to worry about converting this to and from
294     * network byte order, the nameserver does not interpret this value
295     * and returns it unchanged.
296     */
297     do
298     header->id = (header->id + genrand_int32()) & 0xFFFF;
299     while (find_id(header->id));
300    
301     request->id = header->id;
302     ++request->sends;
303    
304 michael 7594 send_res_msg(buf, request_len, request->sends);
305 michael 4312 }
306 adx 30 }
307    
308     /*
309     * do_query_name - nameserver lookup name
310     */
311     static void
312 michael 992 do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
313 adx 30 struct reslist *request, int type)
314     {
315 michael 4409 char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1];
316 adx 30
317 michael 998 strlcpy(host_name, name, sizeof(host_name));
318 adx 30
319 michael 7596 if (!request)
320 adx 30 {
321 michael 7596 request = make_request(callback, ctx);
322     request->type = type;
323 michael 4409 request->namelength = strlcpy(request->name, host_name, sizeof(request->name));
324 adx 30 }
325    
326     request->type = type;
327     query_name(host_name, C_IN, type, request);
328     }
329    
330     /*
331     * do_query_number - Use this to do reverse IP# lookups.
332     */
333     static void
334 michael 992 do_query_number(dns_callback_fnc callback, void *ctx,
335     const struct irc_ssaddr *addr,
336 adx 30 struct reslist *request)
337     {
338 michael 3298 char ipbuf[128] = "";
339 michael 985
340 adx 30 if (addr->ss.ss_family == AF_INET)
341     {
342 michael 992 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
343 michael 3298 const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
344 adx 30
345 michael 1124 snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
346     (unsigned int)(cp[3]), (unsigned int)(cp[2]),
347     (unsigned int)(cp[1]), (unsigned int)(cp[0]));
348 adx 30 }
349     else if (addr->ss.ss_family == AF_INET6)
350     {
351 michael 992 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
352 michael 3298 const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
353 adx 30
354 michael 1124 snprintf(ipbuf, sizeof(ipbuf),
355     "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
356     "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
357     (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
358     (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
359     (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
360     (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
361     (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
362     (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
363     (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
364     (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
365     (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
366     (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
367     (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
368     (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
369     (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
370     (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
371     (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
372     (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
373 adx 30 }
374 michael 4414
375 michael 7596 if (!request)
376 adx 30 {
377 michael 7596 request = make_request(callback, ctx);
378 adx 30 request->type = T_PTR;
379     memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
380     }
381    
382     query_name(ipbuf, C_IN, T_PTR, request);
383     }
384    
385     /*
386 michael 4312 * gethost_byname_type - get host address from name
387     *
388 adx 30 */
389 michael 4312 void
390     gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
391 adx 30 {
392 michael 4312 assert(name);
393     do_query_name(callback, ctx, name, NULL, type);
394     }
395 adx 30
396 michael 4312 /*
397     * gethost_byaddr - get host name from address
398     */
399     void
400     gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
401     {
402     do_query_number(callback, ctx, addr, NULL);
403 adx 30 }
404    
405     static void
406     resend_query(struct reslist *request)
407     {
408 michael 982 switch (request->type)
409 adx 30 {
410     case T_PTR:
411 michael 992 do_query_number(NULL, NULL, &request->addr, request);
412 adx 30 break;
413     case T_A:
414 michael 4450 case T_AAAA:
415 michael 992 do_query_name(NULL, NULL, request->name, request, request->type);
416 adx 30 break;
417     default:
418     break;
419     }
420     }
421    
422     /*
423     * proc_answer - process name server reply
424     */
425     static int
426 michael 4458 proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob)
427 adx 30 {
428 michael 4409 char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */
429 michael 4458 unsigned char *current = buf + sizeof(HEADER); /* current position in buf */
430 michael 4455 unsigned int type = 0; /* answer type */
431     unsigned int rd_length = 0;
432 adx 30 int n; /* temp count */
433     struct sockaddr_in *v4; /* conversion */
434     struct sockaddr_in6 *v6;
435 michael 4414
436 adx 30 for (; header->qdcount > 0; --header->qdcount)
437     {
438 michael 4458 if ((n = irc_dn_skipname(current, eob)) < 0)
439 adx 30 break;
440    
441 michael 1124 current += (size_t)n + QFIXEDSZ;
442 adx 30 }
443    
444     /*
445 michael 4299 * Process each answer sent to us blech.
446 adx 30 */
447 michael 4458 while (header->ancount > 0 && current < eob)
448 adx 30 {
449 michael 7871 --header->ancount;
450 adx 30
451 michael 4458 n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
452 adx 30
453 michael 4299 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
454 michael 1124 return 0;
455 adx 30
456 michael 4409 hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0';
457 adx 30
458 michael 4299 /*
459     * With Address arithmetic you have to be very anal
460 adx 30 * this code was not working on alpha due to that
461     * (spotted by rodder/jailbird/dianora)
462     */
463 michael 3298 current += (size_t)n;
464 adx 30
465 michael 4458 if (!((current + ANSWER_FIXED_SIZE) < eob))
466 adx 30 break;
467    
468     type = irc_ns_get16(current);
469     current += TYPE_SIZE;
470     current += CLASS_SIZE;
471     current += TTL_SIZE;
472     rd_length = irc_ns_get16(current);
473     current += RDLENGTH_SIZE;
474    
475 michael 2916 /*
476     * Wait to set request->type until we verify this structure
477 adx 30 */
478     switch (type)
479     {
480     case T_A:
481     if (request->type != T_A)
482 michael 1124 return 0;
483 adx 30
484     /*
485 michael 3298 * Check for invalid rd_length or too many addresses
486 adx 30 */
487     if (rd_length != sizeof(struct in_addr))
488 michael 1124 return 0;
489    
490 michael 4475 request->addr.ss_len = sizeof(struct sockaddr_in);
491 adx 30 v4 = (struct sockaddr_in *)&request->addr;
492     v4->sin_family = AF_INET;
493     memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
494 michael 1124 return 1;
495 adx 30 break;
496     case T_AAAA:
497     if (request->type != T_AAAA)
498 michael 1124 return 0;
499    
500 adx 30 if (rd_length != sizeof(struct in6_addr))
501 michael 1124 return 0;
502    
503 adx 30 request->addr.ss_len = sizeof(struct sockaddr_in6);
504     v6 = (struct sockaddr_in6 *)&request->addr;
505     v6->sin6_family = AF_INET6;
506     memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
507 michael 1124 return 1;
508 adx 30 break;
509     case T_PTR:
510     if (request->type != T_PTR)
511 michael 1124 return 0;
512    
513 michael 4458 n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
514 michael 4299 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
515 michael 1124 return 0;
516 adx 30
517 michael 4409 request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name));
518 michael 1124 return 1;
519 adx 30 break;
520 michael 4451 case T_CNAME:
521 adx 30 current += rd_length;
522     break;
523     default:
524 michael 4466 return 0;
525 adx 30 break;
526     }
527     }
528    
529 michael 4466 return 0;
530 adx 30 }
531    
532     /*
533     * res_readreply - read a dns reply from the nameserver and process it.
534     */
535     static void
536 michael 8338 res_readreply(fde_t *F, void *data)
537 adx 30 {
538 michael 4458 unsigned char buf[sizeof(HEADER) + MAXPACKET];
539 adx 30 struct reslist *request = NULL;
540 michael 4302 ssize_t rc = 0;
541 adx 30 socklen_t len = sizeof(struct irc_ssaddr);
542     struct irc_ssaddr lsin;
543    
544 michael 8338 while ((rc = recvfrom(F->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1)
545 michael 4318 {
546     if (rc <= (ssize_t)sizeof(HEADER))
547     continue;
548 adx 30
549 michael 4318 /*
550     * Check against possibly fake replies
551     */
552     if (!res_ourserver(&lsin))
553     continue;
554 michael 984
555 michael 4318 /*
556     * Convert DNS reply reader from Network byte order to CPU byte order.
557     */
558 michael 7596 HEADER *header = (HEADER *)buf;
559 michael 4318 header->ancount = ntohs(header->ancount);
560     header->qdcount = ntohs(header->qdcount);
561     header->nscount = ntohs(header->nscount);
562     header->arcount = ntohs(header->arcount);
563 adx 30
564 michael 4318 /*
565     * Response for an id which we have already received an answer for
566     * just ignore this response.
567     */
568     if ((request = find_id(header->id)) == NULL)
569     continue;
570 michael 4238
571 michael 4450 if (header->rcode != NO_ERRORS || header->ancount == 0)
572 michael 4318 {
573 michael 4450 /*
574 michael 7596 * If a bad error was returned, stop here and don't send
575     * any more (no retries granted).
576 michael 4450 */
577     (*request->callback)(request->callback_ctx, NULL, NULL, 0);
578     rem_request(request);
579 michael 4318 continue;
580     }
581 adx 30
582 michael 4318 /*
583     * If this fails there was an error decoding the received packet.
584     * We only give it one shot. If it fails, just leave the client
585     * unresolved.
586     */
587     if (!proc_answer(request, header, buf, buf + rc))
588 db 155 {
589 michael 4409 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
590 michael 984 rem_request(request);
591 michael 4318 continue;
592 michael 984 }
593 michael 4318
594     if (request->type == T_PTR)
595 michael 984 {
596 michael 4409 if (request->namelength == 0)
597 michael 4318 {
598     /*
599     * Got a PTR response with no name, something bogus is happening
600     * don't bother trying again, the client address doesn't resolve
601     */
602 michael 4409 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
603 michael 4318 rem_request(request);
604     continue;
605     }
606    
607 michael 2916 /*
608 michael 4318 * Lookup the 'authoritative' name that we were given for the ip#.
609 adx 30 */
610 michael 4318 if (request->addr.ss.ss_family == AF_INET6)
611     gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
612     else
613 michael 4414 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
614 michael 7596
615 michael 4318 rem_request(request);
616 michael 984 }
617 michael 4318 else
618 adx 30 {
619     /*
620 michael 4318 * Got a name and address response, client resolved
621 adx 30 */
622 michael 4409 (*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength);
623 adx 30 rem_request(request);
624     }
625 michael 4315
626 michael 4318 continue;
627 adx 30 }
628 michael 4318
629 michael 8338 comm_setselect(F, COMM_SELECT_READ, res_readreply, NULL, 0);
630 adx 30 }
631    
632 michael 4312 /*
633     * timeout_query_list - Remove queries from the list which have been
634     * there too long without being resolved.
635     */
636 michael 7329 static uintmax_t
637 michael 4312 timeout_query_list(void)
638     {
639 michael 7913 dlink_node *node, *node_next;
640 michael 7329 uintmax_t next_time = 0;
641 michael 4312
642 michael 4816 DLINK_FOREACH_SAFE(node, node_next, request_list.head)
643 michael 4312 {
644 michael 7913 struct reslist *request = node->data;
645     uintmax_t timeout = request->sentat + request->timeout;
646 michael 4312
647     if (CurrentTime >= timeout)
648     {
649     if (--request->retries <= 0)
650     {
651 michael 4409 (*request->callback)(request->callback_ctx, NULL, NULL, 0);
652 michael 4312 rem_request(request);
653     continue;
654     }
655     else
656     {
657     request->sentat = CurrentTime;
658     request->timeout += request->timeout;
659     resend_query(request);
660     }
661     }
662    
663     if (next_time == 0 || timeout < next_time)
664     next_time = timeout;
665     }
666    
667     return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
668     }
669    
670     /*
671     * timeout_resolver - check request list
672     */
673     static void
674 michael 4438 timeout_resolver(void *unused)
675 michael 4312 {
676     timeout_query_list();
677     }
678    
679     /*
680 michael 7568 * resolver_init - initialize resolver and resolver library
681 michael 4312 */
682     void
683 michael 7568 resolver_init(void)
684 michael 4312 {
685     static struct event event_timeout_resolver =
686     {
687     .name = "timeout_resolver",
688     .handler = timeout_resolver,
689     .when = 1
690     };
691    
692     start_resolver();
693     event_add(&event_timeout_resolver, NULL);
694     }

Properties

Name Value
svn:eol-style native
svn:keywords Id