Пример #1
0
 /**
  * opens a socket connection to the DNS server
  *
  * @return boolean
  * @access public
  *
  */
 public function open()
 {
     //
     // create the socket
     //
     if (Net_DNS2::isIPv4($this->host) == true) {
         $this->sock = @socket_create(AF_INET, $this->type, $this->type == Net_DNS2_Socket::SOCK_STREAM ? SOL_TCP : SOL_UDP);
     } else {
         if (Net_DNS2::isIPv6($this->host) == true) {
             $this->sock = @socket_create(AF_INET6, $this->type, $this->type == Net_DNS2_Socket::SOCK_STREAM ? SOL_TCP : SOL_UDP);
         } else {
             $this->last_error = 'invalid address type: ' . $this->host;
             return false;
         }
     }
     if ($this->sock === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     }
     @socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
     //
     // bind to a local IP/port if it's set
     //
     if (strlen($this->local_host) > 0) {
         $result = @socket_bind($this->sock, $this->local_host, $this->local_port > 0 ? $this->local_port : null);
         if ($result === false) {
             $this->last_error = socket_strerror(socket_last_error());
             return false;
         }
     }
     //
     // mark the socket as non-blocking
     //
     if (@socket_set_nonblock($this->sock) === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     }
     //
     // connect to the socket; don't check for status here, we'll check it on the
     // socket_select() call so we can handle timeouts properly
     //
     @socket_connect($this->sock, $this->host, $this->port);
     $read = null;
     $write = array($this->sock);
     $except = null;
     //
     // select on write to check if the call to connect worked
     //
     $result = @socket_select($read, $write, $except, $this->timeout);
     if ($result === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     } else {
         if ($result == 0) {
             $this->last_error = 'timeout on write select for connect()';
             return false;
         }
     }
     return true;
 }
Пример #2
0
 /**
  * opens a socket connection to the DNS server
  *
  * @return boolean
  * @access public
  *
  */
 public function open()
 {
     //
     // create the socket
     //
     if (Net_DNS2::isIPv4($this->host) == true) {
         $this->_sock = @socket_create(AF_INET, $this->type, $this->type == SOCK_STREAM ? SOL_TCP : SOL_UDP);
     } else {
         if (Net_DNS2::isIPv6($this->host) == true) {
             $this->_sock = @socket_create(AF_INET6, $this->type, $this->type == SOCK_STREAM ? SOL_TCP : SOL_UDP);
         } else {
             $this->last_error = 'invalid address type: ' . $this->host;
             return false;
         }
     }
     if ($this->_sock === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     }
     @socket_set_option($this->_sock, SOL_SOCKET, SO_REUSEADDR, 1);
     //
     // bind to a local IP/port if it's set
     //
     if (strlen($this->local_host) > 0) {
         $result = @socket_bind($this->_sock, $this->local_host, $this->local_port > 0 ? $this->local_port : null);
         if ($result === false) {
             $this->last_error = socket_strerror(socket_last_error());
             return false;
         }
     }
     //
     // connect to the socket
     //
     if (@socket_connect($this->_sock, $this->host, $this->port) === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     }
     //
     // mark the socket as non-blocking
     //
     if (@socket_set_nonblock($this->_sock) === false) {
         $this->last_error = socket_strerror(socket_last_error());
         return false;
     }
     return true;
 }
Пример #3
0
 /**
  * Constructor - builds a new Net_DNS2_Updater objected used for doing 
  * dynamic DNS updates
  *
  * @param string $zone    the domain name to use for DNS updates
  * @param mixed  $options an array of config options or null
  *
  * @throws Net_DNS2_Exception
  * @access public
  *
  */
 public function __construct($zone, array $options = null)
 {
     parent::__construct($options);
     //
     // create the packet
     //
     $this->_packet = new Net_DNS2_Packet_Request(strtolower(trim($zone, " \n\r\t.")), 'SOA', 'IN');
     //
     // make sure the opcode on the packet is set to UPDATE
     //
     $this->_packet->header->opcode = Net_DNS2_Lookups::OPCODE_UPDATE;
 }
