1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/sockets: socket_addrinfo_lookup() allows AF_UNSPEC for ai_family. (#20658)

while still filtering out IPC like addresses and so on.

close GH-20658
This commit is contained in:
David CARLIER
2026-01-15 19:24:56 +00:00
committed by GitHub
parent 0ab1f9f223
commit 9b719cd4a3
5 changed files with 30 additions and 12 deletions

4
NEWS
View File

@@ -80,7 +80,9 @@ PHP NEWS
- Sockets:
. Added the TCP_USER_TIMEOUT constant for Linux to set the maximum time in milliseconds
transmitted data can remain unacknowledged. (James Lucas)
transmitted data can remain unacknowledged. (James Lucas)
. Added AF_UNSPEC support for sock_addrinfo_lookup() as a sole umbrella for
AF_INET* family only. (David Carlier)
- SPL:
. DirectoryIterator key can now work better with filesystem supporting larger

View File

@@ -111,6 +111,7 @@ PHP 8.6 UPGRADE NOTES
- Sockets:
. TCP_USER_TIMEOUT (Linux only).
. AF_UNSPEC.
========================================
11. Changes to INI File Handling

View File

@@ -2749,8 +2749,7 @@ PHP_FUNCTION(socket_export_stream)
/* {{{ Gets array with contents of getaddrinfo about the given hostname. */
PHP_FUNCTION(socket_addrinfo_lookup)
{
char *service = NULL;
size_t service_len = 0;
zend_string *service = NULL;
zend_string *hostname, *key;
zval *hint, *zhints = NULL;
@@ -2760,7 +2759,7 @@ PHP_FUNCTION(socket_addrinfo_lookup)
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(hostname)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(service, service_len)
Z_PARAM_STR_OR_NULL(service)
Z_PARAM_ARRAY(zhints)
ZEND_PARSE_PARAMETERS_END();
@@ -2824,14 +2823,16 @@ PHP_FUNCTION(socket_addrinfo_lookup)
// Some platforms support also PF_LOCAL/AF_UNIX (e.g. FreeBSD) but the security concerns implied
// make it not worth handling it (e.g. unwarranted write permissions on the socket).
// Note existing socket_addrinfo* api already forbid such case.
if (val != AF_UNSPEC) {
#ifdef HAVE_IPV6
if (val != AF_INET && val != AF_INET6) {
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET or AF_INET6");
if (val != AF_INET && val != AF_INET6) {
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET or AF_INET6");
#else
if (val != AF_INET) {
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET");
if (val != AF_INET) {
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET");
#endif
RETURN_THROWS();
RETURN_THROWS();
}
}
hints.ai_family = (int)val;
} else {
@@ -2847,7 +2848,7 @@ PHP_FUNCTION(socket_addrinfo_lookup)
} ZEND_HASH_FOREACH_END();
}
if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
if (getaddrinfo(ZSTR_VAL(hostname), service ? ZSTR_VAL(service) : NULL, &hints, &result) != 0) {
RETURN_FALSE;
}
@@ -2855,7 +2856,11 @@ PHP_FUNCTION(socket_addrinfo_lookup)
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
for (rp = result; rp != NULL; rp = rp->ai_next) {
if (rp->ai_family != AF_UNSPEC) {
if (rp->ai_family == AF_INET
#ifdef HAVE_IPV6
|| rp->ai_family == AF_INET6
#endif
) {
zval zaddr;
object_init_ex(&zaddr, address_info_ce);

View File

@@ -19,6 +19,13 @@ const AF_INET = UNKNOWN;
*/
const AF_INET6 = UNKNOWN;
#endif
#ifdef AF_UNSPEC
/**
* @var int
* @cvalue AF_UNSPEC
*/
const AF_UNSPEC = UNKNOWN;
#endif
#ifdef AF_DIVERT
/**
* @var int

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 038081ca7bb98076d4b559d93b4c9300acc47160 */
* Stub hash: a89c4e54ab913728e10baf8f32b45323495d685b */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_socket_select, 0, 4, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(1, read, IS_ARRAY, 1)
@@ -317,6 +317,9 @@ static void register_sockets_symbols(int module_number)
#if defined(HAVE_IPV6)
REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_PERSISTENT);
#endif
#if defined(AF_UNSPEC)
REGISTER_LONG_CONSTANT("AF_UNSPEC", AF_UNSPEC, CONST_PERSISTENT);
#endif
#if defined(AF_DIVERT)
REGISTER_LONG_CONSTANT("AF_DIVERT", AF_DIVERT, CONST_PERSISTENT);
#endif