dnsmasq更改ttl时间

/ 0评 / 3

https://thekelleys.org.uk/dnsmasq/

dnsmasq自带min-cache-ttl可修改服务器缓存最小值,但是首次请求返回给客户端ttl的是正常ttl,使用以下patch可修改返回给客户端ttl时间,配合min-cache-ttl保持和dnsmasq缓存ttl时间一致,dnsmasq不开启缓存可单独修改min_ttl

dnsmasq-2.86版本

--- a/src/config.h
+++ b/src/config.h
@@ -31,7 +31,7 @@
 #define LOCALS_LOGGED 8 /* Only log this many local addresses when logging state */
 #define LEASE_RETRY 60 /* on error, retry writing leasefile after LEASE_RETRY seconds */
 #define CACHESIZ 150 /* default cache size */
-#define TTL_FLOOR_LIMIT 3600 /* don't allow --min-cache-ttl to raise TTL above this under any circumstances */
+#define TTL_FLOOR_LIMIT 86400 /* don't allow --min-cache-ttl to raise TTL above this under any circumstances */
 #define MAXLEASES 1000 /* maximum number of DHCP leases */
 #define PING_WAIT 3 /* wait for ping address-in-use test */
 #define PING_CACHE_TIME 30 /* Ping test assumed to be valid this long. */
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -1116,7 +1116,7 @@ extern struct daemon {
   int max_logs;  /* queue limit */
   int cachesize, ftabsize;
   int port, query_port, min_port, max_port;
-  unsigned long local_ttl, neg_ttl, max_ttl, min_cache_ttl, max_cache_ttl, auth_ttl, dhcp_ttl, use_dhcp_ttl;
+  unsigned long local_ttl, neg_ttl, min_ttl, max_ttl, min_cache_ttl, max_cache_ttl, auth_ttl, dhcp_ttl, use_dhcp_ttl;
   char *dns_client_id;
   u32 umbrella_org;
   u32 umbrella_asset;
--- a/src/option.c
+++ b/src/option.c
@@ -174,6 +174,7 @@ struct myoption {
 #define LOPT_CMARK_ALST_EN 365
 #define LOPT_CMARK_ALST    366
 #define LOPT_QUIET_TFTP    367
+#define LOPT_MINTTL        368
  
 #ifdef HAVE_GETOPT_LONG
 static const struct option opts[] =  
@@ -292,6 +293,7 @@ static const struct myoption opts[] =
     { "dhcp-name-match", 1, 0, LOPT_NAME_MATCH },
     { "dhcp-broadcast", 2, 0, LOPT_BROADCAST },
     { "neg-ttl", 1, 0, LOPT_NEGTTL },
+    { "min-ttl", 1, 0, LOPT_MINTTL },
     { "max-ttl", 1, 0, LOPT_MAXTTL },
     { "min-cache-ttl", 1, 0, LOPT_MINCTTL },
     { "max-cache-ttl", 1, 0, LOPT_MAXCTTL },
@@ -425,6 +427,7 @@ static struct {
   { 't', ARG_ONE, "<host_name>", gettext_noop("Specify default target in an MX record."), NULL },
   { 'T', ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for replies from /etc/hosts."), NULL },
   { LOPT_NEGTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for negative caching."), NULL },
+  { LOPT_MINTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for minimum TTL to send to clients."), NULL },
   { LOPT_MAXTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for maximum TTL to send to clients."), NULL },
   { LOPT_MAXCTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live ceiling for cache."), NULL },
   { LOPT_MINCTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live floor for cache."), NULL },
@@ -3047,6 +3050,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_MINTTL: /* --min-ttl */
     case LOPT_MAXTTL: /* --max-ttl */
     case LOPT_MINCTTL: /* --min-cache-ttl */
     case LOPT_MAXCTTL: /* --max-cache-ttl */
@@ -3058,6 +3062,8 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
 	  ret_err(gen_err);
 	else if (option == LOPT_NEGTTL)
 	  daemon->neg_ttl = (unsigned long)ttl;
+	else if (option == LOPT_MINTTL)
+	  daemon->min_ttl = (unsigned long)ttl;
 	else if (option == LOPT_MAXTTL)
 	  daemon->max_ttl = (unsigned long)ttl;
 	else if (option == LOPT_MINCTTL)
--- a/src/rfc1035.c
+++ b/src/rfc1035.c
@@ -607,6 +607,7 @@
 	  for (j = 0; j < ntohs(header->ancount); j++) 
 	    {
 	      int secflag = 0;
+	      unsigned long mttl = 0;
 	      if (!(res = extract_name(header, qlen, &p1, name, 0, 10)))
 		return 0; /* bad packet */
 	      
@@ -615,9 +616,17 @@
 	      GETLONG(attl, p1);
 	      
 	      if ((daemon->max_ttl != 0) && (attl > daemon->max_ttl) && !is_sign)
+	        {
+	          mttl = daemon->max_ttl;
+	        }
+	      if ((daemon->min_ttl != 0) && (attl < daemon->min_ttl) && !is_sign)
+	        {
+	           mttl = daemon->min_ttl;
+	        }
+	      if (mttl != 0)
 		{
 		  (p1) -= 4;
-		  PUTLONG(daemon->max_ttl, p1);
+		  PUTLONG(mttl, p1);
 		}
 	      GETSHORT(ardlen, p1);
 	      endrr = p1+ardlen;
@@ -722,6 +731,7 @@
       for (j = 0; j < ntohs(header->ancount); j++) 
 	{
 	  int secflag = 0;
+          unsigned long mttl = 0;
 	  
 	  if (!(res = extract_name(header, qlen, &p1, name, 0, 10)))
 	    return 0; /* bad packet */
@@ -730,9 +740,17 @@
 	  GETSHORT(aqclass, p1);
 	  GETLONG(attl, p1);
 	  if ((daemon->max_ttl != 0) && (attl > daemon->max_ttl) && !is_sign)
+	  {
+	    mttl = daemon->max_ttl;
+	  }
+	  if ((daemon->min_ttl != 0) && (attl < daemon->min_ttl) && !is_sign)
+	  {
+	    mttl = daemon->min_ttl;
+	  }
+	    if (mttl != 0)
 	    {
 	      (p1) -= 4;
-	      PUTLONG(daemon->max_ttl, p1);
+	     PUTLONG(mttl, p1);
 	    }
 	  GETSHORT(ardlen, p1);
 	  endrr = p1+ardlen;

patch补丁的生成

diff -urN a/src/config.h b/src/config.h >> diff.patch

min-ttl=86400 #第一次返回给客户端的ttl

min-cache-ttl=86400 #dnsmasq服务端缓存时间设定

发表评论

邮箱地址不会被公开。 必填项已用*标注