diff options
author | Luca <deri@ntop.org> | 2024-01-15 19:03:46 +0100 |
---|---|---|
committer | Luca <deri@ntop.org> | 2024-01-15 19:03:46 +0100 |
commit | 162c38f18f81a4e069db5f957c13f68c2cdc9b89 (patch) | |
tree | 33555b3e246c83832c0e3a99fda75912ba686461 /src/lib/ndpi_domains.c | |
parent | 61a18623e71626b4c9197f171ca219fae6f5792b (diff) |
Added new API calls
- ndpi_load_domain_suffixes()
- ndpi_get_host_domain_suffix()
whose goal is to find the domain name of a hostname. Example:
www.bbc.co.uk -> co.uk
mail.apple.com -> com
Diffstat (limited to 'src/lib/ndpi_domains.c')
-rw-r--r-- | src/lib/ndpi_domains.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/lib/ndpi_domains.c b/src/lib/ndpi_domains.c new file mode 100644 index 000000000..12f735b36 --- /dev/null +++ b/src/lib/ndpi_domains.c @@ -0,0 +1,93 @@ +/* + * ndpi_domains.c + * + * Copyright (C) 2011-24 - ntop.org and contributors + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "ndpi_config.h" +#include "ndpi_api.h" +#include "ndpi_includes.h" +#include "ndpi_private.h" + +/* ******************************* */ + +int ndpi_load_domain_suffixes(struct ndpi_detection_module_struct *ndpi_str, + char *public_suffix_list_path) { + char buf[256], *line; + FILE *fd; + bool do_trace = false; + u_int num_domains = 0; + + if(public_suffix_list_path == NULL) + return(-1); + + if((fd = fopen(public_suffix_list_path, "r")) == NULL) + return(-2); + + if(ndpi_str->public_domain_suffixes != NULL) { + /* An existing license was aleady loaded: free it and start over */ + ndpi_domain_classify_free(ndpi_str->public_domain_suffixes); + } + + if((ndpi_str->public_domain_suffixes = ndpi_domain_classify_alloc()) == NULL) + return(-3); + + while((line = fgets(buf, sizeof(buf), fd)) != NULL) { + u_int offset, len; + + /* Skip empty lines or comments */ + if((line[0] == '\0') || (line[0] == '/') || (line[0] == '\n') || (line[0] == '\r')) + continue; + + if((line[0] == '*') && (line[1] == '.') && (line[2] != '\0')) + offset = 2; + else + offset = 0; + + len = strlen(line) - 1; + while((len > 0) && (line[len] == '\n')) + line[len--] = '\0'; + + if(!ndpi_domain_classify_add(ndpi_str->public_domain_suffixes, + 1 /* dummy */, &line[offset])) { + if(do_trace) printf("Error while processing domain %s\n", &line[offset]); + } else + num_domains++; + } + + if(!ndpi_domain_classify_finalize(ndpi_str->public_domain_suffixes)) { + if(do_trace) printf("Error while finalizing domain processing\n"); + } + + if(do_trace) printf("Loaded %u domains\n", num_domains); + + return(0); +} + +/* ******************************* */ + +const char* ndpi_get_host_domain_suffix(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)); + } +} |