Add a configuration option for TCP keepalives (SO_KEEPALIVE), default off.
[u/mdw/putty] / portfwd.c
index 8ef1761..b95e5ac 100644 (file)
--- a/portfwd.c
+++ b/portfwd.c
@@ -86,7 +86,7 @@ struct PFwdPrivate {
     int buflen;
 };
 
-static int pfd_closing(Plug plug, char *error_msg, int error_code,
+static int pfd_closing(Plug plug, const char *error_msg, int error_code,
                       int calling_back)
 {
     struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
@@ -107,7 +107,12 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
     struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
     if (pr->dynamic) {
        while (len--) {
+           /*
+            * Throughout SOCKS negotiation, "hostname" is re-used as a
+            * random protocol buffer with "port" storing the length.
+            */ 
            if (pr->port >= lenof(pr->hostname)) {
+               /* Request too long. */
                if ((pr->dynamic >> 12) == 4) {
                    /* Send back a SOCKS 4 error before closing. */
                    char data[8];
@@ -133,6 +138,7 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                    pr->dynamic = 0x4000;
                if (pr->port < 2) continue;/* don't have command code yet */
                if (pr->hostname[1] != 1) {
+                   /* Not CONNECT. */
                    /* Send back a SOCKS 4 error before closing. */
                    char data[8];
                    memset(data, 0, sizeof(data));
@@ -141,9 +147,9 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                    pfd_close(pr->s);
                    return 1;
                }
-               if (pr->port < 8) continue;   /* haven't started username */
+               if (pr->port <= 8) continue; /* haven't started user/hostname */
                if (pr->hostname[pr->port-1] != 0)
-                   continue;          /* haven't _finished_ username */
+                   continue;          /* haven't _finished_ user/hostname */
                /*
                 * Now we have a full SOCKS 4 request. Check it to
                 * see if it's a SOCKS 4A request.
@@ -165,7 +171,7 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                    pr->hostname[0] = 0;   /* reply version code */
                    pr->hostname[1] = 90;   /* request granted */
                    sk_write(pr->s, pr->hostname, 8);
-                   len= pr->port;
+                   len= pr->port - 8;
                    pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
                    memmove(pr->hostname, pr->hostname + 8, len);
                    goto connect;
@@ -220,7 +226,23 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                }
 
                if (pr->dynamic == 0x5001) {
-                   int atype, alen;
+                   /*
+                    * We're receiving a SOCKS request.
+                    */
+                   unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
+                   int atype, alen = 0;
+
+                   /*
+                    * Pre-fill reply packet.
+                    * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
+                    * (atyp=1) in the reply; if we succeed, we don't know
+                    * the right answers, and if we fail, they should be
+                    * ignored.
+                    */
+                   memset(reply, 0, lenof(reply));
+                   reply[0] = 5; /* VER */
+                   reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
+
                    if (pr->port < 6) continue;
                    atype = (unsigned char)pr->hostname[3];
                    if (atype == 1)    /* IPv4 address */
@@ -231,9 +253,9 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                        alen = 1 + (unsigned char)pr->hostname[4];
                    if (pr->port < 6 + alen) continue;
                    if (pr->hostname[1] != 1 || pr->hostname[2] != 0) {
-                       pr->hostname[1] = 1;   /* generic failure */
-                       pr->hostname[2] = 0;   /* reserved */
-                       sk_write(pr->s, pr->hostname, pr->port);
+                       /* Not CONNECT or reserved field nonzero - error */
+                       reply[1] = 1;   /* generic failure */
+                       sk_write(pr->s, (char *) reply, lenof(reply));
                        pfd_close(pr->s);
                        return 1;
                    }
@@ -243,8 +265,8 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                     */
                    pr->port = GET_16BIT_MSB_FIRST(pr->hostname+4+alen);
                    if (atype == 1) {
-                       pr->hostname[1] = 0;   /* succeeded */
-                       sk_write(pr->s, pr->hostname, alen + 6);
+                       /* REP=0 (success) already */
+                       sk_write(pr->s, (char *) reply, lenof(reply));
                        sprintf(pr->hostname, "%d.%d.%d.%d",
                                (unsigned char)pr->hostname[4],
                                (unsigned char)pr->hostname[5],
@@ -252,8 +274,8 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                                (unsigned char)pr->hostname[7]);
                        goto connect;
                    } else if (atype == 3) {
-                       pr->hostname[1] = 0;   /* succeeded */
-                       sk_write(pr->s, pr->hostname, alen + 6);
+                       /* REP=0 (success) already */
+                       sk_write(pr->s, (char *) reply, lenof(reply));
                        memmove(pr->hostname, pr->hostname + 5, alen-1);
                        pr->hostname[alen-1] = '\0';
                        goto connect;
@@ -261,14 +283,14 @@ static int pfd_receive(Plug plug, int urgent, char *data, int len)
                        /*
                         * Unknown address type. (FIXME: support IPv6!)
                         */
-                       pr->hostname[1] = 8;   /* atype not supported */
-                       sk_write(pr->s, pr->hostname, pr->port);
+                       reply[1] = 8;   /* atype not supported */
+                       sk_write(pr->s, (char *) reply, lenof(reply));
                        pfd_close(pr->s);
-                       return 1;                       
+                       return 1;
                    }
                }
            }
-           
+
            /*
             * If we get here without either having done `continue'
             * or `goto connect', it must be because there is no
@@ -331,8 +353,8 @@ static void pfd_sent(Plug plug, int bufsize)
 /*
  * Called when receiving a PORT OPEN from the server
  */
-char *pfd_newconnect(Socket *s, char *hostname, int port, void *c,
-                    const Config *cfg)
+const char *pfd_newconnect(Socket *s, char *hostname, int port,
+                          void *c, const Config *cfg)
 {
     static const struct plug_function_table fn_table = {
        pfd_closing,
@@ -342,15 +364,18 @@ char *pfd_newconnect(Socket *s, char *hostname, int port, void *c,
     };
 
     SockAddr addr;
-    char *err, *dummy_realhost;
+    const char *err;
+    char *dummy_realhost;
     struct PFwdPrivate *pr;
 
     /*
      * Try to find host.
      */
     addr = name_lookup(hostname, port, &dummy_realhost, cfg);
-    if ((err = sk_addr_error(addr)) != NULL)
+    if ((err = sk_addr_error(addr)) != NULL) {
+       sk_addr_free(addr);
        return err;
+    }
 
     /*
      * Open socket.
@@ -362,16 +387,16 @@ char *pfd_newconnect(Socket *s, char *hostname, int port, void *c,
     pr->ready = 1;
     pr->c = c;
     pr->backhandle = NULL;            /* we shouldn't need this */
+    pr->dynamic = 0;
 
     pr->s = *s = new_connection(addr, dummy_realhost, port,
-                               0, 1, 0, (Plug) pr, cfg);
+                               0, 1, 0, 0, (Plug) pr, cfg);
     if ((err = sk_socket_error(*s)) != NULL) {
        sfree(pr);
        return err;
     }
 
     sk_set_private_ptr(*s, pr);
-    sk_addr_free(addr);
     return NULL;
 }
 
@@ -379,7 +404,7 @@ char *pfd_newconnect(Socket *s, char *hostname, int port, void *c,
  called when someone connects to the local port
  */
 
-static int pfd_accepting(Plug p, void *sock)
+static int pfd_accepting(Plug p, OSSocket sock)
 {
     static const struct plug_function_table fn_table = {
        pfd_closing,
@@ -389,7 +414,7 @@ static int pfd_accepting(Plug p, void *sock)
     };
     struct PFwdPrivate *pr, *org;
     Socket s;
-    char *err;
+    const char *err;
 
     org = (struct PFwdPrivate *)p;
     pr = snew(struct PFwdPrivate);
@@ -412,9 +437,10 @@ static int pfd_accepting(Plug p, void *sock)
 
     if (org->dynamic) {
        pr->dynamic = 1;
-       pr->port = 0;                  /* hostname buffer is so far empty */
+       pr->port = 0;                  /* "hostname" buffer is so far empty */
        sk_set_frozen(s, 0);           /* we want to receive SOCKS _now_! */
     } else {
+       pr->dynamic = 0;
        strcpy(pr->hostname, org->hostname);
        pr->port = org->port;   
        pr->c = new_sock_channel(org->backhandle, s);
@@ -435,8 +461,8 @@ static int pfd_accepting(Plug p, void *sock)
 /* Add a new forwarding from port -> desthost:destport
  sets up a listener on the local machine on (srcaddr:)port
  */
-char *pfd_addforward(char *desthost, int destport, char *srcaddr, int port,
-                    void *backhandle, const Config *cfg)
+const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
+                          int port, void *backhandle, const Config *cfg)
 {
     static const struct plug_function_table fn_table = {
        pfd_closing,
@@ -445,7 +471,7 @@ char *pfd_addforward(char *desthost, int destport, char *srcaddr, int port,
        pfd_accepting
     };
 
-    char *err;
+    const char *err;
     struct PFwdPrivate *pr;
     Socket s;