]> jfr.im git - irc/rqf/shadowircd.git/blame - src/res.c
Fix some warnings in the resolver code.
[irc/rqf/shadowircd.git] / src / res.c
CommitLineData
212380e3 1/*
2 * A rewrite of Darren Reeds original res.c As there is nothing
3 * left of Darrens original code, this is now licensed by the hybrid group.
4 * (Well, some of the function names are the same, and bits of the structs..)
5 * You can use it where it is useful, free even. Buy us a beer and stuff.
6 *
7 * The authors takes no responsibility for any damage or loss
8 * of property which results from the use of this software.
9 *
42bda3f3 10 * $Id: res.c 3301 2007-03-28 15:04:06Z jilles $
212380e3 11 * from Hybrid Id: res.c 459 2006-02-12 22:21:37Z db $
12 *
13 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
14 * added callbacks and reference counting of returned hostents.
15 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
16 *
17 * This was all needlessly complicated for irc. Simplified. No more hostent
18 * All we really care about is the IP -> hostname mappings. Thats all.
19 *
20 * Apr 28, 2003 --cryogen and Dianora
21 *
22 * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support
23 * removed, various robustness fixes
24 *
25 * 2006 --jilles and nenolod
26 */
27
28#include "stdinc.h"
29#include "ircd_defs.h"
30#include "common.h"
31#include "ircd.h"
212380e3 32#include "res.h"
33#include "reslib.h"
212380e3 34#include "irc_string.h"
35#include "sprintf_irc.h"
36#include "numeric.h"
37#include "client.h" /* SNO_* */
38
39#if (CHAR_BIT != 8)
40#error this code needs to be able to address individual octets
41#endif
42
43static PF res_readreply;
44
45#define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
46#define RES_MAXALIASES 35 /* maximum aliases allowed */
47#define RES_MAXADDRS 35 /* maximum addresses allowed */
48#define AR_TTL 600 /* TTL in seconds for dns cache entries */
49
50/* RFC 1104/1105 wasn't very helpful about what these fields
51 * should be named, so for now, we'll just name them this way.
52 * we probably should look at what named calls them or something.
53 */
54#define TYPE_SIZE (size_t)2
55#define CLASS_SIZE (size_t)2
56#define TTL_SIZE (size_t)4
57#define RDLENGTH_SIZE (size_t)2
58#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
59
60typedef enum
61{
62 REQ_IDLE, /* We're doing not much at all */
63 REQ_PTR, /* Looking up a PTR */
64 REQ_A, /* Looking up an A or AAAA */
65 REQ_CNAME /* We got a CNAME in response, we better get a real answer next */
66} request_state;
67
68struct reslist
69{
af81d5a0 70 rb_dlink_node node;
212380e3 71 int id;
72 int sent; /* number of requests sent */
73 request_state state; /* State the resolver machine is in */
74 time_t ttl;
75 char type;
76 char queryname[128]; /* name currently being queried */
77 char retries; /* retry counter */
78 char sends; /* number of sends (>1 means resent) */
79 char resend; /* send flag. 0 == dont resend */
80 time_t sentat;
81 time_t timeout;
3ea5fee7 82 struct rb_sockaddr_storage addr;
212380e3 83 char *name;
84 struct DNSQuery *query; /* query callback for this request */
85};
86
80e150e1 87static rb_fde_t *res_fd;
af81d5a0 88static rb_dlink_list request_list = { NULL, NULL, 0 };
212380e3 89
90static void rem_request(struct reslist *request);
91static struct reslist *make_request(struct DNSQuery *query);
92static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int);
3ea5fee7 93static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *,
212380e3 94 struct reslist *request);
95static void query_name(struct reslist *request);
96static int send_res_msg(const char *buf, int len, int count);
97static void resend_query(struct reslist *request);
98static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob);
99static int proc_answer(struct reslist *request, HEADER * header, char *, char *);
100static struct reslist *find_id(int id);
101static struct DNSReply *make_dnsreply(struct reslist *request);
102
3ea5fee7 103extern struct rb_sockaddr_storage irc_nsaddr_list[IRCD_MAXNS];
212380e3 104extern int irc_nscount;
105extern char irc_domain[HOSTLEN + 1];
106
107
108/*
109 * int
110 * res_ourserver(inp)
111 * looks up "inp" in irc_nsaddr_list[]
112 * returns:
113 * 0 : not found
114 * >0 : found
115 * author:
116 * paul vixie, 29may94
117 * revised for ircd, cryogen(stu) may03
118 */
3ea5fee7 119static int res_ourserver(const struct rb_sockaddr_storage *inp)
212380e3 120{
2c2e0aa9 121#ifdef RB_IPV6
cda884c6
JT
122 const struct sockaddr_in6 *v6;
123 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
212380e3 124#endif
cda884c6
JT
125 const struct sockaddr_in *v4;
126 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
212380e3 127 int ns;
128
129 for (ns = 0; ns < irc_nscount; ns++)
130 {
3ea5fee7 131 const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns];
2c2e0aa9 132#ifdef RB_IPV6
cda884c6 133 v6 = (const struct sockaddr_in6 *)srv;
212380e3 134#endif
cda884c6 135 v4 = (const struct sockaddr_in *)srv;
212380e3 136
137 /* could probably just memcmp(srv, inp, srv.ss_len) here
138 * but we'll air on the side of caution - stu
139 */
140 switch (srv->ss_family)
141 {
2c2e0aa9 142#ifdef RB_IPV6
212380e3 143 case AF_INET6:
144 if (srv->ss_family == inp->ss_family)
145 if (v6->sin6_port == v6in->sin6_port)
146 if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
147 sizeof(struct in6_addr)) == 0) ||
148 (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
149 sizeof(struct in6_addr)) == 0))
150 return 1;
151 break;
152#endif
153 case AF_INET:
154 if (srv->ss_family == inp->ss_family)
155 if (v4->sin_port == v4in->sin_port)
156 if ((v4->sin_addr.s_addr == INADDR_ANY)
157 || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
158 return 1;
159 break;
160 default:
161 break;
162 }
163 }
164
165 return 0;
166}
167
168/*
169 * timeout_query_list - Remove queries from the list which have been
170 * there too long without being resolved.
171 */
172static time_t timeout_query_list(time_t now)
173{
af81d5a0 174 rb_dlink_node *ptr;
90a3c35b 175 rb_dlink_node *next_ptr;
212380e3 176 struct reslist *request;
177 time_t next_time = 0;
178 time_t timeout = 0;
179
90a3c35b 180 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
212380e3 181 {
182 request = ptr->data;
183 timeout = request->sentat + request->timeout;
184
185 if (now >= timeout)
186 {
187 if (--request->retries <= 0)
188 {
189 (*request->query->callback) (request->query->ptr, NULL);
190 rem_request(request);
191 continue;
192 }
193 else
194 {
195 request->sentat = now;
196 request->timeout += request->timeout;
197 resend_query(request);
198 }
199 }
200
201 if ((next_time == 0) || timeout < next_time)
202 {
203 next_time = timeout;
204 }
205 }
206
207 return (next_time > now) ? next_time : (now + AR_TTL);
208}
209
210/*
211 * timeout_resolver - check request list
212 */
213static void timeout_resolver(void *notused)
214{
9f6bbe3c 215 timeout_query_list(rb_current_time());
212380e3 216}
217
80e150e1
WP
218static struct ev_entry *timeout_resolver_ev = NULL;
219
212380e3 220/*
221 * start_resolver - do everything we need to read the resolv.conf file
222 * and initialize the resolver file descriptor if needed
223 */
224static void start_resolver(void)
225{
226 irc_res_init();
227
cda884c6 228 if (res_fd == NULL)
212380e3 229 {
38e6acdd 230 if ((res_fd = rb_socket(irc_nsaddr_list[0].ss_family, SOCK_DGRAM, 0,
80e150e1 231 "UDP resolver socket")) == NULL)
212380e3 232 return;
233
234 /* At the moment, the resolver FD data is global .. */
80e150e1
WP
235 rb_setselect(res_fd, RB_SELECT_READ, res_readreply, NULL);
236 timeout_resolver_ev = rb_event_add("timeout_resolver", timeout_resolver, NULL, 1);
212380e3 237 }
238}
239
240/*
241 * init_resolver - initialize resolver and resolver library
242 */
243void init_resolver(void)
244{
245#ifdef HAVE_SRAND48
9f6bbe3c 246 srand48(rb_current_time());
212380e3 247#endif
248 start_resolver();
249}
250
251/*
252 * restart_resolver - reread resolv.conf, reopen socket
253 */
254void restart_resolver(void)
255{
38e6acdd 256 rb_close(res_fd);
80e150e1
WP
257 res_fd = NULL;
258 rb_event_delete(timeout_resolver_ev); /* -ddosen */
212380e3 259 start_resolver();
260}
261
262/*
263 * add_local_domain - Add the domain to hostname, if it is missing
264 * (as suggested by eps@TOASTER.SFSU.EDU)
265 */
266void add_local_domain(char *hname, size_t size)
267{
268 /* try to fix up unqualified names */
269 if (strchr(hname, '.') == NULL)
270 {
271 if (irc_domain[0])
272 {
273 size_t len = strlen(hname);
274
275 if ((strlen(irc_domain) + len + 2) < size)
276 {
277 hname[len++] = '.';
278 strcpy(hname + len, irc_domain);
279 }
280 }
281 }
282}
283
284/*
285 * rem_request - remove a request from the list.
286 * This must also free any memory that has been allocated for
287 * temporary storage of DNS results.
288 */
289static void rem_request(struct reslist *request)
290{
af81d5a0 291 rb_dlinkDelete(&request->node, &request_list);
90a3c35b
VY
292 rb_free(request->name);
293 rb_free(request);
212380e3 294}
295
296/*
297 * make_request - Create a DNS request record for the server.
298 */
299static struct reslist *make_request(struct DNSQuery *query)
300{
8e43b0b4 301 struct reslist *request = rb_malloc(sizeof(struct reslist));
212380e3 302
9f6bbe3c 303 request->sentat = rb_current_time();
212380e3 304 request->retries = 3;
305 request->resend = 1;
306 request->timeout = 4; /* start at 4 and exponential inc. */
307 request->query = query;
308 request->state = REQ_IDLE;
309
af81d5a0 310 rb_dlinkAdd(request, &request->node, &request_list);
212380e3 311
312 return request;
313}
314
315/*
316 * delete_resolver_queries - cleanup outstanding queries
317 * for which there no longer exist clients or conf lines.
318 */
319void delete_resolver_queries(const struct DNSQuery *query)
320{
af81d5a0 321 rb_dlink_node *ptr;
90a3c35b 322 rb_dlink_node *next_ptr;
212380e3 323 struct reslist *request;
324
90a3c35b 325 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
212380e3 326 {
327 if ((request = ptr->data) != NULL)
328 {
329 if (query == request->query)
330 rem_request(request);
331 }
332 }
333}
334
335/*
336 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
337 * This should reflect /etc/resolv.conf. We will get responses
338 * which arent needed but is easier than checking to see if nameserver
339 * isnt present. Returns number of messages successfully sent to
340 * nameservers or -1 if no successful sends.
341 */
342static int send_res_msg(const char *msg, int len, int rcount)
343{
344 int i;
345 int sent = 0;
346 int max_queries = IRCD_MIN(irc_nscount, rcount);
347
348 /* RES_PRIMARY option is not implemented
349 * if (res.options & RES_PRIMARY || 0 == max_queries)
350 */
351 if (max_queries == 0)
352 max_queries = 1;
353
42bda3f3 354 for (i = 0; sent < max_queries && i < irc_nscount; i++)
212380e3 355 {
80e150e1 356 if (sendto(rb_get_fd(res_fd), msg, len, 0,
212380e3 357 (struct sockaddr *)&(irc_nsaddr_list[i]),
80e150e1 358 GET_SS_LEN(&irc_nsaddr_list[i])) == len)
212380e3 359 ++sent;
360 }
361
362 return (sent);
363}
364
365/*
366 * find_id - find a dns request id (id is determined by dn_mkquery)
367 */
368static struct reslist *find_id(int id)
369{
af81d5a0 370 rb_dlink_node *ptr;
212380e3 371 struct reslist *request;
372
8e69bb4e 373 RB_DLINK_FOREACH(ptr, request_list.head)
212380e3 374 {
375 request = ptr->data;
376
377 if (request->id == id)
378 return (request);
379 }
380
381 return (NULL);
382}
383
384/*
385 * gethost_byname_type - get host address from name
386 *
387 */
388void gethost_byname_type(const char *name, struct DNSQuery *query, int type)
389{
390 assert(name != 0);
391 do_query_name(query, name, NULL, type);
392}
393
394/*
395 * gethost_byaddr - get host name from address
396 */
3ea5fee7 397void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query)
212380e3 398{
399 do_query_number(query, addr, NULL);
400}
401
402/*
403 * do_query_name - nameserver lookup name
404 */
405static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request,
406 int type)
407{
408 char host_name[HOSTLEN + 1];
409
14e23b0e 410 strlcpy(host_name, name, HOSTLEN + 1);
212380e3 411 add_local_domain(host_name, HOSTLEN);
412
413 if (request == NULL)
414 {
415 request = make_request(query);
8e43b0b4 416 request->name = (char *)rb_malloc(strlen(host_name) + 1);
212380e3 417 strcpy(request->name, host_name);
418 request->state = REQ_A;
419 }
420
421 strlcpy(request->queryname, host_name, sizeof(request->queryname));
422 request->type = type;
423 query_name(request);
424}
425
426/*
427 * do_query_number - Use this to do reverse IP# lookups.
428 */
3ea5fee7 429static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr,
212380e3 430 struct reslist *request)
431{
432 const unsigned char *cp;
433
434 if (request == NULL)
435 {
436 request = make_request(query);
3ea5fee7 437 memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage));
8e43b0b4 438 request->name = (char *)rb_malloc(HOSTLEN + 1);
212380e3 439 }
440
441 if (addr->ss_family == AF_INET)
442 {
cda884c6 443 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
212380e3 444 cp = (const unsigned char *)&v4->sin_addr.s_addr;
445
38e6acdd 446 rb_sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]),
212380e3 447 (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0]));
448 }
2c2e0aa9 449#ifdef RB_IPV6
212380e3 450 else if (addr->ss_family == AF_INET6)
451 {
cda884c6 452 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
212380e3 453 cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
454
455 (void)sprintf(request->queryname, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
456 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
457 (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
458 (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
459 (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
460 (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
461 (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
462 (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
463 (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
464 (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
465 (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
466 (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
467 (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
468 (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
469 (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
470 (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
471 (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
472 (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
473 }
474#endif
475
476 request->type = T_PTR;
477 query_name(request);
478}
479
480/*
481 * query_name - generate a query based on class, type and name.
482 */
483static void query_name(struct reslist *request)
484{
485 char buf[MAXPACKET];
486 int request_len = 0;
487
488 memset(buf, 0, sizeof(buf));
489
490 if ((request_len =
491 irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0)
492 {
493 HEADER *header = (HEADER *) buf;
494#ifndef HAVE_LRAND48
495 int k = 0;
496 struct timeval tv;
497#endif
498 /*
499 * generate an unique id
500 * NOTE: we don't have to worry about converting this to and from
501 * network byte order, the nameserver does not interpret this value
502 * and returns it unchanged
503 */
504#ifdef HAVE_LRAND48
505 do
506 {
507 header->id = (header->id + lrand48()) & 0xffff;
508 } while (find_id(header->id));
509#else
510 gettimeofday(&tv, NULL);
511 do
512 {
513 header->id = (header->id + k + tv.tv_usec) & 0xffff;
514 k++;
515 } while (find_id(header->id));
516#endif /* HAVE_LRAND48 */
517 request->id = header->id;
518 ++request->sends;
519
520 request->sent += send_res_msg(buf, request_len, request->sends);
521 }
522}
523
524static void resend_query(struct reslist *request)
525{
526 if (request->resend == 0)
527 return;
528
529 switch (request->type)
530 {
531 case T_PTR:
532 do_query_number(NULL, &request->addr, request);
533 break;
534 case T_A:
2c2e0aa9 535#ifdef RB_IPV6
212380e3 536 case T_AAAA:
537#endif
538 do_query_name(NULL, request->name, request, request->type);
539 break;
540 default:
541 break;
542 }
543}
544
545/*
546 * check_question - check if the reply really belongs to the
547 * name we queried (to guard against late replies from previous
548 * queries with the same id).
549 */
550static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob)
551{
552 char hostbuf[128]; /* working buffer */
553 unsigned char *current; /* current position in buf */
554 int n; /* temp count */
555
556 current = (unsigned char *)buf + sizeof(HEADER);
557 if (header->qdcount != 1)
558 return 0;
559 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf,
560 sizeof(hostbuf));
561 if (n <= 0)
562 return 0;
563 if (strcasecmp(hostbuf, request->queryname))
564 return 0;
565 return 1;
566}
567
568/*
569 * proc_answer - process name server reply
570 */
571static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob)
572{
573 char hostbuf[HOSTLEN + 100]; /* working buffer */
574 unsigned char *current; /* current position in buf */
575 int query_class; /* answer class */
576 int type; /* answer type */
577 int n; /* temp count */
578 int rd_length;
579 struct sockaddr_in *v4; /* conversion */
2c2e0aa9 580#ifdef RB_IPV6
212380e3 581 struct sockaddr_in6 *v6;
582#endif
583 current = (unsigned char *)buf + sizeof(HEADER);
584
585 for (; header->qdcount > 0; --header->qdcount)
586 {
587 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
588 return 0;
589
590 current += (size_t) n + QFIXEDSZ;
591 }
592
593 /*
594 * process each answer sent to us blech.
595 */
596 while (header->ancount > 0 && (char *)current < eob)
597 {
598 header->ancount--;
599
600 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf,
601 sizeof(hostbuf));
602
603 if (n < 0)
604 {
605 /*
606 * broken message
607 */
608 return (0);
609 }
610 else if (n == 0)
611 {
612 /*
613 * no more answers left
614 */
615 return (0);
616 }
617
618 hostbuf[HOSTLEN] = '\0';
619
620 /* With Address arithmetic you have to be very anal
621 * this code was not working on alpha due to that
622 * (spotted by rodder/jailbird/dianora)
623 */
624 current += (size_t) n;
625
626 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
627 break;
628
629 type = irc_ns_get16(current);
630 current += TYPE_SIZE;
631
632 query_class = irc_ns_get16(current);
633 current += CLASS_SIZE;
634
635 request->ttl = irc_ns_get32(current);
636 current += TTL_SIZE;
637
638 rd_length = irc_ns_get16(current);
639 current += RDLENGTH_SIZE;
640
641 /*
642 * Wait to set request->type until we verify this structure
643 */
644 switch (type)
645 {
646 case T_A:
647 if (request->type != T_A)
648 return (0);
649
650 /*
651 * check for invalid rd_length or too many addresses
652 */
653 if (rd_length != sizeof(struct in_addr))
654 return (0);
655 v4 = (struct sockaddr_in *)&request->addr;
80e150e1 656 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in));
212380e3 657 v4->sin_family = AF_INET;
658 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
659 return (1);
660 break;
2c2e0aa9 661#ifdef RB_IPV6
212380e3 662 case T_AAAA:
663 if (request->type != T_AAAA)
664 return (0);
665 if (rd_length != sizeof(struct in6_addr))
666 return (0);
80e150e1 667 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6));
212380e3 668 v6 = (struct sockaddr_in6 *)&request->addr;
669 v6->sin6_family = AF_INET6;
670 memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
671 return (1);
672 break;
673#endif
674 case T_PTR:
675 if (request->type != T_PTR)
676 return (0);
677 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
678 hostbuf, sizeof(hostbuf));
679 if (n < 0)
680 return (0); /* broken message */
681 else if (n == 0)
682 return (0); /* no more answers left */
683
14e23b0e 684 strlcpy(request->name, hostbuf, HOSTLEN + 1);
212380e3 685
686 return (1);
687 break;
688 case T_CNAME: /* first check we already havent started looking
689 into a cname */
690 if (request->type != T_PTR)
691 return (0);
692
693 if (request->state == REQ_CNAME)
694 {
695 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
696 current, hostbuf, sizeof(hostbuf));
697
698 if (n < 0)
699 return (0);
700 return (1);
701 }
702
703 request->state = REQ_CNAME;
704 current += rd_length;
705 break;
706
707 default:
708 /* XXX I'd rather just throw away the entire bogus thing
709 * but its possible its just a broken nameserver with still
710 * valid answers. But lets do some rudimentary logging for now...
711 */
712 ilog(L_MAIN, "irc_res.c bogus type %d", type);
713 break;
714 }
715 }
716
717 return (1);
718}
719
720/*
721 * res_readreply - read a dns reply from the nameserver and process it.
722 */
80e150e1 723static void res_readreply(rb_fde_t *F, void *data)
212380e3 724{
725 char buf[sizeof(HEADER) + MAXPACKET]
726 /* Sparc and alpha need 16bit-alignment for accessing header->id
727 * (which is uint16_t). Because of the header = (HEADER*) buf;
728 * lateron, this is neeeded. --FaUl
729 */
730#if defined(__sparc__) || defined(__alpha__)
731 __attribute__ ((aligned(16)))
732#endif
733 ;
734 HEADER *header;
735 struct reslist *request = NULL;
736 struct DNSReply *reply = NULL;
737 int rc;
738 int answer_count;
3ea5fee7
VY
739 socklen_t len = sizeof(struct rb_sockaddr_storage);
740 struct rb_sockaddr_storage lsin;
212380e3 741
80e150e1 742 rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
212380e3 743
744 /* Re-schedule a read *after* recvfrom, or we'll be registering
745 * interest where it'll instantly be ready for read :-) -- adrian
746 */
80e150e1 747 rb_setselect(F, RB_SELECT_READ, res_readreply, NULL);
212380e3 748 /* Better to cast the sizeof instead of rc */
749 if (rc <= (int)(sizeof(HEADER)))
750 return;
751
752 /*
753 * convert DNS reply reader from Network byte order to CPU byte order.
754 */
755 header = (HEADER *) buf;
756 header->ancount = ntohs(header->ancount);
757 header->qdcount = ntohs(header->qdcount);
758 header->nscount = ntohs(header->nscount);
759 header->arcount = ntohs(header->arcount);
760
761 /*
762 * response for an id which we have already received an answer for
763 * just ignore this response.
764 */
765 if (0 == (request = find_id(header->id)))
766 return;
767
768 /*
769 * check against possibly fake replies
770 */
771 if (!res_ourserver(&lsin))
772 return;
773
774 if (!check_question(request, header, buf, buf + rc))
775 return;
776
777 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
778 {
779 if (NXDOMAIN == header->rcode)
780 {
781 (*request->query->callback) (request->query->ptr, NULL);
782 rem_request(request);
783 }
784 else
785 {
786 /*
787 * If a bad error was returned, we stop here and dont send
788 * send any more (no retries granted).
789 */
790 (*request->query->callback) (request->query->ptr, NULL);
791 rem_request(request);
792 }
793 return;
794 }
795 /*
796 * If this fails there was an error decoding the received packet,
797 * give up. -- jilles
798 */
799 answer_count = proc_answer(request, header, buf, buf + rc);
800
801 if (answer_count)
802 {
803 if (request->type == T_PTR)
804 {
805 if (request->name == NULL)
806 {
807 /*
808 * got a PTR response with no name, something bogus is happening
809 * don't bother trying again, the client address doesn't resolve
810 */
811 (*request->query->callback) (request->query->ptr, reply);
812 rem_request(request);
813 return;
814 }
815
816 /*
817 * Lookup the 'authoritative' name that we were given for the
818 * ip#.
819 *
820 */
2c2e0aa9 821#ifdef RB_IPV6
212380e3 822 if (request->addr.ss_family == AF_INET6)
823 gethost_byname_type(request->name, request->query, T_AAAA);
824 else
825#endif
826 gethost_byname_type(request->name, request->query, T_A);
827 rem_request(request);
828 }
829 else
830 {
831 /*
832 * got a name and address response, client resolved
833 */
834 reply = make_dnsreply(request);
835 (*request->query->callback) (request->query->ptr, reply);
90a3c35b 836 rb_free(reply);
212380e3 837 rem_request(request);
838 }
839 }
840 else
841 {
842 /* couldn't decode, give up -- jilles */
843 (*request->query->callback) (request->query->ptr, NULL);
844 rem_request(request);
845 }
846}
847
848static struct DNSReply *make_dnsreply(struct reslist *request)
849{
850 struct DNSReply *cp;
851 s_assert(request != 0);
852
8e43b0b4 853 cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply));
212380e3 854
855 cp->h_name = request->name;
856 memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
857 return (cp);
858}
859
860void report_dns_servers(struct Client *source_p)
861{
862 int i;
863 char ipaddr[128];
864
865 for (i = 0; i < irc_nscount; i++)
866 {
867 if (!inetntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]),
868 ipaddr, sizeof ipaddr))
869 strlcpy(ipaddr, "?", sizeof ipaddr);
870 sendto_one_numeric(source_p, RPL_STATSDEBUG,
871 "A %s", ipaddr);
872 }
873}