mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:17:35 +00:00
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:
parent
767529ebf5
commit
e279a1723b
3 changed files with 13 additions and 18 deletions
|
@ -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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue