1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

LibCore: Add a wrapper for getaddrinfo()

This commit is contained in:
Lucas CHOLLET 2022-12-08 00:47:25 +01:00 committed by Linus Groh
parent 34c13eff11
commit 687ef7740a
2 changed files with 54 additions and 0 deletions

View file

@ -1315,6 +1315,32 @@ ErrorOr<ssize_t> recvfrom(int sockfd, void* buffer, size_t buffer_length, int fl
return received;
}
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints)
{
struct addrinfo* results = nullptr;
int const rc = ::getaddrinfo(nodename, servname, &hints, &results);
if (rc != 0) {
if (rc == EAI_SYSTEM) {
return Error::from_syscall("getaddrinfo"sv, -errno);
}
auto const* error_string = gai_strerror(rc);
return Error::from_string_view({ error_string, strlen(error_string) });
}
Vector<struct addrinfo> addresses;
for (auto* result = results; result != nullptr; result = result->ai_next)
TRY(addresses.try_append(*result));
return AddressInfoVector { move(addresses), results,
[](struct addrinfo* ptr) {
if (ptr)
::freeaddrinfo(ptr);
} };
}
ErrorOr<void> getsockopt(int sockfd, int level, int option, void* value, socklen_t* value_size)
{
if (::getsockopt(sockfd, level, option, value, value_size) < 0)