30 |
|
#include "ircd.h" |
31 |
|
#include "numeric.h" |
32 |
|
#include "restart.h" |
33 |
+ |
#include "rng_mt.h" |
34 |
|
#include "fdlist.h" |
35 |
|
#include "fileio.h" /* for fbopen / fbclose / fbputs */ |
36 |
|
#include "s_bsd.h" |
70 |
|
#ifdef IPV6 |
71 |
|
REQ_AAAA, /* Looking up an AAAA */ |
72 |
|
#endif |
73 |
< |
REQ_CNAME, /* We got a CNAME in response, we better get a real answer next */ |
73 |
< |
REQ_INT /* ip6.arpa failed, falling back to ip6.int */ |
73 |
> |
REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ |
74 |
|
} request_state; |
75 |
|
|
76 |
|
struct reslist |
92 |
|
}; |
93 |
|
|
94 |
|
static fde_t ResolverFileDescriptor; |
95 |
< |
static dlink_list request_list = { NULL, NULL, 0 }; |
95 |
> |
static dlink_list request_list = { NULL, NULL, 0 }; |
96 |
|
|
97 |
|
static void rem_request(struct reslist *request); |
98 |
|
static struct reslist *make_request(struct DNSQuery *query); |
136 |
|
struct sockaddr_in *v4in = (struct sockaddr_in *)inp; |
137 |
|
int ns; |
138 |
|
|
139 |
< |
for (ns = 0; ns < irc_nscount; ns++) |
139 |
> |
for (ns = 0; ns < irc_nscount; ns++) |
140 |
|
{ |
141 |
|
const struct irc_ssaddr *srv = &irc_nsaddr_list[ns]; |
142 |
|
#ifdef IPV6 |
256 |
|
void |
257 |
|
init_resolver(void) |
258 |
|
{ |
259 |
– |
#ifdef HAVE_SRAND48 |
260 |
– |
srand48(CurrentTime); |
261 |
– |
#endif |
259 |
|
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
260 |
|
start_resolver(); |
261 |
|
} |
317 |
|
struct reslist *request; |
318 |
|
|
319 |
|
request = (struct reslist *)MyMalloc(sizeof(struct reslist)); |
323 |
– |
memset(request, 0, sizeof(struct reslist)); |
320 |
|
|
321 |
|
request->sentat = CurrentTime; |
322 |
|
request->retries = 3; |
323 |
|
request->resend = 1; |
324 |
|
request->timeout = 4; /* start at 4 and exponential inc. */ |
329 |
– |
memset(&request->addr, 0, sizeof(request->addr)); |
325 |
|
request->query = query; |
326 |
|
request->state = REQ_IDLE; |
327 |
|
|
443 |
|
{ |
444 |
|
char host_name[HOSTLEN + 1]; |
445 |
|
|
446 |
< |
strlcpy(host_name, name, HOSTLEN); |
447 |
< |
add_local_domain(host_name, HOSTLEN); |
446 |
> |
strlcpy(host_name, name, HOSTLEN + 1); |
447 |
> |
add_local_domain(host_name, HOSTLEN + 1); |
448 |
|
|
449 |
|
if (request == NULL) |
450 |
|
{ |
451 |
|
request = make_request(query); |
452 |
< |
request->name = (char *)MyMalloc(strlen(host_name) + 1); |
452 |
> |
request->name = MyMalloc(strlen(host_name) + 1); |
453 |
|
request->type = type; |
454 |
|
strcpy(request->name, host_name); |
455 |
|
#ifdef IPV6 |
456 |
< |
if (type == T_A) |
462 |
< |
request->state = REQ_A; |
463 |
< |
else |
456 |
> |
if (type != T_A) |
457 |
|
request->state = REQ_AAAA; |
458 |
< |
#else |
466 |
< |
request->state = REQ_A; |
458 |
> |
else |
459 |
|
#endif |
460 |
+ |
request->state = REQ_A; |
461 |
|
} |
462 |
|
|
463 |
|
request->type = type; |
473 |
|
{ |
474 |
|
char ipbuf[128]; |
475 |
|
const unsigned char *cp; |
476 |
< |
#ifdef IPV6 |
484 |
< |
const char *intarpa; |
485 |
< |
#endif |
476 |
> |
|
477 |
|
if (addr->ss.ss_family == AF_INET) |
478 |
|
{ |
479 |
|
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
489 |
|
struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr; |
490 |
|
cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
491 |
|
|
492 |
< |
if (request != NULL && request->state == REQ_INT) |
493 |
< |
intarpa = "int"; |
503 |
< |
else |
504 |
< |
intarpa = "arpa"; |
505 |
< |
|
506 |
< |
(void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
507 |
< |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.", |
492 |
> |
sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
493 |
> |
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
494 |
|
(unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4), |
495 |
|
(unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4), |
496 |
|
(unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4), |
506 |
|
(unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4), |
507 |
|
(unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4), |
508 |
|
(unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4), |
509 |
< |
(unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa); |
509 |
> |
(unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4)); |
510 |
|
} |
511 |
|
#endif |
512 |
|
if (request == NULL) |
514 |
|
request = make_request(query); |
515 |
|
request->type = T_PTR; |
516 |
|
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
517 |
< |
request->name = (char *)MyMalloc(HOSTLEN + 1); |
517 |
> |
request->name = MyMalloc(HOSTLEN + 1); |
518 |
|
} |
519 |
|
|
520 |
|
query_name(ipbuf, C_IN, T_PTR, request); |
536 |
|
(unsigned char *)buf, sizeof(buf))) > 0) |
537 |
|
{ |
538 |
|
HEADER *header = (HEADER *)buf; |
539 |
< |
#ifndef HAVE_LRAND48 |
554 |
< |
int k = 0; |
555 |
< |
struct timeval tv; |
556 |
< |
#endif |
539 |
> |
|
540 |
|
/* |
541 |
|
* generate an unique id |
542 |
|
* NOTE: we don't have to worry about converting this to and from |
543 |
|
* network byte order, the nameserver does not interpret this value |
544 |
|
* and returns it unchanged |
545 |
|
*/ |
563 |
– |
#ifdef HAVE_LRAND48 |
546 |
|
do |
547 |
< |
{ |
548 |
< |
header->id = (header->id + lrand48()) & 0xffff; |
549 |
< |
} while (find_id(header->id)); |
568 |
< |
#else |
569 |
< |
gettimeofday(&tv, NULL); |
570 |
< |
do |
571 |
< |
{ |
572 |
< |
header->id = (header->id + k + tv.tv_usec) & 0xffff; |
573 |
< |
k++; |
574 |
< |
} while (find_id(header->id)); |
575 |
< |
#endif /* HAVE_LRAND48 */ |
547 |
> |
header->id = (header->id + genrand_int32()) & 0xffff; |
548 |
> |
while (find_id(header->id)); |
549 |
> |
|
550 |
|
request->id = header->id; |
551 |
|
++request->sends; |
552 |
|
|
560 |
|
if (request->resend == 0) |
561 |
|
return; |
562 |
|
|
563 |
< |
switch(request->type) |
563 |
> |
switch (request->type) |
564 |
|
{ |
565 |
|
case T_PTR: |
566 |
|
do_query_number(NULL, &request->addr, request); |
696 |
|
else if (n == 0) |
697 |
|
return(0); /* no more answers left */ |
698 |
|
|
699 |
< |
strlcpy(request->name, hostbuf, HOSTLEN); |
699 |
> |
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
700 |
|
|
701 |
|
return(1); |
702 |
|
break; |
738 |
|
static void |
739 |
|
res_readreply(fde_t *fd, void *data) |
740 |
|
{ |
741 |
< |
char buf[sizeof(HEADER) + MAXPACKET]; |
741 |
> |
char buf[sizeof(HEADER) + MAXPACKET] |
742 |
> |
/* Sparc and alpha need 16bit-alignment for accessing header->id |
743 |
> |
* (which is uint16_t). Because of the header = (HEADER*) buf; |
744 |
> |
* lateron, this is neeeded. --FaUl |
745 |
> |
*/ |
746 |
> |
#if defined(__sparc__) || defined(__alpha__) |
747 |
> |
__attribute__((aligned (16))) |
748 |
> |
#endif |
749 |
> |
; |
750 |
|
HEADER *header; |
751 |
|
struct reslist *request = NULL; |
752 |
|
struct DNSReply *reply = NULL; |
761 |
|
* interest where it'll instantly be ready for read :-) -- adrian |
762 |
|
*/ |
763 |
|
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
764 |
+ |
|
765 |
|
/* Better to cast the sizeof instead of rc */ |
766 |
|
if (rc <= (int)(sizeof(HEADER))) |
767 |
|
return; |
790 |
|
|
791 |
|
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
792 |
|
{ |
793 |
< |
if (SERVFAIL == header->rcode) |
794 |
< |
resend_query(request); |
793 |
> |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
794 |
> |
{ |
795 |
> |
/* |
796 |
> |
* If a bad error was returned, stop here and don't |
797 |
> |
* send any more (no retries granted). |
798 |
> |
*/ |
799 |
> |
(*request->query->callback)(request->query->ptr, NULL); |
800 |
> |
rem_request(request); |
801 |
> |
} |
802 |
> |
#ifdef IPV6 |
803 |
|
else |
804 |
|
{ |
805 |
|
/* |
806 |
|
* If we havent already tried this, and we're looking up AAAA, try A |
807 |
|
* now |
808 |
|
*/ |
818 |
– |
|
819 |
– |
#ifdef IPV6 |
809 |
|
if (request->state == REQ_AAAA && request->type == T_AAAA) |
810 |
|
{ |
811 |
|
request->timeout += 4; |
812 |
|
resend_query(request); |
813 |
|
} |
825 |
– |
else if (request->type == T_PTR && request->state != REQ_INT && |
826 |
– |
request->addr.ss.ss_family == AF_INET6) |
827 |
– |
{ |
828 |
– |
request->state = REQ_INT; |
829 |
– |
request->timeout += 4; |
830 |
– |
request->retries--; |
831 |
– |
resend_query(request); |
832 |
– |
} |
833 |
– |
else |
834 |
– |
#endif |
835 |
– |
{ |
836 |
– |
/* |
837 |
– |
* If a bad error was returned, we stop here and dont send |
838 |
– |
* send any more (no retries granted). |
839 |
– |
*/ |
840 |
– |
(*request->query->callback)(request->query->ptr, NULL); |
841 |
– |
rem_request(request); |
842 |
– |
} |
814 |
|
} |
815 |
+ |
#endif |
816 |
|
|
817 |
|
return; |
818 |
|
} |
819 |
+ |
|
820 |
|
/* |
821 |
|
* If this fails there was an error decoding the received packet, |
822 |
|
* try it again and hope it works the next time. |