From a6e76ae453e51e0ed86831e9cb1b883f3c443b2a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 22 Nov 2015 11:49:14 +0000 Subject: [PATCH] Factor out the back ends' plug log functions. I'm about to want to make a change to all those functions at once, and since they're almost identical, it seemed easiest to pull them out into a common helper. The new source file be_misc.c is intended to contain helper code common to all network back ends (crypto and non-crypto, in particular), and initially it contains a backend_socket_log() function which is the common part of ssh_log(), telnet_log(), rlogin_log() etc. --- Recipe | 2 +- be_misc.c | 36 ++++++++++++++++++++++++++++++++++++ network.h | 12 ++++++++++++ raw.c | 13 ++----------- rlogin.c | 13 ++----------- ssh.c | 37 +++++++++++-------------------------- telnet.c | 13 ++----------- 7 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 be_misc.c diff --git a/Recipe b/Recipe index ebc9908f..0978e877 100644 --- a/Recipe +++ b/Recipe @@ -233,7 +233,7 @@ SFTP = sftp int64 logging # Miscellaneous objects appearing in all the network utilities (not # Pageant or PuTTYgen). -MISC = timing callback misc version settings tree234 proxy conf +MISC = timing callback misc version settings tree234 proxy conf be_misc WINMISC = MISC winstore winnet winhandl cmdline windefs winmisc winproxy + wintime winhsock errsock UXMISC = MISC uxstore uxsel uxnet uxpeer cmdline uxmisc uxproxy time diff --git a/be_misc.c b/be_misc.c new file mode 100644 index 00000000..2ca4fac1 --- /dev/null +++ b/be_misc.c @@ -0,0 +1,36 @@ +/* + * be_misc.c: helper functions shared between main network backends. + */ + +#include "putty.h" +#include "network.h" + +void backend_socket_log(void *frontend, int type, SockAddr addr, int port, + const char *error_msg, int error_code) +{ + char addrbuf[256], *msg; + + switch (type) { + case 0: + sk_getaddr(addr, addrbuf, lenof(addrbuf)); + if (sk_addr_needs_port(addr)) { + msg = dupprintf("Connecting to %s port %d", addrbuf, port); + } else { + msg = dupprintf("Connecting to %s", addrbuf); + } + break; + case 1: + sk_getaddr(addr, addrbuf, lenof(addrbuf)); + msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); + break; + default: + msg = NULL; /* shouldn't happen, but placate optimiser */ + break; + } + + if (msg) { + logevent(frontend, msg); + sfree(msg); + } +} + diff --git a/network.h b/network.h index fa90e60e..fbab21a4 100644 --- a/network.h +++ b/network.h @@ -212,4 +212,16 @@ char *get_hostname(void); */ Socket new_error_socket(const char *errmsg, Plug plug); +/* ---------------------------------------------------------------------- + * Functions defined outside the network code, which have to be + * declared in this header file rather than the main putty.h because + * they use types defined here. + */ + +/* + * Exports from be_misc.c. + */ +void backend_socket_log(void *frontend, int type, SockAddr addr, int port, + const char *error_msg, int error_code); + #endif diff --git a/raw.c b/raw.c index 60fbbf65..24807713 100644 --- a/raw.c +++ b/raw.c @@ -40,17 +40,8 @@ static void raw_log(Plug plug, int type, SockAddr addr, int port, const char *error_msg, int error_code) { Raw raw = (Raw) plug; - char addrbuf[256], *msg; - - sk_getaddr(addr, addrbuf, lenof(addrbuf)); - - if (type == 0) - msg = dupprintf("Connecting to %s port %d", addrbuf, port); - else - msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); - - logevent(raw->frontend, msg); - sfree(msg); + backend_socket_log(raw->frontend, type, addr, port, + error_msg, error_code); } static void raw_check_close(Raw raw) diff --git a/rlogin.c b/rlogin.c index e5d2e39c..5541319d 100644 --- a/rlogin.c +++ b/rlogin.c @@ -48,17 +48,8 @@ static void rlogin_log(Plug plug, int type, SockAddr addr, int port, const char *error_msg, int error_code) { Rlogin rlogin = (Rlogin) plug; - char addrbuf[256], *msg; - - sk_getaddr(addr, addrbuf, lenof(addrbuf)); - - if (type == 0) - msg = dupprintf("Connecting to %s port %d", addrbuf, port); - else - msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); - - logevent(rlogin->frontend, msg); - sfree(msg); + backend_socket_log(rlogin->frontend, type, addr, port, + error_msg, error_code); } static int rlogin_closing(Plug plug, const char *error_msg, int error_code, diff --git a/ssh.c b/ssh.c index b13802fe..d1ed1421 100644 --- a/ssh.c +++ b/ssh.c @@ -3452,34 +3452,19 @@ static void ssh_socket_log(Plug plug, int type, SockAddr addr, int port, const char *error_msg, int error_code) { Ssh ssh = (Ssh) plug; - char addrbuf[256], *msg; - if (ssh->attempting_connshare) { - /* - * While we're attempting connection sharing, don't loudly log - * everything that happens. Real TCP connections need to be - * logged when we _start_ trying to connect, because it might - * be ages before they respond if something goes wrong; but - * connection sharing is local and quick to respond, and it's - * sufficient to simply wait and see whether it worked - * afterwards. - */ - } else { - sk_getaddr(addr, addrbuf, lenof(addrbuf)); - - if (type == 0) { - if (sk_addr_needs_port(addr)) { - msg = dupprintf("Connecting to %s port %d", addrbuf, port); - } else { - msg = dupprintf("Connecting to %s", addrbuf); - } - } else { - msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); - } + /* + * While we're attempting connection sharing, don't loudly log + * everything that happens. Real TCP connections need to be logged + * when we _start_ trying to connect, because it might be ages + * before they respond if something goes wrong; but connection + * sharing is local and quick to respond, and it's sufficient to + * simply wait and see whether it worked afterwards. + */ - logevent(msg); - sfree(msg); - } + if (!ssh->attempting_connshare) + backend_socket_log(ssh->frontend, type, addr, port, + error_msg, error_code); } void ssh_connshare_log(Ssh ssh, int event, const char *logtext, diff --git a/telnet.c b/telnet.c index e7a8d8ba..09a72d1f 100644 --- a/telnet.c +++ b/telnet.c @@ -652,17 +652,8 @@ static void telnet_log(Plug plug, int type, SockAddr addr, int port, const char *error_msg, int error_code) { Telnet telnet = (Telnet) plug; - char addrbuf[256], *msg; - - sk_getaddr(addr, addrbuf, lenof(addrbuf)); - - if (type == 0) - msg = dupprintf("Connecting to %s port %d", addrbuf, port); - else - msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); - - logevent(telnet->frontend, msg); - sfree(msg); + backend_socket_log(telnet->frontend, type, addr, port, + error_msg, error_code); } static int telnet_closing(Plug plug, const char *error_msg, int error_code, -- 2.45.2