From 63a5717bc76a99ac1364ea1633692f7df0651f17 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Wed, 20 Nov 2024 13:40:52 +0100 Subject: [PATCH] LibDNS: Immediately resolve IPv4/IPv6 "hostnames" if A/AAAA is queried This makes e.g. lookup(192.168.1.1, A) resolve to the IP instead of querying DNS for the IP. --- Libraries/LibDNS/Resolver.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Libraries/LibDNS/Resolver.h b/Libraries/LibDNS/Resolver.h index 316fd47ee89..97addc70e90 100644 --- a/Libraries/LibDNS/Resolver.h +++ b/Libraries/LibDNS/Resolver.h @@ -208,6 +208,24 @@ public: auto promise = Core::Promise>::construct(); + if (auto maybe_ipv4 = IPv4Address::from_string(name); maybe_ipv4.has_value()) { + if (desired_types.contains_slow(Messages::ResourceType::A)) { + auto result = make_ref_counted(Messages::DomainName {}); + result->add_record({ .name = {}, .type = Messages::ResourceType::A, .class_ = Messages::Class::IN, .ttl = 0, .record = Messages::Records::A { maybe_ipv4.release_value() }, .raw = {} }); + promise->resolve(move(result)); + return promise; + } + } + + if (auto maybe_ipv6 = IPv6Address::from_string(name); maybe_ipv6.has_value()) { + if (desired_types.contains_slow(Messages::ResourceType::AAAA)) { + auto result = make_ref_counted(Messages::DomainName {}); + result->add_record({ .name = {}, .type = Messages::ResourceType::AAAA, .class_ = Messages::Class::IN, .ttl = 0, .record = Messages::Records::AAAA { maybe_ipv6.release_value() }, .raw = {} }); + promise->resolve(move(result)); + return promise; + } + } + if (auto result = lookup_in_cache(name, class_, desired_types)) { promise->resolve(result.release_nonnull()); return promise;