[tor-commits] [torsocks/master] Optimize onion pool search by IP addr

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:26 UTC 2014


commit 826007d5a3110dac36d8c6ff8b88f060c80994ea
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Fri Aug 23 20:55:49 2013 -0400

    Optimize onion pool search by IP addr
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/common/onion.c |   30 +++++++++++-------------------
 src/common/onion.h |    1 +
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/src/common/onion.c b/src/common/onion.c
index 85585e3..8255c89 100644
--- a/src/common/onion.c
+++ b/src/common/onion.c
@@ -16,6 +16,7 @@
  */
 
 #include <assert.h>
+#include <inttypes.h>
 
 #include "defaults.h"
 #include "log.h"
@@ -110,6 +111,7 @@ int onion_pool_init(struct onion_pool *pool, in_addr_t addr, uint8_t mask)
 	pool->max_pos = pool->base + ((1UL << (32 - mask)) - 1);
 	pool->next_entry_pos = 0;
 	pool->count = 0;
+	pool->mask = mask;
 	tsocks_mutex_init(&pool->lock);
 
 	/*
@@ -127,8 +129,8 @@ int onion_pool_init(struct onion_pool *pool, in_addr_t addr, uint8_t mask)
 		goto error;
 	}
 
-	DBG("[onion] Pool initialized with base %lu, max_pos %lu and size %lu",
-			pool->base, pool->max_pos, pool->size);
+	DBG("[onion] Pool initialized with mask %" PRIu8 ", base %lu, max_pos %lu "
+			"and size %lu", pool->mask, pool->base, pool->max_pos, pool->size);
 
 error:
 	return ret;
@@ -222,7 +224,7 @@ struct onion_entry *onion_entry_find_by_name(const char *onion_name,
 	assert(onion_name);
 	assert(pool);
 
-	DBG("[onion] Finding onion entry for name %s", onion_name);
+	DBG("[onion] Finding onion entry by name %s", onion_name);
 
 	for (i = 0; i < pool->count; i++) {
 		if (strcmp(onion_name, pool->entries[i]->hostname) == 0) {
@@ -246,27 +248,17 @@ end:
 struct onion_entry *onion_entry_find_by_ip(in_addr_t ip,
 		struct onion_pool *pool)
 {
-	int i;
+	uint32_t index;
 	struct onion_entry *entry = NULL;
 
 	DBG("[onion] Finding onion entry for IP %s",
 			inet_ntoa(*((struct in_addr *) &ip)));
 
-	/*
-	 * XXX: This can be improved by simply getting the offset of the IP with
-	 * the pool subnet which gives the index in the pool entries. For instance,
-	 * 127.0.0.45 with a ip_subnet set to 127.0.0.0/24, the index in the pool
-	 * entries is 45.
-	 */
-	for (i = 0; i < pool->count; i++) {
-		if (pool->entries[i]->ip == ip) {
-			entry = pool->entries[i];
-			DBG("[onion] Onion entry name %s found in pool.",
-					entry->hostname);
-			goto end;
-		}
-	}
+	index = ip >> pool->mask;
+	entry = pool->entries[index];
+
+	DBG("[onion] Onion entry name %s found in pool at index %" PRIu32,
+			entry->hostname, index);
 
-end:
 	return entry;
 }
diff --git a/src/common/onion.h b/src/common/onion.h
index 90b4d5b..e866ba6 100644
--- a/src/common/onion.h
+++ b/src/common/onion.h
@@ -77,6 +77,7 @@ struct onion_pool {
 	 */
 	uint32_t base;
 	uint32_t max_pos;
+	uint8_t mask;
 
 	/*
 	 * Current size of the array. This is the number of allocated entry in the





More information about the tor-commits mailing list