From 630cac3aa2793e567b6f49a8d192559faa20a96b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 7 Feb 2020 19:18:50 +0000 Subject: [PATCH] Log when a network connection succeeds. Now I've got an enum for PlugLogType, it's easier to add things to it. We were giving a blow-by-blow account of each connection attempt, and when it failed, saying what went wrong before we moved on to the next candidate address, but when one finally succeeded, we never logged _that_. Now we do. --- be_misc.c | 4 ++++ network.h | 4 ++++ nullplug.c | 4 ++-- unix/uxnet.c | 11 +++++++++++ windows/winnet.c | 15 ++++++++++++--- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/be_misc.c b/be_misc.c index ea8fac88..d7545605 100644 --- a/be_misc.c +++ b/be_misc.c @@ -28,6 +28,10 @@ void backend_socket_log(Seat *seat, LogContext *logctx, sk_getaddr(addr, addrbuf, lenof(addrbuf)); msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); break; + case PLUGLOG_CONNECT_SUCCESS: + sk_getaddr(addr, addrbuf, lenof(addrbuf)); + msg = dupprintf("Connected to %s", addrbuf); + break; case PLUGLOG_PROXY_MSG: /* Proxy-related log messages have their own identifying * prefix already, put on by our caller. */ diff --git a/network.h b/network.h index 664304b7..89419fb7 100644 --- a/network.h +++ b/network.h @@ -47,6 +47,7 @@ struct Plug { typedef enum PlugLogType { PLUGLOG_CONNECT_TRYING, PLUGLOG_CONNECT_FAILED, + PLUGLOG_CONNECT_SUCCESS, PLUGLOG_PROXY_MSG, } PlugLogType; @@ -66,6 +67,9 @@ struct PlugVtable { * addresses to fall back to. When it _is_ fatal, the closing() * function will be called. * + * - PLUGLOG_CONNECT_SUCCESS means we have succeeded in + * connecting to address `addr'. + * * - PLUGLOG_PROXY_MSG means that error_msg contains a line of * logging information from whatever the connection is being * proxied through. This will typically be a wodge of diff --git a/nullplug.c b/nullplug.c index 81376d3e..721cedb0 100644 --- a/nullplug.c +++ b/nullplug.c @@ -7,8 +7,8 @@ #include "putty.h" -static void nullplug_socket_log(Plug *plug, int type, SockAddr *addr, int port, - const char *error_msg, int error_code) +static void nullplug_socket_log(Plug *plug, PlugLogType type, SockAddr *addr, + int port, const char *err_msg, int err_code) { } diff --git a/unix/uxnet.c b/unix/uxnet.c index 02d18202..17b2da1c 100644 --- a/unix/uxnet.c +++ b/unix/uxnet.c @@ -733,6 +733,10 @@ static int try_connect(NetSocket *sock) */ sock->connected = true; sock->writable = true; + + SockAddr thisaddr = sk_extractaddr_tmp(sock->addr, &sock->step); + plug_log(sock->plug, PLUGLOG_CONNECT_SUCCESS, + &thisaddr, sock->port, NULL, 0); } uxsel_tell(sock); @@ -1435,6 +1439,13 @@ static void net_select_result(int fd, int event) } if (!s->connected) return; /* another async attempt in progress */ + } else { + /* + * The connection attempt succeeded. + */ + SockAddr thisaddr = sk_extractaddr_tmp(s->addr, &s->step); + plug_log(s->plug, PLUGLOG_CONNECT_SUCCESS, + &thisaddr, s->port, NULL, 0); } } diff --git a/windows/winnet.c b/windows/winnet.c index 71ebc53b..11451f03 100644 --- a/windows/winnet.c +++ b/windows/winnet.c @@ -1068,6 +1068,9 @@ static DWORD try_connect(NetSocket *sock) * and we should set the socket as writable. */ sock->writable = true; + SockAddr thisaddr = sk_extractaddr_tmp(sock->addr, &sock->step); + plug_log(sock->plug, PLUGLOG_CONNECT_SUCCESS, + &thisaddr, sock->port, NULL, 0); } err = 0; @@ -1546,12 +1549,18 @@ void select_result(WPARAM wParam, LPARAM lParam) case FD_CONNECT: s->connected = true; s->writable = true; + /* - * Once a socket is connected, we can stop falling - * back through the candidate addresses to connect - * to. + * Once a socket is connected, we can stop falling back + * through the candidate addresses to connect to. But first, + * let the plug know we were successful. */ if (s->addr) { + SockAddr thisaddr = sk_extractaddr_tmp( + s->addr, &s->step); + plug_log(s->plug, PLUGLOG_CONNECT_SUCCESS, + &thisaddr, s->port, NULL, 0); + sk_addr_free(s->addr); s->addr = NULL; }