2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-01-30 08:06:51 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-11-26 13:38:24 -03:00
|
|
|
#include "LookupServer.h"
|
2022-02-25 07:18:30 -03:00
|
|
|
#include "ConnectionFromClient.h"
|
2021-01-24 11:28:26 -03:00
|
|
|
#include <AK/Debug.h>
|
2019-11-26 13:38:24 -03:00
|
|
|
#include <AK/HashMap.h>
|
2021-05-14 12:17:26 -03:00
|
|
|
#include <AK/Random.h>
|
2019-11-26 13:38:24 -03:00
|
|
|
#include <AK/String.h>
|
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/ConfigFile.h>
|
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
|
#include <LibCore/LocalServer.h>
|
2022-02-06 14:13:04 -03:00
|
|
|
#include <LibCore/Stream.h>
|
2022-04-14 19:58:40 -03:00
|
|
|
#include <LibDNS/Packet.h>
|
2019-11-26 13:38:24 -03:00
|
|
|
#include <stdio.h>
|
2021-05-23 11:35:03 -03:00
|
|
|
#include <time.h>
|
2019-11-26 13:38:24 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2021-02-04 13:07:45 -03:00
|
|
|
namespace LookupServer {
|
|
|
|
|
|
2021-02-05 07:20:05 -03:00
|
|
|
static LookupServer* s_the;
|
2021-06-07 09:16:57 -03:00
|
|
|
// NOTE: This is the TTL we return for the hostname or answers from /etc/hosts.
|
|
|
|
|
static constexpr u32 s_static_ttl = 86400;
|
2021-02-05 07:20:05 -03:00
|
|
|
|
|
|
|
|
LookupServer& LookupServer::the()
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(s_the);
|
2021-02-05 07:20:05 -03:00
|
|
|
return *s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 13:38:24 -03:00
|
|
|
LookupServer::LookupServer()
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(s_the == nullptr);
|
2021-02-05 07:20:05 -03:00
|
|
|
s_the = this;
|
|
|
|
|
|
2022-02-06 10:33:42 -03:00
|
|
|
auto config = Core::ConfigFile::open_for_system("LookupServer").release_value_but_fixme_should_propagate_errors();
|
2021-04-29 16:46:15 -03:00
|
|
|
dbgln("Using network config file at {}", config->filename());
|
2020-10-25 12:30:44 -03:00
|
|
|
m_nameservers = config->read_entry("DNS", "Nameservers", "1.1.1.1,1.0.0.1").split(',');
|
2019-11-26 13:38:24 -03:00
|
|
|
|
|
|
|
|
load_etc_hosts();
|
|
|
|
|
|
2021-06-07 15:26:09 -03:00
|
|
|
auto maybe_file_watcher = Core::FileWatcher::create();
|
|
|
|
|
// NOTE: If this happens during startup, something is very wrong.
|
|
|
|
|
if (maybe_file_watcher.is_error()) {
|
|
|
|
|
dbgln("Core::FileWatcher::create(): {}", maybe_file_watcher.error());
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
m_file_watcher = maybe_file_watcher.release_value();
|
|
|
|
|
|
|
|
|
|
m_file_watcher->on_change = [this](auto&) {
|
|
|
|
|
dbgln("Reloading '/etc/hosts' because it was changed.");
|
|
|
|
|
load_etc_hosts();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto result = m_file_watcher->add_watch("/etc/hosts", Core::FileWatcherEvent::Type::ContentModified | Core::FileWatcherEvent::Type::Deleted);
|
|
|
|
|
// NOTE: If this happens during startup, something is very wrong.
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
dbgln("Core::FileWatcher::add_watch(): {}", result.error());
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
} else if (!result.value()) {
|
|
|
|
|
dbgln("Core::FileWatcher::add_watch(): {}", result.value());
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
LookupServer: Implement a DNS server :^)
LookupServer can now itself server as a DNS server! To service DNS clients, it
uses the exact same lookup logic as it does for LibIPC clients. Namely, it will
synthesize records for data from /etc/hosts on its own (you can use this to
configure host names for your domain!), and forward other questions to
configured upstream DNS servers. On top of that, it implements its own caching,
so once a DNS resource record has been obtained from an upstream server,
LookupServer will cache it locally for faster future lookups.
The DNS server part of LookupServer is disabled by default, because it requires
you to run it as root (for it to bind to the port 53) and on boot, and we don't
want either by default. If you want to try it, modify SystemServer.ini like so:
[LookupServer]
Socket=/tmp/portal/lookup
SocketPermissions=666
Priority=low
KeepAlive=1
User=root
BootModes=text,graphical
and enable server mode in LookupServer.ini like so:
[DNS]
Nameservers=...
EnableServer=1
If in the future we implement socket takeover for IP sockets, these limitations
may be lifted.
2021-02-14 11:24:23 -03:00
|
|
|
if (config->read_bool_entry("DNS", "EnableServer")) {
|
|
|
|
|
m_dns_server = DNSServer::construct(this);
|
|
|
|
|
// TODO: drop root privileges here.
|
|
|
|
|
}
|
2021-05-04 08:47:42 -03:00
|
|
|
m_mdns = MulticastDNS::construct(this);
|
LookupServer: Implement a DNS server :^)
LookupServer can now itself server as a DNS server! To service DNS clients, it
uses the exact same lookup logic as it does for LibIPC clients. Namely, it will
synthesize records for data from /etc/hosts on its own (you can use this to
configure host names for your domain!), and forward other questions to
configured upstream DNS servers. On top of that, it implements its own caching,
so once a DNS resource record has been obtained from an upstream server,
LookupServer will cache it locally for faster future lookups.
The DNS server part of LookupServer is disabled by default, because it requires
you to run it as root (for it to bind to the port 53) and on boot, and we don't
want either by default. If you want to try it, modify SystemServer.ini like so:
[LookupServer]
Socket=/tmp/portal/lookup
SocketPermissions=666
Priority=low
KeepAlive=1
User=root
BootModes=text,graphical
and enable server mode in LookupServer.ini like so:
[DNS]
Nameservers=...
EnableServer=1
If in the future we implement socket takeover for IP sockets, these limitations
may be lifted.
2021-02-14 11:24:23 -03:00
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
m_server = MUST(IPC::MultiServer<ConnectionFromClient>::try_create());
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LookupServer::load_etc_hosts()
|
|
|
|
|
{
|
2021-06-07 15:26:09 -03:00
|
|
|
m_etc_hosts.clear();
|
2022-04-14 19:58:40 -03:00
|
|
|
auto add_answer = [this](Name const& name, RecordType record_type, String data) {
|
|
|
|
|
m_etc_hosts.ensure(name).empend(name, record_type, RecordClass::IN, s_static_ttl, move(data), false);
|
2021-02-14 10:41:43 -03:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
auto file = Core::File::construct("/etc/hosts");
|
2021-06-07 09:16:57 -03:00
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
|
|
|
|
dbgln("Failed to open '/etc/hosts'");
|
2019-11-26 13:38:24 -03:00
|
|
|
return;
|
2021-06-07 09:16:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 line_number = 0;
|
2019-11-26 13:38:24 -03:00
|
|
|
while (!file->eof()) {
|
2021-06-07 09:16:57 -03:00
|
|
|
auto original_line = file->read_line(1024);
|
|
|
|
|
++line_number;
|
|
|
|
|
if (original_line.is_empty())
|
2019-11-26 13:38:24 -03:00
|
|
|
break;
|
2021-06-07 09:16:57 -03:00
|
|
|
auto trimmed_line = original_line.view().trim_whitespace();
|
2022-07-11 14:32:29 -03:00
|
|
|
auto replaced_line = trimmed_line.replace(" "sv, "\t"sv, ReplaceMode::All);
|
2021-10-03 19:49:49 -03:00
|
|
|
auto fields = replaced_line.split_view('\t', false);
|
2021-06-07 09:16:57 -03:00
|
|
|
|
|
|
|
|
if (fields.size() < 2) {
|
|
|
|
|
dbgln("Failed to parse line {} from '/etc/hosts': '{}'", line_number, original_line);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fields.size() > 2)
|
|
|
|
|
dbgln("Line {} from '/etc/hosts' ('{}') has more than two parts, only the first two are used.", line_number, original_line);
|
|
|
|
|
|
|
|
|
|
auto maybe_address = IPv4Address::from_string(fields[0]);
|
|
|
|
|
if (!maybe_address.has_value()) {
|
|
|
|
|
dbgln("Failed to parse line {} from '/etc/hosts': '{}'", line_number, original_line);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto raw_addr = maybe_address->to_in_addr_t();
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
Name name { fields[1] };
|
|
|
|
|
add_answer(name, RecordType::A, String { (char const*)&raw_addr, sizeof(raw_addr) });
|
2019-11-26 13:38:24 -03:00
|
|
|
|
|
|
|
|
StringBuilder builder;
|
2021-06-07 09:16:57 -03:00
|
|
|
builder.append(maybe_address->to_string_reversed());
|
2022-07-11 14:32:29 -03:00
|
|
|
builder.append(".in-addr.arpa"sv);
|
2022-04-14 19:58:40 -03:00
|
|
|
add_answer(builder.to_string(), RecordType::PTR, name.as_string());
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 09:44:30 -03:00
|
|
|
static String get_hostname()
|
|
|
|
|
{
|
2022-07-05 11:19:57 -03:00
|
|
|
char buffer[_POSIX_HOST_NAME_MAX];
|
2021-06-07 09:44:30 -03:00
|
|
|
VERIFY(gethostname(buffer, sizeof(buffer)) == 0);
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, RecordType record_type)
|
2019-11-26 13:38:24 -03:00
|
|
|
{
|
2021-05-01 16:10:08 -03:00
|
|
|
dbgln_if(LOOKUPSERVER_DEBUG, "Got request for '{}'", name.as_string());
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
Vector<Answer> answers;
|
|
|
|
|
auto add_answer = [&](Answer const& answer) {
|
|
|
|
|
Answer answer_with_original_case {
|
2021-02-14 10:57:41 -03:00
|
|
|
name,
|
|
|
|
|
answer.type(),
|
|
|
|
|
answer.class_code(),
|
|
|
|
|
answer.ttl(),
|
2021-05-07 18:58:46 -03:00
|
|
|
answer.record_data(),
|
|
|
|
|
answer.mdns_cache_flush(),
|
2021-02-14 10:57:41 -03:00
|
|
|
};
|
|
|
|
|
answers.append(answer_with_original_case);
|
|
|
|
|
};
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2021-06-07 09:44:30 -03:00
|
|
|
// First, try /etc/hosts.
|
2021-12-05 08:10:17 -03:00
|
|
|
if (auto local_answers = m_etc_hosts.find(name); local_answers != m_etc_hosts.end()) {
|
|
|
|
|
for (auto& answer : local_answers->value) {
|
2021-02-14 10:41:43 -03:00
|
|
|
if (answer.type() == record_type)
|
2021-02-14 10:57:41 -03:00
|
|
|
add_answer(answer);
|
2021-02-14 10:41:43 -03:00
|
|
|
}
|
2021-02-14 10:57:41 -03:00
|
|
|
if (!answers.is_empty())
|
|
|
|
|
return answers;
|
2021-02-14 10:41:43 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-07 09:44:30 -03:00
|
|
|
// Second, try the hostname.
|
|
|
|
|
// NOTE: We don't cache the hostname since it could change during runtime.
|
2022-04-14 19:58:40 -03:00
|
|
|
if (record_type == RecordType::A && get_hostname() == name) {
|
2021-06-07 09:44:30 -03:00
|
|
|
IPv4Address address = { 127, 0, 0, 1 };
|
|
|
|
|
auto raw_address = address.to_in_addr_t();
|
2022-04-14 19:58:40 -03:00
|
|
|
Answer answer { name, RecordType::A, RecordClass::IN, s_static_ttl, String { (char const*)&raw_address, sizeof(raw_address) }, false };
|
2021-06-07 09:44:30 -03:00
|
|
|
answers.append(move(answer));
|
|
|
|
|
return answers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Third, try our cache.
|
2021-12-05 08:10:17 -03:00
|
|
|
if (auto cached_answers = m_lookup_cache.find(name); cached_answers != m_lookup_cache.end()) {
|
|
|
|
|
for (auto& answer : cached_answers->value) {
|
2021-02-14 10:48:14 -03:00
|
|
|
// TODO: Actually remove expired answers from the cache.
|
2021-02-14 10:20:19 -03:00
|
|
|
if (answer.type() == record_type && !answer.has_expired()) {
|
2021-05-01 16:10:08 -03:00
|
|
|
dbgln_if(LOOKUPSERVER_DEBUG, "Cache hit: {} -> {}", name.as_string(), answer.record_data());
|
2021-02-14 10:57:41 -03:00
|
|
|
add_answer(answer);
|
2020-01-26 11:42:03 -03:00
|
|
|
}
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
2021-02-14 10:57:41 -03:00
|
|
|
if (!answers.is_empty())
|
|
|
|
|
return answers;
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-07 09:44:30 -03:00
|
|
|
// Fourth, look up .local names using mDNS instead of DNS nameservers.
|
2022-07-11 14:32:29 -03:00
|
|
|
if (name.as_string().ends_with(".local"sv)) {
|
2021-05-04 08:47:42 -03:00
|
|
|
answers = m_mdns->lookup(name, record_type);
|
|
|
|
|
for (auto& answer : answers)
|
|
|
|
|
put_in_cache(answer);
|
|
|
|
|
return answers;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 09:44:30 -03:00
|
|
|
// Fifth, ask the upstream nameservers.
|
2021-02-14 10:48:14 -03:00
|
|
|
for (auto& nameserver : m_nameservers) {
|
2021-05-01 16:10:08 -03:00
|
|
|
dbgln_if(LOOKUPSERVER_DEBUG, "Doing lookup using nameserver '{}'", nameserver);
|
2021-02-14 10:48:14 -03:00
|
|
|
bool did_get_response = false;
|
|
|
|
|
int retries = 3;
|
2022-04-14 19:58:40 -03:00
|
|
|
Vector<Answer> upstream_answers;
|
2021-02-14 10:48:14 -03:00
|
|
|
do {
|
2022-07-02 15:36:41 -03:00
|
|
|
auto upstream_answers_or_error = lookup(name, nameserver, did_get_response, record_type);
|
|
|
|
|
if (upstream_answers_or_error.is_error())
|
|
|
|
|
continue;
|
|
|
|
|
upstream_answers = upstream_answers_or_error.release_value();
|
2021-02-14 10:48:14 -03:00
|
|
|
if (did_get_response)
|
|
|
|
|
break;
|
|
|
|
|
} while (--retries);
|
2021-02-14 10:57:41 -03:00
|
|
|
if (!upstream_answers.is_empty()) {
|
|
|
|
|
for (auto& answer : upstream_answers)
|
|
|
|
|
add_answer(answer);
|
2021-02-14 10:48:14 -03:00
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
if (!did_get_response)
|
|
|
|
|
dbgln("Never got a response from '{}', trying next nameserver", nameserver);
|
|
|
|
|
else
|
|
|
|
|
dbgln("Received response from '{}' but no result(s), trying next nameserver", nameserver);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-07 09:44:30 -03:00
|
|
|
|
|
|
|
|
// Sixth, fail.
|
2021-02-14 10:57:41 -03:00
|
|
|
if (answers.is_empty()) {
|
2021-02-14 11:01:02 -03:00
|
|
|
dbgln("Tried all nameservers but never got a response :(");
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2021-02-14 10:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2021-03-17 12:30:02 -03:00
|
|
|
return answers;
|
2021-02-14 10:48:14 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, String const& nameserver, bool& did_get_response, RecordType record_type, ShouldRandomizeCase should_randomize_case)
|
2021-02-14 10:48:14 -03:00
|
|
|
{
|
2022-04-14 19:58:40 -03:00
|
|
|
Packet request;
|
2021-02-06 11:48:12 -03:00
|
|
|
request.set_is_query();
|
2021-05-14 12:17:26 -03:00
|
|
|
request.set_id(get_random_uniform(UINT16_MAX));
|
2022-04-14 19:58:40 -03:00
|
|
|
Name name_in_question = name;
|
2021-02-14 10:25:48 -03:00
|
|
|
if (should_randomize_case == ShouldRandomizeCase::Yes)
|
|
|
|
|
name_in_question.randomize_case();
|
2022-04-14 19:58:40 -03:00
|
|
|
request.add_question({ name_in_question, record_type, RecordClass::IN, false });
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2020-01-26 08:27:18 -03:00
|
|
|
auto buffer = request.to_byte_buffer();
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2022-02-06 14:13:04 -03:00
|
|
|
auto udp_socket = TRY(Core::Stream::UDPSocket::connect(nameserver, 53, Time::from_seconds(1)));
|
|
|
|
|
TRY(udp_socket->set_blocking(true));
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2022-02-06 14:13:04 -03:00
|
|
|
TRY(udp_socket->write(buffer));
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2020-01-26 10:47:03 -03:00
|
|
|
u8 response_buffer[4096];
|
2022-04-15 09:33:02 -03:00
|
|
|
int nrecv = TRY(udp_socket->read({ response_buffer, sizeof(response_buffer) })).size();
|
2022-02-06 14:13:04 -03:00
|
|
|
if (udp_socket->is_eof())
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2020-10-25 12:07:31 -03:00
|
|
|
did_get_response = true;
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
auto o_response = Packet::from_raw_packet(response_buffer, nrecv);
|
2020-01-26 08:27:18 -03:00
|
|
|
if (!o_response.has_value())
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2020-01-26 08:27:18 -03:00
|
|
|
auto& response = o_response.value();
|
2019-11-26 13:38:24 -03:00
|
|
|
|
2020-01-26 08:27:18 -03:00
|
|
|
if (response.id() != request.id()) {
|
2020-10-25 11:46:04 -03:00
|
|
|
dbgln("LookupServer: ID mismatch ({} vs {}) :(", response.id(), request.id());
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
2020-01-26 09:45:15 -03:00
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
if (response.code() == Packet::Code::REFUSED) {
|
2020-01-26 09:45:15 -03:00
|
|
|
if (should_randomize_case == ShouldRandomizeCase::Yes) {
|
|
|
|
|
// Retry with 0x20 case randomization turned off.
|
2021-02-14 09:02:02 -03:00
|
|
|
return lookup(name, nameserver, did_get_response, record_type, ShouldRandomizeCase::No);
|
2020-01-26 09:45:15 -03:00
|
|
|
}
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2020-01-26 09:45:15 -03:00
|
|
|
}
|
|
|
|
|
|
2020-01-26 09:06:00 -03:00
|
|
|
if (response.question_count() != request.question_count()) {
|
2020-10-25 11:46:04 -03:00
|
|
|
dbgln("LookupServer: Question count ({} vs {}) :(", response.question_count(), request.question_count());
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
2020-01-26 09:06:00 -03:00
|
|
|
|
2022-03-27 11:41:12 -03:00
|
|
|
// Verify the questions in our request and in their response match, ignoring case.
|
2020-01-26 09:06:00 -03:00
|
|
|
for (size_t i = 0; i < request.question_count(); ++i) {
|
|
|
|
|
auto& request_question = request.questions()[i];
|
|
|
|
|
auto& response_question = response.questions()[i];
|
2022-03-27 11:41:12 -03:00
|
|
|
bool match = request_question.class_code() == response_question.class_code()
|
2021-02-14 09:02:02 -03:00
|
|
|
&& request_question.record_type() == response_question.record_type()
|
2022-03-27 11:41:12 -03:00
|
|
|
&& request_question.name().as_string().equals_ignoring_case(response_question.name().as_string());
|
|
|
|
|
if (!match) {
|
2020-10-25 11:46:04 -03:00
|
|
|
dbgln("Request and response questions do not match");
|
2021-02-14 09:02:02 -03:00
|
|
|
dbgln(" Request: name=_{}_, type={}, class={}", request_question.name().as_string(), response_question.record_type(), response_question.class_code());
|
|
|
|
|
dbgln(" Response: name=_{}_, type={}, class={}", response_question.name().as_string(), response_question.record_type(), response_question.class_code());
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2020-01-26 09:06:00 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 08:27:18 -03:00
|
|
|
if (response.answer_count() < 1) {
|
2021-02-14 11:01:02 -03:00
|
|
|
dbgln("LookupServer: No answers :(");
|
2022-04-14 19:58:40 -03:00
|
|
|
return Vector<Answer> {};
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
Vector<Answer, 8> answers;
|
2020-01-26 08:27:18 -03:00
|
|
|
for (auto& answer : response.answers()) {
|
2021-02-14 10:20:19 -03:00
|
|
|
put_in_cache(answer);
|
2021-01-30 08:06:51 -03:00
|
|
|
if (answer.type() != record_type)
|
2020-05-03 09:18:53 -03:00
|
|
|
continue;
|
2021-02-14 10:57:41 -03:00
|
|
|
answers.append(answer);
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
|
|
|
|
|
2021-03-17 12:30:02 -03:00
|
|
|
return answers;
|
2019-11-26 13:38:24 -03:00
|
|
|
}
|
2021-02-04 13:07:45 -03:00
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
void LookupServer::put_in_cache(Answer const& answer)
|
2021-02-14 10:20:19 -03:00
|
|
|
{
|
|
|
|
|
if (answer.has_expired())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Prevent the cache from growing too big.
|
|
|
|
|
// TODO: Evict least used entries.
|
|
|
|
|
if (m_lookup_cache.size() >= 256)
|
|
|
|
|
m_lookup_cache.remove(m_lookup_cache.begin());
|
|
|
|
|
|
2021-02-14 10:33:16 -03:00
|
|
|
auto it = m_lookup_cache.find(answer.name());
|
2021-02-14 10:20:19 -03:00
|
|
|
if (it == m_lookup_cache.end())
|
2021-02-14 10:33:16 -03:00
|
|
|
m_lookup_cache.set(answer.name(), { answer });
|
2021-05-09 11:49:23 -03:00
|
|
|
else {
|
|
|
|
|
if (answer.mdns_cache_flush()) {
|
|
|
|
|
auto now = time(nullptr);
|
|
|
|
|
|
2022-04-14 19:58:40 -03:00
|
|
|
it->value.remove_all_matching([&](Answer const& other_answer) {
|
2021-05-09 11:49:23 -03:00
|
|
|
if (other_answer.type() != answer.type() || other_answer.class_code() != answer.class_code())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (other_answer.received_time() >= now - 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
dbgln_if(LOOKUPSERVER_DEBUG, "Removing cache entry: {}", other_answer.name());
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-02-14 10:20:19 -03:00
|
|
|
it->value.append(answer);
|
2021-05-09 11:49:23 -03:00
|
|
|
}
|
2021-02-14 10:20:19 -03:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 13:07:45 -03:00
|
|
|
}
|