Skip to content

Commit

Permalink
test: add some test and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Mar 15, 2023
1 parent a14d4a0 commit 2e3e03b
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 87 deletions.
63 changes: 38 additions & 25 deletions src/dns_server.c
Expand Up @@ -38,7 +38,11 @@
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -48,10 +52,6 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/x509.h>

#define DNS_MAX_EVENTS 256
#define IPV6_READY_CHECK_TIME 180
Expand Down Expand Up @@ -372,6 +372,21 @@ static int _dns_server_has_bind_flag(struct dns_request *request, uint32_t flag)
return -1;
}

static int _dns_server_get_reply_ttl(struct dns_request *request, int ttl)
{
int reply_ttl = ttl;

if ((request->passthrough == 0 || request->passthrough == 2) && dns_conf_cachesize > 0 &&
request->check_order_list->orders[0].type != DOMAIN_CHECK_NONE) {
reply_ttl = dns_conf_serve_expired_reply_ttl;
if (reply_ttl < 2) {
reply_ttl = 2;
}
}

return reply_ttl;
}

static int _dns_server_get_conf_ttl(struct dns_request *request, int ttl)
{
int rr_ttl = dns_conf_rr_ttl;
Expand Down Expand Up @@ -1745,7 +1760,6 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context)

if (ttl > dns_conf_rr_ttl_reply_max) {
ttl %= dns_conf_rr_ttl_reply_max;

}

