resolver.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,337 行 · 第 1/3 页

PHP
1,337
字号
<?php/* *  License Information: * *    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 {{{ *//** * A DNS Resolver library * * Resolver library.  Builds a DNS query packet, sends  the packet to the * server and parses the reponse. * * @package Net_DNS */class Net_DNS_Resolver{    /* class variable definitions {{{ */    /**     * An array of all nameservers to query     *     * An array of all nameservers to query     *     * @var array   $nameservers     * @access public     */    var $nameservers;    /**     * The UDP port to use for the query (default = 53)     *     * The UDP port to use for the query (default = 53)     *     * @var integer $port     * @access public     */    var $port;    /**     * The domain in which the resolver client host resides.     *     * The domain in which the resolver client host resides.     *     * @var string $domain     * @access public     */    var $domain;    /**     * The searchlist to apply to unqualified hosts     *     * An array of strings containg domains to apply to unqualified hosts     * passed to the resolver.     *     * @var array $searchlist     * @access public     */    var $searchlist;    /**     * The number of seconds between retransmission of unaswered queries     *     * The number of seconds between retransmission of unaswered queries     *     * @var integer $retrans     * @access public     */    var $retrans;    /**     * The number of times unanswered requests should be retried     *     * The number of times unanswered requests should be retried     *     * @var integer $retry     * @access public     */    var $retry;    /**     * 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.     *     * @var boolean $usevc     * @access public     */    var $usevc;    /**     * Unknown     */    var $stayopen;    /**     * 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).     *     * @access public     * @var boolean $igntc     */    var $igntc;    /**     * Recursion Desired     *     * 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     * request.     *     * @var boolean $recurse     * @access public     */    var $recurse;    /**     * Unknown     */    var $defnames;    /**     * Unknown     */    var $dnsrch;    /**     * Contains the value of the last error returned by the resolver.     *     * Contains the value of the last error returned by the resolver.     *     * @var string $errorstring     * @access public     */    var $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     * @access public     */    var $answerfrom;    /**     * The size of the answer packet.     *     * This contains a integer containing the size of the DNS packet the     * server responded with.     *     * @var string $answersize     * @access public     */    var $answersize;    /**     * The number of seconds after which a TCP connetion should timeout     *     * @var integer $tcp_timeout     * @access public     */    var $tcp_timeout;    /**     * The location of the system resolv.conf file.     *     * The location of the system resolv.conf file.     *      * @var string $resolv_conf     */    var $resolv_conf = '/etc/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     * configuration file     *     * @var string $dotfile     * @see Net_DNS_Resolver::$confpath     */    var $dotfile = '.resolv.conf';    /**     * A array of directories to search for the user's resolver config     *     * A array of directories to search for the user's resolver config     *     * @var string $confpath     * @see Net_DNS_Resolver::$dotfile     */    var $confpath;    /**     * debugging flag     *     * If set to TRUE (non-zero), debugging code will be displayed as the     * resolver makes the request.     *     * @var boolean $debug;     * @access public     */    var $debug;    /**     * 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;     * @access public     */    var $useEnhancedSockets = 1;    /**     * An array of sockets connected to a name servers     *     * @var array $sockets     * @access private     */    var $sockets;    /**     * axfr tcp socket     *     * Used to store a PHP socket resource for a connection to a server     *     * @var resource $_axfr_sock;     * @access private     */    var $_axfr_sock;    /**     * axfr resource record lsit     *     * Used to store a resource record list from a zone transfer     *     * @var resource $_axfr_rr;     * @access private     */    var $_axfr_rr;    /**     * axfr soa count     *     * Used to store the number of soa records received from a zone transfer     *     * @var resource $_axfr_soa_count;     * @access private     */    var $_axfr_soa_count;    /* }}} */    /* class constructor - Net_DNS_Resolver() {{{ */    /**     * Initializes the Resolver Object     */    function Net_DNS_Resolver()    {        $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                );        foreach ($default as $k => $v) {            $this->{$k} = $v;        }        $this->confpath[0] = getenv('HOME');        $this->confpath[1] = '.';        $this->res_init();    }    /* }}} */    /* Net_DNS_Resolver::res_init() {{{ */    /**     * Initalizes the resolver library     *     * res_init() searches for resolver library configuration files  an     * 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     * @access public     */    function res_init()    {        $err = error_reporting(0);        if (file_exists($this->resolv_conf) && is_readable($this->resolv_conf)) {            $this->read_config($this->resolv_conf);        }        foreach ($this->confpath as $dir) {            $file = "$dir/" . $this->dotfile;            if (file_exists($file) && is_readable($file)) {                $this->read_config($file);            }        }        $this->read_env();        if (!strlen($this->domain) && strlen($this->searchlist)) {            $this->default{'domain'} = $this->default{'searchlist'}[0];        } else if (! strlen($this->searchlist) && strlen($this->domain)) {            $this->searchlist = array($this->domain);        }        error_reporting($err);    }    /* }}} */    /* Net_DNS_Resolver::read_config {{{ */    /**     * Reads and parses a resolver configuration file     *     * @param string $file The name of the file to open and parse     */    function read_config($file)    {        if (! ($f = fopen($file, 'r'))) {            $this->error = "can't open $file";        }        while (! feof($f)) {            $line = chop(fgets($f, 10240));            $line = ereg_replace('(.*)[;#].*', '\\1', $line);            if (ereg("^[ \t]*$", $line, $regs)) {                continue;            }            ereg("^[ \t]*([^ \t]+)[ \t]+([^ \t]+)", $line, $regs);            $option = $regs[1];            $value = $regs[2];            switch ($option) {                case 'domain':                    $this->domain = $regs[2];                    break;                case 'search':                    $this->searchlist[count($this->searchlist)] = $regs[2];                    break;                case 'nameserver':                    foreach (split(' ', $regs[2]) as $ns)                        $this->nameservers[count($this->nameservers)] = $ns;                    break;            }        }        fclose($f);    }    /* }}} */    /* Net_DNS_Resolver::read_env() {{{ */    /**     * Examines the environment for resolver config information     */    function read_env()    {        if (getenv('RES_NAMESERVERS')) {            $this->nameservers = split(' ', getenv('RES_NAMESERVERS'));        }        if (getenv('RES_SEARCHLIST')) {            $this->searchlist = split(' ', getenv('RES_SEARCHLIST'));        }        if (getenv('LOCALDOMAIN')) {            $this->domain = getenv('LOCALDOMAIN');        }        if (getenv('RES_OPTIONS')) {            $env = split(' ', getenv('RES_OPTIONS'));            foreach ($env as $opt) {                list($name, $val) = split(':', $opt);                if ($val == '') {                    $val = 1;                }                $this->{$name} = $val;            }        }    }    /* }}} */    /* 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     * for display.     *     * @access public     */    function string()    {        $state = ";; Net_DNS_Resolver state:\n";        $state .= ';;  domain       = ' . $this->domain . "\n";        $state .= ';;  searchlist   = ' . implode(' ', $this->searchlist) . "\n";        $state .= ';;  nameservers  = ' . implode(' ', $this->nameservers) . "\n";        $state .= ';;  port         = ' . $this->port . "\n";        $state .= ';;  tcp_timeout  = ';        $state .= ($this->tcp_timeout ? $this->tcp_timeout : 'indefinite') . "\n";        $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";        return($state);    }    /* }}} */    /* Net_DNS_Resolver::nextid() {{{ */    /**     * Returns the next request Id to be used for the DNS packet header     */    function nextid()    {        global $_Net_DNS_packet_id;        return($_Net_DNS_packet_id++);    }    /* }}} */    /* not completed - Net_DNS_Resolver::nameservers() {{{ */    /**     * Unknown - not ported yet     */    function nameservers($nsa)    {        $defres = new Net_DNS_Resolver();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?