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
Notes: sideshowbarker 2024-07-17 03:59:29 +09:00
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)

View file

@ -9,10 +9,14 @@
#pragma once
#include <AK/Error.h>
#include <AK/Noncopyable.h>
#include <AK/OwnPtrWithCustomDeleter.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <dirent.h>
#include <fcntl.h>
#include <grp.h>
#include <netdb.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
@ -210,6 +214,30 @@ ErrorOr<void> access(StringView pathname, int mode);
ErrorOr<DeprecatedString> readlink(StringView pathname);
ErrorOr<int> poll(Span<struct pollfd>, int timeout);
class AddressInfoVector {
AK_MAKE_NONCOPYABLE(AddressInfoVector);
public:
AddressInfoVector(AddressInfoVector&&) = default;
~AddressInfoVector() = default;
Span<struct addrinfo const> addresses() const { return m_addresses; }
private:
friend ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
AddressInfoVector(Vector<struct addrinfo>&& addresses, struct addrinfo* ptr, AK::Function<void(struct addrinfo*)> deleter)
: m_addresses(move(addresses))
, m_ptr(ptr, move(deleter))
{
}
Vector<struct addrinfo> m_addresses {};
OwnPtrWithCustomDeleter<struct addrinfo> m_ptr;
};
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
#ifdef AK_OS_SERENITY
ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length);
#endif