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.
7 * The authors takes no responsibility for any damage or loss
8 * of property which results from the use of this software.
10 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
11 * added callbacks and reference counting of returned hostents.
12 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
14 * This was all needlessly complicated for irc. Simplified. No more hostent
15 * All we really care about is the IP -> hostname mappings. Thats all.
17 * Apr 28, 2003 --cryogen and Dianora
19 * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support
20 * removed, various robustness fixes
22 * 2006 --jilles and nenolod
24 * Resend queries to other servers if the DNS server replies with an error or
25 * an invalid response. Also, avoid servers that return errors or invalid
28 * October 2012 --mr_flea
30 * ircd-ratbox changes for random IDs merged back in.
32 * January 2016 --kaniini
41 #error this code needs to be able to address individual octets
44 static PF res_readreply
;
46 #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
47 #define RES_MAXALIASES 35 /* maximum aliases allowed */
48 #define RES_MAXADDRS 35 /* maximum addresses allowed */
49 #define AR_TTL 600 /* TTL in seconds for dns cache entries */
51 /* RFC 1104/1105 wasn't very helpful about what these fields
52 * should be named, so for now, we'll just name them this way.
53 * we probably should look at what named calls them or something.
55 #define TYPE_SIZE (size_t)2
56 #define CLASS_SIZE (size_t)2
57 #define TTL_SIZE (size_t)4
58 #define RDLENGTH_SIZE (size_t)2
59 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
62 struct in6_addr ipv6_addr
;
64 struct in_addr ipv4_addr
;
72 char queryname
[IRCD_RES_HOSTLEN
+ 1]; /* name currently being queried */
73 char retries
; /* retry counter */
74 char sends
; /* number of sends (>1 means resent) */
77 int lastns
; /* index of last server sent to */
78 struct rb_sockaddr_storage addr
;
80 struct DNSQuery
*query
; /* query callback for this request */
83 static rb_fde_t
*res_fd
;
84 static rb_dlink_list request_list
= { NULL
, NULL
, 0 };
85 static int ns_failure_count
[IRCD_MAXNS
]; /* timeouts and invalid/failed replies */
87 static void rem_request(struct reslist
*request
);
88 static struct reslist
*make_request(struct DNSQuery
*query
);
89 static void gethost_byname_type_fqdn(const char *name
, struct DNSQuery
*query
,
91 static void do_query_name(struct DNSQuery
*query
, const char *name
, struct reslist
*request
, int);
92 static void do_query_number(struct DNSQuery
*query
, const struct rb_sockaddr_storage
*,
93 struct reslist
*request
);
94 static void query_name(struct reslist
*request
);
95 static int send_res_msg(const char *buf
, int len
, int count
);
96 static void resend_query(struct reslist
*request
);
97 static int check_question(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
);
98 static int proc_answer(struct reslist
*request
, HEADER
* header
, char *, char *);
99 static struct reslist
*find_id(int id
);
100 static struct DNSReply
*make_dnsreply(struct reslist
*request
);
101 static uint16_t generate_random_id(void);
107 #define RES_MIN(a, b) ((a) < (b) ? (a) : (b))
112 * looks up "inp" in irc_nsaddr_list[]
114 * server ID or -1 for not found
116 * paul vixie, 29may94
117 * revised for ircd, cryogen(stu) may03
118 * slightly modified for charybdis, mr_flea oct12
121 res_ourserver(const struct rb_sockaddr_storage
*inp
)
124 const struct sockaddr_in6
*v6
;
125 const struct sockaddr_in6
*v6in
= (const struct sockaddr_in6
*)inp
;
127 const struct sockaddr_in
*v4
;
128 const struct sockaddr_in
*v4in
= (const struct sockaddr_in
*)inp
;
131 for(ns
= 0; ns
< irc_nscount
; ns
++)
133 const struct rb_sockaddr_storage
*srv
= &irc_nsaddr_list
[ns
];
135 v6
= (const struct sockaddr_in6
*)srv
;
137 v4
= (const struct sockaddr_in
*)srv
;
139 /* could probably just memcmp(srv, inp, srv.ss_len) here
140 * but we'll air on the side of caution - stu
142 switch (GET_SS_FAMILY(srv
))
146 if(GET_SS_FAMILY(srv
) == GET_SS_FAMILY(inp
))
147 if(v6
->sin6_port
== v6in
->sin6_port
)
148 if((memcmp(&v6
->sin6_addr
.s6_addr
, &v6in
->sin6_addr
.s6_addr
,
149 sizeof(struct in6_addr
)) == 0) ||
150 (memcmp(&v6
->sin6_addr
.s6_addr
, &in6addr_any
,
151 sizeof(struct in6_addr
)) == 0))
156 if(GET_SS_FAMILY(srv
) == GET_SS_FAMILY(inp
))
157 if(v4
->sin_port
== v4in
->sin_port
)
158 if((v4
->sin_addr
.s_addr
== INADDR_ANY
)
159 || (v4
->sin_addr
.s_addr
== v4in
->sin_addr
.s_addr
))
171 * timeout_query_list - Remove queries from the list which have been
172 * there too long without being resolved.
174 static time_t timeout_query_list(time_t now
)
177 rb_dlink_node
*next_ptr
;
178 struct reslist
*request
;
179 time_t next_time
= 0;
182 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, request_list
.head
)
185 timeout
= request
->sentat
+ request
->timeout
;
189 ns_failure_count
[request
->lastns
]++;
190 request
->sentat
= now
;
191 request
->timeout
+= request
->timeout
;
192 resend_query(request
);
195 if ((next_time
== 0) || timeout
< next_time
)
201 return (next_time
> now
) ? next_time
: (now
+ AR_TTL
);
205 * timeout_resolver - check request list
207 static void timeout_resolver(void *notused
)
209 timeout_query_list(rb_current_time());
212 static struct ev_entry
*timeout_resolver_ev
= NULL
;
215 * start_resolver - do everything we need to read the resolv.conf file
216 * and initialize the resolver file descriptor if needed
218 static void start_resolver(void)
223 for (i
= 0; i
< irc_nscount
; i
++)
224 ns_failure_count
[i
] = 0;
228 if ((res_fd
= rb_socket(GET_SS_FAMILY(&irc_nsaddr_list
[0]), SOCK_DGRAM
, 0,
229 "UDP resolver socket")) == NULL
)
232 /* At the moment, the resolver FD data is global .. */
233 rb_setselect(res_fd
, RB_SELECT_READ
, res_readreply
, NULL
);
234 timeout_resolver_ev
= rb_event_add("timeout_resolver", timeout_resolver
, NULL
, 1);
239 * init_resolver - initialize resolver and resolver library
241 void init_resolver(void)
244 srand48(rb_current_time());
250 * restart_resolver - reread resolv.conf, reopen socket
252 void restart_resolver(void)
256 rb_event_delete(timeout_resolver_ev
); /* -ddosen */
261 * add_local_domain - Add the domain to hostname, if it is missing
262 * (as suggested by eps@TOASTER.SFSU.EDU)
264 void add_local_domain(char *hname
, size_t size
)
266 /* try to fix up unqualified names */
267 if (strchr(hname
, '.') == NULL
)
271 size_t len
= strlen(hname
);
273 if ((strlen(irc_domain
) + len
+ 2) < size
)
276 strcpy(hname
+ len
, irc_domain
);
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.
287 static void rem_request(struct reslist
*request
)
289 rb_dlinkDelete(&request
->node
, &request_list
);
290 rb_free(request
->name
);
295 * make_request - Create a DNS request record for the server.
297 static struct reslist
*make_request(struct DNSQuery
*query
)
299 struct reslist
*request
= rb_malloc(sizeof(struct reslist
));
301 request
->sentat
= rb_current_time();
302 request
->retries
= 3;
303 request
->timeout
= 4; /* start at 4 and exponential inc. */
304 request
->query
= query
;
307 * generate a unique id
308 * NOTE: we don't have to worry about converting this to and from
309 * network byte order, the nameserver does not interpret this value
310 * and returns it unchanged
312 * we generate an id per request now (instead of per send) to allow
313 * late replies to be used.
315 request
->id
= generate_random_id();
317 rb_dlinkAdd(request
, &request
->node
, &request_list
);
323 * retryfreq - determine how many queries to wait before resending
324 * if there have been that many consecutive timeouts
326 static int retryfreq(int timeouts
)
344 * send_res_msg - sends msg to a nameserver.
345 * This should reflect /etc/resolv.conf.
346 * Returns number of nameserver successfully sent to
347 * or -1 if no successful sends.
349 static int send_res_msg(const char *msg
, int len
, int rcount
)
356 /* First try a nameserver that seems to work.
357 * Every once in a while, try a possibly broken one to check
358 * if it is working again.
360 for (i
= 0; i
< irc_nscount
; i
++)
362 ns
= (i
+ rcount
- 1) % irc_nscount
;
363 if (ns_failure_count
[ns
] && retrycnt
% retryfreq(ns_failure_count
[ns
]))
365 if (sendto(rb_get_fd(res_fd
), msg
, len
, 0,
366 (struct sockaddr
*)&(irc_nsaddr_list
[ns
]),
367 GET_SS_LEN(&irc_nsaddr_list
[ns
])) == len
)
371 /* No known working nameservers, try some broken one. */
372 for (i
= 0; i
< irc_nscount
; i
++)
374 ns
= (i
+ rcount
- 1) % irc_nscount
;
375 if (!ns_failure_count
[ns
])
377 if (sendto(rb_get_fd(res_fd
), msg
, len
, 0,
378 (struct sockaddr
*)&(irc_nsaddr_list
[ns
]),
379 GET_SS_LEN(&irc_nsaddr_list
[ns
])) == len
)
387 * find_id - find a dns request id (id is determined by dn_mkquery)
389 static struct reslist
*find_id(int id
)
392 struct reslist
*request
;
394 RB_DLINK_FOREACH(ptr
, request_list
.head
)
398 if (request
->id
== id
)
406 generate_random_id(void)
412 rb_get_random(&id
, sizeof(id
));
421 * gethost_byname_type - get host address from name, adding domain if needed
423 void gethost_byname_type(const char *name
, struct DNSQuery
*query
, int type
)
425 char fqdn
[IRCD_RES_HOSTLEN
+ 1];
428 rb_strlcpy(fqdn
, name
, sizeof fqdn
);
429 add_local_domain(fqdn
, IRCD_RES_HOSTLEN
);
430 gethost_byname_type_fqdn(fqdn
, query
, type
);
434 * gethost_byname_type_fqdn - get host address from fqdn
436 static void gethost_byname_type_fqdn(const char *name
, struct DNSQuery
*query
,
440 do_query_name(query
, name
, NULL
, type
);
444 * gethost_byaddr - get host name from address
446 void gethost_byaddr(const struct rb_sockaddr_storage
*addr
, struct DNSQuery
*query
)
448 do_query_number(query
, addr
, NULL
);
452 * do_query_name - nameserver lookup name
454 static void do_query_name(struct DNSQuery
*query
, const char *name
, struct reslist
*request
,
459 request
= make_request(query
);
460 request
->name
= rb_strdup(name
);
463 rb_strlcpy(request
->queryname
, name
, sizeof(request
->queryname
));
464 request
->type
= type
;
469 * do_query_number - Use this to do reverse IP# lookups.
471 static void do_query_number(struct DNSQuery
*query
, const struct rb_sockaddr_storage
*addr
,
472 struct reslist
*request
)
474 const unsigned char *cp
;
478 request
= make_request(query
);
479 memcpy(&request
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
480 request
->name
= (char *)rb_malloc(IRCD_RES_HOSTLEN
+ 1);
483 if (GET_SS_FAMILY(addr
) == AF_INET
)
485 const struct sockaddr_in
*v4
= (const struct sockaddr_in
*)addr
;
486 cp
= (const unsigned char *)&v4
->sin_addr
.s_addr
;
488 sprintf(request
->queryname
, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp
[3]),
489 (unsigned int)(cp
[2]), (unsigned int)(cp
[1]), (unsigned int)(cp
[0]));
492 else if (GET_SS_FAMILY(addr
) == AF_INET6
)
494 const struct sockaddr_in6
*v6
= (const struct sockaddr_in6
*)addr
;
495 cp
= (const unsigned char *)&v6
->sin6_addr
.s6_addr
;
497 (void)sprintf(request
->queryname
, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
498 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
499 (unsigned int)(cp
[15] & 0xf), (unsigned int)(cp
[15] >> 4),
500 (unsigned int)(cp
[14] & 0xf), (unsigned int)(cp
[14] >> 4),
501 (unsigned int)(cp
[13] & 0xf), (unsigned int)(cp
[13] >> 4),
502 (unsigned int)(cp
[12] & 0xf), (unsigned int)(cp
[12] >> 4),
503 (unsigned int)(cp
[11] & 0xf), (unsigned int)(cp
[11] >> 4),
504 (unsigned int)(cp
[10] & 0xf), (unsigned int)(cp
[10] >> 4),
505 (unsigned int)(cp
[9] & 0xf), (unsigned int)(cp
[9] >> 4),
506 (unsigned int)(cp
[8] & 0xf), (unsigned int)(cp
[8] >> 4),
507 (unsigned int)(cp
[7] & 0xf), (unsigned int)(cp
[7] >> 4),
508 (unsigned int)(cp
[6] & 0xf), (unsigned int)(cp
[6] >> 4),
509 (unsigned int)(cp
[5] & 0xf), (unsigned int)(cp
[5] >> 4),
510 (unsigned int)(cp
[4] & 0xf), (unsigned int)(cp
[4] >> 4),
511 (unsigned int)(cp
[3] & 0xf), (unsigned int)(cp
[3] >> 4),
512 (unsigned int)(cp
[2] & 0xf), (unsigned int)(cp
[2] >> 4),
513 (unsigned int)(cp
[1] & 0xf), (unsigned int)(cp
[1] >> 4),
514 (unsigned int)(cp
[0] & 0xf), (unsigned int)(cp
[0] >> 4));
518 request
->type
= T_PTR
;
523 * query_name - generate a query based on class, type and name.
525 static void query_name(struct reslist
*request
)
531 memset(buf
, 0, sizeof(buf
));
534 irc_res_mkquery(request
->queryname
, C_IN
, request
->type
, (unsigned char *)buf
, sizeof(buf
))) > 0)
536 HEADER
*header
= (HEADER
*)(void *)buf
;
537 header
->id
= request
->id
;
540 ns
= send_res_msg(buf
, request_len
, request
->sends
);
542 request
->lastns
= ns
;
546 static void resend_query(struct reslist
*request
)
548 if (--request
->retries
<= 0)
550 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
551 rem_request(request
);
555 switch (request
->type
)
558 do_query_number(NULL
, &request
->addr
, request
);
564 do_query_name(NULL
, request
->name
, request
, request
->type
);
572 * check_question - check if the reply really belongs to the
573 * name we queried (to guard against late replies from previous
574 * queries with the same id).
576 static int check_question(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
)
578 char hostbuf
[IRCD_RES_HOSTLEN
+ 1]; /* working buffer */
579 unsigned char *current
; /* current position in buf */
580 int n
; /* temp count */
582 current
= (unsigned char *)buf
+ sizeof(HEADER
);
583 if (header
->qdcount
!= 1)
585 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
, hostbuf
,
589 if (strcasecmp(hostbuf
, request
->queryname
))
595 * proc_answer - process name server reply
597 static int proc_answer(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
)
599 char hostbuf
[IRCD_RES_HOSTLEN
+ 100]; /* working buffer */
600 unsigned char *current
; /* current position in buf */
601 int type
; /* answer type */
602 int n
; /* temp count */
604 struct sockaddr_in
*v4
; /* conversion */
606 struct sockaddr_in6
*v6
;
608 current
= (unsigned char *)buf
+ sizeof(HEADER
);
610 for (; header
->qdcount
> 0; --header
->qdcount
)
612 if ((n
= irc_dn_skipname(current
, (unsigned char *)eob
)) < 0)
615 current
+= (size_t) n
+ QFIXEDSZ
;
619 * process each answer sent to us blech.
621 while (header
->ancount
> 0 && (char *)current
< eob
)
625 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
, hostbuf
,
638 * no more answers left
643 hostbuf
[IRCD_RES_HOSTLEN
] = '\0';
645 /* With Address arithmetic you have to be very anal
646 * this code was not working on alpha due to that
647 * (spotted by rodder/jailbird/dianora)
649 current
+= (size_t) n
;
651 if (!(((char *)current
+ ANSWER_FIXED_SIZE
) < eob
))
654 type
= irc_ns_get16(current
);
655 current
+= TYPE_SIZE
;
657 (void) irc_ns_get16(current
);
658 current
+= CLASS_SIZE
;
660 request
->ttl
= irc_ns_get32(current
);
663 rd_length
= irc_ns_get16(current
);
664 current
+= RDLENGTH_SIZE
;
667 * Wait to set request->type until we verify this structure
672 if (request
->type
!= T_A
)
676 * check for invalid rd_length or too many addresses
678 if (rd_length
!= sizeof(struct in_addr
))
680 v4
= (struct sockaddr_in
*)&request
->addr
;
681 SET_SS_LEN(&request
->addr
, sizeof(struct sockaddr_in
));
682 v4
->sin_family
= AF_INET
;
683 memcpy(&v4
->sin_addr
, current
, sizeof(struct in_addr
));
688 if (request
->type
!= T_AAAA
)
690 if (rd_length
!= sizeof(struct in6_addr
))
692 SET_SS_LEN(&request
->addr
, sizeof(struct sockaddr_in6
));
693 v6
= (struct sockaddr_in6
*)&request
->addr
;
694 v6
->sin6_family
= AF_INET6
;
695 memcpy(&v6
->sin6_addr
, current
, sizeof(struct in6_addr
));
700 if (request
->type
!= T_PTR
)
702 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
,
703 hostbuf
, sizeof(hostbuf
));
705 return (0); /* broken message */
707 return (0); /* no more answers left */
709 rb_strlcpy(request
->name
, hostbuf
, IRCD_RES_HOSTLEN
+ 1);
714 /* real answer will follow */
715 current
+= rd_length
;
727 * res_read_single_reply - read a dns reply from the nameserver and process it.
728 * Return value: 1 if a packet was read, 0 otherwise
730 static int res_read_single_reply(rb_fde_t
*F
, void *data
)
732 char buf
[sizeof(HEADER
) + MAXPACKET
]
733 /* Sparc and alpha need 16bit-alignment for accessing header->id
734 * (which is uint16_t). Because of the header = (HEADER*) buf;
735 * lateron, this is neeeded. --FaUl
737 #if defined(__sparc__) || defined(__alpha__)
738 __attribute__ ((aligned(16)))
742 struct reslist
*request
= NULL
;
743 struct DNSReply
*reply
= NULL
;
746 socklen_t len
= sizeof(struct rb_sockaddr_storage
);
747 struct rb_sockaddr_storage lsin
;
750 rc
= recvfrom(rb_get_fd(F
), buf
, sizeof(buf
), 0, (struct sockaddr
*)&lsin
, &len
);
753 if (rc
== 0 || rc
== -1)
757 if (rc
<= (int)(sizeof(HEADER
)))
761 * convert DNS reply reader from Network byte order to CPU byte order.
763 header
= (HEADER
*)(void *)buf
;
764 header
->ancount
= ntohs(header
->ancount
);
765 header
->qdcount
= ntohs(header
->qdcount
);
766 header
->nscount
= ntohs(header
->nscount
);
767 header
->arcount
= ntohs(header
->arcount
);
770 * response for an id which we have already received an answer for
771 * just ignore this response.
773 if (0 == (request
= find_id(header
->id
)))
777 * check against possibly fake replies
779 ns
= res_ourserver(&lsin
);
783 if (ns
!= request
->lastns
)
786 * We'll accept the late reply, but penalize it a little more to make
787 * sure a laggy server doesn't end up favored.
789 ns_failure_count
[ns
] += 3;
793 if (!check_question(request
, header
, buf
, buf
+ rc
))
796 if ((header
->rcode
!= NO_ERRORS
) || (header
->ancount
== 0))
799 * RFC 2136 states that in the event of a server returning SERVFAIL
800 * or NOTIMP, the request should be resent to the next server.
801 * Additionally, if the server refuses our query, resend it as well.
804 if (SERVFAIL
== header
->rcode
|| NOTIMP
== header
->rcode
||
805 REFUSED
== header
->rcode
)
807 ns_failure_count
[ns
]++;
808 resend_query(request
);
813 * Either a fatal error was returned or no answer. Cancel the
816 if (NXDOMAIN
== header
->rcode
)
818 /* If the rcode is NXDOMAIN, treat it as a good response. */
819 ns_failure_count
[ns
] /= 4;
821 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
822 rem_request(request
);
827 * If this fails there was an error decoding the received packet.
830 answer_count
= proc_answer(request
, header
, buf
, buf
+ rc
);
834 if (request
->type
== T_PTR
)
836 if (request
->name
== NULL
)
839 * Got a PTR response with no name, something strange is
840 * happening. Try another DNS server.
842 ns_failure_count
[ns
]++;
843 resend_query(request
);
848 * Lookup the 'authoritative' name that we were given for the
852 if (GET_SS_FAMILY(&request
->addr
) == AF_INET6
)
853 gethost_byname_type_fqdn(request
->name
, request
->query
, T_AAAA
);
856 gethost_byname_type_fqdn(request
->name
, request
->query
, T_A
);
857 rem_request(request
);
862 * got a name and address response, client resolved
864 reply
= make_dnsreply(request
);
865 (*request
->query
->callback
) (request
->query
->ptr
, reply
);
867 rem_request(request
);
870 ns_failure_count
[ns
] /= 4;
874 /* Invalid or corrupt reply - try another resolver. */
875 ns_failure_count
[ns
]++;
876 resend_query(request
);
882 res_readreply(rb_fde_t
*F
, void *data
)
884 while (res_read_single_reply(F
, data
))
886 rb_setselect(F
, RB_SELECT_READ
, res_readreply
, NULL
);
889 static struct DNSReply
*
890 make_dnsreply(struct reslist
*request
)
893 lrb_assert(request
!= 0);
895 cp
= (struct DNSReply
*)rb_malloc(sizeof(struct DNSReply
));
897 cp
->h_name
= request
->name
;
898 memcpy(&cp
->addr
, &request
->addr
, sizeof(cp
->addr
));