From 1be8b626d2de6f4f586aae191eba9544262a1f8c Mon Sep 17 00:00:00 2001 From: sdomi Date: Tue, 10 Sep 2024 23:49:30 +0200 Subject: [PATCH] Kernel/Net: Introduce proper pointers for payload calls Unifying with other structures, this introduces m_payload for all relevant structures, except the TCP header (which has a different concept of payload than the rest). This allows us to return a pointer directly instead of doing pointer arithmetic to point to the end of the structure. --- Kernel/Net/EthernetFrameHeader.h | 2 +- Kernel/Net/ICMP.h | 12 +++++++----- Kernel/Net/IP/IPv4.h | 5 +++-- Kernel/Net/UDP.h | 5 +++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Kernel/Net/EthernetFrameHeader.h b/Kernel/Net/EthernetFrameHeader.h index 8468d0b588d11f..c1cb75dc34dc9b 100644 --- a/Kernel/Net/EthernetFrameHeader.h +++ b/Kernel/Net/EthernetFrameHeader.h @@ -32,7 +32,7 @@ class [[gnu::packed]] EthernetFrameHeader { MACAddress m_destination; MACAddress m_source; NetworkOrdered m_ether_type; - u32 m_payload[0]; + u8 m_payload[0]; }; static_assert(sizeof(EthernetFrameHeader) == 14); diff --git a/Kernel/Net/ICMP.h b/Kernel/Net/ICMP.h index 99656db7e319d0..b7f3fd0aa2debf 100644 --- a/Kernel/Net/ICMP.h +++ b/Kernel/Net/ICMP.h @@ -30,14 +30,14 @@ class [[gnu::packed]] ICMPHeader { u16 checksum() const { return m_checksum; } void set_checksum(u16 w) { m_checksum = w; } - void const* payload() const { return this + 1; } - void* payload() { return this + 1; } + void const* payload() const { return &m_payload[0]; } + void* payload() { return &m_payload[0]; } private: u8 m_type { 0 }; u8 m_code { 0 }; NetworkOrdered m_checksum { 0 }; - // NOTE: The rest of the header is 4 bytes + u8 m_payload[0]; }; static_assert(AssertSize()); @@ -46,6 +46,8 @@ struct [[gnu::packed]] ICMPEchoPacket { ICMPHeader header; NetworkOrdered identifier; NetworkOrdered sequence_number; - void* payload() { return this + 1; } - void const* payload() const { return this + 1; } + u8 m_payload[0]; + + void* payload() { return &m_payload[0]; } + void const* payload() const { return &m_payload[0]; } }; diff --git a/Kernel/Net/IP/IPv4.h b/Kernel/Net/IP/IPv4.h index 62977e6b788e84..0daee35ccdae4d 100644 --- a/Kernel/Net/IP/IPv4.h +++ b/Kernel/Net/IP/IPv4.h @@ -51,8 +51,8 @@ class [[gnu::packed]] IPv4Packet { IPv4Address const& destination() const { return m_destination; } void set_destination(IPv4Address const& address) { m_destination = address; } - void* payload() { return this + 1; } - void const* payload() const { return this + 1; } + void* payload() { return &m_payload[0]; } + void const* payload() const { return &m_payload[0]; } u16 flags_and_fragment() const { return m_flags_and_fragment; } u16 fragment_offset() const { return ((u16)m_flags_and_fragment & 0x1fff); } @@ -97,6 +97,7 @@ class [[gnu::packed]] IPv4Packet { NetworkOrdered m_checksum; IPv4Address m_source; IPv4Address m_destination; + u8 m_payload[0]; }; static_assert(AssertSize()); diff --git a/Kernel/Net/UDP.h b/Kernel/Net/UDP.h index bcbc6e8cbef506..23f691bccc4e01 100644 --- a/Kernel/Net/UDP.h +++ b/Kernel/Net/UDP.h @@ -27,14 +27,15 @@ class [[gnu::packed]] UDPPacket { u16 checksum() const { return m_checksum; } void set_checksum(u16 checksum) { m_checksum = checksum; } - void const* payload() const { return this + 1; } - void* payload() { return this + 1; } + void const* payload() const { return &m_payload[0]; } + void* payload() { return &m_payload[0]; } private: NetworkOrdered m_source_port; NetworkOrdered m_destination_port; NetworkOrdered m_length; NetworkOrdered m_checksum; + u8 m_payload[0]; }; static_assert(sizeof(UDPPacket) == 8);