packet.php

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

PHP
626
字号
<?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_Packet object definition {{{ *//** * A object represation of a DNS packet (RFC1035) * * This object is used to manage a DNS packet.  It contains methods for * DNS packet compression as defined in RFC1035, as well as parsing  a DNS * packet response from a DNS server, or building a DNS packet from  the * instance variables contained in the class. * * @package Net_DNS */class Net_DNS_Packet{    /* class variable definitions {{{ */    /**     * debugging flag     *     * If set to TRUE (non-zero), debugging code will be displayed as the     * packet is parsed.     *     * @var boolean $debug     * @access  public     */    var $debug;    /**     * A packet Header object.     *     * An object of type Net_DNS_Header which contains the header     * information  of the packet.     *     * @var object Net_DNS_Header $header     * @access  public     */    var $header;    /**     * A hash of compressed labels     *     * A list of all labels which have been compressed in the DNS packet     * and  the location offset of the label within the packet.     *     * @var array   $compnames     */    var $compnames;    /**     * The origin of the packet, if the packet is a server response.     *     * 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, if the packet is a server response.     *     * This contains a integer containing the size of the DNS packet the     * server responded with if this packet was received by a DNS server     * using the query() method.     *     * @var string  $answersize     * @access  public     */    var $answersize;    /**     * An array of Net_DNS_Question objects     *     * Contains all of the questions within the packet.  Each question is     * stored as an object of type Net_DNS_Question.     *     * @var array   $question     * @access  public     */    var $question;    /**     * An array of Net_DNS_RR ANSWER objects     *     * Contains all of the answer RRs within the packet.  Each answer is     * stored as an object of type Net_DNS_RR.     *     * @var array   $answer     * @access  public     */    var $answer;    /**     * An array of Net_DNS_RR AUTHORITY objects     *     * Contains all of the authority RRs within the packet.  Each authority is     * stored as an object of type Net_DNS_RR.     *     * @var array   $authority     * @access  public     */    var $authority;    /**     * An array of Net_DNS_RR ADDITIONAL objects     *     * Contains all of the additional RRs within the packet.  Each additional is     * stored as an object of type Net_DNS_RR.     *     * @var array   $additional     * @access  public     */    var $additional;    /* }}} */    /* class constructor - Net_DNS_Packet($debug = FALSE) {{{ */    /*     * unfortunately (or fortunately), we can't follow the same     * silly method for determining if name is a hostname or a packet     * stream in PHP, since there is no ref() function.  So we're going     * to define a new method called parse to deal with this     * circumstance and another method called buildQuestion to build a question.     * I like it better that way anyway.     */    /**     * Initalizes a Net_DNS_Packet object     *     * @param boolean $debug Turns debugging on or off     */    function Net_DNS_Packet($debug = FALSE)    {        $this->debug = $debug;        $this->compnames = array();    }    /* }}} */    /* Net_DNS_Packet::buildQuestion($name, $type = "A", $class = "IN") {{{ */    /**     * Adds a DNS question to the DNS packet     *     * @param   string $name    The name of the record to query     * @param   string $type    The type of record to query     * @param   string $class   The class of record to query     * @see Net_DNS::typesbyname(), Net_DNS::classesbyname()     */    function buildQuestion($name, $type = 'A', $class = 'IN')    {        $this->header = new Net_DNS_Header();        $this->header->qdcount = 1;        $this->question[0] = new Net_DNS_Question($name, $type, $class);        $this->answer = NULL;        $this->authority = NULL;        $this->additional = NULL;        if ($this->debug) {            $this->display();        }    }    /* }}} */    /* Net_DNS_Packet::parse($data) {{{ */    /**     * Parses a DNS packet returned by a DNS server     *     * Parses a complete DNS packet and builds an object hierarchy     * containing all of the parts of the packet:     * <ul>     *   <li>HEADER        *   <li>QUESTION        *   <li>ANSWER || PREREQUISITE        *   <li>ADDITIONAL || UPDATE        *   <li>AUTHORITY     * </ul>     *      * @param string $data  A binary string containing a DNS packet     * @return boolean TRUE on success, NULL on parser error     */    function parse($data)    {        if ($this->debug) {            echo ';; HEADER SECTION' . "\n";        }        $this->header = new Net_DNS_Header($data);        if ($this->debug) {            $this->header->display();        }        /*         *  Print and parse the QUESTION section of the packet         */        if ($this->debug) {            echo "\n";            $section = ($this->header->opcode  == 'UPDATE') ? 'ZONE' : 'QUESTION';            echo ";; $section SECTION (" . $this->header->qdcount . ' record' .                ($this->header->qdcount == 1 ? '' : 's') . ")\n";        }        $offset = 12;        $this->question = array();        for ($ctr = 0; $ctr < $this->header->qdcount; $ctr++) {            list($qobj, $offset) = $this->parse_question($data, $offset);            if (is_null($qobj)) {                return(NULL);            }            $this->question[count($this->question)] = $qobj;            if ($this->debug) {                echo ";;\n;";                $qobj->display();            }        }        /*         *  Print and parse the PREREQUISITE or ANSWER  section of the packet         */        if ($this->debug) {            echo "\n";            $section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' :'ANSWER';            echo ";; $section SECTION (" .                $this->header->ancount . ' record' .                (($this->header->ancount == 1) ? '' : 's') .                ")\n";        }        $this->answer = array();        for ($ctr = 0; $ctr < $this->header->ancount; $ctr++) {            list($rrobj, $offset) = $this->parse_rr($data, $offset);            if (is_null($rrobj)) {                return(NULL);            }            array_push($this->answer, $rrobj);            if ($this->debug) {                $rrobj->display();            }        }        /*         *  Print and parse the UPDATE or AUTHORITY section of the packet         */        if ($this->debug) {            echo "\n";            $section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY';            echo ";; $section SECTION (" .                $this->header->nscount . ' record' .                (($this->header->nscount == 1) ? '' : 's') .                ")\n";        }        $this->authority = array();        for ($ctr = 0; $ctr < $this->header->nscount; $ctr++) {            list($rrobj, $offset) = $this->parse_rr($data, $offset);            if (is_null($rrobj)) {                return(NULL);            }            array_push($this->authority, $rrobj);            if ($this->debug) {                $rrobj->display();            }        }        /*         *  Print and parse the ADDITIONAL section of the packet         */        if ($this->debug) {            echo "\n";            echo ';; ADDITIONAL SECTION (' .                $this->header->arcount . ' record' .                (($this->header->arcount == 1) ? '' : 's') .                ")\n";        }        $this->additional = array();        for ($ctr = 0; $ctr < $this->header->arcount; $ctr++) {            list($rrobj, $offset) = $this->parse_rr($data, $offset);            if (is_null($rrobj)) {                return(NULL);            }            array_push($this->additional, $rrobj);            if ($this->debug) {                $rrobj->display();            }        }        return(TRUE);    }    /* }}} */    /* Net_DNS_Packet::data() {{{*/    /**     * Build a packet from a Packet object hierarchy     *     * Builds a valid DNS packet suitable for sending to a DNS server or     * resolver client containing all of the data in the packet hierarchy.     *     * @return string A binary string containing a DNS Packet

⌨️ 快捷键说明

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