You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2026/10/18 15:31:42 UTC

(nuttx) branch master updated (a0263fc28eb -> 1bdf706a3ca)

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from a0263fc28eb Documentation/ostest: Describe the time slices of the round-robin test.
     new 6966028cf61 include/arpa: Add nameser.h.
     new 74cdbe771b4 libs/libc/netdb: Add res_query() and res_search().
     new 1bdf706a3ca Documentation: Describe res_query() and the other DNS query functions.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 Documentation/reference/user/11_network.rst        |  43 ++++
 include/arpa/nameser.h                             | 282 +++++++++++++++++++++
 include/resolv.h                                   |  18 ++
 libs/libc/netdb/CMakeLists.txt                     |   3 +-
 libs/libc/netdb/Make.defs                          |   4 +-
 libs/libc/netdb/lib_resmkquery.c                   | 106 ++++++++
 .../libc/netdb/lib_resquery.c                      | 124 +++++----
 libs/libc/netdb/lib_ressend.c                      | 191 ++++++++++++++
 8 files changed, 704 insertions(+), 67 deletions(-)
 create mode 100644 include/arpa/nameser.h
 create mode 100644 libs/libc/netdb/lib_resmkquery.c
 copy arch/arm/src/armv8-m/arm_secure_irq.c => libs/libc/netdb/lib_resquery.c (50%)
 create mode 100644 libs/libc/netdb/lib_ressend.c


(nuttx) 03/03: Documentation: Describe res_query() and the other DNS query functions.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1bdf706a3cad4883599e219243b9c734fd88397b
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Sat Oct 17 23:13:56 2026 +0200

    Documentation: Describe res_query() and the other DNS query functions.
    
    The network reference describes the DNS client functions, but not
    res_mkquery(), res_send(), res_query() and res_search().
    
    Add a section for them.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 Documentation/reference/user/11_network.rst | 43 +++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/Documentation/reference/user/11_network.rst b/Documentation/reference/user/11_network.rst
index 3ce41efbd55..d7f87a16b95 100644
--- a/Documentation/reference/user/11_network.rst
+++ b/Documentation/reference/user/11_network.rst
@@ -555,3 +555,46 @@ applications to add, remove, and query DNS nameservers.
 
     -  ``EINVAL``. Unsupported address family.
 
+
+DNS Query Functions (``resolv.h``)
+----------------------------------
+
+These functions send DNS queries for any record type and return the raw
+answer message. ``arpa/nameser.h`` gives the constants and the macros that
+parse the answer. ``res_mkquery()`` is always available with
+``CONFIG_LIBC_NETDB``. The other three need ``CONFIG_NETDB_DNSCLIENT`` and use
+the same name servers, timeouts and retries as the DNS client.
+
+  - :c:func:`res_mkquery`
+  - :c:func:`res_send`
+  - :c:func:`res_query`
+  - :c:func:`res_search`
+
+.. c:function:: int res_mkquery(int op, const char *dname, int rclass, int type, const unsigned char *data, int datalen, const unsigned char *newrr, unsigned char *buf, int buflen);
+
+  Build a message with one question for ``dname`` in ``buf``, with the
+  opcode ``op`` (normally ``QUERY``), a random ID and recursion desired.
+  ``data``, ``datalen`` and ``newrr`` are not used.
+
+  :return: The size of the message on success; -1 on error.
+
+.. c:function:: int res_send(const unsigned char *msg, int msglen, unsigned char *answer, int anslen);
+
+  Send the query message ``msg`` to the name servers over UDP, and copy the
+  first valid answer into ``answer``. A server that answers ``SERVFAIL`` or
+  ``REFUSED`` is skipped. There is no TCP fallback: a truncated answer is
+  returned with the ``TC`` bit set.
+
+  :return: The size of the answer on success; -1 on error.
+
+.. c:function:: int res_query(const char *dname, int rclass, int type, unsigned char *answer, int anslen);
+
+  Query the name servers for the records of class ``rclass`` and type
+  ``type`` for ``dname``, and copy the answer message into ``answer``.
+
+  :return: The size of the answer on success; -1 on error with ``h_errno``
+    set to ``HOST_NOT_FOUND``, ``NO_DATA``, ``NO_RECOVERY`` or ``TRY_AGAIN``.
+
+.. c:function:: int res_search(const char *dname, int rclass, int type, unsigned char *answer, int anslen);
+
+  The same as :c:func:`res_query`. NuttX has no search list.