Пример #4
0
 /**
  * parses the rdata portion from a standard DNS config line
  *
  * @param array $rdata a string split line of values for the rdata
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrFromString(array $rdata)
 {
     //
     // load the data
     //
     $precedence = array_shift($rdata);
     $gateway_type = array_shift($rdata);
     $algorithm = array_shift($rdata);
     $gateway = strtolower(trim(array_shift($rdata)));
     $key = array_shift($rdata);
     //
     // validate it
     //
     switch ($gateway_type) {
         case self::GATEWAY_TYPE_NONE:
             $gateway = '';
             break;
         case self::GATEWAY_TYPE_IPV4:
             if (Net_DNS2::isIPv4($gateway) == false) {
                 return false;
             }
             break;
         case self::GATEWAY_TYPE_IPV6:
             if (Net_DNS2::isIPv6($gateway) == false) {
                 return false;
             }
             break;
         case self::GATEWAY_TYPE_DOMAIN:
             // do nothing
             break;
         default:
             return false;
     }
     //
     // check the algorithm and key
     //
     switch ($algorithm) {
         case self::ALGORITHM_NONE:
             $key = '';
             break;
         case self::ALGORITHM_DSA:
         case self::ALGORITHM_RSA:
             // do nothing
             break;
         default:
             return false;
     }
     //
     // store the values
     //
     $this->precedence = $precedence;
     $this->gateway_type = $gateway_type;
     $this->algorithm = $algorithm;
     $this->gateway = $gateway;
     $this->key = $key;
     return true;
 }
Пример #5
0
 /**
  * parses the rdata of the Net_DNS2_Packet object
  *
  * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrSet(Net_DNS2_Packet &$packet)
 {
     if ($this->rdlength > 0) {
         //
         // parse the
         //
         $offset = $packet->offset;
         $this->mname = Net_DNS2_Packet::expand($packet, $offset);
         $this->rname = Net_DNS2_Packet::expand($packet, $offset);
         //
         // get the SOA values
         //
         $x = unpack('@' . $offset . '/Nserial/Nrefresh/Nretry/Nexpire/Nminimum/', $packet->rdata);
         $this->serial = Net_DNS2::expandUint32($x['serial']);
         $this->refresh = Net_DNS2::expandUint32($x['refresh']);
         $this->retry = Net_DNS2::expandUint32($x['retry']);
         $this->expire = Net_DNS2::expandUint32($x['expire']);
         $this->minimum = Net_DNS2::expandUint32($x['minimum']);
         return true;
     }
     return false;
 }
Пример #6
0
 /**
  * parses the rdata of the Net_DNS2_Packet object
  *
  * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrSet(Net_DNS2_Packet &$packet)
 {
     if ($this->rdlength > 0) {
         //
         // unpack
         //
         $x = unpack('ntc/Calgorithm/Clabels/Norigttl/Nsigexp/Nsigincep/nkeytag', $this->rdata);
         $this->typecovered = Net_DNS2_Lookups::$rr_types_by_id[$x['tc']];
         $this->algorithm = $x['algorithm'];
         $this->labels = $x['labels'];
         $this->origttl = Net_DNS2::expandUint32($x['origttl']);
         //
         // the dates are in GM time
         //
         $this->sigexp = gmdate('YmdHis', $x['sigexp']);
         $this->sigincep = gmdate('YmdHis', $x['sigincep']);
         //
         // get the keytag
         //
         $this->keytag = $x['keytag'];
         //
         // get teh signers name and signature
         //
         $offset = $packet->offset + 18;
         $sigoffset = $offset;
         $this->signname = strtolower(Net_DNS2_Packet::expand($packet, $sigoffset));
         $this->signature = base64_encode(substr($this->rdata, 18 + ($sigoffset - $offset)));
         return true;
     }
     return false;
 }
Пример #7
0
    /**
     * parses the rdata of the Net_DNS2_Packet object
     *
     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
     *
     * @return boolean
     * @access protected
     *
     */
    protected function rrSet(Net_DNS2_Packet &$packet)
    {
        if ($this->rdlength > 0) {
        
            //
            // expand the algorithm
            //
            $offset = $packet->offset;
            $this->algorithm = Net_DNS2_Packet::expand($packet, $offset);
            
            //
            // unpack inception, expiration, mode, error and key size
            //
            $x = unpack(
                '@' . $offset . '/Ninception/Nexpiration/nmode/nerror/nkey_size', 
                $packet->rdata
            );

            $this->inception    = Net_DNS2::expandUint32($x['inception']);
            $this->expiration   = Net_DNS2::expandUint32($x['expiration']);
            $this->mode         = $x['mode'];
            $this->error        = $x['error'];
            $this->key_size     = $x['key_size'];

            $offset += 14;

            //
            // if key_size > 0, then copy out the key
            //
            if ($this->key_size > 0) {

                $this->key_data = substr($packet->rdata, $offset, $this->key_size);
                $offset += $this->key_size;
            }

            //
            // unpack the other length
            //
            $x = unpack('@' . $offset . '/nother_size', $packet->rdata);
            
            $this->other_size = $x['other_size'];
            $offset += 2;

            //
            // if other_size > 0, then copy out the data
            //
            if ($this->other_size > 0) {

                $this->other_data = substr(
                    $packet->rdata, $offset, $this->other_size
                );
            }

            return true;
        }

        return false;
    }
