@@ -57,6 +57,8 @@ class DNSQuery
5757
5858 private string $ lasterror = '' ;
5959
60+ private bool $ connectionException = false ;
61+
6062 public function __construct (string $ server , int $ port = 53 , int $ timeout = 60 , bool $ udp = true , bool $ debug = false , bool $ binarydebug = false )
6163 {
6264 $ this ->server = $ server ;
@@ -357,7 +359,7 @@ private function readRecord(): array
357359
358360 /**
359361 * @return DNSAnswer|false
360- * @throws Exceptions\InvalidQueryTypeName
362+ * @throws Exceptions\InvalidQueryTypeName|Exceptions\ConnectionFailure
361363 */
362364 public function query (string $ question , string $ typeName = DNSTypes::NAME_A )
363365 {
@@ -374,8 +376,16 @@ public function query(string $question, string $typeName = DNSTypes::NAME_A)
374376 $ errno = 0 ;
375377 $ errstr = '' ;
376378
377- if (!$ socket = fsockopen ($ host , $ this ->port , $ errno , $ errstr , $ this ->timeout )) {
378- $ this ->setError ('Failed to Open Socket ' );
379+ if (!$ socket = @fsockopen (
380+ $ host ,
381+ $ this ->port ,
382+ $ errno ,
383+ $ errstr ,
384+ $ this ->timeout ))
385+ {
386+ $ this ->setError ('Failed to Open Socket (Code ' .$ errno .', Message: ' .$ errstr .') ' );
387+ if ($ this ->getConnectionException ())
388+ throw new Exceptions \ConnectionFailure ('Failed to Open Socket (Code ' .$ errno .', Message: ' .$ errstr .')"); ' );
379389 return false ;
380390 }
381391
@@ -742,4 +752,14 @@ public function getLasterror(): string
742752 {
743753 return $ this ->lasterror ;
744754 }
755+
756+ public function getConnectionException (): bool
757+ {
758+ return $ this ->connectionException ;
759+ }
760+
761+ public function setConnectionException (bool $ value ): void
762+ {
763+ $ this ->connectionException = $ value ;
764+ }
745765}
0 commit comments