ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/res.c
Revision: 4318
Committed: Thu Jul 31 21:17:17 2014 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 22545 byte(s)
Log Message:
- res.c:res_readreply(): read as many packets as possible from kernel buffer
  in a single run. This might increase performance for heavy loaded servers
  doing lots of dns requests. As side-effect it fixes issues with edge-triggered
  epoll (we're only doing level-triggered right now)

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file res.c
23 * \brief ircd resolver functions
24 * \version $Id$
25 */
26
27 /*
28 * 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 * (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 * All we really care about is the IP -> hostname mappings. That's all.
42 *
43 * Apr 28, 2003 --cryogen and Dianora
44 */
45
46 #include "stdinc.h"
47 #include "list.h"
48 #include "client.h"
49 #include "event.h"
50 #include "irc_string.h"
51 #include "ircd.h"
52 #include "numeric.h"
53 #include "rng_mt.h"
54 #include "fdlist.h"
55 #include "s_bsd.h"
56 #include "log.h"
57 #include "misc.h"
58 #include "send.h"
59 #include "memory.h"
60 #include "mempool.h"
61 #include "res.h"
62 #include "reslib.h"
63
64 #if (CHAR_BIT != 8)
65 #error this code needs to be able to address individual octets
66 #endif
67
68 static PF res_readreply;
69
70 #define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */
71 #define AR_TTL 600 /**< TTL in seconds for dns cache entries */
72
73 /*
74 * RFC 1104/1105 wasn't very helpful about what these fields
75 * should be named, so for now, we'll just name them this way.
76 * We probably should look at what named calls them or something.
77 */
78 #define TYPE_SIZE (size_t)2
79 #define CLASS_SIZE (size_t)2
80 #define TTL_SIZE (size_t)4
81 #define RDLENGTH_SIZE (size_t)2
82 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
83
84 typedef enum
85 {
86 REQ_IDLE, /**< We're doing not much at all */
87 REQ_PTR, /**< Looking up a PTR */
88 REQ_A, /**< Looking up an A, possibly because AAAA failed */
89 #ifdef IPV6
90 REQ_AAAA, /**< Looking up an AAAA */
91 #endif
92 REQ_CNAME /**< We got a CNAME in response, we better get a real answer next */
93 } request_state;
94
95 struct reslist
96 {
97 dlink_node node; /**< Doubly linked list node. */
98 int id; /**< Request ID (from request header). */
99 int sent; /**< Number of requests sent */
100 request_state state; /**< State the resolver machine is in */
101 char type; /**< Current request type. */
102 char retries; /**< Retry counter */
103 unsigned int sends; /**< Number of sends (>1 means resent). */
104 char resend; /**< Send flag; 0 == don't resend. */
105 time_t sentat; /**< Timestamp we last sent this request. */
106 time_t timeout; /**< When this request times out. */
107 struct irc_ssaddr addr; /**< Address for this request. */
108 char *name; /**< Hostname for this request. */
109 dns_callback_fnc callback; /**< Callback function on completion. */
110 void *callback_ctx; /**< Context pointer for callback. */
111 };
112
113 static fde_t ResolverFileDescriptor;
114 static dlink_list request_list;
115 static mp_pool_t *dns_pool;
116
117
118 /*
119 * rem_request - remove a request from the list.
120 * This must also free any memory that has been allocated for
121 * temporary storage of DNS results.
122 */
123 static void
124 rem_request(struct reslist *request)
125 {
126 dlinkDelete(&request->node, &request_list);
127
128 MyFree(request->name);
129 mp_pool_release(request);
130 }
131
132 /*
133 * make_request - Create a DNS request record for the server.
134 */
135 static struct reslist *
136 make_request(dns_callback_fnc callback, void *ctx)
137 {
138 struct reslist *request = mp_pool_get(dns_pool);
139
140 request->sentat = CurrentTime;
141 request->retries = 2;
142 request->resend = 1;
143 request->timeout = 4; /* Start at 4 and exponential inc. */
144 request->state = REQ_IDLE;
145 request->callback = callback;
146 request->callback_ctx = ctx;
147
148 dlinkAdd(request, &request->node, &request_list);
149 return request;
150 }
151
152 /*
153 * int
154 * res_ourserver(inp)
155 * looks up "inp" in irc_nsaddr_list[]
156 * returns:
157 * 0 : not found
158 * >0 : found
159 * author:
160 * paul vixie, 29may94
161 * revised for ircd, cryogen(stu) may03
162 */
163 static int
164 res_ourserver(const struct irc_ssaddr *inp)
165 {
166 #ifdef IPV6
167 const struct sockaddr_in6 *v6;
168 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
169 #endif
170 const struct sockaddr_in *v4;
171 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
172
173 for (unsigned int i = 0; i < irc_nscount; ++i)
174 {
175 const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
176 #ifdef IPV6
177 v6 = (const struct sockaddr_in6 *)srv;
178 #endif
179 v4 = (const struct sockaddr_in *)srv;
180
181 /*
182 * Could probably just memcmp(srv, inp, srv.ss_len) here
183 * but we'll air on the side of caution - stu
184 */
185 switch (srv->ss.ss_family)
186 {
187 #ifdef IPV6
188 case AF_INET6:
189 if (srv->ss.ss_family == inp->ss.ss_family)
190 if (v6->sin6_port == v6in->sin6_port)
191 if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
192 sizeof(struct in6_addr)))
193 return 1;
194 break;
195 #endif
196 case AF_INET:
197 if (srv->ss.ss_family == inp->ss.ss_family)
198 if (v4->sin_port == v4in->sin_port)
199 if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
200 return 1;
201 break;
202 default:
203 break;
204 }
205 }
206
207 return 0;
208 }
209
210 /*
211 * start_resolver - do everything we need to read the resolv.conf file
212 * and initialize the resolver file descriptor if needed
213 */
214 static void
215 start_resolver(void)
216 {
217 irc_res_init();
218
219 if (!ResolverFileDescriptor.flags.open)
220 {
221 if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family,
222 SOCK_DGRAM, 0, "Resolver socket") == -1)
223 return;
224
225 /* At the moment, the resolver FD data is global .. */
226 comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
227 }
228 }
229
230 /*
231 * restart_resolver - reread resolv.conf, reopen socket
232 */
233 void
234 restart_resolver(void)
235 {
236 fd_close(&ResolverFileDescriptor);
237 start_resolver();
238 }
239
240 /*
241 * delete_resolver_queries - cleanup outstanding queries
242 * for which there no longer exist clients or conf lines.
243 */
244 void
245 delete_resolver_queries(const void *vptr)
246 {
247 dlink_node *ptr = NULL, *ptr_next = NULL;
248
249 DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
250 {
251 struct reslist *request = ptr->data;
252
253 if (request->callback_ctx == vptr)
254 rem_request(request);
255 }
256 }
257
258 /*
259 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
260 * This should reflect /etc/resolv.conf. We will get responses
261 * which arent needed but is easier than checking to see if nameserver
262 * isn't present. Returns number of messages successfully sent to
263 * nameservers or -1 if no successful sends.
264 */
265 static int
266 send_res_msg(const char *msg, int len, unsigned int rcount)
267 {
268 int sent = 0;
269 unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
270
271 /* RES_PRIMARY option is not implemented
272 * if (res.options & RES_PRIMARY || 0 == max_queries)
273 */
274 if (max_queries == 0)
275 max_queries = 1;
276
277 for (unsigned int i = 0; i < max_queries; ++i)
278 {
279 if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
280 (struct sockaddr*)&(irc_nsaddr_list[i]),
281 irc_nsaddr_list[i].ss_len) == len)
282 ++sent;
283 }
284
285 return sent;
286 }
287
288 /*
289 * find_id - find a dns request id (id is determined by dn_mkquery)
290 */
291 static struct reslist *
292 find_id(int id)
293 {
294 dlink_node *ptr = NULL;
295
296 DLINK_FOREACH(ptr, request_list.head)
297 {
298 struct reslist *request = ptr->data;
299
300 if (request->id == id)
301 return request;
302 }
303
304 return NULL;
305 }
306
307 /*
308 * query_name - generate a query based on class, type and name.
309 */
310 static void
311 query_name(const char *name, int query_class, int type,
312 struct reslist *request)
313 {
314 char buf[MAXPACKET];
315 int request_len = 0;
316
317 memset(buf, 0, sizeof(buf));
318
319 if ((request_len = irc_res_mkquery(name, query_class, type,
320 (unsigned char *)buf, sizeof(buf))) > 0)
321 {
322 HEADER *header = (HEADER *)buf;
323
324 /*
325 * Generate an unique id.
326 * NOTE: we don't have to worry about converting this to and from
327 * network byte order, the nameserver does not interpret this value
328 * and returns it unchanged.
329 */
330 do
331 header->id = (header->id + genrand_int32()) & 0xFFFF;
332 while (find_id(header->id));
333
334 request->id = header->id;
335 ++request->sends;
336
337 request->sent += send_res_msg(buf, request_len, request->sends);
338 }
339 }
340
341 /*
342 * do_query_name - nameserver lookup name
343 */
344 static void
345 do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
346 struct reslist *request, int type)
347 {
348 char host_name[HOSTLEN + 1];
349
350 strlcpy(host_name, name, sizeof(host_name));
351
352 if (request == NULL)
353 {
354 request = make_request(callback, ctx);
355 request->name = MyCalloc(strlen(host_name) + 1);
356 request->type = type;
357 strcpy(request->name, host_name);
358 #ifdef IPV6
359 if (type != T_A)
360 request->state = REQ_AAAA;
361 else
362 #endif
363 request->state = REQ_A;
364 }
365
366 request->type = type;
367 query_name(host_name, C_IN, type, request);
368 }
369
370 /*
371 * do_query_number - Use this to do reverse IP# lookups.
372 */
373 static void
374 do_query_number(dns_callback_fnc callback, void *ctx,
375 const struct irc_ssaddr *addr,
376 struct reslist *request)
377 {
378 char ipbuf[128] = "";
379
380 if (addr->ss.ss_family == AF_INET)
381 {
382 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
383 const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
384
385 snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
386 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
387 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
388 }
389 #ifdef IPV6
390 else if (addr->ss.ss_family == AF_INET6)
391 {
392 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
393 const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
394
395 snprintf(ipbuf, sizeof(ipbuf),
396 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
397 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
398 (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
399 (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
400 (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
401 (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
402 (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
403 (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
404 (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
405 (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
406 (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
407 (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
408 (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
409 (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
410 (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
411 (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
412 (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
413 (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
414 }
415 #endif
416 if (request == NULL)
417 {
418 request = make_request(callback, ctx);
419 request->type = T_PTR;
420 memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
421 request->name = MyCalloc(HOSTLEN + 1);
422 }
423
424 query_name(ipbuf, C_IN, T_PTR, request);
425 }
426
427 /*
428 * gethost_byname_type - get host address from name
429 *
430 */
431 void
432 gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
433 {
434 assert(name);
435 do_query_name(callback, ctx, name, NULL, type);
436 }
437
438 /*
439 * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
440 */
441 void
442 gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
443 {
444 #ifdef IPV6
445 gethost_byname_type(callback, ctx, name, T_AAAA);
446 #else
447 gethost_byname_type(callback, ctx, name, T_A);
448 #endif
449 }
450
451 /*
452 * gethost_byaddr - get host name from address
453 */
454 void
455 gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
456 {
457 do_query_number(callback, ctx, addr, NULL);
458 }
459
460 static void
461 resend_query(struct reslist *request)
462 {
463 if (request->resend == 0)
464 return;
465
466 switch (request->type)
467 {
468 case T_PTR:
469 do_query_number(NULL, NULL, &request->addr, request);
470 break;
471 case T_A:
472 do_query_name(NULL, NULL, request->name, request, request->type);
473 break;
474 #ifdef IPV6
475 case T_AAAA: /* Didn't work, try A */
476 if (request->state == REQ_AAAA)
477 do_query_name(NULL, NULL, request->name, request, T_A);
478 #endif
479 default:
480 break;
481 }
482 }
483
484 /*
485 * proc_answer - process name server reply
486 */
487 static int
488 proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
489 {
490 char hostbuf[HOSTLEN + 100]; /* working buffer */
491 unsigned char *current; /* current position in buf */
492 int type; /* answer type */
493 int n; /* temp count */
494 int rd_length;
495 struct sockaddr_in *v4; /* conversion */
496 #ifdef IPV6
497 struct sockaddr_in6 *v6;
498 #endif
499 current = (unsigned char *)buf + sizeof(HEADER);
500
501 for (; header->qdcount > 0; --header->qdcount)
502 {
503 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
504 break;
505
506 current += (size_t)n + QFIXEDSZ;
507 }
508
509 /*
510 * Process each answer sent to us blech.
511 */
512 while (header->ancount > 0 && (char *)current < eob)
513 {
514 header->ancount--;
515
516 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
517 hostbuf, sizeof(hostbuf));
518
519 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
520 return 0;
521
522 hostbuf[HOSTLEN] = '\0';
523
524 /*
525 * With Address arithmetic you have to be very anal
526 * this code was not working on alpha due to that
527 * (spotted by rodder/jailbird/dianora)
528 */
529 current += (size_t)n;
530
531 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
532 break;
533
534 type = irc_ns_get16(current);
535 current += TYPE_SIZE;
536 current += CLASS_SIZE;
537 current += TTL_SIZE;
538 rd_length = irc_ns_get16(current);
539 current += RDLENGTH_SIZE;
540
541 /*
542 * Wait to set request->type until we verify this structure
543 */
544 switch (type)
545 {
546 case T_A:
547 if (request->type != T_A)
548 return 0;
549
550 /*
551 * Check for invalid rd_length or too many addresses
552 */
553 if (rd_length != sizeof(struct in_addr))
554 return 0;
555
556 v4 = (struct sockaddr_in *)&request->addr;
557 request->addr.ss_len = sizeof(struct sockaddr_in);
558 v4->sin_family = AF_INET;
559 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
560 return 1;
561 break;
562 #ifdef IPV6
563 case T_AAAA:
564 if (request->type != T_AAAA)
565 return 0;
566
567 if (rd_length != sizeof(struct in6_addr))
568 return 0;
569
570 request->addr.ss_len = sizeof(struct sockaddr_in6);
571 v6 = (struct sockaddr_in6 *)&request->addr;
572 v6->sin6_family = AF_INET6;
573 memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
574 return 1;
575 break;
576 #endif
577 case T_PTR:
578 if (request->type != T_PTR)
579 return 0;
580
581 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
582 current, hostbuf, sizeof(hostbuf));
583 if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
584 return 0;
585
586 strlcpy(request->name, hostbuf, HOSTLEN + 1);
587 return 1;
588 break;
589 case T_CNAME: /* First check we already haven't started looking into a cname */
590 if (request->type != T_PTR)
591 return 0;
592
593 if (request->state == REQ_CNAME)
594 {
595 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
596 current, hostbuf, sizeof(hostbuf));
597
598 if (n < 0)
599 return 0;
600 return 1;
601 }
602
603 request->state = REQ_CNAME;
604 current += rd_length;
605 break;
606
607 default:
608 /* XXX I'd rather just throw away the entire bogus thing
609 * but its possible its just a broken nameserver with still
610 * valid answers. But lets do some rudimentary logging for now...
611 */
612 ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
613 break;
614 }
615 }
616
617 return 1;
618 }
619
620 /*
621 * res_readreply - read a dns reply from the nameserver and process it.
622 */
623 static void
624 res_readreply(fde_t *fd, void *data)
625 {
626 char buf[sizeof(HEADER) + MAXPACKET];
627 HEADER *header;
628 struct reslist *request = NULL;
629 ssize_t rc = 0;
630 socklen_t len = sizeof(struct irc_ssaddr);
631 struct irc_ssaddr lsin;
632
633 while ((rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1)
634 {
635 if (rc <= (ssize_t)sizeof(HEADER))
636 continue;
637
638 /*
639 * Check against possibly fake replies
640 */
641 if (!res_ourserver(&lsin))
642 continue;
643
644 /*
645 * Convert DNS reply reader from Network byte order to CPU byte order.
646 */
647 header = (HEADER *)buf;
648 header->ancount = ntohs(header->ancount);
649 header->qdcount = ntohs(header->qdcount);
650 header->nscount = ntohs(header->nscount);
651 header->arcount = ntohs(header->arcount);
652
653 /*
654 * Response for an id which we have already received an answer for
655 * just ignore this response.
656 */
657 if ((request = find_id(header->id)) == NULL)
658 continue;
659
660 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
661 {
662 if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
663 {
664 /*
665 * If a bad error was returned, stop here and don't
666 * send any more (no retries granted).
667 */
668 (*request->callback)(request->callback_ctx, NULL, NULL);
669 rem_request(request);
670 }
671 #ifdef IPV6
672 else
673 {
674 /*
675 * If we havent already tried this, and we're looking up AAAA, try A now.
676 */
677 if (request->state == REQ_AAAA && request->type == T_AAAA)
678 {
679 request->timeout += 4;
680 resend_query(request);
681 }
682 }
683 #endif
684 continue;
685 }
686
687 /*
688 * If this fails there was an error decoding the received packet.
689 * We only give it one shot. If it fails, just leave the client
690 * unresolved.
691 */
692 if (!proc_answer(request, header, buf, buf + rc))
693 {
694 (*request->callback)(request->callback_ctx, NULL, NULL);
695 rem_request(request);
696 continue;
697 }
698
699 if (request->type == T_PTR)
700 {
701 if (request->name == NULL)
702 {
703 /*
704 * Got a PTR response with no name, something bogus is happening
705 * don't bother trying again, the client address doesn't resolve
706 */
707 (*request->callback)(request->callback_ctx, NULL, NULL);
708 rem_request(request);
709 continue;
710 }
711
712 /*
713 * Lookup the 'authoritative' name that we were given for the ip#.
714 */
715 #ifdef IPV6
716 if (request->addr.ss.ss_family == AF_INET6)
717 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
718 else
719 #endif
720 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
721 rem_request(request);
722 }
723 else
724 {
725 /*
726 * Got a name and address response, client resolved
727 */
728 (*request->callback)(request->callback_ctx, &request->addr, request->name);
729 rem_request(request);
730 }
731
732 continue;
733 }
734
735 comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
736 }
737
738 void
739 report_dns_servers(struct Client *source_p)
740 {
741 char ipaddr[HOSTIPLEN + 1] = "";
742
743 for (unsigned int i = 0; i < irc_nscount; ++i)
744 {
745 getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
746 irc_nsaddr_list[i].ss_len, ipaddr,
747 sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
748 sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
749 }
750 }
751
752 /*
753 * timeout_query_list - Remove queries from the list which have been
754 * there too long without being resolved.
755 */
756 static time_t
757 timeout_query_list(void)
758 {
759 dlink_node *ptr = NULL, *ptr_next = NULL;
760 struct reslist *request = NULL;
761 time_t next_time = 0;
762 time_t timeout = 0;
763
764 DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
765 {
766 request = ptr->data;
767 timeout = request->sentat + request->timeout;
768
769 if (CurrentTime >= timeout)
770 {
771 if (--request->retries <= 0)
772 {
773 (*request->callback)(request->callback_ctx, NULL, NULL);
774 rem_request(request);
775 continue;
776 }
777 else
778 {
779 request->sentat = CurrentTime;
780 request->timeout += request->timeout;
781 resend_query(request);
782 }
783 }
784
785 if (next_time == 0 || timeout < next_time)
786 next_time = timeout;
787 }
788
789 return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
790 }
791
792 /*
793 * timeout_resolver - check request list
794 */
795 static void
796 timeout_resolver(void *notused)
797 {
798 timeout_query_list();
799 }
800
801 /*
802 * init_resolver - initialize resolver and resolver library
803 */
804 void
805 init_resolver(void)
806 {
807 static struct event event_timeout_resolver =
808 {
809 .name = "timeout_resolver",
810 .handler = timeout_resolver,
811 .when = 1
812 };
813
814 dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
815 memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
816 start_resolver();
817
818 event_add(&event_timeout_resolver, NULL);
819 }

Properties

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