From 23cbbfa101119c32deba038c8c9d0c7b6b401f1f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 27 Nov 2026 20:44:40 +0000 Subject: [PATCH 1/2] ext/standard: Add gethostaddresses(). Resolves a hostname to its IPv4 and IPv6 addresses through the system resolver, unlike gethostbynamel() which is IPv4 only. Fix GH-25446 --- UPGRADING | 4 ++ ext/standard/basic_functions.stub.php | 6 +++ ext/standard/basic_functions_arginfo.h | 6 ++- ext/standard/basic_functions_decl.h | 8 +-- ext/standard/dns.c | 50 +++++++++++++++++++ .../tests/network/gethostaddresses_basic.phpt | 31 ++++++++++++ .../tests/network/gethostaddresses_error.phpt | 9 ++++ .../tests/network/gethostaddresses_ipv6.phpt | 17 +++++++ 8 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 ext/standard/tests/network/gethostaddresses_basic.phpt create mode 100644 ext/standard/tests/network/gethostaddresses_error.phpt create mode 100644 ext/standard/tests/network/gethostaddresses_ipv6.phpt diff --git a/UPGRADING b/UPGRADING index fcff11aabf0b..2ed3ede4744e 100644 --- a/UPGRADING +++ b/UPGRADING @@ -161,6 +161,10 @@ PHP 8.7 UPGRADE NOTES . Added Spoofchecker::getRestrictionLevel() to retrieve the configured restriction level as one of the Spoofchecker restriction level constants. +- Standard: + . Added gethostaddresses() to resolve a hostname to its IPv4 and IPv6 + addresses through the system resolver. + ======================================== 7. New Classes and Interfaces ======================================== diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index ec0645720b8c..2a1ea35e42f2 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -2146,6 +2146,12 @@ function gethostbyname(string $hostname): string {} */ function gethostbynamel(string $hostname): array|false {} +/** + * @return array|false + * @refcount 1 + */ +function gethostaddresses(string $hostname): array|false {} + #if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC)) function dns_check_record(string $hostname, string $type = "MX"): bool {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 62fe3f768665..57eeddff3449 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 + * Stub hash: 1163b1b819d15ad8f683439058ca21d501a1ab0f * Has decl header: yes */ #include "zend_attributes.h" @@ -656,6 +656,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_gethostbynamel, 0, 1, MAY_BE_ARR ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) ZEND_END_ARG_INFO() +#define arginfo_gethostaddresses arginfo_gethostbynamel + #if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC)) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dns_check_record, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -2536,6 +2538,7 @@ ZEND_FUNCTION(gethostname); ZEND_FUNCTION(gethostbyaddr); ZEND_FUNCTION(gethostbyname); ZEND_FUNCTION(gethostbynamel); +ZEND_FUNCTION(gethostaddresses); #if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC)) ZEND_FUNCTION(dns_check_record); ZEND_FUNCTION(dns_get_record); @@ -3144,6 +3147,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(gethostbyaddr, arginfo_gethostbyaddr) ZEND_FE(gethostbyname, arginfo_gethostbyname) ZEND_FE(gethostbynamel, arginfo_gethostbynamel) + ZEND_FE(gethostaddresses, arginfo_gethostaddresses) #if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC)) ZEND_FE(dns_check_record, arginfo_dns_check_record) ZEND_RAW_FENTRY("checkdnsrr", zif_dns_check_record, arginfo_checkdnsrr, 0, NULL, NULL) diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h index a7614da39591..e943c57246df 100644 --- a/ext/standard/basic_functions_decl.h +++ b/ext/standard/basic_functions_decl.h @@ -1,8 +1,8 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 */ + * Stub hash: 1163b1b819d15ad8f683439058ca21d501a1ab0f */ -#ifndef ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H -#define ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H +#ifndef ZEND_BASIC_FUNCTIONS_DECL_1163b1b819d15ad8f683439058ca21d501a1ab0f_H +#define ZEND_BASIC_FUNCTIONS_DECL_1163b1b819d15ad8f683439058ca21d501a1ab0f_H typedef enum zend_enum_SortDirection { ZEND_ENUM_SortDirection_Ascending = 1, @@ -20,4 +20,4 @@ typedef enum zend_enum_RoundingMode { ZEND_ENUM_RoundingMode_PositiveInfinity = 8, } zend_enum_RoundingMode; -#endif /* ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H */ +#endif /* ZEND_BASIC_FUNCTIONS_DECL_1163b1b819d15ad8f683439058ca21d501a1ab0f_H */ diff --git a/ext/standard/dns.c b/ext/standard/dns.c index c9983893b856..1bf353a73294 100644 --- a/ext/standard/dns.c +++ b/ext/standard/dns.c @@ -287,6 +287,56 @@ PHP_FUNCTION(gethostbynamel) } /* }}} */ +/* {{{ Return a list of IPv4 and IPv6 addresses that a given hostname resolves to. */ +PHP_FUNCTION(gethostaddresses) +{ + char *hostname; + size_t hostname_len; + struct sockaddr **sal, **sap; +#ifdef HAVE_IPV6 + char addr[INET6_ADDRSTRLEN]; +#else + char addr[INET_ADDRSTRLEN]; +#endif + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_PATH(hostname, hostname_len) + ZEND_PARSE_PARAMETERS_END(); + + if (hostname_len > MAXFQDNLEN) { + php_error_docref(NULL, E_WARNING, "Host name cannot be longer than %d characters", MAXFQDNLEN); + RETURN_FALSE; + } + + if (!php_network_getaddresses(hostname, SOCK_STREAM, &sal, NULL)) { + RETURN_FALSE; + } + + array_init(return_value); + + for (sap = sal; *sap != NULL; sap++) { + const char *ipaddr = NULL; + + switch ((*sap)->sa_family) { + case AF_INET: + ipaddr = inet_ntop(AF_INET, &((struct sockaddr_in *) *sap)->sin_addr, addr, sizeof(addr)); + break; +#ifdef HAVE_IPV6 + case AF_INET6: + ipaddr = inet_ntop(AF_INET6, &((struct sockaddr_in6 *) *sap)->sin6_addr, addr, sizeof(addr)); + break; +#endif + } + + if (ipaddr) { + add_next_index_string(return_value, ipaddr); + } + } + + php_network_freeaddresses(sal); +} +/* }}} */ + /* {{{ php_gethostbyname */ static zend_string *php_gethostbyname(char *name) { diff --git a/ext/standard/tests/network/gethostaddresses_basic.phpt b/ext/standard/tests/network/gethostaddresses_basic.phpt new file mode 100644 index 000000000000..2c30d3fb361e --- /dev/null +++ b/ext/standard/tests/network/gethostaddresses_basic.phpt @@ -0,0 +1,31 @@ +--TEST-- +gethostaddresses() basic functionality +--FILE-- + 0); +foreach ($addresses as $address) { + if (filter_var($address, FILTER_VALIDATE_IP) === false) { + echo "Invalid address: ", $address, PHP_EOL; + } +} + +var_dump(gethostaddresses("127.0.0.1")); +var_dump(gethostaddresses(str_repeat("a", 2000))); + +try { + gethostaddresses("local\0host"); +} catch (ValueError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} +?> +--EXPECTF-- +bool(true) +array(1) { + [0]=> + string(9) "127.0.0.1" +} + +Warning: gethostaddresses(): Host name cannot be longer than %d characters in %s on line %d +bool(false) +ValueError: gethostaddresses(): Argument #1 ($hostname) must not contain any null bytes diff --git a/ext/standard/tests/network/gethostaddresses_error.phpt b/ext/standard/tests/network/gethostaddresses_error.phpt new file mode 100644 index 000000000000..bced71b63d5a --- /dev/null +++ b/ext/standard/tests/network/gethostaddresses_error.phpt @@ -0,0 +1,9 @@ +--TEST-- +gethostaddresses() with an unresolvable hostname +--FILE-- + +--EXPECTF-- +Warning: gethostaddresses(): php_network_getaddresses: getaddrinfo for gethostaddresses.invalid failed: %s in %s on line %d +bool(false) diff --git a/ext/standard/tests/network/gethostaddresses_ipv6.phpt b/ext/standard/tests/network/gethostaddresses_ipv6.phpt new file mode 100644 index 000000000000..2bc68977737b --- /dev/null +++ b/ext/standard/tests/network/gethostaddresses_ipv6.phpt @@ -0,0 +1,17 @@ +--TEST-- +gethostaddresses() with an IPv6 literal +--SKIPIF-- + +--FILE-- + +--EXPECT-- +array(1) { + [0]=> + string(3) "::1" +} From 691ec70125fc7258822c6670ce72942042b41041 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 27 Nov 2026 21:31:24 +0000 Subject: [PATCH 2/2] fix build --- Zend/Optimizer/zend_func_infos.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h index 0f3121fb945f..d16a66bda2d1 100644 --- a/Zend/Optimizer/zend_func_infos.h +++ b/Zend/Optimizer/zend_func_infos.h @@ -442,6 +442,7 @@ static const func_info_t func_infos[] = { F1("gethostbyaddr", MAY_BE_STRING|MAY_BE_FALSE), F1("gethostbyname", MAY_BE_STRING), F1("gethostbynamel", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE), + F1("gethostaddresses", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE), #if (defined(PHP_WIN32) || defined(HAVE_DNS_SEARCH_FUNC)) F1("dns_get_record", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_FALSE), #endif