Skip to content

Robust TCP/IP Hardening#582

Draft
jorgesg82 wants to merge 9 commits intodevelopmentfrom
robust-tcpip
Draft

Robust TCP/IP Hardening#582
jorgesg82 wants to merge 9 commits intodevelopmentfrom
robust-tcpip

Conversation

@jorgesg82
Copy link
Contributor

Robust TCP/IP Hardening

Summary

This PR hardens the ST-LIB TCP/UDP communication layer on top of lwIP, based on issues reproduced on real hardware under load, fragmentation, and reconnect scenarios.

  • Fixes TCP stream handling so small/fragmented segments are not silently dropped.
  • Improves send-path behavior under backpressure to reduce intermittent delivery failures.
  • Makes ServerSocket lifecycle handling safer across disconnects, close/reopen sequences, and PCB reuse.
  • Hardens DatagramSocket and pbuf processing to avoid unstable UDP parsing paths.
  • Aligns lwIP/ICMP checksum behavior with hardware checksum offload, improving ping behavior.

Impact

These changes reduce silent packet corruption, improve connection stability during stress/soak testing, and make the socket layer more reliable for production boards that depend on strict TCP session behavior.

Validation

It has been validated on a Nucleo Development Board with repeated stress, reconnect, burst, UDP round-trip, and long-duration soak tests. Those test scripts will be uploaded to the template-project

Notes

Even though it has been hardly tested on a Nucleo, this PR will remain as a draft until I validate it on a real board, because this is a critical change, and will affect Levión testing

@jorgesg82 jorgesg82 self-assigned this Feb 28, 2026
Copilot AI review requested due to automatic review settings February 28, 2026 12:39
@jorgesg82 jorgesg82 marked this pull request as draft February 28, 2026 12:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the TCP/UDP communication layer built on top of lwIP for STM32-based embedded systems. It addresses real bugs reproduced on hardware including fragmented TCP stream handling, silent packet drops, and unsafe socket lifecycle management. The changes eliminate several crash-prone and UB-inducing patterns (e.g., calling destructor directly, using nullptr PCBs without guards, direct type-punned pointer dereferences for unaligned data), and replace them with robust patterns using move semantics, null guards, stream-based reassembly, and proper resource management.

Changes:

  • Server, ServerSocket, and Socket are hardened with null guards, corrected move semantics, proper PCB lifecycle management, and stream-based TCP reassembly to handle fragmentation
  • DatagramSocket fixes the move constructor self-reference bug (remote_port(remote_port)remote_port(other.remote_port)), adds null guards, and uses pbuf copy for safe packet parsing
  • PacketValue.hpp replaces direct pointer dereferences with memcpy to avoid undefined behavior from type-punned and potentially unaligned reads; lwipopts.h disables ICMP software checksumming to align with hardware offload

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
Src/ST-LIB_LOW/Communication/Server/Server.cpp Server lifecycle hardening: null guards, capacity enforcement, compaction loop replacing FAULT status transitions
Src/HALAL/Services/Communication/Ethernet/LWIP/UDP/DatagramSocket.cpp Fixes move constructor bug, adds null guards, proper close/reconnect, pbuf-safe receive callback
Src/HALAL/Services/Communication/Ethernet/LWIP/TCP/Socket.cpp Full TCP socket hardening: move semantics, stream-based reassembly, corrected callbacks, ERR_ABRT handling
Src/HALAL/Services/Communication/Ethernet/LWIP/TCP/ServerSocket.cpp Server socket hardening: stream reassembly, correct accept_callback with arg fallback, idempotent close
LWIP/Target/lwipopts.h Disables ICMP software checksum gen/check for hardware offload alignment
Inc/ST-LIB_LOW/Communication/Server/Server.hpp Changes broadcast_order from void to bool
Inc/HALAL/Services/Communication/Ethernet/LWIP/UDP/DatagramSocket.hpp In-class nullptr init for udp_control_block
Inc/HALAL/Services/Communication/Ethernet/LWIP/TCP/Socket.hpp Adds rx_stream_buffer, clear_packet_queues, connect_poll_ticks, MAX_TX_QUEUE_DEPTH; tightens send_order
Inc/HALAL/Services/Communication/Ethernet/LWIP/TCP/ServerSocket.hpp Adds rx_stream_buffer, clear_packet_queues, is_listening(); raises MAX_TX_QUEUE_DEPTH to 64; tightens send_order
Inc/HALAL/Models/Packets/PacketValue.hpp Replaces type-punned pointer dereferences with memcpy for unaligned-safe access
Inc/HALAL/Models/Packets/OrderProtocol.hpp Adds virtual destructor to the base interface class

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants