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 * $Id: res.c 3301 2007-03-28 15:04:06Z jilles $
11 * from Hybrid Id: res.c 459 2006-02-12 22:21:37Z db $
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>)
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.
20 * Apr 28, 2003 --cryogen and Dianora
22 * DNS server flooding lessened, AAAA-or-A lookup removed, ip6.int support
23 * removed, various robustness fixes
25 * 2006 --jilles and nenolod
29 #include "ircd_defs.h"
36 #include "client.h" /* SNO_* */
39 #error this code needs to be able to address individual octets
42 static PF res_readreply
;
44 #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
45 #define RES_MAXALIASES 35 /* maximum aliases allowed */
46 #define RES_MAXADDRS 35 /* maximum addresses allowed */
47 #define AR_TTL 600 /* TTL in seconds for dns cache entries */
49 /* RFC 1104/1105 wasn't very helpful about what these fields
50 * should be named, so for now, we'll just name them this way.
51 * we probably should look at what named calls them or something.
53 #define TYPE_SIZE (size_t)2
54 #define CLASS_SIZE (size_t)2
55 #define TTL_SIZE (size_t)4
56 #define RDLENGTH_SIZE (size_t)2
57 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
63 int sent
; /* number of requests sent */
66 char queryname
[128]; /* name currently being queried */
67 char retries
; /* retry counter */
68 char sends
; /* number of sends (>1 means resent) */
69 char resend
; /* send flag. 0 == dont resend */
72 struct rb_sockaddr_storage addr
;
74 struct DNSQuery
*query
; /* query callback for this request */
77 static rb_fde_t
*res_fd
;
78 static rb_dlink_list request_list
= { NULL
, NULL
, 0 };
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
, const char *name
, struct reslist
*request
, int);
83 static void do_query_number(struct DNSQuery
*query
, const struct rb_sockaddr_storage
*,
84 struct reslist
*request
);
85 static void query_name(struct reslist
*request
);
86 static int send_res_msg(const char *buf
, int len
, int count
);
87 static void resend_query(struct reslist
*request
);
88 static int check_question(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
);
89 static int proc_answer(struct reslist
*request
, HEADER
* header
, char *, char *);
90 static struct reslist
*find_id(int id
);
91 static struct DNSReply
*make_dnsreply(struct reslist
*request
);
96 * looks up "inp" in irc_nsaddr_list[]
101 * paul vixie, 29may94
102 * revised for ircd, cryogen(stu) may03
104 static int res_ourserver(const struct rb_sockaddr_storage
*inp
)
107 const struct sockaddr_in6
*v6
;
108 const struct sockaddr_in6
*v6in
= (const struct sockaddr_in6
*)inp
;
110 const struct sockaddr_in
*v4
;
111 const struct sockaddr_in
*v4in
= (const struct sockaddr_in
*)inp
;
114 for (ns
= 0; ns
< irc_nscount
; ns
++)
116 const struct rb_sockaddr_storage
*srv
= &irc_nsaddr_list
[ns
];
118 v6
= (const struct sockaddr_in6
*)srv
;
120 v4
= (const struct sockaddr_in
*)srv
;
122 /* could probably just memcmp(srv, inp, srv.ss_len) here
123 * but we'll air on the side of caution - stu
125 switch (srv
->ss_family
)
129 if (srv
->ss_family
== inp
->ss_family
)
130 if (v6
->sin6_port
== v6in
->sin6_port
)
131 if ((memcmp(&v6
->sin6_addr
.s6_addr
, &v6in
->sin6_addr
.s6_addr
,
132 sizeof(struct in6_addr
)) == 0) ||
133 (memcmp(&v6
->sin6_addr
.s6_addr
, &in6addr_any
,
134 sizeof(struct in6_addr
)) == 0))
139 if (srv
->ss_family
== inp
->ss_family
)
140 if (v4
->sin_port
== v4in
->sin_port
)
141 if ((v4
->sin_addr
.s_addr
== INADDR_ANY
)
142 || (v4
->sin_addr
.s_addr
== v4in
->sin_addr
.s_addr
))
154 * timeout_query_list - Remove queries from the list which have been
155 * there too long without being resolved.
157 static time_t timeout_query_list(time_t now
)
160 rb_dlink_node
*next_ptr
;
161 struct reslist
*request
;
162 time_t next_time
= 0;
165 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, request_list
.head
)
168 timeout
= request
->sentat
+ request
->timeout
;
172 if (--request
->retries
<= 0)
174 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
175 rem_request(request
);
180 request
->sentat
= now
;
181 request
->timeout
+= request
->timeout
;
182 resend_query(request
);
186 if ((next_time
== 0) || timeout
< next_time
)
192 return (next_time
> now
) ? next_time
: (now
+ AR_TTL
);
196 * timeout_resolver - check request list
198 static void timeout_resolver(void *notused
)
200 timeout_query_list(rb_current_time());
203 static struct ev_entry
*timeout_resolver_ev
= NULL
;
206 * start_resolver - do everything we need to read the resolv.conf file
207 * and initialize the resolver file descriptor if needed
209 static void start_resolver(void)
215 if ((res_fd
= rb_socket(irc_nsaddr_list
[0].ss_family
, SOCK_DGRAM
, 0,
216 "UDP resolver socket")) == NULL
)
219 /* At the moment, the resolver FD data is global .. */
220 rb_setselect(res_fd
, RB_SELECT_READ
, res_readreply
, NULL
);
221 timeout_resolver_ev
= rb_event_add("timeout_resolver", timeout_resolver
, NULL
, 1);
226 * init_resolver - initialize resolver and resolver library
228 void init_resolver(void)
231 srand48(rb_current_time());
237 * restart_resolver - reread resolv.conf, reopen socket
239 void restart_resolver(void)
243 rb_event_delete(timeout_resolver_ev
); /* -ddosen */
248 * add_local_domain - Add the domain to hostname, if it is missing
249 * (as suggested by eps@TOASTER.SFSU.EDU)
251 void add_local_domain(char *hname
, size_t size
)
253 /* try to fix up unqualified names */
254 if (strchr(hname
, '.') == NULL
)
258 size_t len
= strlen(hname
);
260 if ((strlen(irc_domain
) + len
+ 2) < size
)
263 strcpy(hname
+ len
, irc_domain
);
270 * rem_request - remove a request from the list.
271 * This must also free any memory that has been allocated for
272 * temporary storage of DNS results.
274 static void rem_request(struct reslist
*request
)
276 rb_dlinkDelete(&request
->node
, &request_list
);
277 rb_free(request
->name
);
282 * make_request - Create a DNS request record for the server.
284 static struct reslist
*make_request(struct DNSQuery
*query
)
286 struct reslist
*request
= rb_malloc(sizeof(struct reslist
));
288 request
->sentat
= rb_current_time();
289 request
->retries
= 3;
291 request
->timeout
= 4; /* start at 4 and exponential inc. */
292 request
->query
= query
;
294 rb_dlinkAdd(request
, &request
->node
, &request_list
);
300 * delete_resolver_queries - cleanup outstanding queries
301 * for which there no longer exist clients or conf lines.
303 void delete_resolver_queries(const struct DNSQuery
*query
)
306 rb_dlink_node
*next_ptr
;
307 struct reslist
*request
;
309 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, request_list
.head
)
311 if ((request
= ptr
->data
) != NULL
)
313 if (query
== request
->query
)
314 rem_request(request
);
320 * send_res_msg - sends msg to all nameservers found in the "_res" structure.
321 * This should reflect /etc/resolv.conf. We will get responses
322 * which arent needed but is easier than checking to see if nameserver
323 * isnt present. Returns number of messages successfully sent to
324 * nameservers or -1 if no successful sends.
326 static int send_res_msg(const char *msg
, int len
, int rcount
)
330 int max_queries
= IRCD_MIN(irc_nscount
, rcount
);
332 /* RES_PRIMARY option is not implemented
333 * if (res.options & RES_PRIMARY || 0 == max_queries)
335 if (max_queries
== 0)
338 for (i
= 0; sent
< max_queries
&& i
< irc_nscount
; i
++)
340 if (sendto(rb_get_fd(res_fd
), msg
, len
, 0,
341 (struct sockaddr
*)&(irc_nsaddr_list
[i
]),
342 GET_SS_LEN(&irc_nsaddr_list
[i
])) == len
)
350 * find_id - find a dns request id (id is determined by dn_mkquery)
352 static struct reslist
*find_id(int id
)
355 struct reslist
*request
;
357 RB_DLINK_FOREACH(ptr
, request_list
.head
)
361 if (request
->id
== id
)
369 * gethost_byname_type - get host address from name
372 void gethost_byname_type(const char *name
, struct DNSQuery
*query
, int type
)
375 do_query_name(query
, name
, NULL
, type
);
379 * gethost_byaddr - get host name from address
381 void gethost_byaddr(const struct rb_sockaddr_storage
*addr
, struct DNSQuery
*query
)
383 do_query_number(query
, addr
, NULL
);
387 * do_query_name - nameserver lookup name
389 static void do_query_name(struct DNSQuery
*query
, const char *name
, struct reslist
*request
,
392 char host_name
[HOSTLEN
+ 1];
394 rb_strlcpy(host_name
, name
, HOSTLEN
+ 1);
395 add_local_domain(host_name
, HOSTLEN
);
399 request
= make_request(query
);
400 request
->name
= (char *)rb_malloc(strlen(host_name
) + 1);
401 strcpy(request
->name
, host_name
);
404 rb_strlcpy(request
->queryname
, host_name
, sizeof(request
->queryname
));
405 request
->type
= type
;
410 * do_query_number - Use this to do reverse IP# lookups.
412 static void do_query_number(struct DNSQuery
*query
, const struct rb_sockaddr_storage
*addr
,
413 struct reslist
*request
)
415 const unsigned char *cp
;
419 request
= make_request(query
);
420 memcpy(&request
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
421 request
->name
= (char *)rb_malloc(HOSTLEN
+ 1);
424 if (addr
->ss_family
== AF_INET
)
426 const struct sockaddr_in
*v4
= (const struct sockaddr_in
*)addr
;
427 cp
= (const unsigned char *)&v4
->sin_addr
.s_addr
;
429 rb_sprintf(request
->queryname
, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp
[3]),
430 (unsigned int)(cp
[2]), (unsigned int)(cp
[1]), (unsigned int)(cp
[0]));
433 else if (addr
->ss_family
== AF_INET6
)
435 const struct sockaddr_in6
*v6
= (const struct sockaddr_in6
*)addr
;
436 cp
= (const unsigned char *)&v6
->sin6_addr
.s6_addr
;
438 (void)sprintf(request
->queryname
, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
439 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
440 (unsigned int)(cp
[15] & 0xf), (unsigned int)(cp
[15] >> 4),
441 (unsigned int)(cp
[14] & 0xf), (unsigned int)(cp
[14] >> 4),
442 (unsigned int)(cp
[13] & 0xf), (unsigned int)(cp
[13] >> 4),
443 (unsigned int)(cp
[12] & 0xf), (unsigned int)(cp
[12] >> 4),
444 (unsigned int)(cp
[11] & 0xf), (unsigned int)(cp
[11] >> 4),
445 (unsigned int)(cp
[10] & 0xf), (unsigned int)(cp
[10] >> 4),
446 (unsigned int)(cp
[9] & 0xf), (unsigned int)(cp
[9] >> 4),
447 (unsigned int)(cp
[8] & 0xf), (unsigned int)(cp
[8] >> 4),
448 (unsigned int)(cp
[7] & 0xf), (unsigned int)(cp
[7] >> 4),
449 (unsigned int)(cp
[6] & 0xf), (unsigned int)(cp
[6] >> 4),
450 (unsigned int)(cp
[5] & 0xf), (unsigned int)(cp
[5] >> 4),
451 (unsigned int)(cp
[4] & 0xf), (unsigned int)(cp
[4] >> 4),
452 (unsigned int)(cp
[3] & 0xf), (unsigned int)(cp
[3] >> 4),
453 (unsigned int)(cp
[2] & 0xf), (unsigned int)(cp
[2] >> 4),
454 (unsigned int)(cp
[1] & 0xf), (unsigned int)(cp
[1] >> 4),
455 (unsigned int)(cp
[0] & 0xf), (unsigned int)(cp
[0] >> 4));
459 request
->type
= T_PTR
;
464 * query_name - generate a query based on class, type and name.
466 static void query_name(struct reslist
*request
)
471 memset(buf
, 0, sizeof(buf
));
474 irc_res_mkquery(request
->queryname
, C_IN
, request
->type
, (unsigned char *)buf
, sizeof(buf
))) > 0)
476 HEADER
*header
= (HEADER
*) buf
;
482 * generate an unique id
483 * NOTE: we don't have to worry about converting this to and from
484 * network byte order, the nameserver does not interpret this value
485 * and returns it unchanged
490 header
->id
= (header
->id
+ lrand48()) & 0xffff;
491 } while (find_id(header
->id
));
493 gettimeofday(&tv
, NULL
);
496 header
->id
= (header
->id
+ k
+ tv
.tv_usec
) & 0xffff;
498 } while (find_id(header
->id
));
499 #endif /* HAVE_LRAND48 */
500 request
->id
= header
->id
;
503 request
->sent
+= send_res_msg(buf
, request_len
, request
->sends
);
507 static void resend_query(struct reslist
*request
)
509 if (request
->resend
== 0)
512 switch (request
->type
)
515 do_query_number(NULL
, &request
->addr
, request
);
521 do_query_name(NULL
, request
->name
, request
, request
->type
);
529 * check_question - check if the reply really belongs to the
530 * name we queried (to guard against late replies from previous
531 * queries with the same id).
533 static int check_question(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
)
535 char hostbuf
[128]; /* working buffer */
536 unsigned char *current
; /* current position in buf */
537 int n
; /* temp count */
539 current
= (unsigned char *)buf
+ sizeof(HEADER
);
540 if (header
->qdcount
!= 1)
542 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
, hostbuf
,
546 if (strcasecmp(hostbuf
, request
->queryname
))
552 * proc_answer - process name server reply
554 static int proc_answer(struct reslist
*request
, HEADER
* header
, char *buf
, char *eob
)
556 char hostbuf
[HOSTLEN
+ 100]; /* working buffer */
557 unsigned char *current
; /* current position in buf */
558 int query_class
; /* answer class */
559 int type
; /* answer type */
560 int n
; /* temp count */
562 struct sockaddr_in
*v4
; /* conversion */
564 struct sockaddr_in6
*v6
;
566 current
= (unsigned char *)buf
+ sizeof(HEADER
);
568 for (; header
->qdcount
> 0; --header
->qdcount
)
570 if ((n
= irc_dn_skipname(current
, (unsigned char *)eob
)) < 0)
573 current
+= (size_t) n
+ QFIXEDSZ
;
577 * process each answer sent to us blech.
579 while (header
->ancount
> 0 && (char *)current
< eob
)
583 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
, hostbuf
,
596 * no more answers left
601 hostbuf
[HOSTLEN
] = '\0';
603 /* With Address arithmetic you have to be very anal
604 * this code was not working on alpha due to that
605 * (spotted by rodder/jailbird/dianora)
607 current
+= (size_t) n
;
609 if (!(((char *)current
+ ANSWER_FIXED_SIZE
) < eob
))
612 type
= irc_ns_get16(current
);
613 current
+= TYPE_SIZE
;
615 query_class
= irc_ns_get16(current
);
616 current
+= CLASS_SIZE
;
618 request
->ttl
= irc_ns_get32(current
);
621 rd_length
= irc_ns_get16(current
);
622 current
+= RDLENGTH_SIZE
;
625 * Wait to set request->type until we verify this structure
630 if (request
->type
!= T_A
)
634 * check for invalid rd_length or too many addresses
636 if (rd_length
!= sizeof(struct in_addr
))
638 v4
= (struct sockaddr_in
*)&request
->addr
;
639 SET_SS_LEN(&request
->addr
, sizeof(struct sockaddr_in
));
640 v4
->sin_family
= AF_INET
;
641 memcpy(&v4
->sin_addr
, current
, sizeof(struct in_addr
));
646 if (request
->type
!= T_AAAA
)
648 if (rd_length
!= sizeof(struct in6_addr
))
650 SET_SS_LEN(&request
->addr
, sizeof(struct sockaddr_in6
));
651 v6
= (struct sockaddr_in6
*)&request
->addr
;
652 v6
->sin6_family
= AF_INET6
;
653 memcpy(&v6
->sin6_addr
, current
, sizeof(struct in6_addr
));
658 if (request
->type
!= T_PTR
)
660 n
= irc_dn_expand((unsigned char *)buf
, (unsigned char *)eob
, current
,
661 hostbuf
, sizeof(hostbuf
));
663 return (0); /* broken message */
665 return (0); /* no more answers left */
667 rb_strlcpy(request
->name
, hostbuf
, HOSTLEN
+ 1);
672 /* real answer will follow */
673 current
+= rd_length
;
677 /* XXX I'd rather just throw away the entire bogus thing
678 * but its possible its just a broken nameserver with still
679 * valid answers. But lets do some rudimentary logging for now...
681 ilog(L_MAIN
, "irc_res.c bogus type %d", type
);
690 * res_readreply - read a dns reply from the nameserver and process it.
692 static void res_readreply(rb_fde_t
*F
, void *data
)
694 char buf
[sizeof(HEADER
) + MAXPACKET
]
695 /* Sparc and alpha need 16bit-alignment for accessing header->id
696 * (which is uint16_t). Because of the header = (HEADER*) buf;
697 * lateron, this is neeeded. --FaUl
699 #if defined(__sparc__) || defined(__alpha__)
700 __attribute__ ((aligned(16)))
704 struct reslist
*request
= NULL
;
705 struct DNSReply
*reply
= NULL
;
708 socklen_t len
= sizeof(struct rb_sockaddr_storage
);
709 struct rb_sockaddr_storage lsin
;
711 rc
= recvfrom(rb_get_fd(F
), buf
, sizeof(buf
), 0, (struct sockaddr
*)&lsin
, &len
);
713 /* Re-schedule a read *after* recvfrom, or we'll be registering
714 * interest where it'll instantly be ready for read :-) -- adrian
716 rb_setselect(F
, RB_SELECT_READ
, res_readreply
, NULL
);
717 /* Better to cast the sizeof instead of rc */
718 if (rc
<= (int)(sizeof(HEADER
)))
722 * convert DNS reply reader from Network byte order to CPU byte order.
724 header
= (HEADER
*) buf
;
725 header
->ancount
= ntohs(header
->ancount
);
726 header
->qdcount
= ntohs(header
->qdcount
);
727 header
->nscount
= ntohs(header
->nscount
);
728 header
->arcount
= ntohs(header
->arcount
);
731 * response for an id which we have already received an answer for
732 * just ignore this response.
734 if (0 == (request
= find_id(header
->id
)))
738 * check against possibly fake replies
740 if (!res_ourserver(&lsin
))
743 if (!check_question(request
, header
, buf
, buf
+ rc
))
746 if ((header
->rcode
!= NO_ERRORS
) || (header
->ancount
== 0))
748 if (NXDOMAIN
== header
->rcode
)
750 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
751 rem_request(request
);
756 * If a bad error was returned, we stop here and dont send
757 * send any more (no retries granted).
759 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
760 rem_request(request
);
765 * If this fails there was an error decoding the received packet,
768 answer_count
= proc_answer(request
, header
, buf
, buf
+ rc
);
772 if (request
->type
== T_PTR
)
774 if (request
->name
== NULL
)
777 * got a PTR response with no name, something bogus is happening
778 * don't bother trying again, the client address doesn't resolve
780 (*request
->query
->callback
) (request
->query
->ptr
, reply
);
781 rem_request(request
);
786 * Lookup the 'authoritative' name that we were given for the
791 if (request
->addr
.ss_family
== AF_INET6
)
792 gethost_byname_type(request
->name
, request
->query
, T_AAAA
);
795 gethost_byname_type(request
->name
, request
->query
, T_A
);
796 rem_request(request
);
801 * got a name and address response, client resolved
803 reply
= make_dnsreply(request
);
804 (*request
->query
->callback
) (request
->query
->ptr
, reply
);
806 rem_request(request
);
811 /* couldn't decode, give up -- jilles */
812 (*request
->query
->callback
) (request
->query
->ptr
, NULL
);
813 rem_request(request
);
817 static struct DNSReply
*make_dnsreply(struct reslist
*request
)
820 s_assert(request
!= 0);
822 cp
= (struct DNSReply
*)rb_malloc(sizeof(struct DNSReply
));
824 cp
->h_name
= request
->name
;
825 memcpy(&cp
->addr
, &request
->addr
, sizeof(cp
->addr
));
829 void report_dns_servers(struct Client
*source_p
)
834 for (i
= 0; i
< irc_nscount
; i
++)
836 if (!rb_inet_ntop_sock((struct sockaddr
*)&(irc_nsaddr_list
[i
]),
837 ipaddr
, sizeof ipaddr
))
838 rb_strlcpy(ipaddr
, "?", sizeof ipaddr
);
839 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,