Skip to content

Commit 43aa12f

Browse files
committed
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 <jukka.rissanen@linux.intel.com>
1 parent bfc583c commit 43aa12f

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
@@ -322,11 +322,20 @@ static int dns_read(struct dns_resolve_context *ctx,
322322
info->ai_addr.sa_family = AF_INET;
323323
info->ai_addrlen = sizeof(struct sockaddr_in);
324324
} else if (ctx->queries[query_idx].query_type == DNS_QUERY_TYPE_AAAA) {
325+
/* We cannot resolve IPv6 address if IPv6 is disabled. The reason
326+
* being that "struct sockaddr" does not have enough space for
327+
* IPv6 address in that case.
328+
*/
329+
#if defined(CONFIG_NET_IPV6)
325330
address_size = DNS_IPV6_LEN;
326331
addr = (u8_t *)&net_sin6(&info->ai_addr)->sin6_addr;
327332
info->ai_family = AF_INET6;
328333
info->ai_addr.sa_family = AF_INET6;
329334
info->ai_addrlen = sizeof(struct sockaddr_in6);
335+
#else
336+
ret = DNS_EAI_FAMILY;
337+
goto quit;
338+
#endif
330339
} else {
331340
ret = DNS_EAI_FAMILY;
332341
goto quit;
@@ -643,10 +652,10 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
643652
void *user_data,
644653
s32_t timeout)
645654
{
646-
struct net_buf *dns_data;
655+
struct net_buf *dns_data = NULL;
647656
struct net_buf *dns_qname = NULL;
648657
struct sockaddr addr;
649-
int ret, i, j = 0;
658+
int ret, i = -1, j = 0;
650659
int failure = 0;
651660
bool mdns_query = false;
652661

@@ -674,11 +683,16 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
674683
info.ai_addr.sa_family = AF_INET;
675684
info.ai_addrlen = sizeof(struct sockaddr_in);
676685
} else if (type == DNS_QUERY_TYPE_AAAA) {
686+
#if defined(CONFIG_NET_IPV6)
677687
memcpy(net_sin6(&info.ai_addr), net_sin6(&addr),
678688
sizeof(struct sockaddr_in6));
679689
info.ai_family = AF_INET6;
680690
info.ai_addr.sa_family = AF_INET6;
681691
info.ai_addrlen = sizeof(struct sockaddr_in6);
692+
#else
693+
ret = -EAFNOSUPPORT;
694+
goto quit;
695+
#endif
682696
} else {
683697
goto try_resolve;
684698
}
@@ -784,11 +798,14 @@ int dns_resolve_name(struct dns_resolve_context *ctx,
784798

785799
quit:
786800
if (ret < 0) {
787-
if (k_delayed_work_remaining_get(&ctx->queries[i].timer) > 0) {
788-
k_delayed_work_cancel(&ctx->queries[i].timer);
789-
}
801+
if (i >= 0) {
802+
if (k_delayed_work_remaining_get(
803+
&ctx->queries[i].timer) > 0) {
804+
k_delayed_work_cancel(&ctx->queries[i].timer);
805+
}
790806

791-
ctx->queries[i].cb = NULL;
807+
ctx->queries[i].cb = NULL;
808+
}
792809

793810
if (dns_id) {
794811
*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)