From 1feab2e7622d4e537b609b2446c8bab5167decb2 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 23 Jan 2018 15:38:54 +0000 Subject: [PATCH] tcpclient: Get the A and AAAA records before querying the CNAME only if needed (speedup if the CNAME DNS can not be found and hang the request) git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4040 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- tcpclient.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tcpclient.php b/tcpclient.php index 69ba63c..f1a66e0 100644 --- a/tcpclient.php +++ b/tcpclient.php @@ -56,10 +56,16 @@ class tcpclient $i = 0; while (empty ($this->ipv4) && empty ($this->ipv6) && $i < 10) { - $nsRecords = @dns_get_record ($ipOrName, DNS_AAAA + DNS_A + DNS_CNAME); + $nsRecords = @dns_get_record ($ipOrName, DNS_A + DNS_AAAA); if ($nsRecords === false) - throw new \Exception ("Can not find the IP for $ipOrName : ". - "DNS Error", 500); + { + // There is some problems with CNAME if they are not defined. + // So enter in this case only if there is no other solution + $nsRecords = @dns_get_record ($ipOrName, DNS_CNAME); + if ($nsRecords === false) + throw new \Exception ("Can not find the IP for $ipOrName : ". + "DNS Error (No A, AAAA, CNAME entries)", 500); + } foreach ($nsRecords as $val) { if ($val["type"] === "CNAME") @@ -153,11 +159,12 @@ class tcpclient if ($this->socket === null) throw new \Exception ("Can not send to server $this->ipOrName : ". "The server is not connected", 500); - // Setting the options allow the IP to be decided by the connect and valid - // the certificate of the server by the name $options = array ("ssl" => array ( "peer_name" => $this->ipOrName, + "verify_peer" => true, "verify_peer_name" => true, + "capture_peer_cert" => true, + "capture_peer_cert_chain" => true, "SNI_enabled" => true, )); stream_set_blocking ($this->socket, true); @@ -204,13 +211,13 @@ class tcpclient { $read = stream_get_line ($this->socket, $maxLength, "\r\n"); if ($read === false) - throw new \Exception ("Can not read from server", 500); + throw new \Exception ("Can not read from server in text", 500); } else { - $read = @fread ($this->socket, $maxLength); + $read = fread ($this->socket, $maxLength); if ($read === false) - throw new \Exception ("Can not read from server" , 500); + throw new \Exception ("Can not read from server in binary" , 500); } return $read; }