diff --git a/src/args.c b/src/args.c index 4feaed77..29b24e2f 100644 --- a/src/args.c +++ b/src/args.c @@ -507,12 +507,12 @@ void parse_args(int argc, char* argv[]) // Need to get dns.port and the resolver settings readFTLconf(&config, false); - char *name = resolveHostname(argv[2]); + char *name = resolveHostname(argv[2], true); if(name == NULL) exit(EXIT_FAILURE); // Print result - printf("%s -> %s\n", argv[2], name); + printf("%s\n", name); free(name); exit(EXIT_SUCCESS); } diff --git a/src/database/network-table.c b/src/database/network-table.c index 8c0e9a1f..b9da8068 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -19,7 +19,7 @@ #include "../datastructure.h" // struct config #include "../config/config.h" -// resolveHostname() +// resolve_this_name() #include "../resolve.h" // killed #include "../signals.h" diff --git a/src/resolve.c b/src/resolve.c index ad8462a0..11d710b1 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -43,7 +43,7 @@ struct DNS_HEADER bool rd :1; // recursion desired bool tc :1; // truncated message - bool aa :1; // authoritive answer + bool aa :1; // authoritative answer uint8_t opcode :4; // purpose of message bool qr :1; // query/response flag @@ -349,13 +349,13 @@ static void __attribute__((nonnull(1,3))) name_toDNS(unsigned char *dns, const s *dns++='\0'; } -char *__attribute__((malloc)) resolveHostname(const char *addr) +char *__attribute__((malloc)) resolveHostname(const char *addr, const bool force) { // Get host name char *hostn = NULL; // Check if we want to resolve host names - if(!resolve_this_name(addr)) + if(!force && !resolve_this_name(addr)) { log_debug(DEBUG_RESOLVER, "Configured to not resolve host name for %s", addr); @@ -383,15 +383,6 @@ char *__attribute__((malloc)) resolveHostname(const char *addr) return hostn; } - // Check if we want to resolve host names - if(!resolve_this_name(addr)) - { - log_debug(DEBUG_RESOLVER, "Configured to not resolve host name for %s", addr); - - // Return an empty host name - return strdup(""); - } - // Test if we want to resolve an IPv6 address bool IPv6 = false; if(strstr(addr,":") != NULL) @@ -510,7 +501,7 @@ static size_t resolveAndAddHostname(size_t ippos, size_t oldnamepos) // 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); + char *newname = resolveHostname(ipaddr, false); // If no hostname was found, try to obtain hostname from the network table // This may be disabled due to a user setting diff --git a/src/resolve.h b/src/resolve.h index 154c7c7b..2dfc50e1 100644 --- a/src/resolve.h +++ b/src/resolve.h @@ -11,7 +11,7 @@ #define RESOLVE_H void *DNSclient_thread(void *val); -char *resolveHostname(const char *addr) __attribute__((malloc)); +char *resolveHostname(const char *addr, const bool force) __attribute__((malloc)); bool resolve_names(void) __attribute__((pure)); bool resolve_this_name(const char *ipaddr) __attribute__((pure)); diff --git a/test/test_suite.bats b/test/test_suite.bats index 0ea50189..d341a080 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -1570,6 +1570,15 @@ [[ ${lines[0]} == "eae293f0c30369935a7457a789658bedebf92d544e7526bc43aa07883a597fa9 test/test.pem" ]] } +@test "Internal IP -> name resolution works" { + run bash -c "./pihole-FTL ptr 127.0.0.1 | tail -n1" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == "localhost" ]] + run bash -c "./pihole-FTL ptr ::1 | tail -n1" + printf "%s\n" "${lines[@]}" + [[ ${lines[0]} == "localhost" ]] +} + @test "API validation" { run python3 test/api/checkAPI.py printf "%s\n" "${lines[@]}"