From 60a3719ec739be2cc1e11724ac049b09a75059cb Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Sat, 4 Mar 2023 11:21:48 +0800 Subject: [PATCH] dns_server: fix local ttl issue --- src/dns_conf.c | 4 ---- src/dns_server.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/dns_conf.c b/src/dns_conf.c index 307562bdbb..2d6f23530f 100644 --- a/src/dns_conf.c +++ b/src/dns_conf.c @@ -3283,10 +3283,6 @@ static int _dns_conf_load_post(void) dns_conf_rr_ttl_max = dns_conf_rr_ttl_min; } - if (dns_conf_local_ttl == 0) { - dns_conf_local_ttl = dns_conf_rr_ttl_min; - } - if (dns_resolv_file[0] == '\0') { safe_strncpy(dns_resolv_file, DNS_RESOLV_FILE, sizeof(dns_resolv_file)); } diff --git a/src/dns_server.c b/src/dns_server.c index 3e8afc6485..76c169bb53 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -52,6 +52,7 @@ #define DNS_SERVER_TMOUT_TTL (5 * 60) #define DNS_SERVER_FAIL_TTL (60) #define DNS_SERVER_SOA_TTL (30) +#define DNS_SERVER_ADDR_TTL (60) #define DNS_CONN_BUFF_SIZE 4096 #define DNS_REQUEST_MAX_TIMEOUT 950 #define DNS_PING_TIMEOUT (DNS_REQUEST_MAX_TIMEOUT) @@ -3888,6 +3889,34 @@ static int _dns_server_pre_process_rule_flags(struct dns_request *request) return 0; } +static int _dns_server_get_local_ttl(struct dns_request *request) +{ + struct dns_ttl_rule *ttl_rule; + + /* get domain rule flag */ + ttl_rule = _dns_server_get_dns_rule(request, DOMAIN_RULE_TTL); + if (ttl_rule != NULL) { + if (ttl_rule->ttl > 0) { + return ttl_rule->ttl; + } + } + + if (dns_conf_local_ttl > 0) { + return dns_conf_local_ttl; + } + + if (dns_conf_rr_ttl > 0) { + return dns_conf_rr_ttl; + } + + if (dns_conf_rr_ttl_min > 0) { + return dns_conf_rr_ttl_min; + } + + return DNS_SERVER_ADDR_TTL; +} + + static int _dns_server_process_address(struct dns_request *request) { struct dns_rule_address_IPV4 *address_ipv4 = NULL; @@ -3919,7 +3948,7 @@ static int _dns_server_process_address(struct dns_request *request) } request->rcode = DNS_RC_NOERROR; - request->ip_ttl = dns_conf_local_ttl; + request->ip_ttl = _dns_server_get_local_ttl(request); request->has_ip = 1; struct dns_server_post_context context;