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


The following commit(s) were added to refs/heads/master by this push:
     new e7043828a7 netdb: Regard hosts file prior to DNS when resolving
e7043828a7 is described below

commit e7043828a73be3dc3908b9218926fa3abecaf4c5
Author: Zhe Weng <[email protected]>
AuthorDate: Thu May 18 15:28:25 2023 +0800

    netdb: Regard hosts file prior to DNS when resolving
    
    - Linux: What inside /etc/hosts comes first.
    - NuttX: Even if we write a domain in /etc/hosts, we still use DNS
             result instead of hosts lines. This patch change this behavior.
    
    Signed-off-by: Zhe Weng <[email protected]>
---
 libs/libc/netdb/lib_gethostentbynamer.c | 49 ++++++++++++---------------------
 1 file changed, 18 insertions(+), 31 deletions(-)

diff --git a/libs/libc/netdb/lib_gethostentbynamer.c 
b/libs/libc/netdb/lib_gethostentbynamer.c
index 6285b39dc2..8fd8df5ad9 100644
--- a/libs/libc/netdb/lib_gethostentbynamer.c
+++ b/libs/libc/netdb/lib_gethostentbynamer.c
@@ -576,10 +576,9 @@ static int lib_dns_lookup(FAR const char *name, FAR struct 
hostent_s *host,
 #ifdef CONFIG_NETDB_HOSTFILE
 static int lib_hostfile_lookup(FAR const char *name,
                                FAR struct hostent_s *host, FAR char *buf,
-                               size_t buflen, FAR int *h_errnop)
+                               size_t buflen)
 {
   FAR FILE *stream;
-  int herrnocode;
   int nread;
 
   /* Search the hosts file for a match */
@@ -591,10 +590,8 @@ static int lib_hostfile_lookup(FAR const char *name,
 
       nerr("ERROR:  Failed to open the hosts file %s: %d\n",
            CONFIG_NETDB_HOSTCONF_PATH, errcode);
-      UNUSED(errcode);
 
-      herrnocode = NO_RECOVERY;
-      goto errorout_with_herrnocode;
+      return errcode;
     }
 
   /* Loop reading entries from the hosts file until a match is found or
@@ -620,8 +617,8 @@ static int lib_hostfile_lookup(FAR const char *name,
             }
           else if (nread != -EAGAIN)
             {
-              herrnocode = NO_RECOVERY;
-              goto errorout_with_stream;
+              fclose(stream);
+              return nread;
             }
         }
       else if (nread > 0)
@@ -664,21 +661,11 @@ static int lib_hostfile_lookup(FAR const char *name,
   while (nread != 0);
 
   /* We get here when the end of the hosts file is encountered without
-   * finding the hostname.
+   * finding the hostname.  Return 1 meaning that we have no errors but
+   * no match either.
    */
 
-  herrnocode = HOST_NOT_FOUND;
-
-errorout_with_stream:
-  fclose(stream);
-
-errorout_with_herrnocode:
-  if (h_errnop)
-    {
-      *h_errnop = herrnocode;
-    }
-
-  return ERROR;
+  return 1;
 }
 #endif /* CONFIG_NETDB_HOSTFILE */
 
@@ -756,6 +743,17 @@ int gethostentbyname_r(FAR const char *name,
     }
 #endif
 
+#ifdef CONFIG_NETDB_HOSTFILE
+  /* Search the hosts file for a match */
+
+  if (lib_hostfile_lookup(name, host, buf, buflen) == 0)
+    {
+      /* Found the host in hosts file */
+
+      return OK;
+    }
+#endif
+
   /* Try to find the name in the HOSTALIASES environment variable */
 
 #ifdef CONFIG_NETDB_DNSCLIENT
@@ -780,23 +778,12 @@ int gethostentbyname_r(FAR const char *name,
     }
 #endif /* CONFIG_NETDB_DNSCLIENT */
 
-#ifdef CONFIG_NETDB_HOSTFILE
-  /* Search the hosts file for a match */
-
-  return lib_hostfile_lookup(name, host, buf, buflen, h_errnop);
-
-#else
-  /* The host file file is not supported.  The host name mapping was not
-   * found from any lookup heuristic
-   */
-
   if (h_errnop)
     {
       *h_errnop = HOST_NOT_FOUND;
     }
 
   return ERROR;
-#endif
 }
 
 #endif /* CONFIG_LIBC_NETDB */

Reply via email to