[Dnsmasq-discuss] [PATCH] Add --max-cache-ttl to influence cache lifetime

Dennis Kaarsemaker dennis.kaarsemaker at booking.com
Tue Sep 18 11:42:14 BST 2012


Hi Simon,

I want to be able to do minimal DNS caching, using dnsmasq mostly for
splitting resolver paths (1 domain to a different server). This option
helps me achieve that.

Background reasoning (if you're interested):

For our internal domains, all nameservers are authoritative, no caching
is used internally, making name changes appear immediately everywhere.
We have some dumb applications however, which may do dns lookups for the
same name multiple times per second. This isn't much of a problem, so
we've not fixed it. However, I'm now introducing dnsmasq as part of our
internal loadbalancing layer for its possibility to redirect requests
for a specific domain elsewhere. It'd be a shame not to use this
opportunity to "fix" the other issue.

---
 man/dnsmasq.8 |    3 +++
 src/cache.c   |    3 +++
 src/dnsmasq.h |    2 +-
 src/option.c  |    5 +++++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/man/dnsmasq.8 b/man/dnsmasq.8
index 89bc8a4..ac63fa2 100644
--- a/man/dnsmasq.8
+++ b/man/dnsmasq.8
@@ -71,6 +71,9 @@ maximum TTL will be given to clients instead of the true TTL value if it is
 lower. The true TTL value is however kept in the cache to avoid flooding 
 the upstream DNS servers.
 .TP
+.B --max-cache-ttl=<time>
+Set a maximum TTL value for entries in the cache.
+.TP
 .B \-k, --keep-in-foreground
 Do not go into the background at startup but otherwise run as
 normal. This is intended for use when dnsmasq is run under daemontools
diff --git a/src/cache.c b/src/cache.c
index ee4e763..ddbc5c0 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -371,6 +371,9 @@ struct crec *cache_insert(char *name, struct all_addr *addr,
   int freed_all = flags & F_REVERSE;
   int free_avail = 0;
 
+  if(daemon->max_cache_ttl < ttl)
+    ttl = daemon->max_cache_ttl;
+
   /* Don't log keys */
   if (flags & (F_IPV4 | F_IPV6))
     log_query(flags | F_UPSTREAM, name, addr, NULL);
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index 98a2327..3ea0278 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -751,7 +751,7 @@ extern struct daemon {
   int max_logs;  /* queue limit */
   int cachesize, ftabsize;
   int port, query_port, min_port;
-  unsigned long local_ttl, neg_ttl, max_ttl;
+  unsigned long local_ttl, neg_ttl, max_ttl, max_cache_ttl;
   struct hostsfile *addn_hosts;
   struct dhcp_context *dhcp, *dhcp6, *ra_contexts;
   struct dhcp_config *dhcp_conf;
diff --git a/src/option.c b/src/option.c
index 22c08e7..6cfd0d1 100644
--- a/src/option.c
+++ b/src/option.c
@@ -120,6 +120,7 @@ struct myoption {
 #define LOPT_TFTP_LC   309
 #define LOPT_RR        310
 #define LOPT_CLVERBIND 311
+#define LOPT_MAXCTTL   312
 
 #ifdef HAVE_GETOPT_LONG
 static const struct option opts[] =  
@@ -223,6 +224,7 @@ static const struct myoption opts[] =
     { "dhcp-broadcast", 2, 0, LOPT_BROADCAST },
     { "neg-ttl", 1, 0, LOPT_NEGTTL },
     { "max-ttl", 1, 0, LOPT_MAXTTL },
+    { "max-cache-ttl", 1, 0, LOPT_MAXCTTL },
     { "dhcp-alternate-port", 2, 0, LOPT_ALTPORT },
     { "dhcp-scriptuser", 1, 0, LOPT_SCRIPTUSR },
     { "min-port", 1, 0, LOPT_MINPORT },
@@ -1917,6 +1919,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
     case 'T':         /* --local-ttl */
     case LOPT_NEGTTL: /* --neg-ttl */
     case LOPT_MAXTTL: /* --max-ttl */
+    case LOPT_MAXCTTL: /* --max-cache-ttl */
       {
 	int ttl;
 	if (!atoi_check(arg, &ttl))
@@ -1925,6 +1928,8 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
 	  daemon->neg_ttl = (unsigned long)ttl;
 	else if (option == LOPT_MAXTTL)
 	  daemon->max_ttl = (unsigned long)ttl;
+	else if (option == LOPT_MAXCTTL)
+	  daemon->max_cache_ttl = (unsigned long)ttl;
 	else
 	  daemon->local_ttl = (unsigned long)ttl;
 	break;
-- 
1.7.9.5





More information about the Dnsmasq-discuss mailing list