Skip to content

Commit

Permalink
- added the ZONEMD resource record type.
Browse files Browse the repository at this point in the history
- added a new PHPUnit file to test the file cache.
- added a new PHPUnit file to do real-time checks against a public internal server.
- muted errors from stream_select(); it's safe to mute them since the error condition is handled.
- double checked the class index exists first in Net_DNS2_RR::parse() before using it.
- added a typehint for Net_DNS::$last_exception in the comments so PHPStorm doesn't complain.
- added a quick check to confirm all the DNS servers are IP addresses.
- cleaned up the logic around binding to a local host and/or port; some cases wouldn't have worked correctly.
- PHP 8.0 issue: shmop_close() is deprecated in PHP 8.
- PHP 8.0 issue: strlen() generates an error if you pass in null; there's a few placing the code that wasn't testing for this first.
- PHP 8.0 issue: fread() throws an exception now if you set the length to 0; so I have to check the filesize() first before reading.
- PHP 8.1 issue: passing null to preg_split() generates an error; there were three cases where it was possible.
  • Loading branch information
mikepultz committed Nov 28, 2022
1 parent 1284c4b commit dc80537
Show file tree
Hide file tree
Showing 25 changed files with 653 additions and 236 deletions.
22 changes: 18 additions & 4 deletions Net/DNS2.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class Net_DNS2

/*
* the last exeception that was generated
*
* @var Net_DNS2_Exception|null
*/
public $last_exception = null;

Expand All @@ -193,7 +195,7 @@ class Net_DNS2
public $last_exception_list = [];

/*
* name server list
* name server list specified as IPv4 or IPv6 addresses
*/
public $nameservers = [];

