Skip to content

Commit cb2c631

Browse files
committed
ext/sockets: GH-20532 socket_addrinfo_lookup() sets EAI error code on
resolution failures with a new optional argument. update the UPGRADING part
1 parent 7ad406a commit cb2c631

File tree

5 files changed

+234
-5
lines changed

5 files changed

+234
-5
lines changed

UPGRADING

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ PHP 8.6 UPGRADE NOTES
7777
- Phar:
7878
. Phar::mungServer() now supports reference values.
7979

80+
- Sockets:
81+
. socket_addrinfo_lookup() now has an additional optional argument $error
82+
when not null, and on failure, gives the error code (one of the EAI_*
83+
constants).
84+
8085
- Zip:
8186
. ZipArchive::extractTo now raises a TypeError for the
8287
files argument if one or more of the entries is not
@@ -112,6 +117,23 @@ PHP 8.6 UPGRADE NOTES
112117
- Sockets:
113118
. TCP_USER_TIMEOUT (Linux only).
114119
. AF_UNSPEC.
120+
. EAI_BADFLAGS.
121+
. EAI_NONAME.
122+
. EAI_AGAIN.
123+
. EAI_FAIL.
124+
. EAI_NODATA.
125+
. EAI_FAMILY.
126+
. EAI_SOCKTYPE.
127+
. EAI_SERVICE.
128+
. EAI_ADDRFAMILY.
129+
. EAI_SYSTEM.
130+
. EAI_OVERFLOW
131+
. EAI_INPROGRESS.
132+
. EAI_CANCELED.
133+
. EAI_NOTCANCELED.
134+
. EAI_ALLDONE.
135+
. EAI_INTR.
136+
. EAI_IDN_ENCODE.
115137

116138
========================================
117139
11. Changes to INI File Handling

ext/sockets/sockets.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,16 +2751,18 @@ PHP_FUNCTION(socket_addrinfo_lookup)
27512751
{
27522752
zend_string *service = NULL;
27532753
zend_string *hostname, *key;
2754-
zval *hint, *zhints = NULL;
2754+
zval *hint, *zhints = NULL, *error_code = NULL;
2755+
int ret = 0;
27552756

27562757
struct addrinfo hints, *result, *rp;
27572758
php_addrinfo *res;
27582759

2759-
ZEND_PARSE_PARAMETERS_START(1, 3)
2760+
ZEND_PARSE_PARAMETERS_START(1, 4)
27602761
Z_PARAM_STR(hostname)
27612762
Z_PARAM_OPTIONAL
27622763
Z_PARAM_STR_OR_NULL(service)
27632764
Z_PARAM_ARRAY(zhints)
2765+
Z_PARAM_ZVAL_OR_NULL(error_code)
27642766
ZEND_PARSE_PARAMETERS_END();
27652767

27662768
memset(&hints, 0, sizeof(hints));
@@ -2848,7 +2850,10 @@ PHP_FUNCTION(socket_addrinfo_lookup)
28482850
} ZEND_HASH_FOREACH_END();
28492851
}
28502852

