Skip to content

Commit

Permalink
- added Net_DNS2::closeSockets(), which lets you close all cached net…
Browse files Browse the repository at this point in the history
…work sockets in the resolver object.

- added date_created and date_last_used to the Net_DNS2_Socket object, to track usage stats on each socket object.
  • Loading branch information
mikepultz committed Oct 5, 2020
1 parent f10ee35 commit 8dc4b71
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 190 deletions.
36 changes: 27 additions & 9 deletions Net/DNS2.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,23 @@ public function setServers($nameservers)
return true;
}

/**
* give users access to close all open sockets on the resolver object; resetting each
* array, calls the destructor on the Net_DNS2_Socket object, which calls the close()
* method on each object.
*
* @return boolean
* @access public
*
*/
public function closeSockets()
{
$this->sock[Net_DNS2_Socket::SOCK_DGRAM] = [];
$this->sock[Net_DNS2_Socket::SOCK_STREAM] = [];

return true;
}

/**
* parses the options line from a resolv.conf file; we don't support all the options
* yet, and using them is optional.
Expand Down Expand Up @@ -1018,12 +1035,7 @@ private function generateError($_proto, $_ns, $_error)
$last_error = $this->sock[$_proto][$_ns]->last_error;
//
// close it
//
$this->sock[$_proto][$_ns]->close();
//
// remove it from the socket cache
// remove it from the socket cache; this will call the destructor, which calls close() on the socket
//
unset($this->sock[$_proto][$_ns]);
Expand Down Expand Up @@ -1114,7 +1126,9 @@ private function sendTCPRequest($_ns, $_data, $_axfr = false)
//
// read the data off the socket
//
$result = $this->sock[Net_DNS2_Socket::SOCK_STREAM][$_ns]->read($size, ($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);
$result = $this->sock[Net_DNS2_Socket::SOCK_STREAM][$_ns]->read($size,
($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);
if ( ($result === false) || ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE) ) {
//
Expand Down Expand Up @@ -1210,7 +1224,9 @@ private function sendTCPRequest($_ns, $_data, $_axfr = false)
//
} else {

$result = $this->sock[Net_DNS2_Socket::SOCK_STREAM][$_ns]->read($size, ($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);
$result = $this->sock[Net_DNS2_Socket::SOCK_STREAM][$_ns]->read($size,
($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);

if ( ($result === false) || ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE) ) {

$this->generateError(Net_DNS2_Socket::SOCK_STREAM, $_ns, Net_DNS2_Lookups::E_NS_SOCKET_FAILED);
Expand Down Expand Up @@ -1305,7 +1321,9 @@ private function sendUDPRequest($_ns, $_data)
//
$size = 0;

$result = $this->sock[Net_DNS2_Socket::SOCK_DGRAM][$_ns]->read($size, ($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);
$result = $this->sock[Net_DNS2_Socket::SOCK_DGRAM][$_ns]->read($size,
($this->dnssec == true) ? $this->dnssec_payload_size : Net_DNS2_Lookups::DNS_MAX_UDP_SIZE);

if (( $result === false) || ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE)) {

$this->generateError(Net_DNS2_Socket::SOCK_DGRAM, $_ns, Net_DNS2_Lookups::E_NS_SOCKET_FAILED);
Expand Down
38 changes: 33 additions & 5 deletions Net/DNS2/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ class Net_DNS2_Socket
private $timeout;
private $context;

/*
* the local IP and port we'll send the request from
*/
private $local_host;
private $local_port;

/*
* the last error message on the object
*/
public $last_error;

/*
* date the socket connection was created, and the date it was last used
*/
public $date_created;
public $date_last_used;

/*
* type of sockets
*/
Expand All @@ -64,10 +76,11 @@ class Net_DNS2_Socket
*/
public function __construct($type, $host, $port, $timeout)
{
$this->type = $type;
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->type = $type;
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->date_created = microtime(true);
}

