Re: [PATCH] Net_DNS
| From: | Arnaud Limbourg | Date: | Wed, 05 Jan 2005 23:01:58 +0000 |
| Subject: | Re: [PATCH] Net_DNS | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-35364@lists.php.net to get a copy of this message | ||
Can you add it to pear.php.net as a bug so that it does not get lost.
Arnaud.
Mark Reidenbach wrote:
The attached patch fixes bug ids 678, 1871, and 2141 as well as the following fixes and improvements. Note that I did not touch the send_tcp or send_udp_no_sock_lib functions and these paths may still exhibit bugs with php5. Can now query FQDNs that end in a dot (bug 678). Works with php5 by not reassigning $this and using a variable to hold the subclass instead (bug 1871). Net_DNS_RR_MX will now return a result even if the preference is 0 (bug 2141). Uses array_key_exists instead of checking to see if a non-existent key's strlen is 0. The Net_DNS_Resolver::send_udp_with_sock_lib function now sends the query to all the resolvers DNS servers and then checks to see if any answered. It also retries until the timeout period is reached or the retrans count is reached rather than returning after the first unsuccessful attempt (ported from PERL). Before these changes, only the first nameserver was sent the query which resulted in the function returning null if one of two servers were temporarily down instead of the correct response from the 2nd server if it was up. Can now pass in an array to Net_DNS_Resolver to change the default resolver options. Net_DNS_Resolver::nextid works with register globals off You can now set the nameservers to query using Net_DNS_Resolver::nameservers(). This was not functioning before due to testing $ns when the function parameter is $nsa. You can now set nameservers using a FQDN instead of having to use ip addresses. The FQDNs are looked up using the system's default resolver. (This was ported from PERL's Net::DNS and was marked as needing to be ported in the function comments) Net_DNS_Resolver::cname_addr() was ported from PERL and now returns the address for A and CNAME records. This is needed for Net_DNS_Resolver::nameservers() to function properly. A "." was added to "in-addr.arpa" in Net_DNS_Resolver::search() to match PERL Whitespace fixes. ------------------------------------------------------------------------ diff -ru pear/Net_DNS/DNS/Packet.php newpear/Net_DNS/DNS/Packet.php --- pear/Net_DNS/DNS/Packet.php 2004-03-04 23:56:06.000000000 -0600 +++ newpear/Net_DNS/DNS/Packet.php 2004-12-23 11:12:25.000000000 -0600 @@ -164,9 +164,10 @@$this->answer = NULL; $this->authority = NULL; $this->additional = NULL; - if ($this->debug) { - $this->display(); - } + /* if ($this->debug) { + * $this->display(); + * } + */ } /* }}} */@@ -354,7 +355,7 @@*/ function dn_comp($name, $offset) { - $names = explode('.', $name); + $names = array_filter(explode('.', $name)); $compname = ''; while (count($names)) { $dname = join('.', $names);@@ -468,7 +469,7 @@$a = unpack("@$offset/Cchar", $packet); $len = $a['char'];- $offset++;+ $offset++; if ($len + $offset > $packetlen) { $name = substr($packet, $offset);@@ -568,6 +569,7 @@$data, $offset)); + $rrobj= $rrobj->getRR(); if (is_null($rrobj)) { return(array(NULL, NULL)); }diff -ru pear/Net_DNS/DNS/RR/A.php newpear/Net_DNS/DNS/RR/A.php --- pear/Net_DNS/DNS/RR/A.php 2003-02-24 12:33:07.000000000 -0600 +++ newpear/Net_DNS/DNS/RR/A.php 2004-12-22 14:25:39.000000000 -0600 @@ -93,7 +93,6 @@} /* }}} */- } /* }}} */ /* VIM settings {{{ diff -ru pear/Net_DNS/DNS/RR/AAAA.php newpear/Net_DNS/DNS/RR/AAAA.php --- pear/Net_DNS/DNS/RR/AAAA.php 2003-05-06 13:04:28.000000000 -0500 +++ newpear/Net_DNS/DNS/RR/AAAA.php 2004-12-22 14:26:08.000000000 -0600 @@ -50,7 +50,7 @@$this->rdata = $rro->rdata; if ($offset) {- $this->address = Net_DNS_RR_AAAA::ipv6_decompress(substr($this->rdata, 0, $this->rdlength));+ $this->address = Net_DNS_RR_AAAA::ipv6_decompress(substr($this->rdata, 0, $this->rdlength)); } else { if (strlen($data)) { if (count($adata = explode(':', $data, 8)) >= 3) {@@ -75,7 +75,7 @@/* Net_DNS_RR_AAAA::rr_rdata($packet, $offset) {{{ */ function rr_rdata($packet, $offset) {- return Net_DNS_RR_AAAA::ipv6_compress($this->address);+ return Net_DNS_RR_AAAA::ipv6_compress($this->address); } /* }}} */@@ -124,6 +124,7 @@return preg_replace('/(:?(0:){2,})/', '::', $addr); } }+/* }}} */} /* }}} */ diff -ru pear/Net_DNS/DNS/RR/HINFO.php newpear/Net_DNS/DNS/RR/HINFO.php --- pear/Net_DNS/DNS/RR/HINFO.php 2003-05-06 14:40:55.000000000 -0500 +++ newpear/Net_DNS/DNS/RR/HINFO.php 2004-12-22 13:51:00.000000000 -0600 @@ -35,7 +35,7 @@var $ttl; var $rdlength; var $rdata;- var $cpu;+ var $cpu; var $os; /* }}} */@@ -69,7 +69,7 @@} $this->cpu = $regs[1];- $this->os = $regs[2];+ $this->os = $regs[2]; } } diff -ru pear/Net_DNS/DNS/RR/MX.php newpear/Net_DNS/DNS/RR/MX.php--- pear/Net_DNS/DNS/RR/MX.php 2003-02-24 12:33:07.000000000 -0600 +++ newpear/Net_DNS/DNS/RR/MX.php 2005-01-04 17:31:10.000000000 -0600 @@ -68,7 +68,7 @@/* Net_DNS_RR_MX::rdatastr() {{{ */ function rdatastr() { - if ($this->preference) { + if (is_numeric($this->preference)) { return($this->preference . ' ' . $this->exchange . '.'); } return('; no data');diff -ru pear/Net_DNS/DNS/RR/NAPTR.php newpear/Net_DNS/DNS/RR/NAPTR.php --- pear/Net_DNS/DNS/RR/NAPTR.php 2003-05-06 14:35:38.000000000 -0500 +++ newpear/Net_DNS/DNS/RR/NAPTR.php 2004-12-22 13:51:00.000000000 -0600 @@ -35,12 +35,12 @@var $ttl; var $rdlength; var $rdata;- var $order; - var $preference; - var $flags; - var $services; - var $regex; - var $replacement;+ var $order; + var $preference; + var $flags; + var $services; + var $regex; + var $replacement; /* }}} */ /* class constructor - RR(&$rro, $data, $offset = '') {{{ */diff -ru pear/Net_DNS/DNS/RR/SRV.php newpear/Net_DNS/DNS/RR/SRV.php --- pear/Net_DNS/DNS/RR/SRV.php 2003-05-05 18:58:08.000000000 -0500 +++ newpear/Net_DNS/DNS/RR/SRV.php 2004-12-22 13:51:00.000000000 -0600 @@ -36,8 +36,8 @@var $rdlength; var $rdata; var $preference;- var $weight; - var $port;+ var $weight; + var $port; var $target; /* }}} */diff -ru pear/Net_DNS/DNS/RR/TXT.php newpear/Net_DNS/DNS/RR/TXT.php --- pear/Net_DNS/DNS/RR/TXT.php 2003-05-06 14:35:38.000000000 -0500 +++ newpear/Net_DNS/DNS/RR/TXT.php 2004-12-22 13:51:00.000000000 -0600 @@ -35,7 +35,7 @@var $ttl; var $rdlength; var $rdata;- var $text;+ var $text; /* }}} */ /* class constructor - RR(&$rro, $data, $offset = '') {{{ */diff -ru pear/Net_DNS/DNS/RR.php newpear/Net_DNS/DNS/RR.php --- pear/Net_DNS/DNS/RR.php 2004-03-04 23:56:06.000000000 -0600 +++ newpear/Net_DNS/DNS/RR.php 2005-01-04 17:00:01.000000000 -0600 @@ -21,18 +21,18 @@ */ /* Include files {{{ */ -require_once("$phpdns_basedir/DNS/RR/A.php"); -require_once("$phpdns_basedir/DNS/RR/AAAA.php"); -require_once("$phpdns_basedir/DNS/RR/NS.php"); -require_once("$phpdns_basedir/DNS/RR/CNAME.php"); -require_once("$phpdns_basedir/DNS/RR/PTR.php"); -require_once("$phpdns_basedir/DNS/RR/SOA.php"); -require_once("$phpdns_basedir/DNS/RR/MX.php"); -require_once("$phpdns_basedir/DNS/RR/TSIG.php"); -require_once("$phpdns_basedir/DNS/RR/TXT.php"); -require_once("$phpdns_basedir/DNS/RR/HINFO.php"); -require_once("$phpdns_basedir/DNS/RR/SRV.php"); -require_once("$phpdns_basedir/DNS/RR/NAPTR.php"); +require_once('Net/DNS/RR/A.php'); +require_once('Net/DNS/RR/AAAA.php'); +require_once('Net/DNS/RR/NS.php'); +require_once('Net/DNS/RR/CNAME.php'); +require_once('Net/DNS/RR/PTR.php'); +require_once('Net/DNS/RR/SOA.php'); +require_once('Net/DNS/RR/MX.php'); +require_once('Net/DNS/RR/TSIG.php'); +require_once('Net/DNS/RR/TXT.php'); +require_once('Net/DNS/RR/HINFO.php'); +require_once('Net/DNS/RR/SRV.php'); +require_once('Net/DNS/RR/NAPTR.php'); /* }}} */ /* Net_DNS_RR object definition {{{ */ /** @@ -52,6 +52,7 @@var $ttl; var $rdlength; var $rdata; + var $subclass; /* }}} */ /*@@ -62,12 +63,12 @@function Net_DNS_RR($rrdata) { if (is_string($rrdata)) { - $this = $this->new_from_string($rrdata); + $this->subclass = $this->new_from_string($rrdata); } else if (count($rrdata) == 7) { list ($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = $rrdata; - $this = $this->new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset); + $this->subclass = $this->new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset); } else { - $this = $this->new_from_array($rrdata); + $this->subclass = $this->new_from_array($rrdata); } } @@ -86,7 +87,7 @@ $subclass = new $scn($this, $data, $offset); return($subclass); } else { - return($this); + return(NULL); } } @@ -175,10 +176,10 @@ $rc = new $scn($this, $rdata); return($rc); } else { - return($this); + return(NULL); } } else { - return(NULL); + return(false); } } @@ -210,7 +211,7 @@ $rc = new $scn($this, $rdata); return($rc); } else - return($this); + return(NULL); } /* }}} */@@ -278,6 +279,28 @@return($data); }++ /* }}} */ + /* Net_DNS_RR:getRR {{{ */ + /** + * Returns the dns resource record or null if no record was found + * + * @return mixed Returns a Net_DNS_RR_* object if a subclass for the specific resource type exists, + * a Net_DNS_RR object if a subclass does not exist, + * or null if no resource record was found + * @access public + */ + function getRR() + { + if (is_null($this->subclass)) { + return($this); + } elseif ($this->subclass === false) { + return(NULL); + } else { + return($this->subclass); + } + } + /* }}} */} /* }}} */ diff -ru pear/Net_DNS/DNS/Resolver.php newpear/Net_DNS/DNS/Resolver.php --- pear/Net_DNS/DNS/Resolver.php 2004-02-26 20:59:55.000000000 -0600 +++ newpear/Net_DNS/DNS/Resolver.php 2005-01-04 17:47:36.000000000 -0600 @@ -167,6 +167,13 @@* @var integer $tcp_timeout * @access public */ + var $udp_timeout; + /** + * The number of seconds after which a UDP connetion should timeout + * + * @var integer $udp_timeout + * @access public + */ var $tcp_timeout; /** * The location of the system resolv.conf file.@@ -215,7 +222,7 @@* @var boolean $useEnhancedSockets; * @access public */ - var $useEnhancedSockets = 1; + var $useEnhancedSockets = true; /** * An array of sockets connected to a name servers *@@ -257,27 +264,26 @@/** * Initializes the Resolver Object */ - function Net_DNS_Resolver() + function Net_DNS_Resolver($default = array()) { - $default = array( - 'nameservers' => array(), - 'port' => '53', - 'domain' => '', - 'searchlist' => array(), - 'retrans' => 5, - 'retry' => 4, - 'usevc' => 0, - 'stayopen' => 0, - 'igntc' => 0, - 'recurse' => 1, - 'defnames' => 1, - 'dnsrch' => 1, - 'debug' => 0, - 'errorstring' => 'unknown error or no error', - 'answerfrom' => '', - 'answersize' => 0, - 'tcp_timeout' => 120 - ); + $this->nameservers = array(); + $this->port = 53; + $this->domain = ''; + $this->searchlist = array(); + $this->retrans = 5; + $this->retry = 4; + $this->usevc = 0; + $this->stayopen = 0; + $this->igntc = 0; + $this->recurse = 1; + $this->defnames = 1; + $this->dnsrch = 1; + $this->debug = false; + $this->errorstring = 'unknown error or no error'; + $this->answerfrom = ''; + $this->answersize = 0; + $this->tcp_timeout = 120; + $this->udp_timeout = 10; foreach ($default as $k => $v) { $this->{$k} = $v; }@@ -405,7 +411,7 @@*/ function string() { - $state = ";; Net_DNS_Resolver state:\n"; + $state = ";; Net_DNS_Resolver state:\n"; $state .= ';; domain = ' . $this->domain . "\n"; $state .= ';; searchlist = ' . implode(' ', $this->searchlist) . "\n"; $state .= ';; nameservers = ' . implode(' ', $this->nameservers) . "\n";@@ -428,52 +434,66 @@/* Net_DNS_Resolver::nextid() {{{ */ /** * Returns the next request Id to be used for the DNS packet header + * + * @access private */ function nextid() { - global $_Net_DNS_packet_id;-- return($_Net_DNS_packet_id++); + if ($GLOBALS['_Net_DNS_packet_id']++ > 65535) { + $GLOBALS['_Net_DNS_packet_id']= 1; + } + return($GLOBALS['_Net_DNS_packet_id']); } + /* }}} */ - /* not completed - Net_DNS_Resolver::nameservers() {{{ */ + /* Net_DNS_Resolver::nameservers() {{{ */ /** - * Unknown - not ported yet + * Gets or sets the nameservers to be queried. + * + * Returns the current nameservers if an array of new nameservers is not + * given as the argument OR sets the nameservers to the given nameservers. + * + * Nameservers not specified by ip address must be able to be resolved by + * your systems default resolver to be used. + * + * @access public */ function nameservers($nsa) { $defres = new Net_DNS_Resolver(); - if (is_array($ns)) { + if (is_array($nsa)) { + $a= array(); foreach ($nsa as $ns) { - if (ereg('^[0-9]+(\.[0-9]+){0,3}$', $ns, $regs)) { - $newns[count($newns)] = $ns; + $regs= array(); + if (preg_match('/^[0-9]+(\.[0-9]+){0,3}$/', $ns, $regs)) { + $a[] = $ns; } else { - /* - * This still needs to be ported - * - if ($ns !~ /\./) { - if (defined $defres->searchlist) { - @names = map { $ns . "." . $_ } - $defres->searchlist; - } - elsif (defined $defres->domain) { - @names = ($ns . "." . $defres->domain); - } - } - else { - @names = ($ns); - }-- my $packet = $defres->search($ns); - $this->errorstring($defres->errorstring); - if (defined($packet)) { - push @a, cname_addr([@names], $packet); - } - */ + $names = array(); + if (preg_match('/\./', $ns) == 0) { + if (!empty($defres->searchlist)) { + foreach ($defres->searchlist as $suffix) { + $names[] = $ns .'.' . $suffix; + } + } elseif (!empty($defres->domain)) { + $names[] = $ns .'.'. $defres->domain; + } + } else { + $names[] = ($ns); + } + $packet = $defres->search($ns); + if (is_object($packet)) { + $addresses = $this->cname_addr($names, $packet); + foreach ($addresses as $b) { + $a[] = $b; + } + $a = array_unique($a); + } } } - $this->nameservers = $nsa; + if (count($a) > 0) { + $this->nameservers = $a; + } } return($this->nameservers); }@@ -483,9 +503,25 @@/** * Unknown - not ported yet */ - function cname_addr() + function cname_addr($names, $packet) { - } + $addr = array(); + //my $oct2 = '(?:2[0-4]\d|25[0-5]|[0-1]?\d\d|\d)'; + foreach ($packet->answer as $rr) { + if (in_array($rr->name, $names)) { + if ($rr->type == 'CNAME') { + $names[] = $rr->cname; + } elseif ($rr->type == 'A') { + // Run a basic taint check. + //next RR unless $rr->address =~ m/^($oct2\.$oct2\.$oct2\.$oct2)$/o;++ $addr[] = $rr->address; + } + }+ } + return $addr; + } +/* }}} */ /* Net_DNS_Resolver::search() {{{ */ /**@@ -509,14 +545,14 @@* PTR query. */ if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) { - $name = "$regs[4].$regs[3].$regs[2].$regs[1].in-addr.arpa"; + $name = $regs[4] .'.'. $regs[3] .'.'. $regs[2] .'.'. $regs[1] .'.in-addr.arpa.'; $type = 'PTR'; } /* * If the name contains at least one dot then try it as is first. */ - if (strchr($name, '.')) { + if (strpos($name, '.')) { if ($this->debug) { echo ";; search($name, $type, $class)\n"; }@@ -530,7 +566,7 @@* If the name doesn't end in a dot then apply the search list. */ $domain = ''; - if ((! preg_match('/\.$/', $name)) && $this->dnsrch) { + if ((substr($name, -1) != '.') && $this->dnsrch) { foreach ($this->searchlist as $domain) { $newname = "$name.$domain"; if ($this->debug) {@@ -560,7 +596,7 @@/* * No answer was found. */ - return(0); + return(false); } /* }}} */@@ -574,8 +610,8 @@* @param string $name The name (LHS) of a resource record to query. * @param string $type The type of record to query. * @param string $class The class of record to query. - * @return mixed an object of type Net_DNS_Packet, regarless of whether the packet - * has an answer or not + * @return mixed an object of type Net_DNS_Packet, regardless of whether + the packet has an answer or not * @see Net_DNS::typesbyname(), Net_DNS::classesbyname() * @access public */@@ -584,7 +620,7 @@/* * If the name doesn't contain any dots then append the default domain. */ - if ((strchr($name, '.') < 0) && $this->defnames) { + if ((strpos($name, '.') === false) && $this->defnames) { $name .= '.' . $this->domain; } @@ -593,7 +629,7 @@ * PTR query. */ if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) { - $name = "$regs[4].$regs[3].$regs[2].$regs[1].in-addr.arpa"; + $name = $regs[4] .'.'. $regs[3] .'.'. $regs[2] .'.'. $regs[1] .'.in-addr.arpa.'; $type = 'PTR'; } @@ -612,7 +648,7 @@ /** * Queries nameservers for an answer * - * Queries the nameservers listed in the resolver configuration for an + * Queries the nameservers listed in the resolver configuration for an * answer to a question packet. * * @param string $name The name (LHS) of a resource record to query.@@ -629,7 +665,7 @@if (is_object($ans) && $ans->header->ancount > 0) { return($ans); } - return(0); + return(false); } /* }}} */@@ -653,7 +689,6 @@{ $packet = $this->make_query_packet($packetORname, $qtype, $qclass); $packet_data = $packet->data();-if ($this->usevc != 0 || strlen($packet_data > 512)) { $ans = $this->send_tcp($packet, $packet_data); } else {@@ -718,7 +753,6 @@} return(NULL); } - $timeout = $this->tcp_timeout; foreach ($this->nameservers as $ns) { $dstport = $this->port;@@ -730,7 +764,7 @@$sock = &$this->sockets[$sock_key]; } else { if (! ($sock = @fsockopen($ns, $dstport, $errno, - $errstr, $timeout))) { + $errstr, $this->tcp_timeout))) { $this->errorstring = 'connection failed'; if ($this->debug) { echo ";; ERROR: send_tcp: connection failed: $errstr\n";@@ -761,7 +795,7 @@} } - socket_set_timeout($sock, $timeout); + socket_set_timeout($sock, $this->tcp_timeout); $buf = fread($sock, 2); $e = socket_get_status($sock); /* If $buf is empty, we want to supress errors@@ -815,7 +849,7 @@function send_udp_no_sock_lib($packet, $packet_data) { $retrans = $this->retrans; - $timeout = $retrans; + $timeout = $this->udp_timeout; /* * PHP doesn't have excellent socket support as of this writing.@@ -932,7 +966,7 @@// Create a socket handle for each nameserver foreach ($this->nameservers as $nameserver) { if ((($sock[$ctr++] = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) >= 0) && - socket_connect($sock[$ctr-1], $nameserver, $this->port) >= 0) { + socket_connect($sock[$ctr-1], $nameserver, $this->port) == true) { $peerhost[$ctr-1] = $nameserver; $peerport[$ctr-1] = $this->port; socket_set_nonblock($sock[$ctr-1]);@@ -947,53 +981,76 @@return(NULL); } - for ($i = 0; $i < $this->retry; $i++, $retrans *= 2, - $timeout = (int) ($retrans / (count($ns)+1))) { + $retrans_divisor = ((count($this->nameservers) == 0) ? 1 : count($this->nameservers)); + if (!empty($this->udp_timeout)) { + $stop_time = microtime(true) + $this->udp_timeout; + }++ // Perform each round of retries. + for ($i = 0; $i < $this->retry; $i++) { + if ($i != 0) { + $retrans *= 2; + $timeout = (int) ($retrans / $retrans_divisor); + } if ($timeout < 1) { $timeout = 1; } + $set = array_values($sock); + // Try each nameserver. foreach ($sock as $k => $s) { + if (isset($stop_time)) { + $now = microtime(true); + if ($stop_time < $now) { + $this->errorstring = 'query timed out'; + return(NULL); + } + if (($timeout > 1) && ($timeout > ($stop_time - $now))) { + $timeout = $stop_time - $now; + } + }+if ($this->debug) { echo ';; send_udp(' . $peerhost[$k] . ':' . $peerport[$k] . '): sending ' . strlen($packet_data) . " bytes\n"; }-if (! socket_write($s, $packet_data)) { if ($this->debug) { echo ";; send error\n"; } }-- $set = array($s); - if ($this->debug) { - echo ";; timeout set to $timeout seconds\n"; - } - $changed = socket_select($set, $w = null, $e = null, $timeout); - if ($changed) { - $buf = socket_read($set[0], 512); - $this->answerfrom = $peerhost[$k]; - $this->answersize = strlen($buf); - if ($this->debug) { - echo ';; answer from ' . $peerhost[$k] . ':' . - $peerport[$k] . ': ' . strlen($buf) . " bytes\n"; + } + + if ($this->debug) { + echo ";; timeout set to $timeout seconds\n"; + } + $changed = socket_select($set, $write = null, $exceptions = null, $timeout); + if ($changed) { + foreach ($sock as $k => $s) { + if (!in_array($s, $set)) { + continue; } - $ans = new Net_DNS_Packet($this->debug); - if ($ans->parse($buf)) { - if ($ans->header->qr != '1') { - continue; + $buf = ''; + if (socket_recv($s, $buf, 512, 0)) { + $this->answerfrom = $peerhost[$k]; + $this->answersize = strlen($buf); + if ($this->debug) { + echo ';; answer from ' . $peerhost[$k] . ':' . $peerport[$k] . ': ' . strlen($buf) . " bytes\n"; } - if ($ans->header->id != $packet->header->id) { - continue; + $ans = new Net_DNS_Packet($this->debug); + if ($ans->parse($buf)) { + if ($ans->header->qr != '1') { + continue; + } + if ($ans->header->id != $packet->header->id) { + continue; + } + $this->errorstring = $ans->header->rcode; + $ans->answerfrom = $this->answerfrom; + $ans->answersize = $this->answersize; + return($ans); } - $this->errorstring = $ans->header->rcode; - $ans->answerfrom = $this->answerfrom; - $ans->answersize = $this->answersize; - return($ans); } }-- $this->errorstring = 'query timed out'; - return(NULL); } } }@@ -1054,7 +1111,7 @@* PTR query. */ if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) { - $name = "$regs[4].$regs[3].$regs[2].$regs[1].in-addr.arpa"; + $name = $regs[4] .'.'. $regs[3] .'.'. $regs[2] .'.'. $regs[1] .'.'. '.in-addr.arpa.'; $type = 'PTR'; } @@ -1291,7 +1348,7 @@ /** * Unknown - not ported yet */ - function read_tcp($sock, $nbytes, $debug = 0) + function read_tcp($sock, $nbytes, $debug = false) { $buf = ''; while (strlen($buf) < $nbytes) {@@ -1318,6 +1375,7 @@} return($buf); }+/* }}} */} /* }}} */ diff -ru pear/Net_DNS/DNS.php newpear/Net_DNS/DNS.php --- pear/Net_DNS/DNS.php 2003-06-10 17:18:38.000000000 -0500 +++ newpear/Net_DNS/DNS.php 2004-12-24 15:47:04.000000000 -0600 @@ -43,28 +43,27 @@ /* Include information {{{ */- $phpdns_basedir = 'Net'; - require_once("$phpdns_basedir/DNS/Header.php"); - require_once("$phpdns_basedir/DNS/Question.php"); - require_once("$phpdns_basedir/DNS/Packet.php"); - require_once("$phpdns_basedir/DNS/Resolver.php"); - require_once("$phpdns_basedir/DNS/RR.php"); + require_once('Net/DNS/Header.php'); + require_once('Net/DNS/Question.php'); + require_once('Net/DNS/Packet.php'); + require_once('Net/DNS/Resolver.php'); + require_once('Net/DNS/RR.php'); /* }}} *//* GLOBAL VARIABLE definitions {{{ */ // Used by the Net_DNS_Resolver object to generate an ID -mt_srand((double) microtime() * 10000); -$_Net_DNS_packet_id = (int)mt_rand(0, 65535); +mt_srand(floor(microtime(true) * 10000)); +$GLOBALS['_Net_DNS_packet_id'] = mt_rand(0, 65535); /* }}} */ /* Net_DNS object definition (incomplete) {{{ */ /** * Initializes a resolver object * - * Net_DNS allows you to query a nameserver for DNS lookups. It bypasses the + * Net_DNS allows you to query a nameserver for DNS lookups. It bypasses the * system resolver library entirely, which allows you to query any nameserver, - * set your own values for retries, timeouts, recursion, etc. + * set your own values for retries, timeouts, recursion, etc. * * @author Eric Kilfoil <eric@ypass.net> * @package Net_DNS @@ -86,6 +85,7 @@var $RRFIXEDSZ = 10; var $INT32SZ = 4; var $INT16SZ = 2; + /* }}} */ /* class constructor - Net_DNS() {{{ */ /**@@ -125,10 +125,7 @@'NS_NOTIFY_OP' => 4, // RFC 1996 'UPDATE' => 5, // RFC 2136 ); - if (! strlen($op[$opcode])) { - $op[$opcode] = NULL; - } - return($op[$opcode]); + return (array_key_exists($opcode, $op) ? $op[$opcode] : NULL); } /* }}} */@@ -151,10 +148,7 @@4 => 'NS_NOTIFY_OP', 5 => 'UPDATE', ); - if (! strlen($opval[$opcodeval])) { - $opval[$opcodeval] = NULL; - } - return($opval[$opcodeval]); + return (array_key_exists($opcodeval, $opval) ? $opval[$opcodeval] : NULL); } /*}}}*/@@ -196,10 +190,7 @@'NOTAUTH' => 9, // RFC 2136 'NOTZONE' => 10, // RFC 2136 ); - if (! strlen($rc[$rcode])) { - $rc[$rcode] = NULL; - } - return($rc[$rcode]); + return (array_key_exists($rcode, $rc) ? $rc[$rcode] : NULL); } /*}}}*/@@ -228,10 +219,7 @@9 => 'NOTAUTH', 10 => 'NOTZONE', ); - if (! strlen($rc[$rcodeval])) { - $rc[$rcodeval] = NULL; - } - return($rc[$rcodeval]); + return (array_key_exists($rcodeval, $rc) ? $rc[$rcodeval] : NULL); } /*}}}*/@@ -297,56 +285,78 @@function typesbyname($rrtype) { $rc = array( - 'A' => 1, - 'NS' => 2, - 'MD' => 3, - 'MF' => 4, - 'CNAME' => 5, - 'SOA' => 6, - 'MB' => 7, - 'MG' => 8, - 'MR' => 9, - 'NULL' => 10, - 'WKS' => 11, - 'PTR' => 12, - 'HINFO' => 13, - 'MINFO' => 14, - 'MX' => 15, - 'TXT' => 16, - 'RP' => 17, - 'AFSDB' => 18, - 'X25' => 19, - 'ISDN' => 20, - 'RT' => 21, - 'NSAP' => 22, - 'NSAP_PTR' => 23, - 'SIG' => 24, - 'KEY' => 25, - 'PX' => 26, - 'GPOS' => 27, - 'AAAA' => 28, - 'LOC' => 29, - 'NXT' => 30, - 'EID' => 31, - 'NIMLOC' => 32, - 'SRV' => 33, - 'ATMA' => 34, - 'NAPTR' => 35, - 'UINFO' => 100, - 'UID' => 101, - 'GID' => 102, - 'UNSPEC' => 103, - 'TSIG' => 250, - 'IXFR' => 251, - 'AXFR' => 252, - 'MAILB' => 253, - 'MAILA' => 254, - 'ANY' => 255, - ); - if (! strlen($rc[$rrtype])) { - $rc[$rrtype] = NULL; - } - return($rc[$rrtype]); + 'SIGZERO' => 0, // RFC2931 consider this a pseudo type + 'A' => 1, // RFC 1035, Section 3.4.1 + 'NS' => 2, // RFC 1035, Section 3.3.11 + 'MD' => 3, // RFC 1035, Section 3.3.4 (obsolete) + 'MF' => 4, // RFC 1035, Section 3.3.5 (obsolete) + 'CNAME' => 5, // RFC 1035, Section 3.3.1 + 'SOA' => 6, // RFC 1035, Section 3.3.13 + 'MB' => 7, // RFC 1035, Section 3.3.3 + 'MG' => 8, // RFC 1035, Section 3.3.6 + 'MR' => 9, // RFC 1035, Section 3.3.8 + 'NULL' => 10, // RFC 1035, Section 3.3.10 + 'WKS' => 11, // RFC 1035, Section 3.4.2 (deprecated) + 'PTR' => 12, // RFC 1035, Section 3.3.12 + 'HINFO' => 13, // RFC 1035, Section 3.3.2 + 'MINFO' => 14, // RFC 1035, Section 3.3.7 + 'MX' => 15, // RFC 1035, Section 3.3.9 + 'TXT' => 16, // RFC 1035, Section 3.3.14 + 'RP' => 17, // RFC 1183, Section 2.2 + 'AFSDB' => 18, // RFC 1183, Section 1 + 'X25' => 19, // RFC 1183, Section 3.1 + 'ISDN' => 20, // RFC 1183, Section 3.2 + 'RT' => 21, // RFC 1183, Section 3.3 + 'NSAP' => 22, // RFC 1706, Section 5 + 'NSAP_PTR' => 23, // RFC 1348 (obsolete) + // The following 2 RRs are impemented in Net::DNS::SEC + 'SIG' => 24, // RFC 2535, Section 4.1 + 'KEY' => 25, // RFC 2535, Section 3.1 + 'PX' => 26, // RFC 2163, + 'GPOS' => 27, // RFC 1712 (obsolete) + 'AAAA' => 28, // RFC 1886, Section 2.1 + 'LOC' => 29, // RFC 1876 + // The following RR is impemented in Net::DNS::SEC + 'NXT' => 30, // RFC 2535, Section 5.2 + 'EID' => 31, // draft-ietf-nimrod-dns-xx.txt + 'NIMLOC' => 32, // draft-ietf-nimrod-dns-xx.txt + 'SRV' => 33, // RFC 2052 + 'ATMA' => 34, // ??? + 'NAPTR' => 35, // RFC 2168 + 'KX' => 36, // RFC 2230 + 'CERT' => 37, // RFC 2538 + 'DNAME' => 39, // RFC 2672 + 'OPT' => 41, // RFC 2671 + // The following 4 RRs are impemented in Net::DNS::SEC + // Aug 2003: These RRs will be published as RFCs shortly + 'DS' => 43, // draft-ietf-dnsext-delegation-signer + 'SSHFP' => 44, // draft-ietf-secsh-dns (No RFC // yet at time of coding) + 'RRSIG' => 46, // draft-ietf-dnsext-dnssec-2535typecode-change + 'NSEC' => 47, // draft-ietf-dnsext-dnssec-2535typecode-change + 'DNSKEY' => 48, // draft-ietf-dnsext-dnssec-2535typecode-change + 'UINFO' => 100, // non-standard + 'UID' => 101, // non-standard + 'GID' => 102, // non-standard + 'UNSPEC' => 103, // non-standard + 'TKEY' => 249, // RFC 2930 + 'TSIG' => 250, // RFC 2931 + 'IXFR' => 251, // RFC 1995 + 'AXFR' => 252, // RFC 1035 + 'MAILB' => 253, // RFC 1035 (MB, MG, MR) + 'MAILA' => 254, // RFC 1035 (obsolete - see MX) + 'ANY' => 255 // RFC 1035 + ); + if (array_key_exists($rrtype, $rc)) { + return $rc[$rrtype]; + } else { + $matches= null; + preg_match('/^\s*TYPE(\d+)\s*$/', $rrtype, $matches); + if (is_array($matches) && (count($matches) == 2) && ($matches[1] < 256)) { + return $matches[1]; + } else { + return NULL; + } + } } /*}}}*/@@ -363,56 +373,82 @@function typesbyval($rrtypeval) { $rc = array( - 1 => 'A', - 2 => 'NS', - 3 => 'MD', - 4 => 'MF', - 5 => 'CNAME', - 6 => 'SOA', - 7 => 'MB', - 8 => 'MG', - 9 => 'MR', - 10 => 'NULL', - 11 => 'WKS', - 12 => 'PTR', - 13 => 'HINFO', - 14 => 'MINFO', - 15 => 'MX', - 16 => 'TXT', - 17 => 'RP', - 18 => 'AFSDB', - 19 => 'X25', - 20 => 'ISDN', - 21 => 'RT', - 22 => 'NSAP', - 23 => 'NSAP_PTR', - 24 => 'SIG', - 25 => 'KEY', - 26 => 'PX', - 27 => 'GPOS', - 28 => 'AAAA', - 29 => 'LOC', - 30 => 'NXT', - 31 => 'EID', - 32 => 'NIMLOC', - 33 => 'SRV', - 34 => 'ATMA', - 35 => 'NAPTR', - 100 => 'UINFO', - 101 => 'UID', - 102 => 'GID', - 103 => 'UNSPEC', - 250 => 'TSIG', - 251 => 'IXFR', - 252 => 'AXFR', - 253 => 'MAILB', - 254 => 'MAILA', - 255 => 'ANY', - ); - if (! strlen($rc[$rrtypeval])) { - $rc[$rrtypeval] = NULL; - } - return($rc[$rrtypeval]); + 0 => 'SIGZERO', // RFC2931 consider this a pseudo type + 1 => 'A', // RFC 1035, Section 3.4.1 + 2 => 'NS', // RFC 1035, Section 3.3.11 + 3 => 'MD', // RFC 1035, Section 3.3.4 (obsolete) + 4 => 'MF', // RFC 1035, Section 3.3.5 (obsolete) + 5 => 'CNAME', // RFC 1035, Section 3.3.1 + 6 => 'SOA', // RFC 1035, Section 3.3.13 + 7 => 'MB', // RFC 1035, Section 3.3.3 + 8 => 'MG', // RFC 1035, Section 3.3.6 + 9 => 'MR', // RFC 1035, Section 3.3.8 + 10 => 'NULL', // RFC 1035, Section 3.3.10 + 11 => 'WKS', // RFC 1035, Section 3.4.2 (deprecated) + 12 => 'PTR', // RFC 1035, Section 3.3.12 + 13 => 'HINFO', // RFC 1035, Section 3.3.2 + 14 => 'MINFO', // RFC 1035, Section 3.3.7 + 15 => 'MX', // RFC 1035, Section 3.3.9 + 16 => 'TXT', // RFC 1035, Section 3.3.14 + 17 => 'RP', // RFC 1183, Section 2.2 + 18 => 'AFSDB', // RFC 1183, Section 1 + 19 => 'X25', // RFC 1183, Section 3.1 + 20 => 'ISDN', // RFC 1183, Section 3.2 + 21 => 'RT', // RFC 1183, Section 3.3 + 22 => 'NSAP', // RFC 1706, Section 5 + 23 => 'NSAP_PTR', // RFC 1348 (obsolete) + // The following 2 RRs are impemented in Net::DNS::SEC + 24 => 'SIG', // RFC 2535, Section 4.1 + 25 => 'KEY', // RFC 2535, Section 3.1 + 26 => 'PX', // RFC 2163, + 27 => 'GPOS', // RFC 1712 (obsolete) + 28 => 'AAAA', // RFC 1886, Section 2.1 + 29 => 'LOC', // RFC 1876 + // The following RR is impemented in Net::DNS::SEC + 30 => 'NXT', // RFC 2535, Section 5.2 + 31 => 'EID', // draft-ietf-nimrod-dns-xx.txt + 32 => 'NIMLOC', // draft-ietf-nimrod-dns-xx.txt + 33 => 'SRV', // RFC 2052 + 34 => 'ATMA', // ??? + 35 => 'NAPTR', // RFC 2168 + 36 => 'KX', // RFC 2230 + 37 => 'CERT', // RFC 2538 + 39 => 'DNAME', // RFC 2672 + 41 => 'OPT', // RFC 2671 + // The following 4 RRs are impemented in Net::DNS::SEC + // Aug 2003: These RRs will be published as RFCs shortly + 43 => 'DS', // draft-ietf-dnsext-delegation-signer + 44 => 'SSHFP', // draft-ietf-secsh-dns (No RFC // yet at time of coding) + 46 => 'RRSIG', // draft-ietf-dnsext-dnssec-2535typecode-change + 47 => 'NSEC', // draft-ietf-dnsext-dnssec-2535typecode-change + 48 => 'DNSKEY', // draft-ietf-dnsext-dnssec-2535typecode-change + 100 => 'UINFO', // non-standard + 101 => 'UID', // non-standard + 102 => 'GID', // non-standard + 103 => 'UNSPEC', // non-standard + 249 => 'TKEY', // RFC 2930 + 250 => 'TSIG', // RFC 2931 + 251 => 'IXFR', // RFC 1995 + 252 => 'AXFR', // RFC 1035 + 253 => 'MAILB', // RFC 1035 (MB, MG, MR) + 254 => 'MAILA', // RFC 1035 (obsolete - see MX) + 255 => 'ANY' // RFC 1035 + ); + $matches= null; + preg_match('/^\s*\d+\s*$/', $rrtypeval, $matches); + if (is_array($matches) && (count($matches) == 1)) { + $rrtypeval= preg_replace('/\s*/', '', $matches[0]); + $rrtypeval= preg_replace('/^0*/', '', $rrtypeval); + if (array_key_exists($rrtypeval, $rc)) { + return $rc[$rrtypeval]; + } elseif ($rrtypeval < 256) { + return $rrtypeval; + } else { + return NULL; + } + } else { + return NULL; + } } /*}}}*/@@ -435,16 +471,13 @@function classesbyname($class) { $rc = array( - 'IN' => 1, - 'CH' => 3, - 'HS' => 4, - 'NONE' => 254, - 'ANY' => 255 + 'IN' => 1, // RFC 1035 + 'CH' => 3, // RFC 1035 + 'HS' => 4, // RFC 1035 + 'NONE' => 254, // RFC 2136 + 'ANY' => 255 // RFC 1035 ); - if (! isset($rc[$class])) { - $rc[$class] = NULL; - } - return($rc[$class]); + return (array_key_exists($class, $rc) ? $rc[$class] : NULL); } /*}}}*/@@ -467,10 +500,7 @@254 => 'NONE', 255 => 'ANY' ); - if (! strlen($rc[$classval])) { - $rc[$classval] = NULL; - } - return($rc[$classval]); + return (array_key_exists($classval, $rc) ? $rc[$classval] : NULL); } /*}}}*/