mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-26 23:59:41 +08:00
Refactor hashtables to use indices instead of pointers, use blake2 as a hash, mycrypt renamed to 3proxy_crypt
This commit is contained in:
1 parent
4ee7f71fb9
commit
f1af44f3a9
11 files changed
+880
-136
No files matched your search
+61
-66
@@ -1,21 +1,16 @@
|
||||
#include "proxy.h"
|
||||
#include "libs/blake2.h"
|
||||
|
||||
|
||||
static unsigned hashindex(struct hashtable *ht, const unsigned char* hash){
|
||||
unsigned t1, t2, t3, t4;
|
||||
t1 = *(unsigned *)hash;
|
||||
t2 = *(unsigned *)(hash + sizeof(unsigned));
|
||||
t3 = *(unsigned *)(hash + (2*sizeof(unsigned)));
|
||||
t4 = *(unsigned *)(hash + (3*sizeof(unsigned)));
|
||||
return (t1 + (t2 * 7) + (t3 * 17) + (t4 * 29) ) % (ht->tablesize);
|
||||
static unsigned hashindex(struct hashtable *ht, const uint8_t* hash){
|
||||
return (*(unsigned *)hash ) % (ht->tablesize);
|
||||
}
|
||||
|
||||
|
||||
void destroyhashtable(struct hashtable *ht){
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
if(ht->hashtable){
|
||||
myfree(ht->hashtable);
|
||||
ht->hashtable = NULL;
|
||||
if(ht->ihashtable){
|
||||
myfree(ht->ihashtable);
|
||||
ht->ihashtable = NULL;
|
||||
}
|
||||
if(ht->hashvalues){
|
||||
myfree(ht->hashvalues);
|
||||
@@ -26,7 +21,7 @@ void destroyhashtable(struct hashtable *ht){
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
}
|
||||
|
||||
#define hvalue(ht,I) ((struct hashentry *)(ht->hashvalues + (I)*(sizeof(struct hashentry) + ht->recsize - 4)))
|
||||
#define hvalue(ht,I) ((struct hashentry *)(ht->hashvalues + (I-1)*(sizeof(struct hashentry) + ht->recsize - 4)))
|
||||
|
||||
int inithashtable(struct hashtable *ht, unsigned nhashsize){
|
||||
unsigned i;
|
||||
@@ -47,124 +42,124 @@ int inithashtable(struct hashtable *ht, unsigned nhashsize){
|
||||
|
||||
if(nhashsize<4) return 1;
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
if(ht->hashtable){
|
||||
myfree(ht->hashtable);
|
||||
ht->hashtable = NULL;
|
||||
if(ht->ihashtable){
|
||||
myfree(ht->ihashtable);
|
||||
ht->ihashtable = NULL;
|
||||
}
|
||||
if(ht->hashvalues){
|
||||
myfree(ht->hashvalues);
|
||||
ht->hashvalues = NULL;
|
||||
}
|
||||
ht->hashsize = 0;
|
||||
if(!(ht->hashtable = myalloc((nhashsize>>2) * sizeof(struct hashentry *)))){
|
||||
if(!(ht->ihashtable = myalloc((nhashsize>>2) * sizeof(int)))){
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 2;
|
||||
}
|
||||
if(!(ht->hashvalues = myalloc(nhashsize * (sizeof(struct hashentry) + (ht->recsize-4))))){
|
||||
myfree(ht->hashtable);
|
||||
ht->hashtable = NULL;
|
||||
myfree(ht->ihashtable);
|
||||
ht->ihashtable = NULL;
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 3;
|
||||
}
|
||||
ht->hashsize = nhashsize;
|
||||
ht->tablesize = (nhashsize>>2);
|
||||
ht->rnd[0] = myrand(&tb, sizeof(tb));
|
||||
ht->rnd[1] = myrand(ht->hashtable, sizeof(ht->hashtable));
|
||||
ht->rnd[1] = myrand(ht->ihashtable, sizeof(ht->ihashtable));
|
||||
ht->rnd[2] = myrand(&c, sizeof(c));
|
||||
ht->rnd[3] = myrand(ht->hashvalues,sizeof(ht->hashvalues));
|
||||
memset(ht->hashtable, 0, ht->tablesize * sizeof(struct hashentry *));
|
||||
memset(ht->ihashtable, 0, ht->tablesize * sizeof(struct hashentry *));
|
||||
memset(ht->hashvalues, 0, ht->hashsize * (sizeof(struct hashentry) + ht->recsize - 4));
|
||||
|
||||
for(i = 0; i< (ht->hashsize - 1); i++) {
|
||||
hvalue(ht,i)->next = hvalue(ht,i+1);
|
||||
for(i = 1; i < ht->hashsize; i++) {
|
||||
hvalue(ht,i)->inext = i+1;
|
||||
}
|
||||
ht->hashempty = (struct hashentry *)ht->hashvalues;
|
||||
ht->ihashempty = 1;
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void hashcompact(struct hashtable *ht){
|
||||
int i;
|
||||
struct hashentry *he, **hep;
|
||||
int he, *hep;
|
||||
|
||||
if((conf.time - ht->compacted) < 60) return;
|
||||
if((conf.time - ht->compacted) < 60 || !ht->tablesize || !ht->hashsize || ht->hashsize/ht->tablesize >= 4 ) return;
|
||||
for(i = 0; i < ht->tablesize; 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;
|
||||
for(hep = ht->ihashtable + i; (he = *hep) != 0; ){
|
||||
if(hvalue(ht,he)->expires < conf.time ) {
|
||||
(*hep) = hvalue(ht,he)->inext;
|
||||
hvalue(ht,he)->expires = 0;
|
||||
hvalue(ht,he)->inext = ht->ihashempty;
|
||||
ht->ihashempty = he;
|
||||
}
|
||||
else hep=&(he->next);
|
||||
else hep=&(hvalue(ht,he)->inext);
|
||||
}
|
||||
}
|
||||
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;
|
||||
int hen, he;
|
||||
int *hep;
|
||||
|
||||
unsigned index;
|
||||
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
if(!ht->hashempty){
|
||||
if(!ht->ihashempty){
|
||||
hashcompact(ht);
|
||||
}
|
||||
if(!ht||!value||!name||!ht->hashtable||!ht->hashempty) {
|
||||
if(!ht||!value||!name||!ht->ihashtable||!ht->ihashempty) {
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return;
|
||||
}
|
||||
hen = ht->hashempty;
|
||||
ht->hashempty = ht->hashempty->next;
|
||||
ht->index2hash(name, hen->hash, (unsigned char *)ht->rnd);
|
||||
memcpy(hen->value, value, ht->recsize);
|
||||
hen->expires = expires;
|
||||
hen->next = NULL;
|
||||
index = hashindex(ht, hen->hash);
|
||||
hen = ht->ihashempty;
|
||||
ht->ihashempty = hvalue(ht,ht->ihashempty)->inext;
|
||||
ht->index2hash(name, hvalue(ht,hen)->hash, (unsigned char *)ht->rnd);
|
||||
memcpy(hvalue(ht,hen)->value, value, ht->recsize);
|
||||
hvalue(ht,hen)->expires = expires;
|
||||
hvalue(ht,hen)->inext = 0;
|
||||
index = hashindex(ht, hvalue(ht,hen)->hash);
|
||||
|
||||
for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
|
||||
if(he->expires < conf.time || !memcmp(hen->hash, he->hash, sizeof(he->hash))) {
|
||||
(*hep) = he->next;
|
||||
he->expires = 0;
|
||||
he->next = ht->hashempty;
|
||||
ht->hashempty = he;
|
||||
for(hep = ht->ihashtable + index; (he = *hep)!=0; ){
|
||||
if(hvalue(ht,he)->expires < conf.time || !memcmp(hvalue(ht,hen)->hash, hvalue(ht,he)->hash, HASH_SIZE)) {
|
||||
(*hep) = hvalue(ht,he)->inext;
|
||||
hvalue(ht,he)->expires = 0;
|
||||
hvalue(ht,he)->inext = ht->ihashempty;
|
||||
ht->ihashempty = he;
|
||||
}
|
||||
else hep=&(he->next);
|
||||
else hep=&(hvalue(ht,he)->inext);
|
||||
}
|
||||
hen->next = ht->hashtable[index];
|
||||
ht->hashtable[index] = hen;
|
||||
hvalue(ht,hen)->inext = ht->ihashtable[index];
|
||||
ht->ihashtable[index] = hen;
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
}
|
||||
|
||||
int hashresolv(struct hashtable *ht, const void* name, void* value, uint32_t *ttl){
|
||||
unsigned char hash[sizeof(unsigned)*4];
|
||||
struct hashentry ** hep;
|
||||
struct hashentry *he;
|
||||
uint8_t hash[HASH_SIZE];
|
||||
int *hep;
|
||||
int he;
|
||||
unsigned index;
|
||||
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
if(!ht || !ht->hashtable || !name) {
|
||||
if(!ht || !ht->ihashtable || !name) {
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 0;
|
||||
}
|
||||
ht->index2hash(name, hash, (unsigned char *)ht->rnd);
|
||||
index = hashindex(ht, hash);
|
||||
for(hep = ht->hashtable + index; (he = *hep)!=NULL; ){
|
||||
if(he->expires < conf.time) {
|
||||
(*hep) = he->next;
|
||||
he->expires = 0;
|
||||
he->next = ht->hashempty;
|
||||
ht->hashempty = he;
|
||||
for(hep = ht->ihashtable + index; (he = *hep)!=0; ){
|
||||
if(hvalue(ht, he)->expires < conf.time) {
|
||||
(*hep) = hvalue(ht,he)->inext;
|
||||
hvalue(ht,he)->expires = 0;
|
||||
hvalue(ht,he)->inext = ht->ihashempty;
|
||||
ht->ihashempty = he;
|
||||
}
|
||||
else if(!memcmp(hash, he->hash, sizeof(unsigned)*4)){
|
||||
if(ttl) *ttl = (uint32_t)(he->expires - conf.time);
|
||||
memcpy(value, he->value, ht->recsize);
|
||||
else if(!memcmp(hash, hvalue(ht,he)->hash, HASH_SIZE)){
|
||||
if(ttl) *ttl = (uint32_t)(hvalue(ht,he)->expires - conf.time);
|
||||
memcpy(value, hvalue(ht,he)->value, ht->recsize);
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 1;
|
||||
}
|
||||
else hep=&(he->next);
|
||||
else hep=&(hvalue(ht,he)->inext);
|
||||
}
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user