(nuttx) 01/03: include/arpa: Add nameser.h.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6966028cf61a855c5f2e271cbe70bcb9aa2fdd30
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Sat Oct 17 20:25:56 2026 +0200

    include/arpa: Add nameser.h.
    
    Programs that send DNS queries with res_query() parse the raw answer
    with the definitions in <arpa/nameser.h>.  NuttX did not have this
    header.
    
    This commit adds <arpa/nameser.h> with the standard subset that these
    programs use: the message sizes (NS_PACKETSZ, NS_HFIXEDSZ,
    NS_QFIXEDSZ, ...), the ns_opcode, ns_rcode, ns_class and ns_type
    enumerations, the NS_GET16/NS_PUT16 family of macros, the HEADER
    structure and the older BIND names (PACKETSZ, C_IN, T_MX, GETSHORT,
    ...).  The parser functions (ns_initparse() and others) are not
    included.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 include/arpa/nameser.h | 282 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 282 insertions(+)

diff --git a/include/arpa/nameser.h b/include/arpa/nameser.h
new file mode 100644
index 00000000000..59d54c913f7
--- /dev/null
+++ b/include/arpa/nameser.h
@@ -0,0 +1,282 @@
+/****************************************************************************
+ * include/arpa/nameser.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_ARPA_NAMESER_H
+#define __INCLUDE_ARPA_NAMESER_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <endian.h>
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Sizes of DNS message parts (RFC 1035) */
+
+#define NS_PACKETSZ     512    /* Maximum size of a UDP message */
+#define NS_MAXDNAME     1025   /* Maximum size of a domain name string */
+#define NS_MAXCDNAME    255    /* Maximum size of a compressed name */
+#define NS_MAXLABEL     63     /* Maximum size of a label */
+#define NS_HFIXEDSZ     12     /* Size of the message header */
+#define NS_QFIXEDSZ     4      /* Size of the fixed part of a question */
+#define NS_RRFIXEDSZ    10     /* Size of the fixed part of a record */
+#define NS_INT32SZ      4
+#define NS_INT16SZ      2
+#define NS_INT8SZ       1
+#define NS_INADDRSZ     4
+#define NS_IN6ADDRSZ    16
+#define NS_CMPRSFLGS    0xc0   /* Flag bits of a compressed name pointer */
+#define NS_DEFAULTPORT  53
+
+/* Get and put 16-bit and 32-bit values in network byte order.  cp is
+ * an unsigned char pointer and moves past the value.
+ */
+
+#define NS_GET16(s, cp) \
+  do \
+    { \
+      (s) = ((uint16_t)(cp)[0] << 8) | (cp)[1]; \
+      (cp) += NS_INT16SZ; \
+    } \
+  while (0)
+
+#define NS_GET32(l, cp) \
+  do \
+    { \
+      (l) = ((uint32_t)(cp)[0] << 24) | ((uint32_t)(cp)[1] << 16) | \
+            ((uint32_t)(cp)[2] << 8) | (cp)[3]; \
+      (cp) += NS_INT32SZ; \
+    } \
+  while (0)
+
+#define NS_PUT16(s, cp) \
+  do \
+    { \
+      (cp)[0] = (uint16_t)(s) >> 8; \
+      (cp)[1] = (uint16_t)(s); \
+      (cp) += NS_INT16SZ; \
+    } \
+  while (0)
+
+#define NS_PUT32(l, cp) \
+  do \
+    { \
+      (cp)[0] = (uint32_t)(l) >> 24; \
+      (cp)[1] = (uint32_t)(l) >> 16; \
+      (cp)[2] = (uint32_t)(l) >> 8; \
+      (cp)[3] = (uint32_t)(l); \
+      (cp) += NS_INT32SZ; \
+    } \
+  while (0)
+
+/* Older (BIND 4) names of the definitions above and below */
+
+#define PACKETSZ        NS_PACKETSZ
+#define MAXDNAME        NS_MAXDNAME
+#define MAXCDNAME       NS_MAXCDNAME
+#define MAXLABEL        NS_MAXLABEL
+#define HFIXEDSZ        NS_HFIXEDSZ
+#define QFIXEDSZ        NS_QFIXEDSZ
+#define RRFIXEDSZ       NS_RRFIXEDSZ
+#define INT32SZ         NS_INT32SZ
+#define INT16SZ         NS_INT16SZ
+#define INT8SZ          NS_INT8SZ
+#define INADDRSZ        NS_INADDRSZ
+#define IN6ADDRSZ       NS_IN6ADDRSZ
+#define INDIR_MASK      NS_CMPRSFLGS
+#define NAMESERVER_PORT NS_DEFAULTPORT
+
+#define GETSHORT        NS_GET16
+#define GETLONG         NS_GET32
+#define PUTSHORT        NS_PUT16
+#define PUTLONG         NS_PUT32
+
+#define QUERY           ns_o_query
+#define IQUERY          ns_o_iquery
+#define STATUS          ns_o_status
+#define NS_NOTIFY_OP    ns_o_notify
+#define NS_UPDATE_OP    ns_o_update
+
+#define NOERROR         ns_r_noerror
+#define FORMERR         ns_r_formerr
+#define SERVFAIL        ns_r_servfail
+#define NXDOMAIN        ns_r_nxdomain
+#define NOTIMP          ns_r_notimpl
+#define REFUSED         ns_r_refused
+
+#define C_IN            ns_c_in
+#define C_CHAOS         ns_c_chaos
+#define C_HS            ns_c_hs
+#define C_NONE          ns_c_none
+#define C_ANY           ns_c_any
+
+#define T_A             ns_t_a
+#define T_NS            ns_t_ns
+#define T_CNAME         ns_t_cname
+#define T_SOA           ns_t_soa
+#define T_PTR           ns_t_ptr
+#define T_HINFO         ns_t_hinfo
+#define T_MX            ns_t_mx
+#define T_TXT           ns_t_txt
+#define T_AAAA          ns_t_aaaa
+#define T_SRV           ns_t_srv
+#define T_NAPTR         ns_t_naptr
+#define T_OPT           ns_t_opt
+#define T_IXFR          ns_t_ixfr
+#define T_AXFR          ns_t_axfr
+#define T_ANY           ns_t_any
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* Operation codes */
+
+typedef enum __ns_opcode
+{
+  ns_o_query  = 0,
+  ns_o_iquery = 1,
+  ns_o_status = 2,
+  ns_o_notify = 4,
+  ns_o_update = 5,
+  ns_o_max    = 6
+} ns_opcode;
+
+/* Response codes */
+
+typedef enum __ns_rcode
+{
+  ns_r_noerror  = 0,
+  ns_r_formerr  = 1,
+  ns_r_servfail = 2,
+  ns_r_nxdomain = 3,
+  ns_r_notimpl  = 4,
+  ns_r_refused  = 5,
+  ns_r_yxdomain = 6,
+  ns_r_yxrrset  = 7,
+  ns_r_nxrrset  = 8,
+  ns_r_notauth  = 9,
+  ns_r_notzone  = 10,
+  ns_r_max      = 11
+} ns_rcode;
+
+/* Classes */
+
+typedef enum __ns_class
+{
+  ns_c_invalid = 0,
+  ns_c_in      = 1,
+  ns_c_2       = 2,
+  ns_c_chaos   = 3,
+  ns_c_hs      = 4,
+  ns_c_none    = 254,
+  ns_c_any     = 255,
+  ns_c_max     = 65536
+} ns_class;
+
+/* Record types */
+
+typedef enum __ns_type
+{
+  ns_t_invalid = 0,
+  ns_t_a       = 1,
+  ns_t_ns      = 2,
+  ns_t_md      = 3,
+  ns_t_mf      = 4,
+  ns_t_cname   = 5,
+  ns_t_soa     = 6,
+  ns_t_mb      = 7,
+  ns_t_mg      = 8,
+  ns_t_mr      = 9,
+  ns_t_null    = 10,
+  ns_t_wks     = 11,
+  ns_t_ptr     = 12,
+  ns_t_hinfo   = 13,
+  ns_t_minfo   = 14,
+  ns_t_mx      = 15,
+  ns_t_txt     = 16,
+  ns_t_rp      = 17,
+  ns_t_afsdb   = 18,
+  ns_t_sig     = 24,
+  ns_t_key     = 25,
+  ns_t_aaaa    = 28,
+  ns_t_loc     = 29,
+  ns_t_srv     = 33,
+  ns_t_naptr   = 35,
+  ns_t_cert    = 37,
+  ns_t_dname   = 39,
+  ns_t_opt     = 41,
+  ns_t_ds      = 43,
+  ns_t_sshfp   = 44,
+  ns_t_rrsig   = 46,
+  ns_t_nsec    = 47,
+  ns_t_dnskey  = 48,
+  ns_t_tlsa    = 52,
+  ns_t_tkey    = 249,
+  ns_t_tsig    = 250,
+  ns_t_ixfr    = 251,
+  ns_t_axfr    = 252,
+  ns_t_any     = 255,
+  ns_t_uri     = 256,
+  ns_t_caa     = 257,
+  ns_t_max     = 65536
+} ns_type;
+
+/* The fixed DNS message header (RFC 1035, 4.1.1) */
+
+typedef struct
+{
+  unsigned id      : 16;      /* Query identification number */
+#if BYTE_ORDER == BIG_ENDIAN
+  unsigned qr      : 1;       /* Response flag */
+  unsigned opcode  : 4;       /* Purpose of message */
+  unsigned aa      : 1;       /* Authoritative answer */
+  unsigned tc      : 1;       /* Truncated message */
+  unsigned rd      : 1;       /* Recursion desired */
+  unsigned ra      : 1;       /* Recursion available */
+  unsigned unused  : 1;
+  unsigned ad      : 1;       /* Authentic data */
+  unsigned cd      : 1;       /* Checking disabled */
+  unsigned rcode   : 4;       /* Response code */
+#else
+  unsigned rd      : 1;       /* Recursion desired */
+  unsigned tc      : 1;       /* Truncated message */
+  unsigned aa      : 1;       /* Authoritative answer */
+  unsigned opcode  : 4;       /* Purpose of message */
+  unsigned qr      : 1;       /* Response flag */
+  unsigned rcode   : 4;       /* Response code */
+  unsigned cd      : 1;       /* Checking disabled */
+  unsigned ad      : 1;       /* Authentic data */
+  unsigned unused  : 1;
+  unsigned ra      : 1;       /* Recursion available */
+#endif
+  unsigned qdcount : 16;      /* Number of question entries */
+  unsigned ancount : 16;      /* Number of answer entries */
+  unsigned nscount : 16;      /* Number of authority entries */
+  unsigned arcount : 16;      /* Number of resource entries */
+} HEADER;
+
+#endif /* __INCLUDE_ARPA_NAMESER_H */


