From 0cca6cef958f7742e4c232ef79311f6e5b039c1f Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 11 Sep 2021 20:11:30 +0000 Subject: [PATCH] LibCore+LookupServer: Implement and use UDPServer::send --- Userland/Libraries/LibCore/UDPServer.cpp | 14 ++++++++++++++ Userland/Libraries/LibCore/UDPServer.h | 2 ++ Userland/Services/LookupServer/DNSServer.cpp | 4 +++- Userland/Services/LookupServer/MulticastDNS.cpp | 9 +++++---- Userland/Services/LookupServer/MulticastDNS.h | 2 +- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibCore/UDPServer.cpp b/Userland/Libraries/LibCore/UDPServer.cpp index fbe01fc7ce..9d754d2855 100644 --- a/Userland/Libraries/LibCore/UDPServer.cpp +++ b/Userland/Libraries/LibCore/UDPServer.cpp @@ -102,4 +102,18 @@ Optional UDPServer::local_port() const return ntohs(address.sin_port); } +ErrorOr UDPServer::send(ReadonlyBytes buffer, sockaddr_in const& to) +{ + if (m_fd < 0) { + return Error::from_errno(EBADF); + } + + auto result = ::sendto(m_fd, buffer.data(), buffer.size(), 0, (sockaddr const*)&to, sizeof(to)); + if (result < 0) { + return Error::from_errno(errno); + } + + return result; +} + } diff --git a/Userland/Libraries/LibCore/UDPServer.h b/Userland/Libraries/LibCore/UDPServer.h index 705f204fac..453cf43056 100644 --- a/Userland/Libraries/LibCore/UDPServer.h +++ b/Userland/Libraries/LibCore/UDPServer.h @@ -30,6 +30,8 @@ public: return receive(size, saddr); }; + ErrorOr send(ReadonlyBytes, sockaddr_in const& to); + Optional local_address() const; Optional local_port() const; diff --git a/Userland/Services/LookupServer/DNSServer.cpp b/Userland/Services/LookupServer/DNSServer.cpp index 08f199da69..5c664f36a8 100644 --- a/Userland/Services/LookupServer/DNSServer.cpp +++ b/Userland/Services/LookupServer/DNSServer.cpp @@ -58,7 +58,9 @@ void DNSServer::handle_client() response.set_code(DNSPacket::Code::NOERROR); buffer = response.to_byte_buffer(); - sendto(fd(), buffer.data(), buffer.size(), 0, (const sockaddr*)&client_address, sizeof(client_address)); + + // FIXME: We should be handling errors here. + [[maybe_unused]] auto result = send(buffer, client_address); } } diff --git a/Userland/Services/LookupServer/MulticastDNS.cpp b/Userland/Services/LookupServer/MulticastDNS.cpp index c2d3ecb98d..6f01c7acaf 100644 --- a/Userland/Services/LookupServer/MulticastDNS.cpp +++ b/Userland/Services/LookupServer/MulticastDNS.cpp @@ -100,16 +100,17 @@ void MulticastDNS::announce() response.add_answer(answer); } - if (emit_packet(response) < 0) + if (emit_packet(response).is_error()) perror("Failed to emit response packet"); } -ssize_t MulticastDNS::emit_packet(const DNSPacket& packet, const sockaddr_in* destination) +ErrorOr MulticastDNS::emit_packet(const DNSPacket& packet, const sockaddr_in* destination) { auto buffer = packet.to_byte_buffer(); if (!destination) destination = &mdns_addr; - return sendto(fd(), buffer.data(), buffer.size(), 0, (const sockaddr*)destination, sizeof(*destination)); + + return send(buffer, *destination); } Vector MulticastDNS::local_addresses() const @@ -148,7 +149,7 @@ Vector MulticastDNS::lookup(const DNSName& name, DNSRecordType record request.set_recursion_desired(false); request.add_question({ name, record_type, DNSRecordClass::IN, false }); - if (emit_packet(request) < 0) { + if (emit_packet(request).is_error()) { perror("failed to emit request packet"); return {}; } diff --git a/Userland/Services/LookupServer/MulticastDNS.h b/Userland/Services/LookupServer/MulticastDNS.h index 5fa6343c3d..37b98db9bd 100644 --- a/Userland/Services/LookupServer/MulticastDNS.h +++ b/Userland/Services/LookupServer/MulticastDNS.h @@ -24,7 +24,7 @@ private: explicit MulticastDNS(Object* parent = nullptr); void announce(); - ssize_t emit_packet(const DNSPacket&, const sockaddr_in* destination = nullptr); + ErrorOr emit_packet(const DNSPacket&, const sockaddr_in* destination = nullptr); void handle_packet(); void handle_query(const DNSPacket&);