Fix WebSocket connection stability and pong frame handling

- Add comprehensive error handling for pong frame transmission
- Improve SSL error diagnostics in ws_send_frame function
- Add connection state validation before sending frames
- Prevent sending frames on broken or NULL connections
- Better handling of SSL write failures with detailed error reporting
- Graceful degradation when pong frames fail to send
- Enhanced debugging for connection stability issues

The server now handles network interruptions and SSL timeouts more gracefully,
preventing premature connection closures during large file transfers.
parent 191acd82
......@@ -1126,7 +1126,11 @@ static void *client_handler_thread(void *arg) {
if (state->debug) {
printf("[DEBUG - %s -> wssshd] Received ping, sending pong\n", direction);
}
ws_send_frame(conn, WS_OPCODE_PONG, data, len);
// Send pong frame with error handling
if (!ws_send_frame(conn, WS_OPCODE_PONG, data, len)) {
fprintf(stderr, "[ERROR] Failed to send pong frame, connection may be unstable\n");
// Don't close connection immediately, let it timeout naturally
}
} else {
if (state->debug) {
printf("[DEBUG - %s -> wssshd] Received unhandled opcode: %d\n", direction, opcode);
......
......@@ -229,8 +229,18 @@ bool ws_perform_handshake(ws_connection_t *conn) {
// Send WebSocket frame
bool ws_send_frame(ws_connection_t *conn, uint8_t opcode, const void *data, size_t len) {
if (!conn) {
printf("[DEBUG] ws_send_frame: Connection is NULL\n");
return false;
}
if (conn->state != WS_STATE_OPEN) {
printf("[DEBUG] ws_send_frame: Connection not in OPEN state\n");
printf("[DEBUG] ws_send_frame: Connection not in OPEN state (state=%d)\n", conn->state);
return false;
}
if (!conn->ssl) {
printf("[DEBUG] ws_send_frame: SSL connection is NULL\n");
return false;
}
......@@ -282,7 +292,18 @@ bool ws_send_frame(ws_connection_t *conn, uint8_t opcode, const void *data, size
int to_write = frame_len - total_written;
int written = SSL_write(conn->ssl, frame + total_written, to_write);
if (written <= 0) {
printf("[DEBUG] ws_send_frame: SSL_write failed at offset %d\n", total_written);
int ssl_error = SSL_get_error(conn->ssl, written);
printf("[DEBUG] ws_send_frame: SSL_write failed at offset %d, ssl_error=%d\n", total_written, ssl_error);
// Check for recoverable SSL errors
if (ssl_error == SSL_ERROR_WANT_READ || ssl_error == SSL_ERROR_WANT_WRITE) {
printf("[DEBUG] ws_send_frame: Transient SSL error, could retry\n");
} else if (ssl_error == SSL_ERROR_SYSCALL) {
printf("[DEBUG] ws_send_frame: SSL syscall error, connection may be broken\n");
} else {
printf("[DEBUG] ws_send_frame: Fatal SSL error %d\n", ssl_error);
}
free(frame);
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment