Declare 3.0.25 'beta'
[privoxy.git] / jbsockets.c
index d119417..c1a4080 100644 (file)
@@ -1,4 +1,4 @@
-const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.129 2014/10/18 11:27:43 fabiankeil Exp $";
+const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.135 2016/01/16 12:33:35 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
@@ -8,7 +8,7 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.129 2014/10/18 11:27:43 fabia
  *                OS-independent.  Contains #ifdefs to make this work
  *                on many platforms.
  *
- * Copyright   :  Written by and Copyright (C) 2001-2014 the
+ * Copyright   :  Written by and Copyright (C) 2001-2016 the
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -123,6 +123,33 @@ static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client
 static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct client_state *csp);
 #endif
 
+/*********************************************************************
+ *
+ * Function    :  set_no_delay_flag
+ *
+ * Description :  Disables TCP coalescence for the given socket.
+ *
+ * Parameters  :
+ *          1  :  fd = The file descriptor to operate on
+ *
+ * Returns     :  void
+ *
+ *********************************************************************/
+static void set_no_delay_flag(int fd)
+{
+#ifdef TCP_NODELAY
+   int mi = 1;
+
+   if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof(int)))
+   {
+      log_error(LOG_LEVEL_ERROR,
+         "Failed to disable TCP coalescence for socket %d", fd);
+   }
+#else
+#warning set_no_delay_flag() is a nop due to lack of TCP_NODELAY
+#endif /* def TCP_NODELAY */
+}
+
 /*********************************************************************
  *
  * Function    :  connect_to
@@ -279,6 +306,7 @@ static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client
             "Server socket number too high to use select(): %d >= %d",
             fd, FD_SETSIZE);
          close_socket(fd);
+         freeaddrinfo(result);
          return JB_INVALID_SOCKET;
       }
 #endif
@@ -287,12 +315,7 @@ static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client
       mark_socket_for_close_on_execute(fd);
 #endif
 
-#ifdef TCP_NODELAY
-      {  /* turn off TCP coalescence */
-         int mi = 1;
-         setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
-      }
-#endif /* def TCP_NODELAY */
+      set_no_delay_flag(fd);
 
 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
       if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
@@ -480,12 +503,7 @@ static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct cli
    }
 #endif
 
-#ifdef TCP_NODELAY
-   {  /* turn off TCP coalescence */
-      int mi = 1;
-      setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &mi, sizeof (int));
-   }
-#endif /* def TCP_NODELAY */
+   set_no_delay_flag(fd);
 
 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
    if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
@@ -1203,6 +1221,7 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
    int max_selected_socket;
    fd_set selected_fds;
    jb_socket fd;
+   size_t listen_addr_size;
 
    c_length = sizeof(client);
 
@@ -1289,7 +1308,7 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
       struct linger linger_options;
       linger_options.l_onoff  = 1;
       linger_options.l_linger = 5;
-      if (0 != setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger_options, sizeof(linger_options)))
+      if (0 != setsockopt(afd, SOL_SOCKET, SO_LINGER, &linger_options, sizeof(linger_options)))
       {
          log_error(LOG_LEVEL_ERROR, "Setting SO_LINGER on socket %d failed.", afd);
       }
@@ -1311,6 +1330,8 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
    mark_socket_for_close_on_execute(afd);
 #endif
 
+   set_no_delay_flag(afd);
+
    csp->cfd = afd;
 #ifdef HAVE_RFC2553
    csp->ip_addr_str = malloc_or_die(NI_MAXHOST);
@@ -1328,6 +1349,25 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
    csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
 #endif /* def HAVE_RFC2553 */
 
+   /*
+    * Save the name and port of the accepting socket for later lookup.
+    *
+    * The string needs space for strlen(...) + 7 characters:
+    * strlen(haddr[i]) + 1 (':') + 5 (port digits) + 1 ('\0')
+    */
+   listen_addr_size = strlen(csp->config->haddr[i]) + 7;
+   csp->listen_addr_str = malloc_or_die(listen_addr_size);
+   retval = snprintf(csp->listen_addr_str, listen_addr_size,
+      "%s:%d", csp->config->haddr[i], csp->config->hport[i]);
+   if ((-1 == retval) || listen_addr_size <= retval)
+   {
+      log_error(LOG_LEVEL_ERROR,
+         "Server name (%s) and port number (%d) ASCII decimal representation"
+         "don't fit into %d bytes",
+         csp->config->haddr[i], csp->config->hport[i], listen_addr_size);
+      return 0;
+   }
+
    return 1;
 
 }