mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 04:04:58 +00:00

This commit un-deprecates DeprecatedString, and repurposes it as a byte string. As the null state has already been removed, there are no other particularly hairy blockers in repurposing this type as a byte string (what it _really_ is). This commit is auto-generated: $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \ Meta Ports Ladybird Tests Kernel) $ perl -pie 's/\bDeprecatedString\b/ByteString/g; s/deprecated_string/byte_string/g' $xs $ clang-format --style=file -i \ $(git diff --name-only | grep \.cpp\|\.h) $ gn format $(git ls-files '*.gn' '*.gni')
67 lines
2 KiB
C++
67 lines
2 KiB
C++
/*
|
|
* Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "ConnectionFromClient.h"
|
|
#include "LookupServer.h"
|
|
#include <AK/IPv4Address.h>
|
|
#include <LibDNS/Packet.h>
|
|
|
|
namespace LookupServer {
|
|
|
|
using namespace DNS;
|
|
|
|
static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
|
|
|
|
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket, int client_id)
|
|
: IPC::ConnectionFromClient<LookupClientEndpoint, LookupServerEndpoint>(*this, move(socket), client_id)
|
|
{
|
|
s_connections.set(client_id, *this);
|
|
}
|
|
|
|
void ConnectionFromClient::die()
|
|
{
|
|
s_connections.remove(client_id());
|
|
}
|
|
|
|
Messages::LookupServer::LookupNameResponse ConnectionFromClient::lookup_name(ByteString const& name)
|
|
{
|
|
auto maybe_answers = LookupServer::the().lookup(name, RecordType::A);
|
|
if (maybe_answers.is_error()) {
|
|
dbgln("LookupServer: Failed to lookup A record: {}", maybe_answers.error());
|
|
return { 1, {} };
|
|
}
|
|
|
|
auto answers = maybe_answers.release_value();
|
|
Vector<ByteString> addresses;
|
|
for (auto& answer : answers) {
|
|
addresses.append(answer.record_data());
|
|
}
|
|
return { 0, move(addresses) };
|
|
}
|
|
|
|
Messages::LookupServer::LookupAddressResponse ConnectionFromClient::lookup_address(ByteString const& address)
|
|
{
|
|
if (address.length() != 4)
|
|
return { 1, ByteString() };
|
|
IPv4Address ip_address { (u8 const*)address.characters() };
|
|
auto name = ByteString::formatted("{}.{}.{}.{}.in-addr.arpa",
|
|
ip_address[3],
|
|
ip_address[2],
|
|
ip_address[1],
|
|
ip_address[0]);
|
|
|
|
auto maybe_answers = LookupServer::the().lookup(name, RecordType::PTR);
|
|
if (maybe_answers.is_error()) {
|
|
dbgln("LookupServer: Failed to lookup PTR record: {}", maybe_answers.error());
|
|
return { 1, ByteString() };
|
|
}
|
|
|
|
auto answers = maybe_answers.release_value();
|
|
if (answers.is_empty())
|
|
return { 1, ByteString() };
|
|
return { 0, answers[0].record_data() };
|
|
}
|
|
}
|