Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dns.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
26 changes: 23 additions & 3 deletions src/PurplePixie/PhpDns/DNSQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -374,8 +376,16 @@ 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.')');
if ($this->getConnectionException())
throw new Exceptions\ConnectionFailure('Failed to Open Socket (Code '.$errno.', Message: '.$errstr.')");');
return false;
}

Expand Down Expand Up @@ -742,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;
}
}
29 changes: 29 additions & 0 deletions src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright (C) 2025, David Cutting
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see www.gnu.org/licenses
*
* For more information see www.purplepixie.org/phpdns
*/

namespace PurplePixie\PhpDns\Exceptions;

class ConnectionFailure extends \Exception {

public function __construct(string $message) {
parent::__construct('Socket Connection Failed: ' . $message);
}
}
10 changes: 10 additions & 0 deletions tests/DNSQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

use PurplePixie\PhpDns\DNSQuery;

use PurplePixie\PhpDns\DNSTypes;

class DNSQueryTest extends TestCase
{
/**
Expand All @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the test URL to 0.0.0.0 (because someone could have a local DNS server) :)

$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");
Expand Down