summaryrefslogtreecommitdiff
path: root/Kernel/Net
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-07 12:09:52 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-07 13:53:14 +0200
commitb4811324183cc64bd671f93c889fa5e6590e0182 (patch)
tree083ec95329a20fbcf74f78ce8ea2bf3c9a4055b8 /Kernel/Net
parent7bf88444993a72f588ce3bd849042c3420e222a7 (diff)
downloadserenity-b4811324183cc64bd671f93c889fa5e6590e0182.zip
Kernel: Make UserOrKernelBuffer return KResult from read/write/memset
This allows us to simplify a whole bunch of call sites with TRY(). :^)
Diffstat (limited to 'Kernel/Net')
-rw-r--r--Kernel/Net/IPv4Socket.cpp8
-rw-r--r--Kernel/Net/TCPSocket.cpp12
-rw-r--r--Kernel/Net/UDPSocket.cpp8
3 files changed, 15 insertions, 13 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp
index 621a440e4b..0e88cc1fc1 100644
--- a/Kernel/Net/IPv4Socket.cpp
+++ b/Kernel/Net/IPv4Socket.cpp
@@ -236,9 +236,9 @@ KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer&
return set_so_error(ENOMEM);
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
m_peer_address, (IPv4Protocol)protocol(), data_length, m_ttl);
- if (!data.read(packet->buffer->data() + ipv4_payload_offset, data_length)) {
+ if (auto result = data.read(packet->buffer->data() + ipv4_payload_offset, data_length); result.is_error()) {
routing_decision.adapter->release_packet_buffer(*packet);
- return set_so_error(EFAULT);
+ return set_so_error(result);
}
routing_decision.adapter->send_packet(packet->bytes());
routing_decision.adapter->release_packet_buffer(*packet);
@@ -371,8 +371,8 @@ KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& descripti
if (type() == SOCK_RAW) {
size_t bytes_written = min(packet.data.value().size(), buffer_length);
- if (!buffer.write(packet.data.value().data(), bytes_written))
- return set_so_error(EFAULT);
+ if (auto result = buffer.write(packet.data.value().data(), bytes_written); result.is_error())
+ return set_so_error(result);
return bytes_written;
}
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp
index 740696ef6a..06b01cbda9 100644
--- a/Kernel/Net/TCPSocket.cpp
+++ b/Kernel/Net/TCPSocket.cpp
@@ -169,8 +169,8 @@ KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use
size_t payload_size = raw_ipv4_packet.size() - sizeof(IPv4Packet) - tcp_packet.header_size();
dbgln_if(TCP_SOCKET_DEBUG, "payload_size {}, will it fit in {}?", payload_size, buffer_size);
VERIFY(buffer_size >= payload_size);
- if (!buffer.write(tcp_packet.payload(), payload_size))
- return set_so_error(EFAULT);
+ if (auto result = buffer.write(tcp_packet.payload(), payload_size); result.is_error())
+ return set_so_error(result);
return payload_size;
}
@@ -226,9 +226,11 @@ KResult TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload,
tcp_packet.set_ack_number(m_ack_number);
}
- if (payload && !payload->read(tcp_packet.payload(), payload_size)) {
- routing_decision.adapter->release_packet_buffer(*packet);
- return set_so_error(EFAULT);
+ if (payload) {
+ if (auto result = payload->read(tcp_packet.payload(), payload_size); result.is_error()) {
+ routing_decision.adapter->release_packet_buffer(*packet);
+ return set_so_error(result);
+ }
}
if (flags & TCPFlags::SYN) {
diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp
index ef8878ad0a..477b7df6d8 100644
--- a/Kernel/Net/UDPSocket.cpp
+++ b/Kernel/Net/UDPSocket.cpp
@@ -65,8 +65,8 @@ KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
VERIFY(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
size_t read_size = min(buffer_size, udp_packet.length() - sizeof(UDPPacket));
- if (!buffer.write(udp_packet.payload(), read_size))
- return set_so_error(EFAULT);
+ if (auto result = buffer.write(udp_packet.payload(), read_size); result.is_error())
+ return set_so_error(result);
return read_size;
}
@@ -86,8 +86,8 @@ KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_
udp_packet.set_source_port(local_port());
udp_packet.set_destination_port(peer_port());
udp_packet.set_length(udp_buffer_size);
- if (!data.read(udp_packet.payload(), data_length))
- return set_so_error(EFAULT);
+ if (auto result = data.read(udp_packet.payload(), data_length); result.is_error())
+ return set_so_error(result);
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
peer_address(), IPv4Protocol::UDP, udp_buffer_size, ttl());