From 13ac5ace736a5d24c629d8cadc65eda92ea443cb Mon Sep 17 00:00:00 2001
From: guns <[email protected]>
Date: Thu, 17 Jan 2013 04:26:56 -0600
Subject: [PATCH] Add --hosts-cache option
Optionally dumps cache to specified <path> in hosts format on receipt
of SIGUSR1. This file can be loaded later by the user as an additional
hosts file (-H).
---
src/cache.c | 17 ++++++++++++++++-
src/dnsmasq.h | 1 +
src/option.c | 8 ++++++++
3 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/src/cache.c b/src/cache.c
index 03b3e78..20e79d8 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -1165,10 +1165,20 @@ void dump_cache(time_t now)
my_syslog(LOG_INFO, _("server %s#%d: queries sent %u, retried or failed %u"), daemon->addrbuff, port, queries, failed_queries);
}
- if (option_bool(OPT_DEBUG) || option_bool(OPT_LOG))
+ if (option_bool(OPT_DEBUG) || option_bool(OPT_LOG) || daemon->hosts_cache != NULL)
{
struct crec *cache ;
int i;
+ int dumping_hosts = daemon->hosts_cache != NULL;
+ FILE *hosts_cache = NULL;
+ if (dumping_hosts) {
+ hosts_cache = fopen(daemon->hosts_cache, "w");
+ if (hosts_cache == NULL) {
+ dumping_hosts = 0;
+ my_syslog(LOG_WARNING, "Failed to open hosts cache file: \"%s\"", daemon->hosts_cache);
+ }
+ }
+
my_syslog(LOG_INFO, "Host Address Flags Expires");
for (i=0; i<hash_size; i++)
@@ -1206,6 +1216,8 @@ void dump_cache(time_t now)
else if (cache->flags & F_IPV6)
inet_ntop(AF_INET6, &cache->addr.addr, a, ADDRSTRLEN);
#endif
+ if (dumping_hosts)
+ fprintf(hosts_cache, "%s\t%s\n", a, cache_get_name(cache));
}
p += sprintf(p, "%-30.30s %s%s%s%s%s%s%s%s%s%s%s%s%s ", a,
@@ -1231,6 +1243,9 @@ void dump_cache(time_t now)
#endif
my_syslog(LOG_INFO, daemon->namebuff);
}
+
+ if (dumping_hosts)
+ fclose(hosts_cache);
}
}
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index c715e58..a79f5d0 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -778,6 +778,7 @@ struct tftp_prefix {
unsigned int duid_enterprise, duid_config_len;
unsigned char *duid_config;
char *dbus_name;
+ char *hosts_cache;
/* globally used stuff for DNS */
char *packet; /* packet buffer */
diff --git a/src/option.c b/src/option.c
index c935b4b..e9afb09 100644
--- a/src/option.c
+++ b/src/option.c
@@ -121,6 +121,7 @@ struct myoption {
#define LOPT_RR 310
#define LOPT_CLVERBIND 311
#define LOPT_MAXCTTL 312
+#define LOPT_HOSTCACHE 313
#ifdef HAVE_GETOPT_LONG
static const struct option opts[] =
@@ -247,6 +248,7 @@ struct myoption {
{ "dhcp-duid", 1, 0, LOPT_DUID },
{ "host-record", 1, 0, LOPT_HOST_REC },
{ "bind-dynamic", 0, 0, LOPT_CLVERBIND },
+ { "hosts-cache", 1, 0, LOPT_HOSTCACHE },
{ NULL, 0, 0, 0 }
};
@@ -379,6 +381,7 @@ struct myoption {
{ LOPT_HOST_REC, ARG_DUP, "<name>,<address>", gettext_noop("Specify host (A/AAAA and PTR) records"), NULL },
{ LOPT_RR, ARG_DUP, "<name>,<RR-number>,[<data>]", gettext_noop("Specify arbitrary DNS resource record"), NULL },
{ LOPT_CLVERBIND, OPT_CLEVERBIND, NULL, gettext_noop("Bind to interfaces in use - check for new interfaces"), NULL},
+ { LOPT_HOSTCACHE, ARG_ONE, "<path>", gettext_noop("Dump cached A records to <path> on SIGUSR1"), NULL },
{ 0, 0, NULL, NULL, NULL }
};
@@ -3186,6 +3189,11 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
daemon->host_records_tail = new;
break;
}
+ case LOPT_HOSTCACHE: /* --hosts-cache */
+ {
+ daemon->hosts_cache = opt_string_alloc(arg);
+ break;
+ }
default:
ret_err(_("unsupported option (check that dnsmasq was compiled with DHCP/TFTP/DBus support)"));
--
1.8.1.6