]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Factor out the back ends' plug log functions.
authorSimon Tatham <anakin@pobox.com>
Sun, 22 Nov 2015 11:49:14 +0000 (11:49 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 22 Nov 2015 15:11:00 +0000 (15:11 +0000)
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
be_misc.c [new file with mode: 0644]
network.h
raw.c
rlogin.c
ssh.c
telnet.c

diff --git a/Recipe b/Recipe
index ebc9908fbc9c41c90a660e8d31b6ee58b2f0b6fe..0978e877dcd509d92b4b2feb541f5e227d4f87fb 100644 (file)
--- 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 (file)
index 0000000..2ca4fac
--- /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);
+    }
+}
+
index fa90e60e7349ce44437fab0861d24dc0bb67ccd2..fbab21a447ff524651a574c69601ee0cc4c289ef 100644 (file)
--- 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 60fbbf65e2d74b74d80d6e438639a75b7c0d6d64..248077130334a76c4406f76229ae40738c310a90 100644 (file)
--- 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)
index e5d2e39ce0b8a7e378637a2bc3e95c89cc094514..5541319d056fa5de37f8340ae5c04cca66ef7560 100644 (file)
--- 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 b13802feb2abbdbfd26ade67f412a471ef1eb4a4..d1ed1421dfaf290470f84baf7f5c12ef4d5c8a07 100644 (file)
--- 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,
index e7a8d8bad1a16757a4e5a1e9916b24c0cacd93f1..09a72d1fe18a85bdf6ee53cf022cce70c5b9321d 100644 (file)
--- 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,