ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/res.c
Revision: 7595
Committed: Thu Jun 9 16:04:26 2016 UTC (9 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 19733 byte(s)
Log Message:
- res.c: reslist::id is now of unsigned type; remove reslist::sent which we currently don't need

File Contents

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

Properties

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