ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/net/res.c
Revision: 446
Committed: Sun Feb 12 02:46:54 2006 UTC (18 years, 1 month ago) by db
Content type: text/x-csrc
File size: 23738 byte(s)
Log Message:
- reported by Jon, resolver was not checking for NXDOMAIN as well as SERVFAIL
  this caused odd DNS reports and took longer to connect.

  Tested by Jon (ThaPrince).


File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision