diff --git a/api.c b/api.c index c94e11b6..621e73f4 100644 --- a/api.c +++ b/api.c @@ -439,16 +439,13 @@ void getTopClients(char *client_message, int *sock) void getForwardDestinations(char *client_message, int *sock) { bool sort = true; - int i, temparray[counters->forwarded][2], forwardedsum = 0, totalqueries = 0; + int i, temparray[counters->forwarded][2], totalqueries = 0; if(command(client_message, "unsorted")) sort = false; for(i=0; i < counters->forwarded; i++) { validate_access("forwarded", i, true, __LINE__, __FUNCTION__, __FILE__); - // Compute forwardedsum - forwardedsum += forwarded[i].count; - // If we want to print a sorted output, we fill the temporary array with // the values we will use for sorting afterwards if(sort) { @@ -506,24 +503,9 @@ void getForwardDestinations(char *client_message, int *sock) ip = getstr(forwarded[j].ippos); name = getstr(forwarded[j].namepos); - // Math explanation: - // A single query may result in requests being forwarded to multiple destinations - // Hence, in order to be able to give percentages here, we have to normalize the - // number of forwards to each specific destination by the total number of forward - // events. This term is done by - // a = forwarded[j].count / forwardedsum - // - // The fraction a describes now how much share an individual forward destination - // has on the total sum of sent requests. - // We also know the share of forwarded queries on the total number of queries - // b = counters->forwardedqueries / c - // where c is the number of valid queries, - // c = counters->forwardedqueries + counters->cached + counters->blocked - // - // To get the total percentage of a specific query on the total number of queries, - // we simply have to scale b by a which is what we do in the following. - if(forwardedsum > 0 && totalqueries > 0) - percentage = 1e2f * forwarded[j].count / forwardedsum * counters->forwardedqueries / totalqueries; + // Get percentage + if(totalqueries > 0) + percentage = 1e2f * forwarded[j].count / totalqueries; } // Send data: diff --git a/dnsmasq_interface.c b/dnsmasq_interface.c index cc9fa419..b444672d 100644 --- a/dnsmasq_interface.c +++ b/dnsmasq_interface.c @@ -270,13 +270,10 @@ void FTL_forwarded(unsigned int flags, char *name, struct all_addr *addr, int id return; } - // Set query status - queries[i].status = QUERY_FORWARDED; - // Proceed only if // - current query has not been marked as replied to so far // (it could be that answers from multiple forward - // destionations are coimg in for the same query) + // destinations are coming in for the same query) // - the query was formally known as cached but had to be forwarded // (this is a special case further described below) if(queries[i].complete && queries[i].status != QUERY_CACHE) @@ -291,54 +288,58 @@ void FTL_forwarded(unsigned int flags, char *name, struct all_addr *addr, int id int forwardID = findForwardID(forward, true); queries[i].forwardID = forwardID; - if(!queries[i].complete) + int j = queries[i].timeidx; + validate_access("overTime", j, true, __LINE__, __FUNCTION__, __FILE__); + + if(queries[i].status == QUERY_CACHE) { - int j = queries[i].timeidx; - validate_access("overTime", j, true, __LINE__, __FUNCTION__, __FILE__); + // Detect if we cached the but need to ask the upstream + // servers for the actual IPs now, we remove this query from the + // counters for cache replied queries as we had to forward a + // request for it. Example: + // Assume a domain a.com is a CNAME which is cached and has a very + // long TTL. It point to another domain server.a.com which has an + // A record but this has a much lower TTL. + // If you now query a.com and then again after some time, you end + // up in a situation where dnsmasq can answer the first level of + // the DNS result (the CNAME) from cache, hence the status of this + // query is marked as "answered from cache" in FTLDNS. However, for + // server.a.com wit the much shorter TTL, we still have to forward + // something and ask the upstream server for the final IP address. + // This code section acknowledges this by removing one entry from + // the cached counters as we will re-brand this query as having been + // forwarded in the following. + counters->cached--; + // Also correct overTime data + overTime[j].cached--; - if(queries[i].status == QUERY_CACHE) - { - // Detect if we cached the but need to ask the upstream - // servers for the actual IPs now, we remove this query from the - // counters for cache replied queries as we had to forward a - // request for it. Example: - // Assume a domain a.com is a CNAME which is cached and has a very - // long TTL. It point to another domain server.a.com which has an - // A record but this has a much lower TTL. - // If you now query a.com and then again after some time, you end - // up in a situation where dnsmasq can answer the first level of - // the DNS result (the CNAME) from cache, hence the status of this - // query is marked as "answered from cache" in FTLDNS. However, for - // server.a.com wit the much shorter TTL, we still have to forward - // something and ask the upstream server for the final IP address. - // This code section acknowledges this by removing one entry from - // the cached counters as we will re-brand this query as having been - // forwarded in the following. - counters->cached--; - // Also correct overTime data - overTime[j].cached--; - - // Correct reply timer - struct timeval response; - gettimeofday(&response, 0); - // Reset timer, shift slightly into the past to acknowledge the time - // FTLDNS needed to look up the CNAME in its cache - queries[i].response = converttimeval(response) - queries[i].response; - } - else - { - // Normal cache reply - // Query is no longer unknown - counters->unknown--; - // Hereby, this query is now fully determined - queries[i].complete = true; - } - // Update overTime data - overTime[j].forwarded++; - - // Update couter for forwarded queries - counters->forwardedqueries++; + // Correct reply timer + struct timeval response; + gettimeofday(&response, 0); + // Reset timer, shift slightly into the past to acknowledge the time + // FTLDNS needed to look up the CNAME in its cache + queries[i].response = converttimeval(response) - queries[i].response; } + else + { + // Normal forwarded query (status is set below) + // Query is no longer unknown + counters->unknown--; + // Hereby, this query is now fully determined + queries[i].complete = true; + } + + // Set query status to forwarded only after the + // if(queries[i].status == QUERY_CACHE) { ... } + // from above as otherwise this check will always + // be negative + queries[i].status = QUERY_FORWARDED; + + // Update overTime data + overTime[j].forwarded++; + + // Update counter for forwarded queries + counters->forwardedqueries++; // Release allocated memory free(forward); diff --git a/gc.c b/gc.c index 5ef46dde..bc93aa58 100644 --- a/gc.c +++ b/gc.c @@ -13,7 +13,7 @@ bool doGC = false; -int lastGCrun = 0; +time_t lastGCrun = 0; void *GC_thread(void *val) { // Set thread name