Source for file Resolver.php
Documentation is available at Resolver.php
* Net_DNS: A resolver library for PHP
* Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/* Net_DNS_Resolver object definition {{{ */
* Resolver library. Builds a DNS query packet, sends the packet to the
* server and parses the reponse.
/* class variable definitions {{{ */
* An array of all nameservers to query
* @var array $nameservers
* The UDP port to use for the query (default = 53)
* The domain in which the resolver client host resides.
* The searchlist to apply to unqualified hosts
* An array of strings containg domains to apply to unqualified hosts
* passed to the resolver.
* The number of seconds between retransmission of unaswered queries
* The number of times unanswered requests should be retried
* Whether or not to use TCP (Virtual Circuits) instead of UDP
* If set to 0, UDP will be used unless TCP is required. TCP is
* required for questions or responses greater than 512 bytes.
* Ignore TC (truncated) bit
* If the server responds with the TC bit set on a response, and $igntc
* is set to 0, the resolver will automatically retransmit the request
* using virtual circuits (TCP).
* Sets the value of the RD (recursion desired) bit in the header. If
* the RD bit is set to 0, the server will not perform recursion on the
* Contains the value of the last error returned by the resolver.
* @var string $errorstring
* The origin of the packet.
* This contains a string containing the IP address of the name server
* from which the answer was given.
* @var string $answerfrom
* The size of the answer packet.
* This contains a integer containing the size of the DNS packet the
* @var string $answersize
* The number of seconds after which a TCP connection should timeout
* @var integer $tcp_timeout
* The location of the system resolv.conf file.
* @var string $resolv_conf
* The name of the user defined resolv.conf
* The resolver will attempt to look in both the current directory as
* well as the user's home directory for a user defined resolver
* @see Net_DNS_Resolver::$confpath
* A array of directories to search for the user's resolver config
* @see Net_DNS_Resolver::$dotfile
* If set to true (non-zero), debugging code will be displayed as the
* resolver makes the request.
* use the (currently) experimental PHP socket library
* If set to true (non-zero), the Resolver will attempt to use the
* much more effecient PHP sockets extension (if available).
* @var boolean $useEnhancedSockets;
* An array of sockets connected to a name servers
* Used to store a PHP socket resource for a connection to a server
* @var resource $_axfr_sock;
* axfr resource record list
* Used to store a resource record list from a zone transfer
* @var resource $_axfr_rr;
* Used to store the number of soa records received from a zone transfer
* @var resource $_axfr_soa_count;
/* class constructor - Net_DNS_Resolver() {{{ */
* Initializes the Resolver Object
* @return Net_DNS_Resolver
'nameservers' => array (),
'errorstring' => 'unknown error or no error',
foreach ($mydefaults as $k => $v) {
$this->{$k} = isset ($defaults[$k]) ? $defaults[$k] : $v;
$this->confpath [0 ] = getenv ('HOME');
$this->confpath [1 ] = '.';
/* Net_DNS_Resolver::res_init() {{{ */
* Initalizes the resolver library
* res_init() searches for resolver library configuration files and
* initializes the various properties of the resolver object.
* @see Net_DNS_Resolver::$resolv_conf, Net_DNS_Resolver::$dotfile,
* Net_DNS_Resolver::$confpath, Net_DNS_Resolver::$searchlist,
* Net_DNS_Resolver::$domain, Net_DNS_Resolver::$nameservers
$file = $dir.DIRECTORY_SEPARATOR. $this->dotfile;
/* Net_DNS_Resolver::read_config {{{ */
* Reads and parses a resolver configuration file
* @param string $file The name of the file to open and parse
if (! ($f = fopen($file, 'r'))) {
$this->error = " can't open $file";
$this->error = " can't open $file";
if (ereg("^[ \t]*$", $line, $regs)) {
ereg("^[ \t]*([^ \t]+)[ \t]+([^ \t]+)", $line, $regs);
foreach (split(' ', $regs[2 ]) as $ns) {
/* Net_DNS_Resolver::read_env() {{{ */
* Examines the environment for resolver config information
if (getenv('RES_NAMESERVERS')) {
if (getenv('RES_SEARCHLIST')) {
list ($name, $val) = split(':', $opt);
/* Net_DNS_Resolver::string() {{{ */
* Builds a string containing the current state of the resolver
* Builds formatted string containing the state of the resolver library suited
$state = ";; Net_DNS_Resolver state:\n";
$state .= ';; domain = ' . $this->domain . "\n";
$state .= ';; port = ' . $this->port . "\n";
$state .= ';; tcp_timeout = ';
$state .= ';; retrans = ' . $this->retrans . ' ';
$state .= 'retry = ' . $this->retry . "\n";
$state .= ';; usevc = ' . $this->usevc . ' ';
$state .= 'stayopen = ' . $this->stayopen . ' ';
$state .= 'igntc = ' . $this->igntc . "\n";
$state .= ';; defnames = ' . $this->defnames . ' ';
$state .= 'dnsrch = ' . $this->dnsrch . "\n";
$state .= ';; recurse = ' . $this->recurse . ' ';
$state .= 'debug = ' . $this->debug . "\n";
/* Net_DNS_Resolver::nextid() {{{ */
* Returns the next request Id to be used for the DNS packet header
if ($GLOBALS['_Net_DNS_packet_id']++ > 65535 ) {
$GLOBALS['_Net_DNS_packet_id']= 1;
return $GLOBALS['_Net_DNS_packet_id'];
/* Net_DNS_Resolver::nameservers() {{{ */
* 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
* the default settings of a new Net_DNS_Resolver.
$a[] = ($ns == 0 ) ? '0.0.0.0' : $ns;
$names[] = $ns . '.' . $suffix;
} elseif (!empty ($defres->domain)) {
$names[] = $ns . '.'. $defres->domain;
$packet = $defres->search($ns);
foreach ($addresses as $b) {
/* not tested -- Net_DNS_Resolver::cname_addr() {{{ */
//my $oct2 = '(?:2[0-4]\d|25[0-5]|[0-1]?\d\d|\d)';
foreach ($packet->answer as $rr) {
if ($rr->type == 'CNAME') {
} elseif ($rr->type == 'A') {
// Run a basic taint check.
//next RR unless $rr->address =~ m/^($oct2\.$oct2\.$oct2\.$oct2)$/o;
/* Net_DNS_Resolver::search() {{{ */
* Searches nameservers for an answer
* Goes through the search list and attempts to resolve name based on
* the information in the search list.
* @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 on success,
* @see Net_DNS::typesbyname(), Net_DNS::classesbyname()
function search($name, $type = 'A', $class = 'IN')
* If the name looks like an IP address then do an appropriate
if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) {
$name = $regs[4 ]. '.'. $regs[3 ]. '.'. $regs[2 ]. '.'. $regs[1 ]. '.in-addr.arpa.';
* If the name contains at least one dot then try it as is first.
echo " ;; search($name, $type, $class)\n";
$ans = $this->query($name, $type, $class);
* If the name does not end in a dot then apply the search list.
$newname = " $name.$domain";
echo " ;; search($newname, $type, $class)\n";
$ans = $this->query($newname, $type, $class);
* Finally, if the name has no dots then try it as is.
if (strpos($name, '.') === false ) {
echo " ;; search($name, $type, $class)\n";
$ans = $this->query($name. '.', $type, $class);
/* Net_DNS_Resolver::rawQuery() {{{ */
* Queries nameservers for an answer
* 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.
* @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, regardless of whether the packet
* @see Net_DNS::typesbyname(), Net_DNS::classesbyname()
function rawQuery($name, $type = 'A', $class = 'IN')
* If the name does not contain any dots then append the default domain.
* If the name looks like an IP address then do an appropriate
if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) {
$name = $regs[4 ]. '.'. $regs[3 ]. '.'. $regs[2 ]. '.'. $regs[1 ]. '.in-addr.arpa.';
echo " ;; query($name, $type, $class)\n";
$packet->buildQuestion ($name, $type, $class);
$ans = $this->send($packet);
/* Net_DNS_Resolver::query() {{{ */
* Queries nameservers for an answer
* 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.
* @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 on success,
* @see Net_DNS::typesbyname(), Net_DNS::classesbyname()
function query($name, $type = 'A', $class = 'IN')
$ans = $this->rawQuery($name, $type, $class);
/* Net_DNS_Resolver::send($packetORname, $qtype = '', $qclass = '') {{{ */
* Sends a packet to a nameserver
* Determines the appropriate communication method (UDP or TCP) and
* sends a DNS packet to a nameserver. Use of the this function
* directly is discouraged. $packetORname should always be a properly
* formatted binary DNS packet. However, it is possible to send a
* query here and bypass Net_DNS_Resolver::query()
* @param string $packetORname A binary DNS packet stream or a
* @param string $qtype This should not be used
* @param string $qclass This should not be used
* @return object Net_DNS_Packet An answer packet object
function send($packetORname, $qtype = '', $qclass = '')
$packet_data = $packet->data ();
$ans = $this->send_tcp($packet, $packet_data);
$ans = $this->send_udp($packet, $packet_data);
echo ";;\n;; packet truncated: retrying using TCP\n";
$ans = $this->send_tcp($packet, $packet_data);
/* Net_DNS_Resolver::printhex($packet_data) {{{ */
* Prints packet data as hex code.
while ($start < strlen($data)) {
for ($ctr = $start; $ctr < $start+16; $ctr++ ) {
for ($ctr = $start; $ctr < $start+16; $ctr++ ) {
if (ord($data[$ctr]) < 32 || ord($data[$ctr]) > 127 ) {
/* Net_DNS_Resolver::send_tcp($packet, $packet_data) {{{ */
* Sends a packet via TCP to the list of name servers.
* @param string $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_udp(), Net_DNS_Resolver::send()
function send_tcp($packet, $packet_data)
echo ";; ERROR: send_tcp: no nameservers\n";
echo " ;; send_tcp($ns:$dstport)\n";
$sock_key = " $ns:$dstport";
if (isset ($this->sockets [$sock_key]) && is_resource($this->sockets [$sock_key])) {
$sock = &$this->sockets [$sock_key];
if (! ($sock = @fsockopen($ns, $dstport, $errno,
echo " ;; ERROR: send_tcp: connection failed: $errstr\n";
$this->sockets [$sock_key] = $sock;
$sock = &$this->sockets [$sock_key];
echo ';; sending ' . strlen($packet_data) . " bytes\n";
if (($sent = fwrite($sock, $lenmsg)) == -1 ) {
echo ";; ERROR: send_tcp: length send failed\n";
if (($sent = fwrite($sock, $packet_data)) == -1 ) {
echo ";; ERROR: send_tcp: packet data send failed\n";
/* If $buf is empty, we want to supress errors
long enough to reach the continue; down the line */
$buf = fread($sock, $len);
echo " ;; received $actual bytes\n";
$this->errorstring = " expected $len bytes, received $buf";
/* Net_DNS_Resolver::send_udp_no_sock_lib($packet, $packet_data) {{{ */
* Sends a packet via UDP to the list of name servers.
* This function sends a packet to a nameserver. It is called by
* send_udp if the sockets PHP extension is not compiled into PHP.
* @param string $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_tcp(), Net_DNS_Resolver::send(),
* Net_DNS_Resolver::send_udp(), Net_DNS_Resolver::send_udp_with_sock_lib()
* PHP doesn't have excellent socket support as of this writing.
* This needs to be rewritten when PHP POSIX socket support is
* Obviously, this code is MUCH different than the PERL implementation
// Create a socket handle for each nameserver
if ($sock[$ctr++ ] = fsockopen(" udp://$nameserver" , $this->port)) {
$peerhost[$ctr-1 ] = $nameserver;
$peerport[$ctr-1 ] = $this->port;
for ($i = 0; $i < $this->retry; $i++ , $retrans *= 2 ,
$timeout = (int) ($retrans / $ctr)) {
foreach ($sock as $k => $s) {
echo ';; send_udp(' . $peerhost[$k] . ':' . $peerport[$k] . '): sending ' . strlen($packet_data) . " bytes\n";
if (! fwrite($s, $packet_data)) {
* Here's where it get's really nasty. We don't have a select()
* function here, so we have to poll for a response... UGH!
* let's sleep for a few hundred microseconds to let the
* data come in from the network...
if ($buf = fread($s, 512 )) {
echo ';; answer from ' . $peerhost[$k] . ':' .
$peerport[$k] . ': ' . strlen($buf) . " bytes\n";
if ($ans->header->qr != '1') {
// Sleep another 1/100th of a second... this sucks...
/* Net_DNS_Resolver::send_udp_with_sock_lib($packet, $packet_data) {{{ */
* Sends a packet via UDP to the list of name servers.
* This function sends a packet to a nameserver. It is called by
* send_udp if the sockets PHP extension is compiled into PHP.
* @param string $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_tcp(), Net_DNS_Resolver::send(),
* Net_DNS_Resolver::send_udp(), Net_DNS_Resolver::send_udp_no_sock_lib()
//$w = error_reporting(0);
// Create a socket handle for each nameserver
if ((($sock[$ctr++ ] = socket_create(AF_INET , SOCK_DGRAM , SOL_UDP ))) &&
$peerhost[$ctr-1 ] = $nameserver;
$peerport[$ctr-1 ] = $this->port;
// Try each nameserver up to $this->retry times
for ($i = 0; $i < $this->retry; $i++ ) {
// Set the timeout for each retry based on the number of
// nameservers there is a connected socket for.
$timeout = (int) ($retrans / $ctr);
// Make sure the timeout is at least 1 second
foreach ($sock as $k => $s) {
echo "\n;; send_udp(" . $peerhost[$k] . ':' . $peerport[$k] . '): sending ' . strlen($packet_data) . " bytes\n";
echo " ;; timeout set to $timeout seconds\n";
// Test to see if the connection was refused. Linux servers will send
// an ICMP message which will cause the client's next system call to
// return ECONNREFUSED if the server is not listening on the ip:port queried
// Unix socket connection was refused
echo ';; connection to ' . $peerhost[$k] . ':' . $peerport[$k] . " was refused\n";
// No data could be read from socket
echo ';; no data could be read from ' . $peerhost[$k] . ':' . $peerport[$k] . "\n";
// Reset the non-specific socket error status
echo ';; answer from ' . $peerhost[$k] . ':' .
$peerport[$k] . ': ' . strlen($buf) . " bytes\n";
if ($ans->header->qr != '1') {
// Ignore packet if it is not a response
// Ignore packet if the response id does not match the query id
// Return the DNS response packet
} elseif ($this->debug) {
echo ";; query to ". $peerhost[$k] . ':' . $peerport[$k] . " timed out\n";
/* Net_DNS_Resolver::send_udp($packet, $packet_data) {{{ */
* Sends a packet via UDP to the list of name servers.
* This function sends a packet to a nameserver. send_udp calls
* either Net_DNS_Resolver::send_udp_no_sock_lib() or
* Net_DNS_Resolver::send_udp_with_sock_lib() depending on whether or
* not the sockets extension is compiled into PHP. Note that using the
* sockets extension is MUCH more efficient.
* @param object Net_DNS_Packet $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_tcp(), Net_DNS_Resolver::send(),
* Net_DNS_Resolver::send_udp(), Net_DNS_Resolver::send_udp_no_sock_lib()
function send_udp($packet, $packet_data)
echo "\n;; using extended PHP sockets\n";
echo "\n;; using simple sockets\n";
/* Net_DNS_Resolver::make_query_packet($packetORname, $type = '', $class = '') {{{ */
* If the name looks like an IP address then do an appropriate
if (preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $name, $regs)) {
$name = $regs[4 ]. '.'. $regs[3 ]. '.'. $regs[2 ]. '.'. $regs[1 ]. '.in-addr.arpa.';
echo " ;; query($name, $type, $class)\n";
$packet->buildQuestion ($name, $type, $class);
/* Net_DNS_Resolver::axfr_old($dname, $class = 'IN') {{{ */
* Performs an AXFR query (zone transfer) (OLD BUGGY STYLE)
* This is deprecated and should not be used!
* @param string $dname The domain (zone) to transfer
* @param string $class The class in which to look for the zone.
* @return object Net_DNS_Packet
function axfr_old($dname, $class = 'IN')
return $this->axfr($dname, $class, true );
/* Net_DNS_Resolver::axfr($dname, $class = 'IN', $old = false) {{{ */
* Performs an AXFR query (zone transfer)
* Requests a zone transfer from the nameservers. Note that zone
* transfers will ALWAYS use TCP regardless of the setting of the
* Net_DNS_Resolver::$usevc flag. If $old is set to true, Net_DNS requires
* a nameserver that supports the many-answers style transfer format. Large
* zone transfers will not function properly. Setting $old to true is _NOT_
* recommended and should only be used for backwards compatibility.
* @param string $dname The domain (zone) to transfer
* @param string $class The class in which to look for the zone.
* @param boolean $old Requires 'old' style many-answer format to function.
Used for backwards compatibility only.
* @return object Net_DNS_Packet
function axfr($dname, $class = 'IN', $old = false )
echo " ;; axfr_start($dname, $class)\n";
echo ";; ERROR: no nameservers\n";
$packet_data = $packet->data ();
$ans = $this->send_tcp($packet, $packet_data);
while (($ans = $this->axfr_next()) !== null ) {
/* Net_DNS_Resolver::axfr_start($dname, $class = 'IN') {{{ */
* Sends a packet via TCP to the list of name servers.
* @param string $packet A packet object to send to the NS list
* @param string $packet_data The data in the packet as returned by
* the Net_DNS_Packet::data() method
* @return object Net_DNS_Packet Returns an answer packet object
* @see Net_DNS_Resolver::send_tcp()
echo " ;; axfr_start($dname, $class)\n";
echo ";; ERROR: axfr_start: no nameservers\n";
$packet_data = $packet->data ();
echo " ;; axfr_start($ns:$dstport)\n";
$sock_key = " $ns:$dstport";
$sock = &$this->sockets [$sock_key];
if (! ($sock = fsockopen($ns, $dstport, $errno,
echo " ;; ERROR: axfr_start: connection failed: $errstr\n";
$this->sockets [$sock_key] = $sock;
$sock = &$this->sockets [$sock_key];
echo ";; sending " . strlen($packet_data) . " bytes\n";
if (($sent = fwrite($sock, $lenmsg)) == -1 ) {
echo ";; ERROR: axfr_start: length send failed\n";
if (($sent = fwrite($sock, $packet_data)) == -1 ) {
echo ";; ERROR: axfr_start: packet data send failed\n";
$this->_axfr_sock = $sock;
$this->_axfr_rr = array ();
$this->_axfr_soa_count = 0;
/* Net_DNS_Resolver::axfr_next() {{{ */
* Requests the next RR from a existing transfer started with axfr_start
* @return object Net_DNS_RR Returns a Net_DNS_RR object of the next RR
* @see Net_DNS_Resolver::send_tcp()
if (! count($this->_axfr_rr )) {
if (! isset ($this->_axfr_sock ) || ! is_resource($this->_axfr_sock )) {
echo ';; received ' . strlen($buf) . "bytes\n";
echo ';; ' . $err . "\n";
if (! $ans->parse ($buf)) {
$this->errorstring = 'unknown error during packet parsing';
if ($ans->header->ancount < 1 ) {
if ($ans->header->rcode != 'NOERROR') {
foreach ($ans->answer as $rr) {
if ($rr->type == 'SOA') {
if (++ $this->_axfr_soa_count < 2 ) {
if ($this->_axfr_soa_count >= 2 ) {
unset ($this->_axfr_sock );
/* Net_DNS_Resolver::read_tcp() {{{ */
* Unknown - not ported yet
function read_tcp($sock, $nbytes, $debug = 0 )
while (strlen($buf) < $nbytes) {
$nread = $nbytes - strlen($buf);
echo " ;; read_tcp: expecting $nread bytes\n";
$read_buf = fread($sock, $nread);
echo ";; ERROR: read_tcp: fread failed\n";
echo ';; read_tcp: received ' . strlen($read_buf) . " bytes\n";
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
Documentation generated on Mon, 11 Mar 2019 14:47:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|