From b9348e9d6e0e754c4b17661c643ca258f1540ca1 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Thu, 10 Oct 2024 16:51:45 +0200 Subject: Added new API calls for serializing/restoring the DNS cache - 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); --- src/lib/ndpi_cache.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/lib') 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 @@ -396,6 +396,73 @@ bool ndpi_address_cache_insert(struct ndpi_address_cache *cache, return(true); } +/* ***************************************************** */ + +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; inum_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; jhostname, 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); +} + /* ***************************************************** */ /* ***************************************************** */ -- cgit v1.2.3