Skip to content

Commit

Permalink
Kernel/Net: Introduce proper pointers for payload calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sdomi committed Sep 11, 2024
1 parent 99e9158 commit 5f65574
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Kernel/Net/EthernetFrameHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class [[gnu::packed]] EthernetFrameHeader {
MACAddress m_destination;
MACAddress m_source;
NetworkOrdered<u16> m_ether_type;
u32 m_payload[0];
u8 m_payload[0];
};

static_assert(sizeof(EthernetFrameHeader) == 14);
12 changes: 7 additions & 5 deletions Kernel/Net/ICMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> m_checksum { 0 };
// NOTE: The rest of the header is 4 bytes
u8 m_payload[0];
};

static_assert(AssertSize<ICMPHeader, 4>());
Expand All @@ -46,6 +46,8 @@ struct [[gnu::packed]] ICMPEchoPacket {
ICMPHeader header;
NetworkOrdered<u16> identifier;
NetworkOrdered<u16> 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]; }
};
5 changes: 3 additions & 2 deletions Kernel/Net/IP/IPv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -97,6 +97,7 @@ class [[gnu::packed]] IPv4Packet {
NetworkOrdered<u16> m_checksum;
IPv4Address m_source;
IPv4Address m_destination;
u8 m_payload[0];
};

static_assert(AssertSize<IPv4Packet, 20>());
Expand Down
5 changes: 3 additions & 2 deletions Kernel/Net/UDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> m_source_port;
NetworkOrdered<u16> m_destination_port;
NetworkOrdered<u16> m_length;
NetworkOrdered<u16> m_checksum;
u8 m_payload[0];
};

static_assert(sizeof(UDPPacket) == 8);
Expand Down

0 comments on commit 5f65574

Please sign in to comment.