if (ttl == 0) {
Expand All @@ -1770,6 +1784,7 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context)
static int _dns_request_post(struct dns_server_post_context *context)
{
struct dns_request *request = context->request;
char clientip[DNS_MAX_CNAME_LEN] = {0};
int ret = 0;

tlog(TLOG_DEBUG, "reply %s qtype: %d, rcode: %d, reply: %d", request->domain, request->qtype,
Expand Down Expand Up @@ -1827,6 +1842,10 @@ static int _dns_request_post(struct dns_server_post_context *context)
return -1;
}

tlog(TLOG_INFO, "reply %s to %s, qtype: %d, id: %d, group: %s", request->domain,
get_host_by_addr(clientip, sizeof(clientip), (struct sockaddr *)&request->addr), request->qtype, request->id,
request->dns_group_name[0] != '\0' ? request->dns_group_name : "default");

ret = _dns_reply_inpacket(request, context->inpacket, context->inpacket_len);
if (ret != 0) {
tlog(TLOG_WARN, "reply raw packet to client failed.");
Expand Down Expand Up @@ -1983,7 +2002,7 @@ static int _dns_server_force_dualstack(struct dns_request *request)
static int _dns_server_request_complete_with_all_IPs(struct dns_request *request, int with_all_ips)
{
int ttl = 0;
int reply_ttl = 0;
struct dns_server_post_context context;

if (request->rcode == DNS_RC_SERVFAIL || request->rcode == DNS_RC_NXDOMAIN) {
ttl = DNS_SERVER_FAIL_TTL;
Expand Down Expand Up @@ -2029,23 +2048,13 @@ static int _dns_server_request_complete_with_all_IPs(struct dns_request *request
}

out:
reply_ttl = ttl;
if (request->passthrough == 0 && dns_conf_cachesize > 0 &&
request->check_order_list->orders[0].type != DOMAIN_CHECK_NONE) {
reply_ttl = dns_conf_serve_expired_reply_ttl;
if (reply_ttl < 2) {
reply_ttl = 2;
}
}

struct dns_server_post_context context;
_dns_server_post_context_init(&context, request);
context.do_cache = 1;
context.do_ipset = 1;
context.do_force_soa = request->dualstack_selection_force_soa;
context.do_audit = 1;
context.do_reply = 1;
context.reply_ttl = reply_ttl;
context.reply_ttl = _dns_server_get_reply_ttl(request, ttl);
context.skip_notify_count = 1;
context.select_all_best_ip = with_all_ips;
context.no_release_parent = 1;
Expand Down Expand Up @@ -3042,8 +3051,7 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const
/* Ad blocking result */
if (addr[0] == 0 || addr[0] == 127) {
/* If half of the servers return the same result, then ignore this address */
if (atomic_inc_return(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) {
request->rcode = DNS_RC_NOERROR;
if (atomic_read(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) {
_dns_server_request_release(request);
return 0;
}
Expand Down Expand Up @@ -3087,8 +3095,7 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const
/* Ad blocking result */
if (_dns_server_is_adblock_ipv6(addr) == 0) {
/* If half of the servers return the same result, then ignore this address */
if (atomic_inc_return(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) {
request->rcode = DNS_RC_NOERROR;
if (atomic_read(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) {
_dns_server_request_release(request);
return 0;
}
Expand Down Expand Up @@ -3262,13 +3269,19 @@ static int _dns_server_reply_passthrough(struct dns_server_post_context *context
_dns_result_child_post(context);

if (request->conn && context->do_reply == 1) {
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);
if (ret != 0) {
tlog(TLOG_ERROR, "update packet ttl failed.");
return -1;
}
_dns_reply_inpacket(request, context->inpacket, context->inpacket_len);

tlog(TLOG_INFO, "reply %s to %s, qtype: %d, id: %d, group: %s", request->domain,
get_host_by_addr(clientip, sizeof(clientip), (struct sockaddr *)&request->addr), request->qtype,
request->id, request->dns_group_name[0] != '\0' ? request->dns_group_name : "default");
}

return _dns_server_reply_all_pending_list(request, context);
Expand Down Expand Up @@ -3379,7 +3392,7 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype
}

if (rtype == DNS_QUERY_RESULT) {
tlog(TLOG_DEBUG, "query result from server %s: %d, type: %d", dns_client_get_server_ip(server_info),
tlog(TLOG_DEBUG, "query result from server %s:%d, type: %d", dns_client_get_server_ip(server_info),
dns_client_get_server_port(server_info), dns_client_get_server_type(server_info));

if (request->passthrough == 1 && atomic_read(&request->notified) == 0) {
Expand Down Expand Up @@ -3411,8 +3424,8 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype
context.do_audit = 1;
context.do_reply = 1;
context.do_ipset = 1;
context.reply_ttl = 2;
context.cache_ttl = 2;
context.reply_ttl = _dns_server_get_reply_ttl(request, ttl);
context.cache_ttl = _dns_server_get_conf_ttl(request, ttl);
context.no_check_add_ip = 1;
_dns_server_reply_passthrough(&context);
request->cname[0] = 0;
Expand Down Expand Up @@ -5225,7 +5238,7 @@ static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *in
goto errout;
}

tlog(TLOG_INFO, "query server %s from %s, qtype: %d\n", request->domain, name, request->qtype);
tlog(TLOG_INFO, "query %s from %s, qtype: %d, id: %d\n", request->domain, name, request->qtype, request->id);

ret = _dns_server_do_query(request, 1);
if (ret != 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/fast_ping.h
Expand Up @@ -21,9 +21,9 @@

#include <netdb.h>
#include <sys/time.h>
#ifdef __cpluscplus
#ifdef __cplusplus
extern "C" {
#endif
#endif /*__cplusplus */

typedef enum {
PING_TYPE_ICMP = 1,
Expand Down Expand Up @@ -54,8 +54,8 @@ int fast_ping_init(void);

void fast_ping_exit(void);

#ifdef __cpluscplus
#ifdef __cplusplus
}
#endif
#endif /*__cplusplus */

#endif // !FAST_PING_H
24 changes: 17 additions & 7 deletions src/tlog.c
Expand Up @@ -628,30 +628,40 @@ int tlog_printf(struct tlog_log *log, const char *format, ...)
return len;
}

static int _tlog_early_print(const char *format, va_list ap)
static int _tlog_early_print(tlog_level level, const char *file, int line, const char *func, const char *format, va_list ap)
{
char log_buf[TLOG_MAX_LINE_LEN];
size_t len = 0;
size_t out_len = 0;
struct tlog_time cur_time;
int unused __attribute__((unused));

if (tlog_disable_early_print) {
return 0;
}

len = vsnprintf(log_buf, sizeof(log_buf), format, ap);
if (_tlog_gettime(&cur_time) != 0) {
return -1;
}

len = snprintf(log_buf, sizeof(log_buf), "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
cur_time.year, cur_time.mon, cur_time.mday, cur_time.hour, cur_time.min, cur_time.sec, cur_time.usec / 1000,
tlog_get_level_string(level), file, line);
out_len = len;
len = vsnprintf(log_buf + out_len, sizeof(log_buf) - out_len - 1, format, ap);
out_len += len;
if (len <= 0) {
return -1;
} else if (len >= sizeof(log_buf)) {
out_len = sizeof(log_buf);
} else if (len >= sizeof(log_buf) - 1) {
out_len = sizeof(log_buf) - 1;
}

unused = write(STDOUT_FILENO, log_buf, out_len);
if (log_buf[out_len - 1] != '\n') {
unused = write(STDOUT_FILENO, "\n", 1);
log_buf[out_len] = '\n';
out_len++;
}

unused = write(STDOUT_FILENO, log_buf, out_len);
return len;
}

Expand All @@ -664,7 +674,7 @@ int tlog_vext(tlog_level level, const char *file, int line, const char *func, vo
}

if (tlog.root == NULL) {
return _tlog_early_print(format, ap);
return _tlog_early_print(level, file, line, func, format, ap);
}

if (unlikely(tlog.root->logsize <= 0)) {
Expand Down
73 changes: 73 additions & 0 deletions test/cases/test-bind.cc
@@ -0,0 +1,73 @@
#include "client.h"
#include "include/utils.h"
#include "server.h"
#include "gtest/gtest.h"

TEST(Bind, tls)
{
Defer
{
unlink("/tmp/smartdns-cert.pem");
unlink("/tmp/smartdns-key.pem");
};

smartdns::Server server_wrap;
smartdns::Server server;

server.Start(R"""(bind [::]:61053
server-tls 127.0.0.1:60053 -no-check-certificate
log-num 0
log-console yes
log-level debug
cache-persist no)""");
server_wrap.Start(R"""(bind-tls [::]:60053
address /example.com/1.2.3.4
log-num 0
log-console yes
log-level debug
cache-persist no)""");
smartdns::Client client;
ASSERT_TRUE(client.Query("example.com", 61053));
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
}

TEST(Bind, udp_tcp)
{
smartdns::MockServer server_upstream;
smartdns::MockServer server_upstream2;
smartdns::Server server;

server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
unsigned char addr[4] = {1, 2, 3, 4};
dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
request->response_packet->head.rcode = DNS_RC_NOERROR;
return true;
});

server.Start(R"""(
bind [::]:60053
bind-tcp [::]:60053
server 127.0.0.1:61053
log-num 0
log-console yes
log-level debug
cache-persist no)""");
smartdns::Client client;
ASSERT_TRUE(client.Query("a.com +tcp", 60053));
std::cout << client.GetResult() << std::endl;
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");

ASSERT_TRUE(client.Query("a.com", 60053));
std::cout << client.GetResult() << std::endl;
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 611);
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");

}

0 comments on commit 2e3e03b

Please sign in to comment.