Skip to content

Commit

Permalink
Unify query and cache status enums
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 14, 2024
1 parent c12c47c commit 3e090f5
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/config/toml_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t
}
case CONF_ENUM_BLOCKING_EDNS_MODE:
{
const toml_datum_t val = toml_string_in(toml, key);
toml_datum_t val = toml_string_in(toml, key);
if(val.ok)
{
const int edns_mode = get_edns_mode_val(val.u.s);
Expand Down
60 changes: 58 additions & 2 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ int _findCacheID(const int domainID, const int clientID, const enum query_type q

// Initialize cache entry
dns_cache->magic = MAGICBYTE;
dns_cache->blocking_status = UNKNOWN_BLOCKED;
dns_cache->blocking_status = QUERY_UNKNOWN;
dns_cache->expires = 0;
dns_cache->domainID = domainID;
dns_cache->clientID = clientID;
dns_cache->query_type = query_type;
Expand Down Expand Up @@ -570,7 +571,9 @@ void FTL_reset_per_client_domain_data(void)
continue;

// Reset blocking status
dns_cache->blocking_status = UNKNOWN_BLOCKED;
dns_cache->blocking_status = QUERY_UNKNOWN;
// Reset expiry
dns_cache->expires = 0;
// Reset domainlist ID
dns_cache->list_id = -1;
}
Expand Down Expand Up @@ -1069,6 +1072,59 @@ void _query_set_status(queriesData *query, const enum query_status new_status, c
return;
}

// Memorize this in the DNS cache if blocked due to the response
// We do not cache intermittent statuses as they are subject to change
if(!init &&
new_status != QUERY_UNKNOWN &&
new_status != QUERY_DBBUSY &&
new_status != QUERY_IN_PROGRESS &&
new_status != QUERY_RETRIED &&
new_status != QUERY_RETRIED_DNSSEC)
{
const int cacheID = findCacheID(query->domainID, query->clientID, query->type, true);
DNSCacheData *dns_cache = getDNSCache(cacheID, true);
if(dns_cache != NULL && dns_cache->blocking_status != new_status)
{
// Memorize blocking status DNS cache for the domain/client combination
dns_cache->blocking_status = new_status;

// Set expiration time for this cache entry (if applicable)
// We set this only if not already set to avoid extending the TTL of an
// existing entry
if(config.dns.cache.upstreamBlockedTTL.v.ui > 0 &&
dns_cache->expires == 0 &&
(new_status == QUERY_EXTERNAL_BLOCKED_NXRA ||
new_status == QUERY_EXTERNAL_BLOCKED_NULL ||
new_status == QUERY_EXTERNAL_BLOCKED_IP ||
new_status == QUERY_EXTERNAL_BLOCKED_EDE15))
{
// Set expiration time for this cache entry
dns_cache->expires = time(NULL) + config.dns.cache.upstreamBlockedTTL.v.ui;
}

if(config.debug.queries.v.b)
{
// Debug logging
const char *qtype = get_query_type_str(dns_cache->query_type, NULL, NULL);
const char *domain = getDomainString(query);
const char *clientstr = getClientIPString(query);
const char *statusstr = get_query_status_str(new_status);

if(dns_cache->expires > 0)
{
log_debug(DEBUG_QUERIES, "DNS cache: %s/%s/%s -> %s, expires in %lis",
qtype, clientstr, domain, statusstr,
(long)(dns_cache->expires - time(NULL)));
}
else
{
log_debug(DEBUG_QUERIES, "DNS cache: %s/%s/%s -> %s, no expiry",
qtype, clientstr, domain, statusstr);
}
}
}
}

// else: update global counters, ...
if(!init)
{
Expand Down
5 changes: 4 additions & 1 deletion src/datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ typedef struct {

typedef struct {
unsigned char magic;
enum domain_client_status blocking_status;
struct {
bool allowed :1;
} flags;
enum query_status blocking_status;
enum reply_type force_reply;
enum query_type query_type;
int domainID;
Expand Down
Loading

0 comments on commit 3e090f5

Please sign in to comment.