Skip to content

Commit

Permalink
Implemented ndpi_strrstr()
Browse files Browse the repository at this point in the history
Fixed bug in ndpi_get_host_domain
  • Loading branch information
lucaderi committed Sep 19, 2024
1 parent 456bc2a commit 191694f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
3 changes: 3 additions & 0 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

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

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ndpi_domains.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndp
return(hostname);
}

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


/*
Example
Expand All @@ -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);
Expand Down
26 changes: 24 additions & 2 deletions src/lib/ndpi_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 191694f

Please sign in to comment.