Fix Windows networking bugs and connection metadata mismatch

- Use closesocket() instead of close() for sockets on Windows (lib/tcp.c)
- Call WSAStartup in tcp_init so outbound connections work (lib/tcp.c)
- Check WSAGetLastError() instead of errno for recv/send on Windows (lib/tcp.c)
- Use WSAEWOULDBLOCK instead of EINPROGRESS for non-blocking connect (lib/tcp.c)
- Add NULL guard in tcp_write for stale connection handles (lib/tcp.c)
- Fix ConnMetadata/TCP_Conn index mismatch in get_next_message: use
  event.handle.idx directly instead of searching for a free metadata
  slot, which could diverge when stale entries remain from failed
  outbound connections (lib/message.c)
- Initialize num_senders to 0 and skip unused metadata in
  find_conn_by_target (lib/message.c)
- Clear ready flag in http_server_next_request to prevent infinite
  loop (lib/http_server.c)
- Add CRT invalid parameter handler for the HTTP proxy (src/main.c)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-25 16:10:01 +01:00
co-authored by Claude Opus 4.6
parent 6b4f773052
commit 1b53abf633
5 changed files with 104 additions and 31 deletions
+11 -8
View File
@@ -26,6 +26,7 @@ int message_system_init(MessageSystem *msys,
for (int i = 0; i < max_conns; i++) {
msys->conns[i].used = false;
msys->conns[i].gen = 0;
msys->conns[i].num_senders = 0;
}
int ret = tcp_init(&msys->tcp, max_conns);
@@ -81,17 +82,17 @@ void *get_next_message(MessageSystem *msys)
// Skip if already set up by ensure_conn (outbound connection)
if (tcp_get_user_ptr(event.handle) == NULL) {
int i = 0;
while (i < msys->max_conns && msys->conns[i].used)
i++;
if (i == msys->max_conns) {
tcp_close(event.handle);
continue;
}
// Use the TCP connection's slot index so that the
// ConnMetadata index always matches the TCP_Conn index.
// Searching for a "free" metadata slot could assign a
// different index when stale metadata entries remain
// from failed outbound connections.
int i = event.handle.idx;
assert(i >= 0 && i < msys->max_conns);
ConnMetadata *meta = &msys->conns[i];
meta->used = true;
meta->gen = event.handle.gen;
meta->num_senders = 0;
meta->message = NULL;
tcp_set_user_ptr(event.handle, meta);
@@ -168,6 +169,8 @@ static TCP_Handle find_conn_by_target(MessageSystem *msys, int target)
{
for (int i = 0; i < msys->max_conns; i++) {
ConnMetadata *meta = &msys->conns[i];
if (!meta->used)
continue;
for (int j = 0; j < meta->num_senders; j++) {
if (meta->senders[j] == target) {
return (TCP_Handle) { &msys->tcp, meta->gen, i };