mirror of
https://github.com/subhobhai943/SUB-OS.git
synced 2026-10-25 16:52:29 +00:00
Adds the client half of TCP -- tcp_connect/tcp_send/tcp_recv/tcp_close -- and wires SOCK_STREAM sockets to it, so the kernel can originate an outbound stream rather than only answer inbound ones. Inbound segments are now matched against the connection table before being offered to the listeners; previously anything not addressed to a listening port was answered with RST, which would have rejected the SYN-ACK completing our own handshake. Transmission is stop-and-wait: one segment outstanding, retransmitted until acknowledged. Every blocking call is bounded against the timer tick and halts the CPU between checks, so a silent peer costs a timeout rather than a hang. Connections linger in TIME_WAIT so a late duplicate cannot be mistaken for traffic on a new incarnation. Verifying it against a real peer uncovered two pre-existing bugs that had made TCP unusable in both directions. Neither was visible from inside the guest, which is why the servers had looked fine: 1. The TCP and UDP checksums were computed by reading the header a uint16_t at a time, immediately after the caller stored zero into that same header's checksum field. Nothing stops the compiler reordering those word reads around that store, and at -O2 it did: the stale checksum field was folded into its own result. Every TCP segment the kernel had ever sent carried a wrong checksum and was silently dropped by the peer -- no client could complete a handshake with sshd or httpd, and it is the likely reason DHCP and DNS replies never arrived. The checksum is now summed a byte at a time through unsigned char pointers, which may alias any object and so cannot be reordered, and which also drops the alignment assumption that reading a packed header as uint16_t made -- a fault rather than a slowdown on ARM. The helper is shared by IP, TCP and UDP instead of being open-coded four times. 2. net_receive derived the datagram length from the Ethernet frame rather than from the IP header's total_length, so the padding on any frame shorter than 60 bytes was delivered as protocol payload. A bare ACK or FIN appeared to carry six bytes, which pushed the acknowledgement number out of step with the peer and turned every close into a FIN retransmission storm answered with RST. Also adds `tcpconnect` and `httpget` applets, and gives netstat the real per-connection state and direction from the engine's table. Verified on x86_64 against a real TCP stack, checked packet by packet in a capture: `httpget` fetched a 349-byte page from a server on the host -- handshake, 81-byte request, response, orderly four-way close, every sequence number exact, zero bad checksums, no retransmissions. In the other direction curl on the host now fetches a live page from the guest's httpd, which had never worked. Builds clean on x86_64, aarch64 and armv8i. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01Fh5z6V7nGPbHvqVG5p4CXX