Add Norman Brandinger's suggested `-m' option in plink, to read the
[u/mdw/putty] / x11fwd.c
index c537f2d..2b4a6bc 100644 (file)
--- a/x11fwd.c
+++ b/x11fwd.c
@@ -62,12 +62,15 @@ extern void sshfwd_close(void *);
 extern void sshfwd_write(void *, char *, int);
 
 struct X11Private {
+    struct plug_function_table *fn;
+    /* the above variable absolutely *must* be the first in this structure */
     unsigned char firstpkt[12];        /* first X data packet */
     char *auth_protocol;
     unsigned char *auth_data;
     int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;
     int verified;
     void *c;                           /* data used by ssh.c */
+    Socket s;
 };
 
 void x11_close (Socket s);
@@ -103,15 +106,22 @@ static int x11_verify(char *proto, unsigned char *data, int dlen) {
     return 1;
 }
 
-static int x11_receive (Socket s, int urgent, char *data, int len) {
-    struct X11Private *pr = (struct X11Private *)sk_get_private_ptr(s);
+static int x11_closing (Plug plug, char *error_msg, int error_code, int calling_back) {
+    struct X11Private *pr = (struct X11Private *) plug;
+
+    /*
+     * We have no way to communicate down the forwarded connection,
+     * so if an error occurred on the socket, we just ignore it
+     * and treat it like a proper close.
+     */
+    sshfwd_close(pr->c);
+    x11_close(pr->s);
+    return 1;
+}
+
+static int x11_receive (Plug plug, int urgent, char *data, int len) {
+    struct X11Private *pr = (struct X11Private *) plug;
 
-    if (!len) {
-       /* Connection has closed. */
-        sshfwd_close(pr->c);
-       x11_close(s);
-        return 1;
-    }
     sshfwd_write(pr->c, data, len);
     return 1;
 }
@@ -121,13 +131,16 @@ static int x11_receive (Socket s, int urgent, char *data, int len) {
  * 
  * Returns an error message, or NULL on success.
  * also, fills the SocketsStructure
- *
- * Also places the canonical host name into `realhost'.
  */
-char *x11_init (Socket *s, char *display, void *c, char **realhost) {
+char *x11_init (Socket *s, char *display, void *c) {
+    static struct plug_function_table fn_table = {
+       x11_closing,
+       x11_receive
+    };
+
     SockAddr addr;
     int port;
-    char *err;
+    char *err, *dummy_realhost;
     char host[128];
     int n, displaynum;
     struct X11Private *pr;
@@ -148,7 +161,7 @@ char *x11_init (Socket *s, char *display, void *c, char **realhost) {
     /*
      * Try to find host.
      */
-    addr = sk_namelookup(host, realhost);
+    addr = sk_namelookup(host, &dummy_realhost);
     if ( (err = sk_addr_error(addr)) )
        return err;
 
@@ -157,23 +170,29 @@ char *x11_init (Socket *s, char *display, void *c, char **realhost) {
     /*
      * Open socket.
      */
-    *s = sk_new(addr, port, 0, x11_receive);
-    if ( (err = sk_socket_error(*s)) )
-       return err;
-
     pr = (struct X11Private *)smalloc(sizeof(struct X11Private));
+    pr->fn = &fn_table;
     pr->auth_protocol = NULL;
     pr->verified = 0;
     pr->data_read = 0;
     pr->c = c;
 
+    pr->s = *s = sk_new(addr, port, 0, 1, (Plug) pr);
+    if ( (err = sk_socket_error(*s)) ) {
+       sfree (pr);
+       return err;
+    }
+
     sk_set_private_ptr(*s, pr);
     sk_addr_free(addr);
     return NULL;
 }
 
 void x11_close (Socket s) {
-    struct X11Private *pr = (struct X11Private *)sk_get_private_ptr(s);
+    struct X11Private *pr;\r
+       if (!s)\r
+               return;\r
+       pr = (struct X11Private *)sk_get_private_ptr(s);
 
     if (pr->auth_protocol) {
         sfree(pr->auth_protocol);