[Dnsmasq-discuss] [patch] add support for setting minimum ttl

Mike Frysinger vapier@gentoo.org
Tue, 3 May 2005 08:48:54 -0400


--Boundary-00=_2M3dCyq4rkPZJfz
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

this patch adds a 'min-ttl' configuration option.  this allows you to override 
the ttl for all queries ... for example, if you run `dnsmasq --min-ttl 3600`, 
then dnsmasq will cache all queries for at least 1 day (3600 seconds in a 
day) even if the record has a ttl value of say 300 seconds (like google 
normally uses)

after reading the archives it seems like this probably won't be added to the 
official package, but i imagine some people want this feature, so at least 
it'll sit in the mail archives for people to use :p
-mike

--Boundary-00=_2M3dCyq4rkPZJfz
Content-Type: text/x-diff;
  charset="us-ascii";
  name="minttl.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="minttl.patch"

--- dnsmasq-2.22/src/dnsmasq.h
+++ dnsmasq-2.22/src/dnsmasq.h
@@ -399,7 +399,7 @@
   struct server *servers;
   int cachesize;
   int port, query_port;
-  unsigned long local_ttl;
+  unsigned long local_ttl, min_ttl;
   struct hostsfile *addn_hosts;
   struct dhcp_context *dhcp;
   struct dhcp_config *dhcp_conf;
--- dnsmasq-2.22/src/option.c
+++ dnsmasq-2.22/src/option.c
@@ -21,7 +21,7 @@
   int val;
 };
 
-#define OPTSTRING "yZDNLERKzowefnbvhdkqr:m:p:c:l:s:i:t:u:g:a:x:S:C:A:T:H:Q:I:B:F:G:O:M:X:V:U:j:P:J:W:Y:"
+#define OPTSTRING "yZDNLERKzowefnbvhdkqr:m:p:c:l:s:i:t:u:g:a:x:S:C:A:T:H:Q:I:B:F:G:O:M:X:V:U:j:P:J:W:Y:_:"
 
 static struct myoption opts[] = { 
   {"version", 0, 0, 'v'},
@@ -79,6 +79,7 @@
   {"srv-host", 1, 0, 'W'},
   {"localise-queries", 0, 0, 'y'},
   {"txt-record", 1, 0, 'Y'},
+  {"min-ttl", 1, 0, '_'},
   {0, 0, 0, 0}
 };
 
@@ -170,6 +171,7 @@
 "-Y  --txt-record=name,txt....       Specify TXT DNS record.\n"
 "-z, --bind-interfaces               Bind only to interfaces in use.\n"
 "-Z, --read-ethers                   Read DHCP static host information from " ETHERSFILE ".\n"
+"-_, --min-ttl                       Force all records to have a TTL at least this long.\n"
 "\n";
 
 static void add_txt(struct daemon *daemon, char *name, char *txt)
@@ -216,6 +218,7 @@
 
   /* Set defaults - everything else is zero or NULL */
   daemon->min_leasetime = UINT_MAX;
+  daemon->local_ttl = daemon->min_ttl = 0;
   daemon->cachesize = CACHESIZ;
   daemon->port = NAMESERVER_PORT;
   daemon->default_resolv.is_default = 1;
@@ -819,6 +822,16 @@
 		break;
 	      }
 
+	    case '_':
+	      {
+		int ttl;
+		if (!atoi_check(arg, &ttl))
+		  option = '?';
+		else
+		  daemon->min_ttl = (unsigned long)ttl;
+		break;
+	      }
+
 	    case 'X':
 	      if (!atoi_check(arg, &daemon->dhcp_max))
 		option = '?';
--- dnsmasq-2.22/src/rfc1035.c
+++ dnsmasq-2.22/src/rfc1035.c
@@ -607,6 +607,8 @@
 		  /* TTL of record is minimum of CNAMES and PTR */
 		  if (attl < cttl)
 		    cttl = attl;
+		  if (cttl < daemon->min_ttl)
+		    cttl = daemon->min_ttl;
 
 		  if (aqclass == C_IN && res != 2 && (aqtype == T_CNAME || aqtype == T_PTR))
 		    {
@@ -637,6 +642,8 @@
 		  searched_soa = 1;
 		  ttl = find_soa(header, NULL, qlen);
 		}
+	      if (ttl < daemon->min_ttl)
+		ttl = daemon->min_ttl;
 	      if (ttl)
 		cache_insert(name, &addr, now, ttl, name_encoding | F_REVERSE | F_NEG | flags);	
 	    }
@@ -671,6 +681,8 @@
 		  GETLONG(attl, p1);
 		  GETSHORT(ardlen, p1);
 		  endrr = p1+ardlen;
+		  if (attl < daemon->min_ttl)
+		    attl = daemon->min_ttl;
 		  
 		  if (aqclass == C_IN && res != 2 && (aqtype == T_CNAME || aqtype == qtype))
 		    {
@@ -725,6 +740,8 @@
 		 pointing at this, inherit it's TTL */
 	      if (ttl || cpp)
 		{
+		  if (ttl < daemon->min_ttl)
+		    ttl = daemon->min_ttl;
 		  newc = cache_insert(name, (struct all_addr *)p, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags);	
 		  if (newc && cpp)
 		    {
--- dnsmasq-2.22/src/dnsmasq.8
+++ dnsmasq-2.22/src/dnsmasq.8
@@ -46,6 +46,9 @@
 reduce the load on the server at the expense of clients using stale
 data under some circumstances.
 .TP
+.B \-_, --min-ttl=<time>
+When storing entries in the cache, override the minimum TTL value.
+.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.
--- dnsmasq-2.22/src/dnsmasq.conf.example
+++ dnsmasq-2.22/src/dnsmasq.conf.example
@@ -282,6 +282,11 @@
 # seconds) here.
 #local-ttl=
 
+# Set a minimum Time-To-Live value for all cached entries. If an entry has 
+# a larger TTL, then that will still be used. See the warning in local-ttl 
+# about load vs stale data.
+#min-ttl=
+
 # If you want dnsmasq to detect attempts by Verisign to send queries
 # to unregistered .com and .net hosts to its sitefinder service and
 # have dnsmasq instead return the correct NXDOMAIN response, uncomment

--Boundary-00=_2M3dCyq4rkPZJfz--