diff --git a/src/database/network-table.c b/src/database/network-table.c index 561331e4..40c648b0 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -1581,82 +1581,6 @@ void updateMACVendorRecords(void) dbclose(); } -char *__attribute__((malloc)) getDatabaseHostname(const char *ipaddr) -{ - // Test if this is an IPv6 address - bool IPv6 = false; - if(ipaddr != NULL && strstr(ipaddr,":") != NULL) - { - IPv6 = true; - } - - // Do we want to resolve IPv4/IPv6 names at all? - if( (IPv6 && !config.resolveIPv6) || - (!IPv6 && !config.resolveIPv4)) - { - if(config.debug & DEBUG_RESOLVER) - { - logg(" ---> \"\" (configured to not resolve %s host names)", - IPv6 ? "IPv6" : "IPv4"); - } - return strdup(""); - } - - // Open pihole-FTL.db database file if needed - const bool db_already_open = FTL_DB_avail(); - if(!db_already_open && !dbopen()) - { - logg("getDatabaseHostname(\"%s\") - Failed to open DB", ipaddr); - return NULL; - } - - // Prepare SQLite statement - sqlite3_stmt *stmt = NULL; - const char *querystr = "SELECT name FROM network_addresses " - "WHERE name IS NOT NULL AND ip = ?;"; - int rc = sqlite3_prepare_v2(FTL_db, querystr, -1, &stmt, NULL); - if( rc != SQLITE_OK ){ - logg("getDatabaseHostname(\"%s\") - SQL error prepare: %s", - ipaddr, sqlite3_errstr(rc)); - if(!db_already_open) - dbclose(); - return strdup(""); - } - - // Bind ipaddr to prepared statement - if((rc = sqlite3_bind_text(stmt, 1, ipaddr, -1, SQLITE_STATIC)) != SQLITE_OK) - { - logg("getDatabaseHostname(\"%s\"): Failed to bind ip: %s", - ipaddr, sqlite3_errstr(rc)); - sqlite3_reset(stmt); - sqlite3_finalize(stmt); - if(!db_already_open) - dbclose(); - return strdup(""); - } - - char *hostname = NULL; - rc = sqlite3_step(stmt); - if(rc == SQLITE_ROW) - { - // Database record found (result might be empty) - hostname = strdup((char*)sqlite3_column_text(stmt, 0)); - } - else - { - // Not found or error (will be logged automatically through our SQLite3 hook) - hostname = strdup(""); - } - - // Finalize statement and close database handle - sqlite3_reset(stmt); - sqlite3_finalize(stmt); - if(!db_already_open) - dbclose(); - - return hostname; -} - // Get hardware address of device identified by IP address char *__attribute__((malloc)) getMACfromIP(const char *ipaddr) { diff --git a/src/database/network-table.h b/src/database/network-table.h index ebe0a272..1c958435 100644 --- a/src/database/network-table.h +++ b/src/database/network-table.h @@ -16,10 +16,9 @@ bool create_network_addresses_with_names_table(void); void parse_neighbor_cache(void); void updateMACVendorRecords(void); bool unify_hwaddr(void); -char* getDatabaseHostname(const char* ipaddr) __attribute__((malloc)); -char* __attribute__((malloc)) getMACfromIP(const char* ipaddr); -char* __attribute__((malloc)) getNameFromIP(const char* ipaddr); -char* __attribute__((malloc)) getIfaceFromIP(const char* ipaddr); +char *getMACfromIP(const char* ipaddr) __attribute__((malloc)); +char *getNameFromIP(const char* ipaddr) __attribute__((malloc)); +char *getIfaceFromIP(const char* ipaddr) __attribute__((malloc)); void resolveNetworkTableNames(void); #endif //NETWORKTABLE_H diff --git a/src/resolve.c b/src/resolve.c index a6390901..871492e0 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -134,17 +134,6 @@ char *resolveHostname(const char *addr) IPv6 = true; } - if( (IPv6 && !config.resolveIPv6) || - (!IPv6 && !config.resolveIPv4)) - { - if(config.debug & DEBUG_RESOLVER) - { - logg(" ---> \"\" (configured to not resolve %s host names)", - IPv6 ? "IPv6" : "IPv4"); - } - return strdup(""); - } - // 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 // address(es). If no server is given, the local host is tried. If no domain @@ -285,6 +274,24 @@ 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)) + { + if(config.debug & DEBUG_RESOLVER) + { + logg(" ---> \"\" (configured to not resolve %s host names)", + IPv6 ? "IPv6" : "IPv4"); + } + return 0; + } + // Important: Don't hold a lock while resolving as the main thread // (dnsmasq) needs to be operable during the call to resolveHostname() char* newname = resolveHostname(ipaddr); @@ -294,7 +301,7 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos) if(strlen(newname) == 0 && config.names_from_netdb) { free(newname); - newname = getDatabaseHostname(ipaddr); + newname = getNameFromIP(ipaddr); } // Only store new newname if it is valid and differs from oldname