diff --git a/src/hash.c b/src/hash.c index f902ab6..ba53fc3 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,7 +1,7 @@ #include "proxy.h" -unsigned hashindex(struct hashtable *ht, const unsigned char* hash){ +static unsigned hashindex(struct hashtable *ht, const unsigned char* hash){ unsigned t1, t2, t3, t4; t1 = *(unsigned *)hash; t2 = *(unsigned *)(hash + sizeof(unsigned)); @@ -80,6 +80,25 @@ int inithashtable(struct hashtable *ht, unsigned nhashsize){ return 0; } +static void hashcompact(struct hashtable *ht){ + int i; + struct hashentry *he, **hep; + + if((conf.time - ht->compacted) < 60) return; + for(i = 0; i < ht->hashsize; i++){ + for(hep = ht->hashtable + i; (he = *hep)!=NULL; ){ + if(he->expires < conf.time ) { + (*hep) = he->next; + he->expires = 0; + he->next = ht->hashempty; + ht->hashempty = he; + } + else hep=&(he->next); + } + } + ht->compacted = conf.time; +} + void hashadd(struct hashtable *ht, const void* name, const void* value, time_t expires){ struct hashentry * hen, *he; struct hashentry ** hep; @@ -87,6 +106,9 @@ void hashadd(struct hashtable *ht, const void* name, const void* value, time_t e unsigned index; pthread_mutex_lock(&hash_mutex); + if(!ht->hashempty){ + hashcompact(ht); + } if(!ht||!value||!name||!ht->hashtable||!ht->hashempty) { pthread_mutex_unlock(&hash_mutex); return; diff --git a/src/proxy.h b/src/proxy.h index 2f632d6..6cc36be 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -244,8 +244,6 @@ void genchallenge(struct clientparam *param, char * challenge, char *buf); void mschap(const unsigned char *win_password, const unsigned char *challenge, unsigned char *response); -struct hashtable; -unsigned hashindex(struct hashtable *ht, const unsigned char* hash); void destroyhashtable(struct hashtable *ht); int inithashtable(struct hashtable *ht, unsigned nhashsize); void hashadd(struct hashtable *ht, const void* name, const void* value, time_t expires); diff --git a/src/structures.h b/src/structures.h index 6ea8751..ffd90aa 100644 --- a/src/structures.h +++ b/src/structures.h @@ -770,6 +770,7 @@ struct hashtable { struct hashentry * hashempty; void (*index2hash)(const void *index, unsigned char *hash, const unsigned char *rnd); int grow; + time_t compacted; }; extern struct hashtable dns_table;