aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/ndpiReader.c17
-rw-r--r--src/include/ndpi_api.h2
-rw-r--r--src/lib/ndpi_cache.c67
3 files changed, 84 insertions, 2 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 6b32370c7..d1e5d9bb1 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -6419,12 +6419,13 @@ void domainCacheTestUnit() {
ndpi_ip_addr_t ip;
u_int32_t epoch_now = (u_int32_t)time(NULL);
struct ndpi_address_cache_item *ret;
-
+ const char *fname = "/tmp/cache.dump";
+
assert(cache);
memset(&ip, 0, sizeof(ip));
ip.ipv4 = 12345678;
- assert(ndpi_address_cache_insert(cache, ip, "nodomain.local", epoch_now, 0) == true);
+ assert(ndpi_address_cache_insert(cache, ip, "nodomain.local", epoch_now, 32) == true);
ip.ipv4 = 87654321;
assert(ndpi_address_cache_insert(cache, ip, "hello.local", epoch_now, 0) == true);
@@ -6434,7 +6435,19 @@ void domainCacheTestUnit() {
sleep(1);
assert(ndpi_address_cache_find(cache, ip, time(NULL)) == NULL);
+ assert(ndpi_address_cache_dump(cache, (char*)fname, epoch_now));
+ ndpi_term_address_cache(cache);
+
+ cache = ndpi_init_address_cache(32000);
+ assert(cache);
+ assert(ndpi_address_cache_restore(cache, (char*)fname, epoch_now) == 1);
+
+ ip.ipv4 = 12345678;
+ assert((ret = ndpi_address_cache_find(cache, ip, epoch_now)) != NULL);
+ assert(strcmp(ret->hostname, "nodomain.local") == 0);
+
ndpi_term_address_cache(cache);
+ unlink(fname);
}
/* *********************************************** */
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h
index b08b6e69f..94ba1f920 100644
--- a/src/include/ndpi_api.h
+++ b/src/include/ndpi_api.h
@@ -2344,6 +2344,8 @@ extern "C" {
struct ndpi_address_cache_item* ndpi_cache_address_find(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_ip_addr_t ip_addr);
+ bool ndpi_address_cache_dump(struct ndpi_address_cache *cache, char *path, u_int32_t epoch_now);
+ u_int32_t ndpi_address_cache_restore(struct ndpi_address_cache *cache, char *path, u_int32_t epoch_now);
/* ******************************* */
diff --git a/src/lib/ndpi_cache.c b/src/lib/ndpi_cache.c
index 1efb99f6d..58842b95b 100644
--- a/src/lib/ndpi_cache.c
+++ b/src/lib/ndpi_cache.c
@@ -397,6 +397,73 @@ bool ndpi_address_cache_insert(struct ndpi_address_cache *cache,
}
/* ***************************************************** */
+
+bool ndpi_address_cache_dump(struct ndpi_address_cache *cache,
+ char *path, u_int32_t epoch_now) {
+ FILE *fd = fopen(path, "w");
+ u_int i;
+
+ if(!fd) return(false);
+
+ for(i=0; i<cache->num_root_nodes; i++) {
+ struct ndpi_address_cache_item *root = cache->address_cache_root[i];
+
+ while(root != NULL) {
+ char buf[33];
+ u_char *a = (u_char*)&(root->addr);
+ u_int j, idx;
+
+ if(epoch_now && (root->expire_epoch < epoch_now))
+ continue; /* Expired epoch */
+
+ for(j=0, idx=0; j<sizeof(ndpi_ip_addr_t); j++, idx += 2)
+ snprintf(&buf[idx], sizeof(buf)-idx, "%02X", a[j]);
+
+ fprintf(fd, "%s\t%s\t%u\n", buf, root->hostname, root->expire_epoch);
+
+ root = root->next;
+ }
+ }
+
+ fclose(fd);
+ return(true);
+}
+
+/* ***************************************************** */
+
+/* Return the number of items restored */
+u_int32_t ndpi_address_cache_restore(struct ndpi_address_cache *cache, char *path, u_int32_t epoch_now) {
+ FILE *fd = fopen(path, "r");
+ char ip[33], hostname[256];
+ u_int32_t epoch, num_added = 0;
+
+ if(!fd) return(false);
+
+ while(fscanf(fd, "%s\t%s\t%u\n", ip, hostname, &epoch) > 0) {
+ if(epoch >= epoch_now) { /* Entry not yet expired */
+ u_int ttl = epoch-epoch_now;
+ ndpi_ip_addr_t addr;
+ char *a = (char*)&addr;
+ u_int i, j;
+
+ for(i=0, j=0; i<(sizeof(ndpi_ip_addr_t)*2); i += 2, j++) {
+ char buf[3];
+
+ buf[0] = ip[i], buf[1] = ip[i+1], buf[2] = '\0';
+ a[j] = strtol(buf, NULL, 16);
+ }
+
+ if(ndpi_address_cache_insert(cache, addr, hostname, epoch_now, ttl))
+ num_added++;
+ }
+ }
+
+ fclose(fd);
+
+ return(num_added);
+}
+
+/* ***************************************************** */
/* ***************************************************** */
bool ndpi_cache_address(struct ndpi_detection_module_struct *ndpi_struct,