2 * src/res.c (C)opyright 1992 Darren Reed. All rights reserved.
3 * This file may not be distributed without the author's permission in any
4 * shape or form. The author takes no responsibility for any damage or loss
5 * of property which results from the use of this software.
9 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
10 * added callbacks and reference counting of returned hostents.
11 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
16 #include "ircd_alloc.h"
18 #include "ircd_osdep.h"
19 #include "ircd_string.h"
25 #include "sprintf_irc.h"
35 #include <sys/socket.h>
39 #include <arpa/nameser.h>
42 #include <arpa/inet.h>
46 #error this code needs to be able to address individual octets
50 * Some systems do not define INADDR_NONE (255.255.255.255)
51 * INADDR_NONE is actually a valid address, but it should never
52 * be returned from any nameserver.
53 * NOTE: The bit pattern for INADDR_NONE and INADDR_ANY (0.0.0.0) should be
54 * the same on all hosts so we shouldn't need to use htonl or ntohl to
55 * compare or set the values.
58 #define INADDR_NONE ((unsigned int) 0xffffffff)
61 #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
62 #define RES_MAXALIASES 35 /* maximum aliases allowed */
63 #define RES_MAXADDRS 35 /* maximum addresses allowed */
65 * OSF1 doesn't have RES_NOALIASES
68 #define RES_NOALIASES 0
72 * macros used to calulate offsets into fixed query buffer
74 #define ALIAS_BLEN ((RES_MAXALIASES + 1) * sizeof(char*))
75 #define ADDRS_BLEN ((RES_MAXADDRS + 1) * sizeof(struct in_addr*))
77 #define ADDRS_OFFSET (ALIAS_BLEN + ADDRS_BLEN)
78 #define ADDRS_DLEN (RES_MAXADDRS * sizeof(struct in_addr))
79 #define NAMES_OFFSET (ADDRS_OFFSET + ADDRS_DLEN)
80 #define MAXGETHOSTLEN (NAMES_OFFSET + MAXPACKET)
82 #define AR_TTL 600 /* TTL in seconds for dns cache entries */
85 * the following values should be prime
87 #define ARES_CACSIZE 307
91 * RFC 1104/1105 wasn't very helpful about what these fields
92 * should be named, so for now, we'll just name them this way.
93 * we probably should look at what named calls them or something.
98 #define RDLENGTH_SIZE 2
99 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
102 * Building the Hostent
103 * The Hostent struct is arranged like this:
104 * +-------------------------------+
105 * Hostent: | struct hostent h |
106 * |-------------------------------|
108 * +-------------------------------+
112 * +-------------------------------+
113 * buf: | h_aliases pointer array | Max size: ALIAS_BLEN;
114 * | NULL | contains `char *'s
115 * |-------------------------------|
116 * | h_addr_list pointer array | Max size: ADDRS_BLEN;
117 * | NULL | contains `struct in_addr *'s
118 * |-------------------------------|
119 * | h_addr_list addresses | Max size: ADDRS_DLEN;
120 * | | contains `struct in_addr's
121 * |-------------------------------|
122 * | storage for hostname strings | Max size: ALIAS_DLEN;
123 * +-------------------------------+ contains `char's
125 * For requests the size of the h_aliases, and h_addr_list pointer
126 * array sizes are set to MAXALISES and MAXADDRS respectively, and
127 * buf is a fixed size with enough space to hold the largest expected
128 * reply from a nameserver, see RFC 1034 and RFC 1035.
129 * For cached entries the sizes are dependent on the actual number
130 * of aliases and addresses. If new aliases and addresses are found
131 * for cached entries, the buffer is grown and the new entries are added.
132 * The hostent struct is filled in with the addresses of the entries in
133 * the Hostent buf as follows:
134 * h_name - contains a pointer to the start of the hostname string area,
135 * or NULL if none is set. The h_name is followed by the
136 * aliases, in the storage for hostname strings area.
137 * h_aliases - contains a pointer to the start of h_aliases pointer array.
138 * This array contains pointers to the storage for hostname
139 * strings area and is terminated with a NULL. The first alias
140 * is stored directly after the h_name.
141 * h_addr_list - contains a pointer to the start of h_addr_list pointer array.
142 * This array contains pointers to in_addr structures in the
143 * h_addr_list addresses area and is terminated with a NULL.
145 * Filling the buffer this way allows for proper alignment of the h_addr_list
148 * This arrangement allows us to alias a Hostent struct pointer as a
149 * real struct hostent* without lying. It also allows us to change the
150 * values contained in the cached entries and requests without changing
151 * the actual hostent pointer, which is saved in a client struct and can't
152 * be changed without blowing things up or a lot more fiddling around.
153 * It also allows for defered allocation of the fixed size buffers until
154 * they are really needed.
155 * Nov. 17, 1997 --Bleep
158 typedef struct Hostent {
159 struct hostent h; /* the hostent struct we are passing around */
160 char* buf; /* buffer for data pointed to from hostent */
164 struct ResRequest* next;
166 int sent; /* number of requests sent */
169 char retries; /* retry counter */
170 char sends; /* number of sends (>1 means resent) */
171 char resend; /* send flag. 0 == dont resend */
176 struct DNSQuery query; /* query callback for this request */
181 struct CacheEntry* hname_next;
182 struct CacheEntry* hnum_next;
183 struct CacheEntry* list_next;
187 struct DNSReply reply;
191 struct CacheEntry* num_list;
192 struct CacheEntry* name_list;
196 int ResolverFileDescriptor = -1; /* GLOBAL - used in s_bsd.c */
198 static time_t nextDNSCheck = 0;
199 static time_t nextCacheExpire = 1;
202 * Keep a spare file descriptor open. res_init calls fopen to read the
203 * resolv.conf file. If ircd is hogging all the file descriptors below 256,
204 * on systems with crippled FILE structures this will cause wierd bugs.
205 * This is definitely needed for Solaris which uses an unsigned char to
206 * hold the file descriptor. --Dianora
208 static int spare_fd = -1;
210 static int cachedCount = 0;
211 static struct CacheTable hashtable[ARES_CACSIZE];
212 static struct CacheEntry* cacheTop;
213 static struct ResRequest* requestListHead; /* head of resolver request list */
214 static struct ResRequest* requestListTail; /* tail of resolver request list */
217 static void add_request(struct ResRequest* request);
218 static void rem_request(struct ResRequest* request);
219 static struct ResRequest* make_request(const struct DNSQuery* query);
220 static void rem_cache(struct CacheEntry*);
221 static void do_query_name(const struct DNSQuery* query,
223 struct ResRequest* request);
224 static void do_query_number(const struct DNSQuery* query,
225 const struct in_addr*,
226 struct ResRequest* request);
227 static void query_name(const char* name,
230 struct ResRequest* request);
231 static void resend_query(struct ResRequest* request);
232 static struct CacheEntry* make_cache(struct ResRequest* request);
233 static struct CacheEntry* find_cache_name(const char* name);
234 static struct CacheEntry* find_cache_number(struct ResRequest* request,
236 static struct ResRequest* find_id(int);
238 static struct cacheinfo {
248 static struct resinfo {
263 * From bind 8.3, these aren't declared in earlier versions of bind
265 extern u_short _getshort(const u_char *);
266 extern u_int _getlong(const u_char *);
269 * res_isourserver(ina)
270 * looks up "ina" in _res.ns_addr_list[]
275 * paul vixie, 29may94
278 res_ourserver(const struct __res_state* statp, const struct sockaddr_in *inp)
280 struct sockaddr_in ina;
284 for (ns = 0; ns < statp->nscount; ns++) {
285 const struct sockaddr_in *srv = &statp->nsaddr_list[ns];
287 if (srv->sin_family == ina.sin_family &&
288 srv->sin_port == ina.sin_port &&
289 (srv->sin_addr.s_addr == INADDR_ANY ||
290 srv->sin_addr.s_addr == ina.sin_addr.s_addr))
297 * start_resolver - do everything we need to read the resolv.conf file
298 * and initialize the resolver file descriptor if needed
300 static void start_resolver(void)
302 Debug((DEBUG_DNS, "Resolver: start_resolver"));
304 * close the spare file descriptor so res_init can read resolv.conf
305 * successfully. Needed on Solaris
310 res_init(); /* res_init always returns 0 */
312 * make sure we have a valid file descriptor below 256 so we can
313 * do this again. Needed on Solaris
315 spare_fd = open("/dev/null",O_RDONLY,0);
316 if ((spare_fd < 0) || (spare_fd > 255)) {
318 sprintf_irc(sparemsg, "invalid spare_fd %d", spare_fd);
319 server_restart(sparemsg);
324 _res.nsaddr_list[0].sin_addr.s_addr = inet_addr("127.0.0.1");
326 _res.options |= RES_NOALIASES;
328 if (ResolverFileDescriptor < 0) {
329 ResolverFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
330 if (-1 == ResolverFileDescriptor) {
331 report_error("Resolver: error creating socket for %s: %s",
335 if (!os_set_nonblocking(ResolverFileDescriptor))
336 report_error("Resolver: error setting non-blocking for %s: %s",
342 * init_resolver - initialize resolver and resolver library
344 int init_resolver(void)
346 Debug((DEBUG_DNS, "Resolver: init_resolver"));
348 srand48(CurrentTime);
350 memset(&cainfo, 0, sizeof(cainfo));
351 memset(hashtable, 0, sizeof(hashtable));
352 memset(&reinfo, 0, sizeof(reinfo));
354 requestListHead = requestListTail = NULL;
358 Debug((DEBUG_DNS, "Resolver: fd %d errno: %d h_errno: %d: %s",
359 ResolverFileDescriptor, errno, h_errno,
360 (strerror(errno)) ? strerror(errno) : "Unknown"));
361 return ResolverFileDescriptor;
365 * restart_resolver - flush the cache, reread resolv.conf, reopen socket
367 void restart_resolver(void)
369 /* flush_cache(); flush the dns cache */
374 * add_request - place a new request in the request list
376 static void add_request(struct ResRequest* request)
378 assert(0 != request);
379 if (!requestListHead)
380 requestListHead = requestListTail = request;
382 requestListTail->next = request;
383 requestListTail = request;
385 request->next = NULL;
386 ++reinfo.re_requests;
390 * rem_request - remove a request from the list.
391 * This must also free any memory that has been allocated for
392 * temporary storage of DNS results.
394 static void rem_request(struct ResRequest* request)
396 struct ResRequest** current;
397 struct ResRequest* prev = NULL;
399 assert(0 != request);
400 for (current = &requestListHead; *current; ) {
401 if (*current == request) {
402 *current = request->next;
403 if (requestListTail == request)
404 requestListTail = prev;
408 current = &(*current)->next;
410 MyFree(request->he.buf);
411 MyFree(request->name);
416 * make_request - Create a DNS request record for the server.
418 static struct ResRequest* make_request(const struct DNSQuery* query)
420 struct ResRequest* request;
422 request = (struct ResRequest*) MyMalloc(sizeof(struct ResRequest));
423 memset(request, 0, sizeof(struct ResRequest));
425 request->sentat = CurrentTime;
426 request->retries = 3;
428 request->timeout = 5; /* start at 5 per RFC1123 */
429 request->addr.s_addr = INADDR_NONE;
430 request->he.h.h_addrtype = AF_INET;
431 request->he.h.h_length = sizeof(struct in_addr);
432 request->query.vptr = query->vptr;
433 request->query.callback = query->callback;
435 #if defined(NULL_POINTER_NOT_ZERO)
436 request->next = NULL;
437 request->he.buf = NULL;
438 request->he.h.h_name = NULL;
439 request->he.h.h_aliases = NULL;
440 request->he.h.h_addr_list = NULL;
442 add_request(request);
447 * timeout_query_list - Remove queries from the list which have been
448 * there too long without being resolved.
450 static time_t timeout_query_list(time_t now)
452 struct ResRequest* request;
453 struct ResRequest* next_request = 0;
454 time_t next_time = 0;
457 Debug((DEBUG_DNS, "Resolver: timeout_query_list at %s", myctime(now)));
458 for (request = requestListHead; request; request = next_request) {
459 next_request = request->next;
460 timeout = request->sentat + request->timeout;
462 if (--request->retries <= 0) {
463 ++reinfo.re_timeouts;
464 (*request->query.callback)(request->query.vptr, 0);
465 rem_request(request);
469 request->sentat = now;
470 request->timeout += request->timeout;
471 resend_query(request);
474 if (!next_time || timeout < next_time) {
478 return (next_time > now) ? next_time : (now + AR_TTL);
482 * expire_cache - removes entries from the cache which are older
483 * than their expiry times. returns the time at which the server
484 * should next poll the cache.
486 static time_t expire_cache(time_t now)
488 struct CacheEntry* cp;
489 struct CacheEntry* cp_next;
492 Debug((DEBUG_DNS, "Resolver: expire_cache at %s", myctime(now)));
493 for (cp = cacheTop; cp; cp = cp_next) {
494 cp_next = cp->list_next;
495 if (cp->expireat < now) {
499 else if (!expire || expire > cp->expireat)
500 expire = cp->expireat;
502 return (expire > now) ? expire : (now + AR_TTL);
506 * timeout_resolver - check request list and cache for expired entries
508 time_t timeout_resolver(time_t now)
510 if (nextDNSCheck < now)
511 nextDNSCheck = timeout_query_list(now);
512 if (nextCacheExpire < now)
513 nextCacheExpire = expire_cache(now);
514 return IRCD_MIN(nextDNSCheck, nextCacheExpire);
519 * delete_resolver_queries - cleanup outstanding queries
520 * for which there no longer exist clients or conf lines.
522 void delete_resolver_queries(const void* vptr)
524 struct ResRequest* request;
525 struct ResRequest* next_request;
527 for (request = requestListHead; request; request = next_request) {
528 next_request = request->next;
529 if (vptr == request->query.vptr)
530 rem_request(request);
535 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
536 * This should reflect /etc/resolv.conf. We will get responses
537 * which arent needed but is easier than checking to see if nameserver
538 * isnt present. Returns number of messages successfully sent to
539 * nameservers or -1 if no successful sends.
541 static int send_res_msg(const u_char* msg, int len, int rcount)
545 int max_queries = IRCD_MIN(_res.nscount, rcount);
549 * RES_PRIMARY option is not implemented
550 * if (_res.options & RES_PRIMARY || 0 == max_queries)
552 if (0 == max_queries)
555 Debug((DEBUG_DNS, "Resolver: sendto %d", max_queries));
557 for (i = 0; i < max_queries; i++) {
558 if (sendto(ResolverFileDescriptor, msg, len, 0,
559 (struct sockaddr*) &(_res.nsaddr_list[i]),
560 sizeof(struct sockaddr_in)) == len) {
565 ircd_log(L_ERROR, "Resolver: send failed %s",
566 (strerror(errno)) ? strerror(errno) : "Unknown");
572 * find_id - find a dns request id (id is determined by dn_mkquery)
574 static struct ResRequest* find_id(int id)
576 struct ResRequest* request;
578 for (request = requestListHead; request; request = request->next) {
579 if (request->id == id)
586 * gethost_byname - get host address from name
588 struct DNSReply* gethost_byname(const char* name,
589 const struct DNSQuery* query)
591 struct CacheEntry* cp;
594 Debug((DEBUG_DNS, "Resolver: gethost_byname %s", name));
596 if ((cp = find_cache_name(name)))
599 do_query_name(query, name, NULL);
605 * gethost_byaddr - get host name from address
607 struct DNSReply* gethost_byaddr(const char* addr,
608 const struct DNSQuery* query)
610 struct CacheEntry *cp;
614 Debug((DEBUG_DNS, "Resolver: gethost_byaddr %s", ircd_ntoa(addr)));
617 if ((cp = find_cache_number(NULL, addr)))
620 do_query_number(query, (const struct in_addr*) addr, NULL);
626 * do_query_name - nameserver lookup name
628 static void do_query_name(const struct DNSQuery* query,
629 const char* name, struct ResRequest* request)
631 char hname[HOSTLEN + 1];
634 ircd_strncpy(hname, name, HOSTLEN);
635 hname[HOSTLEN] = '\0';
638 request = make_request(query);
640 request->name = (char*) MyMalloc(strlen(hname) + 1);
641 strcpy(request->name, hname);
643 query_name(hname, C_IN, T_A, request);
647 * do_query_number - Use this to do reverse IP# lookups.
649 static void do_query_number(const struct DNSQuery* query,
650 const struct in_addr* addr,
651 struct ResRequest* request)
654 const unsigned char* cp;
657 cp = (const unsigned char*) &addr->s_addr;
658 sprintf_irc(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
659 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
660 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
663 request = make_request(query);
664 request->type = T_PTR;
665 request->addr.s_addr = addr->s_addr;
667 query_name(ipbuf, C_IN, T_PTR, request);
671 * query_name - generate a query based on class, type and name.
673 static void query_name(const char* name, int query_class,
674 int type, struct ResRequest* request)
680 assert(0 != request);
682 Debug((DEBUG_DNS, "Resolver: query_name: %s %d %d", name, query_class, type));
683 memset(buf, 0, sizeof(buf));
684 if ((request_len = res_mkquery(QUERY, name, query_class, type,
685 NULL, 0, NULL, buf, sizeof(buf))) > 0) {
686 HEADER* header = (HEADER*) buf;
692 * generate a unique id
693 * NOTE: we don't have to worry about converting this to and from
694 * network byte order, the nameserver does not interpret this value
695 * and returns it unchanged
699 header->id = (header->id + lrand48()) & 0xffff;
700 } while (find_id(header->id));
702 gettimeofday(&tv, NULL);
704 header->id = (header->id + k + tv.tv_usec) & 0xffff;
706 } while (find_id(header->id));
708 request->id = header->id;
710 Debug((DEBUG_DNS, "Resolver: query_name %d: %s %d %d", request->id,
711 name, query_class, type));
712 request->sent += send_res_msg(buf, request_len, request->sends);
716 static void resend_query(struct ResRequest* request)
718 assert(0 != request);
720 if (request->resend == 0)
723 switch(request->type) {
725 do_query_number(NULL, &request->addr, request);
728 do_query_name(NULL, request->name, request);
736 * proc_answer - process name server reply
737 * build a hostent struct in the passed request
739 static int proc_answer(struct ResRequest* request, HEADER* header,
740 u_char* buf, u_char* eob)
742 char hostbuf[HOSTLEN + 1]; /* working buffer */
743 u_char* current; /* current position in buf */
744 char** alias; /* alias list */
745 char** addr; /* address list */
746 char* name; /* pointer to name string */
747 char* address; /* pointer to address */
748 char* endp; /* end of our buffer */
749 int query_class; /* answer class */
750 int type; /* answer type */
751 int rd_length; /* record data length */
752 int answer_count = 0; /* answer counter */
753 int n; /* temp count */
754 int addr_count = 0; /* number of addresses in hostent */
755 int alias_count = 0; /* number of aliases in hostent */
756 struct hostent* hp; /* hostent getting filled */
758 assert(0 != request);
763 current = buf + sizeof(HEADER);
764 hp = &(request->he.h);
766 * lazy allocation of request->he.buf, we don't allocate a buffer
767 * unless there is something to put in it.
769 if (!request->he.buf) {
770 request->he.buf = (char*) MyMalloc(MAXGETHOSTLEN + 1);
771 request->he.buf[MAXGETHOSTLEN] = '\0';
773 * array of alias list pointers starts at beginning of buf
775 hp->h_aliases = (char**) request->he.buf;
776 hp->h_aliases[0] = NULL;
778 * array of address list pointers starts after alias list pointers
779 * the actual addresses follow the the address list pointers
781 hp->h_addr_list = (char**)(request->he.buf + ALIAS_BLEN);
783 * don't copy the host address to the beginning of h_addr_list
785 hp->h_addr_list[0] = NULL;
787 endp = request->he.buf + MAXGETHOSTLEN;
789 * find the end of the address list
791 addr = hp->h_addr_list;
797 * make address point to first available address slot
799 address = request->he.buf + ADDRS_OFFSET +
800 (sizeof(struct in_addr) * addr_count);
802 * find the end of the alias list
804 alias = hp->h_aliases;
810 * make name point to first available space in request->buf
812 if (alias_count > 0) {
813 name = hp->h_aliases[alias_count - 1];
814 name += (strlen(name) + 1);
817 name = hp->h_name + strlen(hp->h_name) + 1;
819 name = request->he.buf + ADDRS_OFFSET + ADDRS_DLEN;
824 while (header->qdcount-- > 0) {
825 if ((n = dn_skipname(current, eob)) < 0)
827 current += (n + QFIXEDSZ);
830 * process each answer sent to us blech.
832 while (header->ancount-- > 0 && current < eob && name < endp) {
833 n = dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
836 * no more answers left
840 hostbuf[HOSTLEN] = '\0';
842 * With Address arithmetic you have to be very anal
843 * this code was not working on alpha due to that
844 * (spotted by rodder/jailbird/dianora)
846 current += (size_t) n;
848 if (!((current + ANSWER_FIXED_SIZE) < eob))
851 type = _getshort(current);
852 current += TYPE_SIZE;
854 query_class = _getshort(current);
855 current += CLASS_SIZE;
857 request->ttl = _getlong(current);
860 rd_length = _getshort(current);
861 current += RDLENGTH_SIZE;
864 * Wait to set request->type until we verify this structure
869 * check for invalid rd_length or too many addresses
871 if (rd_length != sizeof(struct in_addr))
873 if (++addr_count < RES_MAXADDRS) {
874 if (answer_count == 1)
875 hp->h_addrtype = (query_class == C_IN) ? AF_INET : AF_UNSPEC;
877 memcpy(address, current, sizeof(struct in_addr));
880 address += sizeof(struct in_addr);
883 strcpy(name, hostbuf);
885 name += strlen(name) + 1;
887 Debug((DEBUG_DNS, "Resolver: A %s for %s",
888 ircd_ntoa((char*) hp->h_addr_list[addr_count - 1]), hostbuf));
890 current += rd_length;
894 n = dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
903 * no more answers left
908 * This comment is based on analysis by Shadowfax, Wohali and johan,
909 * not me. (Dianora) I am only commenting it.
911 * dn_expand is guaranteed to not return more than sizeof(hostbuf)
912 * but do all implementations of dn_expand also guarantee
913 * buffer is terminated with null byte? Lets not take chances.
916 hostbuf[HOSTLEN] = '\0';
917 current += (size_t) n;
919 Debug((DEBUG_DNS, "Resolver: PTR %s", hostbuf));
921 * copy the returned hostname into the host name
922 * ignore duplicate ptr records
925 strcpy(name, hostbuf);
927 name += strlen(name) + 1;
932 Debug((DEBUG_DNS, "Resolver: CNAME %s", hostbuf));
933 if (++alias_count < RES_MAXALIASES) {
934 ircd_strncpy(name, hostbuf, endp - name);
937 name += strlen(name) + 1;
939 current += rd_length;
943 Debug((DEBUG_DNS,"Resolver: proc_answer type: %d for: %s", type, hostbuf));
951 * resolver_read - read a dns reply from the nameserver and process it.
952 * return 0 if nothing was read from the socket, otherwise return 1
954 int resolver_read(void)
956 u_char buf[sizeof(HEADER) + MAXPACKET];
958 struct ResRequest* request = 0;
959 struct CacheEntry* cp = 0;
961 int answer_count = 0;
962 struct sockaddr_in sin;
964 Debug((DEBUG_DNS, "Resolver: read"));
965 if (IO_SUCCESS != os_recvfrom_nonb(ResolverFileDescriptor,
966 buf, sizeof(buf), &rc, &sin)) {
969 if (rc < sizeof(HEADER)) {
970 Debug((DEBUG_DNS, "Resolver: short reply %d: %s", rc,
971 (strerror(errno)) ? strerror(errno) : "Unknown"));
975 * convert DNS reply reader from Network byte order to CPU byte order.
977 header = (HEADER*) buf;
978 /* header->id = ntohs(header->id); */
979 header->ancount = ntohs(header->ancount);
980 header->qdcount = ntohs(header->qdcount);
981 header->nscount = ntohs(header->nscount);
982 header->arcount = ntohs(header->arcount);
985 * response for an id which we have already received an answer for
986 * just ignore this response.
988 if (0 == (request = find_id(header->id))) {
989 Debug((DEBUG_DNS, "Resolver: can't find request id: %d", header->id));
993 * check against possibly fake replies
995 if (!res_ourserver(&_res, &sin)) {
996 Debug((DEBUG_DNS, "Resolver: fake reply from: %s",
997 (const char*) &sin.sin_addr));
1002 if ((header->rcode != NOERROR) || (header->ancount == 0)) {
1004 if (SERVFAIL == header->rcode)
1005 resend_query(request);
1008 * If a bad error was returned, we stop here and dont send
1009 * send any more (no retries granted).
1010 * Isomer: Perhaps we should return these error messages back to
1014 switch (header->rcode) {
1016 Debug((DEBUG_DNS, "Fatal DNS error: No Error"));
1019 Debug((DEBUG_DNS, "Fatal DNS error: Format Error"));
1022 Debug((DEBUG_DNS, "Fatal DNS error: Server Failure"));
1025 Debug((DEBUG_DNS, "DNS error: Non Existant Domain"));
1028 Debug((DEBUG_DNS, "Fatal DNS error: Not Implemented"));
1031 Debug((DEBUG_DNS, "Fatal DNS error: Query Refused"));
1034 Debug((DEBUG_DNS, "Unassigned fatal DNS error: %i", header->rcode));
1037 #endif /* DEBUGMODE */
1038 (*request->query.callback)(request->query.vptr, 0);
1039 rem_request(request);
1044 * If this fails there was an error decoding the received packet,
1045 * try it again and hope it works the next time.
1047 answer_count = proc_answer(request, header, buf, buf + rc);
1049 if (T_PTR == request->type) {
1050 struct DNSReply* reply = NULL;
1051 if (0 == request->he.h.h_name) {
1053 * got a PTR response with no name, something bogus is happening
1054 * don't bother trying again, the client address doesn't resolve
1056 (*request->query.callback)(request->query.vptr, reply);
1057 rem_request(request);
1060 Debug((DEBUG_DNS, "relookup %s <-> %s",
1061 request->he.h.h_name, ircd_ntoa((char*) &request->addr)));
1063 * Lookup the 'authoritive' name that we were given for the
1064 * ip#. By using this call rather than regenerating the
1065 * type we automatically gain the use of the cache with no
1068 reply = gethost_byname(request->he.h.h_name, &request->query);
1071 * If name wasn't found, a request has been queued and it will
1072 * be the last one queued. This is rather nasty way to keep
1073 * a host alias with the query. -avalon
1075 MyFree(requestListTail->he.buf);
1076 requestListTail->he.buf = request->he.buf;
1077 request->he.buf = 0;
1078 memcpy(&requestListTail->he.h, &request->he.h, sizeof(struct hostent));
1081 (*request->query.callback)(request->query.vptr, reply);
1082 rem_request(request);
1086 * got a name and address response, client resolved
1087 * XXX - Bug found here by Dianora -
1088 * make_cache() occasionally returns a NULL pointer when a
1089 * PTR returned a CNAME, cp was not checked before so the
1090 * callback was being called with a value of 0x2C != NULL.
1092 cp = make_cache(request);
1093 (*request->query.callback)(request->query.vptr,
1094 (cp) ? &cp->reply : 0);
1095 rem_request(request);
1098 else if (!request->sent) {
1100 * XXX - we got a response for a query we didn't send with a valid id?
1101 * this should never happen, bail here and leave the client unresolved
1103 (*request->query.callback)(request->query.vptr, 0);
1104 rem_request(request);
1110 * resolver_read_multiple - process up to count reads
1112 void resolver_read_multiple(int count)
1115 for ( ; i < count; ++i) {
1116 if (0 == resolver_read())
1121 static size_t calc_hostent_buffer_size(const struct hostent* hp)
1130 count += (strlen(hp->h_name) + 1);
1134 for (p = hp->h_aliases; *p; ++p)
1135 count += (strlen(*p) + 1 + sizeof(char*));
1137 * space for addresses
1139 for (p = hp->h_addr_list; *p; ++p)
1140 count += (hp->h_length + sizeof(char*));
1142 * space for 2 nulls to terminate h_aliases and h_addr_list
1144 count += (2 * sizeof(char*));
1150 * dup_hostent - Duplicate a hostent struct, allocate only enough memory for
1151 * the data we're putting in it.
1153 static void dup_hostent(aHostent* new_hp, struct hostent* hp)
1158 int alias_count = 0;
1160 size_t bytes_needed = 0;
1162 assert(0 != new_hp);
1165 /* how much buffer do we need? */
1166 bytes_needed += (strlen(hp->h_name) + 1);
1170 bytes_needed += (strlen(*pp++) + 1 + sizeof(char*));
1173 pp = hp->h_addr_list;
1175 bytes_needed += (hp->h_length + sizeof(char*));
1178 /* Reserve space for 2 nulls to terminate h_aliases and h_addr_list */
1179 bytes_needed += (2 * sizeof(char*));
1181 /* Allocate memory */
1182 new_hp->buf = (char*) MyMalloc(bytes_needed);
1184 new_hp->h.h_addrtype = hp->h_addrtype;
1185 new_hp->h.h_length = hp->h_length;
1187 /* first write the address list */
1188 pp = hp->h_addr_list;
1189 ap = new_hp->h.h_addr_list =
1190 (char**)(new_hp->buf + ((alias_count + 1) * sizeof(char*)));
1191 p = (char*)ap + ((addr_count + 1) * sizeof(char*));
1195 memcpy(p, *pp++, hp->h_length);
1199 /* next write the name */
1200 new_hp->h.h_name = p;
1201 strcpy(p, hp->h_name);
1202 p += (strlen(p) + 1);
1204 /* last write the alias list */
1206 ap = new_hp->h.h_aliases = (char**) new_hp->buf;
1210 p += (strlen(p) + 1);
1216 * update_hostent - Add records to a Hostent struct in place.
1218 static void update_hostent(aHostent* hp, char** addr, char** alias)
1223 int alias_count = 0;
1226 size_t bytes_needed = 0;
1228 if (!hp || !hp->buf)
1231 /* how much buffer do we need? */
1232 bytes_needed = strlen(hp->h.h_name) + 1;
1233 pp = hp->h.h_aliases;
1235 bytes_needed += (strlen(*pp++) + 1 + sizeof(char*));
1241 bytes_needed += (strlen(*pp++) + 1 + sizeof(char*));
1245 pp = hp->h.h_addr_list;
1247 bytes_needed += (hp->h.h_length + sizeof(char*));
1253 bytes_needed += (hp->h.h_length + sizeof(char*));
1257 /* Reserve space for 2 nulls to terminate h_aliases and h_addr_list */
1258 bytes_needed += 2 * sizeof(char*);
1260 /* Allocate memory */
1261 buf = (char*) MyMalloc(bytes_needed);
1264 /* first write the address list */
1265 pp = hp->h.h_addr_list;
1266 ap = hp->h.h_addr_list =
1267 (char**)(buf + ((alias_count + 1) * sizeof(char*)));
1268 p = (char*)ap + ((addr_count + 1) * sizeof(char*));
1270 memcpy(p, *pp++, hp->h.h_length);
1272 p += hp->h.h_length;
1276 memcpy(p, *addr++, hp->h.h_length);
1278 p += hp->h.h_length;
1283 /* next write the name */
1284 strcpy(p, hp->h.h_name);
1286 p += (strlen(p) + 1);
1288 /* last write the alias list */
1289 pp = hp->h.h_aliases;
1290 ap = hp->h.h_aliases = (char**) buf;
1294 p += (strlen(p) + 1);
1298 strcpy(p, *alias++);
1300 p += (strlen(p) + 1);
1304 /* release the old buffer */
1311 * hash_number - IP address hash function
1313 static int hash_number(const unsigned char* ip)
1315 /* could use loop but slower */
1317 const u_char* p = (const u_char*) ip;
1322 hashv += hashv + *p++;
1323 hashv += hashv + *p++;
1324 hashv += hashv + *p;
1325 hashv %= ARES_CACSIZE;
1330 * hash_name - hostname hash function
1332 static int hash_name(const char* name)
1334 unsigned int hashv = 0;
1335 const u_char* p = (const u_char*) name;
1339 for (; *p && *p != '.'; ++p)
1341 hashv %= ARES_CACSIZE;
1346 * add_to_cache - Add a new cache item to the queue and hash table.
1348 static struct CacheEntry* add_to_cache(struct CacheEntry* ocp)
1354 ocp->list_next = cacheTop;
1357 hashv = hash_name(ocp->he.h.h_name);
1359 ocp->hname_next = hashtable[hashv].name_list;
1360 hashtable[hashv].name_list = ocp;
1362 hashv = hash_number(ocp->he.h.h_addr);
1364 ocp->hnum_next = hashtable[hashv].num_list;
1365 hashtable[hashv].num_list = ocp;
1368 * LRU deletion of excessive cache entries.
1370 if (++cachedCount > MAXCACHED) {
1371 struct CacheEntry* cp;
1372 struct CacheEntry* cp_next;
1373 for (cp = ocp->list_next; cp; cp = cp_next) {
1374 cp_next = cp->list_next;
1383 * update_list - does not alter the cache structure passed. It is assumed that
1384 * it already contains the correct expire time, if it is a new entry. Old
1385 * entries have the expirey time updated.
1387 static void update_list(struct ResRequest* request, struct CacheEntry* cachep)
1390 struct CacheEntry** cpp;
1392 struct CacheEntry* cp = cachep;
1398 char* addrs[RES_MAXADDRS + 1];
1399 char* aliases[RES_MAXALIASES + 1];
1402 * search for the new cache item in the cache list by hostname.
1403 * If found, move the entry to the top of the list and return.
1405 ++cainfo.ca_updates;
1407 for (cpp = &cacheTop; *cpp; cpp = &((*cpp)->list_next)) {
1413 *cpp = cp->list_next;
1414 cp->list_next = cacheTop;
1421 * Compare the cache entry against the new record. Add any
1422 * previously missing names for this entry.
1426 for (i = 0, s = request->he.h.h_name; s; s = request->he.h.h_aliases[i++]) {
1427 for (j = 0, t = cp->he.h.h_name; t; t = cp->he.h.h_aliases[j++]) {
1428 if (0 == ircd_strcmp(t, s))
1437 * Do the same again for IP#'s.
1441 for (i = 0; (s = request->he.h.h_addr_list[i]); i++) {
1442 for (j = 0; (t = cp->he.h.h_addr_list[j]); j++) {
1443 if (!memcmp(t, s, sizeof(struct in_addr)))
1451 if (*addrs || *aliases)
1452 update_hostent(&cp->he, addrs, aliases);
1456 * find_cache_name - find name in nameserver cache
1458 static struct CacheEntry* find_cache_name(const char* name)
1460 struct CacheEntry* cp;
1466 hashv = hash_name(name);
1468 cp = hashtable[hashv].name_list;
1470 for (; cp; cp = cp->hname_next) {
1471 for (i = 0, s = cp->he.h.h_name; s; s = cp->he.h.h_aliases[i++]) {
1472 if (0 == ircd_strcmp(s, name)) {
1473 ++cainfo.ca_na_hits;
1479 for (cp = cacheTop; cp; cp = cp->list_next) {
1481 * if no aliases or the hash value matches, we've already
1482 * done this entry and all possiblilities concerning it.
1484 if (!cp->he.h.h_name || hashv == hash_name(cp->he.h.h_name))
1486 for (i = 0, s = cp->he.h.h_aliases[i]; s; s = cp->he.h.h_aliases[++i]) {
1487 if (0 == ircd_strcmp(name, s)) {
1488 ++cainfo.ca_na_hits;
1497 * find_cache_number - find a cache entry by ip# and update its expire time
1499 static struct CacheEntry* find_cache_number(struct ResRequest* request,
1502 struct CacheEntry* cp;
1507 hashv = hash_number(addr);
1508 cp = hashtable[hashv].num_list;
1510 for (; cp; cp = cp->hnum_next) {
1511 for (i = 0; cp->he.h.h_addr_list[i]; ++i) {
1512 if (!memcmp(cp->he.h.h_addr_list[i], addr, sizeof(struct in_addr))) {
1513 ++cainfo.ca_nu_hits;
1518 for (cp = cacheTop; cp; cp = cp->list_next) {
1520 * single address entry...would have been done by hashed
1522 * if the first IP# has the same hashnumber as the IP# we
1523 * are looking for, its been done already.
1525 if (!cp->he.h.h_addr_list[1] ||
1526 hashv == hash_number(cp->he.h.h_addr_list[0]))
1528 for (i = 1; cp->he.h.h_addr_list[i]; ++i) {
1529 if (!memcmp(cp->he.h.h_addr_list[i], addr, sizeof(struct in_addr))) {
1530 ++cainfo.ca_nu_hits;
1538 static struct CacheEntry* make_cache(struct ResRequest* request)
1540 struct CacheEntry* cp;
1543 assert(0 != request);
1545 hp = &request->he.h;
1547 * shouldn't happen but it just might...
1549 assert(0 != hp->h_name);
1550 assert(0 != hp->h_addr_list[0]);
1551 if (!hp->h_name || !hp->h_addr_list[0])
1554 * Make cache entry. First check to see if the cache already exists
1555 * and if so, return a pointer to it.
1557 for (i = 0; hp->h_addr_list[i]; ++i) {
1558 if ((cp = find_cache_number(request, hp->h_addr_list[i]))) {
1559 update_list(request, cp);
1564 * a matching entry wasnt found in the cache so go and make one up.
1566 cp = (struct CacheEntry*) MyMalloc(sizeof(struct CacheEntry));
1569 memset(cp, 0, sizeof(struct CacheEntry));
1570 dup_hostent(&cp->he, hp);
1571 cp->reply.hp = &cp->he.h;
1573 * hmmm... we could time out the cache after 10 minutes regardless
1574 * would that be reasonable since we don't save the reply?
1576 if (request->ttl < AR_TTL) {
1577 ++reinfo.re_shortttl;
1581 cp->ttl = request->ttl;
1582 cp->expireat = CurrentTime + cp->ttl;
1583 return add_to_cache(cp);
1587 * rem_cache - delete a cache entry from the cache structures
1588 * and lists and return all memory used for the cache back to the memory pool.
1590 static void rem_cache(struct CacheEntry* ocp)
1592 struct CacheEntry** cp;
1598 if (0 < ocp->reply.ref_count) {
1599 if (ocp->expireat < CurrentTime) {
1600 ocp->expireat = CurrentTime + AR_TTL;
1601 Debug((DEBUG_DNS, "Resolver: referenced cache entry not removed for: %s",
1607 * remove cache entry from linked list
1609 for (cp = &cacheTop; *cp; cp = &((*cp)->list_next)) {
1611 *cp = ocp->list_next;
1617 * remove cache entry from hashed name list
1619 assert(0 != hp->h_name);
1620 hashv = hash_name(hp->h_name);
1622 for (cp = &hashtable[hashv].name_list; *cp; cp = &((*cp)->hname_next)) {
1624 *cp = ocp->hname_next;
1629 * remove cache entry from hashed number list
1631 hashv = hash_number(hp->h_addr);
1634 for (cp = &hashtable[hashv].num_list; *cp; cp = &((*cp)->hnum_next)) {
1636 *cp = ocp->hnum_next;
1641 * free memory used to hold the various host names and the array
1642 * of alias pointers.
1644 MyFree(ocp->he.buf);
1650 void flush_resolver_cache(void)
1653 * stubbed - iterate cache and remove everything that isn't referenced
1658 * m_dns - dns status query
1660 int m_dns(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1662 #if !defined(NDEBUG)
1663 struct CacheEntry* cp;
1667 if (parv[1] && *parv[1] == 'l') {
1668 for(cp = cacheTop; cp; cp = cp->list_next) {
1670 sendto_one(sptr, "NOTICE %s :Ex %d ttl %d host %s(%s)",
1671 parv[0], cp->expireat - CurrentTime, cp->ttl,
1672 hp->h_name, ircd_ntoa(hp->h_addr));
1673 for (i = 0; hp->h_aliases[i]; i++)
1674 sendto_one(sptr,"NOTICE %s : %s = %s (CN)",
1675 parv[0], hp->h_name, hp->h_aliases[i]);
1676 for (i = 1; hp->h_addr_list[i]; i++)
1677 sendto_one(sptr,"NOTICE %s : %s = %s (IP)",
1678 parv[0], hp->h_name, ircd_ntoa(hp->h_addr_list[i]));
1682 if (parv[1] && *parv[1] == 'd') {
1683 sendto_one(sptr, "NOTICE %s :ResolverFileDescriptor = %d",
1684 parv[0], ResolverFileDescriptor);
1687 sendto_one(sptr,"NOTICE %s :Ca %d Cd %d Ce %d Cl %d Ch %d:%d Cu %d",
1689 cainfo.ca_adds, cainfo.ca_dels, cainfo.ca_expires,
1690 cainfo.ca_lookups, cainfo.ca_na_hits, cainfo.ca_nu_hits,
1693 sendto_one(sptr,"NOTICE %s :Re %d Rl %d/%d Rp %d Rq %d",
1694 sptr->name, reinfo.re_errors, reinfo.re_nu_look,
1695 reinfo.re_na_look, reinfo.re_replies, reinfo.re_requests);
1696 sendto_one(sptr,"NOTICE %s :Ru %d Rsh %d Rs %d(%d) Rt %d", sptr->name,
1697 reinfo.re_unkrep, reinfo.re_shortttl, reinfo.re_sent,
1698 reinfo.re_resends, reinfo.re_timeouts);
1703 size_t cres_mem(struct Client* sptr)
1705 struct CacheEntry* entry;
1706 struct ResRequest* request;
1707 size_t cache_mem = 0;
1708 size_t request_mem = 0;
1709 int cache_count = 0;
1710 int request_count = 0;
1712 for (entry = cacheTop; entry; entry = entry->list_next) {
1713 cache_mem += sizeof(struct CacheEntry);
1714 cache_mem += calc_hostent_buffer_size(&entry->he.h);
1717 for (request = requestListHead; request; request = request->next) {
1718 request_mem += sizeof(struct ResRequest);
1720 request_mem += strlen(request->name) + 1;
1721 if (request->he.buf)
1722 request_mem += MAXGETHOSTLEN + 1;
1725 if (cachedCount != cache_count) {
1727 ":%s %d %s :Resolver: cache count mismatch: %d != %d",
1728 me.name, RPL_STATSDEBUG, sptr->name, cachedCount, cache_count);
1729 assert(cachedCount == cache_count);
1731 sendto_one(sptr, ":%s %d %s :Resolver: cache %d(%d) requests %d(%d)",
1732 me.name, RPL_STATSDEBUG, sptr->name, cache_count, cache_mem,
1733 request_count, request_mem);
1734 return cache_mem + request_mem;