(nuttx) 02/03: libs/libc/netdb: Add res_query() and res_search().

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 74cdbe771b49002db2cdaf67e023cb9db3efb80b
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Sat Oct 17 20:25:56 2026 +0200

    libs/libc/netdb: Add res_query() and res_search().
    
    The DNS client resolves host addresses only.  Programs that need other
    records, for example MX, SRV or TXT records, use res_query() to get
    the raw answer from the name server.  NuttX did not have res_query().
    
    This commit adds these functions to <resolv.h>:
    - res_mkquery() builds a query message with one question.
    - res_send() sends a message over UDP to the name servers of the DNS
      client and returns the answer.  It uses the same name server list,
      socket timeouts and retries as the DNS client.  It skips a server
      that fails or refuses the query.  It does not retry over TCP: a
      large answer comes back with the TC bit set.
    - res_query() builds the query, sends it and sets h_errno from the
      response code.
    - res_search() is the same as res_query(), because NuttX has no search
      domain list.
    
    res_send(), res_query() and res_search() need
    CONFIG_NETDB_DNSCLIENT, as the DNS client does.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 include/resolv.h                 |  18 ++++
 libs/libc/netdb/CMakeLists.txt   |   3 +-
 libs/libc/netdb/Make.defs        |   4 +-
 libs/libc/netdb/lib_resmkquery.c | 106 ++++++++++++++++++++++
 libs/libc/netdb/lib_resquery.c   | 118 ++++++++++++++++++++++++
 libs/libc/netdb/lib_ressend.c    | 191 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 437 insertions(+), 3 deletions(-)

