From 15ec013af8e6fd48c211e0ea14bf27739c9c4211 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 11 Nov 2019 23:52:24 +0100 Subject: Added ndpi_load_ipv4_ptree() API call --- src/include/ndpi_api.h | 4 +++- src/lib/ndpi_main.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 14de814f6..088837f98 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -842,7 +842,9 @@ extern "C" { void ndpi_user_pwd_payload_copy(u_int8_t *dest, u_int dest_len, u_int offset, const u_int8_t *src, u_int src_len); u_char* ndpi_base64_decode(const u_char *src, size_t len, size_t *out_len); - + int ndpi_load_ipv4_ptree(struct ndpi_detection_module_struct *ndpi_str, + const char *path, u_int16_t protocol_id); + int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int8_t ip_version, diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 1c7becdde..2641ceeaf 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1943,6 +1943,61 @@ static patricia_node_t* add_to_ptree(patricia_tree_t *tree, int family, return(node); } +/* ******************************************* */ + +/* + Load a file containing IPv4 addresses in CIDR format as 'protocol_id' + + Return: the number of entries loaded or -1 in case of error +*/ +int ndpi_load_ipv4_ptree(struct ndpi_detection_module_struct *ndpi_str, + const char *path, u_int16_t protocol_id) { + char buffer[128], *line, *addr, *cidr, *saveptr; + FILE *fd; + int len; + u_int num_loaded = 0; + + fd = fopen(path, "r"); + + if(fd == NULL) { + NDPI_LOG_ERR(ndpi_str, "Unable to open file %s [%s]\n", path, strerror(errno)); + return(-1); + } + + while(fd) { + line = fgets(buffer, sizeof(buffer), fd); + + if(line == NULL) + break; + + len = strlen(line); + + if((len <= 1) || (line[0] == '#')) + continue; + + line[len-1] = '\0'; + addr = strtok_r(line, "/", &saveptr); + + if(addr) { + cidr = strtok_r(NULL, "\n", &saveptr); + + if(cidr) { + struct in_addr pin; + patricia_node_t *node; + + pin.s_addr = inet_addr(addr); + if((node = add_to_ptree(ndpi_str->protocols_ptree, AF_INET, + &pin, atoi(cidr) /* bits */)) != NULL) + node->value.user_value = protocol_id, num_loaded++; + } + } + } + + fclose(fd); + return(num_loaded); +} + + /* ******************************************* */ static void ndpi_init_ptree_ipv4(struct ndpi_detection_module_struct *ndpi_str, -- cgit v1.2.3