@@ -59,6 +59,8 @@ class DNSQuery
5959
6060 private bool $ connectionException = false ;
6161
62+ private bool $ responseException = false ;
63+
6264 public function __construct (string $ server , int $ port = 53 , int $ timeout = 60 , bool $ udp = true , bool $ debug = false , bool $ binarydebug = false )
6365 {
6466 $ this ->server = $ server ;
@@ -188,7 +190,7 @@ private function clearError(): void
188190
189191 /**
190192 * @return array
191- * @throws Exceptions\InvalidQueryTypeId
193+ * @throws Exceptions\InvalidQueryTypeId|Exceptions\InvalidResponse
192194 */
193195 private function readRecord (): array
194196 {
@@ -198,13 +200,32 @@ private function readRecord(): array
198200
199201 $ ans_header_bin = $ this ->readResponse (10 ); // 10 byte header
200202
201- $ ans_header = unpack ('ntype/nclass/Nttl/nlength ' , $ ans_header_bin );
203+ $ ans_header = @ unpack ('ntype/nclass/Nttl/nlength ' , $ ans_header_bin );
202204
203- if (is_array ($ ans_header )) $ this ->debug (
204- 'Record Type ' . $ ans_header ['type ' ] . ' Class ' . $ ans_header ['class ' ] .
205- ' TTL ' . $ ans_header ['ttl ' ] . ' Length ' . $ ans_header ['length ' ]
206- );
207- else $ this ->debug ("Error unpacking answer header, no array returned. " );
205+ if ($ ans_header === null || $ ans_header === false || !is_array ($ ans_header )) // the unpack has failed - we assume an invalid return
206+ {
207+ $ this ->debug ("Error unpacking answer header, no array returned. " );
208+
209+ if ($ this ->responseException )
210+ throw new Exceptions \InvalidResponse ("Answer header invalid format or empty " );
211+
212+ return [
213+ 'header ' => [],
214+ 'typeid ' => null ,
215+ 'typename ' => "" ,
216+ 'data ' => "" ,
217+ 'domain ' => "" ,
218+ 'string ' => "Error unpacking answer header " ,
219+ 'extras ' => "" ,
220+ ];
221+ }
222+ else
223+ {
224+ $ this ->debug (
225+ 'Record Type ' . $ ans_header ['type ' ] . ' Class ' . $ ans_header ['class ' ] .
226+ ' TTL ' . $ ans_header ['ttl ' ] . ' Length ' . $ ans_header ['length ' ]
227+ );
228+ }
208229
209230 $ typeId = $ ans_header ['type ' ];
210231
@@ -767,4 +788,14 @@ public function setConnectionException(bool $value): void
767788 {
768789 $ this ->connectionException = $ value ;
769790 }
791+
792+ public function setResponseException (bool $ value ): void
793+ {
794+ $ this ->responseException = $ value ;
795+ }
796+
797+ public function getResponseException (): bool
798+ {
799+ return $ this ->responseException ;
800+ }
770801}
0 commit comments