From 2e679ba8647aff7b0114de2940a03e4186ccc3dc Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Wed, 28 May 2025 22:10:10 +0200 Subject: Added ndpi_load_protocols_dir() API call for loading IP-based protocol detection Added --protocols-list-dir to ndpiReader for loading IP_based protocol --- src/lib/ndpi_main.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 1 deletion(-) (limited to 'src/lib/ndpi_main.c') 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 ) 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 _