mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibCore: Add Windows implementation of TCPSocket::connect
This commit is contained in:
parent
c14e6473d6
commit
6be0816172
Notes:
github-actions[bot]
2025-06-24 00:59:13 +00:00
Author: https://github.com/stasoid
Commit: 6be0816172
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5065
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/alimpfard
2 changed files with 119 additions and 0 deletions
|
@ -162,4 +162,121 @@ ErrorOr<int> LocalSocket::release_fd()
|
|||
return fd;
|
||||
}
|
||||
|
||||
ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
|
||||
{
|
||||
int socket_domain;
|
||||
switch (domain) {
|
||||
case SocketDomain::Inet:
|
||||
socket_domain = AF_INET;
|
||||
break;
|
||||
case SocketDomain::Inet6:
|
||||
socket_domain = AF_INET6;
|
||||
break;
|
||||
case SocketDomain::Local:
|
||||
socket_domain = AF_LOCAL;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto fd = TRY(System::socket(socket_domain, socket_type, 0));
|
||||
(void)System::set_close_on_exec(fd, true);
|
||||
return fd;
|
||||
}
|
||||
|
||||
ErrorOr<Vector<Variant<IPv4Address, IPv6Address>>> Socket::resolve_host(ByteString const& host, SocketType type)
|
||||
{
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
struct addrinfo hints = {};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = socket_type;
|
||||
hints.ai_flags = 0;
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
auto const results = TRY(System::getaddrinfo(host.characters(), nullptr, hints));
|
||||
|
||||
Vector<Variant<IPv4Address, IPv6Address>> addresses;
|
||||
|
||||
for (auto const& result : results.addresses()) {
|
||||
if (result.ai_family == AF_INET6) {
|
||||
auto* socket_address = bit_cast<struct sockaddr_in6*>(result.ai_addr);
|
||||
auto address = IPv6Address { socket_address->sin6_addr.s6_addr };
|
||||
addresses.append(address);
|
||||
}
|
||||
|
||||
if (result.ai_family == AF_INET) {
|
||||
auto* socket_address = bit_cast<struct sockaddr_in*>(result.ai_addr);
|
||||
NetworkOrdered<u32> const network_ordered_address { socket_address->sin_addr.s_addr };
|
||||
addresses.append(IPv4Address { network_ordered_address });
|
||||
}
|
||||
}
|
||||
|
||||
if (addresses.is_empty())
|
||||
return Error::from_string_literal("Could not resolve to IPv4 or IPv6 address");
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
ErrorOr<void> Socket::connect_inet(int fd, SocketAddress const& address)
|
||||
{
|
||||
if (address.type() == SocketAddress::Type::IPv6) {
|
||||
auto addr = address.to_sockaddr_in6();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
} else {
|
||||
auto addr = address.to_sockaddr_in();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
}
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(ByteString const& host, u16 port)
|
||||
{
|
||||
auto ip_addresses = TRY(resolve_host(host, SocketType::Stream));
|
||||
|
||||
// It should return an error instead of an empty vector.
|
||||
VERIFY(!ip_addresses.is_empty());
|
||||
|
||||
// FIXME: Support trying to connect to multiple IP addresses (e.g. if one of them doesn't seem to be working, try another one)
|
||||
return ip_addresses.first().visit([port](auto address) { return connect(SocketAddress { address, port }); });
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(SocketAddress const& address)
|
||||
{
|
||||
auto socket = adopt_own(*new TCPSocket);
|
||||
|
||||
auto socket_domain = SocketDomain::Inet6;
|
||||
if (address.type() == SocketAddress::Type::IPv4)
|
||||
socket_domain = SocketDomain::Inet;
|
||||
|
||||
auto fd = TRY(create_fd(socket_domain, SocketType::Stream));
|
||||
socket->m_helper.set_fd(fd);
|
||||
|
||||
TRY(connect_inet(fd, address));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue