test: add test case for cache and fix some issue

This commit is contained in:
Nick Peng
2023-03-20 23:41:34 +08:00
parent aa6f6fd519
commit bf68385d0e
11 changed files with 405 additions and 47 deletions

View File

@@ -3337,6 +3337,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
int send_err = 0;
int i = 0;
int total_server = 0;
int send_count = 0;
query->send_tick = get_tick_count();
@@ -3368,6 +3369,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
}
atomic_inc(&query->dns_request_sent);
send_count++;
errno = 0;
switch (server_info->type) {
case DNS_SERVER_UDP:
@@ -3402,6 +3404,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
server_info->type);
_dns_client_close_socket(server_info);
atomic_dec(&query->dns_request_sent);
send_count--;
continue;
}
@@ -3416,6 +3419,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
}
atomic_dec(&query->dns_request_sent);
send_count--;
continue;
}
time(&server_info->last_send);
@@ -3423,12 +3427,12 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
}
pthread_mutex_unlock(&client.server_list_lock);
if (atomic_read(&query->dns_request_sent) > 0) {
if (send_count > 0) {
break;
}
}
if (atomic_read(&query->dns_request_sent) <= 0) {
if (send_count <= 0) {
tlog(TLOG_WARN, "Send query to upstream server failed, total server number %d", total_server);
return -1;
}
@@ -3590,12 +3594,60 @@ static int _dns_client_query_parser_options(struct dns_query_struct *query, stru
return 0;
}
static int _dns_client_add_hashmap(struct dns_query_struct *query)
{
uint32_t key = 0;
struct hlist_node *tmp = NULL;
struct dns_query_struct *query_check = NULL;
int is_exists = 0;
int loop = 0;
while (loop ++ <= 32) {
if (RAND_bytes((unsigned char *)&query->sid, sizeof(query->sid)) != 1) {
query->sid = random();
}
key = hash_string(query->domain);
key = jhash(&query->sid, sizeof(query->sid), key);
key = jhash(&query->qtype, sizeof(query->qtype), key);
is_exists = 0;
pthread_mutex_lock(&client.domain_map_lock);
hash_for_each_possible_safe(client.domain_map, query_check, tmp, domain_node, key)
{
if (query->sid != query_check->sid) {
continue;
}
if (query->qtype != query_check->qtype) {
continue;
}
if (strncmp(query_check->domain, query->domain, DNS_MAX_CNAME_LEN) != 0) {
continue;
}
is_exists = 1;
break;
}
if (is_exists == 1) {
pthread_mutex_unlock(&client.domain_map_lock);
continue;
}
hash_add(client.domain_map, &query->domain_node, key);
pthread_mutex_unlock(&client.domain_map_lock);
break;
}
return 0;
}
int dns_client_query(const char *domain, int qtype, dns_client_callback callback, void *user_ptr,
const char *group_name, struct dns_query_options *options)
{
struct dns_query_struct *query = NULL;
int ret = 0;
uint32_t key = 0;
int unused __attribute__((unused));
if (domain == NULL) {
@@ -3620,9 +3672,6 @@ int dns_client_query(const char *domain, int qtype, dns_client_callback callback
query->qtype = qtype;
query->send_tick = 0;
query->has_result = 0;
if (RAND_bytes((unsigned char *)&query->sid, sizeof(query->sid)) != 1) {
query->sid = random();
}
query->server_group = _dns_client_get_dnsserver_group(group_name);
if (query->server_group == NULL) {
tlog(TLOG_ERROR, "get dns server group %s failed.", group_name);
@@ -3636,12 +3685,10 @@ int dns_client_query(const char *domain, int qtype, dns_client_callback callback
_dns_client_query_get(query);
/* add query to hashtable */
key = hash_string(domain);
key = jhash(&query->sid, sizeof(query->sid), key);
key = jhash(&query->qtype, sizeof(query->qtype), key);
pthread_mutex_lock(&client.domain_map_lock);
hash_add(client.domain_map, &query->domain_node, key);
pthread_mutex_unlock(&client.domain_map_lock);
if (_dns_client_add_hashmap(query) != 0) {
tlog(TLOG_ERROR, "add query to hash map failed.");
goto errout;
}
/* send query */
_dns_client_query_get(query);

View File

@@ -1748,7 +1748,7 @@ static int _dns_result_child_post(struct dns_server_post_context *context)
return 0;
}
static int _dns_request_update_ttl(struct dns_server_post_context *context)
static int _dns_request_update_id_ttl(struct dns_server_post_context *context)
{
int ttl = context->reply_ttl;
struct dns_request *request = context->request;
@@ -1767,17 +1767,22 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context)
}
}
if (ttl > 0) {
struct dns_update_param param;
param.id = request->id;
param.cname_ttl = ttl;
param.ip_ttl = ttl;
if (dns_packet_update(context->inpacket, context->inpacket_len, &param) != 0) {
tlog(TLOG_ERROR, "update packet info failed.");
return -1;
if (ttl == 0) {
ttl = request->ip_ttl;
if (ttl == 0) {
ttl = _dns_server_get_conf_ttl(request, ttl);
}
}
struct dns_update_param param;
param.id = request->id;
param.cname_ttl = ttl;
param.ip_ttl = ttl;
if (dns_packet_update(context->inpacket, context->inpacket_len, &param) != 0) {
tlog(TLOG_ERROR, "update packet info failed.");
return -1;
}
return 0;
}
@@ -1836,7 +1841,7 @@ static int _dns_request_post(struct dns_server_post_context *context)
return 0;
}
ret = _dns_request_update_ttl(context);
ret = _dns_request_update_id_ttl(context);
if (ret != 0) {
tlog(TLOG_ERROR, "update packet ttl failed.");
return -1;
@@ -2765,7 +2770,7 @@ static int _dns_server_process_answer_A(struct dns_rrs *rrs, struct dns_request
return -2;
}
if (request->has_ip == 0) {
if (atomic_read(&request->ip_map_num) == 0) {
request->has_ip = 1;
memcpy(request->ip_addr, addr, DNS_RR_A_LEN);
request->ip_ttl = _dns_server_get_conf_ttl(request, ttl);
@@ -2842,7 +2847,7 @@ static int _dns_server_process_answer_AAAA(struct dns_rrs *rrs, struct dns_reque
return -2;
}
if (request->has_ip == 0) {
if (atomic_read(&request->ip_map_num) == 0) {
request->has_ip = 1;
memcpy(request->ip_addr, addr, DNS_RR_AAAA_LEN);
request->ip_ttl = _dns_server_get_conf_ttl(request, ttl);
@@ -3113,6 +3118,9 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const
char tmpname[DNS_MAX_CNAME_LEN];
char tmpbuf[DNS_MAX_CNAME_LEN];
dns_get_CNAME(rrs, tmpname, DNS_MAX_CNAME_LEN, &ttl, tmpbuf, DNS_MAX_CNAME_LEN);
if (request->ip_ttl == 0) {
request->ip_ttl = ttl;
}
}
break;
}
@@ -3272,7 +3280,7 @@ static int _dns_server_reply_passthrough(struct dns_server_post_context *context
char clientip[DNS_MAX_CNAME_LEN] = {0};
/* When passthrough, modify the id to be the id of the client request. */
int ret = _dns_request_update_ttl(context);
int ret = _dns_request_update_id_ttl(context);
if (ret != 0) {
tlog(TLOG_ERROR, "update packet ttl failed.");
return -1;
@@ -3294,14 +3302,12 @@ static void _dns_server_query_end(struct dns_request *request)
pthread_mutex_lock(&request->ip_map_lock);
ip_num = atomic_read(&request->ip_map_num);
/* if adblock ip address exist */
ip_num += atomic_read(&request->adblock) == 0 ? 0 : 1;
request_wait = request->request_wait;
request->request_wait--;
pthread_mutex_unlock(&request->ip_map_lock);
/* Not need to wait check result if only has one ip address */
if (ip_num == 1 && request_wait == 1) {
if (ip_num <= 1 && request_wait == 1) {
if (request->dualstack_selection_query == 1) {
if ((dns_conf_ipset_no_speed.ipv4_enable || dns_conf_nftset_no_speed.ip6_enable ||
dns_conf_ipset_no_speed.ipv6_enable || dns_conf_nftset_no_speed.ip6_enable) &&
@@ -3328,7 +3334,8 @@ static int dns_server_dualstack_callback(const char *domain, dns_rtcode_t rtcode
unsigned int ping_time, void *user_ptr)
{
struct dns_request *request = (struct dns_request *)user_ptr;
tlog(TLOG_DEBUG, "dualstack result: domain: %s, ip: %s, type: %d, ping: %d, rcode: %d", domain, ip, addr_type, ping_time, rtcode);
tlog(TLOG_DEBUG, "dualstack result: domain: %s, ip: %s, type: %d, ping: %d, rcode: %d", domain, ip, addr_type,
ping_time, rtcode);
if (request == NULL) {
return -1;
}
@@ -3426,6 +3433,7 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype
context.do_ipset = 1;
context.reply_ttl = _dns_server_get_reply_ttl(request, ttl);
context.cache_ttl = _dns_server_get_conf_ttl(request, ttl);
request->ip_ttl = context.cache_ttl;
context.no_check_add_ip = 1;
_dns_server_reply_passthrough(&context);
request->cname[0] = 0;
@@ -5341,7 +5349,8 @@ errout:
return ret;
}
static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct epoll_event *event, unsigned long now)
static int _dns_server_process_udp_one(struct dns_server_conn_udp *udpconn, struct epoll_event *event,
unsigned long now)
{
int len = 0;
unsigned char inpacket[DNS_IN_PACKSIZE];
@@ -5366,6 +5375,9 @@ static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct e
len = recvmsg(udpconn->head.fd, &msg, MSG_DONTWAIT);
if (len < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return -2;
}
tlog(TLOG_ERROR, "recvfrom failed, %s\n", strerror(errno));
return -1;
}
@@ -5386,6 +5398,25 @@ static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct e
return _dns_server_recv(&udpconn->head, inpacket, len, &local, local_len, &from, from_len);
}
static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct epoll_event *event, unsigned long now)
{
int count = 0;
while (count < 32) {
int ret = _dns_server_process_udp_one(udpconn, event, now);
if (ret != 0) {
if (ret == -2) {
return 0;
}
return ret;
}
count++;
}
return 0;
}
static void _dns_server_client_touch(struct dns_server_conn_head *conn)
{
time(&conn->last_request_time);