Skip to content

Commit

Permalink
Added new API calls for serializing/restoring the DNS cache
Browse files Browse the repository at this point in the history
-  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);
  • Loading branch information
lucaderi committed Oct 10, 2024
1 parent ab3e073 commit b9348e9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
17 changes: 15 additions & 2 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/* *********************************************** */
Expand Down
2 changes: 2 additions & 0 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/* ******************************* */

Expand Down
67 changes: 67 additions & 0 deletions src/lib/ndpi_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; 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);
}

/* ***************************************************** */
/* ***************************************************** */

Expand Down

0 comments on commit b9348e9

Please sign in to comment.