]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Add a Socket implementation which just holds an error message.
authorSimon Tatham <anakin@pobox.com>
Sun, 17 Nov 2013 14:03:36 +0000 (14:03 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 17 Nov 2013 14:03:36 +0000 (14:03 +0000)
This isn't yet used, but I plan to use it in situations where you have
to report errors by returning a valid Socket on which the client wlil
call sk_socket_error, but in fact you notice the error _before_
instantiating your usual kind of Socket. The resulting Socket is
usable for nothing except reading out the error string and closing it.

[originally from svn r10065]

Recipe
errsock.c [new file with mode: 0644]
network.h

diff --git a/Recipe b/Recipe
index ca79f1fcea48a70a5dcef966dbc6270c025dc41c..5eb07de453be3c5781cb085d74ffe46af9645f45 100644 (file)
--- a/Recipe
+++ b/Recipe
@@ -300,7 +300,7 @@ SSH      = ssh sshcrc sshdes sshmd5 sshrsa sshrand sshsha sshblowf
          + sshdh sshcrcda sshpubk sshzlib sshdss x11fwd portfwd
          + sshaes sshsh256 sshsh512 sshbn wildcard pinger ssharcf
          + sshgssc pgssapi
-WINSSH   = SSH winnoise winpgntc wingss
+WINSSH   = SSH winnoise winpgntc wingss errsock
 UXSSH    = SSH uxnoise uxagentc uxgss
 
 # SFTP implementation (pscp, psftp).
@@ -310,7 +310,7 @@ SFTP     = sftp int64 logging
 # Pageant or PuTTYgen).
 MISC     = timing callback misc version settings tree234 proxy conf
 WINMISC  = MISC winstore winnet winhandl cmdline windefs winmisc winproxy
-         + wintime
+         + wintime errsock
 UXMISC   = MISC uxstore uxsel uxnet cmdline uxmisc uxproxy time
 OSXMISC  = MISC uxstore uxsel osxsel uxnet uxmisc uxproxy time
 
diff --git a/errsock.c b/errsock.c
new file mode 100644 (file)
index 0000000..b71aa54
--- /dev/null
+++ b/errsock.c
@@ -0,0 +1,82 @@
+/*
+ * A dummy Socket implementation which just holds an error message.
+ */
+
+#include <stdio.h>
+#include <assert.h>
+
+#define DEFINE_PLUG_METHOD_MACROS
+#include "tree234.h"
+#include "putty.h"
+#include "network.h"
+
+typedef struct Socket_error_tag *Error_Socket;
+
+struct Socket_error_tag {
+    const struct socket_function_table *fn;
+    /* the above variable absolutely *must* be the first in this structure */
+
+    char *error;
+    Plug plug;
+    void *privptr;
+};
+
+static Plug sk_error_plug(Socket s, Plug p)
+{
+    Error_Socket ps = (Error_Socket) s;
+    Plug ret = ps->plug;
+    if (p)
+       ps->plug = p;
+    return ret;
+}
+
+static void sk_error_close(Socket s)
+{
+    Error_Socket ps = (Error_Socket) s;
+
+    sfree(ps->error);
+    sfree(ps);
+}
+
+static void sk_error_set_private_ptr(Socket s, void *ptr)
+{
+    Error_Socket ps = (Error_Socket) s;
+    ps->privptr = ptr;
+}
+
+static void *sk_error_get_private_ptr(Socket s)
+{
+    Error_Socket ps = (Error_Socket) s;
+    return ps->privptr;
+}
+
+static const char *sk_error_socket_error(Socket s)
+{
+    Error_Socket ps = (Error_Socket) s;
+    return ps->error;
+}
+
+Socket new_error_socket(const char *errmsg, Plug plug)
+{
+    static const struct socket_function_table socket_fn_table = {
+       sk_error_plug,
+       sk_error_close,
+       NULL /* write */,
+       NULL /* write_oob */,
+       NULL /* write_eof */,
+       NULL /* flush */,
+       sk_error_set_private_ptr,
+       sk_error_get_private_ptr,
+       NULL /* set_frozen */,
+       sk_error_socket_error
+    };
+
+    Error_Socket ret;
+
+    ret = snew(struct Socket_error_tag);
+    ret->fn = &socket_fn_table;
+    ret->plug = plug;
+    ret->error = dupstr(errmsg);
+
+    return (Socket) ret;
+}
index f390a5d0eb1013bec752c0836de2156df2c0c713..5192529b0ab714c8756109f9c763a2d9686ceb06 100644 (file)
--- a/network.h
+++ b/network.h
@@ -205,6 +205,12 @@ int net_service_lookup(char *service);
  */
 char *get_hostname(void);
 
+/*
+ * Trivial socket implementation which just stores an error. Found in
+ * errsock.c.
+ */
+Socket new_error_socket(const char *errmsg, Plug plug);
+
 /********** SSL stuff **********/
 
 /*