Catch DNS Query that fail unpack()

This commit is contained in:
Deon George 2023-09-20 22:26:35 +10:00
parent 612efda945
commit b7c1c97cf7
2 changed files with 14 additions and 4 deletions

View File

@ -32,7 +32,8 @@ final class Query
'arcount' => [0x05,'n',1], // Resource Records in the addition records section
];
public function __construct(string $buf) {
public function __construct(string $buf)
{
$this->buf = $buf;
$rx_ptr = 0;
@ -54,7 +55,15 @@ final class Query
}
// Get the query type/class
$result = unpack('ntype/nclass',substr($this->buf,$rx_ptr,4));
try {
$result = unpack('ntype/nclass',substr($this->buf,$rx_ptr,4));
} catch (\Exception $e) {
Log::error(sprintf('%s:! Unpack failed: Buffer: [%s] (%d), RXPTR [%d]',self::LOGKEY,hex_dump($this->buf),strlen($this->buf),$rx_ptr));
return;
}
$rx_ptr += 4;
$this->type = $result['type'];
$this->class = $result['class'];
@ -96,7 +105,8 @@ final class Query
}
}
public static function header_len() {
public static function header_len()
{
return collect(self::header)->sum(function($item) { return $item[2]*2; });
}

View File

@ -320,7 +320,7 @@ final class SocketClient {
}
} catch (\Exception $e) {
Log::error(sprintf('%s: - socket_recv Exception [%s]',self::LOGKEY,$e->getMessage()));
Log::error(sprintf('%s:! socket_recv Exception [%s]',self::LOGKEY,$e->getMessage()));
throw new SocketException($x=socket_last_error($this->connection),socket_strerror($x));
}