Skip to content

Commit bd425e6

Browse files
jukkarAnas Nashif
authored andcommitted
net: dns: Do not resolve numeric IP address
If the query name is already in numeric format, there is no need to send the query string to DNS server as we can just convert it ourselves. Jira: ZEP-2562 Signed-off-by: Jukka Rissanen <[email protected]>
1 parent a71164e commit bd425e6

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

subsys/net/lib/dns/resolve.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
610610
{
611611
struct net_buf *dns_data;
612612
struct net_buf *dns_qname = NULL;
613+
struct sockaddr addr;
613614
int ret, i, j = 0;
614615
int failure = 0;
615616

@@ -623,6 +624,36 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
623624
return -EINVAL;
624625
}
625626

627+
ret = net_ipaddr_parse(query, strlen(query), &addr);
628+
if (ret) {
629+
/* The query name was already in numeric form, no
630+
* need to continue further.
631+
*/
632+
struct dns_addrinfo info = { 0 };
633+
634+
if (type == DNS_QUERY_TYPE_A) {
635+
memcpy(net_sin(&info.ai_addr), net_sin(&addr),
636+
sizeof(struct sockaddr_in));
637+
info.ai_family = AF_INET;
638+
info.ai_addr.sa_family = AF_INET;
639+
info.ai_addrlen = sizeof(struct sockaddr_in);
640+
} else if (type == DNS_QUERY_TYPE_AAAA) {
641+
memcpy(net_sin6(&info.ai_addr), net_sin6(&addr),
642+
sizeof(struct sockaddr_in6));
643+
info.ai_family = AF_INET6;
644+
info.ai_addr.sa_family = AF_INET6;
645+
info.ai_addrlen = sizeof(struct sockaddr_in6);
646+
} else {
647+
goto try_resolve;
648+
}
649+
650+
cb(DNS_EAI_INPROGRESS, &info, user_data);
651+
cb(DNS_EAI_ALLDONE, NULL, user_data);
652+
653+
return 0;
654+
}
655+
656+
try_resolve:
626657
i = get_cb_slot(ctx);
627658
if (i < 0) {
628659
return -EAGAIN;

tests/net/lib/dns_resolve/prj.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CONFIG_DNS_SERVER3="192.0.2.2:5353"
1616
CONFIG_DNS_SERVER4="[2001:db8::2]:5353"
1717

1818
CONFIG_NET_LOG=y
19-
CONFIG_SYS_LOG_NET_LEVEL=4
19+
CONFIG_SYS_LOG_NET_LEVEL=2
2020
CONFIG_SYS_LOG_SHOW_COLOR=y
2121
#CONFIG_NET_DEBUG_DNS_RESOLVE=y
2222

@@ -29,3 +29,5 @@ CONFIG_NET_ARP=n
2929

3030
CONFIG_PRINTK=y
3131
CONFIG_ZTEST=y
32+
33+
CONFIG_MAIN_STACK_SIZE=1344

tests/net/lib/dns_resolve/src/main.c

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@
3030

3131
#define NAME4 "4.zephyr.test"
3232
#define NAME6 "6.zephyr.test"
33+
#define NAME_IPV4 "192.0.2.1"
34+
#define NAME_IPV6 "2001:db8::1"
3335

3436
#define DNS_TIMEOUT 500 /* ms */
3537

3638
#if defined(CONFIG_NET_IPV6)
3739
/* Interface 1 addresses */
3840
static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
3941
0, 0, 0, 0, 0, 0, 0, 0x1 } } };
42+
static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
43+
0, 0, 0, 0, 0, 0, 0, 0x1 } } };
4044

4145
/* Extra address is assigned to ll_addr */
4246
static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
@@ -637,6 +641,101 @@ static void dns_query_ipv6(void)
637641
}
638642
}
639643

