Skip to content

Commit

Permalink
force-qtype-SOA: support qtype range.
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Jun 5, 2023
1 parent f5c8d3c commit 6961a57
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
92 changes: 82 additions & 10 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,7 @@ static int _config_qtype_soa(void *data, int argc, char *argv[])
{
struct dns_qtype_soa_list *soa_list = NULL;
int i = 0;
int j = 0;

if (argc <= 1) {
return -1;
Expand All @@ -2393,19 +2394,81 @@ static int _config_qtype_soa(void *data, int argc, char *argv[])
char sub_arg[1024];
safe_strncpy(sub_arg, argv[i], sizeof(sub_arg));
for (char *tok = strtok(sub_arg, ","); tok; tok = strtok(NULL, ",")) {
soa_list = malloc(sizeof(*soa_list));
if (soa_list == NULL) {
tlog(TLOG_ERROR, "cannot malloc memory");
return -1;
char *dash = strstr(tok, "-");
if (dash != NULL) {
*dash = '\0';
}

long start = atol(tok);
long end = start;

if (start > MAX_QTYPE_NUM || start < 0) {
tlog(TLOG_ERROR, "invalid qtype %ld", start);
continue;
}

memset(soa_list, 0, sizeof(*soa_list));
soa_list->qtypeid = atol(tok);
if (soa_list->qtypeid == DNS_T_AAAA) {
dns_conf_force_AAAA_SOA = 1;
if (dash != NULL && *(dash + 1) != '\0') {
end = atol(dash + 1);
if (end > MAX_QTYPE_NUM) {
end = MAX_QTYPE_NUM;
}
}

for (j = start; j <= end; j++) {
/* if qtype number is larget than 2^8, then use array*/
if (dns_qtype_soa_table.qtype_num > (2 ^ 8)) {
if (dns_qtype_soa_table.qtype_array == NULL) {
dns_qtype_soa_table.qtype_array = malloc(MAX_QTYPE_NUM);
if (dns_qtype_soa_table.qtype_array == NULL) {
tlog(TLOG_ERROR, "malloc failed");
return -1;
}
memset(dns_qtype_soa_table.qtype_array, 0, MAX_QTYPE_NUM);
struct hlist_node *tmp = NULL;
unsigned long k = 0;

hash_for_each_safe(dns_qtype_soa_table.qtype, k, tmp, soa_list, node)
{
dns_qtype_soa_table.qtype_array[soa_list->qtypeid - 1] = 1;
hlist_del_init(&soa_list->node);
free(soa_list);
}
}

dns_qtype_soa_table.qtype_array[j - 1] = 1;
dns_qtype_soa_table.qtype_num++;
continue;
}

uint32_t key = hash_32_generic(j, 32);
hash_for_each_possible(dns_qtype_soa_table.qtype, soa_list, node, key)
{
if ((uint32_t)j != soa_list->qtypeid) {
continue;
}

j = end + 1;
break;
}

if (j > end) {
continue;
}

soa_list = malloc(sizeof(*soa_list));
if (soa_list == NULL) {
tlog(TLOG_ERROR, "cannot malloc memory");
return -1;
}

memset(soa_list, 0, sizeof(*soa_list));
soa_list->qtypeid = j;
if (soa_list->qtypeid == DNS_T_AAAA) {
dns_conf_force_AAAA_SOA = 1;
}
hash_add(dns_qtype_soa_table.qtype, &soa_list->node, key);
dns_qtype_soa_table.qtype_num++;
}
uint32_t key = hash_32_generic(soa_list->qtypeid, 32);
hash_add(dns_qtype_soa_table.qtype, &soa_list->node, key);
}
}

Expand All @@ -2423,6 +2486,13 @@ static void _config_qtype_soa_table_destroy(void)
hlist_del_init(&soa_list->node);
free(soa_list);
}

if (dns_qtype_soa_table.qtype_array) {
free(dns_qtype_soa_table.qtype_array);
dns_qtype_soa_table.qtype_array = NULL;
}

dns_qtype_soa_table.qtype_num = 0;
}

static void _config_domain_set_name_table_destroy(void)
Expand Down Expand Up @@ -3531,6 +3601,8 @@ static int _dns_server_load_conf_init(void)
hash_init(dns_ipset_table.ipset);
hash_init(dns_nftset_table.nftset);
hash_init(dns_qtype_soa_table.qtype);
dns_qtype_soa_table.qtype_num = 0;
dns_qtype_soa_table.qtype_array = NULL;
hash_init(dns_group_table.group);
hash_init(dns_hosts_table.hosts);
hash_init(dns_ptr_table.ptr);
Expand Down
6 changes: 5 additions & 1 deletion src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extern "C" {
#define DEFAULT_DNS_TLS_PORT 853
#define DEFAULT_DNS_HTTPS_PORT 443
#define DNS_MAX_CONF_CNAME_LEN 256
#define MAX_QTYPE_NUM 65535

#define SMARTDNS_CONF_FILE "/etc/smartdns/smartdns.conf"
#define SMARTDNS_LOG_FILE "/var/log/smartdns/smartdns.log"
#define SMARTDNS_AUDIT_FILE "/var/log/smartdns/smartdns-audit.log"
Expand Down Expand Up @@ -387,7 +389,9 @@ struct dns_qtype_soa_list {
};

struct dns_qtype_soa_table {
DECLARE_HASHTABLE(qtype, 8);
DECLARE_HASHTABLE(qtype, 10);
int qtype_num;
unsigned char *qtype_array;
};
extern struct dns_qtype_soa_table dns_qtype_soa_table;

Expand Down
16 changes: 15 additions & 1 deletion src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct dns_request {
int request_wait;
int prefetch;
int prefetch_expired_domain;

int dualstack_selection;
int dualstack_selection_force_soa;
int dualstack_selection_query;
Expand Down Expand Up @@ -4580,6 +4580,20 @@ static int _dns_server_qtype_soa(struct dns_request *request)
return -1;
}

if (dns_qtype_soa_table.qtype_array) {
if (request->qtype <= 0 || request->qtype > MAX_QTYPE_NUM) {
return -1;
}

if (dns_qtype_soa_table.qtype_array[request->qtype - 1]) {
_dns_server_reply_SOA(DNS_RC_NOERROR, request);
tlog(TLOG_DEBUG, "force qtype %d soa", request->qtype);
return 0;
}

return -1;
}

uint32_t key = hash_32_generic(request->qtype, 32);
hash_for_each_possible(dns_qtype_soa_table.qtype, soa_list, node, key)
{
Expand Down

0 comments on commit 6961a57

Please sign in to comment.