aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2024-10-10 18:10:03 +0200
committerLuca Deri <deri@ntop.org>2024-10-10 18:10:03 +0200
commit2e5edd2cc956b420f6b9e2a2ffec7d435694a69a (patch)
tree51cafc72126996a24cb821ba5050ca6a9081f5e8 /src
parentb9348e9d6e0e754c4b17661c643ca258f1540ca1 (diff)
Added -N option for dumping/restoring the DNS cache (when enabled)
Example ndpiReader -i en0 --cfg=dpi.address_cache_size,32768 -N /tmp/a
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_api.h14
-rw-r--r--src/lib/ndpi_cache.c39
2 files changed, 45 insertions, 8 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h
index 94ba1f920..c1db4bbfd 100644
--- a/src/include/ndpi_api.h
+++ b/src/include/ndpi_api.h
@@ -2337,16 +2337,22 @@ extern "C" {
/* Address cache API */
struct ndpi_address_cache* ndpi_init_address_cache(u_int32_t max_num_entries);
void ndpi_term_address_cache(struct ndpi_address_cache *cache);
- u_int ndpi_address_cache_flush_expired(struct ndpi_address_cache *cache, u_int32_t epoch_now);
+ u_int32_t ndpi_address_cache_flush_expired(struct ndpi_address_cache *cache, u_int32_t epoch_now);
struct ndpi_address_cache_item* ndpi_address_cache_find(struct ndpi_address_cache *cache, ndpi_ip_addr_t ip_addr, u_int32_t epoch_now);
bool ndpi_address_cache_insert(struct ndpi_address_cache *cache, ndpi_ip_addr_t ip_addr, char *hostname,
u_int32_t epoch_now, u_int32_t ttl);
-
- 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);
+
+ bool ndpi_cache_address(struct ndpi_detection_module_struct *ndpi_struct,
+ ndpi_ip_addr_t ip_addr, char *hostname,
+ u_int32_t epoch_now, u_int32_t ttl);
+ struct ndpi_address_cache_item* ndpi_cache_address_find(struct ndpi_detection_module_struct *ndpi_struct, ndpi_ip_addr_t ip_addr);
+ bool ndpi_cache_address_dump(struct ndpi_detection_module_struct *ndpi_struct, char *path, u_int32_t epoch_now);
+ u_int32_t ndpi_cache_address_restore(struct ndpi_detection_module_struct *ndpi_struct, char *path, u_int32_t epoch_now);
+ u_int32_t ndpi_cache_address_flush_expired(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t epoch_now);
+
/* ******************************* */
const char *ndpi_lru_cache_idx_to_name(lru_cache_type idx);
diff --git a/src/lib/ndpi_cache.c b/src/lib/ndpi_cache.c
index 58842b95b..a85cb1623 100644
--- a/src/lib/ndpi_cache.c
+++ b/src/lib/ndpi_cache.c
@@ -264,10 +264,10 @@ void ndpi_term_address_cache(struct ndpi_address_cache *cache) {
/* ***************************************************** */
/* Return the number of purged entries */
-u_int ndpi_address_cache_flush_expired(struct ndpi_address_cache *cache,
- u_int32_t epoch_now) {
- u_int i, num_purged = 0;
-
+u_int32_t ndpi_address_cache_flush_expired(struct ndpi_address_cache *cache,
+ u_int32_t epoch_now) {
+ u_int32_t i, num_purged = 0;
+
for(i=0; i<cache->num_root_nodes; i++) {
struct ndpi_address_cache_item *root = cache->address_cache_root[i];
struct ndpi_address_cache_item *prev = NULL;
@@ -488,3 +488,34 @@ struct ndpi_address_cache_item* ndpi_cache_address_find(struct ndpi_detection_mo
return(ndpi_address_cache_find(ndpi_struct->address_cache, ip_addr, 0));
}
+
+/* ***************************************************** */
+
+bool ndpi_cache_address_dump(struct ndpi_detection_module_struct *ndpi_struct, char *path, u_int32_t epoch_now) {
+ if(ndpi_struct->address_cache == NULL) return(false);
+
+ return(ndpi_address_cache_dump(ndpi_struct->address_cache, path, epoch_now));
+}
+
+/* ***************************************************** */
+
+u_int32_t ndpi_cache_address_restore(struct ndpi_detection_module_struct *ndpi_struct, char *path, u_int32_t epoch_now) {
+ if(ndpi_struct->address_cache == NULL) {
+ if(ndpi_struct->cfg.address_cache_size == 0)
+ return(0);
+
+ if((ndpi_struct->address_cache = ndpi_init_address_cache(ndpi_struct->cfg.address_cache_size)) == 0)
+ return(0);
+ }
+
+ return(ndpi_address_cache_restore(ndpi_struct->address_cache, path, epoch_now));
+}
+
+/* ***************************************************** */
+
+u_int32_t ndpi_cache_address_flush_expired(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t epoch_now) {
+ if(ndpi_struct->address_cache == NULL)
+ return(0);
+ else
+ return(ndpi_address_cache_flush_expired(ndpi_struct->address_cache, epoch_now));
+}