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

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

commit aa678c65a7a573c00c1ca1ef5caae1ed34a29790
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Oct 21 17:21:15 2026 +0200

    libs/libc/netdb: Add res_init() and dn_skipname().
    
    A program that sends DNS queries with res_query() often calls res_init()
    first, and it skips the names of the records in the answer with
    dn_skipname().  glibc, musl and the BSDs have both functions.  NuttX has
    neither, so such programs do not compile.
    
    res_init() returns 0: the DNS client gets the name servers at each
    query, so there is no state to read again.  It is built with
    CONFIG_NETDB_DNSCLIENT, as res_query() is.  dn_skipname() gives the size
    of a compressed domain name without expanding it.  It is in lib_dn.c
    with dn_comp() and dn_expand().  Declare both in <resolv.h>.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/resolv.h               |  5 ++++
 libs/libc/netdb/CMakeLists.txt |  3 ++-
 libs/libc/netdb/Make.defs      |  2 +-
 libs/libc/netdb/lib_dn.c       | 34 +++++++++++++++++++++++++++
 libs/libc/netdb/lib_resinit.c  | 53 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 95 insertions(+), 2 deletions(-)

diff --git a/include/resolv.h b/include/resolv.h
index 216dfd068de..b4431405cec 100644
--- a/include/resolv.h
+++ b/include/resolv.h
@@ -54,8 +54,13 @@ 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);
 
+int dn_skipname(FAR const unsigned char *src,
+                FAR const unsigned char *end);
+
 /* DNS queries */
 
+int res_init(void);
+
 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,
diff --git a/libs/libc/netdb/CMakeLists.txt b/libs/libc/netdb/CMakeLists.txt
index 20c2ad208f4..a909195839c 100644
--- a/libs/libc/netdb/CMakeLists.txt
+++ b/libs/libc/netdb/CMakeLists.txt
@@ -63,7 +63,8 @@ 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 lib_ressend.c lib_resquery.c)
+    list(APPEND SRCS lib_dnsqueryfamily.c lib_ressend.c lib_resquery.c
+         lib_resinit.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 a335bf52aab..1e3da679157 100644
--- a/libs/libc/netdb/Make.defs
+++ b/libs/libc/netdb/Make.defs
@@ -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 lib_ressend.c lib_resquery.c
+CSRCS += lib_dnsqueryfamily.c lib_ressend.c lib_resquery.c lib_resinit.c
 
 ifneq ($(CONFIG_NETDB_DNSCLIENT_ENTRIES),0)
 CSRCS += lib_dnscache.c
diff --git a/libs/libc/netdb/lib_dn.c b/libs/libc/netdb/lib_dn.c
index 20d61f95252..a229c8ea447 100644
--- a/libs/libc/netdb/lib_dn.c
+++ b/libs/libc/netdb/lib_dn.c
@@ -317,3 +317,37 @@ int dn_expand(FAR const unsigned char *base, FAR const 
unsigned char *end,
 
   return -1;
 }
+
+int dn_skipname(FAR const unsigned char *src, FAR const unsigned char *end)
+{
+  FAR const unsigned char *p = src;
+
+  while (p < end)
+    {
+      if (*p == 0)
+        {
+          /* The root label ends the name */
+
+          return p + 1 - src;
+        }
+      else if ((*p & 0xc0) == 0xc0)
+        {
+          /* A compression pointer (two bytes) ends the name */
+
+          if (end - p < 2)
+            {
+              break;
+            }
+
+          return p + 2 - src;
+        }
+      else if ((*p & 0xc0) != 0 || end - p < *p + 1)
+        {
+          break;
+        }
+
+      p += *p + 1;
+    }
+
+  return -1;
+}
diff --git a/libs/libc/netdb/lib_resinit.c b/libs/libc/netdb/lib_resinit.c
new file mode 100644
index 00000000000..d6e8fd72772
--- /dev/null
+++ b/libs/libc/netdb/lib_resinit.c
@@ -0,0 +1,53 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_resinit.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 <resolv.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: res_init
+ *
+ * Description:
+ *   Initialize the resolver.  The DNS client gets the name servers at each
+ *   query: from CONFIG_NETDB_RESOLVCONF_PATH with CONFIG_NETDB_RESOLVCONF,
+ *   otherwise from the list that dns_add_nameserver() changes.  Thus there
+ *   is no state to initialize or to read again.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   Always zero.
+ *
+ ****************************************************************************/
+
+int res_init(void)
+{
+  return 0;
+}

Reply via email to