Пример #8
0
 /**
  * formats an IPv6 IP address in the preferred format
  *
  * @param string $address The IPv6 IP address to format
  *
  * @return string The IPv6 IP address formatted in the new format
  * @access public
  * @deprecated function deprecated in 1.1.3
  *
  */
 public static function formatIPv6($address)
 {
     return Net_DNS2::expandIPv6($address);
 }
Пример #9
0
 /**
  * opens a socket connection to the DNS server
  *
  * @return boolean
  * @access public
  *
  */
 public function open()
 {
     //
     // create a list of options for the context
     //
     $opts = array('socket' => array());
     //
     // bind to a local IP/port if it's set
     //
     if (strlen($this->local_host) > 0) {
         $opts['socket']['bindto'] = $this->local_host;
         if ($this->local_port > 0) {
             $opts['socket']['bindto'] .= ':' . $this->local_port;
         }
     }
     //
     // create the context
     //
     $this->_context = @stream_context_create($opts);
     //
     // create socket
     //
     $errno;
     $errstr;
     switch ($this->type) {
         case Net_DNS2_Socket::SOCK_STREAM:
             if (Net_DNS2::isIPv4($this->host) == true) {
                 $this->sock = @stream_socket_client('tcp://' . $this->host . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context);
             } else {
                 if (Net_DNS2::isIPv6($this->host) == true) {
                     $this->sock = @stream_socket_client('tcp://[' . $this->host . ']:' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context);
                 } else {
                     $this->last_error = 'invalid address type: ' . $this->host;
                     return false;
                 }
             }
             break;
         case Net_DNS2_Socket::SOCK_DGRAM:
             if (Net_DNS2::isIPv4($this->host) == true) {
                 $this->sock = @stream_socket_client('udp://' . $this->host . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context);
             } else {
                 if (Net_DNS2::isIPv6($this->host) == true) {
                     $this->sock = @stream_socket_client('udp://[' . $this->host . ']:' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context);
                 } else {
                     $this->last_error = 'invalid address type: ' . $this->host;
                     return false;
                 }
             }
             break;
         default:
             $this->last_error = 'Invalid socket type: ' . $this->type;
             return false;
     }
     if ($this->sock === false) {
         $this->last_error = $errstr;
         return false;
     }
     //
     // set it to non-blocking and set the timeout
     //
     @stream_set_blocking($this->sock, 0);
     @stream_set_timeout($this->sock, $this->timeout);
     return true;
 }
Пример #10
0
 /**
  * builds a new Net_DNS2_Packet_Request object
  *
  * @param string $name  the domain name for the packet
  * @param string $type  the DNS RR type for the packet
  * @param string $class the DNS class for the packet
  *
  * @return boolean
  * @throws Net_DNS2_Exception
  * @access public
  *
  */
 public function set($name, $type = 'A', $class = 'IN')
 {
     //
     // generate a new header
     //
     $this->header = new Net_DNS2_Header();
     //
     // add a new question
     //
     $q = new Net_DNS2_Question();
     //
     // allow queries directly to . for the root name servers
     //
     if ($name != '.') {
         $name = trim(strtolower($name), " \t\n\r\v.");
     }
     $type = strtoupper(trim($type));
     $class = strtoupper(trim($class));
     //
     // check that the input string has some data in it
     //
     if (empty($name)) {
         throw new Net_DNS2_Exception('empty query string provided', Net_DNS2_Lookups::E_PACKET_INVALID);
     }
     //
     // if the type is "*", rename it to "ANY"- both are acceptable.
     //
     if ($type == '*') {
         $type = 'ANY';
     }
     //
     // check that the type and class are valid
     //
     if (!isset(Net_DNS2_Lookups::$rr_types_by_name[$type]) || !isset(Net_DNS2_Lookups::$classes_by_name[$class])) {
         throw new Net_DNS2_Exception('invalid type (' . $type . ') or class (' . $class . ') specified.', Net_DNS2_Lookups::E_PACKET_INVALID);
     }
     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) {
             //
             // IPv4
             //
             $name = implode('.', array_reverse(explode('.', $name)));
             $name .= '.in-addr.arpa';
         } else {
             if (Net_DNS2::isIPv6($name) == true) {
                 //
                 // IPv6
                 //
                 $e = Net_DNS2::expandIPv6($name);
                 if ($e !== false) {
                     $name = implode('.', array_reverse(str_split(str_replace(':', '', $e))));
                     $name .= '.ip6.arpa';
                 } else {
                     throw new Net_DNS2_Exception('unsupported PTR value: ' . $name, Net_DNS2_Lookups::E_PACKET_INVALID);
                 }
             }
         }
     }
     //
     // store the data
     //
     $q->qname = $name;
     $q->qtype = $type;
     $q->qclass = $class;
     $this->question[] = $q;
     //
     // the answer, authority and additional are empty; they can be modified
     // after the request is created for UPDATE requests if needed.
     //
     $this->answer = array();
     $this->authority = array();
     $this->additional = array();
     return true;
 }
