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