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