X-Git-Url: https://jfr.im/git/irc/rqf/shadowircd.git/blobdiff_plain/cbb49fab6afe2b80c91326f260588b9952f88af5..ab72b3e800c5848fa2376dc982c49b0ffa6079ed:/src/res.c diff --git a/src/res.c b/src/res.c index f61485e..2dac47f 100644 --- a/src/res.c +++ b/src/res.c @@ -7,8 +7,6 @@ * The authors takes no responsibility for any damage or loss * of property which results from the use of this software. * - * $Id: res.c 3301 2007-03-28 15:04:06Z jilles $ - * from Hybrid Id: res.c 459 2006-02-12 22:21:37Z db $ * * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, * added callbacks and reference counting of returned hostents. @@ -31,8 +29,7 @@ #include "ircd.h" #include "res.h" #include "reslib.h" -#include "irc_string.h" -#include "sprintf_irc.h" +#include "match.h" #include "numeric.h" #include "client.h" /* SNO_* */ @@ -57,40 +54,31 @@ static PF res_readreply; #define RDLENGTH_SIZE (size_t)2 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) -typedef enum -{ - REQ_IDLE, /* We're doing not much at all */ - REQ_PTR, /* Looking up a PTR */ - REQ_A, /* Looking up an A or AAAA */ - REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ -} request_state; - struct reslist { rb_dlink_node node; int id; - int sent; /* number of requests sent */ - request_state state; /* State the resolver machine is in */ time_t ttl; char type; - char queryname[128]; /* name currently being queried */ + char queryname[IRCD_RES_HOSTLEN + 1]; /* name currently being queried */ char retries; /* retry counter */ char sends; /* number of sends (>1 means resent) */ - char resend; /* send flag. 0 == dont resend */ time_t sentat; time_t timeout; - struct irc_sockaddr_storage addr; + unsigned int lastns; /* index of last server sent to */ + struct rb_sockaddr_storage addr; char *name; struct DNSQuery *query; /* query callback for this request */ }; -static int res_fd; +static rb_fde_t *res_fd; static rb_dlink_list request_list = { NULL, NULL, 0 }; +static int ns_timeout_count[IRCD_MAXNS]; static void rem_request(struct reslist *request); static struct reslist *make_request(struct DNSQuery *query); static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int); -static void do_query_number(struct DNSQuery *query, const struct irc_sockaddr_storage *, +static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *, struct reslist *request); static void query_name(struct reslist *request); static int send_res_msg(const char *buf, int len, int count); @@ -100,11 +88,6 @@ static int proc_answer(struct reslist *request, HEADER * header, char *, char *) static struct reslist *find_id(int id); static struct DNSReply *make_dnsreply(struct reslist *request); -extern struct irc_sockaddr_storage irc_nsaddr_list[IRCD_MAXNS]; -extern int irc_nscount; -extern char irc_domain[HOSTLEN + 1]; - - /* * int * res_ourserver(inp) @@ -116,30 +99,30 @@ extern char irc_domain[HOSTLEN + 1]; * paul vixie, 29may94 * revised for ircd, cryogen(stu) may03 */ -static int res_ourserver(const struct irc_sockaddr_storage *inp) +static int res_ourserver(const struct rb_sockaddr_storage *inp) { -#ifdef IPV6 - struct sockaddr_in6 *v6; - struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp; +#ifdef RB_IPV6 + const struct sockaddr_in6 *v6; + const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; #endif - struct sockaddr_in *v4; - struct sockaddr_in *v4in = (struct sockaddr_in *)inp; + const struct sockaddr_in *v4; + const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; int ns; for (ns = 0; ns < irc_nscount; ns++) { - const struct irc_sockaddr_storage *srv = &irc_nsaddr_list[ns]; -#ifdef IPV6 - v6 = (struct sockaddr_in6 *)srv; + const struct rb_sockaddr_storage *srv = &irc_nsaddr_list[ns]; +#ifdef RB_IPV6 + v6 = (const struct sockaddr_in6 *)srv; #endif - v4 = (struct sockaddr_in *)srv; + v4 = (const struct sockaddr_in *)srv; /* could probably just memcmp(srv, inp, srv.ss_len) here * but we'll air on the side of caution - stu */ switch (srv->ss_family) { -#ifdef IPV6 +#ifdef RB_IPV6 case AF_INET6: if (srv->ss_family == inp->ss_family) if (v6->sin6_port == v6in->sin6_port) @@ -147,7 +130,10 @@ static int res_ourserver(const struct irc_sockaddr_storage *inp) sizeof(struct in6_addr)) == 0) || (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any, sizeof(struct in6_addr)) == 0)) + { + ns_timeout_count[ns] = 0; return 1; + } break; #endif case AF_INET: @@ -155,7 +141,10 @@ static int res_ourserver(const struct irc_sockaddr_storage *inp) if (v4->sin_port == v4in->sin_port) if ((v4->sin_addr.s_addr == INADDR_ANY) || (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)) + { + ns_timeout_count[ns] = 0; return 1; + } break; default: break; @@ -192,6 +181,7 @@ static time_t timeout_query_list(time_t now) } else { + ns_timeout_count[request->lastns]++; request->sentat = now; request->timeout += request->timeout; resend_query(request); @@ -215,23 +205,29 @@ static void timeout_resolver(void *notused) timeout_query_list(rb_current_time()); } +static struct ev_entry *timeout_resolver_ev = NULL; + /* * start_resolver - do everything we need to read the resolv.conf file * and initialize the resolver file descriptor if needed */ static void start_resolver(void) { + int i; + irc_res_init(); + for (i = 0; i < irc_nscount; i++) + ns_timeout_count[i] = 0; - if (res_fd <= 0) /* there isn't any such thing as fd 0, that's just a myth. */ + if (res_fd == NULL) { if ((res_fd = rb_socket(irc_nsaddr_list[0].ss_family, SOCK_DGRAM, 0, - "UDP resolver socket")) == -1) + "UDP resolver socket")) == NULL) return; /* At the moment, the resolver FD data is global .. */ - rb_setselect(res_fd, FDLIST_NONE, COMM_SELECT_READ, res_readreply, NULL, 0); - rb_event_add("timeout_resolver", timeout_resolver, NULL, 1); + rb_setselect(res_fd, RB_SELECT_READ, res_readreply, NULL); + timeout_resolver_ev = rb_event_add("timeout_resolver", timeout_resolver, NULL, 1); } } @@ -252,8 +248,8 @@ void init_resolver(void) void restart_resolver(void) { rb_close(res_fd); - res_fd = -1; - eventDelete(timeout_resolver, NULL); /* -ddosen */ + res_fd = NULL; + rb_event_delete(timeout_resolver_ev); /* -ddosen */ start_resolver(); } @@ -300,10 +296,8 @@ static struct reslist *make_request(struct DNSQuery *query) request->sentat = rb_current_time(); request->retries = 3; - request->resend = 1; request->timeout = 4; /* start at 4 and exponential inc. */ request->query = query; - request->state = REQ_IDLE; rb_dlinkAdd(request, &request->node, &request_list); @@ -331,33 +325,67 @@ void delete_resolver_queries(const struct DNSQuery *query) } /* - * send_res_msg - sends msg to all nameservers found in the "_res" structure. - * This should reflect /etc/resolv.conf. We will get responses - * which arent needed but is easier than checking to see if nameserver - * isnt present. Returns number of messages successfully sent to - * nameservers or -1 if no successful sends. + * retryfreq - determine how many queries to wait before resending + * if there have been that many consecutive timeouts + */ +static int retryfreq(int timeouts) +{ + switch (timeouts) + { + case 1: + return 3; + case 2: + return 9; + case 3: + return 27; + case 4: + return 81; + default: + return 243; + } +} + +/* + * send_res_msg - sends msg to a nameserver. + * This should reflect /etc/resolv.conf. + * Returns number of nameserver successfully sent to + * or -1 if no successful sends. */ static int send_res_msg(const char *msg, int len, int rcount) { int i; - int sent = 0; - int max_queries = IRCD_MIN(irc_nscount, rcount); + int ns; + static int retrycnt; - /* RES_PRIMARY option is not implemented - * if (res.options & RES_PRIMARY || 0 == max_queries) + retrycnt++; + /* First try a nameserver that seems to work. + * Every once in a while, try a possibly broken one to check + * if it is working again. */ - if (max_queries == 0) - max_queries = 1; + for (i = 0; i < irc_nscount; i++) + { + ns = (i + rcount - 1) % irc_nscount; + if (ns_timeout_count[ns] && retrycnt % retryfreq(ns_timeout_count[ns])) + continue; + if (sendto(rb_get_fd(res_fd), msg, len, 0, + (struct sockaddr *)&(irc_nsaddr_list[ns]), + GET_SS_LEN(&irc_nsaddr_list[ns])) == len) + return ns; + } - for (i = 0; sent < max_queries && i < irc_nscount; i++) + /* No known working nameservers, try some broken one. */ + for (i = 0; i < irc_nscount; i++) { - if (sendto(res_fd, msg, len, 0, - (struct sockaddr *)&(irc_nsaddr_list[i]), - GET_SS_LEN(irc_nsaddr_list[i])) == len) - ++sent; + ns = (i + rcount - 1) % irc_nscount; + if (!ns_timeout_count[ns]) + continue; + if (sendto(rb_get_fd(res_fd), msg, len, 0, + (struct sockaddr *)&(irc_nsaddr_list[ns]), + GET_SS_LEN(&irc_nsaddr_list[ns])) == len) + return ns; } - return (sent); + return -1; } /* @@ -392,7 +420,7 @@ void gethost_byname_type(const char *name, struct DNSQuery *query, int type) /* * gethost_byaddr - get host name from address */ -void gethost_byaddr(const struct irc_sockaddr_storage *addr, struct DNSQuery *query) +void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *query) { do_query_number(query, addr, NULL); } @@ -403,20 +431,19 @@ void gethost_byaddr(const struct irc_sockaddr_storage *addr, struct DNSQuery *qu static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int type) { - char host_name[HOSTLEN + 1]; + char host_name[IRCD_RES_HOSTLEN + 1]; - strlcpy(host_name, name, HOSTLEN + 1); - add_local_domain(host_name, HOSTLEN); + rb_strlcpy(host_name, name, IRCD_RES_HOSTLEN + 1); + add_local_domain(host_name, IRCD_RES_HOSTLEN); if (request == NULL) { request = make_request(query); request->name = (char *)rb_malloc(strlen(host_name) + 1); strcpy(request->name, host_name); - request->state = REQ_A; } - strlcpy(request->queryname, host_name, sizeof(request->queryname)); + rb_strlcpy(request->queryname, host_name, sizeof(request->queryname)); request->type = type; query_name(request); } @@ -424,7 +451,7 @@ static void do_query_name(struct DNSQuery *query, const char *name, struct resli /* * do_query_number - Use this to do reverse IP# lookups. */ -static void do_query_number(struct DNSQuery *query, const struct irc_sockaddr_storage *addr, +static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *addr, struct reslist *request) { const unsigned char *cp; @@ -432,22 +459,22 @@ static void do_query_number(struct DNSQuery *query, const struct irc_sockaddr_st if (request == NULL) { request = make_request(query); - memcpy(&request->addr, addr, sizeof(struct irc_sockaddr_storage)); - request->name = (char *)rb_malloc(HOSTLEN + 1); + memcpy(&request->addr, addr, sizeof(struct rb_sockaddr_storage)); + request->name = (char *)rb_malloc(IRCD_RES_HOSTLEN + 1); } if (addr->ss_family == AF_INET) { - struct sockaddr_in *v4 = (struct sockaddr_in *)addr; + const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; cp = (const unsigned char *)&v4->sin_addr.s_addr; rb_sprintf(request->queryname, "%u.%u.%u.%u.in-addr.arpa", (unsigned int)(cp[3]), (unsigned int)(cp[2]), (unsigned int)(cp[1]), (unsigned int)(cp[0])); } -#ifdef IPV6 +#ifdef RB_IPV6 else if (addr->ss_family == AF_INET6) { - struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr; + const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; cp = (const unsigned char *)&v6->sin6_addr.s6_addr; (void)sprintf(request->queryname, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." @@ -482,6 +509,7 @@ static void query_name(struct reslist *request) { char buf[MAXPACKET]; int request_len = 0; + int ns; memset(buf, 0, sizeof(buf)); @@ -515,22 +543,21 @@ static void query_name(struct reslist *request) request->id = header->id; ++request->sends; - request->sent += send_res_msg(buf, request_len, request->sends); + ns = send_res_msg(buf, request_len, request->sends); + if (ns != -1) + request->lastns = ns; } } static void resend_query(struct reslist *request) { - if (request->resend == 0) - return; - switch (request->type) { case T_PTR: do_query_number(NULL, &request->addr, request); break; case T_A: -#ifdef IPV6 +#ifdef RB_IPV6 case T_AAAA: #endif do_query_name(NULL, request->name, request, request->type); @@ -547,7 +574,7 @@ static void resend_query(struct reslist *request) */ static int check_question(struct reslist *request, HEADER * header, char *buf, char *eob) { - char hostbuf[128]; /* working buffer */ + char hostbuf[IRCD_RES_HOSTLEN + 1]; /* working buffer */ unsigned char *current; /* current position in buf */ int n; /* temp count */ @@ -568,14 +595,14 @@ static int check_question(struct reslist *request, HEADER * header, char *buf, c */ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char *eob) { - char hostbuf[HOSTLEN + 100]; /* working buffer */ + char hostbuf[IRCD_RES_HOSTLEN + 100]; /* working buffer */ unsigned char *current; /* current position in buf */ int query_class; /* answer class */ int type; /* answer type */ int n; /* temp count */ int rd_length; struct sockaddr_in *v4; /* conversion */ -#ifdef IPV6 +#ifdef RB_IPV6 struct sockaddr_in6 *v6; #endif current = (unsigned char *)buf + sizeof(HEADER); @@ -613,7 +640,7 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char return (0); } - hostbuf[HOSTLEN] = '\0'; + hostbuf[IRCD_RES_HOSTLEN] = '\0'; /* With Address arithmetic you have to be very anal * this code was not working on alpha due to that @@ -651,18 +678,18 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char if (rd_length != sizeof(struct in_addr)) return (0); v4 = (struct sockaddr_in *)&request->addr; - SET_SS_LEN(request->addr, sizeof(struct sockaddr_in)); + SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in)); v4->sin_family = AF_INET; memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); return (1); break; -#ifdef IPV6 +#ifdef RB_IPV6 case T_AAAA: if (request->type != T_AAAA) return (0); if (rd_length != sizeof(struct in6_addr)) return (0); - SET_SS_LEN(request->addr, sizeof(struct sockaddr_in6)); + SET_SS_LEN(&request->addr, sizeof(struct sockaddr_in6)); v6 = (struct sockaddr_in6 *)&request->addr; v6->sin6_family = AF_INET6; memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); @@ -679,26 +706,12 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char else if (n == 0) return (0); /* no more answers left */ - strlcpy(request->name, hostbuf, HOSTLEN + 1); + rb_strlcpy(request->name, hostbuf, IRCD_RES_HOSTLEN + 1); return (1); break; - case T_CNAME: /* first check we already havent started looking - into a cname */ - if (request->type != T_PTR) - return (0); - - if (request->state == REQ_CNAME) - { - n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, - current, hostbuf, sizeof(hostbuf)); - - if (n < 0) - return (0); - return (1); - } - - request->state = REQ_CNAME; + case T_CNAME: + /* real answer will follow */ current += rd_length; break; @@ -716,9 +729,10 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char } /* - * res_readreply - read a dns reply from the nameserver and process it. + * res_read_single_reply - read a dns reply from the nameserver and process it. + * Return value: 1 if a packet was read, 0 otherwise */ -static void res_readreply(int fd, void *data) +static int res_read_single_reply(rb_fde_t *F, void *data) { char buf[sizeof(HEADER) + MAXPACKET] /* Sparc and alpha need 16bit-alignment for accessing header->id @@ -734,18 +748,18 @@ static void res_readreply(int fd, void *data) struct DNSReply *reply = NULL; int rc; int answer_count; - socklen_t len = sizeof(struct irc_sockaddr_storage); - struct irc_sockaddr_storage lsin; + socklen_t len = sizeof(struct rb_sockaddr_storage); + struct rb_sockaddr_storage lsin; - rc = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); + rc = recvfrom(rb_get_fd(F), buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); - /* Re-schedule a read *after* recvfrom, or we'll be registering - * interest where it'll instantly be ready for read :-) -- adrian - */ - rb_setselect(fd, FDLIST_NONE, COMM_SELECT_READ, res_readreply, NULL, 0); - /* Better to cast the sizeof instead of rc */ + /* No packet */ + if (rc == 0 || rc == -1) + return 0; + + /* Too small */ if (rc <= (int)(sizeof(HEADER))) - return; + return 1; /* * convert DNS reply reader from Network byte order to CPU byte order. @@ -761,16 +775,16 @@ static void res_readreply(int fd, void *data) * just ignore this response. */ if (0 == (request = find_id(header->id))) - return; + return 1; /* * check against possibly fake replies */ if (!res_ourserver(&lsin)) - return; + return 1; if (!check_question(request, header, buf, buf + rc)) - return; + return 1; if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) { @@ -788,7 +802,7 @@ static void res_readreply(int fd, void *data) (*request->query->callback) (request->query->ptr, NULL); rem_request(request); } - return; + return 1; } /* * If this fails there was an error decoding the received packet, @@ -808,7 +822,7 @@ static void res_readreply(int fd, void *data) */ (*request->query->callback) (request->query->ptr, reply); rem_request(request); - return; + return 1; } /* @@ -816,7 +830,7 @@ static void res_readreply(int fd, void *data) * ip#. * */ -#ifdef IPV6 +#ifdef RB_IPV6 if (request->addr.ss_family == AF_INET6) gethost_byname_type(request->name, request->query, T_AAAA); else @@ -841,6 +855,14 @@ static void res_readreply(int fd, void *data) (*request->query->callback) (request->query->ptr, NULL); rem_request(request); } + return 1; +} + +static void res_readreply(rb_fde_t *F, void *data) +{ + while (res_read_single_reply(F, data)) + ; + rb_setselect(F, RB_SELECT_READ, res_readreply, NULL); } static struct DNSReply *make_dnsreply(struct reslist *request) @@ -862,10 +884,10 @@ void report_dns_servers(struct Client *source_p) for (i = 0; i < irc_nscount; i++) { - if (!inetntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]), + if (!rb_inet_ntop_sock((struct sockaddr *)&(irc_nsaddr_list[i]), ipaddr, sizeof ipaddr)) - strlcpy(ipaddr, "?", sizeof ipaddr); + rb_strlcpy(ipaddr, "?", sizeof ipaddr); sendto_one_numeric(source_p, RPL_STATSDEBUG, - "A %s", ipaddr); + "A %s %d", ipaddr, ns_timeout_count[i]); } }