Skip to content

Commit 2f57d8d

Browse files
committed
dns_client: retry upstream server when discard IP
1 parent 5b3c47f commit 2f57d8d

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/dns_client.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,12 @@ static int _dns_client_recv(struct dns_server_info *server_info, unsigned char *
18921892
_dns_client_retry_dns_query(query);
18931893
}
18941894
} else {
1895-
query->has_result = 1;
1895+
if (ret == DNS_CLIENT_ACTION_OK) {
1896+
query->has_result = 1;
1897+
} else {
1898+
tlog(TLOG_DEBUG, "query %s result is invalid, %d", query->domain, ret);
1899+
}
1900+
18961901
if (request_num == 0) {
18971902
/* if all server replied, or done, stop query, release resource */
18981903
_dns_client_query_remove(query);
@@ -2595,9 +2600,11 @@ static int _dns_client_process_udp(struct dns_server_info *server_info, struct e
25952600
}
25962601
}
25972602

2603+
int from_port = from.ss_family == AF_INET ? ntohs(((struct sockaddr_in *)&from)->sin_port)
2604+
: ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
25982605
int latency = get_tick_count() - server_info->send_tick;
2599-
tlog(TLOG_DEBUG, "recv udp packet from %s, len: %d, ttl: %d, latency: %d",
2600-
get_host_by_addr(from_host, sizeof(from_host), (struct sockaddr *)&from), len, ttl, latency);
2606+
tlog(TLOG_DEBUG, "recv udp packet from %s:%d, len: %d, ttl: %d, latency: %d",
2607+
get_host_by_addr(from_host, sizeof(from_host), (struct sockaddr *)&from), from_port, len, ttl, latency);
26012608

26022609
/* update recv time */
26032610
time(&server_info->last_recv);

src/dns_client.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct dns_server_info;
7272
#define DNS_CLIENT_ACTION_UNDEFINE (-1)
7373
#define DNS_CLIENT_ACTION_DROP (-2)
7474
#define DNS_CLIENT_ACTION_RETRY (-3)
75+
#define DNS_CLIENT_ACTION_MAY_RETRY (-4)
7576
typedef int (*dns_client_callback)(const char *domain, dns_result_type rtype, struct dns_server_info *server_info,
7677
struct dns_packet *packet, unsigned char *inpacket, int inpacket_len,
7778
void *user_ptr);

src/dns_server.c

+9
Original file line numberDiff line numberDiff line change
@@ -4038,6 +4038,7 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
40384038
int ret = 0;
40394039
int is_skip = 0;
40404040
int has_result = 0;
4041+
int is_rcode_set = 0;
40414042

40424043
if (packet->head.rcode != DNS_RC_NOERROR && packet->head.rcode != DNS_RC_NXDOMAIN) {
40434044
if (request->rcode == DNS_RC_SERVFAIL) {
@@ -4092,6 +4093,7 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
40924093
return -1;
40934094
}
40944095
request->rcode = packet->head.rcode;
4096+
is_rcode_set = 1;
40954097
} break;
40964098
case DNS_T_AAAA: {
40974099
ret = _dns_server_process_answer_AAAA(rrs, request, domain, cname, result_flag);
@@ -4104,6 +4106,7 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
41044106
return -1;
41054107
}
41064108
request->rcode = packet->head.rcode;
4109+
is_rcode_set = 1;
41074110
} break;
41084111
case DNS_T_NS: {
41094112
char nsname[DNS_MAX_CNAME_LEN];
@@ -4130,6 +4133,7 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
41304133
continue;
41314134
}
41324135
request->rcode = packet->head.rcode;
4136+
is_rcode_set = 1;
41334137
if (request->has_ip == 0) {
41344138
request->passthrough = 1;
41354139
_dns_server_request_complete(request);
@@ -4147,6 +4151,7 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
41474151
request->has_soa = 1;
41484152
if (request->rcode != DNS_RC_NOERROR) {
41494153
request->rcode = packet->head.rcode;
4154+
is_rcode_set = 1;
41504155
}
41514156
dns_get_SOA(rrs, name, 128, &ttl, &request->soa);
41524157
tlog(TLOG_DEBUG,
@@ -4182,6 +4187,10 @@ static int _dns_server_process_answer(struct dns_request *request, const char *d
41824187
return DNS_CLIENT_ACTION_RETRY;
41834188
}
41844189

4190+
if (is_rcode_set == 0 && has_result == 1) {
4191+
return DNS_CLIENT_ACTION_MAY_RETRY;
4192+
}
4193+
41854194
return DNS_CLIENT_ACTION_OK;
41864195
}
41874196

test/cases/test-ip-rule.cc

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ TEST_F(IPRule, black_list)
120120
return smartdns::SERVER_REQUEST_SOA;
121121
}
122122

123+
usleep(800000);
123124
smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
124125
return smartdns::SERVER_REQUEST_OK;
125126
});

0 commit comments

Comments
 (0)