Kernel: Move on_close_impl from network socket to udp socket

This commit is contained in:
Bananymous 2024-02-12 04:26:25 +02:00
parent be01ccdb08
commit ba06269b14
7 changed files with 21 additions and 19 deletions

View File

@ -51,6 +51,8 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override; virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override;
virtual size_t header_size() const override { return sizeof(IPv4Header); }
private: private:
IPv4Layer(); IPv4Layer();

View File

@ -28,6 +28,8 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0; virtual BAN::ErrorOr<size_t> sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0;
virtual size_t header_size() const = 0;
protected: protected:
NetworkLayer() = default; NetworkLayer() = default;
}; };

View File

@ -40,8 +40,6 @@ namespace Kernel
protected: protected:
NetworkSocket(NetworkLayer&, ino_t, const TmpInodeInfo&); NetworkSocket(NetworkLayer&, ino_t, const TmpInodeInfo&);
virtual void on_close_impl() override;
virtual BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override; virtual BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override;
protected: protected:

View File

@ -32,6 +32,8 @@ namespace Kernel
protected: protected:
virtual void receive_packet(BAN::ConstByteSpan, const sockaddr_storage& sender) override; virtual void receive_packet(BAN::ConstByteSpan, const sockaddr_storage& sender) override;
virtual void on_close_impl() override;
virtual BAN::ErrorOr<void> bind_impl(const sockaddr* address, socklen_t address_len) override; virtual BAN::ErrorOr<void> bind_impl(const sockaddr* address, socklen_t address_len) override;
virtual BAN::ErrorOr<size_t> sendto_impl(BAN::ConstByteSpan message, const sockaddr* address, socklen_t address_len) override; virtual BAN::ErrorOr<size_t> sendto_impl(BAN::ConstByteSpan message, const sockaddr* address, socklen_t address_len) override;
virtual BAN::ErrorOr<size_t> recvfrom_impl(BAN::ByteSpan buffer, sockaddr* address, socklen_t* address_len) override; virtual BAN::ErrorOr<size_t> recvfrom_impl(BAN::ByteSpan buffer, sockaddr* address, socklen_t* address_len) override;

View File

@ -4,6 +4,7 @@
#include <kernel/Networking/IPv4Layer.h> #include <kernel/Networking/IPv4Layer.h>
#include <kernel/Networking/NetworkManager.h> #include <kernel/Networking/NetworkManager.h>
#include <kernel/Networking/UDPSocket.h> #include <kernel/Networking/UDPSocket.h>
#include <kernel/Random.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -89,19 +90,18 @@ namespace Kernel
LockGuard _(m_lock); LockGuard _(m_lock);
uint16_t port = NetworkSocket::PORT_NONE; uint16_t port = NetworkSocket::PORT_NONE;
for (uint32_t temp = 0xC000; temp < 0xFFFF; temp++) for (uint32_t i = 0; i < 100 && port == NetworkSocket::PORT_NONE; i++)
{ if (uint32_t temp = 0xC000 | (Random::get_u32() & 0x3FFF); !m_bound_sockets.contains(temp))
if (!m_bound_sockets.contains(temp)) port = temp;
{ for (uint32_t temp = 0xC000; temp < 0xFFFF && port == NetworkSocket::PORT_NONE; temp++)
if (!m_bound_sockets.contains(temp))
port = temp; port = temp;
break;
}
}
if (port == NetworkSocket::PORT_NONE) if (port == NetworkSocket::PORT_NONE)
{ {
dwarnln("No ports available"); dwarnln("No ports available");
return BAN::Error::from_errno(EAGAIN); return BAN::Error::from_errno(EAGAIN);
} }
dprintln_if(DEBUG_IPV4, "using port {}", port);
struct sockaddr_in target; struct sockaddr_in target;
target.sin_family = AF_INET; target.sin_family = AF_INET;

View File

@ -15,16 +15,6 @@ namespace Kernel
{ {
} }
void NetworkSocket::on_close_impl()
{
if (is_bound())
{
m_network_layer.unbind_socket(this, m_port);
m_interface = nullptr;
m_port = PORT_NONE;
}
}
void NetworkSocket::bind_interface_and_port(NetworkInterface* interface, uint16_t port) void NetworkSocket::bind_interface_and_port(NetworkInterface* interface, uint16_t port)
{ {
ASSERT(!m_interface); ASSERT(!m_interface);

View File

@ -24,6 +24,14 @@ namespace Kernel
: NetworkSocket(network_layer, ino, inode_info) : NetworkSocket(network_layer, ino, inode_info)
{ } { }
void UDPSocket::on_close_impl()
{
if (is_bound())
m_network_layer.unbind_socket(this, m_port);
m_port = PORT_NONE;
m_interface = nullptr;
}
void UDPSocket::add_protocol_header(BAN::ByteSpan packet, uint16_t dst_port, PseudoHeader) void UDPSocket::add_protocol_header(BAN::ByteSpan packet, uint16_t dst_port, PseudoHeader)
{ {
auto& header = packet.as<UDPHeader>(); auto& header = packet.as<UDPHeader>();