]> jfr.im git - solanum.git/blob - authd/res.c
authd: fix race with aborting clients.
[solanum.git] / authd / 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 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
11 * added callbacks and reference counting of returned hostents.
12 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
13 *
14 * This was all needlessly complicated for irc. Simplified. No more hostent
15 * All we really care about is the IP -> hostname mappings. Thats all.
16 *
17 * Apr 28, 2003 --cryogen and Dianora
18 *
19 * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support
20 * removed, various robustness fixes
21 *
22 * 2006 --jilles and nenolod
23 *
24 * Resend queries to other servers if the DNS server replies with an error or
25 * an invalid response. Also, avoid servers that return errors or invalid
26 * responses.
27 *
28 * October 2012 --mr_flea
29 *
30 * ircd-ratbox changes for random IDs merged back in.
31 *
32 * January 2016 --kaniini
33 */
34
35 #include <rb_lib.h>
36 #include "setup.h"
37 #include "res.h"
38 #include "reslib.h"
39
40 #if (CHAR_BIT != 8)
41 #error this code needs to be able to address individual octets
42 #endif
43
44 static PF res_readreply;
45
46 #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
47 #define RES_MAXALIASES 35 /* maximum aliases allowed */
48 #define RES_MAXADDRS 35 /* maximum addresses allowed */
49 #define AR_TTL 600 /* TTL in seconds for dns cache entries */
50
51 /* RFC 1104/1105 wasn't very helpful about what these fields
52 * should be named, so for now, we'll just name them this way.
53 * we probably should look at what named calls them or something.
54 */
55 #define TYPE_SIZE (size_t)2
56 #define CLASS_SIZE (size_t)2
57 #define TTL_SIZE (size_t)4
58 #define RDLENGTH_SIZE (size_t)2
59 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
60
61 #ifdef RB_IPV6
62 struct in6_addr ipv6_addr;
63 #endif
64 struct in_addr ipv4_addr;
65
66 struct reslist
67 {
68 rb_dlink_node node;
69 int id;
70 time_t ttl;
71 char type;
72 char queryname[IRCD_RES_HOSTLEN + 1]; /* name currently being queried */
73 char retries; /* retry counter */
74 char sends; /* number of sends (>1 means resent) */
75 time_t sentat;
76 time_t timeout;
77 int lastns; /* index of last server sent to */
78 struct rb_sockaddr_storage addr;
79 char *name;
80 struct DNSQuery *query; /* query callback for this request */
81 };
82
83 static rb_fde_t *res_fd;
84 static rb_dlink_list request_list = { NULL, NULL, 0 };
85 static int ns_failure_count[IRCD_MAXNS]; /* timeouts and invalid/failed replies */
86
87 static void rem_request(struct reslist *request);
88 static struct reslist *make_request(struct DNSQuery *query);
89 static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query,
90 int type);
91 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int);
92 static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *,
93 struct reslist *request);
94 static void query_name(struct reslist *request);
95 static int send_res_msg(const char *buf, int len, int count);
96 static void resend_query(struct reslist *request);
97 static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob);
98 static int proc_answer(struct reslist *request, HEADER * header, char *, char *);
99 static struct reslist *find_id(int id);
100 static struct DNSReply *make_dnsreply(struct reslist *request);
101 static uint16_t generate_random_id(void);
102
103 #ifdef RES_MIN
104 #undef RES_MIN
105 #endif
106
107 #define RES_MIN(a, b) ((a) < (b) ? (a) : (b))
108
109 /*
110 * int
111 * res_ourserver(inp)
112 * looks up "inp" in irc_nsaddr_list[]
113 * returns:
114 * server ID or -1 for not found
115 * author:
116 * paul vixie, 29may94
117 * revised for ircd, cryogen(stu) may03
118 * slightly modified for charybdis, mr_flea oct12
119 */
120 static int
121 res_ourserver(const struct rb_sockaddr_storage *inp)
122 {
123 #ifdef RB_IPV6
124 const struct sockaddr_in6 *v6;
125 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
126 #endif
127 const struct sockaddr_in *v4;
128 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
129 int ns;
130
131 for(ns = 0; ns < irc_nscount; ns++)
132 {
133 const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns];
134 #ifdef RB_IPV6
135 v6 = (const struct sockaddr_in6 *)srv;
136 #endif
137 v4 = (const struct sockaddr_in *)srv;
138
139 /* could probably just memcmp(srv, inp, srv.ss_len) here
140 * but we'll air on the side of caution - stu
141 */
142 switch (GET_SS_FAMILY(srv))
143 {
144 #ifdef RB_IPV6
145 case AF_INET6:
146 if(GET_SS_FAMILY(srv) == GET_SS_FAMILY(inp))
147 if(v6->sin6_port == v6in->sin6_port)
148 if((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
149 sizeof(struct in6_addr)) == 0) ||
150 (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
151 sizeof(struct in6_addr)) == 0))
152 return 1;
153 break;
154 #endif
155 case AF_INET:
156 if(GET_SS_FAMILY(srv) == GET_SS_FAMILY(inp))
157 if(v4->sin_port == v4in->sin_port)
158 if((v4->sin_addr.s_addr == INADDR_ANY)
159 || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
160 return 1;
161 break;
162 default:
163 break;
164 }
165 }
166
167 return 0;
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(GET_SS_FAMILY(&irc_nsaddr_list[0]), 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 request->id = generate_random_id();
316
317 rb_dlinkAdd(request, &request->node, &request_list);
318
319 return request;
320 }
321
322 /*
323 * retryfreq - determine how many queries to wait before resending
324 * if there have been that many consecutive timeouts
325 */
326 static int retryfreq(int timeouts)
327 {
328 switch (timeouts)
329 {
330 case 1:
331 return 3;
332 case 2:
333 return 9;
334 case 3:
335 return 27;
336 case 4:
337 return 81;
338 default:
339 return 243;
340 }
341 }
342
343 /*
344 * send_res_msg - sends msg to a nameserver.
345 * This should reflect /etc/resolv.conf.
346 * Returns number of nameserver successfully sent to
347 * or -1 if no successful sends.
348 */
349 static int send_res_msg(const char *msg, int len, int rcount)
350 {
351 int i;
352 int ns;
353 static int retrycnt;
354
355 retrycnt++;
356 /* First try a nameserver that seems to work.
357 * Every once in a while, try a possibly broken one to check
358 * if it is working again.
359 */
360 for (i = 0; i < irc_nscount; i++)
361 {
362 ns = (i + rcount - 1) % irc_nscount;
363 if (ns_failure_count[ns] && retrycnt % retryfreq(ns_failure_count[ns]))
364 continue;
365 if (sendto(rb_get_fd(res_fd), msg, len, 0,
366 (struct sockaddr *)&(irc_nsaddr_list[ns]),
367 GET_SS_LEN(&irc_nsaddr_list[ns])) == len)
368 return ns;
369 }
370
371 /* No known working nameservers, try some broken one. */
372 for (i = 0; i < irc_nscount; i++)
373 {
374 ns = (i + rcount - 1) % irc_nscount;
375 if (!ns_failure_count[ns])
376 continue;
377 if (sendto(rb_get_fd(res_fd), msg, len, 0,
378 (struct sockaddr *)&(irc_nsaddr_list[ns]),
379 GET_SS_LEN(&irc_nsaddr_list[ns])) == len)
380 return ns;
381 }
382
383 return -1;
384 }
385
386 /*
387 * find_id - find a dns request id (id is determined by dn_mkquery)
388 */
389 static struct reslist *find_id(int id)
390 {
391 rb_dlink_node *ptr;
392 struct reslist *request;
393
394 RB_DLINK_FOREACH(ptr, request_list.head)
395 {
396 request = ptr->data;
397
398 if (request->id == id)
399 return (request);
400 }
401
402 return (NULL);
403 }
404
405 static uint16_t
406 generate_random_id(void)
407 {
408 uint16_t id;
409
410 do
411 {
412 rb_get_random(&id, sizeof(id));
413 if(id == 0xffff)
414 continue;
415 }
416 while(find_id(id));
417 return id;
418 }
419
420 /*
421 * gethost_byname_type - get host address from name, adding domain if needed
422 */
423 void gethost_byname_type(const char *name, struct DNSQuery *query, int type)
424 {
425 char fqdn[IRCD_RES_HOSTLEN + 1];
426 assert(name != 0);
427
428 rb_strlcpy(fqdn, name, sizeof fqdn);
429 add_local_domain(fqdn, IRCD_RES_HOSTLEN);
430 gethost_byname_type_fqdn(fqdn, query, type);
431 }
432
433 /*
434 * gethost_byname_type_fqdn - get host address from fqdn
435 */
436 static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query,
437 int type)
438 {
439 assert(name != 0);
440 do_query_name(query, name, NULL, type);
441 }
442
443 /*
444 * gethost_byaddr - get host name from address
445 */
446 void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query)
447 {
448 do_query_number(query, addr, NULL);
449 }
450
451 /*
452 * do_query_name - nameserver lookup name
453 */
454 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request,
455 int type)
456 {
457 if (request == NULL)
458 {
459 request = make_request(query);
460 request->name = rb_strdup(name);
461 }
462
463 rb_strlcpy(request->queryname, name, sizeof(request->queryname));
464 request->type = type;
465 query_name(request);
466 }
467
468 /* Build an rDNS style query - if suffix is NULL, use the appropriate .arpa zone */
469 void build_rdns(char *buf, size_t size, const struct rb_sockaddr_storage *addr, const char *suffix)
470 {
471 const unsigned char *cp;
472
473 if (GET_SS_FAMILY(addr) == AF_INET)
474 {
475 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
476 cp = (const unsigned char *)&v4->sin_addr.s_addr;
477
478 (void) snprintf(buf, size, "%u.%u.%u.%u.%s",
479 (unsigned int)(cp[3]),
480 (unsigned int)(cp[2]),
481 (unsigned int)(cp[1]),
482 (unsigned int)(cp[0]),
483 suffix == NULL ? "in-addr.arpa" : suffix);
484 }
485 #ifdef RB_IPV6
486 else if (GET_SS_FAMILY(addr) == AF_INET6)
487 {
488 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
489 cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
490
491 (void) snprintf(buf, size,
492 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s",
493 (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
494 (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
495 (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
496 (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
497 (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
498 (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
499 (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
500 (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
501 (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
502 (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
503 (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
504 (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
505 (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
506 (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
507 (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
508 (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4),
509 suffix == NULL ? "ip6.arpa" : suffix);
510 }
511 #endif
512 }
513
514 /*
515 * do_query_number - Use this to do reverse IP# lookups.
516 */
517 static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr,
518 struct reslist *request)
519 {
520 if (request == NULL)
521 {
522 request = make_request(query);
523 memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage));
524 request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1);
525 }
526
527 build_rdns(request->queryname, IRCD_RES_HOSTLEN + 1, addr, NULL);
528
529 request->type = T_PTR;
530 query_name(request);
531 }
532
533 /*
534 * query_name - generate a query based on class, type and name.
535 */
536 static void query_name(struct reslist *request)
537 {
538 char buf[MAXPACKET];
539 int request_len = 0;
540 int ns;
541
542 memset(buf, 0, sizeof(buf));
543
544 if ((request_len =
545 irc_res_mkquery(request->queryname, C_IN, request->type, (unsigned char *)buf, sizeof(buf))) > 0)
546 {
547 HEADER *header = (HEADER *)(void *)buf;
548 header->id = request->id;
549 ++request->sends;
550
551 ns = send_res_msg(buf, request_len, request->sends);
552 if (ns != -1)
553 request->lastns = ns;
554 }
555 }
556
557 static void resend_query(struct reslist *request)
558 {
559 if (--request->retries <= 0)
560 {
561 (*request->query->callback) (request->query->ptr, NULL);
562 rem_request(request);
563 return;
564 }
565
566 switch (request->type)
567 {
568 case T_PTR:
569 do_query_number(NULL, &request->addr, request);
570 break;
571 case T_A:
572 #ifdef RB_IPV6
573 case T_AAAA:
574 #endif
575 do_query_name(NULL, request->name, request, request->type);
576 break;
577 default:
578 break;
579 }
580 }
581
582 /*
583 * check_question - check if the reply really belongs to the
584 * name we queried (to guard against late replies from previous
585 * queries with the same id).
586 */
587 static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob)
588 {
589 char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */
590 unsigned char *current; /* current position in buf */
591 int n; /* temp count */
592
593 current = (unsigned char *)buf + sizeof(HEADER);
594 if (header->qdcount != 1)
595 return 0;
596 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf,
597 sizeof(hostbuf));
598 if (n <= 0)
599 return 0;
600 if (strcasecmp(hostbuf, request->queryname))
601 return 0;
602 return 1;
603 }
604
605 /*
606 * proc_answer - process name server reply
607 */
608 static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob)
609 {
610 char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */
611 unsigned char *current; /* current position in buf */
612 int type; /* answer type */
613 int n; /* temp count */
614 int rd_length;
615 struct sockaddr_in *v4; /* conversion */
616 #ifdef RB_IPV6
617 struct sockaddr_in6 *v6;
618 #endif
619 current = (unsigned char *)buf + sizeof(HEADER);
620
621 for (; header->qdcount > 0; --header->qdcount)
622 {
623 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
624 return 0;
625
626 current += (size_t) n + QFIXEDSZ;
627 }
628
629 /*
630 * process each answer sent to us blech.
631 */
632 while (header->ancount > 0 && (char *)current < eob)
633 {
634 header->ancount--;
635
636 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, hostbuf,
637 sizeof(hostbuf));
638
639 if (n < 0)
640 {
641 /*
642 * broken message
643 */
644 return (0);
645 }
646 else if (n == 0)
647 {
648 /*
649 * no more answers left
650 */
651 return (0);
652 }
653
654 hostbuf[IRCD_RES_HOSTLEN] = '\0';
655
656 /* With Address arithmetic you have to be very anal
657 * this code was not working on alpha due to that
658 * (spotted by rodder/jailbird/dianora)
659 */
660 current += (size_t) n;
661
662 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
663 break;
664
665 type = irc_ns_get16(current);
666 current += TYPE_SIZE;
667
668 (void) irc_ns_get16(current);
669 current += CLASS_SIZE;
670
671 request->ttl = irc_ns_get32(current);
672 current += TTL_SIZE;
673
674 rd_length = irc_ns_get16(current);
675 current += RDLENGTH_SIZE;
676
677 /*
678 * Wait to set request->type until we verify this structure
679 */
680 switch (type)
681 {
682 case T_A:
683 if (request->type != T_A)
684 return (0);
685
686 /*
687 * check for invalid rd_length or too many addresses
688 */
689 if (rd_length != sizeof(struct in_addr))
690 return (0);
691 v4 = (struct sockaddr_in *)&request->addr;
692 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in));
693 v4->sin_family = AF_INET;
694 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
695 return (1);
696 break;
697 #ifdef RB_IPV6
698 case T_AAAA:
699 if (request->type != T_AAAA)
700 return (0);
701 if (rd_length != sizeof(struct in6_addr))
702 return (0);
703 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6));
704 v6 = (struct sockaddr_in6 *)&request->addr;
705 v6->sin6_family = AF_INET6;
706 memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
707 return (1);
708 break;
709 #endif
710 case T_PTR:
711 if (request->type != T_PTR)
712 return (0);
713 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
714 hostbuf, sizeof(hostbuf));
715 if (n < 0)
716 return (0); /* broken message */
717 else if (n == 0)
718 return (0); /* no more answers left */
719
720 rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1);
721
722 return (1);
723 break;
724 case T_CNAME:
725 /* real answer will follow */
726 current += rd_length;
727 break;
728
729 default:
730 break;
731 }
732 }
733
734 return (1);
735 }
736
737 /*
738 * res_read_single_reply - read a dns reply from the nameserver and process it.
739 * Return value: 1 if a packet was read, 0 otherwise
740 */
741 static int res_read_single_reply(rb_fde_t *F, void *data)
742 {
743 char buf[sizeof(HEADER) + MAXPACKET]
744 /* Sparc and alpha need 16bit-alignment for accessing header->id
745 * (which is uint16_t). Because of the header = (HEADER*) buf;
746 * lateron, this is neeeded. --FaUl
747 */
748 #if defined(__sparc__) || defined(__alpha__)
749 __attribute__ ((aligned(16)))
750 #endif
751 ;
752 HEADER *header;
753 struct reslist *request = NULL;
754 struct DNSReply *reply = NULL;
755 int rc;
756 int answer_count;
757 socklen_t len = sizeof(struct rb_sockaddr_storage);
758 struct rb_sockaddr_storage lsin;
759 int ns;
760
761 rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
762
763 /* No packet */
764 if (rc == 0 || rc == -1)
765 return 0;
766
767 /* Too small */
768 if (rc <= (int)(sizeof(HEADER)))
769 return 1;
770
771 /*
772 * convert DNS reply reader from Network byte order to CPU byte order.
773 */
774 header = (HEADER *)(void *)buf;
775 header->ancount = ntohs(header->ancount);
776 header->qdcount = ntohs(header->qdcount);
777 header->nscount = ntohs(header->nscount);
778 header->arcount = ntohs(header->arcount);
779
780 /*
781 * response for an id which we have already received an answer for
782 * just ignore this response.
783 */
784 if (0 == (request = find_id(header->id)))
785 return 1;
786
787 /*
788 * check against possibly fake replies
789 */
790 ns = res_ourserver(&lsin);
791 if (ns == -1)
792 return 1;
793
794 if (ns != request->lastns)
795 {
796 /*
797 * We'll accept the late reply, but penalize it a little more to make
798 * sure a laggy server doesn't end up favored.
799 */
800 ns_failure_count[ns] += 3;
801 }
802
803
804 if (!check_question(request, header, buf, buf + rc))
805 return 1;
806
807 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
808 {
809 /*
810 * RFC 2136 states that in the event of a server returning SERVFAIL
811 * or NOTIMP, the request should be resent to the next server.
812 * Additionally, if the server refuses our query, resend it as well.
813 * -- mr_flea
814 */
815 if (SERVFAIL == header->rcode || NOTIMP == header->rcode ||
816 REFUSED == header->rcode)
817 {
818 ns_failure_count[ns]++;
819 resend_query(request);
820 }
821 else
822 {
823 /*
824 * Either a fatal error was returned or no answer. Cancel the
825 * request.
826 */
827 if (NXDOMAIN == header->rcode)
828 {
829 /* If the rcode is NXDOMAIN, treat it as a good response. */
830 ns_failure_count[ns] /= 4;
831 }
832 (*request->query->callback) (request->query->ptr, NULL);
833 rem_request(request);
834 }
835 return 1;
836 }
837 /*
838 * If this fails there was an error decoding the received packet.
839 * -- jilles
840 */
841 answer_count = proc_answer(request, header, buf, buf + rc);
842
843 if (answer_count)
844 {
845 if (request->type == T_PTR)
846 {
847 if (request->name == NULL)
848 {
849 /*
850 * Got a PTR response with no name, something strange is
851 * happening. Try another DNS server.
852 */
853 ns_failure_count[ns]++;
854 resend_query(request);
855 return 1;
856 }
857
858 /*
859 * Lookup the 'authoritative' name that we were given for the
860 * ip#.
861 */
862 #ifdef RB_IPV6
863 if (GET_SS_FAMILY(&request->addr) == AF_INET6)
864 gethost_byname_type_fqdn(request->name, request->query, T_AAAA);
865 else
866 #endif
867 gethost_byname_type_fqdn(request->name, request->query, T_A);
868 rem_request(request);
869 }
870 else
871 {
872 /*
873 * got a name and address response, client resolved
874 */
875 reply = make_dnsreply(request);
876 (*request->query->callback) (request->query->ptr, reply);
877 rb_free(reply);
878 rem_request(request);
879 }
880
881 ns_failure_count[ns] /= 4;
882 }
883 else
884 {
885 /* Invalid or corrupt reply - try another resolver. */
886 ns_failure_count[ns]++;
887 resend_query(request);
888 }
889 return 1;
890 }
891
892 static void
893 res_readreply(rb_fde_t *F, void *data)
894 {
895 while (res_read_single_reply(F, data))
896 ;
897 rb_setselect(F, RB_SELECT_READ, res_readreply, NULL);
898 }
899
900 static struct DNSReply *
901 make_dnsreply(struct reslist *request)
902 {
903 struct DNSReply *cp;
904 lrb_assert(request != 0);
905
906 cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply));
907
908 cp->h_name = request->name;
909 memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
910 return (cp);
911 }