Replaced nDPI's internal hashmap with uthash. (#1602)
Signed-off-by: lns <matzeton@googlemail.com>
This commit is contained in:
+11
-7
@@ -4553,20 +4553,24 @@ void rsiUnitTest() {
|
|||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|
||||||
void hashUnitTest() {
|
void hashUnitTest() {
|
||||||
ndpi_str_hash *h = ndpi_hash_alloc(16384);
|
ndpi_str_hash *h;
|
||||||
char* dict[] = { "hello", "world", NULL };
|
char * const dict[] = { "hello", "world", NULL };
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
assert(h);
|
assert(ndpi_hash_init(&h) == 0);
|
||||||
|
assert(h == NULL);
|
||||||
|
|
||||||
for(i=0; dict[i] != NULL; i++) {
|
for(i=0; dict[i] != NULL; i++) {
|
||||||
u_int8_t l = strlen(dict[i]), v;
|
u_int8_t l = strlen(dict[i]);
|
||||||
|
int * v;
|
||||||
|
|
||||||
assert(ndpi_hash_add_entry(h, dict[i], l, i) == 0);
|
assert(ndpi_hash_add_entry(&h, dict[i], l, &i) == 0);
|
||||||
assert(ndpi_hash_find_entry(h, dict[i], l, &v) == 0);
|
assert(ndpi_hash_find_entry(h, dict[i], l, (void **)&v) == 0);
|
||||||
|
assert(v == (void *)&i && *v == i);
|
||||||
}
|
}
|
||||||
|
|
||||||
ndpi_hash_free(h);
|
ndpi_hash_free(&h, NULL);
|
||||||
|
assert(h == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|||||||
@@ -1693,10 +1693,52 @@ extern "C" {
|
|||||||
|
|
||||||
/* ******************************* */
|
/* ******************************* */
|
||||||
|
|
||||||
ndpi_str_hash* ndpi_hash_alloc(u_int32_t max_num_entries);
|
/**
|
||||||
void ndpi_hash_free(ndpi_str_hash *h);
|
* Initialize the hashmap.
|
||||||
int ndpi_hash_find_entry(ndpi_str_hash *h, char *key, u_int key_len, u_int8_t *value);
|
*
|
||||||
int ndpi_hash_add_entry(ndpi_str_hash *h, char *key, u_int8_t key_len, u_int8_t value);
|
* @par h = pointer to the hash map [in, out]
|
||||||
|
*
|
||||||
|
* @return 0 on success, 1 otherwise
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int ndpi_hash_init(ndpi_str_hash **h);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free the hashmap.
|
||||||
|
*
|
||||||
|
* @par h = pointer to the hash map [in, out]
|
||||||
|
* @par cleanup_func = pointer to a optional callback function
|
||||||
|
* called for each element in the hashmap [in]
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void ndpi_hash_free(ndpi_str_hash **h, void (*cleanup_func)(ndpi_str_hash *h));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for an entry in the hashmap.
|
||||||
|
*
|
||||||
|
* @par h = pointer to the hash map [in]
|
||||||
|
* @par key = character string (no '\0' required) [in]
|
||||||
|
* @par key_len = length of the character string @key [in]
|
||||||
|
* @par value = pointer to a pointer to the value, which contains a
|
||||||
|
* previously added hash entry [in, out]
|
||||||
|
*
|
||||||
|
* @return 0 if an entry with that key was found, 1 otherwise
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int ndpi_hash_find_entry(ndpi_str_hash *h, char *key, u_int key_len, void **value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an entry to the hashmap.
|
||||||
|
*
|
||||||
|
* @par h = pointer to the hash map [in, out]
|
||||||
|
* @par key = character string (no '\0' required) [in]
|
||||||
|
* @par key_len = length of the character string @key [in]
|
||||||
|
* @par value = pointer to the value to add [in]
|
||||||
|
*
|
||||||
|
* @return 0 if the entry was added, 1 otherwise
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int ndpi_hash_add_entry(ndpi_str_hash **h, char *key, u_int8_t key_len, void *value);
|
||||||
|
|
||||||
/* ******************************* */
|
/* ******************************* */
|
||||||
|
|
||||||
|
|||||||
@@ -1608,19 +1608,12 @@ struct ndpi_bin {
|
|||||||
|
|
||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|
||||||
struct ndpi_str_hash_info {
|
typedef struct ndpi_str_hash {
|
||||||
char *key; /* Key */
|
unsigned int hash;
|
||||||
u_int8_t key_len;
|
void *value;
|
||||||
u_int8_t value; /* Value */
|
u_int8_t private_data[0];
|
||||||
struct ndpi_str_hash_info *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
u_int32_t num_buckets, max_num_entries;
|
|
||||||
struct ndpi_str_hash_info **buckets;
|
|
||||||
} ndpi_str_hash;
|
} ndpi_str_hash;
|
||||||
|
|
||||||
|
|
||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|
||||||
#define HW_HISTORY_LEN 4
|
#define HW_HISTORY_LEN 4
|
||||||
|
|||||||
+66
-78
@@ -51,6 +51,7 @@
|
|||||||
#include "third_party/include/libinjection.h"
|
#include "third_party/include/libinjection.h"
|
||||||
#include "third_party/include/libinjection_sqli.h"
|
#include "third_party/include/libinjection_sqli.h"
|
||||||
#include "third_party/include/libinjection_xss.h"
|
#include "third_party/include/libinjection_xss.h"
|
||||||
|
#include "third_party/include/uthash.h"
|
||||||
#include "third_party/include/rce_injection.h"
|
#include "third_party/include/rce_injection.h"
|
||||||
|
|
||||||
#define NDPI_CONST_GENERIC_PROTOCOL_NAME "GenericProtocol"
|
#define NDPI_CONST_GENERIC_PROTOCOL_NAME "GenericProtocol"
|
||||||
@@ -68,6 +69,20 @@ struct pcre_struct {
|
|||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Please keep this strcture in sync with
|
||||||
|
* `struct ndpi_str_hash` in src/include/ndpi_typedefs.h
|
||||||
|
*/
|
||||||
|
typedef struct ndpi_str_hash_private {
|
||||||
|
unsigned int hash;
|
||||||
|
void *value;
|
||||||
|
UT_hash_handle hh;
|
||||||
|
} ndpi_str_hash_private;
|
||||||
|
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||||
|
_Static_assert(sizeof(struct ndpi_str_hash) == sizeof(struct ndpi_str_hash_private) - sizeof(UT_hash_handle),
|
||||||
|
"Please keep `struct ndpi_str_hash` and `struct ndpi_str_hash_private` syncd.");
|
||||||
|
#endif
|
||||||
|
|
||||||
/* ****************************************** */
|
/* ****************************************** */
|
||||||
|
|
||||||
/* implementation of the punycode check function */
|
/* implementation of the punycode check function */
|
||||||
@@ -2063,106 +2078,79 @@ u_int32_t ndpi_quick_16_byte_hash(u_int8_t *in_16_bytes_long) {
|
|||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
ndpi_str_hash* ndpi_hash_alloc(u_int32_t max_num_entries) {
|
int ndpi_hash_init(ndpi_str_hash **h)
|
||||||
ndpi_str_hash *h = (ndpi_str_hash*)ndpi_malloc(sizeof(ndpi_str_hash));
|
{
|
||||||
|
if (h == NULL)
|
||||||
if(!h) return(NULL);
|
{
|
||||||
if(max_num_entries < 1024) max_num_entries = 1024;
|
return 1;
|
||||||
if(max_num_entries > 10000000) max_num_entries = 10000000;
|
|
||||||
|
|
||||||
h->max_num_entries = max_num_entries, h->num_buckets = max_num_entries/2;
|
|
||||||
h->buckets = (struct ndpi_str_hash_info**)ndpi_calloc(sizeof(struct ndpi_str_hash_info*), h->num_buckets);
|
|
||||||
|
|
||||||
if(h->buckets == NULL) {
|
|
||||||
ndpi_free(h);
|
|
||||||
return(NULL);
|
|
||||||
} else
|
|
||||||
return(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ******************************************************************** */
|
|
||||||
|
|
||||||
void ndpi_hash_free(ndpi_str_hash *h) {
|
|
||||||
u_int32_t i;
|
|
||||||
|
|
||||||
for(i=0; i<h->num_buckets; i++) {
|
|
||||||
struct ndpi_str_hash_info *head = h->buckets[i];
|
|
||||||
|
|
||||||
while(head != NULL) {
|
|
||||||
struct ndpi_str_hash_info *next = head->next;
|
|
||||||
|
|
||||||
ndpi_free(head->key);
|
|
||||||
ndpi_free(head);
|
|
||||||
head = next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ndpi_free(h->buckets);
|
*h = NULL;
|
||||||
ndpi_free(h);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
static u_int32_t _ndpi_hash_function(ndpi_str_hash *h, char *key, u_int8_t key_len) {
|
void ndpi_hash_free(ndpi_str_hash **h, void (*cleanup_func)(ndpi_str_hash *h))
|
||||||
u_int32_t hv = 0;
|
{
|
||||||
u_int8_t i;
|
struct ndpi_str_hash_private *h_priv;
|
||||||
|
struct ndpi_str_hash_private *current, *tmp;
|
||||||
|
|
||||||
for(i=0; i<key_len; i++)
|
if (h == NULL)
|
||||||
hv += key[i]*(i+1);
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
h_priv = *(struct ndpi_str_hash_private **)h;
|
||||||
|
|
||||||
return(hv % h->num_buckets);
|
HASH_ITER(hh, h_priv, current, tmp) {
|
||||||
}
|
HASH_DEL(h_priv, current);
|
||||||
|
if (cleanup_func != NULL)
|
||||||
/* ******************************************************************** */
|
{
|
||||||
|
cleanup_func((ndpi_str_hash *)current);
|
||||||
static int _ndpi_hash_find_entry(ndpi_str_hash *h, u_int32_t hashval, char *key, u_int key_len, u_int8_t *value) {
|
|
||||||
struct ndpi_str_hash_info *head = h->buckets[hashval];
|
|
||||||
|
|
||||||
while(head != NULL) {
|
|
||||||
if((head->key_len == key_len) && (memcmp(head->key, key, key_len) == 0)) {
|
|
||||||
*value = head->value;
|
|
||||||
return(0); /* Found */
|
|
||||||
}
|
}
|
||||||
|
free(current);
|
||||||
head = head-> next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return(-1); /* Not found */
|
*h = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
int ndpi_hash_find_entry(ndpi_str_hash *h, char *key, u_int key_len, u_int8_t *value) {
|
int ndpi_hash_find_entry(ndpi_str_hash *h, char *key, u_int key_len, void **value)
|
||||||
u_int32_t hv = _ndpi_hash_function(h, key, key_len);
|
{
|
||||||
|
struct ndpi_str_hash_private *h_priv = (struct ndpi_str_hash_private *)h;
|
||||||
|
struct ndpi_str_hash_private *found;
|
||||||
|
unsigned int hash_value;
|
||||||
|
|
||||||
return(_ndpi_hash_find_entry(h, hv, key, key_len, value));
|
HASH_VALUE(key, key_len, hash_value);
|
||||||
|
HASH_FIND_INT(h_priv, &hash_value, found);
|
||||||
|
if (found != NULL)
|
||||||
|
{
|
||||||
|
*value = found->value;
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
int ndpi_hash_add_entry(ndpi_str_hash *h, char *key, u_int8_t key_len, u_int8_t value) {
|
int ndpi_hash_add_entry(ndpi_str_hash **h, char *key, u_int8_t key_len, void *value)
|
||||||
u_int32_t hv = _ndpi_hash_function(h, key, key_len);
|
{
|
||||||
u_int8_t ret_value;
|
struct ndpi_str_hash_private **h_priv = (struct ndpi_str_hash_private **)h;
|
||||||
int rc = _ndpi_hash_find_entry(h, hv, key, key_len, &ret_value);
|
struct ndpi_str_hash_private *new = ndpi_calloc(1, sizeof(*new));
|
||||||
|
unsigned int hash_value;
|
||||||
|
|
||||||
if(rc == -1) {
|
if (new == NULL)
|
||||||
/* Not found */
|
{
|
||||||
struct ndpi_str_hash_info *e = (struct ndpi_str_hash_info*)ndpi_malloc(sizeof(struct ndpi_str_hash_info));
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if(e == NULL)
|
HASH_VALUE(key, key_len, hash_value);
|
||||||
return(-2);
|
new->hash = hash_value;
|
||||||
|
new->value = value;
|
||||||
if((e->key = (char*)ndpi_malloc(key_len)) == NULL)
|
HASH_ADD_INT(*h_priv, hash, new);
|
||||||
return(-3);
|
return 0;
|
||||||
|
|
||||||
memcpy(e->key, key, key_len);
|
|
||||||
e->key_len = key_len, e->value = value;
|
|
||||||
e->next = h->buckets[hv];
|
|
||||||
h->buckets[hv] = e;
|
|
||||||
|
|
||||||
return(0);
|
|
||||||
} else
|
|
||||||
return(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|||||||
Vendored
+493
-451
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user