/**
Expand Down Expand Up @@ -243,6 +256,11 @@ public function write($data)
$write = [ $this->sock ];
$except = null;

//
// increment the date last used timestamp
//
$this->date_last_used = microtime(true);

//
// select on write
//
Expand Down Expand Up @@ -302,6 +320,11 @@ public function read(&$size, $max_size)
$write = null;
$except = null;

//
// increment the date last used timestamp
//
$this->date_last_used = microtime(true);

//
// make sure our socket is non-blocking
//
Expand Down Expand Up @@ -337,7 +360,12 @@ public function read(&$size, $max_size)
$this->last_error = 'failed on fread() for data length';
return false;
}

if (strlen($data) == 0)
{
$this->last_error = 'failed on fread() for data length';
return false;
}

$length = ord($data[0]) << 8 | ord($data[1]);
if ($length < Net_DNS2_Lookups::DNS_HEADER_SIZE) {

Expand Down
11 changes: 8 additions & 3 deletions package.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@
$pkg->setAPIStability('stable');
$pkg->setNotes(
"- added the AMTRELAY resource record type (RFC 8777).\n" .
"- added Net_DNS2_RR::asArray(), which returns the same values as __toString(), but as an array for easier access.\n" .
"- added Net_DNS2::closeSockets(), which lets you close all cached network sockets in the resolver object.\n" .
"- added date_created and date_last_used to the Net_DNS2_Socket object, to track usage stats on each socket object.\n" .
"- dropped the Net_DNS2_Socket_Sockets, and switch to just using the streams code. There's no speed difference anymore.\n" .
"- fixed a bug in Net_DNS2_Packet::compress() and Net_DNS2_Packet::expand() related to dot literals in compressed names.\n" .
"- fixed a display issue in the IPSECKEY RR when displaying hostname / domain names in the gateway field.\n" .
"- the Net_DNS2_RR_NIMLOC class was incorrectly named Net_DNS2_RR_NIMLOCK.\n" .
"- fixed a couple inconsistencies in the docs.\n" .
"- fixed a PHP 7.4 bug in Sockets.php; accessing a null value as an array throws an exception now.\n" .
"- the Net_DNS2_RR_NIMLOC class was incorrectly named Net_DNS2_RR_NIMLOCK.\n" .
"- Net_DNS2_PrivateKey was using the wrong member variable name for the key_format value.\n" .
"- added Net_DNS2_RR::asArray(), which returns the same values as __toString(), but as an array for easier access.\n" .
"- removed all sorts of license noise from the files.\n"
"- changed all references to array() to [].\n" .
"- removed all sorts of license noise from the files.\n" .
"- updated the test cases to use PHPUnit v9+.\n"
);
$pkg->setPackageType('php');
$pkg->addRelease();
Expand Down
47 changes: 4 additions & 43 deletions tests/Tests_Net_DNS2_AllTests.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
<?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.
* Copyright (c) 2020, Mike Pultz <mike@mikepultz.com>. All rights reserved.
*
* * 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.
* See LICENSE for more details.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @copyright 2020 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
* @link https://netdns2.com/
* @since File available since Release 1.0.0
*
*/
Expand All @@ -67,12 +34,6 @@
* 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 Tests_Net_DNS2_AllTests
{
Expand Down
49 changes: 5 additions & 44 deletions tests/Tests_Net_DNS2_DNSSECTest.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
<?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.
* Copyright (c) 2020, Mike Pultz <mike@mikepultz.com>. All rights reserved.
*
* * 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.
* See LICENSE for more details.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @copyright 2020 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
* @link https://netdns2.com/
* @since File available since Release 1.0.0
*
*/
Expand All @@ -55,12 +22,6 @@
/**
* 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 Tests_Net_DNS2_DNSSECTest extends PHPUnit\Framework\TestCase
{
Expand All @@ -85,4 +46,4 @@ public function testDNSSEC()
$this->assertTrue(($result->additional[0] instanceof Net_DNS2_RR_OPT));
$this->assertTrue(($result->additional[0]->do == 1));
}
};
}
47 changes: 4 additions & 43 deletions tests/Tests_Net_DNS2_ParserTest.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
<?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.
* Copyright (c) 2020, Mike Pultz <mike@mikepultz.com>. All rights reserved.
*
* * 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.
* See LICENSE for more details.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @copyright 2020 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
* @link https://netdns2.com/
* @since File available since Release 1.0.0
*
*/
Expand All @@ -55,12 +22,6 @@
/**
* Test class to test the parsing code
*
* @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 Tests_Net_DNS2_ParserTest extends PHPUnit\Framework\TestCase
{
Expand Down
Loading

0 comments on commit 8dc4b71

Please sign in to comment.