Skip to content

Commit 80e458c

Browse files
jukkarcfriedt
authored andcommitted
net: dns: Resolve localhost name
Allow user to query localhost name and get a localhost address 127.0.0.1 or ::1 back. This is only enabled if loopback driver is used as the use case does not make much sense otherwise. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent cb163d1 commit 80e458c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

subsys/net/lib/dns/resolve.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,48 @@ int dns_resolve_name_internal(struct dns_resolve_context *ctx,
18271827
ARG_UNUSED(use_cache);
18281828
#endif /* CONFIG_DNS_RESOLVER_CACHE */
18291829

1830+
/* If we get a query to localhost, then short circuit early */
1831+
if ((IS_ENABLED(CONFIG_NET_LOOPBACK) || IS_ENABLED(CONFIG_NET_TEST)) &&
1832+
strcmp(query, "localhost") == 0) {
1833+
struct dns_addrinfo info = { 0 };
1834+
1835+
if (type == DNS_QUERY_TYPE_A) {
1836+
if (!IS_ENABLED(CONFIG_NET_IPV4)) {
1837+
return -EAFNOSUPPORT;
1838+
}
1839+
1840+
struct in_addr addr4 = INADDR_LOOPBACK_INIT;
1841+
1842+
memcpy(&net_sin(&info.ai_addr)->sin_addr, &addr4,
1843+
sizeof(struct in_addr));
1844+
1845+
info.ai_family = AF_INET;
1846+
info.ai_addr.sa_family = AF_INET;
1847+
info.ai_addrlen = sizeof(struct sockaddr_in);
1848+
1849+
} else if (type == DNS_QUERY_TYPE_AAAA) {
1850+
if (!IS_ENABLED(CONFIG_NET_IPV6)) {
1851+
return -EAFNOSUPPORT;
1852+
}
1853+
1854+
struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
1855+
1856+
memcpy(&net_sin6(&info.ai_addr)->sin6_addr, &addr6,
1857+
sizeof(struct in6_addr));
1858+
1859+
info.ai_family = AF_INET6;
1860+
info.ai_addr.sa_family = AF_INET6;
1861+
info.ai_addrlen = sizeof(struct sockaddr_in6);
1862+
} else {
1863+
return -EINVAL;
1864+
}
1865+
1866+
cb(DNS_EAI_INPROGRESS, &info, user_data);
1867+
cb(DNS_EAI_ALLDONE, NULL, user_data);
1868+
1869+
return 0;
1870+
}
1871+
18301872
if (IS_ENABLED(CONFIG_NET_HOSTNAME_ENABLE)) {
18311873
const char *hostname = net_hostname_get();
18321874
struct dns_addrinfo info = { 0 };

0 commit comments

Comments
 (0)