diff --git a/include/resolv.h b/include/resolv.h
index 2acbf6271f4..216dfd068de 100644
--- a/include/resolv.h
+++ b/include/resolv.h
@@ -29,6 +29,8 @@
 
 #include <stddef.h>
 
+#include <arpa/nameser.h>
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -52,6 +54,22 @@ int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
 int dn_expand(FAR const unsigned char *base, FAR const unsigned char *end,
               FAR const unsigned char *src, FAR char *dest, int space);
 
+/* DNS queries */
+
+int res_mkquery(int op, FAR const char *dname, int rclass, int type,
+                FAR const unsigned char *data, int datalen,
+                FAR const unsigned char *newrr,
+                FAR unsigned char *buf, int buflen);
+
+int res_send(FAR const unsigned char *msg, int msglen,
+             FAR unsigned char *answer, int anslen);
+
+int res_query(FAR const char *dname, int rclass, int type,
+              FAR unsigned char *answer, int anslen);
+
+int res_search(FAR const char *dname, int rclass, int type,
+               FAR unsigned char *answer, int anslen);
+
 #if defined(__cplusplus)
 }
 #endif
diff --git a/libs/libc/netdb/CMakeLists.txt b/libs/libc/netdb/CMakeLists.txt
index cf063f00797..20c2ad208f4 100644
--- a/libs/libc/netdb/CMakeLists.txt
+++ b/libs/libc/netdb/CMakeLists.txt
@@ -46,6 +46,7 @@ if(CONFIG_LIBC_NETDB)
     lib_getnameinfo.c
     lib_rexec.c
     lib_dn.c
