Improve error handling for broken SSH connections

- Added detailed error reporting when socket becomes invalid
- Mark tunnel as inactive when SSH connection is broken
- Enhanced debugging for socket closure detection
- Added errno reporting for connection errors
- Improved error messages to distinguish between different failure modes
- Added proper tunnel state management when connections fail
- Better handling of SSH client disconnections during handshake
- Added mutex protection for tunnel state updates during errors
parent 0ca61906
...@@ -272,9 +272,19 @@ void *forward_tcp_to_ws(void *arg) { ...@@ -272,9 +272,19 @@ void *forward_tcp_to_ws(void *arg) {
bytes_read = recv(sock, buffer, sizeof(buffer), 0); bytes_read = recv(sock, buffer, sizeof(buffer), 0);
if (bytes_read <= 0) { if (bytes_read <= 0) {
if (debug) { if (debug) {
printf("[DEBUG] TCP connection closed or error\n"); if (bytes_read == 0) {
printf("[DEBUG] TCP connection closed by peer (SSH client disconnected)\n");
} else {
printf("[DEBUG] TCP connection error: %s\n", strerror(errno));
}
fflush(stdout); fflush(stdout);
} }
// Mark tunnel as inactive since SSH connection is broken
pthread_mutex_lock(&tunnel_mutex);
if (active_tunnel) {
active_tunnel->active = 0;
}
pthread_mutex_unlock(&tunnel_mutex);
break; break;
} }
...@@ -449,9 +459,14 @@ void handle_tunnel_data(SSL *ssl __attribute__((unused)), const char *request_id ...@@ -449,9 +459,14 @@ void handle_tunnel_data(SSL *ssl __attribute__((unused)), const char *request_id
int socket_flags = fcntl(target_sock, F_GETFL); int socket_flags = fcntl(target_sock, F_GETFL);
if (socket_flags == -1) { if (socket_flags == -1) {
if (debug) { if (debug) {
printf("[DEBUG] Socket %d is invalid (fcntl failed), ignoring tunnel_data\n", target_sock); printf("[DEBUG] Socket %d is invalid (fcntl failed: %s), SSH connection likely closed\n",
target_sock, strerror(errno));
fflush(stdout); fflush(stdout);
} }
// Mark tunnel as inactive since SSH connection is broken
if (active_tunnel) {
active_tunnel->active = 0;
}
pthread_mutex_unlock(&tunnel_mutex); pthread_mutex_unlock(&tunnel_mutex);
return; return;
} }
......
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