644+
struct expected_addr_status {
645+
struct sockaddr addr;
646+
int status1;
647+
int status2;
648+
const char *caller;
649+
};
650+
651+
void dns_result_numeric_cb(enum dns_resolve_status status,
652+
struct dns_addrinfo *info,
653+
void *user_data)
654+
{
655+
struct expected_addr_status *expected = user_data;
656+
657+
if (status != expected->status1 && status != expected->status2) {
658+
DBG("Result status %d\n", status);
659+
DBG("Expected status1 %d\n", expected->status1);
660+
DBG("Expected status2 %d\n", expected->status2);
661+
DBG("Caller %s\n", expected->caller);
662+
663+
zassert_true(false, "Invalid status");
664+
}
665+
666+
if (info && info->ai_family == AF_INET) {
667+
if (net_ipv4_addr_cmp(&net_sin(&info->ai_addr)->sin_addr,
668+
&my_addr2) != true) {
669+
zassert_true(false, "IPv4 address does not match");
670+
}
671+
}
672+
673+
if (info && info->ai_family == AF_INET6) {
674+
if (net_ipv6_addr_cmp(&net_sin6(&info->ai_addr)->sin6_addr,
675+
&my_addr3) != true) {
676+
zassert_true(false, "IPv6 address does not match");
677+
}
678+
}
679+
680+
k_sem_give(&wait_data2);
681+
}
682+
683+
static void dns_query_ipv4_numeric(void)
684+
{
685+
struct expected_addr_status status = {
686+
.status1 = DNS_EAI_INPROGRESS,
687+
.status2 = DNS_EAI_ALLDONE,
688+
.caller = __func__,
689+
};
690+
int ret;
691+
692+
timeout_query = false;
693+
694+
ret = dns_get_addr_info(NAME_IPV4,
695+
DNS_QUERY_TYPE_A,
696+
&current_dns_id,
697+
dns_result_numeric_cb,
698+
&status,
699+
DNS_TIMEOUT);
700+
zassert_equal(ret, 0, "Cannot create IPv4 numeric query");
701+
702+
DBG("Query id %u\n", current_dns_id);
703+
704+
k_yield(); /* mandatory so that net_if send func gets to run */
705+
706+
if (k_sem_take(&wait_data2, WAIT_TIME)) {
707+
zassert_true(false, "Timeout while waiting data");
708+
}
709+
}
710+
711+
static void dns_query_ipv6_numeric(void)
712+
{
713+
struct expected_addr_status status = {
714+
.status1 = DNS_EAI_INPROGRESS,
715+
.status2 = DNS_EAI_ALLDONE,
716+
.caller = __func__,
717+
};
718+
int ret;
719+
720+
timeout_query = false;
721+
722+
ret = dns_get_addr_info(NAME_IPV6,
723+
DNS_QUERY_TYPE_AAAA,
724+
&current_dns_id,
725+
dns_result_numeric_cb,
726+
&status,
727+
DNS_TIMEOUT);
728+
zassert_equal(ret, 0, "Cannot create IPv6 query");
729+
730+
DBG("Query id %u\n", current_dns_id);
731+
732+
k_yield(); /* mandatory so that net_if send func gets to run */
733+
734+
if (k_sem_take(&wait_data2, WAIT_TIME)) {
735+
zassert_true(false, "Timeout while waiting data");
736+
}
737+
}
738+
640739
void test_main(void)
641740
{
642741
ztest_test_suite(dns_tests,
@@ -654,7 +753,9 @@ void test_main(void)
654753
ztest_unit_test(dns_query_ipv4_cancel),
655754
ztest_unit_test(dns_query_ipv6_cancel),
656755
ztest_unit_test(dns_query_ipv4),
657-
ztest_unit_test(dns_query_ipv6));
756+
ztest_unit_test(dns_query_ipv6),
757+
ztest_unit_test(dns_query_ipv4_numeric),
758+
ztest_unit_test(dns_query_ipv6_numeric));
658759

659760
ztest_run_test_suite(dns_tests);
660761
}

0 commit comments

Comments
 (0)