summaryrefslogtreecommitdiff
path: root/Kernel/Net
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-07 14:44:29 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-07 14:44:29 +0200
commitededd6aac65ee07449c5099957809533f7f6f584 (patch)
tree6d84d361d90960c645311e5bfb1ac541d8bcde15 /Kernel/Net
parented5d04b0ead11ecc452db9416bdccc4da9ca92ef (diff)
downloadserenity-ededd6aac65ee07449c5099957809533f7f6f584.zip
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.
Diffstat (limited to 'Kernel/Net')
-rw-r--r--Kernel/Net/NetworkTask.cpp7
-rw-r--r--Kernel/Net/TCPSocket.cpp15
-rw-r--r--Kernel/Net/TCPSocket.h2
3 files changed, 10 insertions, 14 deletions
diff --git a/Kernel/Net/NetworkTask.cpp b/Kernel/Net/NetworkTask.cpp
index 66c2d8785b..5450bf30e0 100644
--- a/Kernel/Net/NetworkTask.cpp
+++ b/Kernel/Net/NetworkTask.cpp
@@ -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);
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp
index 22905a61c8..05e6554a9d 100644
--- a/Kernel/Net/TCPSocket.cpp
+++ b/Kernel/Net/TCPSocket.cpp
@@ -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);
diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h
index 25b54ff5e4..f588b77af4 100644
--- a/Kernel/Net/TCPSocket.h
+++ b/Kernel/Net/TCPSocket.h
@@ -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();