----------  Forwarded Message  ----------

Subject: Re: [PATCH] Include local IP address in the connection cache hash
Date: Monday, March 11, 2013, 23:12:55
From: Linus Nielsen Feltzing <[email protected]>
To: Kamil Dudka <[email protected]>

On 03/11/2013 10:02 PM, Kamil Dudka wrote:
> Please consider replacing the open-coded string concatenation above by:
>
>      return aprintf("%s%s", conn->local_ip, conn->host.name);

Sure. Here is a new patch, using aprintf() instead.

Linus
-----------------------------------------

Thanks!  I am forwarding the new patch back to the mailing list...

Kamil
>From d22a60595b7a2806ee85bbc713654ff678810e34 Mon Sep 17 00:00:00 2001
From: Linus Nielsen Feltzing <[email protected]>
Date: Fri, 8 Mar 2013 10:52:32 +0100
Subject: [PATCH] Add local IP address to the hash key for the connection
 cache.

---
 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
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to