From 6871dbdd047d7ff2b6a8a81973ffb541a379b89c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 5 Dec 2019 13:56:14 +0000 Subject: [PATCH] Add /api/dns/cacheinfo Signed-off-by: DL6ER --- src/api/dns.c | 18 ++++++++++++++++++ src/api/dns.h | 22 ++++++++++++++++++++++ src/api/routes.c | 4 ++++ src/api/routes.h | 1 + src/dnsmasq_interface.c | 11 ++++------- src/dnsmasq_interface.h | 3 ++- 6 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/api/dns.h diff --git a/src/api/dns.c b/src/api/dns.c index 2f440acb..1bd9167d 100644 --- a/src/api/dns.c +++ b/src/api/dns.c @@ -11,6 +11,7 @@ #include "FTL.h" // counters #include "shmem.h" +#include "dns.h" #include "http-common.h" #include "routes.h" #include "json_macros.h" @@ -287,3 +288,20 @@ int api_dns_somelist(struct mg_connection *conn, bool exact, bool whitelist) return 0; } } + +int api_dns_cacheinfo(struct mg_connection *conn) +{ + // Verify requesting client is allowed to access this ressource + if(check_client_auth(conn) < 0) + { + return send_json_unauthorized(conn); + } + + cacheinforecord cacheinfo; + getCacheInformation(&cacheinfo); + cJSON *json = JSON_NEW_OBJ(); + JSON_OBJ_ADD_NUMBER(json, "cache-size", cacheinfo.cache_size); + JSON_OBJ_ADD_NUMBER(json, "cache-live-freed", cacheinfo.cache_live_freed); + JSON_OBJ_ADD_NUMBER(json, "cache-inserted", cacheinfo.cache_inserted); + JSON_SEND_OBJECT(json); +} diff --git a/src/api/dns.h b/src/api/dns.h new file mode 100644 index 00000000..896aa0ac --- /dev/null +++ b/src/api/dns.h @@ -0,0 +1,22 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2019 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* API DNS prototypes +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ +#ifndef API_DNS_H +#define API_DNS_H + +typedef struct cacheinforecord { + int cache_size; + int cache_live_freed; + int cache_inserted; +} cacheinforecord; + +// defined in src/dnsmasq_interface.c +extern void getCacheInformation(cacheinforecord *cacheinfo); + +#endif // API_DNS_H \ No newline at end of file diff --git a/src/api/routes.c b/src/api/routes.c index 955d3f39..7dc813fb 100644 --- a/src/api/routes.c +++ b/src/api/routes.c @@ -45,6 +45,10 @@ int api_handler(struct mg_connection *conn, void *ignored) { ret = api_dns_somelist(conn, false, false); } + else if(startsWith("/api/dns/cacheinfo", request->local_uri)) + { + ret = api_dns_cacheinfo(conn); + } /******************************** api/ftl ****************************/ else if(startsWith("/api/ftl/clientIP", request->local_uri)) { diff --git a/src/api/routes.h b/src/api/routes.h index 9a485f18..08221075 100644 --- a/src/api/routes.h +++ b/src/api/routes.h @@ -35,6 +35,7 @@ int api_ftl_network(struct mg_connection *conn); // DNS methods int api_dns_status(struct mg_connection *conn); int api_dns_somelist(struct mg_connection *conn, bool exact, bool whitelist); +int api_dns_cacheinfo(struct mg_connection *conn); // Version method int api_version(struct mg_connection *conn); diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index a9145b0c..165b34e5 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -1689,16 +1689,14 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw) } // int cache_inserted, cache_live_freed are defined in dnsmasq/cache.c -int *getCacheInformation(void) +void getCacheInformation(cacheinforecord *cacheinfo) { - // Allocate memory - int *cacheinfo = calloc(3,sizeof(int)); // cache-size - interpretation is obvious - cacheinfo[0] = daemon->cachesize; + cacheinfo->cache_size = daemon->cachesize; // cache-live-freed - interpretation see below - cacheinfo[1] = daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]; + cacheinfo->cache_live_freed = daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]; // cache-inserted - interpretation see below - cacheinfo[2] = daemon->metrics[METRIC_DNS_CACHE_INSERTED]; + cacheinfo->cache_inserted = daemon->metrics[METRIC_DNS_CACHE_INSERTED]; // cache-live-freed and cache-inserted: // It means the resolver handled names lookups that // needed to be sent to upstream servers and that @@ -1708,7 +1706,6 @@ int *getCacheInformation(void) // cached. If the cache is full with entries which haven't reached // the end of their time-to-live, then the entry which hasn't been // looked up for the longest time is evicted. - return cacheinfo; } void _FTL_forwarding_failed(const struct server *server, const char* file, const int line) diff --git a/src/dnsmasq_interface.h b/src/dnsmasq_interface.h index 61ae6b12..33e44db4 100644 --- a/src/dnsmasq_interface.h +++ b/src/dnsmasq_interface.h @@ -11,6 +11,8 @@ #define DNSMASQ_INTERFACE_H #include +// cacheinforecord +#include "api/dns.h" extern unsigned char* pihole_privacylevel; enum { TCP, UDP }; @@ -46,7 +48,6 @@ void _FTL_get_blocking_metadata(union all_addr **addrp, unsigned int *flags, con #define FTL_CNAME(domain, cpp, id) _FTL_CNAME(domain, cpp, id, __FILE__, __LINE__) bool _FTL_CNAME(const char *domain, const struct crec *cpp, const int id, const char* file, const int line); -int *getCacheInformation(void); void FTL_dnsmasq_reload(void); void FTL_fork_and_bind_sockets(struct passwd *ent_pw);