Skip to content

Commit 92ce3bc

Browse files
jukkarAnas Nashif
authored andcommitted
net: dns: Do not resolve IPv6 address if IPv6 is disabled
If IPv6 is disabled, then it is useless to try to resolve IPv6 address because "struct sockaddr" does not have enough space to store IPv6 address. Fixes #1487 Signed-off-by: Jukka Rissanen <[email protected]>
1 parent b407d02 commit 92ce3bc

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

subsys/net/lib/dns/resolve.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,20 @@ static int dns_read(struct dns_resolve_context *ctx,
287287
info->ai_addr.sa_family = AF_INET;
288288
info->ai_addrlen = sizeof(struct sockaddr_in);
289289
} else if (ctx->queries[query_idx].query_type == DNS_QUERY_TYPE_AAAA) {
290+
/* We cannot resolve IPv6 address if IPv6 is disabled. The reason
291+
* being that "struct sockaddr" does not have enough space for
292+
* IPv6 address in that case.
293+
*/
294+
#if defined(CONFIG_NET_IPV6)
290295
address_size = DNS_IPV6_LEN;
291296
addr = (u8_t *)&net_sin6(&info->ai_addr)->sin6_addr;
292297
info->ai_family = AF_INET6;
293298
info->ai_addr.sa_family = AF_INET6;
294299
info->ai_addrlen = sizeof(struct sockaddr_in6);
300+
#else
301+
ret = DNS_EAI_FAMILY;
302+
goto quit;
303+
#endif
295304
} else {
296305
ret = DNS_EAI_FAMILY;
297306
goto quit;
@@ -608,10 +617,10 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
608617
void *user_data,
609618
s32_t timeout)
610619
{
611-
struct net_buf *dns_data;
620+
struct net_buf *dns_data = NULL;
612621
struct net_buf *dns_qname = NULL;
613622
struct sockaddr addr;
614-
int ret, i, j = 0;
623+
int ret, i = -1, j = 0;
615624
int failure = 0;
616625

617626
if (!ctx || !ctx->is_used || !query || !cb) {
@@ -638,11 +647,16 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
638647
info.ai_addr.sa_family = AF_INET;
639648
info.ai_addrlen = sizeof(struct sockaddr_in);
640649
} else if (type == DNS_QUERY_TYPE_AAAA) {
650+
#if defined(CONFIG_NET_IPV6)
641651
memcpy(net_sin6(&info.ai_addr), net_sin6(&addr),
642652
sizeof(struct sockaddr_in6));
643653
info.ai_family = AF_INET6;
644654
info.ai_addr.sa_family = AF_INET6;
645655
info.ai_addrlen = sizeof(struct sockaddr_in6);
656+
#else
657+
ret = -EAFNOSUPPORT;
658+
goto quit;
659+
#endif
646660
} else {
647661
goto try_resolve;
648662
}
@@ -729,11 +743,14 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
729743

730744
quit:
731745
if (ret < 0) {
732-
if (k_delayed_work_remaining_get(&ctx->queries[i].timer) > 0) {
733-
k_delayed_work_cancel(&ctx->queries[i].timer);
734-
}
746+
if (i >= 0) {
747+
if (k_delayed_work_remaining_get(
748+
&ctx->queries[i].timer) > 0) {
749+
k_delayed_work_cancel(&ctx->queries[i].timer);
750+
}
735751

736-
ctx->queries[i].cb = NULL;
752+
ctx->queries[i].cb = NULL;
753+
}
737754

738755
if (dns_id) {
739756
*dns_id = 0;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CONFIG_NETWORKING=y
2+
CONFIG_NET_TEST=y
3+
CONFIG_RANDOM_GENERATOR=y
4+
CONFIG_TEST_RANDOM_GENERATOR=y
5+
6+
CONFIG_NET_L2_DUMMY=y
7+
8+
CONFIG_DNS_RESOLVER=y
9+
CONFIG_DNS_RESOLVER_MAX_SERVERS=2
10+
CONFIG_DNS_NUM_OF_CONCUR_QUERIES=1
11+
12+
CONFIG_DNS_SERVER_IP_ADDRESSES=y
13+
CONFIG_DNS_SERVER1="192.0.2.2"
14+
CONFIG_DNS_SERVER2="192.0.2.2:5353"
15+
16+
CONFIG_NET_LOG=y
17+
CONFIG_SYS_LOG_NET_LEVEL=2
18+
CONFIG_SYS_LOG_SHOW_COLOR=y
19+
#CONFIG_NET_DEBUG_DNS_RESOLVE=y
20+
21+
CONFIG_NET_IPV4=y
22+
CONFIG_NET_IPV6=n
23+
CONFIG_NET_IPV6_DAD=n
24+
CONFIG_NET_IPV6_MLD=n
25+
CONFIG_NET_IPV6_ND=n
26+
CONFIG_NET_ARP=n
27+
28+
CONFIG_PRINTK=y
29+
CONFIG_ZTEST=y
30+
31+
CONFIG_MAIN_STACK_SIZE=1344

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,20 @@ static void dns_query_ipv4_server_count(void)
374374
continue;
375375
}
376376

377-
if (ctx->servers[i].dns_server.sa_family == AF_INET) {
378-
count++;
377+
if (ctx->servers[i].dns_server.sa_family == AF_INET6) {
378+
continue;
379379
}
380380

381+
count++;
382+
381383
if (net_sin(&ctx->servers[i].dns_server)->sin_port ==
382384
ntohs(53)) {
383385
port++;
384386
}
385387
}
386388

387389
zassert_equal(count, 2, "Invalid number of IPv4 servers");
388-
zassert_equal(port, 2, "Invalid number of IPv4 servers with port 53");
390+
zassert_equal(port, 1, "Invalid number of IPv4 servers with port 53");
389391
}
390392

391393
static void dns_query_ipv6_server_count(void)
@@ -402,18 +404,25 @@ static void dns_query_ipv6_server_count(void)
402404
continue;
403405
}
404406

