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