From d25f05d3398d661cbfa06331dcda12c8b68eacda Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 29 Nov 2020 13:21:24 +0100 Subject: [PATCH] Respect settings RESOLVE_IPV4 and RESOLVE_IPv6 also when trying to resolve host names from the database (network table) Signed-off-by: DL6ER --- src/database/network-table.c | 17 ++++++++++++ src/resolve.c | 53 ++++++++++++++++++++++++------------ src/resolve.h | 2 ++ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/database/network-table.c b/src/database/network-table.c index eb8faa1d..1fabb3dd 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -1695,6 +1695,15 @@ char *__attribute__((malloc)) getNameFromIP(const char *ipaddr) return NULL; } + // Check if we want to resolve host names + if(!resolve_this_name(ipaddr)) + { + if(config.debug & DEBUG_DATABASE) + logg("getNameFromIP(\"%s\") - configured to not resolve host name", ipaddr); + + return NULL; + } + // Check for a host name associated with the same IP address sqlite3_stmt *stmt = NULL; const char *querystr = "SELECT name FROM network_addresses WHERE name IS NOT NULL AND ip = ?;"; @@ -1858,6 +1867,14 @@ void resolveNetworkTableNames(void) return; } + // Check if we want to resolve host names + if(!resolve_names()) + { + if(config.debug & DEBUG_DATABASE) + logg("resolveNetworkTableNames() - configured to not resolve host names"); + return; + } + const char sql[] = "BEGIN TRANSACTION IMMEDIATE"; int rc = dbquery(sql); if( rc != SQLITE_OK ) diff --git a/src/resolve.c b/src/resolve.c index f9776416..f7c425bb 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -112,12 +112,39 @@ static void print_used_resolvers(const char *message) } } +// Return if we want to resolve address to names at all +// (may be disabled due to config settings) +bool __attribute__((pure)) resolve_names(void) +{ + if(!config.resolveIPv4 && !config.resolveIPv6) + return false; + return true; +} + +// Return if we want to resolve this type of address to a name +bool __attribute__((pure)) resolve_this_name(const char *ipaddr) +{ + if(!config.resolveIPv4 || + (!config.resolveIPv6 && strstr(ipaddr,":") != NULL)) + return false; + return true; +} + char *resolveHostname(const char *addr) { // Get host name struct hostent *he = NULL; char *hostname = NULL; - bool IPv6 = false; + + // Check if we want to resolve host names + if(!resolve_this_name(addr)) + { + if(config.debug & DEBUG_RESOLVER) + logg("Configured to not resolve host name for %s", addr); + + // Return an empty host name + return strdup(""); + } if(config.debug & DEBUG_RESOLVER) logg("Trying to resolve %s", addr); @@ -133,10 +160,9 @@ char *resolveHostname(const char *addr) } // Test if we want to resolve an IPv6 address + bool IPv6 = false; if(strstr(addr,":") != NULL) - { IPv6 = true; - } // Initialize resolver subroutines if trying to resolve for the first time // res_init() reads resolv.conf to get the default domain name and name server @@ -248,7 +274,7 @@ char *resolveHostname(const char *addr) } else { - // No (he == NULL) or invalid (valid_hostname returned false) hostname found + // No hostname found (empty PTR) hostname = strdup(""); if(config.debug & DEBUG_RESOLVER) @@ -270,21 +296,14 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos) char* oldname = strdup(getstr(oldnamepos)); unlock_shm(); - // Test if we want to resolve an IPv6 address - bool IPv6 = false; - if(strstr(ipaddr,":") != NULL) - { - IPv6 = true; - } - - if( (IPv6 && !config.resolveIPv6) || - (!IPv6 && !config.resolveIPv4)) + // Test if we want to resolve host names, otherwise all calls to resolveHostname() + // and getNameFromIP() can be skipped as they will all return empty names (= no records) + if(!resolve_this_name(ipaddr)) { if(config.debug & DEBUG_RESOLVER) - { - logg(" ---> \"\" (configured to not resolve %s host names)", - IPv6 ? "IPv6" : "IPv4"); - } + logg(" ---> \"\" (configured to not resolve host name)"); + + // Return fixed position of empty string return 0; } diff --git a/src/resolve.h b/src/resolve.h index c8516bf9..6598faa9 100644 --- a/src/resolve.h +++ b/src/resolve.h @@ -12,6 +12,8 @@ void *DNSclient_thread(void *val); char *resolveHostname(const char *addr); +bool resolve_names(void) __attribute__((pure)); +bool resolve_this_name(const char *ipaddr) __attribute__((pure)); // musl does not define MAXHOSTNAMELEN // If it is not defined, we set the value