@@ -332,25 +332,38 @@ private static function dnsResolve(string $host, NativeClientState $multi, array
332
332
{
333
333
$ flag = '' !== $ host && '[ ' === $ host [0 ] && '] ' === $ host [-1 ] && str_contains ($ host , ': ' ) ? \FILTER_FLAG_IPV6 : \FILTER_FLAG_IPV4 ;
334
334
$ ip = \FILTER_FLAG_IPV6 === $ flag ? substr ($ host , 1 , -1 ) : $ host ;
335
+ $ now = microtime (true );
335
336
336
337
if (filter_var ($ ip , \FILTER_VALIDATE_IP , $ flag )) {
337
338
// The host is already an IP address
338
339
} elseif (null === $ ip = $ multi ->dnsCache [$ host ] ?? null ) {
339
340
$ info ['debug ' ] .= "* Hostname was NOT found in DNS cache \n" ;
340
- $ now = microtime (true );
341
341
342
- if (!$ ip = gethostbynamel ($ host )) {
342
+ if ($ ip = gethostbynamel ($ host )) {
343
+ $ ip = $ ip [0 ];
344
+ } elseif (!\defined ('STREAM_PF_INET6 ' )) {
345
+ throw new TransportException (\sprintf ('Could not resolve host "%s". ' , $ host ));
346
+ } elseif ($ ip = dns_get_record ($ host , \DNS_AAAA )) {
347
+ $ ip = $ ip [0 ]['ipv6 ' ];
348
+ } elseif (\extension_loaded ('sockets ' )) {
349
+ if (!$ addrInfo = socket_addrinfo_lookup ($ host , 0 , ['ai_socktype ' => \SOCK_STREAM , 'ai_family ' => \AF_INET6 ])) {
350
+ throw new TransportException (\sprintf ('Could not resolve host "%s". ' , $ host ));
351
+ }
352
+
353
+ $ ip = socket_addrinfo_explain ($ addrInfo [0 ])['ai_addr ' ]['sin6_addr ' ];
354
+ } elseif ('localhost ' === $ host || 'localhost. ' === $ host ) {
355
+ $ ip = '::1 ' ;
356
+ } else {
343
357
throw new TransportException (\sprintf ('Could not resolve host "%s". ' , $ host ));
344
358
}
345
359
346
- $ multi ->dnsCache [$ host ] = $ ip = $ ip [ 0 ] ;
360
+ $ multi ->dnsCache [$ host ] = $ ip ;
347
361
$ info ['debug ' ] .= "* Added {$ host }:0: {$ ip } to DNS cache \n" ;
348
- $ host = $ ip ;
349
362
} else {
350
363
$ info ['debug ' ] .= "* Hostname was found in DNS cache \n" ;
351
- $ host = str_contains ($ ip , ': ' ) ? "[ $ ip] " : $ ip ;
352
364
}
353
365
366
+ $ host = str_contains ($ ip , ': ' ) ? "[ $ ip] " : $ ip ;
354
367
$ info ['namelookup_time ' ] = microtime (true ) - ($ info ['start_time ' ] ?: $ now );
355
368
$ info ['primary_ip ' ] = $ ip ;
356
369
0 commit comments