@@ -332,25 +332,38 @@ private static function dnsResolve(string $host, NativeClientState $multi, array
332332 {
333333 $ flag = '' !== $ host && '[ ' === $ host [0 ] && '] ' === $ host [-1 ] && str_contains ($ host , ': ' ) ? \FILTER_FLAG_IPV6 : \FILTER_FLAG_IPV4 ;
334334 $ ip = \FILTER_FLAG_IPV6 === $ flag ? substr ($ host , 1 , -1 ) : $ host ;
335+ $ now = microtime (true );
335336
336337 if (filter_var ($ ip , \FILTER_VALIDATE_IP , $ flag )) {
337338 // The host is already an IP address
338339 } elseif (null === $ ip = $ multi ->dnsCache [$ host ] ?? null ) {
339340 $ info ['debug ' ] .= "* Hostname was NOT found in DNS cache \n" ;
340- $ now = microtime (true );
341341
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 {
343357 throw new TransportException (\sprintf ('Could not resolve host "%s". ' , $ host ));
344358 }
345359
346- $ multi ->dnsCache [$ host ] = $ ip = $ ip [ 0 ] ;
360+ $ multi ->dnsCache [$ host ] = $ ip ;
347361 $ info ['debug ' ] .= "* Added {$ host }:0: {$ ip } to DNS cache \n" ;
348- $ host = $ ip ;
349362 } else {
350363 $ info ['debug ' ] .= "* Hostname was found in DNS cache \n" ;
351- $ host = str_contains ($ ip , ': ' ) ? "[ $ ip] " : $ ip ;
352364 }
353365
366+ $ host = str_contains ($ ip , ': ' ) ? "[ $ ip] " : $ ip ;
354367 $ info ['namelookup_time ' ] = microtime (true ) - ($ info ['start_time ' ] ?: $ now );
355368 $ info ['primary_ip ' ] = $ ip ;
356369
0 commit comments