Add enhanced socket debugging and validation

- Added fcntl() check to validate socket before sending data
- Enhanced error reporting with socket descriptor and errno details
- Added detailed send operation logging for troubleshooting
- Improved socket state validation to prevent bad file descriptor errors
- Added errno reporting for better error diagnosis
- Enhanced debugging output for socket operations
- Added socket validity checks using file descriptor flags
- Improved error messages with more context information
parent 891795a3
......@@ -438,12 +438,24 @@ void handle_tunnel_data(SSL *ssl __attribute__((unused)), const char *request_id
// Check if target socket is valid
if (target_sock < 0) {
if (debug) {
printf("[DEBUG] Target socket not ready yet, ignoring tunnel_data\n");
printf("[DEBUG] Target socket not ready yet (sock=%d), ignoring tunnel_data\n", target_sock);
fflush(stdout);
}
pthread_mutex_unlock(&tunnel_mutex);
return;
}
// Double-check socket validity with fcntl
int socket_flags = fcntl(target_sock, F_GETFL);
if (socket_flags == -1) {
if (debug) {
printf("[DEBUG] Socket %d is invalid (fcntl failed), ignoring tunnel_data\n", target_sock);
fflush(stdout);
}
pthread_mutex_unlock(&tunnel_mutex);
return;
}
pthread_mutex_unlock(&tunnel_mutex);
// Validate hex data length
......@@ -493,12 +505,23 @@ void handle_tunnel_data(SSL *ssl __attribute__((unused)), const char *request_id
pthread_mutex_unlock(&tunnel_mutex);
} else {
// wsssh/wssshc: Send directly to target socket
if (debug) {
printf("[DEBUG] Attempting to send %zu bytes to socket %d\n", data_len, target_sock);
fflush(stdout);
}
ssize_t sent = send(target_sock, data, data_len, 0);
if (sent < 0 && debug) {
printf("[DEBUG] Failed to send %zu bytes to target socket: %s\n", data_len, strerror(errno));
if (sent < 0) {
if (debug) {
printf("[DEBUG] Failed to send %zu bytes to target socket %d: %s (errno=%d)\n",
data_len, target_sock, strerror(errno), errno);
fflush(stdout);
} else if (debug && (size_t)sent != data_len) {
printf("[DEBUG] Partial send: sent %zd of %zu bytes\n", sent, data_len);
}
} else if (debug) {
if ((size_t)sent != data_len) {
printf("[DEBUG] Partial send: sent %zd of %zu bytes to socket %d\n", sent, data_len, target_sock);
} else {
printf("[DEBUG] Successfully sent %zu bytes to socket %d\n", data_len, target_sock);
}
fflush(stdout);
}
}
......
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