Skip to content

Commit

Permalink
libs:netdb: Restrict DNS query types
Browse files Browse the repository at this point in the history
Corresponds to the problem of name resolution with different IP
address types in networks where only one of IPv4 or IPv6 can be
used due to physical layer reasons (e.g., LTE networks).
  • Loading branch information
SPRESENSE committed Sep 14, 2023
1 parent a0cc455 commit 39512cb
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 58 deletions.
10 changes: 10 additions & 0 deletions include/nuttx/net/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ int dns_register_notify(dns_callback_t callback, FAR void *arg);

int dns_unregister_notify(dns_callback_t callback, FAR void *arg);

/****************************************************************************
* Name: dns_set_queryaddrtype
*
* Description:
* Configure the address type to be used for queries.
*
****************************************************************************/

int dns_set_queryaddrtype(sa_family_t addrtype);

#undef EXTERN
#if defined(__cplusplus)
}
Expand Down
1 change: 1 addition & 0 deletions libs/libc/netdb/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += lib_dnsinit.c lib_dnsbind.c lib_dnsquery.c
CSRCS += lib_dnsaddserver.c lib_dnsdefaultserver.c
CSRCS += lib_dnsforeach.c lib_dnsnotify.c
CSRCS += lib_dnsqueryaddrtype.c

ifneq ($(CONFIG_NETDB_DNSCLIENT_ENTRIES),0)
CSRCS += lib_dnscache.c
Expand Down
17 changes: 17 additions & 0 deletions libs/libc/netdb/lib_dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ int dns_bind(sa_family_t family);
int dns_query(FAR const char *hostname, FAR union dns_addr_u *addr,
FAR int *naddr);

/****************************************************************************
* Name: dns_isavail_queryaddrtype
*
* Description:
* Determine if the specified address type is available for DNS query.
*
* Input Parameters:
* addrtype - The address type. AF_INET or AF_INET6 is specified.
*
* Returned Value:
* Returns true if the address type specified in the addrtype argument
* is available.
*
****************************************************************************/

bool dns_isavail_queryaddrtype(sa_family_t addrtype);

/****************************************************************************
* Name: dns_save_answer
*
Expand Down
122 changes: 64 additions & 58 deletions libs/libc/netdb/lib_dnsquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,85 +643,91 @@ static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
for (retries = 0; retries < CONFIG_NETDB_DNSCLIENT_RETRIES; retries++)
{
#ifdef CONFIG_NET_IPv6
/* Send the IPv6 query */

sd = dns_bind(addr->sa_family);
if (sd < 0)
{
query->result = sd;
return 0;
}

ret = dns_send_query(sd, query->hostname,
(FAR union dns_addr_u *)addr,
DNS_RECTYPE_AAAA, &qdata->qinfo, qdata->buffer);
if (ret < 0)
{
dns_query_error("ERROR: IPv6 dns_send_query failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
else
if (dns_isavail_queryaddrtype(AF_INET6))
{
/* Obtain the IPv6 response */
/* Send the IPv6 query */

ret = dns_recv_response(sd, &query->addr[next],
*query->naddr - next, &qdata->qinfo,
&query->ttl, qdata->buffer);
if (ret >= 0)
sd = dns_bind(addr->sa_family);
if (sd < 0)
{
next += ret;
query->result = sd;
return 0;
}
else

ret = dns_send_query(sd, query->hostname,
(FAR union dns_addr_u *)addr,
DNS_RECTYPE_AAAA, &qdata->qinfo, qdata->buffer);
if (ret < 0)
{
dns_query_error("ERROR: IPv6 dns_recv_response failed",
dns_query_error("ERROR: IPv6 dns_send_query failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
}
else
{
/* Obtain the IPv6 response */

ret = dns_recv_response(sd, &query->addr[next],
*query->naddr - next, &qdata->qinfo,
&query->ttl, qdata->buffer);
if (ret >= 0)
{
next += ret;
}
else
{
dns_query_error("ERROR: IPv6 dns_recv_response failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
}

close(sd);
close(sd);
}
#endif

#ifdef CONFIG_NET_IPv4
/* Send the IPv4 query */

sd = dns_bind(addr->sa_family);
if (sd < 0)
{
query->result = sd;
return 0;
}

ret = dns_send_query(sd, query->hostname,
(FAR union dns_addr_u *)addr,
DNS_RECTYPE_A, &qdata->qinfo, qdata->buffer);
if (ret < 0)
{
dns_query_error("ERROR: IPv4 dns_send_query failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
else
if (dns_isavail_queryaddrtype(AF_INET))
{
/* Obtain the IPv4 response */
/* Send the IPv4 query */

ret = dns_recv_response(sd, &query->addr[next],
*query->naddr - next, &qdata->qinfo,
&query->ttl, qdata->buffer);
if (ret >= 0)
sd = dns_bind(addr->sa_family);
if (sd < 0)
{
next += ret;
query->result = sd;
return 0;
}
else

ret = dns_send_query(sd, query->hostname,
(FAR union dns_addr_u *)addr,
DNS_RECTYPE_A, &qdata->qinfo, qdata->buffer);
if (ret < 0)
{
dns_query_error("ERROR: IPv4 dns_recv_response failed",
dns_query_error("ERROR: IPv4 dns_send_query failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
}
else
{
/* Obtain the IPv4 response */

ret = dns_recv_response(sd, &query->addr[next],
*query->naddr - next, &qdata->qinfo,
&query->ttl, qdata->buffer);
if (ret >= 0)
{
next += ret;
}
else
{
dns_query_error("ERROR: IPv4 dns_recv_response failed",
ret, (FAR union dns_addr_u *)addr);
query->result = ret;
}
}

close(sd);
close(sd);
}
#endif /* CONFIG_NET_IPv4 */

if (next > 0)
Expand Down
98 changes: 98 additions & 0 deletions libs/libc/netdb/lib_dnsqueryaddrtype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/****************************************************************************
* libs/libc/netdb/lib_dnsqueryaddrtype.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>

#include "netdb/lib_dns.h"

/****************************************************************************
* Private Data
****************************************************************************/

static int g_query_addrtype = AF_UNSPEC;

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: dns_isavail_queryaddrtype
*
* Description:
* Determine if the specified address type is available for DNS query.
*
****************************************************************************/

bool dns_isavail_queryaddrtype(sa_family_t addrtype)
{
DEBUGASSERT(addrtype == AF_INET || addrtype == AF_INET6);

return (g_query_addrtype == AF_UNSPEC) ? true :
(g_query_addrtype == addrtype);
}

/****************************************************************************
* Name: dns_set_queryaddrtype
*
* Description:
* Configure the address type to be used for queries.
*
****************************************************************************/

int dns_set_queryaddrtype(sa_family_t addrtype)
{
int ret = OK;

switch (addrtype)
{
#ifdef CONFIG_NET_IPv4
case AF_INET:
#endif
#ifdef CONFIG_NET_IPv6
case AF_INET6:
#endif
case AF_UNSPEC:
dns_lock();
g_query_addrtype = addrtype;
dns_unlock();
ninfo("Configure address type for dns query: %d\n", addrtype);
break;
default:
nerr("ERROR: Unsupported family: %d\n", addrtype);
ret = -ENOSYS;
break;
}

#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
dns_clear_answer();
#endif

return ret;
}

0 comments on commit 39512cb

Please sign in to comment.