+#ifdef FEATURE_CONNECTION_SHARING
+#ifndef FEATURE_CONNECTION_KEEP_ALIVE
+#error Using FEATURE_CONNECTION_SHARING without FEATURE_CONNECTION_KEEP_ALIVE is impossible
+#endif
+
+#define MAX_REUSABLE_CONNECTIONS 100
+
+static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
+static int mark_connection_unused(const struct reusable_connection *connection);
+
+/*********************************************************************
+ *
+ * Function : initialize_reusable_connections
+ *
+ * Description : Initializes the reusable_connection structures.
+ * Must be called with connection_reuse_mutex locked.
+ *
+ * Parameters : N/A
+ *
+ * Returns : void
+ *
+ *********************************************************************/
+extern void initialize_reusable_connections(void)
+{
+ unsigned int slot = 0;
+
+#if !defined(HAVE_POLL) && !defined(_WIN32)
+ log_error(LOG_LEVEL_INFO,
+ "Detecting already dead connections might not work "
+ "correctly on your platform. In case of problems, "
+ "unset the keep-alive-timeout option.");
+#endif
+
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ mark_connection_closed(&reusable_connection[slot]);
+ }
+
+ log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
+}
+
+
+/*********************************************************************
+ *
+ * Function : remember_connection
+ *
+ * Description : Remembers a server connection for reuse later on.
+ *
+ * Parameters :
+ * 1 : connection = The server connection to remember.
+ *
+ * Returns : void
+ *
+ *********************************************************************/
+void remember_connection(const struct reusable_connection *connection)
+{
+ unsigned int slot = 0;
+ int free_slot_found = FALSE;
+
+ assert(NULL != connection);
+ assert(connection->sfd != JB_INVALID_SOCKET);
+
+ if (mark_connection_unused(connection))
+ {
+ return;
+ }
+
+ privoxy_mutex_lock(&connection_reuse_mutex);
+
+ /* Find free socket slot. */
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
+ {
+ assert(reusable_connection[slot].in_use == 0);
+ log_error(LOG_LEVEL_CONNECT,
+ "Remembering socket %d for %s:%d in slot %d.",
+ connection->sfd, connection->host, connection->port, slot);
+ free_slot_found = TRUE;
+ break;
+ }
+ }
+
+ if (!free_slot_found)
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "No free slots found to remember socket for %s:%d. Last slot %d.",
+ connection->host, connection->port, slot);
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+ close_socket(connection->sfd);
+ return;
+ }
+
+ assert(slot < SZ(reusable_connection));
+ assert(NULL != connection->host);
+ reusable_connection[slot].host = strdup_or_die(connection->host);
+ reusable_connection[slot].sfd = connection->sfd;
+ reusable_connection[slot].port = connection->port;
+ reusable_connection[slot].in_use = 0;
+ reusable_connection[slot].timestamp = connection->timestamp;
+ reusable_connection[slot].request_sent = connection->request_sent;
+ reusable_connection[slot].response_received = connection->response_received;
+ reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
+ reusable_connection[slot].requests_sent_total = connection->requests_sent_total;
+
+ assert(reusable_connection[slot].gateway_host == NULL);
+ assert(reusable_connection[slot].gateway_port == 0);
+ assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
+ assert(reusable_connection[slot].forward_host == NULL);
+ assert(reusable_connection[slot].forward_port == 0);
+
+ reusable_connection[slot].forwarder_type = connection->forwarder_type;
+ if (NULL != connection->gateway_host)
+ {
+ reusable_connection[slot].gateway_host = strdup_or_die(connection->gateway_host);
+ }
+ else
+ {
+ reusable_connection[slot].gateway_host = NULL;
+ }
+ reusable_connection[slot].gateway_port = connection->gateway_port;
+
+ if (NULL != connection->forward_host)
+ {
+ reusable_connection[slot].forward_host = strdup_or_die(connection->forward_host);
+ }
+ else
+ {
+ reusable_connection[slot].forward_host = NULL;
+ }
+ reusable_connection[slot].forward_port = connection->forward_port;
+
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+}
+#endif /* def FEATURE_CONNECTION_SHARING */
+
+
+/*********************************************************************
+ *
+ * Function : mark_connection_closed
+ *
+ * Description : Marks a reused connection closed.
+ *
+ * Parameters :
+ * 1 : closed_connection = The connection to mark as closed.
+ *
+ * Returns : void
+ *
+ *********************************************************************/
+void mark_connection_closed(struct reusable_connection *closed_connection)
+{
+ closed_connection->in_use = FALSE;
+ closed_connection->sfd = JB_INVALID_SOCKET;
+ freez(closed_connection->host);
+ closed_connection->port = 0;
+ closed_connection->timestamp = 0;
+ closed_connection->request_sent = 0;
+ closed_connection->response_received = 0;
+ closed_connection->keep_alive_timeout = 0;
+ closed_connection->requests_sent_total = 0;
+ closed_connection->forwarder_type = SOCKS_NONE;
+ freez(closed_connection->gateway_host);
+ closed_connection->gateway_port = 0;
+ freez(closed_connection->forward_host);
+ closed_connection->forward_port = 0;
+}
+
+
+#ifdef FEATURE_CONNECTION_SHARING
+/*********************************************************************
+ *
+ * Function : forget_connection
+ *
+ * Description : Removes a previously remembered connection from
+ * the list of reusable connections.
+ *
+ * Parameters :
+ * 1 : sfd = The socket belonging to the connection in question.
+ *
+ * Returns : void
+ *
+ *********************************************************************/
+void forget_connection(jb_socket sfd)
+{
+ unsigned int slot = 0;
+
+ assert(sfd != JB_INVALID_SOCKET);
+
+ privoxy_mutex_lock(&connection_reuse_mutex);
+
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ if (reusable_connection[slot].sfd == sfd)
+ {
+ assert(reusable_connection[slot].in_use);
+
+ log_error(LOG_LEVEL_CONNECT,
+ "Forgetting socket %d for %s:%d in slot %d.",
+ sfd, reusable_connection[slot].host,
+ reusable_connection[slot].port, slot);
+ mark_connection_closed(&reusable_connection[slot]);
+ break;
+ }
+ }
+
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+
+}
+#endif /* def FEATURE_CONNECTION_SHARING */
+
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+/*********************************************************************
+ *
+ * Function : connection_destination_matches
+ *
+ * Description : Determines whether a remembered connection can
+ * be reused. That is, whether the destination and
+ * the forwarding settings match.
+ *
+ * Parameters :
+ * 1 : connection = The connection to check.
+ * 2 : http = The destination for the connection.
+ * 3 : fwd = The forwarder settings.
+ *
+ * Returns : TRUE for yes, FALSE otherwise.
+ *
+ *********************************************************************/
+int connection_destination_matches(const struct reusable_connection *connection,
+ const struct http_request *http,
+ const struct forward_spec *fwd)
+{
+ if ((connection->forwarder_type != fwd->type)
+ || (connection->gateway_port != fwd->gateway_port)
+ || (connection->forward_port != fwd->forward_port)
+ || (connection->port != http->port))
+ {
+ return FALSE;
+ }
+
+ if (( (NULL != connection->gateway_host)
+ && (NULL != fwd->gateway_host)
+ && strcmpic(connection->gateway_host, fwd->gateway_host))
+ && (connection->gateway_host != fwd->gateway_host))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Gateway mismatch. Previous gateway: %s. Current gateway: %s",
+ connection->gateway_host, fwd->gateway_host);
+ return FALSE;
+ }
+
+ if (( (NULL != connection->forward_host)
+ && (NULL != fwd->forward_host)
+ && strcmpic(connection->forward_host, fwd->forward_host))
+ && (connection->forward_host != fwd->forward_host))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Forwarding proxy mismatch. Previous proxy: %s. Current proxy: %s",
+ connection->forward_host, fwd->forward_host);
+ return FALSE;
+ }
+
+ return (!strcmpic(connection->host, http->host));
+
+}
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
+
+#ifdef FEATURE_CONNECTION_SHARING
+/*********************************************************************
+ *
+ * Function : close_unusable_connections
+ *
+ * Description : Closes remembered connections that have timed
+ * out or have been closed on the other side.
+ *
+ * Parameters : none
+ *
+ * Returns : Number of connections that are still alive.
+ *
+ *********************************************************************/
+int close_unusable_connections(void)
+{
+ unsigned int slot = 0;
+ int connections_alive = 0;
+
+ privoxy_mutex_lock(&connection_reuse_mutex);
+
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ if (!reusable_connection[slot].in_use
+ && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
+ {
+ time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
+ time_t latency = (reusable_connection[slot].response_received -
+ reusable_connection[slot].request_sent) / 2;
+
+ if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "The connection to %s:%d in slot %d timed out. "
+ "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
+ reusable_connection[slot].host,
+ reusable_connection[slot].port, slot,
+ reusable_connection[slot].sfd,
+ reusable_connection[slot].keep_alive_timeout,
+ latency);
+ close_socket(reusable_connection[slot].sfd);
+ mark_connection_closed(&reusable_connection[slot]);
+ }
+ else if (!socket_is_still_alive(reusable_connection[slot].sfd))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "The connection to %s:%d in slot %d is no longer usable. "
+ "Closing socket %d.", reusable_connection[slot].host,
+ reusable_connection[slot].port, slot,
+ reusable_connection[slot].sfd);
+ close_socket(reusable_connection[slot].sfd);
+ mark_connection_closed(&reusable_connection[slot]);
+ }
+ else
+ {
+ connections_alive++;
+ }
+ }
+ }
+
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+
+ return connections_alive;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_reusable_connection
+ *
+ * Description : Returns an open socket to a previously remembered
+ * open connection (if there is one).
+ *
+ * Parameters :
+ * 1 : http = The destination for the connection.
+ * 2 : fwd = The forwarder settings.
+ *
+ * Returns : JB_INVALID_SOCKET => No reusable connection found,
+ * otherwise a usable socket.
+ *
+ *********************************************************************/
+static jb_socket get_reusable_connection(const struct http_request *http,
+ const struct forward_spec *fwd)
+{
+ jb_socket sfd = JB_INVALID_SOCKET;
+ unsigned int slot = 0;
+
+ close_unusable_connections();
+
+ privoxy_mutex_lock(&connection_reuse_mutex);
+
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ if (!reusable_connection[slot].in_use
+ && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
+ {
+ if (connection_destination_matches(&reusable_connection[slot], http, fwd))
+ {
+ reusable_connection[slot].in_use = TRUE;
+ sfd = reusable_connection[slot].sfd;
+ log_error(LOG_LEVEL_CONNECT,
+ "Found reusable socket %d for %s:%d in slot %d. Timestamp made %d "
+ "seconds ago. Timeout: %d. Latency: %d. Requests served: %d",
+ sfd, reusable_connection[slot].host, reusable_connection[slot].port,
+ slot, time(NULL) - reusable_connection[slot].timestamp,
+ reusable_connection[slot].keep_alive_timeout,
+ (int)(reusable_connection[slot].response_received -
+ reusable_connection[slot].request_sent),
+ reusable_connection[slot].requests_sent_total);
+ break;
+ }
+ }
+ }
+
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+
+ return sfd;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : mark_connection_unused
+ *
+ * Description : Gives a remembered connection free for reuse.
+ *
+ * Parameters :
+ * 1 : connection = The connection in question.
+ *
+ * Returns : TRUE => Socket found and marked as unused.
+ * FALSE => Socket not found.
+ *
+ *********************************************************************/
+static int mark_connection_unused(const struct reusable_connection *connection)
+{
+ unsigned int slot = 0;
+ int socket_found = FALSE;
+
+ assert(connection->sfd != JB_INVALID_SOCKET);
+
+ privoxy_mutex_lock(&connection_reuse_mutex);
+
+ for (slot = 0; slot < SZ(reusable_connection); slot++)
+ {
+ if (reusable_connection[slot].sfd == connection->sfd)
+ {
+ assert(reusable_connection[slot].in_use);
+ socket_found = TRUE;
+ log_error(LOG_LEVEL_CONNECT,
+ "Marking open socket %d for %s:%d in slot %d as unused.",
+ connection->sfd, reusable_connection[slot].host,
+ reusable_connection[slot].port, slot);
+ reusable_connection[slot].in_use = 0;
+ reusable_connection[slot].timestamp = connection->timestamp;
+ break;
+ }
+ }
+
+ privoxy_mutex_unlock(&connection_reuse_mutex);
+
+ return socket_found;
+
+}
+#endif /* def FEATURE_CONNECTION_SHARING */
+