Added ndpi_get_host_domain() for returning the host domain

vs ndpi_get_host_domain_prefix() that instead returnd the host TLD
This commit is contained in:
Luca
2024-01-16 06:56:51 +01:00
parent 111015b872
commit 1637a991a4
4 changed files with 45 additions and 8 deletions
+5
View File
@@ -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, "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, "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_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); ndpi_exit_detection_module(ndpi_info_mod);
+18 -2
View File
@@ -2149,7 +2149,8 @@ extern "C" {
bool ndpi_domain_classify_finalize(ndpi_domain_classify *s); bool ndpi_domain_classify_finalize(ndpi_domain_classify *s);
const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s,
u_int8_t *class_id /* out */, u_int8_t *class_id /* out */,
const char *hostnname); const char *hostnname,
bool return_subprefix);
bool ndpi_domain_classify_contains(ndpi_domain_classify *s, bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
u_int8_t *class_id /* out */, u_int8_t *class_id /* out */,
const char *domain); const char *domain);
@@ -2222,12 +2223,27 @@ extern "C" {
* @par ndpi_str = the struct created for the protocol detection * @par ndpi_str = the struct created for the protocol detection
* @par hostname = the hostname from which the domain name has to be extracted * @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* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndpi_str,
const char *hostname); 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 */ /* Can't call libc functions from kernel space, define some stub instead */
+7 -5
View File
@@ -217,9 +217,10 @@ static bool is_valid_domain_char(u_char c) {
const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s, const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s,
u_int8_t *class_id /* out */, u_int8_t *class_id /* out */,
const char *hostname) { const char *hostname,
bool return_subprefix) {
u_int32_t i, len; u_int32_t i, len;
const char *dot, *elem; const char *dot, *elem, *prev_elem;
*class_id = 0; /* Unknown class_id */ *class_id = 0; /* Unknown class_id */
@@ -245,7 +246,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s,
return(hostname); return(hostname);
} }
elem = hostname; elem = prev_elem = hostname;
while(elem != NULL) { while(elem != NULL) {
u_int64_t hash = ndpi_quick_hash64(elem, strlen(elem)); 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); printf("[contains] %s = %d\n", hostname, s->classes[i].class_id);
#endif #endif
*class_id = s->classes[i].class_id; *class_id = s->classes[i].class_id;
return(elem); return(return_subprefix ? prev_elem : elem);
} }
} else } else
break; break;
@@ -268,6 +269,7 @@ const char* ndpi_domain_classify_longest_prefix(ndpi_domain_classify *s,
if(elem == NULL) break; if(elem == NULL) break;
// if(elem == dot) break; // if(elem == dot) break;
prev_elem = elem;
elem = &elem[1]; elem = &elem[1];
} /* while */ } /* 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, bool ndpi_domain_classify_contains(ndpi_domain_classify *s,
u_int8_t *class_id /* out */, u_int8_t *class_id /* out */,
const char *domain) { 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); return((*class_id == 0) ? false : true);
} }
+15 -1
View File
@@ -88,6 +88,20 @@ const char* ndpi_get_host_domain_suffix(struct ndpi_detection_module_struct *ndp
u_int8_t class_id; u_int8_t class_id;
return(ndpi_domain_classify_longest_prefix(ndpi_str->public_domain_suffixes, 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));
} }
} }