Пример #11
0
 /**
  * parses the rdata of the Net_DNS2_Packet object
  *
  * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrSet(Net_DNS2_Packet &$packet)
 {
     if ($this->rdlength > 0) {
         //
         // expand the algorithm
         //
         $newoffset = $packet->offset;
         $this->algorithm = Net_DNS2_Packet::expand($packet, $newoffset);
         $offset = $newoffset - $packet->offset;
         //
         // unpack time, fudge and mac_size
         //
         $x = unpack('@' . $offset . '/ntime_high/Ntime_low/nfudge/nmac_size', $this->rdata);
         $this->time_signed = Net_DNS2::expandUint32($x['time_low']);
         $this->fudge = $x['fudge'];
         $this->mac_size = $x['mac_size'];
         $offset += 10;
         //
         // copy out the mac
         //
         if ($this->mac_size > 0) {
             $this->mac = substr($this->rdata, $offset, $this->mac_size);
             $offset += $this->mac_size;
         }
         //
         // unpack the original id, error, and other_length values
         //
         $x = unpack('@' . $offset . '/noriginal_id/nerror/nother_length', $this->rdata);
         $this->original_id = $x['original_id'];
         $this->error = $x['error'];
         $this->other_length = $x['other_length'];
         //
         // the only time there is actually any "other data", is when there's
         // a BADTIME error code.
         //
         // The other length should be 6, and the other data field includes the
         // servers current time - per RFC 2845 section 4.5.2
         //
         if ($this->error == Net_DNS2_Lookups::RCODE_BADTIME) {
             if ($this->other_length != 6) {
                 return false;
             }
             //
             // other data is a 48bit timestamp
             //
             $x = unpack('nhigh/nlow', substr($this->rdata, $offset + 6, $this->other_length));
             $this->other_data = $x['low'];
         }
         return true;
     }
     return false;
 }
Пример #12
0
 /**
  * Constructor - creates a new Net_DNS2_Resolver object
  *
  * @param mixed $options either an array with options or null
  *
  * @access public
  *
  */
 public function __construct(array $options = null)
 {
     parent::__construct($options);
 }
Пример #13
0
 /**
  * parses the rdata portion from a standard DNS config line
  *
  * @param array $rdata a string split line of values for the rdata
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrFromString(array $rdata)
 {
     $value = array_shift($rdata);
     if (Net_DNS2::isIPv4($value) == true) {
         $this->address = $value;
         return true;
     }
     return false;
 }
Пример #14
0
 /**
  * parses the rdata portion from a standard DNS config line
  *
  * @param array $rdata a string split line of values for the rdata
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrFromString(array $rdata)
 {
     //
     // expand out compressed formats
     //
     $value = array_shift($rdata);
     if (Net_DNS2::isIPv6($value) == true) {
         $this->address = $value;
         return true;
     }
     return false;
 }
Пример #15
0
 /**
  * parses the rdata of the Net_DNS2_Packet object
  *
  * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  *
  * @return boolean
  * @access protected
  *
  */
 protected function rrSet(Net_DNS2_Packet &$packet)
 {
     if ($this->rdlength > 0) {
         //
         // unpack the serial and flags values
         //
         $x = unpack('@' . $packet->offset . '/Nserial/nflags', $packet->rdata);
         $this->serial = Net_DNS2::expandUint32($x['serial']);
         $this->flags = $x['flags'];
         //
         // parse out the RR bitmap
         //
         $this->type_bit_maps = Net_DNS2_BitMap::bitMapToArray(substr($this->rdata, 6));
         return true;
     }
     return false;
 }