mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:37:34 +00:00
LookupServer: Migrate from DeprecatedFile to File
This commit is contained in:
parent
f890b70eae
commit
b10106fc7d
2 changed files with 34 additions and 14 deletions
|
@ -6,13 +6,15 @@
|
||||||
|
|
||||||
#include "LookupServer.h"
|
#include "LookupServer.h"
|
||||||
#include "ConnectionFromClient.h"
|
#include "ConnectionFromClient.h"
|
||||||
|
#include <AK/BufferedStream.h>
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/Random.h>
|
#include <AK/Random.h>
|
||||||
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/ConfigFile.h>
|
#include <LibCore/ConfigFile.h>
|
||||||
#include <LibCore/DeprecatedFile.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/LocalServer.h>
|
#include <LibCore/LocalServer.h>
|
||||||
#include <LibDNS/Packet.h>
|
#include <LibDNS/Packet.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
@ -77,24 +79,39 @@ LookupServer::LookupServer()
|
||||||
|
|
||||||
void LookupServer::load_etc_hosts()
|
void LookupServer::load_etc_hosts()
|
||||||
{
|
{
|
||||||
m_etc_hosts.clear();
|
auto new_hosts_or_error = this->try_load_etc_hosts();
|
||||||
auto add_answer = [this](Name const& name, RecordType record_type, DeprecatedString data) {
|
if (new_hosts_or_error.is_error())
|
||||||
m_etc_hosts.ensure(name).empend(name, record_type, RecordClass::IN, s_static_ttl, move(data), false);
|
dbgln("Ignoring '/etc/hosts', keeping old values");
|
||||||
|
else
|
||||||
|
m_etc_hosts = new_hosts_or_error.release_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<HashMap<Name, Vector<Answer>, Name::Traits>> LookupServer::try_load_etc_hosts()
|
||||||
|
{
|
||||||
|
HashMap<Name, Vector<Answer>, Name::Traits> map;
|
||||||
|
auto add_answer = [&map](Name const& name, RecordType record_type, DeprecatedString data) -> ErrorOr<void> {
|
||||||
|
// FIXME: Since try_ensure does not return a reference to the contained value, we have to
|
||||||
|
// retrieve it separately. This is a try_ensure bug that should be fixed.
|
||||||
|
TRY(map.try_ensure(name, []() { return Vector<Answer> {}; }));
|
||||||
|
auto& entry = map.find(name)->value;
|
||||||
|
return entry.try_empend(name, record_type, RecordClass::IN, s_static_ttl, move(data), false);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto file = Core::DeprecatedFile::construct("/etc/hosts");
|
auto file_or_error = Core::File::open("/etc/hosts"sv, Core::File::OpenMode::Read);
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
if (file_or_error.is_error()) {
|
||||||
dbgln("Failed to open '/etc/hosts'");
|
dbgln("Failed to open '/etc/hosts'");
|
||||||
return;
|
return file_or_error.release_error();
|
||||||
}
|
}
|
||||||
|
auto file = TRY(Core::InputBufferedFile::create(file_or_error.release_value()));
|
||||||
|
auto buffer = TRY(ByteBuffer::create_uninitialized(1 * KiB));
|
||||||
|
|
||||||
u32 line_number = 0;
|
u32 line_number = 0;
|
||||||
while (!file->eof()) {
|
while (TRY(file->can_read_line())) {
|
||||||
auto original_line = file->read_line(1024);
|
auto original_line = TRY(file->read_line(buffer));
|
||||||
++line_number;
|
++line_number;
|
||||||
if (original_line.is_empty())
|
if (original_line.is_empty())
|
||||||
break;
|
break;
|
||||||
auto trimmed_line = original_line.view().trim_whitespace();
|
auto trimmed_line = original_line.trim_whitespace();
|
||||||
auto replaced_line = trimmed_line.replace(" "sv, "\t"sv, ReplaceMode::All);
|
auto replaced_line = trimmed_line.replace(" "sv, "\t"sv, ReplaceMode::All);
|
||||||
auto fields = replaced_line.split_view('\t');
|
auto fields = replaced_line.split_view('\t');
|
||||||
|
|
||||||
|
@ -115,13 +132,15 @@ void LookupServer::load_etc_hosts()
|
||||||
auto raw_addr = maybe_address->to_in_addr_t();
|
auto raw_addr = maybe_address->to_in_addr_t();
|
||||||
|
|
||||||
Name name { fields[1] };
|
Name name { fields[1] };
|
||||||
add_answer(name, RecordType::A, DeprecatedString { (char const*)&raw_addr, sizeof(raw_addr) });
|
TRY(add_answer(name, RecordType::A, DeprecatedString { (char const*)&raw_addr, sizeof(raw_addr) }));
|
||||||
|
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(maybe_address->to_deprecated_string_reversed());
|
TRY(builder.try_append(maybe_address->to_deprecated_string_reversed()));
|
||||||
builder.append(".in-addr.arpa"sv);
|
TRY(builder.try_append(".in-addr.arpa"sv));
|
||||||
add_answer(builder.to_deprecated_string(), RecordType::PTR, name.as_string());
|
TRY(add_answer(builder.to_deprecated_string(), RecordType::PTR, name.as_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
static DeprecatedString get_hostname()
|
static DeprecatedString get_hostname()
|
||||||
|
|
|
@ -29,6 +29,7 @@ public:
|
||||||
private:
|
private:
|
||||||
LookupServer();
|
LookupServer();
|
||||||
|
|
||||||
|
ErrorOr<HashMap<Name, Vector<Answer>, Name::Traits>> try_load_etc_hosts();
|
||||||
void load_etc_hosts();
|
void load_etc_hosts();
|
||||||
void put_in_cache(Answer const&);
|
void put_in_cache(Answer const&);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue