Skip to content

Commit

Permalink
Search fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaderi committed Aug 26, 2023
1 parent eeeee46 commit 34986de
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
18 changes: 13 additions & 5 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5421,20 +5421,28 @@ void sketchUnitTest() {

void domainSearchUnitTest() {
ndpi_domain_classify *sc = ndpi_domain_classify_alloc();
const char *fname = NDPI_BASE_DIR "/lists/gambling.list";
char *domain = "ntop.org";
u_int32_t num_domains;

assert(sc);

ndpi_domain_classify_add(sc, NDPI_PROTOCOL_NTOP, ".ntop.org");
ndpi_domain_classify_add(sc, NDPI_PROTOCOL_NTOP, domain);
assert(ndpi_domain_classify_contains(sc, domain));

num_domains = ndpi_domain_classify_add_domains(sc, NDPI_PROTOCOL_GAMBLING, (char*)fname);
assert(num_domains == 35370);
#if 0
{
const char *fname = NDPI_BASE_DIR "/lists/gambling.list";
u_int32_t num_domains;

num_domains = ndpi_domain_classify_add_domains(sc, NDPI_PROTOCOL_GAMBLING, (char*)fname);
assert(num_domains == 35370);

assert(ndpi_domain_classify_contains(sc, "0grand-casino.com") == NDPI_PROTOCOL_GAMBLING);
}
#endif

/* Subdomain check */
assert(ndpi_domain_classify_contains(sc, "blog.ntop.org") == NDPI_PROTOCOL_NTOP);
assert(ndpi_domain_classify_contains(sc, "0grand-casino.com") == NDPI_PROTOCOL_GAMBLING);

#ifdef DEBUG_TRACE
struct stat st;
Expand Down
2 changes: 1 addition & 1 deletion src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,7 @@ typedef struct {
} ndpi_string_search;


#define MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS 6
#define MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS 16

/* **************************************** */

Expand Down
58 changes: 43 additions & 15 deletions src/lib/ndpi_domain_classify.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ typedef struct {
ndpi_domain_classify_t *class[MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS];
} ndpi_domain_classifications_t;

//#define DEBUG

/* ********************************************************** */

static void ndpi_domain_search_free(ndpi_domain_search *search) {
Expand Down Expand Up @@ -126,9 +128,15 @@ static inline u_int32_t ndpi_hash_string(char *domain) {
/* NOTE: domain will be modified: copy it if necessary */
static bool ndpi_domain_search_add(ndpi_domain_search *search, char *domain) {
char *elem;
u_int32_t bitmap_id = 0;
u_int32_t bitmap_id = 0, len;
bool quit = false;


if(domain == NULL) return(false);
if((len = strlen(domain)) == 0) return(false);
if(domain[len-1] == '.') domain[len-1] = '0';

if(domain[0] == '.') ++domain;

elem = strrchr(domain, '.');
while(elem) {
u_int32_t h;
Expand Down Expand Up @@ -256,7 +264,7 @@ bool ndpi_domain_classify_add(ndpi_domain_classify *_s,
u_int32_t i;
ndpi_domain_classifications_t *s = (ndpi_domain_classifications_t*)_s;
char buf[256];

for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->class[i] != NULL) {
if(s->class[i]->class_id == class_id) {
Expand All @@ -278,7 +286,11 @@ bool ndpi_domain_classify_add(ndpi_domain_classify *_s,
return(false);

snprintf(buf, sizeof(buf), "%s", domain);


#ifdef DEBUG
printf("[add] %s @ %u\n", domain, class_id);
#endif

return(ndpi_domain_search_add(s->class[i]->domains, buf));
}

Expand All @@ -292,7 +304,7 @@ u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *_s,
char buf[256];
FILE *fd;
char *line;

for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->class[i] != NULL) {
if(s->class[i]->class_id == class_id) {
Expand All @@ -314,11 +326,11 @@ u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *_s,
return(false);

/* *************************************** */

fd = fopen(file_path, "r");
if(fd == NULL)
return(false);

while((line = fgets(buf, sizeof(buf), fd)) != NULL) {
u_int len;

Expand All @@ -335,8 +347,8 @@ u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *_s,

if(ndpi_domain_search_add(s->class[i]->domains, line))
num_added++;
}
}

fclose(fd);

return(num_added);
Expand All @@ -346,18 +358,34 @@ u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *_s,

u_int16_t ndpi_domain_classify_contains(ndpi_domain_classify *_s,
char *domain) {
u_int32_t i;
u_int32_t i, len;
ndpi_domain_classifications_t *s = (ndpi_domain_classifications_t*)_s;
char buf[256];

snprintf(buf, sizeof(buf), "%s", domain);


if(!domain) return(0);
if((len = strlen(domain)) == 0) return(0);

/* This is a number or a numeric IP or similar */
if(isdigit(domain[len-1]) && isdigit(domain[0]))
return(0);

for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->class[i] != NULL) {
if(ndpi_domain_search_contains(s->class[i]->domains, buf))
char buf[256];

snprintf(buf, sizeof(buf), "%s", domain);

if(ndpi_domain_search_contains(s->class[i]->domains, buf)) {
#ifdef DEBUG
printf("[search] %s = %d\n", domain, s->class[i]->class_id);
#endif
return(s->class[i]->class_id);
}
}
}

#ifdef DEBUG
printf("[search] %s NOT FOUND\n", domain);
#endif

return(0);
}

0 comments on commit 34986de

Please sign in to comment.