Skip to content

Commit ec18ab1

Browse files
jukkarmmahadevan108
authored andcommitted
net: dns: Convert the query and answer to small case letters
Because we might get answers in capital letters, convert the answer to small case letters and also make sure we send query in small case latters. This makes sure that our query_hash is properly calculated regardless of how the resolver gets the data. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent adeb28d commit ec18ab1

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

subsys/net/lib/dns/resolve.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LOG_MODULE_REGISTER(net_dns_resolve, CONFIG_DNS_RESOLVER_LOG_LEVEL);
1919
#include <string.h>
2020
#include <errno.h>
2121
#include <stdlib.h>
22+
#include <ctype.h>
2223

2324
#include <zephyr/sys/crc.h>
2425
#include <zephyr/net/net_ip.h>
@@ -653,7 +654,7 @@ int dns_validate_msg(struct dns_resolve_context *ctx,
653654
struct dns_addrinfo info = { 0 };
654655
uint32_t ttl; /* RR ttl, so far it is not passed to caller */
655656
uint8_t *src, *addr;
656-
const char *query_name;
657+
char *query_name;
657658
int address_size;
658659
/* index that points to the current answer being analyzed */
659660
int answer_ptr;
@@ -743,6 +744,13 @@ int dns_validate_msg(struct dns_resolve_context *ctx,
743744

744745
query_name = dns_msg->msg + dns_msg->query_offset;
745746

747+
/* Convert the query name to small case so that our
748+
* hash checker can find it.
749+
*/
750+
for (size_t i = 0, n = strlen(query_name); i < n; i++) {
751+
query_name[i] = tolower(query_name[i]);
752+
}
753+
746754
/* Add \0 and query type (A or AAAA) to the hash */
747755
*query_hash = crc16_ansi(query_name,
748756
strlen(query_name) + 1 + 2);
@@ -955,6 +963,7 @@ static int dns_write(struct dns_resolve_context *ctx,
955963
int server_addr_len;
956964
uint16_t dns_id, len;
957965
int ret, sock, family;
966+
char *query_name;
958967

959968
sock = ctx->servers[server_idx].sock;
960969
family = ctx->servers[server_idx].dns_server.sa_family;
@@ -971,11 +980,20 @@ static int dns_write(struct dns_resolve_context *ctx,
971980
return -EINVAL;
972981
}
973982

983+
query_name = buf + DNS_MSG_HEADER_SIZE;
984+
985+
/* Convert the query name to small case so that our
986+
* hash checker can find it later when we get the answer.
987+
*/
988+
for (int i = 0; i < dns_qname->len; i++) {
989+
query_name[i] = tolower(query_name[i]);
990+
}
991+
974992
/* Add \0 and query type (A or AAAA) to the hash. Note that
975993
* the dns_qname->len contains the length of \0
976994
*/
977995
ctx->queries[query_idx].query_hash =
978-
crc16_ansi(buf + DNS_MSG_HEADER_SIZE, dns_qname->len + 2);
996+
crc16_ansi(query_name, dns_qname->len + 2);
979997

980998
if (hop_limit > 0) {
981999
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {

0 commit comments

Comments
 (0)