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