cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] Add local IP address to the hash key for the connection cache.

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

---
 lib/conncache.c |   41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)
diff --git a/lib/conncache.c b/lib/conncache.c
index 530cdc2..bc10bf1 100644
--- a/lib/conncache.c
+++ b/lib/conncache.c
@@ -47,6 +47,21 @@ 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)
+{
+  size_t iplen = strlen(conn->local_ip);
+  size_t hostlen = strlen(conn->host.name);
+  char *key = malloc(hostlen + iplen + 1);
+
+  if(!key)
+    return NULL;
+
+  strcpy(key, conn->local_ip);
+  strcpy(key + iplen, conn->host.name);
+  return key;
+}
+
 struct conncache *Curl_conncache_init(void)
 {
   struct conncache *connc;
@@ -76,23 +91,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 +143,23 @@ CURLcode Curl_conncache_add_conn(struct conncache *connc,
   struct connectbundle *bundle;
   struct connectbundle *new_bundle = NULL;
   struct SessionHandle *data = conn->data;
+  char *key;
 
-  bundle = Curl_conncache_find_bundle(data->state.conn_cache,
-                                      conn->host.name);
+  key = create_hash_key(conn);
+  if(!key)
+    return CURLE_OUT_OF_MEMORY;
+
+  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 +169,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
--------------070608000007010406010801
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
--------------070608000007010406010801--
Received on 2001-09-17