mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 02:03:06 -05:00
LookupServer: Store /etc/hosts as Vector<DNSAnswer>
...just like we store m_lookup_cache, in other words. This immediately lets us match on types: for instance we will now only resolve 1.0.0.127.in-addr.arpa to localhost if asked for type PTR, not for type A. In the future, this could also let us have the same /etc/hosts name resolve to *multiple* addresses.
This commit is contained in:
parent
e9387e43db
commit
af6aac8c55
Notes:
sideshowbarker
2024-07-18 22:16:49 +09:00
Author: https://github.com/bugaevc Commit: https://github.com/SerenityOS/serenity/commit/af6aac8c55d Pull-request: https://github.com/SerenityOS/serenity/pull/5345
2 changed files with 26 additions and 6 deletions
|
@ -79,6 +79,19 @@ LookupServer::LookupServer()
|
||||||
|
|
||||||
void LookupServer::load_etc_hosts()
|
void LookupServer::load_etc_hosts()
|
||||||
{
|
{
|
||||||
|
// The TTL we return for static data from /etc/hosts.
|
||||||
|
// The value here is 1 day.
|
||||||
|
static constexpr u32 static_ttl = 86400;
|
||||||
|
|
||||||
|
auto add_answer = [this](const DNSName& name, unsigned short record_type, String data) {
|
||||||
|
auto it = m_etc_hosts.find(name);
|
||||||
|
if (it == m_etc_hosts.end()) {
|
||||||
|
m_etc_hosts.set(name, {});
|
||||||
|
it = m_etc_hosts.find(name);
|
||||||
|
}
|
||||||
|
it->value.empend(name, record_type, (u16)C_IN, static_ttl, data);
|
||||||
|
};
|
||||||
|
|
||||||
auto file = Core::File::construct("/etc/hosts");
|
auto file = Core::File::construct("/etc/hosts");
|
||||||
if (!file->open(Core::IODevice::ReadOnly))
|
if (!file->open(Core::IODevice::ReadOnly))
|
||||||
return;
|
return;
|
||||||
|
@ -98,7 +111,7 @@ void LookupServer::load_etc_hosts()
|
||||||
auto raw_addr = addr.to_in_addr_t();
|
auto raw_addr = addr.to_in_addr_t();
|
||||||
|
|
||||||
DNSName name = fields[1];
|
DNSName name = fields[1];
|
||||||
m_etc_hosts.set(name, String { (const char*)&raw_addr, sizeof(raw_addr) });
|
add_answer(name, T_A, String { (const char*)&raw_addr, sizeof(raw_addr) });
|
||||||
|
|
||||||
IPv4Address reverse_addr {
|
IPv4Address reverse_addr {
|
||||||
(u8)atoi(sections[3].characters()),
|
(u8)atoi(sections[3].characters()),
|
||||||
|
@ -109,7 +122,7 @@ void LookupServer::load_etc_hosts()
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(reverse_addr.to_string());
|
builder.append(reverse_addr.to_string());
|
||||||
builder.append(".in-addr.arpa");
|
builder.append(".in-addr.arpa");
|
||||||
m_etc_hosts.set(builder.to_string(), name);
|
add_answer(builder.to_string(), T_PTR, name.as_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,9 +134,16 @@ Vector<String> LookupServer::lookup(const DNSName& name, unsigned short record_t
|
||||||
|
|
||||||
Vector<String> responses;
|
Vector<String> responses;
|
||||||
|
|
||||||
if (auto known_host = m_etc_hosts.get(name); known_host.has_value()) {
|
if (auto local_answers = m_etc_hosts.get(name); local_answers.has_value()) {
|
||||||
responses.append(known_host.value());
|
for (auto& answer : local_answers.value()) {
|
||||||
} else if (!name.as_string().is_empty()) {
|
if (answer.type() == record_type)
|
||||||
|
responses.append(answer.name().as_string());
|
||||||
|
}
|
||||||
|
if (!responses.is_empty())
|
||||||
|
return responses;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!name.as_string().is_empty()) {
|
||||||
for (auto& nameserver : m_nameservers) {
|
for (auto& nameserver : m_nameservers) {
|
||||||
#if LOOKUPSERVER_DEBUG
|
#if LOOKUPSERVER_DEBUG
|
||||||
dbgln("Doing lookup using nameserver '{}'", nameserver);
|
dbgln("Doing lookup using nameserver '{}'", nameserver);
|
||||||
|
|
|
@ -52,7 +52,7 @@ private:
|
||||||
|
|
||||||
RefPtr<Core::LocalServer> m_local_server;
|
RefPtr<Core::LocalServer> m_local_server;
|
||||||
Vector<String> m_nameservers;
|
Vector<String> m_nameservers;
|
||||||
HashMap<DNSName, String, DNSName::Traits> m_etc_hosts;
|
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_etc_hosts;
|
||||||
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache;
|
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue