LookupServer: Propagate the errors from MulticastDNS::lookup()

This patch slightly change the signature of lookup() method
and propagates all the errors to the caller with help of ErrorOr.
This commit is contained in:
Alexander Narsudinov 2022-12-18 00:50:14 +03:00 committed by Andreas Kling
parent 767529ebf5
commit e279a1723b
Notes: sideshowbarker 2024-07-17 02:57:13 +09:00
3 changed files with 13 additions and 18 deletions

View File

@ -183,7 +183,7 @@ ErrorOr<Vector<Answer>> LookupServer::lookup(Name const& name, RecordType record
// Fourth, look up .local names using mDNS instead of DNS nameservers.
if (name.as_string().ends_with(".local"sv)) {
answers = m_mdns->lookup(name, record_type);
answers = TRY(m_mdns->lookup(name, record_type));
for (auto& answer : answers)
put_in_cache(answer);
return answers;

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,6 +12,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <limits.h>
#include <poll.h>
#include <sys/socket.h>
@ -142,36 +144,28 @@ Vector<IPv4Address> MulticastDNS::local_addresses() const
return addresses;
}
Vector<Answer> MulticastDNS::lookup(Name const& name, RecordType record_type)
ErrorOr<Vector<Answer>> MulticastDNS::lookup(Name const& name, RecordType record_type)
{
Packet request;
request.set_is_query();
request.set_recursion_desired(false);
request.add_question({ name, record_type, RecordClass::IN, false });
if (emit_packet(request).is_error()) {
perror("failed to emit request packet");
return {};
}
TRY(emit_packet(request));
Vector<Answer> answers;
// FIXME: It would be better not to block
// the main loop while we wait for a response.
while (true) {
pollfd pfd { fd(), POLLIN, 0 };
auto rc = poll(&pfd, 1, 1000);
if (rc < 0) {
perror("poll");
} else if (rc == 0) {
auto pfd = pollfd { fd(), POLLIN, 0 };
auto rc = TRY(Core::System::poll({ &pfd, 1 }, 1000));
if (rc == 0) {
// Timed out.
return {};
return Vector<Answer> {};
}
// TODO: propagate the error somehow
auto buffer = MUST(receive(1024));
auto buffer = TRY(receive(1024));
if (buffer.is_empty())
return {};
return Vector<Answer> {};
auto optional_packet = Packet::from_raw_packet(buffer.data(), buffer.size());
if (!optional_packet.has_value()) {
dbgln("Got an invalid mDNS packet");

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -20,7 +21,7 @@ using namespace DNS;
class MulticastDNS : public Core::UDPServer {
C_OBJECT(MulticastDNS)
public:
Vector<Answer> lookup(Name const&, RecordType record_type);
ErrorOr<Vector<Answer>> lookup(Name const&, RecordType record_type);
private:
explicit MulticastDNS(Object* parent = nullptr);