]> jfr.im git - irc/rqf/shadowircd.git/blob - src/res.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.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 *
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"
30 #include "res.h"
31 #include "reslib.h"
32 #include "match.h"
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
40 static 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
57 struct reslist
58 {
59 rb_dlink_node node;
60 int id;
61 time_t ttl;
62 char type;
63 char queryname[IRCD_RES_HOSTLEN + 1]; /* name currently being queried */
64 char retries; /* retry counter */
65 char sends; /* number of sends (>1 means resent) */
66 time_t sentat;
67 time_t timeout;
68 unsigned int lastns; /* index of last server sent to */
69 struct rb_sockaddr_storage addr;
70 char *name;
71 struct DNSQuery *query; /* query callback for this request */
72 };
73
74 static rb_fde_t *res_fd;
75 static rb_dlink_list request_list = { NULL, NULL, 0 };
76 static int ns_timeout_count[IRCD_MAXNS];
77
78 static void rem_request(struct reslist *request);
79 static struct reslist *make_request(struct DNSQuery *query);
80 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int);
81 static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *,
82 struct reslist *request);
83 static void query_name(struct reslist *request);
84 static int send_res_msg(const char *buf, int len, int count);
85 static void resend_query(struct reslist *request);
86 static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob);
87 static int proc_answer(struct reslist *request, HEADER * header, char *, char *);
88 static struct reslist *find_id(int id);
89 static struct DNSReply *make_dnsreply(struct reslist *request);
90
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 */
102 static int res_ourserver(const struct rb_sockaddr_storage *inp)
103 {
104 #ifdef RB_IPV6
105 const struct sockaddr_in6 *v6;
106 const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
107 #endif
108 const struct sockaddr_in *v4;
109 const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
110 int ns;
111
112 for (ns = 0; ns < irc_nscount; ns++)
113 {
114 const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns];
115 #ifdef RB_IPV6
116 v6 = (const struct sockaddr_in6 *)srv;
117 #endif
118 v4 = (const struct sockaddr_in *)srv;
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 {
125 #ifdef RB_IPV6
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))
133 {
134 ns_timeout_count[ns] = 0;
135 return 1;
136 }
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))
144 {
145 ns_timeout_count[ns] = 0;
146 return 1;
147 }
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 */
161 static time_t timeout_query_list(time_t now)
162 {
163 rb_dlink_node *ptr;
164 rb_dlink_node *next_ptr;
165 struct reslist *request;
166 time_t next_time = 0;
167 time_t timeout = 0;
168
169 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
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 {
184 ns_timeout_count[request->lastns]++;
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 */
203 static void timeout_resolver(void *notused)
204 {
205 timeout_query_list(rb_current_time());
206 }
207
208 static struct ev_entry *timeout_resolver_ev = NULL;
209
210 /*
211 * start_resolver - do everything we need to read the resolv.conf file
212 * and initialize the resolver file descriptor if needed
213 */
214 static void start_resolver(void)
215 {
216 int i;
217
218 irc_res_init();
219 for (i = 0; i < irc_nscount; i++)
220 ns_timeout_count[i] = 0;
221
222 if (res_fd == NULL)
223 {
224 if ((res_fd = rb_socket(irc_nsaddr_list[0].ss_family, SOCK_DGRAM, 0,
225 "UDP resolver socket")) == NULL)
226 return;
227
228 /* At the moment, the resolver FD data is global .. */
229 rb_setselect(res_fd, RB_SELECT_READ, res_readreply, NULL);
230 timeout_resolver_ev = rb_event_add("timeout_resolver", timeout_resolver, NULL, 1);
231 }
232 }
233
234 /*
235 * init_resolver - initialize resolver and resolver library
236 */
237 void init_resolver(void)
238 {
239 #ifdef HAVE_SRAND48
240 srand48(rb_current_time());
241 #endif
242 start_resolver();
243 }
244
245 /*
246 * restart_resolver - reread resolv.conf, reopen socket
247 */
248 void restart_resolver(void)
249 {
250 rb_close(res_fd);
251 res_fd = NULL;
252 rb_event_delete(timeout_resolver_ev); /* -ddosen */
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 */
260 void 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 */
283 static void rem_request(struct reslist *request)
284 {
285 rb_dlinkDelete(&request->node, &request_list);
286 rb_free(request->name);
287 rb_free(request);
288 }
289
290 /*
291 * make_request - Create a DNS request record for the server.
292 */
293 static struct reslist *make_request(struct DNSQuery *query)
294 {
295 struct reslist *request = rb_malloc(sizeof(struct reslist));
296
297 request->sentat = rb_current_time();
298 request->retries = 3;
299 request->timeout = 4; /* start at 4 and exponential inc. */
300 request->query = query;
301
302 rb_dlinkAdd(request, &request->node, &request_list);
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 */
311 void delete_resolver_queries(const struct DNSQuery *query)
312 {
313 rb_dlink_node *ptr;
314 rb_dlink_node *next_ptr;
315 struct reslist *request;
316
317 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
318 {
319 if ((request = ptr->data) != NULL)
320 {
321 if (query == request->query)
322 rem_request(request);
323 }
324 }
325 }
326
327 /*
328 * retryfreq - determine how many queries to wait before resending
329 * if there have been that many consecutive timeouts
330 */
331 static 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.
353 */
354 static int send_res_msg(const char *msg, int len, int rcount)
355 {
356 int i;
357 int ns;
358 static int retrycnt;
359
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.
364 */
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 }
375
376 /* No known working nameservers, try some broken one. */
377 for (i = 0; i < irc_nscount; i++)
378 {
379 ns = (i + rcount - 1) % irc_nscount;
380 if (!ns_timeout_count[ns])
381 continue;
382 if (sendto(rb_get_fd(res_fd), msg, len, 0,
383 (struct sockaddr *)&(irc_nsaddr_list[ns]),
384 GET_SS_LEN(&irc_nsaddr_list[ns])) == len)
385 return ns;
386 }
387
388 return -1;
389 }
390
391 /*
392 * find_id - find a dns request id (id is determined by dn_mkquery)
393 */
394 static struct reslist *find_id(int id)
395 {
396 rb_dlink_node *ptr;
397 struct reslist *request;
398
399 RB_DLINK_FOREACH(ptr, request_list.head)
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 */
414 void 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 */
423 void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query)
424 {
425 do_query_number(query, addr, NULL);
426 }
427
428 /*
429 * do_query_name - nameserver lookup name
430 */
431 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request,
432 int type)
433 {
434 char host_name[IRCD_RES_HOSTLEN + 1];
435
436 rb_strlcpy(host_name, name, IRCD_RES_HOSTLEN + 1);
437 add_local_domain(host_name, IRCD_RES_HOSTLEN);
438
439 if (request == NULL)
440 {
441 request = make_request(query);
442 request->name = (char *)rb_malloc(strlen(host_name) + 1);
443 strcpy(request->name, host_name);
444 }
445
446 rb_strlcpy(request->queryname, host_name, sizeof(request->queryname));
447 request->type = type;
448 query_name(request);
449 }
450
451 /*
452 * do_query_number - Use this to do reverse IP# lookups.
453 */
454 static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr,
455 struct reslist *request)
456 {
457 const unsigned char *cp;
458
459 if (request == NULL)
460 {
461 request = make_request(query);
462 memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage));
463 request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1);
464 }
465
466 if (addr->ss_family == AF_INET)
467 {
468 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
469 cp = (const unsigned char *)&v4->sin_addr.s_addr;
470
471 rb_sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]),
472 (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0]));
473 }
474 #ifdef RB_IPV6
475 else if (addr->ss_family == AF_INET6)
476 {
477 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
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 */
508 static void query_name(struct reslist *request)
509 {
510 char buf[MAXPACKET];
511 int request_len = 0;
512 int ns;
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
546 ns = send_res_msg(buf, request_len, request->sends);
547 if (ns != -1)
548 request->lastns = ns;
549 }
550 }
551
552 static void resend_query(struct reslist *request)
553 {
554 switch (request->type)
555 {
556 case T_PTR:
557 do_query_number(NULL, &request->addr, request);
558 break;
559 case T_A:
560 #ifdef RB_IPV6
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 */
575 static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob)
576 {
577 char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */
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 */
596 static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob)
597 {
598 char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */
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 */
605 #ifdef RB_IPV6
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
643 hostbuf[IRCD_RES_HOSTLEN] = '\0';
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;
681 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in));
682 v4->sin_family = AF_INET;
683 memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
684 return (1);
685 break;
686 #ifdef RB_IPV6
687 case T_AAAA:
688 if (request->type != T_AAAA)
689 return (0);
690 if (rd_length != sizeof(struct in6_addr))
691 return (0);
692 SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6));
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
709 rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1);
710
711 return (1);
712 break;
713 case T_CNAME:
714 /* real answer will follow */
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 /*
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
734 */
735 static int res_read_single_reply(rb_fde_t *F, void *data)
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;
751 socklen_t len = sizeof(struct rb_sockaddr_storage);
752 struct rb_sockaddr_storage lsin;
753
754 rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
755
756 /* No packet */
757 if (rc == 0 || rc == -1)
758 return 0;
759
760 /* Too small */
761 if (rc <= (int)(sizeof(HEADER)))
762 return 1;
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)))
778 return 1;
779
780 /*
781 * check against possibly fake replies
782 */
783 if (!res_ourserver(&lsin))
784 return 1;
785
786 if (!check_question(request, header, buf, buf + rc))
787 return 1;
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 }
805 return 1;
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);
825 return 1;
826 }
827
828 /*
829 * Lookup the 'authoritative' name that we were given for the
830 * ip#.
831 *
832 */
833 #ifdef RB_IPV6
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);
848 rb_free(reply);
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 }
858 return 1;
859 }
860
861 static 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);
866 }
867
868 static struct DNSReply *make_dnsreply(struct reslist *request)
869 {
870 struct DNSReply *cp;
871 s_assert(request != 0);
872
873 cp = (struct DNSReply *)rb_malloc(sizeof(struct DNSReply));
874
875 cp->h_name = request->name;
876 memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
877 return (cp);
878 }
879
880 void 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 {
887 if (!rb_inet_ntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]),
888 ipaddr, sizeof ipaddr))
889 rb_strlcpy(ipaddr, "?", sizeof ipaddr);
890 sendto_one_numeric(source_p, RPL_STATSDEBUG,
891 "A %s %d", ipaddr, ns_timeout_count[i]);
892 }
893 }