Kernel: Make TCPSocket client construction use KResultOr and TRY()

We don't really have anywhere to propagate the error in NetworkTask at
the moment, since it runs in its own kernel thread and has no direct
userspace caller.
This commit is contained in:
Andreas Kling 2021-09-07 14:44:29 +02:00
parent ed5d04b0ea
commit ededd6aac6
Notes: sideshowbarker 2024-07-18 04:31:36 +09:00
3 changed files with 10 additions and 14 deletions

View file

@ -449,11 +449,12 @@ void handle_tcp(IPv4Packet const& ipv4_packet, Time const& packet_timestamp)
dbgln_if(TCP_DEBUG, "handle_tcp: incoming connection");
auto& local_address = ipv4_packet.destination();
auto& peer_address = ipv4_packet.source();
auto client = socket->create_client(local_address, tcp_packet.destination_port(), peer_address, tcp_packet.source_port());
if (!client) {
dmesgln("handle_tcp: couldn't create client socket");
auto client_or_error = socket->try_create_client(local_address, tcp_packet.destination_port(), peer_address, tcp_packet.source_port());
if (client_or_error.is_error()) {
dmesgln("handle_tcp: couldn't create client socket: {}", client_or_error.error());
return;
}
auto client = client_or_error.release_value();
MutexLocker locker(client->mutex());
dbgln_if(TCP_DEBUG, "handle_tcp: created new client socket with tuple {}", client->tuple().to_string());
client->set_sequence_number(1000);

View file

@ -90,21 +90,16 @@ RefPtr<TCPSocket> TCPSocket::from_tuple(const IPv4SocketTuple& tuple)
return {};
});
}
RefPtr<TCPSocket> TCPSocket::create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port)
KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port)
{
auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port);
return sockets_by_tuple().with_exclusive([&](auto& table) -> RefPtr<TCPSocket> {
return sockets_by_tuple().with_exclusive([&](auto& table) -> KResultOr<NonnullRefPtr<TCPSocket>> {
if (table.contains(tuple))
return {};
return EEXIST;
auto receive_buffer = try_create_receive_buffer();
if (receive_buffer.is_error())
return {};
auto client_or_error = TCPSocket::try_create(protocol(), receive_buffer.release_value());
if (client_or_error.is_error())
return {};
auto receive_buffer = TRY(try_create_receive_buffer());
auto client = TRY(TCPSocket::try_create(protocol(), move(receive_buffer)));
auto client = client_or_error.release_value();
client->set_setup_state(SetupState::InProgress);
client->set_local_address(new_local_address);
client->set_local_port(new_local_port);

View file

@ -147,7 +147,7 @@ public:
static MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets();
RefPtr<TCPSocket> create_client(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port);
KResultOr<NonnullRefPtr<TCPSocket>> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port);
void set_originator(TCPSocket& originator) { m_originator = originator; }
bool has_originator() { return !!m_originator; }
void release_to_originator();