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