From 39a178902e940652b43acb4019528967345df589 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Sat, 8 Mar 2025 10:45:24 +0000 Subject: [PATCH] Improve logging on failed connect attempt --- common/trans.c | 7 +++ common/trans.h | 3 ++ vnc/vnc.c | 115 ++++++++++++++++++++++++++----------------------- xup/xup.c | 3 +- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/common/trans.c b/common/trans.c index ceaf7802..e08ed81b 100644 --- a/common/trans.c +++ b/common/trans.c @@ -22,6 +22,8 @@ #include #endif +#include + #include "os_calls.h" #include "string_calls.h" #include "trans.h" @@ -750,6 +752,7 @@ trans_connect(struct trans *self, const char *server, const char *port, unsigned int start_time = g_get_elapsed_ms(); int error; int ms_before_next_connect; + int connect_errno = 0; /* * Function pointers which we use in the main loop to avoid @@ -792,6 +795,7 @@ trans_connect(struct trans *self, const char *server, const char *port, if (self->sck < 0) { + connect_errno = errno; error = 1; break; } @@ -800,6 +804,7 @@ trans_connect(struct trans *self, const char *server, const char *port, g_file_set_cloexec(self->sck, 1); g_tcp_set_non_blocking(self->sck); error = f_connect(self->sck, server, port); + connect_errno = errno; if (error == 0) { /* Connect was immediately successful */ @@ -851,6 +856,8 @@ trans_connect(struct trans *self, const char *server, const char *port, g_tcp_close(self->sck); self->sck = -1; } + /* Ensure errno is representative of the last connection attempt */ + errno = connect_errno; self->status = TRANS_STATUS_DOWN; } else diff --git a/common/trans.h b/common/trans.h index cf265534..76070b13 100644 --- a/common/trans.h +++ b/common/trans.h @@ -158,6 +158,9 @@ trans_write_copy_s(struct trans *self, struct stream *out_s); * * Multiple connection attempts may be made within the timeout period. * + * If the operation is not successful, errno will have been set by + * the last connection attempt. + * * If the operation is successful, 0 is returned and self->status will * be TRANS_STATUS_UP */ diff --git a/vnc/vnc.c b/vnc/vnc.c index b65a6342..8aba530b 100644 --- a/vnc/vnc.c +++ b/vnc/vnc.c @@ -1732,71 +1732,78 @@ lib_mod_connect(struct vnc *v) error = trans_connect(v->trans, v->ip, con_port, 3000); + if (error != 0) + { + g_snprintf(text, sizeof(text), "Error connecting to VNC server [%s]", + g_get_strerror()); + v->server_msg(v, text, 0); + free_stream(s); + free_stream(pixel_format); + return 1; + } + + v->server_msg(v, "VNC tcp connected", 0); + /* protocol version */ + init_stream(s, 8192); + error = trans_force_read_s(v->trans, s, 12); if (error == 0) { - v->server_msg(v, "VNC tcp connected", 0); - /* protocol version */ - init_stream(s, 8192); - error = trans_force_read_s(v->trans, s, 12); - if (error == 0) - { - s->p = s->data; - out_uint8a(s, "RFB 003.003\n", 12); - s_mark_end(s); - error = trans_force_write_s(v->trans, s); - } + s->p = s->data; + out_uint8a(s, "RFB 003.003\n", 12); + s_mark_end(s); + error = trans_force_write_s(v->trans, s); + } - /* sec type */ - if (error == 0) + /* sec type */ + if (error == 0) + { + init_stream(s, 8192); + error = trans_force_read_s(v->trans, s, 4); + } + + if (error == 0) + { + in_uint32_be(s, i); + g_sprintf(text, "VNC security level is %d (1 = none, 2 = standard)", i); + v->server_msg(v, text, 0); + + if (i == 1) /* none */ + { + check_sec_result = 0; + } + else if (i == 2) /* dec the password and the server random */ { init_stream(s, 8192); - error = trans_force_read_s(v->trans, s, 4); - } + error = trans_force_read_s(v->trans, s, 16); - if (error == 0) - { - in_uint32_be(s, i); - g_sprintf(text, "VNC security level is %d (1 = none, 2 = standard)", i); - v->server_msg(v, text, 0); - - if (i == 1) /* none */ - { - check_sec_result = 0; - } - else if (i == 2) /* dec the password and the server random */ + if (error == 0) { init_stream(s, 8192); - error = trans_force_read_s(v->trans, s, 16); - - if (error == 0) + if (guid_is_set(&v->guid)) { - init_stream(s, 8192); - if (guid_is_set(&v->guid)) - { - char guid_str[GUID_STR_SIZE]; - guid_to_str(&v->guid, guid_str); - rfbHashEncryptBytes(s->data, guid_str); - } - else - { - rfbEncryptBytes(s->data, v->password); - } - s->p += 16; - s_mark_end(s); - error = trans_force_write_s(v->trans, s); - check_sec_result = 1; // not needed + char guid_str[GUID_STR_SIZE]; + guid_to_str(&v->guid, guid_str); + rfbHashEncryptBytes(s->data, guid_str); } + else + { + rfbEncryptBytes(s->data, v->password); + } + s->p += 16; + s_mark_end(s); + error = trans_force_write_s(v->trans, s); + check_sec_result = 1; // not needed } - else if (i == 0) - { - LOG(LOG_LEVEL_ERROR, "VNC Server will disconnect"); - error = 1; - } - else - { - LOG(LOG_LEVEL_ERROR, "VNC unsupported security level %d", i); - error = 1; - } + } + else if (i == 0) + { + LOG(LOG_LEVEL_ERROR, "VNC Server will disconnect"); + error = 1; + } + else + { + LOG(LOG_LEVEL_ERROR, "VNC unsupported security level %d", i); + error = 1; } } diff --git a/xup/xup.c b/xup/xup.c index ddc2c66c..e59657b5 100644 --- a/xup/xup.c +++ b/xup/xup.c @@ -324,7 +324,8 @@ lib_mod_connect(struct mod *mod) } else { - mod->server_msg(mod, "connection problem, giving up", 0); + LOG(LOG_LEVEL_ERROR, "Error connecting to X server [%s]", + g_get_strerror()); } if (error == 0)