In rfc2553_connect_to(), remove a useless check for NULL
[privoxy.git] / jbsockets.c
index 02e5c75..81befe3 100644 (file)
@@ -1,4 +1,4 @@
-const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.78 2010/07/26 11:28:11 fabiankeil Exp $";
+const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.83 2011/03/27 13:53:25 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
@@ -118,6 +118,11 @@ const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
 
 #define MAX_LISTEN_BACKLOG 128
 
+#ifdef HAVE_RFC2553
+static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
+#else
+static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
+#endif
 
 /*********************************************************************
  *
@@ -130,15 +135,40 @@ const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION;
  *          1  :  host = hostname to connect to
  *          2  :  portnum = port to connent on (XXX: should be unsigned)
  *          3  :  csp = Current client state (buffers, headers, etc...)
- *                      Not modified, only used for source IP and ACL.
  *
  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket
  *                file descriptor.
  *
  *********************************************************************/
+jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
+{
+   jb_socket fd;
+   int forwarded_connect_retries = 0;
+
+   do
+   {
+#ifdef HAVE_RFC2553
+      fd = rfc2553_connect_to(host, portnum, csp);
+#else
+      fd = no_rfc2553_connect_to(host, portnum, csp);
+#endif
+      if ((fd != JB_INVALID_SOCKET) || (errno != EINVAL))
+      {
+         break;
+      }
+      forwarded_connect_retries++;
+      log_error(LOG_LEVEL_ERROR,
+         "Attempt %d of %d to connect to %s failed. Trying again.",
+         forwarded_connect_retries, csp->config->forwarded_connect_retries, host);
+
+   } while (forwarded_connect_retries < csp->config->forwarded_connect_retries);
+
+   return fd;
+}
+
 #ifdef HAVE_RFC2553
 /* Getaddrinfo implementation */
-jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
+static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client_state *csp)
 {
    struct addrinfo hints, *result, *rp;
    char service[6];
@@ -208,12 +238,11 @@ jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
       }
       retval = getnameinfo(rp->ai_addr, rp->ai_addrlen,
          csp->http->host_ip_addr_str, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
-      if (!csp->http->host_ip_addr_str || retval)
+      if (retval)
       {
          log_error(LOG_LEVEL_ERROR,
             "Can not save csp->http->host_ip_addr_str: %s",
-            (csp->http->host_ip_addr_str) ?
-            gai_strerror(retval) : "Insufficient memory");
+            gai_strerror(retval));
          freez(csp->http->host_ip_addr_str);
          continue;
       }
@@ -292,10 +321,6 @@ jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
       if ((select((int)fd + 1, NULL, &wfds, NULL, tv) > 0)
          && FD_ISSET(fd, &wfds))
       {
-         /*
-          * See Linux connect(2) man page for more info
-          * about connecting on non-blocking socket.
-          */
          int socket_in_error;
          socklen_t optlen = sizeof(socket_in_error);
          if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_in_error, &optlen))
@@ -337,7 +362,7 @@ jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
 #else /* ndef HAVE_RFC2553 */
 /* Pre-getaddrinfo implementation */
 
-jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
+static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct client_state *csp)
 {
    struct sockaddr_in inaddr;
    jb_socket fd;