aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/ndpi_api.h4
-rw-r--r--src/lib/ndpi_main.c55
2 files changed, 58 insertions, 1 deletions
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
@@ -1945,6 +1945,61 @@ static patricia_node_t* add_to_ptree(patricia_tree_t *tree, int family,
/* ******************************************* */
+/*
+ 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,
void *ptree, ndpi_network host_list[],
u_int8_t skip_tor_hosts) {