1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 08:47:35 +00:00

Kernel/Net: Add a special SOCKET_TRY() and use it in socket code

Sockets remember their last error code in the SO_ERROR field, so we need
to take special care to remember this when returning an error.

This patch adds a SOCKET_TRY() that works like TRY() but also calls
set_so_error() on the failure path.

There's probably a lot more code that should be using this, but that's
outside the scope of this patch.
This commit is contained in:
Andreas Kling 2021-09-07 15:05:51 +02:00
parent 3c44e381d4
commit 308773ffda
5 changed files with 29 additions and 46 deletions

View file

@ -65,8 +65,7 @@ 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 (auto result = buffer.write(udp_packet.payload(), read_size); result.is_error())
return set_so_error(result);
SOCKET_TRY(buffer.write(udp_packet.payload(), read_size));
return read_size;
}
@ -86,9 +85,7 @@ 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 (auto result = data.read(udp_packet.payload(), data_length); result.is_error())
return set_so_error(result);
SOCKET_TRY(data.read(udp_packet.payload(), data_length));
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
peer_address(), IPv4Protocol::UDP, udp_buffer_size, ttl());
routing_decision.adapter->send_packet(packet->bytes());