Skip to content

Commit 9b719cd

Browse files
authored
ext/sockets: socket_addrinfo_lookup() allows AF_UNSPEC for ai_family. (php#20658)
while still filtering out IPC like addresses and so on. close phpGH-20658
1 parent 0ab1f9f commit 9b719cd

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ PHP NEWS
8080

8181
- Sockets:
8282
. Added the TCP_USER_TIMEOUT constant for Linux to set the maximum time in milliseconds
83-
transmitted data can remain unacknowledged. (James Lucas)
83+
transmitted data can remain unacknowledged. (James Lucas)
84+
. Added AF_UNSPEC support for sock_addrinfo_lookup() as a sole umbrella for
85+
AF_INET* family only. (David Carlier)
8486

8587
- SPL:
8688
. DirectoryIterator key can now work better with filesystem supporting larger

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ PHP 8.6 UPGRADE NOTES
111111

112112
- Sockets:
113113
. TCP_USER_TIMEOUT (Linux only).
114+
. AF_UNSPEC.
114115

115116
========================================
116117
11. Changes to INI File Handling

ext/sockets/sockets.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,8 +2749,7 @@ PHP_FUNCTION(socket_export_stream)
27492749
/* {{{ Gets array with contents of getaddrinfo about the given hostname. */
27502750
PHP_FUNCTION(socket_addrinfo_lookup)
27512751
{
2752-
char *service = NULL;
2753-
size_t service_len = 0;
2752+
zend_string *service = NULL;
27542753
zend_string *hostname, *key;
27552754
zval *hint, *zhints = NULL;
27562755

@@ -2760,7 +2759,7 @@ PHP_FUNCTION(socket_addrinfo_lookup)
27602759
ZEND_PARSE_PARAMETERS_START(1, 3)
27612760
Z_PARAM_STR(hostname)
27622761
Z_PARAM_OPTIONAL
2763-
Z_PARAM_STRING_OR_NULL(service, service_len)
2762+
Z_PARAM_STR_OR_NULL(service)
27642763
Z_PARAM_ARRAY(zhints)
27652764
ZEND_PARSE_PARAMETERS_END();
27662765

@@ -2824,14 +2823,16 @@ PHP_FUNCTION(socket_addrinfo_lookup)
28242823
// Some platforms support also PF_LOCAL/AF_UNIX (e.g. FreeBSD) but the security concerns implied
28252824
// make it not worth handling it (e.g. unwarranted write permissions on the socket).
28262825
// Note existing socket_addrinfo* api already forbid such case.
2826+
if (val != AF_UNSPEC) {
28272827
#ifdef HAVE_IPV6
2828-
if (val != AF_INET && val != AF_INET6) {
2829-
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET or AF_INET6");
2828+
if (val != AF_INET && val != AF_INET6) {
2829+
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET or AF_INET6");
28302830
#else
2831-
if (val != AF_INET) {
2832-
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET");
2831+
if (val != AF_INET) {
2832+
zend_argument_value_error(3, "\"ai_family\" key must be AF_INET");
28332833
#endif
2834-
RETURN_THROWS();
2834+
RETURN_THROWS();
2835+
}
28352836
}
28362837
hints.ai_family = (int)val;
28372838
} else {
@@ -2847,15 +2848,19 @@ PHP_FUNCTION(socket_addrinfo_lookup)
28472848
} ZEND_HASH_FOREACH_END();
28482849
}
28492850

2850-
if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
2851+
if (getaddrinfo(ZSTR_VAL(hostname), service ? ZSTR_VAL(service) : NULL, &hints, &result) != 0) {
28512852
RETURN_FALSE;
28522853
}
28532854

28542855
array_init(return_value);
28552856
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
28562857

28572858
for (rp = result; rp != NULL; rp = rp->ai_next) {
2858-
if (rp->ai_family != AF_UNSPEC) {
2859+
if (rp->ai_family == AF_INET
2860+
#ifdef HAVE_IPV6
2861+
|| rp->ai_family == AF_INET6
2862+
#endif
2863+
) {
28592864
zval zaddr;
28602865

28612866
object_init_ex(&zaddr, address_info_ce);

ext/sockets/sockets.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
*/
2020
const AF_INET6 = UNKNOWN;
2121
#endif
22+
#ifdef AF_UNSPEC
23+
/**
24+
* @var int
25+
* @cvalue AF_UNSPEC
26+
*/
27+
const AF_UNSPEC = UNKNOWN;
28+
#endif
2229
#ifdef AF_DIVERT
2330
/**
2431
* @var int

ext/sockets/sockets_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)