+    lib_resmkquery.c
     lib_proto.c
     lib_protor.c)
 
@@ -62,7 +63,7 @@ if(CONFIG_LIBC_NETDB)
     list(APPEND SRCS lib_dnsaddserver.c lib_dnsdelserver.c
          lib_dnsdefaultserver.c)
     list(APPEND SRCS lib_dnsforeach.c lib_dnsnotify.c)
-    list(APPEND SRCS lib_dnsqueryfamily.c)
+    list(APPEND SRCS lib_dnsqueryfamily.c lib_ressend.c lib_resquery.c)
 
     if(NOT CONFIG_NETDB_DNSCLIENT_ENTRIES EQUAL 0)
       list(APPEND SRCS lib_dnscache.c)
diff --git a/libs/libc/netdb/Make.defs b/libs/libc/netdb/Make.defs
index 95aac44cc73..a335bf52aab 100644
--- a/libs/libc/netdb/Make.defs
+++ b/libs/libc/netdb/Make.defs
@@ -31,7 +31,7 @@ CSRCS += lib_gethostbyaddr.c lib_gethostbyaddrr.c
 CSRCS += lib_getservbyname.c lib_getservbynamer.c
 CSRCS += lib_getservbyport.c lib_getservbyportr.c
 CSRCS += lib_gaistrerror.c lib_freeaddrinfo.c lib_getaddrinfo.c
-CSRCS += lib_getnameinfo.c lib_rexec.c lib_dn.c
+CSRCS += lib_getnameinfo.c lib_rexec.c lib_dn.c lib_resmkquery.c
 CSRCS += lib_proto.c lib_protor.c
 
 # Add host file support
@@ -46,7 +46,7 @@ ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
 CSRCS += lib_dnsinit.c lib_dnsbind.c lib_dnsquery.c
 CSRCS += lib_dnsaddserver.c lib_dnsdelserver.c lib_dnsdefaultserver.c
 CSRCS += lib_dnsforeach.c lib_dnsnotify.c
-CSRCS += lib_dnsqueryfamily.c
+CSRCS += lib_dnsqueryfamily.c lib_ressend.c lib_resquery.c
 
 ifneq ($(CONFIG_NETDB_DNSCLIENT_ENTRIES),0)
 CSRCS += lib_dnscache.c
