diff --git a/kernel/include/kernel/Networking/IPv4Layer.h b/kernel/include/kernel/Networking/IPv4Layer.h index e12512955e..7c5991324f 100644 --- a/kernel/include/kernel/Networking/IPv4Layer.h +++ b/kernel/include/kernel/Networking/IPv4Layer.h @@ -51,6 +51,8 @@ namespace Kernel virtual BAN::ErrorOr sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) override; + virtual size_t header_size() const override { return sizeof(IPv4Header); } + private: IPv4Layer(); diff --git a/kernel/include/kernel/Networking/NetworkLayer.h b/kernel/include/kernel/Networking/NetworkLayer.h index 79f4cfb7f3..af9ca05054 100644 --- a/kernel/include/kernel/Networking/NetworkLayer.h +++ b/kernel/include/kernel/Networking/NetworkLayer.h @@ -28,6 +28,8 @@ namespace Kernel virtual BAN::ErrorOr sendto(NetworkSocket&, BAN::ConstByteSpan, const sockaddr*, socklen_t) = 0; + virtual size_t header_size() const = 0; + protected: NetworkLayer() = default; }; diff --git a/kernel/include/kernel/Networking/NetworkSocket.h b/kernel/include/kernel/Networking/NetworkSocket.h index 8b5f2f0765..19424a099d 100644 --- a/kernel/include/kernel/Networking/NetworkSocket.h +++ b/kernel/include/kernel/Networking/NetworkSocket.h @@ -40,8 +40,6 @@ namespace Kernel protected: NetworkSocket(NetworkLayer&, ino_t, const TmpInodeInfo&); - virtual void on_close_impl() override; - virtual BAN::ErrorOr ioctl_impl(int request, void* arg) override; protected: diff --git a/kernel/include/kernel/Networking/UDPSocket.h b/kernel/include/kernel/Networking/UDPSocket.h index 17a8ded1d6..0a72a7a33f 100644 --- a/kernel/include/kernel/Networking/UDPSocket.h +++ b/kernel/include/kernel/Networking/UDPSocket.h @@ -32,6 +32,8 @@ namespace Kernel protected: virtual void receive_packet(BAN::ConstByteSpan, const sockaddr_storage& sender) override; + virtual void on_close_impl() override; + virtual BAN::ErrorOr bind_impl(const sockaddr* address, socklen_t address_len) override; virtual BAN::ErrorOr sendto_impl(BAN::ConstByteSpan message, const sockaddr* address, socklen_t address_len) override; virtual BAN::ErrorOr recvfrom_impl(BAN::ByteSpan buffer, sockaddr* address, socklen_t* address_len) override; diff --git a/kernel/kernel/Networking/IPv4Layer.cpp b/kernel/kernel/Networking/IPv4Layer.cpp index 6a12f7a09c..7ff8e2048a 100644 --- a/kernel/kernel/Networking/IPv4Layer.cpp +++ b/kernel/kernel/Networking/IPv4Layer.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -89,19 +90,18 @@ namespace Kernel LockGuard _(m_lock); uint16_t port = NetworkSocket::PORT_NONE; - for (uint32_t temp = 0xC000; temp < 0xFFFF; temp++) - { - if (!m_bound_sockets.contains(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)) + port = temp; + for (uint32_t temp = 0xC000; temp < 0xFFFF && port == NetworkSocket::PORT_NONE; temp++) + if (!m_bound_sockets.contains(temp)) port = temp; - break; - } - } if (port == NetworkSocket::PORT_NONE) { dwarnln("No ports available"); return BAN::Error::from_errno(EAGAIN); } + dprintln_if(DEBUG_IPV4, "using port {}", port); struct sockaddr_in target; target.sin_family = AF_INET; diff --git a/kernel/kernel/Networking/NetworkSocket.cpp b/kernel/kernel/Networking/NetworkSocket.cpp index 19e76e79e0..c88627c897 100644 --- a/kernel/kernel/Networking/NetworkSocket.cpp +++ b/kernel/kernel/Networking/NetworkSocket.cpp @@ -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) { ASSERT(!m_interface); diff --git a/kernel/kernel/Networking/UDPSocket.cpp b/kernel/kernel/Networking/UDPSocket.cpp index 787618d076..f49e5bd957 100644 --- a/kernel/kernel/Networking/UDPSocket.cpp +++ b/kernel/kernel/Networking/UDPSocket.cpp @@ -24,6 +24,14 @@ namespace Kernel : 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) { auto& header = packet.as();