cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] Connection cache: Add local IP address to the hash key.

From: Linus Nielsen Feltzing <linus_at_haxx.se>
Date: Fri, 8 Mar 2013 10:52:32 +0100

Use both remote host name and local IP to create the hash key
used for the connection cache. This allows for correct reuse
when using multiple network interfaces.

---
 lib/conncache.c |   35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/lib/conncache.c b/lib/conncache.c
index 530cdc2..dcb775d 100644
--- a/lib/conncache.c
+++ b/lib/conncache.c
@@ -34,6 +34,9 @@
 #include "bundles.h"
 #include "conncache.h"
 
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
 #include "curl_memory.h"
 /* The last #include file should be: */
 #include "memdebug.h"
@@ -47,6 +50,12 @@ static void free_bundle_hash_entry(void *freethis)
   Curl_bundle_destroy(b);
 }
 
+/* Combine hostname and local IP address to create a keystring for hashing */
+static char *create_hash_key(struct connectdata *conn)
+{
+  return aprintf("%s%s", conn->local_ip, conn->host.name);
+}
+
 struct conncache *Curl_conncache_init(void)
 {
   struct conncache *connc;
@@ -76,23 +85,23 @@ void Curl_conncache_destroy(struct conncache *connc)
 }
 
 struct connectbundle *Curl_conncache_find_bundle(struct conncache *connc,
-                                                 char *hostname)
+                                                 char *key)
 {
   struct connectbundle *bundle = NULL;
 
   if(connc)
-    bundle = Curl_hash_pick(connc->hash, hostname, strlen(hostname)+1);
+    bundle = Curl_hash_pick(connc->hash, key, strlen(key)+1);
 
   return bundle;
 }
 
 static bool conncache_add_bundle(struct conncache *connc,
-                                 char *hostname,
+                                 char *key,
                                  struct connectbundle *bundle)
 {
   void *p;
 
-  p = Curl_hash_add(connc->hash, hostname, strlen(hostname)+1, bundle);
+  p = Curl_hash_add(connc->hash, key, strlen(key)+1, bundle);
 
   return p?TRUE:FALSE;
 }
@@ -128,17 +137,23 @@ CURLcode Curl_conncache_add_conn(struct conncache *connc,
   struct connectbundle *bundle;
   struct connectbundle *new_bundle = NULL;
   struct SessionHandle *data = conn->data;
+  char *key;
+
+  key = create_hash_key(conn);
+  if(!key)
+    return CURLE_OUT_OF_MEMORY;
 
-  bundle = Curl_conncache_find_bundle(data->state.conn_cache,
-                                      conn->host.name);
+  bundle = Curl_conncache_find_bundle(data->state.conn_cache, key);
   if(!bundle) {
     result = Curl_bundle_create(data, &new_bundle);
-    if(result != CURLE_OK)
+    if(result != CURLE_OK) {
+      Curl_safefree(key);
       return result;
+    }
 
-    if(!conncache_add_bundle(data->state.conn_cache,
-                             conn->host.name, new_bundle)) {
+    if(!conncache_add_bundle(data->state.conn_cache, key, new_bundle)) {
       Curl_bundle_destroy(new_bundle);
+      Curl_safefree(key);
       return CURLE_OUT_OF_MEMORY;
     }
     bundle = new_bundle;
@@ -148,11 +163,13 @@ CURLcode Curl_conncache_add_conn(struct conncache *connc,
   if(result != CURLE_OK) {
     if(new_bundle)
       conncache_remove_bundle(data->state.conn_cache, new_bundle);
+    Curl_safefree(key);
     return result;
   }
 
   connc->num_connections++;
 
+  Curl_safefree(key);
   return CURLE_OK;
 }
 
-- 
1.7.10.4
--------------080906090803050301070502
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
--------------080906090803050301070502--
Received on 2001-09-17