Skip to content

Commit

Permalink
Merge pull request #2 from pymumu/master
Browse files Browse the repository at this point in the history
Fork Sync
  • Loading branch information
PikuZheng authored Jun 28, 2022
2 parents 9a74938 + e343626 commit 8cc1bd9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct dns_conf_address_rule dns_conf_address_rule;

/* dual-stack selection */
int dns_conf_dualstack_ip_selection = 1;
int dns_conf_dualstack_ip_allow_force_AAAA;
int dns_conf_dualstack_ip_selection_threshold = 15;

/* TTL */
Expand Down Expand Up @@ -1878,6 +1879,7 @@ static struct config_item _config_item[] = {
CONF_INT("serve-expired-reply-ttl", &dns_conf_serve_expired_reply_ttl, 0, CONF_INT_MAX),
CONF_INT("serve-expired-prefetch-time", &dns_conf_serve_expired_prefetch_time, 0, CONF_INT_MAX),
CONF_YESNO("dualstack-ip-selection", &dns_conf_dualstack_ip_selection),
CONF_YESNO("dualstack-ip-allow-force-AAAA", &dns_conf_dualstack_ip_allow_force_AAAA),
CONF_INT("dualstack-ip-selection-threshold", &dns_conf_dualstack_ip_selection_threshold, 0, 1000),
CONF_CUSTOM("log-level", _config_log_level, NULL),
CONF_STRING("log-file", (char *)dns_conf_log_file, DNS_MAX_PATH),
Expand Down
1 change: 1 addition & 0 deletions src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ extern art_tree dns_conf_domain_rule;
extern struct dns_conf_address_rule dns_conf_address_rule;

extern int dns_conf_dualstack_ip_selection;
extern int dns_conf_dualstack_ip_allow_force_AAAA;
extern int dns_conf_dualstack_ip_selection_threshold;

extern int dns_conf_max_reply_ip_num;
Expand Down
27 changes: 21 additions & 6 deletions src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define RECV_ERROR_OK 0
#define RECV_ERROR_FAIL -1
#define RECV_ERROR_CLOSE -2
#define RECV_ERROR_INVALID_PACKET -3

typedef enum {
DNS_CONN_TYPE_UDP_SERVER = 0,
Expand Down Expand Up @@ -1499,6 +1500,10 @@ static int _dns_server_force_dualstack(struct dns_request *request)
}
}

if (request->qtype == DNS_T_A && dns_conf_dualstack_ip_allow_force_AAAA == 0) {
return -1;
}

/* if ipv4 is fasting than ipv6, add ipv4 to cache, and return SOA for AAAA request */
tlog(TLOG_INFO, "result: %s, qtype: %d, force %s perfered, id: %d, time1: %d, time2: %d", request->domain,
request->qtype, request->qtype == DNS_T_AAAA ? "IPv4" : "IPv6", request->id, request->ping_time,
Expand Down Expand Up @@ -3448,6 +3453,10 @@ static int _dns_server_process_cache(struct dns_request *request)
goto out;
}

if (request->qtype == DNS_T_A && dns_conf_dualstack_ip_allow_force_AAAA == 0) {
goto reply_cache;
}

if (request->dualstack_selection) {
int dualstack_qtype;
if (request->qtype == DNS_T_A) {
Expand Down Expand Up @@ -3479,6 +3488,7 @@ static int _dns_server_process_cache(struct dns_request *request)
}
}

reply_cache:
if (dns_cache_is_soa(dns_cache)) {
if (dns_cache_get_ttl(dns_cache) > 0) {
ret = _dns_server_process_cache_packet(request, dns_cache);
Expand Down Expand Up @@ -4000,11 +4010,12 @@ static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *in
struct dns_request *request = NULL;

/* decode packet */
tlog(TLOG_DEBUG, "recv query packet from %s, len = %d",
gethost_by_addr(name, sizeof(name), (struct sockaddr *)from), inpacket_len);
tlog(TLOG_DEBUG, "recv query packet from %s, len = %d, type = %d",
gethost_by_addr(name, sizeof(name), (struct sockaddr *)from), inpacket_len, conn->type);
decode_len = dns_decode(packet, DNS_PACKSIZE, inpacket, inpacket_len);
if (decode_len < 0) {
tlog(TLOG_DEBUG, "decode failed.\n");
ret = RECV_ERROR_INVALID_PACKET;
goto errout;
}

Expand Down Expand Up @@ -4306,10 +4317,10 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client
request_data = (unsigned char *)(tcpclient->recvbuff.buf + proceed_len + sizeof(unsigned short));

/* process one record */
if (_dns_server_recv(&tcpclient->head, request_data, request_len, &tcpclient->localaddr,
tcpclient->localaddr_len, &tcpclient->addr, tcpclient->addr_len) != 0) {
tlog(TLOG_ERROR, "process tcp request failed.");
return RECV_ERROR_FAIL;
ret = _dns_server_recv(&tcpclient->head, request_data, request_len, &tcpclient->localaddr,
tcpclient->localaddr_len, &tcpclient->addr, tcpclient->addr_len);
if (ret != 0) {
return ret;
}

proceed_len += sizeof(unsigned short) + request_len;
Expand Down Expand Up @@ -4446,6 +4457,10 @@ static int _dns_server_process(struct dns_server_conn_head *conn, struct epoll_e
}
_dns_server_conn_release(conn);

if (ret == RECV_ERROR_INVALID_PACKET) {
ret = 0;
}

return ret;
}

Expand Down
22 changes: 22 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,25 @@ void print_stack(void)
tlog(TLOG_FATAL, "#%.2d: %p %s from %s+%p", idx + 1, addr, symbol, info.dli_fname, offset);
}
}

int write_file(const char *filename, void *data, int data_len)
{
int fd = open(filename, O_WRONLY|O_CREAT, 0644);
if (fd < 0) {
return -1;
}

int len = write(fd, data, data_len);
if (len < 0) {
goto errout;
}

close(fd);
return 0;
errout:
if (fd > 0) {
close(fd);
}

return -1;
}
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ uint64_t get_free_space(const char *path);

void print_stack(void);

int write_file(const char *filename, void *data, int data_len);

#ifdef __cplusplus
}
#endif /*__cplusplus */
Expand Down

0 comments on commit 8cc1bd9

Please sign in to comment.