ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/res.c
Revision: 7330
Committed: Fri Feb 19 17:50:13 2016 UTC (9 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 19888 byte(s)
Log Message:
- Now that we got time_t to work nicely on openbsd with snprintf's conversion specifiers,
  we ran into a similiar issue on Raspbian/ARMv7's time_t which is of signed 32 bit and
  doesn't cope at all with %j. Instead of doing tricks, get rid of time_t everywhere and
  forever and use uintmax_t instead which has at least a 'standardized' conversion specifier
  associated with it.

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

Properties

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