Add a configuration option for TCP keepalives (SO_KEEPALIVE), default off.
[u/mdw/putty] / proxy.c
diff --git a/proxy.c b/proxy.c
index 59f2abb..d3bcca6 100644 (file)
--- a/proxy.c
+++ b/proxy.c
@@ -16,7 +16,9 @@
 
 #define do_proxy_dns(cfg) \
     (cfg->proxy_dns == FORCE_ON || \
-        (cfg->proxy_dns == AUTO && cfg->proxy_type != PROXY_SOCKS))
+        (cfg->proxy_dns == AUTO && \
+              cfg->proxy_type != PROXY_SOCKS4 && \
+              cfg->proxy_type != PROXY_SOCKS5))
 
 /*
  * Call this when proxy negotiation is complete, so that this
@@ -88,6 +90,7 @@ static void sk_proxy_close (Socket s)
     Proxy_Socket ps = (Proxy_Socket) s;
 
     sk_close(ps->sub_socket);
+    sk_addr_free(ps->remote_addr);
     sfree(ps);
 }
 
@@ -158,10 +161,14 @@ static void sk_proxy_set_frozen (Socket s, int is_frozen)
         */
         while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
            void *data;
+           char databuf[512];
            int len;
            bufchain_prefix(&ps->pending_input_data, &data, &len);
-           plug_receive(ps->plug, 0, data, len);
+           if (len > lenof(databuf))
+               len = lenof(databuf);
+           memcpy(databuf, data, len);
            bufchain_consume(&ps->pending_input_data, len);
+           plug_receive(ps->plug, 0, databuf, len);
        }
 
        /* if we're still frozen, we'll have to wait for another
@@ -232,7 +239,7 @@ static void plug_proxy_sent (Plug p, int bufsize)
     plug_sent(ps->plug, bufsize);
 }
 
-static int plug_proxy_accepting (Plug p, void *sock)
+static int plug_proxy_accepting (Plug p, OSSocket sock)
 {
     Proxy_Plug pp = (Proxy_Plug) p;
     Proxy_Socket ps = pp->proxy_socket;
@@ -316,9 +323,9 @@ static int proxy_for_destination (SockAddr addr, char *hostname, int port,
             * match (ie. a specific IP)
             */
 
-           if (addr && stricmp(hostip, exclude_list + s) == 0)
+           if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
                return 0; /* IP/hostname excluded. do not use proxy. */
-           if (stricmp(hostname, exclude_list + s) == 0)
+           if (strnicmp(hostname, exclude_list + s, e - s) == 0)
                return 0; /* IP/hostname excluded. do not use proxy. */
        }
 
@@ -347,15 +354,10 @@ SockAddr name_lookup(char *host, int port, char **canonicalname,
     return sk_namelookup(host, canonicalname);
 }
 
-Socket platform_new_connection(SockAddr addr, char *hostname,
-                              int port, int privport,
-                              int oobinline, int nodelay, Plug plug,
-                              const Config *cfg);
-
 Socket new_connection(SockAddr addr, char *hostname,
                      int port, int privport,
-                     int oobinline, int nodelay, Plug plug,
-                     const Config *cfg)
+                     int oobinline, int nodelay, int keepalive,
+                     Plug plug, const Config *cfg)
 {
     static const struct socket_function_table socket_fn_table = {
        sk_proxy_plug,
@@ -385,15 +387,17 @@ Socket new_connection(SockAddr addr, char *hostname,
        char *proxy_canonical_name;
        Socket sret;
 
-       if ( (sret = platform_new_connection(addr, hostname, port, privport,
-                                            oobinline, nodelay, plug, cfg)) )
+       if ((sret = platform_new_connection(addr, hostname, port, privport,
+                                           oobinline, nodelay, keepalive,
+                                           plug, cfg)) !=
+           NULL)
            return sret;
 
        ret = snew(struct Socket_proxy_tag);
        ret->fn = &socket_fn_table;
        ret->cfg = *cfg;               /* STRUCTURE COPY */
        ret->plug = plug;
-       ret->remote_addr = addr;
+       ret->remote_addr = addr;       /* will need to be freed on close */
        ret->remote_port = port;
 
        ret->error = NULL;
@@ -410,11 +414,10 @@ Socket new_connection(SockAddr addr, char *hostname,
        
        if (cfg->proxy_type == PROXY_HTTP) {
            ret->negotiate = proxy_http_negotiate;
-       } else if (cfg->proxy_type == PROXY_SOCKS) {
-           if (cfg->proxy_socks_version == 4)
-               ret->negotiate = proxy_socks4_negotiate;
-           else
-               ret->negotiate = proxy_socks5_negotiate;
+       } else if (cfg->proxy_type == PROXY_SOCKS4) {
+            ret->negotiate = proxy_socks4_negotiate;
+       } else if (cfg->proxy_type == PROXY_SOCKS5) {
+            ret->negotiate = proxy_socks5_negotiate;
        } else if (cfg->proxy_type == PROXY_TELNET) {
            ret->negotiate = proxy_telnet_negotiate;
        } else {
@@ -442,12 +445,10 @@ Socket new_connection(SockAddr addr, char *hostname,
         */
        ret->sub_socket = sk_new(proxy_addr, cfg->proxy_port,
                                 privport, oobinline,
-                                nodelay, (Plug) pplug);
+                                nodelay, keepalive, (Plug) pplug);
        if (sk_socket_error(ret->sub_socket) != NULL)
            return (Socket) ret;
 
-       sk_addr_free(proxy_addr);
-
        /* start the proxy negotiation process... */
        sk_set_frozen(ret->sub_socket, 0);
        ret->negotiate(ret, PROXY_CHANGE_NEW);
@@ -456,7 +457,7 @@ Socket new_connection(SockAddr addr, char *hostname,
     }
 
     /* no proxy, so just return the direct socket */
-    return sk_new(addr, port, privport, oobinline, nodelay, plug);
+    return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);
 }
 
 Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
@@ -590,8 +591,14 @@ int proxy_http_negotiate (Proxy_Socket p, int change)
            /* get the status line */
            len = bufchain_size(&p->pending_input_data);
            assert(len > 0);           /* or we wouldn't be here */
-           data = snewn(len, char);
+           data = snewn(len+1, char);
            bufchain_fetch(&p->pending_input_data, data, len);
+           /*
+            * We must NUL-terminate this data, because Windows
+            * sscanf appears to require a NUL at the end of the
+            * string because it strlens it _first_. Sigh.
+            */
+           data[len] = '\0';
 
            eol = get_line_end(data, len);
            if (eol < 0) {