Improve logging on failed connect attempt

This commit is contained in:
matt335672
2025-03-08 10:45:24 +00:00
parent 39ec7089ac
commit 39a178902e
4 changed files with 73 additions and 55 deletions
+7
View File
@@ -22,6 +22,8 @@
#include <config_ac.h> #include <config_ac.h>
#endif #endif
#include <errno.h>
#include "os_calls.h" #include "os_calls.h"
#include "string_calls.h" #include "string_calls.h"
#include "trans.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(); unsigned int start_time = g_get_elapsed_ms();
int error; int error;
int ms_before_next_connect; int ms_before_next_connect;
int connect_errno = 0;
/* /*
* Function pointers which we use in the main loop to avoid * 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) if (self->sck < 0)
{ {
connect_errno = errno;
error = 1; error = 1;
break; break;
} }
@@ -800,6 +804,7 @@ trans_connect(struct trans *self, const char *server, const char *port,
g_file_set_cloexec(self->sck, 1); g_file_set_cloexec(self->sck, 1);
g_tcp_set_non_blocking(self->sck); g_tcp_set_non_blocking(self->sck);
error = f_connect(self->sck, server, port); error = f_connect(self->sck, server, port);
connect_errno = errno;
if (error == 0) if (error == 0)
{ {
/* Connect was immediately successful */ /* Connect was immediately successful */
@@ -851,6 +856,8 @@ trans_connect(struct trans *self, const char *server, const char *port,
g_tcp_close(self->sck); g_tcp_close(self->sck);
self->sck = -1; self->sck = -1;
} }
/* Ensure errno is representative of the last connection attempt */
errno = connect_errno;
self->status = TRANS_STATUS_DOWN; self->status = TRANS_STATUS_DOWN;
} }
else else
+3
View File
@@ -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. * 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 * If the operation is successful, 0 is returned and self->status will
* be TRANS_STATUS_UP * be TRANS_STATUS_UP
*/ */
+61 -54
View File
@@ -1732,71 +1732,78 @@ lib_mod_connect(struct vnc *v)
error = trans_connect(v->trans, v->ip, con_port, 3000); 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) if (error == 0)
{ {
v->server_msg(v, "VNC tcp connected", 0); s->p = s->data;
/* protocol version */ out_uint8a(s, "RFB 003.003\n", 12);
init_stream(s, 8192); s_mark_end(s);
error = trans_force_read_s(v->trans, s, 12); error = trans_force_write_s(v->trans, s);
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);
}
/* sec type */ /* sec type */
if (error == 0) 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); 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) 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); init_stream(s, 8192);
error = trans_force_read_s(v->trans, s, 16); if (guid_is_set(&v->guid))
if (error == 0)
{ {
init_stream(s, 8192); char guid_str[GUID_STR_SIZE];
if (guid_is_set(&v->guid)) guid_to_str(&v->guid, guid_str);
{ rfbHashEncryptBytes(s->data, guid_str);
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
{
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) }
{ else if (i == 0)
LOG(LOG_LEVEL_ERROR, "VNC Server will disconnect"); {
error = 1; LOG(LOG_LEVEL_ERROR, "VNC Server will disconnect");
} error = 1;
else }
{ else
LOG(LOG_LEVEL_ERROR, "VNC unsupported security level %d", i); {
error = 1; LOG(LOG_LEVEL_ERROR, "VNC unsupported security level %d", i);
} error = 1;
} }
} }
+2 -1
View File
@@ -324,7 +324,8 @@ lib_mod_connect(struct mod *mod)
} }
else 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) if (error == 0)