aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/ndpi_main.c')
-rw-r--r--src/lib/ndpi_main.c161
1 files changed, 160 insertions, 1 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index 1adae4a3e..1370909eb 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -3450,7 +3450,9 @@ static const char *categories[NDPI_PROTOCOL_NUM_CATEGORIES] = {
"Hw/Sw",
"Dating",
"Travel",
- "Food"
+ "Food",
+ "Bots",
+ "Scanners"
};
#if !defined(NDPI_CFFI_PREPROCESSING) && defined(__linux__)
@@ -5247,6 +5249,99 @@ int load_category_file_fd(struct ndpi_detection_module_struct *ndpi_str,
/* ******************************************************************** */
+int load_protocol_id_file_fd(struct ndpi_detection_module_struct *ndpi_str,
+ FILE *fd, u_int16_t protocol_id) {
+ char buffer[256], *line;
+ u_int num_loaded = 0;
+ unsigned int failed_lines = 0;
+ unsigned int lines_read = 0;
+
+ (void)lines_read;
+
+ if(!ndpi_str || !fd || !ndpi_str->protocols)
+ return(0);
+
+ while(1) {
+ int len;
+
+ line = fgets(buffer, sizeof(buffer), fd);
+
+ if(line == NULL)
+ break;
+
+ lines_read++;
+ len = strlen(line);
+
+ if(len <= 1 || len == sizeof(buffer) - 1) {
+ NDPI_LOG_ERR(ndpi_str, "[NDPI] Failed to read file line #%u, line too short/long\n",
+ lines_read);
+ failed_lines++;
+ continue;
+ } else if (line[0] == '#')
+ continue;
+
+ int i = 0;
+
+ for (i = 0; i < len; ++i) {
+ if (line[i] == '\r' || line[i] == '\n') {
+ line[i] = '\0';
+ break;
+ }
+ }
+
+ if(strchr(line, ':') != NULL) {
+ if(ndpi_add_host_ip_subprotocol(ndpi_str, line, protocol_id, 1 /* IPv6 */) == 0)
+ num_loaded++;
+ else
+ failed_lines++;
+ } else if(strchr(line, '.') != NULL) {
+ /* IPv4 */
+ if(ndpi_add_host_ip_subprotocol(ndpi_str, line, protocol_id, 0 /* IPv4 */) == 0)
+ num_loaded++;
+ else
+ failed_lines++;
+ } else {
+ /* No clue */
+ failed_lines++;
+ continue;
+ }
+ }
+
+ if(failed_lines)
+ return(-1 * failed_lines);
+
+ return(num_loaded);
+}
+
+/* ******************************************************************** */
+
+/*
+ Loads a file (separated by <cr>) of IP addresses associated with the
+ specified protocol
+*/
+int ndpi_load_protocol_id_file(struct ndpi_detection_module_struct *ndpi_str,
+ char *path, u_int16_t protocol_id) {
+ int rc;
+ FILE *fd;
+
+ if(!ndpi_str || !path)
+ return(-1);
+
+ fd = fopen(path, "r");
+ if(fd == NULL) {
+ NDPI_LOG_ERR(ndpi_str, "Unable to open file %s [%s]\n", path, strerror(errno));
+ return -1;
+ }
+
+ rc = load_protocol_id_file_fd(ndpi_str, fd, protocol_id);
+
+ fclose(fd);
+
+ return rc;
+}
+
+/* ******************************************************************** */
+
/*
Load files (whose name is <categoryid>_<label>.<extension>) stored
in a directory and bind each domain to the specified category.
@@ -5311,6 +5406,70 @@ int ndpi_load_categories_dir(struct ndpi_detection_module_struct *ndpi_str,
/* ******************************************************************** */
+/*
+ Load files (whose name is <protocolid>_<label>.<extension>) stored
+ in a directory and bind each domain to the specified protocol.
+
+ It can be used to load all files store in the lists/ directory
+
+ It returns the number of loaded files or -1 in case of failure
+*/
+int ndpi_load_protocols_dir(struct ndpi_detection_module_struct *ndpi_str,
+ char *dir_path) {
+! DIR *dirp;
+ struct dirent *dp;
+ int failed_files = 0;
+ int num_loaded = 0;
+
+ if(!ndpi_str || !dir_path)
+ return(0);
+
+ dirp = opendir(dir_path);
+ if (dirp == NULL)
+ return(0);
+
+ while((dp = readdir(dirp)) != NULL) {
+ char *underscore, *extn;
+
+ if(dp->d_name[0] == '.') continue;
+ extn = strrchr(dp->d_name, '.');
+
+ if((extn == NULL) || strcmp(extn, ".list"))
+ continue;
+
+ /* Check if the format is <proto it>_<string>.<extension> */
+ if((underscore = strchr(dp->d_name, '_')) != NULL) {
+ int proto_id;
+ const char *errstrp;
+
+ underscore[0] = '\0';
+ proto_id = ndpi_strtonum(dp->d_name, 1, NDPI_LAST_IMPLEMENTED_PROTOCOL - 1, &errstrp, 10);
+ if(errstrp == NULL) {
+ /* Valid file */
+ char path[512];
+
+ underscore[0] = '_';
+ snprintf(path, sizeof(path), "%s/%s", dir_path, dp->d_name);
+
+ if (ndpi_load_protocol_id_file(ndpi_str, path, proto_id) < 0) {
+ NDPI_LOG_ERR(ndpi_str, "Failed to load '%s'\n", path);
+ failed_files++;
+ }else
+ num_loaded++;
+ }
+ }
+ }
+
+ (void)closedir(dirp);
+
+ if(failed_files)
+ return(-1 * failed_files);
+
+ return(num_loaded);
+}
+
+/* ******************************************************************** */
+
static int ndpi_load_risky_domain(struct ndpi_detection_module_struct *ndpi_str,
char* domain_name) {
if(ndpi_str->risky_domain_automa.ac_automa == NULL) {