Skip to content

Commit

Permalink
- fixed a bug in the Net_DNS2_Exception class; the 'previous' argumen…
Browse files Browse the repository at this point in the history
…t was only added in PHP 5.3.0

- fixed Net_DNS2_Packet_Request::set so we can pass '.' in as name value for querying the root name severs
- fixed Net_DNS2::setServers() so it overrides any existing values, rather than just adding to them. Also made it remove any duplicate nameserver entries.
- added the query response_time to the Net_DNS2_Packet_Response object.
  • Loading branch information
mike.pultz committed Apr 13, 2014
1 parent 18efb93 commit b37e876
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 40 deletions.
56 changes: 42 additions & 14 deletions Net/DNS2.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ public function setServers($nameservers)

} else {

//
// temporary list of name servers; do it this way rather than just
// resetting the local nameservers value, just incase an exception
// is thrown here; this way we might avoid ending up with an empty
// namservers list.
//
$ns = array();

//
// check to see if the file is readable
//
Expand Down Expand Up @@ -404,7 +412,7 @@ public function setServers($nameservers)
|| (self::isIPv6($value) == true)
) {

$this->nameservers[] = $value;
$ns[] = $value;
} else {

throw new Net_DNS2_Exception(
Expand Down Expand Up @@ -443,8 +451,21 @@ public function setServers($nameservers)
Net_DNS2_Lookups::E_NS_INVALID_FILE
);
}

//
// store the name servers locally
//
if (count($ns) > 0) {
$this->nameservers = $ns;
}
}

//
// remove any duplicates; not sure if we should bother with this- if people
// put duplicate name servers, who I am to stop them?
//
$this->nameservers = array_unique($this->nameservers);

//
// check the name servers
//
Expand Down Expand Up @@ -685,9 +706,7 @@ public static function isIPv4($_address)
//
if (extension_loaded('filter') == true) {

if (filter_var(
$_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4
) == false) {
if (filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == false) {
return false;
}
} else {
Expand All @@ -702,10 +721,7 @@ public static function isIPv4($_address)
//
// then make sure we're not a IPv6 address
//
if (preg_match(
'/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',
$_address
) == 0) {
if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 0) {
return false;
}
}
Expand All @@ -728,9 +744,7 @@ public static function isIPv6($_address)
// use filter_var() if it's available; it's faster than preg
//
if (extension_loaded('filter') == true) {
if (filter_var(
$_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6
) == false) {
if (filter_var($_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == false) {
return false;
}
} else {
Expand All @@ -745,9 +759,7 @@ public static function isIPv6($_address)
//
// then make sure it doesn't match a IPv4 address
//
if (preg_match(
'/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address
) == 1) {
if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_address) == 1) {
return false;
}
}
Expand Down Expand Up @@ -850,6 +862,7 @@ protected function sendPacket(Net_DNS2_Packet $request, $use_tcp)
$ns = '';
$socket_type = null;
$tcp_fallback = false;
$start_time = 0;

while (1) {

Expand Down Expand Up @@ -917,6 +930,11 @@ protected function sendPacket(Net_DNS2_Packet $request, $use_tcp)
);
}

//
// grab the start time
//
$start_time = microtime(true);

//
// open it; if it fails, continue in the while loop
//
Expand Down Expand Up @@ -1089,6 +1107,11 @@ protected function sendPacket(Net_DNS2_Packet $request, $use_tcp)
);
}

//
// grab the start time
//
$start_time = microtime(true);

//
// open it
//
Expand Down Expand Up @@ -1162,6 +1185,11 @@ protected function sendPacket(Net_DNS2_Packet $request, $use_tcp)
);
}

//
// store the query time
//
$response->response_time = microtime(true) - $start_time;

//
// add the name server that the response came from to the response object,
// and the socket type that was used.
Expand Down
12 changes: 9 additions & 3 deletions Net/DNS2/Packet/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ public function set($name, $type = 'A', $class = 'IN')
//
$q = new Net_DNS2_Question();

