mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
LibCore: Add a wrapper for getaddrinfo()
This commit is contained in:
parent
34c13eff11
commit
687ef7740a
Notes:
sideshowbarker
2024-07-17 03:59:29 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/687ef7740a Pull-request: https://github.com/SerenityOS/serenity/pull/16383 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/MacDue Reviewed-by: https://github.com/linusg ✅ Reviewed-by: https://github.com/trflynn89 ✅
2 changed files with 54 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue