Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UNKNOWN option for REFRESH_HOSTNAME #985

Merged
merged 4 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ void read_FTLconf(void)
config.refresh_hostnames = REFRESH_NONE;
logg(" REFRESH_HOSTNAMES: Not periodically refreshing names");
}
else if(buffer != NULL && strcasecmp(buffer, "UNKNOWN") == 0)
{
config.refresh_hostnames = REFRESH_UNKNOWN;
logg(" REFRESH_HOSTNAMES: Only refreshing recently active clients with unknown hostnames");
}
else
{
config.refresh_hostnames = REFRESH_IPV4_ONLY;
Expand Down
2 changes: 2 additions & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ enum events {
RELOAD_GRAVITY,
RELOAD_PRIVACY_LEVEL,
RERESOLVE_HOSTNAMES,
RERESOLVE_HOSTNAMES_FORCE,
REIMPORT_ALIASCLIENTS,
PARSE_NEIGHBOR_CACHE,
EVENTS_MAX
Expand All @@ -153,6 +154,7 @@ enum events {
enum refresh_hostnames {
REFRESH_ALL,
REFRESH_IPV4_ONLY,
REFRESH_UNKNOWN,
REFRESH_NONE
} __attribute__ ((packed));

Expand Down
2 changes: 2 additions & 0 deletions src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ static const char *eventtext(const enum events event)
return "RELOAD_PRIVACY_LEVEL";
case RERESOLVE_HOSTNAMES:
return "RERESOLVE_HOSTNAMES";
case RERESOLVE_HOSTNAMES_FORCE:
return "RERESOLVE_HOSTNAMES_FORCE";
case REIMPORT_ALIASCLIENTS:
return "REIMPORT_ALIASCLIENTS";
case PARSE_NEIGHBOR_CACHE:
Expand Down
40 changes: 30 additions & 10 deletions src/resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos)
}

// Resolve client host names
static void resolveClients(const bool onlynew)
static void resolveClients(const bool onlynew, const bool force_refreshing)
{
const time_t now = time(NULL);
// Lock counter access here, we use a copy in the following loop
Expand Down Expand Up @@ -386,7 +386,7 @@ static void resolveClients(const bool onlynew)

// Only try to resolve host names of clients which were recently active if we are re-resolving
// Limit for a "recently active" client is two hours ago
if(onlynew == false && client->lastQuery < now - 2*60*60)
if(!force_refreshing && !onlynew && client->lastQuery < now - 2*60*60)
{
if(config.debug & DEBUG_RESOLVER)
{
Expand All @@ -401,7 +401,7 @@ static void resolveClients(const bool onlynew)

// If onlynew flag is set, we will only resolve new clients
// If not, we will try to re-resolve all known clients
if(onlynew && !newflag)
if(!force_refreshing && onlynew && !newflag)
{
if(config.debug & DEBUG_RESOLVER)
{
Expand All @@ -419,15 +419,27 @@ static void resolveClients(const bool onlynew)
IPv6 = true;

// If we're in refreshing mode (onlynew == false), we skip clients if
// either IPv4-only or none is selected
// 1. We should not refresh any hostnames
// 2. We should only refresh IPv4 client, but this client is IPv6
// 3. We should only refresh unknown hostnames, but leave
// existing ones as they are
if(onlynew == false &&
(config.refresh_hostnames == REFRESH_NONE ||
(config.refresh_hostnames == REFRESH_IPV4_ONLY && IPv6)))
(config.refresh_hostnames == REFRESH_NONE ||
(config.refresh_hostnames == REFRESH_IPV4_ONLY && IPv6) ||
(config.refresh_hostnames == REFRESH_UNKNOWN && oldnamepos != 0)))
{
if(config.debug & DEBUG_RESOLVER)
{
logg("Skipping client %s (%s) because it should not be refreshed",
getstr(ippos), getstr(oldnamepos));
const char *reason = "N/A";
if(config.refresh_hostnames == REFRESH_NONE)
reason = "Not refreshing any hostnames";
else if(config.refresh_hostnames == REFRESH_IPV4_ONLY)
reason = "Only refreshing IPv4 names";
else if(config.refresh_hostnames == REFRESH_UNKNOWN)
reason = "Looking only for unknown hostnames";

logg("Skipping client %s (%s) because it should not be refreshed: %s",
getstr(ippos), getstr(oldnamepos), reason);
}
skipped++;
continue;
Expand Down Expand Up @@ -559,7 +571,8 @@ void *DNSclient_thread(void *val)
if(resolver_ready && (time(NULL) % RESOLVE_INTERVAL == 0))
{
// Try to resolve new client host names (onlynew=true)
resolveClients(true);
// We're not forcing refreshing here
resolveClients(true, false);
// Try to resolve new upstream destination host names (onlynew=true)
resolveUpstreams(true);
// Prevent immediate re-run of this routine
Expand All @@ -572,11 +585,18 @@ void *DNSclient_thread(void *val)
set_event(RERESOLVE_HOSTNAMES); // done below
}

bool force_refreshing = false;
if(get_and_clear_event(RERESOLVE_HOSTNAMES_FORCE))
{
set_event(RERESOLVE_HOSTNAMES); // done below
force_refreshing = true;
}

// Process resolver related event queue elements
if(get_and_clear_event(RERESOLVE_HOSTNAMES))
{
// Try to resolve all client host names (onlynew=false)
resolveClients(false);
resolveClients(false, force_refreshing);
// Try to resolve all upstream destination host names (onlynew=false)
resolveUpstreams(false);
// Prevent immediate re-run of this routine
Expand Down
4 changes: 3 additions & 1 deletion src/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ static void SIGRT_handler(int signum, siginfo_t *si, void *unused)
else if(rtsig == 4)
{
// Re-resolve all clients and forward destinations
set_event(RERESOLVE_HOSTNAMES);
// Force refreshing hostnames according to
// REFRESH_HOSTNAMES config option
set_event(RERESOLVE_HOSTNAMES_FORCE);
}
else if(rtsig == 5)
{
Expand Down