Add Ian to the team
[privoxy.git] / gateway.c
index 1860136..2e43cb8 100644 (file)
--- a/gateway.c
+++ b/gateway.c
@@ -1,4 +1,4 @@
-const char gateway_rcs[] = "$Id: gateway.c,v 1.71 2011/02/19 13:56:22 fabiankeil Exp $";
+const char gateway_rcs[] = "$Id: gateway.c,v 1.83 2011/12/24 15:28:45 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/gateway.c,v $
@@ -68,7 +68,7 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.71 2011/02/19 13:56:22 fabiankeil
 #include "miscutil.h"
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
 #ifdef HAVE_POLL
-#ifdef __GLIBC__ 
+#ifdef __GLIBC__
 #include <sys/poll.h>
 #else
 #include <poll.h>
@@ -338,16 +338,12 @@ void forget_connection(jb_socket sfd)
             sfd, reusable_connection[slot].host,
             reusable_connection[slot].port, slot);
          mark_connection_closed(&reusable_connection[slot]);
-         privoxy_mutex_unlock(&connection_reuse_mutex);
-
-         return;
+         break;
       }
    }
 
-   log_error(LOG_LEVEL_CONNECT,
-      "Socket %d already forgotten or never remembered.", sfd);
-
    privoxy_mutex_unlock(&connection_reuse_mutex);
+
 }
 #endif /* def FEATURE_CONNECTION_SHARING */
 
@@ -659,7 +655,7 @@ jb_socket forwarded_connect(const struct forward_spec * fwd,
       default:
          /* Should never get here */
          log_error(LOG_LEVEL_FATAL,
-            "SOCKS4 impossible internal error - bad SOCKS type.");
+            "Internal error in forwarded_connect(). Bad proxy type: %d", fwd->type);
    }
 
    if (JB_INVALID_SOCKET != sfd)
@@ -728,7 +724,7 @@ static jb_socket socks4_connect(const struct forward_spec * fwd,
    if (err)
    {
       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
-      csp->error_message = strdup(errstr); 
+      csp->error_message = strdup(errstr);
       errno = EINVAL;
       return(JB_INVALID_SOCKET);
    }
@@ -812,6 +808,13 @@ static jb_socket socks4_connect(const struct forward_spec * fwd,
       /* The error an its reason have already been logged by connect_to()  */
       return(JB_INVALID_SOCKET);
    }
+   else if (write_socket(sfd, (char *)c, csiz))
+   {
+      errstr = "SOCKS4 negotiation write failed.";
+      log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
+      err = 1;
+      close_socket(sfd);
+   }
    else if (!data_is_available(sfd, csp->config->socket_timeout))
    {
       if (socket_is_still_alive(sfd))
@@ -826,13 +829,6 @@ static jb_socket socks4_connect(const struct forward_spec * fwd,
       err = 1;
       close_socket(sfd);
    }
-   else if (write_socket(sfd, (char *)c, csiz))
-   {
-      errstr = "SOCKS4 negotiation write failed.";
-      log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
-      err = 1;
-      close_socket(sfd);
-   }
    else if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
    {
       errstr = "SOCKS4 negotiation read failed.";
@@ -843,7 +839,7 @@ static jb_socket socks4_connect(const struct forward_spec * fwd,
 
    if (err)
    {
-      csp->error_message = strdup(errstr);      
+      csp->error_message = strdup(errstr);
       return(JB_INVALID_SOCKET);
    }
 
@@ -947,7 +943,7 @@ static jb_socket socks5_connect(const struct forward_spec *fwd,
 {
    int err = 0;
    char cbuf[300];
-   char sbuf[30];
+   char sbuf[10];
    size_t client_pos = 0;
    int server_size = 0;
    size_t hostlen = 0;
@@ -1003,6 +999,8 @@ static jb_socket socks5_connect(const struct forward_spec *fwd,
    {
       errstr = "socks5 server unreachable";
       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
+      /* Free the generic error message provided by connect_to() */
+      freez(csp->error_message);
       csp->error_message = strdup(errstr);
       return(JB_INVALID_SOCKET);
    }
@@ -1083,7 +1081,7 @@ static jb_socket socks5_connect(const struct forward_spec *fwd,
 
    if (write_socket(sfd, cbuf, client_pos))
    {
-      errstr = "SOCKS5 negotiation read failed";
+      errstr = "SOCKS5 negotiation write failed";
       csp->error_message = strdup(errstr);
       log_error(LOG_LEVEL_CONNECT, "%s", errstr);
       close_socket(sfd);
@@ -1092,43 +1090,42 @@ static jb_socket socks5_connect(const struct forward_spec *fwd,
    }
 
    server_size = read_socket(sfd, sbuf, sizeof(sbuf));
-   if (server_size < 3)
+   if (server_size != sizeof(sbuf))
    {
       errstr = "SOCKS5 negotiation read failed";
-      err = 1;
-   }
-   else if (server_size > 20)
-   {
-      /* This is somewhat unexpected but doesn't realy matter. */
-      log_error(LOG_LEVEL_CONNECT, "socks5_connect: read %d bytes "
-         "from socks server. Would have accepted up to %d.",
-         server_size, sizeof(sbuf));
    }
-
-   if (!err && (sbuf[0] != '\x05'))
-   {
-      errstr = "SOCKS5 negotiation protocol version error";
-      err = 1;
-   }
-
-   if (!err && (sbuf[2] != '\x00'))
-   {
-      errstr = "SOCKS5 negotiation protocol error";
-      err = 1;
-   }
-
-   if (!err)
+   else
    {
-      if (sbuf[1] == SOCKS5_REQUEST_GRANTED)
+      if (sbuf[0] != '\x05')
+      {
+         errstr = "SOCKS5 negotiation protocol version error";
+      }
+      else if (sbuf[2] != '\x00')
+      {
+         errstr = "SOCKS5 negotiation protocol error";
+      }
+      else if (sbuf[1] != SOCKS5_REQUEST_GRANTED)
+      {
+         errstr = translate_socks5_error(sbuf[1]);
+      }
+      else
       {
          return(sfd);
       }
-      errstr = translate_socks5_error(sbuf[1]);
    }
 
    assert(errstr != NULL);
    csp->error_message = strdup(errstr);
-   log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
+   if (server_size == -1)
+   {
+      /*
+       * We didn't read() anything from the server at all.
+       * Don't try to log a negative number of characters
+       * which could trigger an assert().
+       */
+      server_size = 0;
+   }
+   log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s: %N", errstr, server_size, sbuf);
    close_socket(sfd);
    errno = EINVAL;