2851-
if (getaddrinfo(ZSTR_VAL(hostname), service ? ZSTR_VAL(service) : NULL, &hints, &result) != 0) {
2853+
if ((ret = getaddrinfo(ZSTR_VAL(hostname), service ? ZSTR_VAL(service) : NULL, &hints, &result)) != 0) {
2854+
if (error_code) {
2855+
ZEND_TRY_ASSIGN_REF_LONG(error_code, ret);
2856+
}
28522857
RETURN_FALSE;
28532858
}
28542859

ext/sockets/sockets.stub.php

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,136 @@
20672067
const SHUT_RDWR = UNKNOWN;
20682068
#endif
20692069

2070+
2071+
#ifdef EAI_BADFLAGS
2072+
/**
2073+
* @var int
2074+
* @cvalue EAI_BADFLAGS
2075+
*/
2076+
const EAI_BADFLAGS = UNKNOWN;
2077+
#endif
2078+
#ifdef EAI_NONAME
2079+
/**
2080+
* @var int
2081+
* @cvalue EAI_NONAME
2082+
*/
2083+
const EAI_NONAME = UNKNOWN;
2084+
#endif
2085+
#ifdef EAI_AGAIN
2086+
/**
2087+
* @var int
2088+
* @cvalue EAI_AGAIN
2089+
*/
2090+
const EAI_AGAIN = UNKNOWN;
2091+
#endif
2092+
#ifdef EAI_FAIL
2093+
/**
2094+
* @var int
2095+
* @cvalue EAI_FAIL
2096+
*/
2097+
const EAI_FAIL = UNKNOWN;
2098+
#endif
2099+
#ifdef EAI_NODATA
2100+
/**
2101+
* @var int
2102+
* @cvalue EAI_NODATA
2103+
*/
2104+
const EAI_NODATA = UNKNOWN;
2105+
#endif
2106+
#ifdef EAI_FAMILY
2107+
/**
2108+
* @var int
2109+
* @cvalue EAI_FAMILY
2110+
*/
2111+
const EAI_FAMILY = UNKNOWN;
2112+
#endif
2113+
#ifdef EAI_SOCKTYPE
2114+
/**
2115+
* @var int
2116+
* @cvalue EAI_SOCKTYPE
2117+
*/
2118+
const EAI_SOCKTYPE = UNKNOWN;
2119+
#endif
2120+
#ifdef EAI_SERVICE
2121+
/**
2122+
* @var int
2123+
* @cvalue EAI_SERVICE
2124+
*/
2125+
const EAI_SERVICE = UNKNOWN;
2126+
#endif
2127+
#ifdef EAI_ADDRFAMILY
2128+
/**
2129+
* @var int
2130+
* @cvalue EAI_ADDRFAMILY
2131+
*/
2132+
const EAI_ADDRFAMILY = UNKNOWN;
2133+
#else
2134+
#ifdef EAI_FAMILY
2135+
/**
2136+
* @var int
2137+
* @cvalue EAI_FAMILY
2138+
*/
2139+
const EAI_ADDRFAMILY = UNKNOWN;
2140+
#else
2141+
#endif
2142+
#endif
2143+
#ifdef EAI_SYSTEM
2144+
/**
2145+
* @var int
2146+
* @cvalue EAI_SYSTEM
2147+
*/
2148+
const EAI_SYSTEM = UNKNOWN;
2149+
#endif
2150+
#ifdef EAI_OVERFLOW
2151+
/**
2152+
* @var int
2153+
* @cvalue EAI_OVERFLOW
2154+
*/
2155+
const EAI_OVERFLOW = UNKNOWN;
2156+
#endif
2157+
#ifdef EAI_INPROGRESS
2158+
/**
2159+
* @var int
2160+
* @cvalue EAI_INPROGRESS
2161+
*/
2162+
const EAI_INPROGRESS = UNKNOWN;
2163+
#endif
2164+
#ifdef EAI_CANCELED
2165+
/**
2166+
* @var int
2167+
* @cvalue EAI_CANCELED
2168+
*/
2169+
const EAI_CANCELED = UNKNOWN;
2170+
#endif
2171+
#ifdef EAI_NOTCANCELED
2172+
/**
2173+
* @var int
2174+
* @cvalue EAI_NOTCANCELED
2175+
*/
2176+
const EAI_NOTCANCELED = UNKNOWN;
2177+
#endif
2178+
#ifdef EAI_ALLDONE
2179+
/**
2180+
* @var int
2181+
* @cvalue EAI_ALLDONE
2182+
*/
2183+
const EAI_ALLDONE = UNKNOWN;
2184+
#endif
2185+
#ifdef EAI_INTR
2186+
/**
2187+
* @var int
2188+
* @cvalue EAI_INTR
2189+
*/
2190+
const EAI_INTR = UNKNOWN;
2191+
#endif
2192+
#ifdef EAI_IDN_ENCODE
2193+
/**
2194+
* @var int
2195+
* @cvalue EAI_IDN_ENCODE
2196+
*/
2197+
const EAI_IDN_ENCODE = UNKNOWN;
2198+
#endif
2199+
20702200
/**
20712201
* @strict-properties
20722202
* @not-serializable
@@ -2187,9 +2317,10 @@ function socket_cmsg_space(int $level, int $type, int $num = 0): ?int {}
21872317

21882318
/**
21892319
* @return array<int, AddressInfo>|false
2320+
* @param int $error_code
21902321
* @refcount 1
21912322
*/
2192-
function socket_addrinfo_lookup(string $host, ?string $service = null, array $hints = []): array|false {}
2323+
function socket_addrinfo_lookup(string $host, ?string $service = null, array $hints = [], &$error_code = null): array|false {}
21932324

21942325
function socket_addrinfo_connect(AddressInfo $address): Socket|false {}
21952326

ext/sockets/sockets_arginfo.h

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

ext/sockets/tests/gh20532.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-20562 - socket_addrinfo_lookup() returns error codes on resolution failures.
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
$error_code = 0;
8+
var_dump(socket_addrinfo_lookup(".whynot", null, [], $error_code) === false && $error_code === EAI_NONAME);
9+
var_dump(socket_addrinfo_lookup("2001:db8::1", null, ['ai_family' => AF_INET], $error_code) === false && in_array($error_code, [EAI_FAMILY, EAI_ADDRFAMILY, EAI_NONAME, EAI_NODATA]));
10+
var_dump(socket_addrinfo_lookup("example.com", "http", ['ai_socktype' => SOCK_RAW, 'ai_flags' => 2147483647], $error_code) === false && in_array($error_code, [EAI_SOCKTYPE, EAI_SERVICE, EAI_BADFLAGS, EAI_NONAME]));
11+
?>
12+
--EXPECT--
13+
bool(true)
14+
bool(true)
15+
bool(true)
16+

0 commit comments

Comments
 (0)