diff --git a/libs/libc/netdb/lib_resmkquery.c b/libs/libc/netdb/lib_resmkquery.c
new file mode 100644
index 00000000000..3bdb4a3067c
--- /dev/null
+++ b/libs/libc/netdb/lib_resmkquery.c
@@ -0,0 +1,106 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_resmkquery.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+#include <resolv.h>
+
+#include <arpa/nameser.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: res_mkquery
+ *
+ * Description:
+ *   Build a DNS query message with one question for the domain name dname
+ *   of the given class and type.  The data and newrr arguments are not
+ *   used.
+ *
+ * Input Parameters:
+ *   op      - The operation code, normally QUERY.
+ *   dname   - The domain name to query.
+ *   rclass  - The class of the query, for example C_IN.
+ *   type    - The type of the query, for example T_MX.
+ *   data    - Not used.
+ *   datalen - Not used.
+ *   newrr   - Not used.
+ *   buf     - The buffer that receives the message.
+ *   buflen  - The size of buf.
+ *
+ * Returned Value:
+ *   The size of the message on success, or -1 on failure.
+ *
+ ****************************************************************************/
+
+int res_mkquery(int op, FAR const char *dname, int rclass, int type,
+                FAR const unsigned char *data, int datalen,
+                FAR const unsigned char *newrr,
+                FAR unsigned char *buf, int buflen)
+{
+  FAR unsigned char *cp;
+  uint16_t id;
+  int n;
+
+  UNUSED(data);
+  UNUSED(datalen);
+  UNUSED(newrr);
+
+  if (op < 0 || op > 15 || rclass < 0 || rclass > 0xffff ||
+      type < 0 || type > 0xffff || buf == NULL ||
+      buflen < NS_HFIXEDSZ + 1 + NS_QFIXEDSZ)
+    {
+      return -1;
+    }
+
+  /* Encode the domain name after the header */
+
+  n = dn_comp(dname, buf + NS_HFIXEDSZ,
+              buflen - NS_HFIXEDSZ - NS_QFIXEDSZ, NULL, NULL);
+  if (n < 0)
+    {
+      return -1;
+    }
+
+  /* The header: a random ID, recursion desired and one question */
+
+  memset(buf, 0, NS_HFIXEDSZ);
+  id     = arc4random();
+  buf[0] = id >> 8;
+  buf[1] = id;
+  buf[2] = (op << 3) | 0x01;
+  buf[5] = 1;
+
+  /* The type and class of the question */
+
+  cp = buf + NS_HFIXEDSZ + n;
+  NS_PUT16(type, cp);
+  NS_PUT16(rclass, cp);
+
+  return cp - buf;
+}
diff --git a/libs/libc/netdb/lib_resquery.c b/libs/libc/netdb/lib_resquery.c
new file mode 100644
index 00000000000..e8b63ce7595
--- /dev/null
+++ b/libs/libc/netdb/lib_resquery.c
@@ -0,0 +1,118 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_resquery.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <netdb.h>
+#include <resolv.h>
+
+#include <arpa/nameser.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: res_query
+ *
+ * Description:
+ *   Query the name servers for records of the given class and type for
+ *   the domain name dname, and copy the raw answer into the buffer
+ *   answer.
+ *
+ * Input Parameters:
+ *   dname  - The domain name to query.
+ *   rclass - The class of the query, for example C_IN.
+ *   type   - The type of the query, for example T_SRV.
+ *   answer - The buffer that receives the answer message.
+ *   anslen - The size of answer.
+ *
+ * Returned Value:
+ *   The size of the answer on success.  On failure, -1 is returned and
+ *   h_errno is set to HOST_NOT_FOUND, NO_DATA, NO_RECOVERY or TRY_AGAIN.
+ *
+ ****************************************************************************/
+
+int res_query(FAR const char *dname, int rclass, int type,
+              FAR unsigned char *answer, int anslen)
+{
+  unsigned char query[NS_HFIXEDSZ + NS_MAXCDNAME + NS_QFIXEDSZ];
+  int ret;
+
+  ret = res_mkquery(QUERY, dname, rclass, type, NULL, 0, NULL,
+                    query, sizeof(query));
+  if (ret < 0)
+    {
+      h_errno = NO_RECOVERY;
+      return -1;
+    }
+
+  ret = res_send(query, ret, answer, anslen);
+  if (ret < NS_HFIXEDSZ)
+    {
+      h_errno = TRY_AGAIN;
+      return -1;
+    }
+
+  switch (answer[3] & 0x0f)
+    {
+      case ns_r_noerror:
+        if (answer[6] == 0 && answer[7] == 0)
+          {
+            h_errno = NO_DATA;
+            return -1;
+          }
+
+        return ret;
+
+      case ns_r_nxdomain:
+        h_errno = HOST_NOT_FOUND;
+        break;
+
+      case ns_r_servfail:
+        h_errno = TRY_AGAIN;
+        break;
+
+      default:
+        h_errno = NO_RECOVERY;
+        break;
+    }
+
+  return -1;
+}
+
+/****************************************************************************
+ * Name: res_search
+ *
+ * Description:
+ *   Same as res_query().  NuttX has no search domain list, so the name is
+ *   queried as given.
+ *
+ ****************************************************************************/
+
+int res_search(FAR const char *dname, int rclass, int type,
+               FAR unsigned char *answer, int anslen)
+{
+  return res_query(dname, rclass, type, answer, anslen);
+}
diff --git a/libs/libc/netdb/lib_ressend.c b/libs/libc/netdb/lib_ressend.c
new file mode 100644
index 00000000000..432fc277f93
--- /dev/null
+++ b/libs/libc/netdb/lib_ressend.c
@@ -0,0 +1,191 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_ressend.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <resolv.h>
+
+#include <arpa/nameser.h>
+#include <sys/socket.h>
+
+#include "netdb/lib_dns.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct res_send_s
+{
+  FAR const unsigned char *msg;   /* The query message */
+  int msglen;                     /* The size of the query */
+  FAR unsigned char *answer;      /* The buffer for the answer */
+  int anslen;                     /* The size of the answer buffer */
+  int result;                     /* Size of the answer or negated errno */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: res_send_callback
+ *
+ * Description:
+ *   Send the query to one name server and wait for the answer.
+ *
+ * Returned Value:
+ *   One (1) if an answer was received, zero (0) to try the next name
+ *   server.
+ *
+ ****************************************************************************/
+
+static int res_send_callback(FAR void *arg, FAR struct sockaddr *addr,
+                             socklen_t addrlen)
+{
+  FAR struct res_send_s *rs = arg;
+  ssize_t nrecv;
+  int retries;
+  int rcode;
+  int sd;
+
+  for (retries = 0; retries < CONFIG_NETDB_DNSCLIENT_RETRIES; retries++)
+    {
+      sd = dns_bind(addr->sa_family, false, retries);
+      if (sd < 0)
+        {
+          rs->result = sd;
+          return 0;
+        }
+
+      if (connect(sd, addr, addrlen) < 0 ||
+          send(sd, rs->msg, rs->msglen, 0) < 0)
+        {
+          rs->result = -get_errno();
+          close(sd);
+          return 0;
+        }
+
+      /* Wait for the answer with the ID of the query.  Ignore other
+       * messages.  A timeout sends the query again.
+       */
+
+      for (; ; )
+        {
+          nrecv = recv(sd, rs->answer, rs->anslen, 0);
+          if (nrecv < 0)
+            {
+              rs->result = -get_errno();
+              break;
+            }
+
+          if (nrecv >= NS_HFIXEDSZ &&
+              (rs->answer[2] & 0x80) != 0 &&
+              memcmp(rs->answer, rs->msg, 2) == 0)
+            {
+              break;
+            }
+        }
+
+      close(sd);
+
+      if (nrecv >= 0)
+        {
+          rs->result = nrecv;
+
+          /* After a server failure or a refusal, try the next server */
+
+          rcode = rs->answer[3] & 0x0f;
+          return rcode == ns_r_servfail || rcode == ns_r_refused ? 0 : 1;
+        }
+
+      if (rs->result != -EAGAIN)
+        {
+          break;
+        }
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: res_send
+ *
+ * Description:
+ *   Send the DNS message msg to the configured name servers over UDP and
+ *   copy the answer into the buffer answer.  A server that fails or
+ *   refuses the query is skipped.  An answer that does not fit the buffer
+ *   is truncated.
+ *
+ * Input Parameters:
+ *   msg    - The query message, for example from res_mkquery().
+ *   msglen - The size of the query message.
+ *   answer - The buffer that receives the answer.
+ *   anslen - The size of answer.
+ *
+ * Returned Value:
+ *   The size of the answer on success.  On failure, -1 is returned and
+ *   errno is set.
+ *
+ ****************************************************************************/
+
+int res_send(FAR const unsigned char *msg, int msglen,
+             FAR unsigned char *answer, int anslen)
+{
+  struct res_send_s rs;
+  int ret;
+
+  if (msg == NULL || msglen < NS_HFIXEDSZ || answer == NULL ||
+      anslen < NS_HFIXEDSZ)
+    {
+      set_errno(EINVAL);
+      return -1;
+    }
+
+  rs.msg    = msg;
+  rs.msglen = msglen;
+  rs.answer = answer;
+  rs.anslen = anslen;
+  rs.result = -EADDRNOTAVAIL;
+
+  ret = dns_foreach_nameserver(res_send_callback, &rs);
+  if (ret < 0)
+    {
+      rs.result = ret;
+    }
+
+  if (rs.result < 0)
+    {
+      set_errno(-rs.result);
+      return -1;
+    }
+
+  return rs.result;
+}