This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI 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 e8dd6db666c libs/libc/netdb: gethostbyname() gives AF_INET where the
system has IPv4.
e8dd6db666c is described below
commit e8dd6db666c69fe7ce764447e63e21efc29d2144
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 5 20:27:17 2026 +0100
libs/libc/netdb: gethostbyname() gives AF_INET where the system has IPv4.
gethostbyname() and gethostbyname_r() asked for AF_UNSPEC. On a system with
IPv4 and IPv6, the first address of the resolver then decides the family of
the result. For a name with both kinds of address, a program can get an
IPv6 address. Programs copy h_addr_list[0] into a struct sockaddr_in, as
POSIX lets them: this function is the IPv4 interface, and getaddrinfo() is
the one for both families.
With CONFIG_NET_IPv4 they now ask for AF_INET. A name with IPv6 addresses
only then gives no result, and neither does an IPv6 literal. Without
CONFIG_NET_IPv4 nothing changes: AF_UNSPEC gives the IPv6 addresses.
gethostbyname2() and gethostbyname2_r() still give the family that the
caller asks for.
The change is in the C library and is the same in flat, protected and kernel
builds, on every architecture.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
libs/libc/netdb/lib_gethostbyname.c | 8 +++++++-
libs/libc/netdb/lib_gethostbynamer.c | 7 ++++++-
libs/libc/netdb/lib_netdb.h | 11 +++++++++++
3 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/libs/libc/netdb/lib_gethostbyname.c
b/libs/libc/netdb/lib_gethostbyname.c
index e2014614134..b514df0461d 100644
--- a/libs/libc/netdb/lib_gethostbyname.c
+++ b/libs/libc/netdb/lib_gethostbyname.c
@@ -28,6 +28,8 @@
#include <netdb.h>
+#include "netdb/lib_netdb.h"
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -46,6 +48,10 @@
* and its struct in_addr equivalent into the h_addr_list[0] field of the
* returned hostent structure.
*
+ * With CONFIG_NET_IPv4 this function gives only IPv4 addresses, also on
+ * a system that has IPv6: programs copy h_addr_list[0] into a struct
+ * sockaddr_in. Use getaddrinfo() or gethostbyname2() for IPv6.
+ *
* Input Parameters:
* name - The name of the host to find.
*
@@ -62,6 +68,6 @@
FAR struct hostent *gethostbyname(FAR const char *name)
{
- return gethostbyname2(name, AF_UNSPEC);
+ return gethostbyname2(name, NETDB_HOSTBYNAME_FAMILY);
}
diff --git a/libs/libc/netdb/lib_gethostbynamer.c
b/libs/libc/netdb/lib_gethostbynamer.c
index 4218e7c1b5b..966c79e9759 100644
--- a/libs/libc/netdb/lib_gethostbynamer.c
+++ b/libs/libc/netdb/lib_gethostbynamer.c
@@ -28,6 +28,8 @@
#include <netdb.h>
+#include "netdb/lib_netdb.h"
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -49,6 +51,9 @@
* gethostname_r() is *not* POSIX but is similar to a Glibc extension and
* is used internally by NuttX to implement the POSIX gethostname().
*
+ * With CONFIG_NET_IPv4 this function gives only IPv4 addresses, as
+ * gethostbyname() does. Use gethostbyname2_r() for IPv6.
+ *
* Input Parameters:
* name - The name of the host to find.
* host - Caller provided location to return the host data.
@@ -69,7 +74,7 @@ int gethostbyname_r(FAR const char *name,
size_t buflen, FAR struct hostent **result,
FAR int *h_errnop)
{
- return gethostbyname2_r(name, AF_UNSPEC,
+ return gethostbyname2_r(name, NETDB_HOSTBYNAME_FAMILY,
host, buf, buflen, result, h_errnop);
}
diff --git a/libs/libc/netdb/lib_netdb.h b/libs/libc/netdb/lib_netdb.h
index 48da805da7f..33c54f7c1ce 100644
--- a/libs/libc/netdb/lib_netdb.h
+++ b/libs/libc/netdb/lib_netdb.h
@@ -32,6 +32,7 @@
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
+#include <sys/socket.h>
#ifdef CONFIG_LIBC_NETDB
@@ -70,6 +71,16 @@
#define CONFIG_NETDB_MAX_IPADDR (CONFIG_NETDB_MAX_IPv4ADDR + \
CONFIG_NETDB_MAX_IPv6ADDR)
+/* The address family of gethostbyname() and gethostbyname_r: IPv4 where the
+ * system has it, whatever else the system has.
+ */
+
+#ifdef CONFIG_NET_IPv4
+# define NETDB_HOSTBYNAME_FAMILY AF_INET
+#else
+# define NETDB_HOSTBYNAME_FAMILY AF_UNSPEC
+#endif
+
/****************************************************************************
* Public Types
****************************************************************************/