diff options
-rw-r--r-- | example/ndpiReader.c | 5 | ||||
-rw-r--r-- | src/include/ndpi_api.h | 20 | ||||
-rw-r--r-- | src/lib/ndpi_domain_classify.c | 12 | ||||
-rw-r--r-- | src/lib/ndpi_domains.c | 16 |
4 files changed, 45 insertions, 8 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 238cffdab..bea676427 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -5649,6 +5649,11 @@ void domainsUnitTest() { assert(strcmp(ndpi_get_host_domain_suffix(ndpi_info_mod, "www.unipi.it"), "it") == 0); assert(strcmp(ndpi_get_host_domain_suffix(ndpi_info_mod, "mail.apple.com"), "com") == 0); assert(strcmp(ndpi_get_host_domain_suffix(ndpi_info_mod, "www.bbc.co.uk"), "co.uk") == 0); + + assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.chosei.chiba.jp"), "chosei.chiba.jp") == 0); + assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.unipi.it"), "unipi.it") == 0); + assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "mail.apple.com"), "apple.com") == 0); + assert(strcmp(ndpi_get_host_domain(ndpi_info_mod, "www.bbc.co.uk"), "bbc.co.uk") == 0); } ndpi_exit_detection_module(ndpi_info_mod); diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index f4c2f6114..8bec8e3eb 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -2149,7 +2149,8 @@ extern "C" { bool ndpi_domain_classify_finalize(ndpi_domain_classify *s); const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, u_int8_t *class_id /* out */, - const char *hostnname); + const char *hostnname, + bool return_subprefix); bool ndpi_domain_classify_contains(ndpi_domain_classify *s, u_int8_t *class_id /* out */, const char *domain); @@ -2222,12 +2223,27 @@ extern "C" { * @par ndpi_str = the struct created for the protocol detection * @par hostname = the hostname from which the domain name has to be extracted * - * @return The host domain name or the hostitself if not found. + * @return The host domain name suffic or the host itself if not found. * */ const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndpi_str, const char *hostname); + /** + * Returns the domain (including the TLS) suffix out of the specified hostname. + * The returned pointer is an offset of the original hostname. + * Note that you need to call ndpi_load_domain_suffixes() before + * calling this function. + * + * @par ndpi_str = the struct created for the protocol detection + * @par hostname = the hostname from which the domain name has to be extracted + * + * @return The host domain name or the hosti tself if not found. + * + */ + const char* ndpi_get_host_domain(struct ndpi_detection_module_struct *ndpi_str, + const char *hostname); + /* ******************************* */ /* Can't call libc functions from kernel space, define some stub instead */ diff --git a/src/lib/ndpi_domain_classify.c b/src/lib/ndpi_domain_classify.c index edec6afcd..00c491b4a 100644 --- a/src/lib/ndpi_domain_classify.c +++ b/src/lib/ndpi_domain_classify.c @@ -217,9 +217,10 @@ static bool is_valid_domain_char(u_char c) { const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, u_int8_t *class_id /* out */, - const char *hostname) { + const char *hostname, + bool return_subprefix) { u_int32_t i, len; - const char *dot, *elem; + const char *dot, *elem, *prev_elem; *class_id = 0; /* Unknown class_id */ @@ -245,7 +246,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, return(hostname); } - elem = hostname; + elem = prev_elem = hostname; while(elem != NULL) { u_int64_t hash = ndpi_quick_hash64(elem, strlen(elem)); @@ -257,7 +258,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, printf("[contains] %s = %d\n", hostname, s->classes[i].class_id); #endif *class_id = s->classes[i].class_id; - return(elem); + return(return_subprefix ? prev_elem : elem); } } else break; @@ -268,6 +269,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, if(elem == NULL) break; // if(elem == dot) break; + prev_elem = elem; elem = &elem[1]; } /* while */ @@ -280,7 +282,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, bool ndpi_domain_classify_contains(ndpi_domain_classify *s, u_int8_t *class_id /* out */, const char *domain) { - (void)ndpi_domain_classify_longest_prefix(s, class_id, domain); /* UNUSED */ + (void)ndpi_domain_classify_longest_prefix(s, class_id, domain, false); /* UNUSED */ return((*class_id == 0) ? false : true); } diff --git a/src/lib/ndpi_domains.c b/src/lib/ndpi_domains.c index 7c321a474..aeb34b010 100644 --- a/src/lib/ndpi_domains.c +++ b/src/lib/ndpi_domains.c @@ -88,6 +88,20 @@ const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndp u_int8_t class_id; return(ndpi_domain_classify_longest_prefix(ndpi_str->public_domain_suffixes, - &class_id, hostname)); + &class_id, hostname, false)); + } +} + +/* ******************************* */ + +const char* ndpi_get_host_domain(struct ndpi_detection_module_struct *ndpi_str, + const char *hostname) { + if(ndpi_str->public_domain_suffixes == NULL) + return(hostname); + else { + u_int8_t class_id; + + return(ndpi_domain_classify_longest_prefix(ndpi_str->public_domain_suffixes, + &class_id, hostname, true)); } } |