$name = trim(strtolower($name), " \t\n\r\0\x0B.");
$type = strtoupper(trim($type));
$class = strtoupper(trim($class));
//
// allow queries directly to . for the root name servers
//
if ($name != '.') {
$name = trim(strtolower($name), " \t\n\r\0\x0B.");
}

$type = strtoupper(trim($type));
$class = strtoupper(trim($class));

//
// check that the input string has some data in it
Expand Down
5 changes: 5 additions & 0 deletions Net/DNS2/Packet/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Net_DNS2_Packet_Response extends Net_DNS2_Packet
*/
public $answer_socket_type;

/*
* The query response time in microseconds
*/
public $response_time = 0;

/**
* Constructor - builds a new Net_DNS2_Packet_Response object
*
Expand Down
4 changes: 1 addition & 3 deletions Net/DNS2/RR/SIG.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)
//
// sign the data
//
if (openssl_sign(
$sigdata, $this->signature, $this->private_key->instance, $algorithm
) == false) {
if (openssl_sign($sigdata, $this->signature, $this->private_key->instance, $algorithm) == false) {

throw new Net_DNS2_Exception(
openssl_error_string(),
Expand Down
5 changes: 4 additions & 1 deletion package.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
$pkg->setReleaseStability('stable');
$pkg->setAPIStability('stable');
$pkg->setNotes(
"- fixed a bug in the Net_DNS2_Exception class; the 'previous' argument was only added in PHP 5.3.0\n";
"- fixed a bug in the Net_DNS2_Exception class; the 'previous' argument was only added in PHP 5.3.0\n" .
"- fixed Net_DNS2_Packet_Request::set so we can pass '.' in as name value for querying the root name severs\n" .
"- fixed Net_DNS2::setServers() so it overrides any existing values, rather than just adding to them. Also made it remove any duplicate nameserver entries.\n"
"- added the query response_time to the Net_DNS2_Packet_Response object.\n"
);
$pkg->setPackageType('php');
$pkg->addRelease();
Expand Down
87 changes: 79 additions & 8 deletions tests/AllTests.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,99 @@
<?php

//
// This test suite assumes that Net_DNS2 will be in the include path, otherwise it
// will fail. There's no other way to hardcode a include_path in here that would make
// it work everywhere.
//
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.0.0
*
*/

error_reporting(E_ALL | E_STRICT);

if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Net_DNS2_AllTests::main');
if (!defined('PHPUNIT_MAIN_METHOD')) {
define('PHPUNIT_MAIN_METHOD', 'Net_DNS2_AllTests::main');
}

require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'Net_DNS2_ParserTest.php';
require_once 'Net_DNS2_ResolverTest.php';
require_once 'Net_DNS2_DNSSECTest.php';

/**
* This test suite assumes that Net_DNS2 will be in the include path, otherwise it
* will fail. There's no other way to hardcode a include_path in here that would
* make it work everywhere.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
class Net_DNS2_AllTests
{
/**
* the main runner
*
* @return void
* @access public
*
*/
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

/**
* test suite
*
* @return void
* @access public
*
*/
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('PEAR - Net_DNS2');
Expand All @@ -35,7 +106,7 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'Net_DNS2_AllTests::main') {
if (PHPUNIT_MAIN_METHOD == 'Net_DNS2_AllTests::main') {
Net_DNS2_AllTests::main();
}

Expand Down
73 changes: 71 additions & 2 deletions tests/Net_DNS2_DNSSECTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.0.0
*
*/

require_once 'Net/DNS2.php';

/**
* Test class to test the DNSSEC logic
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
*
*/
class Net_DNS2_DNSSECTest extends PHPUnit_Framework_TestCase
{
public function testTSIG()
/**
* function to test the TSIG logic
*
* @return void
* @access public
*
*/
public function testDNSSEC()
{
$r = new Net_DNS2_Resolver(array('nameservers' => array('8.8.8.8', '8.8.4.4')));
$ns = array('8.8.8.8', '8.8.4.4');

$r = new Net_DNS2_Resolver(array('nameservers' => $ns));

$r->dnssec = true;

Expand Down
Loading

0 comments on commit b37e876

Please sign in to comment.