From 7326d00baa319edee25835becc8774b6777a6ced Mon Sep 17 00:00:00 2001 From: mobounya Date: Mon, 29 Jan 2024 10:20:58 +0100 Subject: [PATCH] RequestServer: Avoid using gethostbyname Replace gethostbyname with Core::Socket::resolve_host in RequestServer::ConnectionFromClient::ensure_connection. Resolved #22199. --- Userland/Libraries/LibCore/Socket.h | 27 ++++++++++--------- .../RequestServer/ConnectionFromClient.cpp | 5 +++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibCore/Socket.h b/Userland/Libraries/LibCore/Socket.h index e57abf7690..b95464c808 100644 --- a/Userland/Libraries/LibCore/Socket.h +++ b/Userland/Libraries/LibCore/Socket.h @@ -20,6 +20,16 @@ namespace Core { /// classes. Sockets are non-seekable streams which can be read byte-wise. class Socket : public Stream { public: + enum class PreventSIGPIPE { + No, + Yes, + }; + + enum class SocketType { + Stream, + Datagram, + }; + Socket(Socket&&) = default; Socket& operator=(Socket&&) = default; @@ -46,12 +56,11 @@ public: /// Conversely, set_notifications_enabled(true) will re-enable notifications. virtual void set_notifications_enabled(bool) { } - Function on_ready_to_read; + // FIXME: This will need to be updated when IPv6 socket arrives. Perhaps a + // base class for all address types is appropriate. + static ErrorOr resolve_host(ByteString const&, SocketType); - enum class PreventSIGPIPE { - No, - Yes, - }; + Function on_ready_to_read; protected: enum class SocketDomain { @@ -59,20 +68,12 @@ protected: Inet, }; - enum class SocketType { - Stream, - Datagram, - }; - Socket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No) : m_prevent_sigpipe(prevent_sigpipe == PreventSIGPIPE::Yes) { } static ErrorOr create_fd(SocketDomain, SocketType); - // FIXME: This will need to be updated when IPv6 socket arrives. Perhaps a - // base class for all address types is appropriate. - static ErrorOr resolve_host(ByteString const&, SocketType); static ErrorOr connect_local(int fd, ByteString const& path); static ErrorOr connect_inet(int fd, SocketAddress const&); diff --git a/Userland/Services/RequestServer/ConnectionFromClient.cpp b/Userland/Services/RequestServer/ConnectionFromClient.cpp index 4bf64e2106..1acb6630d6 100644 --- a/Userland/Services/RequestServer/ConnectionFromClient.cpp +++ b/Userland/Services/RequestServer/ConnectionFromClient.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -159,7 +160,9 @@ void ConnectionFromClient::ensure_connection(URL const& url, ::RequestServer::Ca if (cache_level == CacheLevel::ResolveOnly) { return Core::deferred_invoke([host = url.serialized_host().release_value_but_fixme_should_propagate_errors().to_byte_string()] { dbgln("EnsureConnection: DNS-preload for {}", host); - (void)gethostbyname(host.characters()); + auto resolved_host = Core::Socket::resolve_host(host, Core::Socket::SocketType::Stream); + if (resolved_host.is_error()) + dbgln("EnsureConnection: DNS-preload failed for {}", host); }); }