405-
if (ctx->servers[i].dns_server.sa_family == AF_INET6) {
406-
count++;
407+
if (ctx->servers[i].dns_server.sa_family == AF_INET) {
408+
continue;
407409
}
408410

411+
count++;
412+
409413
if (net_sin6(&ctx->servers[i].dns_server)->sin6_port ==
410414
ntohs(53)) {
411415
port++;
412416
}
413417
}
414418

419+
#if defined(CONFIG_NET_IPV6)
415420
zassert_equal(count, 2, "Invalid number of IPv6 servers");
416-
zassert_equal(port, 2, "Invalid number of IPv6 servers with port 53");
421+
zassert_equal(port, 1, "Invalid number of IPv6 servers with port 53");
422+
#else
423+
zassert_equal(count, 0, "Invalid number of IPv6 servers");
424+
zassert_equal(port, 0, "Invalid number of IPv6 servers with port 53");
425+
#endif
417426
}
418427

419428
static void dns_query_too_many(void)
@@ -671,10 +680,12 @@ void dns_result_numeric_cb(enum dns_resolve_status status,
671680
}
672681

673682
if (info && info->ai_family == AF_INET6) {
683+
#if defined(CONFIG_NET_IPV6)
674684
if (net_ipv6_addr_cmp(&net_sin6(&info->ai_addr)->sin6_addr,
675685
&my_addr3) != true) {
676686
zassert_true(false, "IPv6 address does not match");
677687
}
688+
#endif
678689
}
679690

680691
k_sem_give(&wait_data2);

tests/net/lib/dns_resolve/testcase.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ tests:
33
tags: dns net
44
min_ram: 16
55
timeout: 600
6+
- test-no-ipv6:
7+
tags: dns net
8+
min_ram: 16
9+
timeout: 600
10+
extra_args: CONF_FILE=prj-no-ipv6.conf

0 commit comments

Comments
 (0)