Fix authcachesize
Some checks are pending
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI MacOS / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI Windows / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (windows-2022) (push) Waiting to run

This commit is contained in:
Vladimir Dubrovin 2026-04-20 20:13:21 +03:00
parent 3957210609
commit 8bf0d3f4ae
4 changed files with 27 additions and 28 deletions

View File

@ -655,14 +655,14 @@ static int h_fakeresolve(int argc, unsigned char **argv){
} }
static int h_nscache(int argc, unsigned char **argv){ static int h_nscache(int argc, unsigned char **argv){
int res; unsigned res;
res = atoi((char *)argv[1]); res = (unsigned)atoi((char *)argv[1]);
if(res < 256) { if(res < 256) {
fprintf(stderr, "Invalid NS cache size: %d\n", res); fprintf(stderr, "Invalid NS cache size: %d\n", res);
return 1; return 1;
} }
if(inithashtable(&dns_table, (unsigned)res)){ if(inithashtable(&dns_table, (res << 2), (res << 2), res)){
fprintf(stderr, "Failed to initialize NS cache\n"); fprintf(stderr, "Failed to initialize NS cache\n");
return 2; return 2;
} }
@ -678,14 +678,14 @@ static int h_parentretries(int argc, unsigned char **argv){
} }
static int h_nscache6(int argc, unsigned char **argv){ static int h_nscache6(int argc, unsigned char **argv){
int res; unsigned res;
res = atoi((char *)argv[1]); res = (unsigned)atoi((char *)argv[1]);
if(res < 256) { if(res < 256) {
fprintf(stderr, "Invalid NS cache size: %d\n", res); fprintf(stderr, "Invalid NS cache size: %d\n", res);
return 1; return 1;
} }
if(inithashtable(&dns6_table, (unsigned)res)){ if(inithashtable(&dns6_table, (res<<2), (res<<2), res)){
fprintf(stderr, "Failed to initialize NS cache\n"); fprintf(stderr, "Failed to initialize NS cache\n");
return 2; return 2;
} }
@ -1429,8 +1429,9 @@ static int h_radius(int argc, unsigned char **argv){
} }
#endif #endif
static int h_authcache(int argc, unsigned char **argv){ static int h_authcache(int argc, unsigned char **argv){
int authcachesize = 0;
conf.authcachetype = 0; conf.authcachetype = 0;
int authcachesize;
if(strstr((char *) *(argv + 1), "ip")) conf.authcachetype |= 1; if(strstr((char *) *(argv + 1), "ip")) conf.authcachetype |= 1;
if(strstr((char *) *(argv + 1), "user")) conf.authcachetype |= 2; if(strstr((char *) *(argv + 1), "user")) conf.authcachetype |= 2;
if(strstr((char *) *(argv + 1), "pass")) conf.authcachetype |= 4; if(strstr((char *) *(argv + 1), "pass")) conf.authcachetype |= 4;
@ -1438,14 +1439,14 @@ static int h_authcache(int argc, unsigned char **argv){
if(strstr((char *) *(argv + 1), "acl")) conf.authcachetype |= 16; if(strstr((char *) *(argv + 1), "acl")) conf.authcachetype |= 16;
if(strstr((char *) *(argv + 1), "ext")) conf.authcachetype |= 32; if(strstr((char *) *(argv + 1), "ext")) conf.authcachetype |= 32;
if(argc > 2) conf.authcachetime = (unsigned) atoi((char *) *(argv + 2)); if(argc > 2) conf.authcachetime = (unsigned) atoi((char *) *(argv + 2));
if(argc > 3) authcachesize = (unsigned) atoi((char *) *(argv + 2)); if(argc > 3) authcachesize = (unsigned) atoi((char *) *(argv + 3));
if(!conf.authcachetype) conf.authcachetype = 6; if(!conf.authcachetype) conf.authcachetype = 6;
if(!conf.authcachetime) conf.authcachetime = 600; if(!conf.authcachetime) conf.authcachetime = 600;
if(inithashtable(&auth_table, authcachesize? authcachesize : 4096)){ if(!authcachesize) authcachesize = 65536*4;
if(inithashtable(&auth_table, 1024, 1024, authcachesize)){
fprintf(stderr, "Failed to initialize auth cache\n"); fprintf(stderr, "Failed to initialize auth cache\n");
return 2; return 2;
} }
if(!authcachesize)auth_table.growlimit = 65536*4;
return 0; return 0;
} }

View File

@ -35,9 +35,8 @@ void destroyhashtable(struct hashtable *ht){
#define hvalue(ht,I) ((struct hashentry *)(ht->hashvalues + (I-1)*(sizeof(struct hashentry) + ht->recsize - 4))) #define hvalue(ht,I) ((struct hashentry *)(ht->hashvalues + (I-1)*(sizeof(struct hashentry) + ht->recsize - 4)))
#define hhash(ht,I) ((ht->hashhashvalues + (I-1)*(ht->hash_size))) #define hhash(ht,I) ((ht->hashhashvalues + (I-1)*(ht->hash_size)))
int inithashtable(struct hashtable *ht, unsigned npoolsize){ int inithashtable(struct hashtable *ht, unsigned tablesize, unsigned poolsize, unsigned growlimit){
unsigned i; unsigned i;
unsigned tablesize, poolsize;
clock_t c; clock_t c;
#ifdef _WIN32 #ifdef _WIN32
@ -52,8 +51,7 @@ int inithashtable(struct hashtable *ht, unsigned npoolsize){
#endif #endif
c = clock(); c = clock();
poolsize = tablesize = (npoolsize >> 2); if(tablesize < 2 || poolsize < tablesize || growlimit < poolsize) return 1;
if(tablesize < 2) return 1;
pthread_mutex_lock(&hash_mutex); pthread_mutex_lock(&hash_mutex);
if(ht->ihashtable){ if(ht->ihashtable){
myfree(ht->ihashtable); myfree(ht->ihashtable);
@ -82,7 +80,7 @@ int inithashtable(struct hashtable *ht, unsigned npoolsize){
} }
ht->poolsize = poolsize; ht->poolsize = poolsize;
ht->tablesize = tablesize; ht->tablesize = tablesize;
ht->growlimit = npoolsize; ht->growlimit = growlimit;
memset(ht->ihashtable, 0, ht->tablesize * sizeof(uint32_t)); memset(ht->ihashtable, 0, ht->tablesize * sizeof(uint32_t));
memset(ht->hashvalues, 0, ht->poolsize * (sizeof(struct hashentry) + ht->recsize - 4)); memset(ht->hashvalues, 0, ht->poolsize * (sizeof(struct hashentry) + ht->recsize - 4));
@ -153,7 +151,7 @@ void hashadd(struct hashtable *ht, const void* name, const void* value, time_t e
return; return;
} }
ht->index2hash(name, hash); ht->index2hash(ht, name, hash);
pthread_mutex_lock(&hash_mutex); pthread_mutex_lock(&hash_mutex);
index = hashindex(ht, hash); index = hashindex(ht, hash);
@ -201,7 +199,7 @@ int hashresolv(struct hashtable *ht, const void* name, void* value, uint32_t *tt
if(!ht || !ht->ihashtable || !name) { if(!ht || !ht->ihashtable || !name) {
return 0; return 0;
} }
ht->index2hash(name, hash); ht->index2hash(ht,name, hash);
pthread_mutex_lock(&hash_mutex); pthread_mutex_lock(&hash_mutex);
index = hashindex(ht, hash); index = hashindex(ht, hash);
for(hep = ht->ihashtable + index; (he = *hep)!=0; ){ for(hep = ht->ihashtable + index; (he = *hep)!=0; ){
@ -223,13 +221,13 @@ int hashresolv(struct hashtable *ht, const void* name, void* value, uint32_t *tt
return 0; return 0;
} }
void char_index2hash(const void *index, uint8_t *hash){ void char_index2hash(const struct hashtable *ht, const void *index, uint8_t *hash){
const char* name = index; const char* name = index;
blake2b(hash, HASH_SIZE, index, strlen((const char*)index), NULL, 0); blake2b(hash, ht->hash_size, index, strlen((const char*)index), NULL, 0);
} }
void param2hash(const void *index, uint8_t *hash){ void param2hash(const struct hashtable *ht, const void *index, uint8_t *hash){
blake2b_state S; blake2b_state S;
const struct clientparam *param = (struct clientparam *)index; const struct clientparam *param = (struct clientparam *)index;
@ -238,9 +236,9 @@ void param2hash(const void *index, uint8_t *hash){
if((conf.authcachetype & 4) && param->password)blake2b_update(&S, param->password, strlen((const char *)param->password) + 1); if((conf.authcachetype & 4) && param->password)blake2b_update(&S, param->password, strlen((const char *)param->password) + 1);
if((conf.authcachetype & 1) && !(conf.authcachetype & 8))blake2b_update(&S, SAADDR(&param->sincr), SAADDRLEN(&param->sincr)); if((conf.authcachetype & 1) && !(conf.authcachetype & 8))blake2b_update(&S, SAADDR(&param->sincr), SAADDRLEN(&param->sincr));
if((conf.authcachetype & 16))blake2b_update(&S, &param->srv->acl, sizeof(param->srv->acl)); if((conf.authcachetype & 16))blake2b_update(&S, &param->srv->acl, sizeof(param->srv->acl));
blake2b_final(&S, hash, HASH_SIZE); blake2b_final(&S, hash, ht->hash_size);
} }
struct hashtable dns_table = {0, 0, 0, 4, HASH_SIZE, char_index2hash}; struct hashtable dns_table = {char_index2hash, 4, HASH_SIZE};
struct hashtable dns6_table = {0, 0, 0, 16, HASH_SIZE, char_index2hash}; struct hashtable dns6_table = {char_index2hash, 16, HASH_SIZE};
struct hashtable auth_table = {0, 0, 0, sizeof(struct authcache), HASH_SIZE, param2hash}; struct hashtable auth_table = {param2hash, sizeof(struct authcache), HASH_SIZE};

View File

@ -244,7 +244,7 @@ void mschap(const unsigned char *win_password,
const unsigned char *challenge, unsigned char *response); const unsigned char *challenge, unsigned char *response);
void destroyhashtable(struct hashtable *ht); void destroyhashtable(struct hashtable *ht);
int inithashtable(struct hashtable *ht, unsigned nhashsize); int inithashtable(struct hashtable *ht, unsigned tablesize, unsigned poolsize, unsigned growlimit);
void hashadd(struct hashtable *ht, const void* name, const void* value, time_t expires); void hashadd(struct hashtable *ht, const void* name, const void* value, time_t expires);
int hashresolv(struct hashtable *ht, const void* name, void* value, uint32_t *ttl); int hashresolv(struct hashtable *ht, const void* name, void* value, uint32_t *ttl);

View File

@ -757,12 +757,12 @@ struct child {
#define HASH_SIZE (16) #define HASH_SIZE (16)
struct hashtable { struct hashtable {
void (*index2hash)(const struct hashtable *ht, const void *index, uint8_t *hash);
unsigned recsize;
unsigned hash_size;
unsigned poolsize; unsigned poolsize;
unsigned tablesize; unsigned tablesize;
unsigned growlimit; unsigned growlimit;
unsigned recsize;
unsigned hash_size;
void (*index2hash)(const void *index, unsigned char *hash);
uint32_t * ihashtable; uint32_t * ihashtable;
uint8_t * hashvalues; uint8_t * hashvalues;
uint8_t * hashhashvalues; uint8_t * hashhashvalues;