Expand Down Expand Up @@ -323,6 +325,17 @@ public function setServers($nameservers)
//
if (is_array($nameservers)) {

//
// make sure all the name servers are IP addresses (either v4 or v6)
//
foreach($nameservers as $value) {

if ( (self::isIPv4($value) == false) && (self::isIPv6($value) == false) ) {

throw new Net_DNS2_Exception('invalid nameserver entry: ' . $value, Net_DNS2_Lookups::E_NS_INVALID_ENTRY);
}
}

$this->nameservers = $nameservers;

} else {
Expand Down Expand Up @@ -543,9 +556,10 @@ protected function checkServers($default = null)
{
if (empty($this->nameservers)) {

if (isset($default)) {
if (is_null($default) == false) {

$this->setServers($default);

} else {

throw new Net_DNS2_Exception(
Expand Down Expand Up @@ -1093,7 +1107,7 @@ private function sendTCPRequest($_ns, $_data, $_axfr = false)
//
// if a local IP address / port is set, then add it
//
if (strlen($this->local_host) > 0) {
if ( (strlen($this->local_host) > 0) || ($this->local_port > 0) ) {

$this->sock[Net_DNS2_Socket::SOCK_STREAM][$_ns]->bindAddress(
$this->local_host, $this->local_port
Expand Down Expand Up @@ -1303,7 +1317,7 @@ private function sendUDPRequest($_ns, $_data)
//
// if a local IP address / port is set, then add it
//
if (strlen($this->local_host) > 0) {
if ( (strlen($this->local_host) > 0) || ($this->local_port > 0) ) {

$this->sock[Net_DNS2_Socket::SOCK_DGRAM][$_ns]->bindAddress(
$this->local_host, $this->local_port
Expand Down
1 change: 1 addition & 0 deletions Net/DNS2/Lookups.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ class Net_DNS2_Lookups
60 => 'Net_DNS2_RR_CDNSKEY',
61 => 'Net_DNS2_RR_OPENPGPKEY',
62 => 'Net_DNS2_RR_CSYNC',
63 => 'Net_DNS2_RR_ZONEMD',
99 => 'Net_DNS2_RR_SPF',
104 => 'Net_DNS2_RR_NID',
105 => 'Net_DNS2_RR_L32',
Expand Down
74 changes: 74 additions & 0 deletions Net/DNS2/Names.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* DNS Library for handling lookups and updates.
*
* Copyright (c) 2022, Mike Pultz <[email protected]>. All rights reserved.
*
* See LICENSE for more details.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <[email protected]>
* @copyright 2022 Mike Pultz <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://netdns2.com/
* @since File available since Release 1.5.3
*
*/

/**
* text compression/expansion and labeling class
*
*/
class Net_DNS2_Names
{
/**
* pack a text string
*
* @param string $name a name to be packed
*
* @return string
* @access public
*
*/
public static function pack($name)
{
return (is_null($name) == true) ? null : pack('Ca*', strlen($name), $name);
}

/**
* parses a domain string into a single string
*
* @param Net_DNS2_Packet &$packet the DNS packet to look in for the domain name
* @param integer &$offset the offset into the given packet object
*
* @return mixed either a name string or null if it's not found.
* @access public
*
*/
public static function unpack($rdata, &$offset)
{
$name = '';

if (strlen($rdata) < ($offset + 1))
{
return null;
}

$xlen = ord($rdata[$offset]);
++$offset;

if (($xlen + $offset) > strlen($rdata)) {

$name = substr($rdata, $offset);
$offset = strlen($rdata);
} else {

$name = substr($rdata, $offset, $xlen);
$offset += $xlen;
}

return trim($name, '.');
}
}
68 changes: 26 additions & 42 deletions Net/DNS2/Packet.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,39 +214,6 @@ public function compress($name, &$offset)
return $compname;
}

/**
* applies a standard DNS name compression on the given name/offset
*
* This logic was based on the Net::DNS::Packet::dn_comp() function
* by Michanel Fuhr
*
* @param string $name the name to be compressed
*
* @return string
* @access public
*
*/
public static function pack($name)
{
$offset = 0;
$names = explode('.', $name);
$compname = '';

while (!empty($names)) {

$first = array_shift($names);
$length = strlen($first);

$compname .= pack('Ca*', $length, $first);
$offset += $length + 1;
}

$compname .= "\0";
$offset++;

return $compname;
}

/**
* expands the domain name stored at a given offset in a DNS Packet
*
Expand Down Expand Up @@ -322,6 +289,23 @@ public static function expand(Net_DNS2_Packet &$packet, &$offset, $escape_dot_li
return trim($name, '.');
}

/**
* applies a standard DNS name compression on the given name/offset
*
* This logic was based on the Net::DNS::Packet::dn_comp() function
* by Michanel Fuhr
*
* @param string $name the name to be compressed
*
* @return string
* @access public
*
*/
public static function pack($name)
{
return (is_null($name) == true) ? null : pack('Ca*', strlen($name), $name);
}

/**
* parses a domain label from a DNS Packet at the given offset
*
Expand All @@ -332,29 +316,29 @@ public static function expand(Net_DNS2_Packet &$packet, &$offset, $escape_dot_li
* @access public
*
*/
public static function label(Net_DNS2_Packet &$packet, &$offset)
public static function label($rdata, &$offset)
{
$name = '';

if ($packet->rdlength < ($offset + 1)) {

if (strlen($rdata) < ($offset + 1))
{
return null;
}

$xlen = ord($packet->rdata[$offset]);
$xlen = ord($rdata[$offset]);
++$offset;

if (($xlen + $offset) > $packet->rdlength) {
if (($xlen + $offset) > strlen($rdata)) {

$name = substr($packet->rdata, $offset);
$offset = $packet->rdlength;
$name = substr($rdata, $offset);
$offset = strlen($rdata);
} else {

$name = substr($packet->rdata, $offset, $xlen);
$name = substr($rdata, $offset, $xlen);
$offset += $xlen;
}

return $name;
return trim($name, '.');
}

/**
Expand Down
40 changes: 22 additions & 18 deletions Net/DNS2/RR.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class Net_DNS2_RR
/*
* The name of the resource record
*/
public $name;
public $name = '';

/*
* The resource record type
Expand Down Expand Up @@ -374,7 +374,13 @@ public function get(Net_DNS2_Packet &$packet)
//
// add the RR
//
$data .= pack('n', strlen($rdata)) . $rdata;
if ( (is_null($rdata) == false) && (strlen($rdata) > 0) ) {

$data .= pack('n', strlen($rdata)) . $rdata;
} else
{
$data .= pack('n', 0);
}

return $data;
}
Expand Down Expand Up @@ -432,31 +438,28 @@ public static function parse(Net_DNS2_Packet &$packet)
ord($packet->rdata[$packet->offset++]);

if ($packet->rdlength < ($packet->offset + $object['rdlength'])) {
return null;

throw new Net_DNS2_Exception(
'failed to parse resource record: packet too small.',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}

//
// lookup the class to use
//
$o = null;
$class = Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']];

if (isset($class)) {
if ( (isset(Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']]) == true) &&
(class_exists(Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']]) == true) ) {

$o = new $class($packet, $object);
$o = new Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']]($packet, $object);
if ($o) {

$packet->offset += $object['rdlength'];
$packet->offset += $object['rdlength'];
}
} else {

throw new Net_DNS2_Exception(
'un-implemented resource record type: ' . $object['type'],
Net_DNS2_Lookups::E_RR_INVALID
);
return $o;
}

return $o;
throw new Net_DNS2_Exception('un-implemented resource record type: ' . $object['type'], Net_DNS2_Lookups::E_RR_INVALID);
}

/**
Expand All @@ -471,7 +474,7 @@ public static function parse(Net_DNS2_Packet &$packet)
*/
public function cleanString($data)
{
return strtolower(rtrim($data, '.'));
return (is_null($data) == true) ? null : strtolower(rtrim($data, '.'));
}

/**
Expand All @@ -495,7 +498,8 @@ public function cleanString($data)
*/
public static function fromString($line)
{
if (strlen($line) == 0) {
if ( (is_null($line) == true) || (strlen($line) == 0) ) {

throw new Net_DNS2_Exception(
'empty config line provided.',
Net_DNS2_Lookups::E_PARSE_ERROR
Expand Down
7 changes: 3 additions & 4 deletions Net/DNS2/RR/AMTRELAY.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ protected function rrSet(Net_DNS2_Packet &$packet)
break;

case self::AMTRELAY_TYPE_DOMAIN:
$doffset = $packet->offset + $offset;
$this->relay = Net_DNS2_Packet::label($packet, $doffset);

$this->relay = Net_DNS2_Names::unpack($this->rdata, $offset);
break;

default:
Expand Down Expand Up @@ -245,7 +243,8 @@ protected function rrGet(Net_DNS2_Packet &$packet)
break;

case self::AMTRELAY_TYPE_DOMAIN:
$data .= pack('Ca*', strlen($this->relay), $this->relay);
$data .= Net_DNS2_Names::pack($this->relay);
$packet->offset += 1;
break;

default:
Expand Down
6 changes: 3 additions & 3 deletions Net/DNS2/RR/HINFO.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ protected function rrSet(Net_DNS2_Packet &$packet)

$offset = $packet->offset;

$this->cpu = Net_DNS2_Packet::label($packet, $offset);
$this->os = Net_DNS2_Packet::label($packet, $offset);
$this->cpu = Net_DNS2_Names::unpack($packet->rdata, $offset);
$this->os = Net_DNS2_Names::unpack($packet->rdata, $offset);

return true;
}
Expand All @@ -113,7 +113,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->cpu) > 0) {

$data = pack('Ca*Ca*', strlen($this->cpu), $this->cpu, strlen($this->os), $this->os);
$data = Net_DNS2_Names::pack($this->cpu) . Net_DNS2_Names::pack($this->os);

$packet->offset += strlen($data);

Expand Down
Loading

0 comments on commit dc80537

Please sign in to comment.