Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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
========================================
Expand Down
1 change: 1 addition & 0 deletions Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,12 @@ function gethostbyname(string $hostname): string {}
*/
function gethostbynamel(string $hostname): array|false {}

/**
* @return array<int, string>|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 {}

Expand Down
6 changes: 5 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/standard/basic_functions_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions ext/standard/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Comment thread
iliaal marked this conversation as resolved.
Resolved
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)
{
Expand Down
31 changes: 31 additions & 0 deletions ext/standard/tests/network/gethostaddresses_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
gethostaddresses() basic functionality
--FILE--
<?php
$addresses = gethostaddresses("localhost");
var_dump(is_array($addresses) && array_is_list($addresses) && count($addresses) > 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
9 changes: 9 additions & 0 deletions ext/standard/tests/network/gethostaddresses_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
gethostaddresses() with an unresolvable hostname
--FILE--
<?php
var_dump(gethostaddresses("gethostaddresses.invalid"));
?>
--EXPECTF--
Warning: gethostaddresses(): php_network_getaddresses: getaddrinfo for gethostaddresses.invalid failed: %s in %s on line %d
bool(false)
17 changes: 17 additions & 0 deletions ext/standard/tests/network/gethostaddresses_ipv6.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
gethostaddresses() with an IPv6 literal
--SKIPIF--
<?php
if (@stream_socket_server("tcp://[::1]:0") === false) {
die("skip IPv6 not available");
}
?>
--FILE--
<?php
var_dump(gethostaddresses("::1"));
?>
--EXPECT--
array(1) {
[0]=>
string(3) "::1"
}
Loading