From 191694f797639fc0b56adcf050bc9cfa8dc02f3d Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Thu, 19 Sep 2024 13:18:26 +0200 Subject: [PATCH] Implemented ndpi_strrstr() Fixed bug in ndpi_get_host_domain --- example/ndpiReader.c | 3 +++ src/include/ndpi_api.h | 14 +++++++------- src/lib/ndpi_domains.c | 4 ++-- src/lib/ndpi_utils.c | 26 ++++++++++++++++++++++++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 8a59b77e3e5..0d3a427911d 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -6258,6 +6258,9 @@ void domainsUnitTest() { assert(ndpi_load_domain_suffixes(ndpi_str, (char*)lists_path) == 0); + assert(strcmp(ndpi_get_host_domain(ndpi_str, "extension.femetrics.grammarly.io"), "grammarly.io") == 0); + assert(strcmp(ndpi_get_host_domain(ndpi_str, "www.ovh.commander1.com"), "commander1.com") == 0); + assert(strcmp(ndpi_get_host_domain_suffix(ndpi_str, "www.chosei.chiba.jp", &suffix_id), "chosei.chiba.jp") == 0); assert(strcmp(ndpi_get_host_domain_suffix(ndpi_str, "www.unipi.it", &suffix_id), "it") == 0); assert(strcmp(ndpi_get_host_domain_suffix(ndpi_str, "mail.apple.com", &suffix_id), "com") == 0); diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 1bc629b8392..6e0e96e2fa0 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -1907,17 +1907,17 @@ extern "C" { - https://varshasaini.in/k-nearest-neighbor-knn-algorithm-in-machine-learning/ NOTE: - with ball tree, data is a vector of vector pointers (no array) - */ + with ball tree, data is a vector of vector pointers (no array) + */ ndpi_btree* ndpi_btree_init(double **data, u_int32_t n_rows, u_int32_t n_columns); ndpi_knn ndpi_btree_query(ndpi_btree *b, double **query_data, u_int32_t query_data_num_rows, u_int32_t query_data_num_columns, u_int32_t max_num_results); void ndpi_free_knn(ndpi_knn knn); void ndpi_free_btree(ndpi_btree *tree); - + /* ******************************* */ - + /* * Finds outliers using Z-score * Z-Score = (Value - Mean) / StdDev @@ -2288,8 +2288,8 @@ extern "C" { int64_t ndpi_strtonum(const char *numstr, int64_t minval, int64_t maxval, const char **errstrp, int base); int ndpi_vsnprintf(char * str, size_t size, char const * format, va_list va_args); int ndpi_snprintf(char * str, size_t size, char const * format, ...); - struct tm *ndpi_gmtime_r(const time_t *timep, - struct tm *result); + struct tm *ndpi_gmtime_r(const time_t *timep, struct tm *result); + char* ndpi_strrstr(const char *haystack, const char *needle); /* ******************************* */ @@ -2309,7 +2309,7 @@ extern "C" { bool ndpi_serialize_flow_fingerprint(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow, ndpi_serializer *serializer); - + /* ******************************* */ const char *ndpi_lru_cache_idx_to_name(lru_cache_type idx); diff --git a/src/lib/ndpi_domains.c b/src/lib/ndpi_domains.c index f4398b1c84a..00ef8e75605 100644 --- a/src/lib/ndpi_domains.c +++ b/src/lib/ndpi_domains.c @@ -122,7 +122,7 @@ const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndp return(hostname); } -/* ******************************* */ + /* Example @@ -146,7 +146,7 @@ const char* ndpi_get_host_domain(struct ndpi_detection_module_struct *ndpi_str, if((ret == NULL) || (ret == hostname)) return(hostname); - dot = strstr(hostname, ret); + dot = ndpi_strrstr(hostname, ret); if(dot == NULL || dot == hostname) return(hostname); diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index 6a2e237a367..a1b0a5e6e25 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -3424,8 +3424,8 @@ int tpkt_verify_hdr(const struct ndpi_packet_struct * const packet) /* ******************************************* */ -int64_t ndpi_strtonum(const char *numstr, int64_t minval, int64_t maxval, const char **errstrp, int base) -{ +int64_t ndpi_strtonum(const char *numstr, int64_t minval, + int64_t maxval, const char **errstrp, int base) { int64_t val = 0; char* endptr; @@ -3441,14 +3441,17 @@ int64_t ndpi_strtonum(const char *numstr, int64_t minval, int64_t maxval, const *errstrp = "value too small"; return 0; } + if((val == LLONG_MAX && errno == ERANGE) || (val > maxval )) { *errstrp = "value too large"; return 0; } + if(errno != 0 && val == 0) { *errstrp = "generic error"; return 0; } + if(endptr == numstr) { *errstrp = "No digits were found"; return 0; @@ -3459,6 +3462,25 @@ int64_t ndpi_strtonum(const char *numstr, int64_t minval, int64_t maxval, const return val; } +/* ****************************************************** */ + +char* ndpi_strrstr(const char *haystack, const char *needle) { + char *ret = NULL; + + while(true) { + char *s = strstr(haystack, needle); + + if(s == NULL) + break; + else { + ret = s; + haystack = &s[1]; /* Skip the first char */ + } + } + + return(ret); +} + /* ******************************************* */ const char *ndpi_lru_cache_idx_to_name(lru_cache_type idx)