1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2022 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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 "event.h" |
49 |
#include "irc_string.h" |
50 |
#include "ircd.h" |
51 |
#include "rng_mt.h" |
52 |
#include "fdlist.h" |
53 |
#include "s_bsd.h" |
54 |
#include "misc.h" |
55 |
#include "res.h" |
56 |
#include "reslib.h" |
57 |
#include "memory.h" |
58 |
#include "conf.h" |
59 |
#include "hostmask.h" |
60 |
|
61 |
#if (CHAR_BIT != 8) |
62 |
#error this code needs to be able to address individual octets |
63 |
#endif |
64 |
|
65 |
static void res_readreply(fde_t *, void *); |
66 |
|
67 |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
68 |
|
69 |
/* |
70 |
* RFC 1104/1105 wasn't very helpful about what these fields |
71 |
* should be named, so for now, we'll just name them this way. |
72 |
* We probably should look at what named calls them or something. |
73 |
*/ |
74 |
#define TYPE_SIZE (size_t)2 |
75 |
#define CLASS_SIZE (size_t)2 |
76 |
#define TTL_SIZE (size_t)4 |
77 |
#define RDLENGTH_SIZE (size_t)2 |
78 |
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
79 |
|
80 |
struct reslist |
81 |
{ |
82 |
dlink_node node; /**< Doubly linked list node. */ |
83 |
unsigned int id; /**< Request ID (from request header). */ |
84 |
char type; /**< Current request type. */ |
85 |
char retries; /**< Retry counter */ |
86 |
unsigned int sends; /**< Number of sends (>1 means resent). */ |
87 |
uintmax_t sentat; /**< Timestamp we last sent this request. */ |
88 |
uintmax_t timeout; /**< When this request times out. */ |
89 |
struct irc_ssaddr addr; /**< Address for this request. */ |
90 |
char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */ |
91 |
size_t name_len; /**< Actual hostname length. */ |
92 |
dns_callback_fnc callback; /**< Callback function on completion. */ |
93 |
void *callback_ctx; /**< Context pointer for callback. */ |
94 |
}; |
95 |
|
96 |
static fde_t *ResolverFileDescriptor; |
97 |
static dlink_list request_list; |
98 |
|
99 |
|
100 |
/* |
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 |
xfree(request); |
110 |
} |
111 |
|
112 |
/* |
113 |
* 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 = xcalloc(sizeof(*request)); |
119 |
|
120 |
request->sentat = event_base->time.sec_monotonic; |
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 |
* int |
132 |
* res_ourserver(inp) |
133 |
* looks up "inp" in reslib_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 bool |
142 |
res_ourserver(const struct irc_ssaddr *inp) |
143 |
{ |
144 |
for (unsigned int i = 0; i < reslib_nscount; ++i) |
145 |
if (address_compare(inp, &reslib_nsaddr_list[i], true, true, 0) == true) |
146 |
return true; |
147 |
|
148 |
return false; |
149 |
} |
150 |
|
151 |
/* |
152 |
* start_resolver - do everything we need to read the resolv.conf file |
153 |
* and initialize the resolver file descriptor if needed |
154 |
*/ |
155 |
static void |
156 |
start_resolver(void) |
157 |
{ |
158 |
reslib_res_init(); |
159 |
|
160 |
if (ResolverFileDescriptor == NULL) |
161 |
{ |
162 |
int fd = comm_socket(reslib_nsaddr_list[0].ss.ss_family, SOCK_DGRAM, 0); |
163 |
if (fd == -1) |
164 |
return; |
165 |
|
166 |
ResolverFileDescriptor = fd_open(fd, true, "UDP resolver socket"); |
167 |
|
168 |
/* At the moment, the resolver FD data is global .. */ |
169 |
comm_setselect(ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
170 |
} |
171 |
} |
172 |
|
173 |
/* |
174 |
* restart_resolver - reread resolv.conf, reopen socket |
175 |
*/ |
176 |
void |
177 |
restart_resolver(void) |
178 |
{ |
179 |
if (ResolverFileDescriptor) |
180 |
{ |
181 |
fd_close(ResolverFileDescriptor); |
182 |
ResolverFileDescriptor = NULL; |
183 |
} |
184 |
|
185 |
start_resolver(); |
186 |
} |
187 |
|
188 |
/* |
189 |
* delete_resolver_queries - cleanup outstanding queries |
190 |
* for which there no longer exist clients or conf lines. |
191 |
*/ |
192 |
void |
193 |
delete_resolver_queries(const void *vptr) |
194 |
{ |
195 |
dlink_node *node, *node_next; |
196 |
|
197 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
198 |
{ |
199 |
struct reslist *request = node->data; |
200 |
|
201 |
if (request->callback_ctx == vptr) |
202 |
rem_request(request); |
203 |
} |
204 |
} |
205 |
|
206 |
/* |
207 |
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
208 |
* This should reflect /etc/resolv.conf. We will get responses |
209 |
* which arent needed but is easier than checking to see if nameserver |
210 |
* isn't present. Returns number of messages successfully sent to |
211 |
* nameservers or -1 if no successful sends. |
212 |
*/ |
213 |
static void |
214 |
send_res_msg(const unsigned char *msg, int len, unsigned int rcount) |
215 |
{ |
216 |
unsigned int max_queries = IRCD_MIN(reslib_nscount, rcount); |
217 |
|
218 |
/* RES_PRIMARY option is not implemented |
219 |
* if (res.options & RES_PRIMARY || 0 == max_queries) |
220 |
*/ |
221 |
if (max_queries == 0) |
222 |
max_queries = 1; |
223 |
|
224 |
for (unsigned int i = 0; i < max_queries; ++i) |
225 |
sendto(ResolverFileDescriptor->fd, msg, len, 0, |
226 |
(struct sockaddr *)&reslib_nsaddr_list[i], reslib_nsaddr_list[i].ss_len); |
227 |
} |
228 |
|
229 |
/* |
230 |
* find_id - find a dns request id (id is determined by dn_mkquery) |
231 |
*/ |
232 |
static struct reslist * |
233 |
find_id(unsigned int id) |
234 |
{ |
235 |
dlink_node *node; |
236 |
|
237 |
DLINK_FOREACH(node, request_list.head) |
238 |
{ |
239 |
struct reslist *request = node->data; |
240 |
|
241 |
if (request->id == id) |
242 |
return request; |
243 |
} |
244 |
|
245 |
return NULL; |
246 |
} |
247 |
|
248 |
/* |
249 |
* query_name - generate a query based on class, type and name. |
250 |
*/ |
251 |
static void |
252 |
query_name(const char *name, int query_class, int type, struct reslist *request) |
253 |
{ |
254 |
unsigned char buf[MAXPACKET]; |
255 |
|
256 |
int request_len = reslib_res_mkquery(name, query_class, type, buf, sizeof(buf)); |
257 |
if (request_len > 0) |
258 |
{ |
259 |
HEADER *header = (HEADER *)buf; |
260 |
|
261 |
/* |
262 |
* Generate an unique id. |
263 |
* NOTE: we don't have to worry about converting this to and from |
264 |
* network byte order, the nameserver does not interpret this value |
265 |
* and returns it unchanged. |
266 |
*/ |
267 |
do |
268 |
header->id = (header->id + genrand_int32()) & 0xFFFF; |
269 |
while (find_id(header->id)); |
270 |
|
271 |
request->id = header->id; |
272 |
++request->sends; |
273 |
|
274 |
send_res_msg(buf, request_len, request->sends); |
275 |
} |
276 |
} |
277 |
|
278 |
/* |
279 |
* do_query_name - nameserver lookup name |
280 |
*/ |
281 |
static void |
282 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
283 |
struct reslist *request, int type) |
284 |
{ |
285 |
char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1]; |
286 |
|
287 |
strlcpy(host_name, name, sizeof(host_name)); |
288 |
|
289 |
if (request == NULL) |
290 |
{ |
291 |
request = make_request(callback, ctx); |
292 |
request->type = type; |
293 |
request->name_len = strlcpy(request->name, host_name, sizeof(request->name)); |
294 |
} |
295 |
|
296 |
request->type = type; |
297 |
query_name(host_name, C_IN, type, request); |
298 |
} |
299 |
|
300 |
/* |
301 |
* do_query_number - Use this to do reverse IP# lookups. |
302 |
*/ |
303 |
static void |
304 |
do_query_number(dns_callback_fnc callback, void *ctx, |
305 |
const struct irc_ssaddr *addr, |
306 |
struct reslist *request) |
307 |
{ |
308 |
char ipbuf[128] = ""; |
309 |
|
310 |
if (addr->ss.ss_family == AF_INET) |
311 |
{ |
312 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
313 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
314 |
|
315 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
316 |
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
317 |
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
318 |
} |
319 |
else if (addr->ss.ss_family == AF_INET6) |
320 |
{ |
321 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
322 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
323 |
|
324 |
snprintf(ipbuf, sizeof(ipbuf), |
325 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
326 |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
327 |
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
328 |
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
329 |
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
330 |
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
331 |
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
332 |
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
333 |
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
334 |
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
335 |
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
336 |
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
337 |
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
338 |
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
339 |
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
340 |
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
341 |
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
342 |
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
343 |
} |
344 |
|
345 |
if (request == NULL) |
346 |
{ |
347 |
request = make_request(callback, ctx); |
348 |
request->type = T_PTR; |
349 |
request->addr = *addr; |
350 |
} |
351 |
|
352 |
query_name(ipbuf, C_IN, T_PTR, request); |
353 |
} |
354 |
|
355 |
/* |
356 |
* gethost_byname_type - get host address from name |
357 |
* |
358 |
*/ |
359 |
void |
360 |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
361 |
{ |
362 |
assert(name); |
363 |
do_query_name(callback, ctx, name, NULL, type); |
364 |
} |
365 |
|
366 |
/* |
367 |
* gethost_byaddr - get host name from address |
368 |
*/ |
369 |
void |
370 |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
371 |
{ |
372 |
do_query_number(callback, ctx, addr, NULL); |
373 |
} |
374 |
|
375 |
static void |
376 |
resend_query(struct reslist *request) |
377 |
{ |
378 |
switch (request->type) |
379 |
{ |
380 |
case T_PTR: |
381 |
do_query_number(NULL, NULL, &request->addr, request); |
382 |
break; |
383 |
case T_A: |
384 |
case T_AAAA: |
385 |
do_query_name(NULL, NULL, request->name, request, request->type); |
386 |
break; |
387 |
default: |
388 |
break; |
389 |
} |
390 |
} |
391 |
|
392 |
/* |
393 |
* proc_answer - process name server reply |
394 |
*/ |
395 |
static bool |
396 |
proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob) |
397 |
{ |
398 |
char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */ |
399 |
unsigned char *current = buf + sizeof(HEADER); /* current position in buf */ |
400 |
unsigned int type = 0; /* answer type */ |
401 |
unsigned int rd_length = 0; |
402 |
struct sockaddr_in *v4; /* conversion */ |
403 |
struct sockaddr_in6 *v6; |
404 |
|
405 |
for (; header->qdcount > 0; --header->qdcount) |
406 |
{ |
407 |
int n = reslib_dn_skipname(current, eob); |
408 |
if (n < 0) |
409 |
break; |
410 |
|
411 |
current += (size_t)n + QFIXEDSZ; |
412 |
} |
413 |
|
414 |
/* |
415 |
* Process each answer sent to us blech. |
416 |
*/ |
417 |
while (header->ancount > 0 && current < eob) |
418 |
{ |
419 |
--header->ancount; |
420 |
|
421 |
int n = reslib_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
422 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
423 |
return false; |
424 |
|
425 |
hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0'; |
426 |
|
427 |
/* |
428 |
* With Address arithmetic you have to be very anal |
429 |
* this code was not working on alpha due to that |
430 |
* (spotted by rodder/jailbird/dianora) |
431 |
*/ |
432 |
current += (size_t)n; |
433 |
|
434 |
if (!((current + ANSWER_FIXED_SIZE) < eob)) |
435 |
break; |
436 |
|
437 |
type = reslib_ns_get16(current); |
438 |
current += TYPE_SIZE; |
439 |
current += CLASS_SIZE; |
440 |
current += TTL_SIZE; |
441 |
rd_length = reslib_ns_get16(current); |
442 |
current += RDLENGTH_SIZE; |
443 |
|
444 |
/* |
445 |
* Wait to set request->type until we verify this structure |
446 |
*/ |
447 |
switch (type) |
448 |
{ |
449 |
case T_A: |
450 |
if (request->type != T_A) |
451 |
return false; |
452 |
|
453 |
/* |
454 |
* Check for invalid rd_length or too many addresses |
455 |
*/ |
456 |
if (rd_length != sizeof(struct in_addr)) |
457 |
return false; |
458 |
|
459 |
request->addr.ss_len = sizeof(struct sockaddr_in); |
460 |
v4 = (struct sockaddr_in *)&request->addr; |
461 |
v4->sin_family = AF_INET; |
462 |
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
463 |
return true; |
464 |
break; |
465 |
|
466 |
case T_AAAA: |
467 |
if (request->type != T_AAAA) |
468 |
return false; |
469 |
|
470 |
if (rd_length != sizeof(struct in6_addr)) |
471 |
return false; |
472 |
|
473 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
474 |
v6 = (struct sockaddr_in6 *)&request->addr; |
475 |
v6->sin6_family = AF_INET6; |
476 |
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
477 |
return true; |
478 |
break; |
479 |
|
480 |
case T_PTR: |
481 |
if (request->type != T_PTR) |
482 |
return false; |
483 |
|
484 |
n = reslib_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
485 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
486 |
return false; |
487 |
|
488 |
request->name_len = strlcpy(request->name, hostbuf, sizeof(request->name)); |
489 |
return true; |
490 |
break; |
491 |
|
492 |
case T_CNAME: |
493 |
current += rd_length; |
494 |
break; |
495 |
|
496 |
default: |
497 |
return false; |
498 |
break; |
499 |
} |
500 |
} |
501 |
|
502 |
return false; |
503 |
} |
504 |
|
505 |
/* |
506 |
* res_readreply - read a dns reply from the nameserver and process it. |
507 |
*/ |
508 |
static void |
509 |
res_readreply(fde_t *F, void *data) |
510 |
{ |
511 |
unsigned char buf[sizeof(HEADER) + MAXPACKET]; |
512 |
ssize_t rc; |
513 |
socklen_t len = sizeof(struct irc_ssaddr); |
514 |
struct irc_ssaddr lsin; |
515 |
|
516 |
while ((rc = recvfrom(F->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1) |
517 |
{ |
518 |
if (rc <= (ssize_t)sizeof(HEADER)) |
519 |
continue; |
520 |
|
521 |
/* |
522 |
* Check against possibly fake replies |
523 |
*/ |
524 |
if (res_ourserver(&lsin) == false) |
525 |
continue; |
526 |
|
527 |
/* |
528 |
* Convert DNS reply reader from Network byte order to CPU byte order. |
529 |
*/ |
530 |
HEADER *header = (HEADER *)buf; |
531 |
header->ancount = ntohs(header->ancount); |
532 |
header->qdcount = ntohs(header->qdcount); |
533 |
header->nscount = ntohs(header->nscount); |
534 |
header->arcount = ntohs(header->arcount); |
535 |
|
536 |
/* |
537 |
* Response for an id which we have already received an answer for |
538 |
* just ignore this response. |
539 |
*/ |
540 |
struct reslist *request = find_id(header->id); |
541 |
if (request == NULL) |
542 |
continue; |
543 |
|
544 |
if (header->rcode != NO_ERRORS || header->ancount == 0) |
545 |
{ |
546 |
/* |
547 |
* If a bad error was returned, stop here and don't send |
548 |
* any more (no retries granted). |
549 |
*/ |
550 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
551 |
rem_request(request); |
552 |
continue; |
553 |
} |
554 |
|
555 |
/* |
556 |
* If this fails there was an error decoding the received packet. |
557 |
* We only give it one shot. If it fails, just leave the client |
558 |
* unresolved. |
559 |
*/ |
560 |
if (proc_answer(request, header, buf, buf + rc) == false) |
561 |
{ |
562 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
563 |
rem_request(request); |
564 |
continue; |
565 |
} |
566 |
|
567 |
if (request->type == T_PTR) |
568 |
{ |
569 |
if (request->name_len == 0) |
570 |
{ |
571 |
/* |
572 |
* Got a PTR response with no name, something bogus is happening |
573 |
* don't bother trying again, the client address doesn't resolve |
574 |
*/ |
575 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
576 |
rem_request(request); |
577 |
continue; |
578 |
} |
579 |
|
580 |
/* |
581 |
* Lookup the 'authoritative' name that we were given for the ip#. |
582 |
*/ |
583 |
if (request->addr.ss.ss_family == AF_INET6) |
584 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
585 |
else |
586 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
587 |
|
588 |
rem_request(request); |
589 |
} |
590 |
else |
591 |
{ |
592 |
/* |
593 |
* Got a name and address response, client resolved |
594 |
*/ |
595 |
(*request->callback)(request->callback_ctx, &request->addr, request->name, request->name_len); |
596 |
rem_request(request); |
597 |
} |
598 |
} |
599 |
|
600 |
comm_setselect(F, COMM_SELECT_READ, res_readreply, NULL, 0); |
601 |
} |
602 |
|
603 |
/* |
604 |
* timeout_query_list - Remove queries from the list which have been |
605 |
* there too long without being resolved. |
606 |
*/ |
607 |
static void |
608 |
resolver_timeout(void *unused) |
609 |
{ |
610 |
dlink_node *node, *node_next; |
611 |
|
612 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
613 |
{ |
614 |
struct reslist *request = node->data; |
615 |
uintmax_t timeout = request->sentat + request->timeout; |
616 |
|
617 |
if (event_base->time.sec_monotonic >= timeout) |
618 |
{ |
619 |
if (--request->retries <= 0) |
620 |
{ |
621 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
622 |
rem_request(request); |
623 |
} |
624 |
else |
625 |
{ |
626 |
request->sentat = event_base->time.sec_monotonic; |
627 |
request->timeout += request->timeout; |
628 |
resend_query(request); |
629 |
} |
630 |
} |
631 |
} |
632 |
} |
633 |
|
634 |
/* |
635 |
* resolver_init - initialize resolver and resolver library |
636 |
*/ |
637 |
void |
638 |
resolver_init(void) |
639 |
{ |
640 |
static struct event resolver_timeout_event = |
641 |
{ |
642 |
.name = "resolver_timeout", |
643 |
.handler = resolver_timeout, |
644 |
.when = 1 |
645 |
}; |
646 |
|
647 |
start_resolver(); |
648 |
event_add(&resolver_timeout_event, NULL); |
649 |
} |