Skip to content
Snippets Groups Projects
Unverified Commit 7ea9e28c authored by Mathieu Parent's avatar Mathieu Parent
Browse files

Imported Upstream version 1.4.1

parent d838b083
Branches
Tags upstream/1.4.1
No related merge requests found
Showing
with 509 additions and 446 deletions
<?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.
//
error_reporting(E_ALL | E_STRICT);
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';
class Net_DNS2_AllTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('PEAR - Net_DNS2');
$suite->addTestSuite('Net_DNS2_ParserTest');
$suite->addTestSuite('Net_DNS2_ResolverTest');
$suite->addTestSuite('Net_DNS2_DNSSECTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Net_DNS2_AllTests::main') {
Net_DNS2_AllTests::main();
}
?>
<?php
require_once 'Net/DNS2.php';
class Net_DNS2_DNSSECTest extends PHPUnit_Framework_TestCase
{
public function testTSIG()
{
$r = new Net_DNS2_Resolver(array('nameservers' => array('8.8.8.8', '8.8.4.4')));
$r->dnssec = true;
$result = $r->query('org', 'SOA', 'IN');
$this->assertTrue(($result->header->ad == 1));
$this->assertTrue(($result->additional[0] instanceof Net_DNS2_RR_OPT));
$this->assertTrue(($result->additional[0]->do == 1));
}
};
?>
<?php
require_once 'Net/DNS2.php';
//
// This test uses the Google public DNS servers to perform a resolution test; this should work on
// *nix and Windows, but will require an internet connection.
//
class Net_DNS2_ResolverTest extends PHPUnit_Framework_TestCase
{
public function testResolver()
{
$r = new Net_DNS2_Resolver(array('nameservers' => array('8.8.8.8', '8.8.4.4')));
$result = $r->query('google.com', 'MX');
$this->assertSame($result->header->qr, Net_DNS2_Lookups::QR_RESPONSE);
$this->assertSame(count($result->question), 1);
$this->assertTrue(count($result->answer) > 0);
$this->assertTrue($result->answer[0] instanceof Net_DNS2_RR_MX);
}
}
?>
File moved
......@@ -43,7 +43,7 @@
* @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: DNS2.php 216 2013-10-28 04:24:17Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......@@ -72,7 +72,7 @@ class Net_DNS2
/*
* the current version of this library
*/
const VERSION = '1.3.2';
const VERSION = '1.4.1';
/*
* the default path to a resolv.conf file
......@@ -128,7 +128,7 @@ class Net_DNS2
/*
* the max size of the cache file (in bytes)
*/
public $cache_size = 10000;
public $cache_size = 50000;
/*
* the method to use for storing cache data; either "serialize" or "json"
......@@ -203,22 +203,35 @@ class Net_DNS2
/*
* the EDNS(0) UDP payload size to use when making DNSSEC requests
* see RFC 2671 section 6.2.3 for more details
* see RFC 4035 section 4.1 - EDNS Support.
*
* http://tools.ietf.org/html/draft-ietf-dnsext-rfc2671bis-edns0-10
* there is some different ideas on the suggest size to supprt; but it seems to
* be "at least 1220 bytes, but SHOULD support 4000 bytes.
*
* we'll just support 4000
*
*/
public $dnssec_payload_size = 1280;
public $dnssec_payload_size = 4000;
/*
* local sockets
* the last exeception that was generated
*/
protected $sock = array('udp' => array(), 'tcp' => array());
public $last_exception = null;
/*
* the list of exceptions by name server
*/
public $last_exception_list = array();
/*
* name server list
*/
protected $nameservers = array();
public $nameservers = array();
/*
* local sockets
*/
protected $sock = array('udp' => array(), 'tcp' => array());
/*
* if the socket extension is loaded
......@@ -240,11 +253,6 @@ class Net_DNS2
*/
protected $use_cache = false;
/*
* the last erro message returned by the sockets class
*/
private $_last_socket_error = '';
/**
* Constructor - base constructor for the Resolver and Updater
*
......@@ -257,9 +265,17 @@ class Net_DNS2
public function __construct(array $options = null)
{
//
// check for the sockets extension
// check for the sockets extension; we no longer support the sockets library under
// windows- there have been too many errors related to sockets under windows-
// specifically inconsistent socket defines between versions of windows-
//
$this->sockets_enabled = extension_loaded('sockets');
// and since I can't seem to find a way to get the actual windows version, it
// doesn't seem fixable in the code.
//
if ( (extension_loaded('sockets') == true) && (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') ) {
$this->sockets_enabled = true;
}
//
// load any options that were provided
......@@ -360,6 +376,14 @@ class Net_DNS2
} 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
//
......@@ -404,7 +428,7 @@ class Net_DNS2
|| (self::isIPv6($value) == true)
) {
$this->nameservers[] = $value;
$ns[] = $value;
} else {
throw new Net_DNS2_Exception(
......@@ -443,8 +467,21 @@ class Net_DNS2
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
//
......@@ -606,11 +643,14 @@ class Net_DNS2
}
//
// only RSAMD5 and RSASHA1 are supported for SIG(0)
// only RSA algorithms are supported for SIG(0)
//
switch($this->auth_signature->algorithm) {
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSAMD5:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA1:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA256:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA512:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_DSA:
break;
default:
throw new Net_DNS2_Exception(
......@@ -685,9 +725,7 @@ class Net_DNS2
//
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 {
......@@ -702,10 +740,7 @@ class Net_DNS2
//
// 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;
}
}
......@@ -728,9 +763,7 @@ class Net_DNS2
// 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 {
......@@ -745,9 +778,7 @@ class Net_DNS2
//
// 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;
}
}
......@@ -848,372 +879,471 @@ class Net_DNS2
//
$response = null;
$ns = '';
$socket_type = null;
$tcp_fallback = false;
while (1) {
//
// grab the next DNS server
//
if ($tcp_fallback == false) {
$ns = each($this->nameservers);
if ($ns === false) {
$ns = each($this->nameservers);
if ($ns === false) {
if (is_null($this->last_exception) == false) {
throw $this->last_exception;
} else {
throw new Net_DNS2_Exception(
'every name server provided has failed: ' .
$this->_last_socket_error,
'every name server provided has failed',
Net_DNS2_Lookups::E_NS_FAILED
);
}
$ns = $ns[1];
}
$ns = $ns[1];
//
// if the use TCP flag (force TCP) is set, or the packet is bigger
// than 512 bytes, use TCP for sending the packet
// if the use TCP flag (force TCP) is set, or the packet is bigger than our
// max allowed UDP size- which is either 512, or if this is DNSSEC request,
// then whatever the configured dnssec_payload_size is.
//
if ( ($use_tcp == true)
|| (strlen($data) > Net_DNS2_Lookups::DNS_MAX_UDP_SIZE)
|| ($tcp_fallback == true)
) {
$tcp_fallback = false;
$socket_type = Net_DNS2_Socket::SOCK_STREAM;
//
// create the socket object
//
if ( (!isset($this->sock['tcp'][$ns]))
|| (!($this->sock['tcp'][$ns] instanceof Net_DNS2_Socket))
) {
if ($this->sockets_enabled === true) {
$this->sock['tcp'][$ns] = new Net_DNS2_Socket_Sockets(
Net_DNS2_Socket::SOCK_STREAM,
$ns,
$this->dns_port,
$this->timeout
);
} else {
$max_udp_size = Net_DNS2_Lookups::DNS_MAX_UDP_SIZE;
if ($this->dnssec == true)
{
$max_udp_size = $this->dnssec_payload_size;
}
$this->sock['tcp'][$ns] = new Net_DNS2_Socket_Streams(
Net_DNS2_Socket::SOCK_STREAM,
$ns,
$this->dns_port,
$this->timeout
);
}
}
if ( ($use_tcp == true) || (strlen($data) > $max_udp_size) ) {
//
// if a local IP address / port is set, then add it
//
if (strlen($this->local_host) > 0) {
try
{
$response = $this->sendTCPRequest($ns, $data, ($request->question[0]->qtype == 'AXFR') ? true : false);
$this->sock['tcp'][$ns]->bindAddress(
$this->local_host, $this->local_port
);
}
} catch(Net_DNS2_Exception $e) {
//
// open it; if it fails, continue in the while loop
//
if ($this->sock['tcp'][$ns]->open() === false) {
$this->last_exception = $e;
$this->last_exception_list[$ns] = $e;
$this->_last_socket_error = $this->sock['tcp'][$ns]->last_error;
continue;
}
//
// write the data to the socket; if it fails, continue on
// the while loop
//
if ($this->sock['tcp'][$ns]->write($data) === false) {
//
// otherwise, send it using UDP
//
} else {
try
{
$response = $this->sendUDPRequest($ns, $data);
//
// check the packet header for a trucated bit; if it was truncated,
// then re-send the request as TCP.
//
if ($response->header->tc == 1) {
$response = $this->sendTCPRequest($ns, $data);
}
} catch(Net_DNS2_Exception $e) {
$this->last_exception = $e;
$this->last_exception_list[$ns] = $e;
$this->_last_socket_error = $this->sock['tcp'][$ns]->last_error;
continue;
}
}
//
// read the content, using select to wait for a response
//
$size = 0;
$result = null;
//
// make sure header id's match between the request and response
//
if ($request->header->id != $response->header->id) {
//
// handle zone transfer requests differently than other requests.
//
if ($request->question[0]->qtype == 'AXFR') {
$this->last_exception = new Net_DNS2_Exception(
$soa_count = 0;
'invalid header: the request and response id do not match.',
Net_DNS2_Lookups::E_HEADER_INVALID,
null,
$request,
$response
);
while (1) {
$this->last_exception_list[$ns] = $this->last_exception;
continue;
}
//
// read the data off the socket
//
$result = $this->sock['tcp'][$ns]->read($size);
if ( ($result === false)
|| ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE)
) {
$this->_last_socket_error = $this->sock['tcp'][$ns]->last_error;
break;
}
//
// make sure the response is actually a response
//
// 0 = query, 1 = response
//
if ($response->header->qr != Net_DNS2_Lookups::QR_RESPONSE) {
$this->last_exception = new Net_DNS2_Exception(
//
// parse the first chunk as a packet
//
$chunk = new Net_DNS2_Packet_Response($result, $size);
'invalid header: the response provided is not a response packet.',
Net_DNS2_Lookups::E_HEADER_INVALID,
null,
$request,
$response
);
//
// if this is the first packet, then clone it directly, then
// go through it to see if there are two SOA records
// (indicating that it's the only packet)
//
if (is_null($response) == true) {
$response = clone $chunk;
//
// look for a failed response; if the zone transfer
// failed, then we don't need to do anything else at this
// point, and we should just break out.
//
if ($response->header->rcode != Net_DNS2_Lookups::RCODE_NOERROR) {
break;
}
//
// go through each answer
//
foreach ($response->answer as $index => $rr) {
//
// count the SOA records
//
if ($rr->type == 'SOA') {
$soa_count++;
}
}
//
// if we have 2 or more SOA records, then we're done;
// otherwise continue out so we read the rest of the
// packets off the socket
//
if ($soa_count >= 2) {
break;
} else {
continue;
}
$this->last_exception_list[$ns] = $this->last_exception;
continue;
}
} else {
//
// make sure the response code in the header is ok
//
if ($response->header->rcode != Net_DNS2_Lookups::RCODE_NOERROR) {
$this->last_exception = new Net_DNS2_Exception(
'DNS request failed: ' .
Net_DNS2_Lookups::$result_code_messages[$response->header->rcode],
$response->header->rcode,
null,
$request,
$response
);
//
// go through all these answers, and look for SOA records
//
foreach ($chunk->answer as $index => $rr) {
//
// count the number of SOA records we find
//
if ($rr->type == 'SOA') {
$soa_count++;
}
//
// add the records to a single response object
//
$response->answer[] = $rr;
}
//
// if we've found the second SOA record, we're done
//
if ($soa_count >= 2) {
break;
}
}
}
$this->last_exception_list[$ns] = $this->last_exception;
continue;
}
} else {
break;
}
$result = $this->sock['tcp'][$ns]->read($size);
if ( ($result === false)
|| ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE)
) {
$this->_last_socket_error = $this->sock['tcp'][$ns]->last_error;
continue;
}
return $response;
}
//
// create the packet object
//
$response = new Net_DNS2_Packet_Response($result, $size);
}
/**
* sends a DNS request using TCP
*
* @param string $_ns the name server to use for the request
* @param string $_data the raw DNS packet data
* @param boolean $_axfr if this is a zone transfer request
*
* @return Net_DNS2_Packet_Response the reponse object
* @throws Net_DNS2_Exception
* @access private
*
*/
private function sendTCPRequest($_ns, $_data, $_axfr = false)
{
//
// grab the start time
//
$start_time = microtime(true);
break;
//
// see if we already have an open socket from a previous request; if so, try to use
// that instead of opening a new one.
//
if ( (!isset($this->sock['tcp'][$_ns]))
|| (!($this->sock['tcp'][$_ns] instanceof Net_DNS2_Socket))
) {
//
// if the socket library is available, then use that
//
if ($this->sockets_enabled === true) {
$this->sock['tcp'][$_ns] = new Net_DNS2_Socket_Sockets(
Net_DNS2_Socket::SOCK_STREAM, $_ns, $this->dns_port, $this->timeout
);
//
// otherwise the streams library
//
} else {
$socket_type = Net_DNS2_Socket::SOCK_DGRAM;
$this->sock['tcp'][$_ns] = new Net_DNS2_Socket_Streams(
Net_DNS2_Socket::SOCK_STREAM, $_ns, $this->dns_port, $this->timeout
);
}
//
// create the socket object
//
if ( (!isset($this->sock['udp'][$ns]))
|| (!($this->sock['udp'][$ns] instanceof Net_DNS2_Socket))
) {
if ($this->sockets_enabled === true) {
//
// if a local IP address / port is set, then add it
//
if (strlen($this->local_host) > 0) {
$this->sock['udp'][$ns] = new Net_DNS2_Socket_Sockets(
Net_DNS2_Socket::SOCK_DGRAM, $ns, $this->dns_port, $this->timeout
);
} else {
$this->sock['tcp'][$_ns]->bindAddress(
$this->local_host, $this->local_port
);
}
$this->sock['udp'][$ns] = new Net_DNS2_Socket_Streams(
Net_DNS2_Socket::SOCK_DGRAM, $ns, $this->dns_port, $this->timeout
);
}
}
//
// open the socket
//
if ($this->sock['tcp'][$_ns]->open() === false) {
throw new Net_DNS2_Exception(
$this->sock['tcp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
}
//
// write the data to the socket; if it fails, continue on
// the while loop
//
if ($this->sock['tcp'][$_ns]->write($_data) === false) {
throw new Net_DNS2_Exception(
$this->sock['tcp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
//
// read the content, using select to wait for a response
//
$size = 0;
$result = null;
$response = null;
//
// handle zone transfer requests differently than other requests.
//
if ($_axfr == true) {
$soa_count = 0;
while (1) {
//
// if a local IP address / port is set, then add it
// read the data off the socket
//
if (strlen($this->local_host) > 0) {
$result = $this->sock['tcp'][$_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->sock['udp'][$ns]->bindAddress(
$this->local_host, $this->local_port
throw new Net_DNS2_Exception(
$this->sock['tcp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
//
// open it
// parse the first chunk as a packet
//
if ($this->sock['udp'][$ns]->open() === false) {
$this->_last_socket_error = $this->sock['udp'][$ns]->last_error;
continue;
}
$chunk = new Net_DNS2_Packet_Response($result, $size);
//
// write the data to the socket
// if this is the first packet, then clone it directly, then
// go through it to see if there are two SOA records
// (indicating that it's the only packet)
//
if ($this->sock['udp'][$ns]->write($data) === false) {
if (is_null($response) == true) {
$this->_last_socket_error = $this->sock['udp'][$ns]->last_error;
continue;
}
$response = clone $chunk;
//
// read the content, using select to wait for a response
//
$size = 0;
//
// look for a failed response; if the zone transfer
// failed, then we don't need to do anything else at this
// point, and we should just break out.
//
if ($response->header->rcode != Net_DNS2_Lookups::RCODE_NOERROR) {
break;
}
$result = $this->sock['udp'][$ns]->read($size);
if (( $result === false)
|| ($size < Net_DNS2_Lookups::DNS_HEADER_SIZE)
) {
$this->_last_socket_error = $this->sock['udp'][$ns]->last_error;
continue;
}
//
// go through each answer
//
foreach ($response->answer as $index => $rr) {
//
// create the packet object
//
$response = new Net_DNS2_Packet_Response($result, $size);
if (is_null($response)) {
//
// count the SOA records
//
if ($rr->type == 'SOA') {
$soa_count++;
}
}
throw new Net_DNS2_Exception(
'empty response object',
Net_DNS2_Lookups::E_NS_FAILED,
null,
$request
);
}
//
// if we have 2 or more SOA records, then we're done;
// otherwise continue out so we read the rest of the
// packets off the socket
//
if ($soa_count >= 2) {
break;
} else {
continue;
}
//
// check the packet header for a trucated bit; if it was truncated,
// then re-send the request as TCP.
//
if ($response->header->tc == 1) {
} else {
$tcp_fallback = true;
continue;
}
//
// go through all these answers, and look for SOA records
//
foreach ($chunk->answer as $index => $rr) {
//
// count the number of SOA records we find
//
if ($rr->type == 'SOA') {
$soa_count++;
}
break;
//
// add the records to a single response object
//
$response->answer[] = $rr;
}
//
// if we've found the second SOA record, we're done
//
if ($soa_count >= 2) {
break;
}
}
}
}
//
// if $response is null, then we didn't even try once; which shouldn't
// actually ever happen
// everything other than a AXFR
//
if (is_null($response)) {
} else {
throw new Net_DNS2_Exception(
'empty response object',
Net_DNS2_Lookups::E_NS_FAILED,
null,
$request
);
$result = $this->sock['tcp'][$_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) ) {
throw new Net_DNS2_Exception(
$this->sock['tcp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
//
// create the packet object
//
$response = new Net_DNS2_Packet_Response($result, $size);
}
//
// 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.
//
$response->answer_from = $ns;
$response->answer_socket_type = $socket_type;
$response->answer_from = $_ns;
$response->answer_socket_type = Net_DNS2_Socket::SOCK_STREAM;
//
// make sure header id's match between the request and response
// return the Net_DNS2_Packet_Response object
//
if ($request->header->id != $response->header->id) {
return $response;
}
throw new Net_DNS2_Exception(
'invalid header: the request and response id do not match.',
Net_DNS2_Lookups::E_HEADER_INVALID,
null,
$request,
$response
);
/**
* sends a DNS request using UDP
*
* @param string $_ns the name server to use for the request
* @param string $_data the raw DNS packet data
*
* @return Net_DNS2_Packet_Response the reponse object
* @throws Net_DNS2_Exception
* @access private
*
*/
private function sendUDPRequest($_ns, $_data)
{
//
// grab the start time
//
$start_time = microtime(true);
//
// see if we already have an open socket from a previous request; if so, try to use
// that instead of opening a new one.
//
if ( (!isset($this->sock['udp'][$_ns]))
|| (!($this->sock['udp'][$_ns] instanceof Net_DNS2_Socket))
) {
//
// if the socket library is available, then use that
//
if ($this->sockets_enabled === true) {
$this->sock['udp'][$_ns] = new Net_DNS2_Socket_Sockets(
Net_DNS2_Socket::SOCK_DGRAM, $_ns, $this->dns_port, $this->timeout
);
//
// otherwise the streams library
//
} else {
$this->sock['udp'][$_ns] = new Net_DNS2_Socket_Streams(
Net_DNS2_Socket::SOCK_DGRAM, $_ns, $this->dns_port, $this->timeout
);
}
//
// if a local IP address / port is set, then add it
//
if (strlen($this->local_host) > 0) {
$this->sock['udp'][$_ns]->bindAddress(
$this->local_host, $this->local_port
);
}
//
// open the socket
//
if ($this->sock['udp'][$_ns]->open() === false) {
throw new Net_DNS2_Exception(
$this->sock['udp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
}
//
// make sure the response is actually a response
//
// 0 = query, 1 = response
// write the data to the socket
//
if ($response->header->qr != Net_DNS2_Lookups::QR_RESPONSE) {
if ($this->sock['udp'][$_ns]->write($_data) === false) {
throw new Net_DNS2_Exception(
'invalid header: the response provided is not a response packet.',
Net_DNS2_Lookups::E_HEADER_INVALID,
null,
$request,
$response
$this->sock['udp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
//
// make sure the response code in the header is ok
// read the content, using select to wait for a response
//
if ($response->header->rcode != Net_DNS2_Lookups::RCODE_NOERROR) {
$size = 0;
$result = $this->sock['udp'][$_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)) {
throw new Net_DNS2_Exception(
'DNS request failed: ' .
Net_DNS2_Lookups::$result_code_messages[$response->header->rcode],
$response->header->rcode,
null,
$request,
$response
$this->sock['udp'][$_ns]->last_error, Net_DNS2_Lookups::E_NS_SOCKET_FAILED
);
}
//
// create the packet object
//
$response = new Net_DNS2_Packet_Response($result, $size);
//
// 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.
//
$response->answer_from = $_ns;
$response->answer_socket_type = Net_DNS2_Socket::SOCK_DGRAM;
//
// return the Net_DNS2_Packet_Response object
//
return $response;
}
}
......
......@@ -43,7 +43,7 @@
* @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: BitMap.php 198 2013-05-26 05:05:22Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: Cache.php 218 2013-11-28 22:34:20Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.1.0
*
......
......@@ -43,7 +43,7 @@
* @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: File.php 160 2012-07-18 03:57:32Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.1.0
*
......
......@@ -43,7 +43,7 @@
* @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: Shm.php 160 2012-07-18 03:57:32Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.1.0
*
......
......@@ -43,7 +43,7 @@
* @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: Exception.php 197 2013-04-22 00:28:00Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*/
......@@ -92,7 +92,17 @@ class Net_DNS2_Exception extends Exception
//
// call the parent constructor
//
parent::__construct($message, $code, $previous);
// the "previous" argument was added in PHP 5.3.0
//
// https://code.google.com/p/netdns2/issues/detail?id=25
//
if (version_compare(PHP_VERSION, '5.3.0', '>=') == true) {
parent::__construct($message, $code, $previous);
} else {
parent::__construct($message, $code);
}
}
/**
......
......@@ -43,7 +43,7 @@
* @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: Header.php 198 2013-05-26 05:05:22Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: Lookups.php 215 2013-10-28 04:20:36Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......@@ -168,6 +168,7 @@ class Net_DNS2_Lookups
const E_NS_INVALID_FILE = 200;
const E_NS_INVALID_ENTRY = 201;
const E_NS_FAILED = 202;
const E_NS_SOCKET_FAILED = 203;
const E_PACKET_INVALID = 300;
const E_PARSE_ERROR = 301;
......@@ -273,15 +274,18 @@ class Net_DNS2_Lookups
'NSEC3PARAM' => 51, // RFC 5155
'TLSA' => 52, // RFC 6698
// 52 - 54 unassigned
// 53 - 54 unassigned
'HIP' => 55, // RFC 5205
'NINFO' => 56, // Not implemented
'RKEY' => 57, // Not implemented
'TALINK' => 58, // IETF (draft-barwood-dnsop-ds-publish-02)
'CDS' => 59, // IETF (draft-barwood-dnsop-ds-publish-02)
'TALINK' => 58, //
'CDS' => 59, // RFC 7344
'CDNSKEY' => 60, // RFC 7344
'OPENPGPKEY' => 61, // IETF (draft-ietf-dane-openpgpkey)
'CSYNC' => 62, // RFC 7477
// 60 - 98 unassigned
// 63 - 98 unassigned
'SPF' => 99, // RFC 4408
'UINFO' => 100, // no RFC, Not implemented
......@@ -383,6 +387,9 @@ class Net_DNS2_Lookups
55 => 'Net_DNS2_RR_HIP',
58 => 'Net_DNS2_RR_TALINK',
59 => 'Net_DNS2_RR_CDS',
60 => 'Net_DNS2_RR_CDNSKEY',
61 => 'Net_DNS2_RR_OPENPGPKEY',
62 => 'Net_DNS2_RR_CSYNC',
99 => 'Net_DNS2_RR_SPF',
104 => 'Net_DNS2_RR_NID',
105 => 'Net_DNS2_RR_L32',
......
......@@ -43,7 +43,7 @@
* @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: Packet.php 218 2013-11-28 22:34:20Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: Request.php 155 2012-05-06 23:45:23Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......@@ -104,9 +104,15 @@ class Net_DNS2_Packet_Request extends Net_DNS2_Packet
//
$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
......@@ -139,12 +145,15 @@ class Net_DNS2_Packet_Request extends Net_DNS2_Packet
);
}
//
// if it's a PTR request for an IP address, then make sure we tack on
// the arpa domain
//
if ($type == 'PTR') {
//
// if it's a PTR request for an IP address, then make sure we tack on
// the arpa domain.
//
// there are other types of PTR requests, so if an IP adress doesn't match,
// then just let it flow through and assume it's a hostname
//
if (Net_DNS2::isIPv4($name) == true) {
//
......@@ -174,19 +183,6 @@ class Net_DNS2_Packet_Request extends Net_DNS2_Packet
Net_DNS2_Lookups::E_PACKET_INVALID
);
}
} else if (preg_match('/arpa$/', $name) == true) {
//
// an already formatted IPv4 or IPv6 address in the arpa domain
//
} else {
throw new Net_DNS2_Exception(
'unsupported PTR value: ' . $name,
Net_DNS2_Lookups::E_PACKET_INVALID
);
}
}
......
......@@ -43,7 +43,7 @@
* @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: Response.php 198 2013-05-26 05:05:22Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......@@ -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
*
......
......@@ -43,7 +43,7 @@
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2011 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id: PrivateKey.php 133 2011-12-03 23:42:24Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 1.1.0
*
......@@ -134,27 +134,27 @@ class Net_DNS2_PrivateKey
/*
* DSA: prime
*/
//private $_prime;
public $prime;
/*
* DSA: subprime
*/
//private $_subprime;
public $subprime;
/*
* DSA: base
*/
//private $_base;
public $base;
/*
* DSA: private value
*/
//private $_private_value;
public $private_value;
/*
* DSA: public value
*/
//private $_public_value;
public $public_value;
/**
* Constructor - base constructor the private key container class
......@@ -308,26 +308,26 @@ class Net_DNS2_PrivateKey
//
// DSA - this won't work in PHP until the OpenSSL extension is better
//
/*case 'prime(p)':
$this->_prime = $value;
case 'prime(p)':
$this->prime = $value;
break;
case 'subprime(q)':
$this->_subprime = $value;
$this->subprime = $value;
break;
case 'base(g)':
$this->_base = $value;
$this->base = $value;
break;
case 'private_value(x)':
$this->_private_value = $value;
$this->private_value = $value;
break;
case 'public_value(y)':
$this->_public_value = $value;
$this->public_value = $value;
break;
*/
default:
throw new Net_DNS2_Exception(
'unknown private key data: ' . $key . ': ' . $value,
......@@ -348,6 +348,8 @@ class Net_DNS2_PrivateKey
//
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSAMD5:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA1:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA256:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_RSASHA512:
$args = array(
......@@ -369,22 +371,22 @@ class Net_DNS2_PrivateKey
//
// DSA - this won't work in PHP until the OpenSSL extension is better
//
/*case Net_DNS2_Lookups::DNSSEC_ALGORITHM_DSA:
case Net_DNS2_Lookups::DNSSEC_ALGORITHM_DSA:
$args = array(
'dsa' => array(
'p' => base64_decode($this->_prime),
'q' => base64_decode($this->_subprime),
'g' => base64_decode($this->_base),
'priv_key' => base64_decode($this->_private_value),
'pub_key' => base64_decode($this->_public_value)
'p' => base64_decode($this->prime),
'q' => base64_decode($this->subprime),
'g' => base64_decode($this->base),
'priv_key' => base64_decode($this->private_value),
'pub_key' => base64_decode($this->public_value)
)
);
break;
*/
default:
throw new Net_DNS2_Exception(
'we only currently support RSAMD5 and RSASHA1 encryption.',
......
......@@ -43,7 +43,7 @@
* @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: Question.php 124 2011-12-02 23:23:15Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: RR.php 188 2013-03-31 01:25:46Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: A.php 113 2011-07-25 02:54:19Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
......@@ -43,7 +43,7 @@
* @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: AAAA.php 179 2012-11-23 05:49:01Z mike.pultz $
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment