Skip to content

Commit cb52096

Browse files
committed
net: getaddrinfo: Query both IPv4 and IPv6 if family is AF_UNSPEC
We must query both IPv4 and IPv6 addresses if the hints parameter is NULL i.e., user does not supply hints or if family is set to AF_UNSPEC. Fixes zephyrproject-rtos#16453 Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 05cd342 commit cb52096

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

subsys/net/lib/sockets/getaddrinfo.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int exec_query(const char *host, int family,
105105
{
106106
enum dns_query_type qtype = DNS_QUERY_TYPE_A;
107107

108-
if (IS_ENABLED(CONFIG_NET_IPV6) && family != AF_INET) {
108+
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {
109109
qtype = DNS_QUERY_TYPE_AAAA;
110110
}
111111

@@ -205,6 +205,7 @@ int z_impl_z_zsock_getaddrinfo_internal(const char *host, const char *service,
205205
/* Link entries in advance */
206206
ai_state.ai_arr[0].ai_next = &ai_state.ai_arr[1];
207207

208+
/* If the family is AF_UNSPEC, then we query IPv4 address first */
208209
ret = exec_query(host, family, &ai_state);
209210
if (ret == 0) {
210211
/* If the DNS query for reason fails so that the
@@ -226,6 +227,26 @@ int z_impl_z_zsock_getaddrinfo_internal(const char *host, const char *service,
226227
st1 = DNS_EAI_SYSTEM;
227228
}
228229

230+
/* If family is AF_UNSPEC, the IPv4 query has been already done
231+
* so we can do IPv6 query next if IPv6 is enabled in the config.
232+
*/
233+
if (family == AF_UNSPEC && IS_ENABLED(CONFIG_NET_IPV6)) {
234+
ret = exec_query(host, AF_INET6, &ai_state);
235+
if (ret == 0) {
236+
int ret = k_sem_take(&ai_state.sem,
237+
CONFIG_NET_SOCKETS_DNS_TIMEOUT +
238+
K_MSEC(100));
239+
if (ret == -EAGAIN) {
240+
return DNS_EAI_AGAIN;
241+
}
242+
243+
st2 = ai_state.status;
244+
} else {
245+
errno = -ret;
246+
st2 = DNS_EAI_SYSTEM;
247+
}
248+
}
249+
229250
if (ai_state.idx > 0) {
230251
ai_addr = &ai_state.ai_arr[ai_state.idx - 1]._ai_addr;
231252
net_sin(ai_addr)->sin_port = htons(port);

0 commit comments

Comments
 (0)