-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Move resolution of client and upstream host names to a dedicated thread #405
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2f3566
Move resolution of client and upstream host names to a dedicated thre…
DL6ER f10b49c
Remove obsolete debug output
DL6ER 5135663
Call to ">reresolve" also re-resolves host names of all known forward…
DL6ER 1a3a595
Use "DNS client" as custom thread name for the new name resolving thread
DL6ER fd53505
Rename routines/variables to reflect "DNS client"
DL6ER e6157da
Also re-resolve upstream host names every hour
DL6ER 9f86037
Change logic of onlynew variable: If onlynew is set and an item is NO…
DL6ER File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,14 @@ | |
|
||
#include "FTL.h" | ||
|
||
// Resolve new client and upstream server host names | ||
// once every minute | ||
#define RESOLVE_INTERVAL 60 | ||
|
||
// Re-resolve client names | ||
// once every hour | ||
#define RERESOLVE_INTERVAL 3600 | ||
|
||
char *resolveHostname(const char *addr) | ||
{ | ||
// Get host name | ||
|
@@ -62,68 +70,92 @@ char *resolveHostname(const char *addr) | |
return hostname; | ||
} | ||
|
||
// This routine is run *after* garbage cleaning (default interval is once per hour) | ||
// to account for possibly updated hostnames | ||
void reresolveHostnames(void) | ||
// Resolve client host names | ||
void resolveClients(bool onlynew) | ||
{ | ||
int clientID; | ||
for(clientID = 0; clientID < counters.clients; clientID++) | ||
int i; | ||
for(i = 0; i < counters.clients; i++) | ||
{ | ||
// Memory validation | ||
validate_access("clients", clientID, true, __LINE__, __FUNCTION__, __FILE__); | ||
|
||
// Process this client only if it has at least one active query in the log | ||
if(clients[clientID].count < 1) | ||
continue; | ||
validate_access("clients", i, true, __LINE__, __FUNCTION__, __FILE__); | ||
|
||
// Get client hostname | ||
char *hostname = resolveHostname(clients[clientID].ip); | ||
if(strlen(hostname) > 0) | ||
// If onlynew flag is set, we will only resolve new clients | ||
// If not, we will try to re-resolve all known clients | ||
if(onlynew && clients[i].new) | ||
{ | ||
// Delete possibly already existing hostname pointer before storing new data | ||
if(clients[clientID].name != NULL) | ||
{ | ||
free(clients[clientID].name); | ||
clients[clientID].name = NULL; | ||
} | ||
|
||
// Store client hostname | ||
clients[clientID].name = strdup(hostname); | ||
clients[clientID].new = false; | ||
char *hostname = resolveHostname(clients[i].ip); | ||
|
||
enable_thread_lock(); | ||
|
||
if(clients[i].name != NULL) | ||
free(clients[i].name); | ||
|
||
clients[i].name = hostname; | ||
clients[i].new = false; | ||
|
||
disable_thread_lock(); | ||
} | ||
free(hostname); | ||
} | ||
} | ||
|
||
// This routine is run *before* saving to the database (default interval is once per minute) | ||
// to account for new clients (and forward destinations) | ||
void resolveNewClients(void) | ||
// Resolve upstream destination host names | ||
void resolveForwardDestinations(bool onlynew) | ||
{ | ||
int i; | ||
for(i = 0; i < counters.clients; i++) | ||
for(i = 0; i < counters.forwarded; i++) | ||
{ | ||
// Memory validation | ||
validate_access("clients", i, true, __LINE__, __FUNCTION__, __FILE__); | ||
validate_access("forwarded", i, true, __LINE__, __FUNCTION__, __FILE__); | ||
|
||
// Only try to resolve new clients | ||
// Note that it can happen that we are not able to find hostnames but we don't | ||
// want to try to resolve them every minute in this case. | ||
if(clients[i].new) | ||
// If onlynew flag is set, we will only resolve new upstream destinations | ||
// If not, we will try to re-resolve all known upstream destinations | ||
if(onlynew && forwarded[i].new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment. |
||
{ | ||
clients[i].name = resolveHostname(clients[i].ip); | ||
clients[i].new = false; | ||
char *hostname = resolveHostname(forwarded[i].ip); | ||
|
||
enable_thread_lock(); | ||
|
||
if(forwarded[i].name != NULL) | ||
free(forwarded[i].name); | ||
|
||
forwarded[i].name = hostname; | ||
forwarded[i].new = false; | ||
|
||
disable_thread_lock(); | ||
} | ||
} | ||
for(i = 0; i < counters.forwarded; i++) | ||
} | ||
|
||
void *DNSclient_thread(void *val) | ||
{ | ||
// Set thread name | ||
prctl(PR_SET_NAME, "DNS client", 0, 0, 0); | ||
|
||
while(!killed) | ||
{ | ||
// Memory validation | ||
validate_access("forwarded", i, true, __LINE__, __FUNCTION__, __FILE__); | ||
// Run every minute to resolve only new clients and upstream servers | ||
if(time(NULL) % RESOLVE_INTERVAL == 0) | ||
{ | ||
// Try to resolve new client host names (onlynew=true) | ||
resolveClients(true); | ||
// Try to resolve new upstream destination host names (onlynew=true) | ||
resolveForwardDestinations(true); | ||
// Prevent immediate re-run of this routine | ||
sleepms(500); | ||
} | ||
|
||
// Only try to resolve new forward destinations | ||
if(forwarded[i].new) | ||
// Run every hour to update possibly changed client host names | ||
if(time(NULL) % RERESOLVE_INTERVAL == 0) | ||
{ | ||
forwarded[i].name = resolveHostname(forwarded[i].ip); | ||
forwarded[i].new = false; | ||
// Try to resolve all client host names (onlynew=false) | ||
resolveClients(false); | ||
DL6ER marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Prevent immediate re-run of this routine | ||
sleepms(500); | ||
} | ||
|
||
// Idle for 0.5 sec before checking again the time criteria | ||
sleepms(500); | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
onlynew
is false, no resolving will occur at all.