Skip to content

Commit

Permalink
ip-alias: add option -no-ipalias for domain-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Aug 29, 2023
1 parent 45e3455 commit 2ae2cf1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions etc/smartdns/smartdns.conf
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ log-level info
# [-d] -dualstack-ip-selection [yes|no]: same as dualstack-ip-selection option
# -no-serve-expired: ignore expired domain
# -delete: delete domain rule
# -no-ip-alias: ignore ip alias
# -no-cache: ignore cache

# collection of domains
# the domain-set can be used with /domain/ for address, nameserver, ipset, etc.
Expand Down
14 changes: 14 additions & 0 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,11 @@ static int _conf_domain_rule_no_cache(const char *domain)
return _config_domain_rule_flag_set(domain, DOMAIN_FLAG_NO_CACHE, 0);
}

static int _conf_domain_rule_no_ipalias(const char *domain)
{
return _config_domain_rule_flag_set(domain, DOMAIN_FLAG_NO_IPALIAS, 0);
}

static int _conf_domain_rules(void *data, int argc, char *argv[])
{
int opt = 0;
Expand All @@ -3056,6 +3061,7 @@ static int _conf_domain_rules(void *data, int argc, char *argv[])
{"no-serve-expired", no_argument, NULL, 254},
{"delete", no_argument, NULL, 255},
{"no-cache", no_argument, NULL, 256},
{"no-ip-alias", no_argument, NULL, 257},
{NULL, no_argument, NULL, 0}
};
/* clang-format on */
Expand Down Expand Up @@ -3211,6 +3217,14 @@ static int _conf_domain_rules(void *data, int argc, char *argv[])

break;
}
case 257: {
if (_conf_domain_rule_no_ipalias(domain) != 0) {
tlog(TLOG_ERROR, "set no-ipalias rule failed.");
goto errout;
}

break;
}
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ typedef enum {
#define DOMAIN_FLAG_NO_SERVE_EXPIRED (1 << 15)
#define DOMAIN_FLAG_CNAME_IGN (1 << 16)
#define DOMAIN_FLAG_NO_CACHE (1 << 17)
#define DOMAIN_FLAG_NO_IPALIAS (1 << 18)

#define SERVER_FLAG_EXCLUDE_DEFAULT (1 << 0)
#define SERVER_FLAG_HITCHHIKING (1 << 1)
Expand Down
9 changes: 7 additions & 2 deletions src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ struct dns_request {
int no_select_possible_ip;
int no_cache_cname;
int no_cache;
int no_ipalias;

int has_cname_loop;
};
Expand Down Expand Up @@ -2861,7 +2862,7 @@ static int _dns_server_process_ip_rule(struct dns_request *request, unsigned cha
return ret;
}

if (rule->ip_alias_enable && alias != NULL) {
if (rule->ip_alias_enable && alias != NULL && request->no_ipalias == 0) {
*alias = rule->ip_alias;
if (alias == NULL) {
return 0;
Expand All @@ -2871,7 +2872,7 @@ static int _dns_server_process_ip_rule(struct dns_request *request, unsigned cha
return -1;
}

return 0;
return -1;
}

static int _dns_server_is_adblock_ipv6(const unsigned char addr[16])
Expand Down Expand Up @@ -4137,6 +4138,10 @@ static int _dns_server_pre_process_rule_flags(struct dns_request *request)
request->no_cache = 1;
}

if (flags & DOMAIN_FLAG_NO_IPALIAS) {
request->no_ipalias = 1;
}

if (flags & DOMAIN_FLAG_ADDR_IGN) {
/* ignore this domain */
goto skip_soa_out;
Expand Down
70 changes: 70 additions & 0 deletions test/cases/test-ip-alias.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,73 @@ cache-persist no)""");
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
EXPECT_EQ(client.GetAnswer()[0].GetData(), "ffff::1");
}

TEST(IPAlias, no_ip_alias)
{
smartdns::MockServer server_upstream;
smartdns::Server server;

server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
std::string domain = request->domain;
if (request->domain.length() == 0) {
return smartdns::SERVER_REQUEST_ERROR;
}

if (request->qtype == DNS_T_A) {
unsigned char addr[][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[0]);
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[1]);
dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[2]);
} else if (request->qtype == DNS_T_AAAA) {
unsigned char addr[][16] = {{1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{10, 11, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[0]);
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[1]);
dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr[2]);
} else {
return smartdns::SERVER_REQUEST_ERROR;
}

request->response_packet->head.rcode = DNS_RC_NOERROR;
return smartdns::SERVER_REQUEST_OK;
});

server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 100);
server.MockPing(PING_TYPE_ICMP, "5.6.7.8", 60, 110);
server.MockPing(PING_TYPE_ICMP, "9.10.11.12", 60, 140);
server.MockPing(PING_TYPE_ICMP, "10.10.10.10", 60, 120);
server.MockPing(PING_TYPE_ICMP, "11.11.11.11", 60, 150);
server.MockPing(PING_TYPE_ICMP, "0102:0304:0500::", 60, 100);
server.MockPing(PING_TYPE_ICMP, "0506:0708:0900::", 60, 110);
server.MockPing(PING_TYPE_ICMP, "0a0b:0c0d:0e00::", 60, 140);
server.MockPing(PING_TYPE_ICMP, "ffff::1", 60, 120);
server.MockPing(PING_TYPE_ICMP, "ffff::2", 60, 150);

server.Start(R"""(bind [::]:60053
server 127.0.0.1:61053
log-num 0
log-console yes
log-level debug
dualstack-ip-selection no
ip-alias 1.2.3.4 10.10.10.10
ip-alias 5.6.7.8/32 11.11.11.11
ip-alias 0102::/16 ffff::1
ip-alias 0506::/16 ffff::2
domain-rules /a.com/ -no-ip-alias
cache-persist no)""");
smartdns::Client client;
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].GetName(), "a.com");
EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");

ASSERT_TRUE(client.Query("a.com AAAA", 60053));
std::cout << client.GetResult() << std::endl;
ASSERT_EQ(client.GetAnswerNum(), 1);
EXPECT_EQ(client.GetStatus(), "NOERROR");
EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
EXPECT_EQ(client.GetAnswer()[0].GetData(), "102:304:500::");
}

0 comments on commit 2ae2cf1

Please sign in to comment.