From 0491d34d0c56a6de24fa9e73ff16c1a0774f3d2c Mon Sep 17 00:00:00 2001 From: Christopher Dumas Date: Thu, 6 Jun 2019 12:41:06 -0700 Subject: [PATCH] LookupServer: use /etc/hosts even for reverse lookups --- Servers/LookupServer/main.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Servers/LookupServer/main.cpp b/Servers/LookupServer/main.cpp index 9929565a11..3281ff7c57 100644 --- a/Servers/LookupServer/main.cpp +++ b/Servers/LookupServer/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -40,12 +41,13 @@ int main(int argc, char** argv) auto DNS_IP = config->read_entry("DNS", "IPAddress", "127.0.0.53"); dbgprintf("LookupServer: Loading hosts from /etc/hosts:\n"); - HashMap dns_custom_hostnames; + HashMap dns_custom_hostnames; auto* file = fopen("/etc/hosts", "r"); auto linebuf = ByteBuffer::create_uninitialized(256); while (fgets((char*)linebuf.pointer(), linebuf.size(), file)) { auto str_line = String::copy(linebuf); auto fields = str_line.split('\t'); + auto sections = fields[0].split('.'); IPv4Address addr { (byte)atoi(sections[0].characters()), @@ -57,8 +59,19 @@ int main(int argc, char** argv) while ((fields[1][len++]) != -123) ; auto name = fields[1].substring(0, len - 3); - dbgprintf("LookupServer: Hosts: %s\t%s\n", name.characters(), addr.to_string().characters()); - dns_custom_hostnames.set(name, addr); + + dns_custom_hostnames.set(name, addr.to_string()); + + IPv4Address addr2 { + (byte)atoi(sections[3].characters()), + (byte)atoi(sections[2].characters()), + (byte)atoi(sections[1].characters()), + (byte)atoi(sections[0].characters()), + }; + auto sb = StringBuilder(); + sb.append(addr2.to_string()); + sb.append(".in-addr.arpa"); + dns_custom_hostnames.set(sb.to_string(), name); } int server_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); @@ -131,9 +144,12 @@ int main(int argc, char** argv) Vector responses; + for (auto& key : dns_custom_hostnames.keys()) { + dbgprintf("Known Hostname: %s\n", key.characters()); + } if (dns_custom_hostnames.contains(hostname)) { - addresses.append(dns_custom_hostnames.get(hostname)); - dbgprintf("LookupServer: Found preconfigured host (from /etc/hosts): %s\n", addresses[0].to_string().characters()); + responses.append(dns_custom_hostnames.get(hostname)); + dbgprintf("LookupServer: Found preconfigured host (from /etc/hosts): %s\n", responses[0].characters()); } else if (!hostname.is_empty()) { bool did_timeout; int retries = 3;