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