Skip to content

Commit 665a192

Browse files
authored
Merge pull request #25 from purplepixie/connection-exception
Connection exception
2 parents df2dd75 + a14eb4b commit 665a192

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

dns.inc.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* -------------------------------------------------------------
33
This file is the PurplePixie PHP DNS Query Classes
44
5-
The software is (C) Copyright 2008-2016 PurplePixie Systems
5+
The software is (C) Copyright 2008-2025 PurplePixie Systems
66
77
This is free software: you can redistribute it and/or modify
88
it under the terms of the GNU General Public License as published by
@@ -27,3 +27,7 @@
2727
require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSQuery.php';
2828
require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSResult.php';
2929
require_once __DIR__ . '/src/PurplePixie/PhpDns/DNSTypes.php';
30+
31+
require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/InvalidQueryTypeId.php';
32+
require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/InvalidQueryTypeName.php';
33+
require_once __DIR__ . '/src/PurplePixie/PhpDns/Exceptions/ConnectionFailure.php';

src/PurplePixie/PhpDns/DNSQuery.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2025, David Cutting
5+
*
6+
* This is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The software is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this software. If not, see www.gnu.org/licenses
18+
*
19+
* For more information see www.purplepixie.org/phpdns
20+
*/
21+
22+
namespace PurplePixie\PhpDns\Exceptions;
23+
24+
class ConnectionFailure extends \Exception {
25+
26+
public function __construct(string $message) {
27+
parent::__construct('Socket Connection Failed: ' . $message);
28+
}
29+
}

tests/DNSQueryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
use PurplePixie\PhpDns\DNSQuery;
2525

26+
use PurplePixie\PhpDns\DNSTypes;
27+
2628
class DNSQueryTest extends TestCase
2729
{
2830
/**
@@ -36,6 +38,14 @@ public function testInvalidDNSTypeName(): void
3638
$query->query('google.com', 'invalid');
3739
}
3840

41+
public function testConnectionExceptionTCP(): void
42+
{
43+
$query = new DNSQuery("127.0.0.1", 53, 60, false);
44+
$query->setConnectionException(true);
45+
$this->expectException(\PurplePixie\PhpDns\Exceptions\ConnectionFailure::class);
46+
$query->query('google.com', DNSTypes::NAME_A);
47+
}
48+
3949
public function testALookup() : void {
4050

4151
$query = new DNSQuery("8.8.8.8");

0 commit comments

Comments
 (0)