From 048d0a98701eae33daf9892be45b83498b3b7f69 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 8 Dec 2022 10:18:21 +0100 Subject: [PATCH] LibCore: Use `System::getaddrinfo()` in `Socket::resolve_host` --- Userland/Libraries/LibCore/Stream.cpp | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 9b00aaed8f..507df05df8 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -367,24 +367,12 @@ ErrorOr Socket::resolve_host(DeprecatedString const& host, SocketTy hints.ai_flags = 0; hints.ai_protocol = 0; - // FIXME: Convert this to Core::System - struct addrinfo* results = nullptr; - int rc = getaddrinfo(host.characters(), nullptr, &hints, &results); - if (rc != 0) { - if (rc == EAI_SYSTEM) { - return Error::from_syscall("getaddrinfo"sv, -errno); - } + auto const results = TRY(Core::System::getaddrinfo(host.characters(), nullptr, hints)); - auto const* error_string = gai_strerror(rc); - return Error::from_string_view({ error_string, strlen(error_string) }); - } - - ScopeGuard free_results = [results] { freeaddrinfo(results); }; - - for (auto* result = results; result != nullptr; result = result->ai_next) { - if (result->ai_family == AF_INET) { - auto* socket_address = bit_cast(result->ai_addr); - NetworkOrdered network_ordered_address { socket_address->sin_addr.s_addr }; + for (auto const& result : results.addresses()) { + if (result.ai_family == AF_INET) { + auto* socket_address = bit_cast(result.ai_addr); + NetworkOrdered const network_ordered_address { socket_address->sin_addr.s_addr }; return IPv4Address { network_ordered_address }; } }