From 610659198b367db82ae7eff5328b3abefc11af71 Mon Sep 17 00:00:00 2001 From: David Cutting Date: Wed, 24 Sep 2025 18:49:14 +0100 Subject: [PATCH 1/4] suppress warnings from fsockopen and include error message --- src/PurplePixie/PhpDns/DNSQuery.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PurplePixie/PhpDns/DNSQuery.php b/src/PurplePixie/PhpDns/DNSQuery.php index c790d73..178f3c2 100644 --- a/src/PurplePixie/PhpDns/DNSQuery.php +++ b/src/PurplePixie/PhpDns/DNSQuery.php @@ -374,8 +374,14 @@ public function query(string $question, string $typeName = DNSTypes::NAME_A) $errno = 0; $errstr = ''; - if (!$socket = fsockopen($host, $this->port, $errno, $errstr, $this->timeout)) { - $this->setError('Failed to Open Socket'); + if (!$socket = @fsockopen( + $host, + $this->port, + $errno, + $errstr, + $this->timeout)) + { + $this->setError('Failed to Open Socket (Code '.$errno.', Message: '.$errstr.')'); return false; } From 81735fffb813ac3d3ecc9cb32601ac2dc8e88d4d Mon Sep 17 00:00:00 2001 From: David Cutting Date: Wed, 24 Sep 2025 18:52:18 +0100 Subject: [PATCH 2/4] ConnectionFailure exception created --- .../PhpDns/Exceptions/ConnectionFailure.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php diff --git a/src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php b/src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php new file mode 100644 index 0000000..e1ef40f --- /dev/null +++ b/src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php @@ -0,0 +1,29 @@ + Date: Thu, 25 Sep 2025 13:42:59 +0100 Subject: [PATCH 3/4] fsockopen error throws connection error exception when requested --- dns.inc.php | 6 +++++- src/PurplePixie/PhpDns/DNSQuery.php | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/dns.inc.php b/dns.inc.php index a99adf3..ee98882 100644 --- a/dns.inc.php +++ b/dns.inc.php @@ -2,7 +2,7 @@ /* ------------------------------------------------------------- This file is the PurplePixie PHP DNS Query Classes -The software is (C) Copyright 2008-2016 PurplePixie Systems +The software is (C) Copyright 2008-2025 PurplePixie Systems This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,3 +27,7 @@ require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSQuery.php'; require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSResult.php'; require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSTypes.php'; + +require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/InvalidQueryTypeId.php'; +require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/InvalidQueryTypeName.php'; +require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php'; diff --git a/src/PurplePixie/PhpDns/DNSQuery.php b/src/PurplePixie/PhpDns/DNSQuery.php index 178f3c2..9f9297d 100644 --- a/src/PurplePixie/PhpDns/DNSQuery.php +++ b/src/PurplePixie/PhpDns/DNSQuery.php @@ -57,6 +57,8 @@ class DNSQuery private string $lasterror = ''; + private bool $connectionException = false; + public function __construct(string $server, int $port = 53, int $timeout = 60, bool $udp = true, bool $debug = false, bool $binarydebug = false) { $this->server = $server; @@ -357,7 +359,7 @@ private function readRecord(): array /** * @return DNSAnswer|false - * @throws Exceptions\InvalidQueryTypeName + * @throws Exceptions\InvalidQueryTypeName|Exceptions\ConnectionFailure */ public function query(string $question, string $typeName = DNSTypes::NAME_A) { @@ -382,6 +384,8 @@ public function query(string $question, string $typeName = DNSTypes::NAME_A) $this->timeout)) { $this->setError('Failed to Open Socket (Code '.$errno.', Message: '.$errstr.')'); + if ($this->getConnectionException()) + throw new Exceptions\ConnectionFailure('Failed to Open Socket (Code '.$errno.', Message: '.$errstr.')");'); return false; } @@ -748,4 +752,14 @@ public function getLasterror(): string { return $this->lasterror; } + + public function getConnectionException(): bool + { + return $this->connectionException; + } + + public function setConnectionException(bool $value): void + { + $this->connectionException = $value; + } } From a14eb4b474d7258d8e618f064d2b2eb4d4688fdc Mon Sep 17 00:00:00 2001 From: David Cutting Date: Thu, 25 Sep 2025 13:46:08 +0100 Subject: [PATCH 4/4] unit test to check ConnectionException --- tests/DNSQueryTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/DNSQueryTest.php b/tests/DNSQueryTest.php index d0841b8..3d7fba4 100644 --- a/tests/DNSQueryTest.php +++ b/tests/DNSQueryTest.php @@ -23,6 +23,8 @@ use PurplePixie\PhpDns\DNSQuery; +use PurplePixie\PhpDns\DNSTypes; + class DNSQueryTest extends TestCase { /** @@ -36,6 +38,14 @@ public function testInvalidDNSTypeName(): void $query->query('google.com', 'invalid'); } + public function testConnectionExceptionTCP(): void + { + $query = new DNSQuery("127.0.0.1", 53, 60, false); + $query->setConnectionException(true); + $this->expectException(\PurplePixie\PhpDns\Exceptions\ConnectionFailure::class); + $query->query('google.com', DNSTypes::NAME_A); + } + public function testALookup() : void { $query = new DNSQuery("8.8.8.8");