Net_DNS
[ class tree: Net_DNS ] [ index: Net_DNS ] [ all elements ]

Source for file Header.php

Documentation is available at Header.php

  1. <?php
  2. /*
  3.  *  License Information:
  4.  *
  5.  *    Net_DNS:  A resolver library for PHP
  6.  *    Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  7.  *
  8.  *    This library is free software; you can redistribute it and/or
  9.  *    modify it under the terms of the GNU Lesser General Public
  10.  *    License as published by the Free Software Foundation; either
  11.  *    version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  *    This library is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *    Lesser General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU Lesser General Public
  19.  *    License along with this library; if not, write to the Free Software
  20.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23. /*  Net_DNS_Header object definition {{{ */
  24. /**
  25.  * Object representation of the HEADER section of a DNS packet
  26.  *
  27.  * The Net_DNS::Header class contains the values of a DNS  packet.  It parses
  28.  * the header of a DNS packet or can  generate the binary data
  29.  * representation of the packet.  The format of the header is described in
  30.  * RFC1035.
  31.  *
  32.  * @package Net_DNS
  33.  */
  34. {
  35.     /* class variable definitions {{{ */
  36.     /**
  37.      * The packet's request id
  38.      *
  39.      * The request id of the packet represented as  a 16 bit integer.
  40.      */
  41.     var $id;
  42.     /**
  43.      * The QR bit in a DNS packet header
  44.      *
  45.      * The QR bit as described in RFC1035.  QR is set to 0 for queries, and
  46.      * 1 for repsones.
  47.      */
  48.     var $qr;
  49.     /**
  50.      * The OPCODE name of this packet.
  51.      *
  52.      * The string value (name) of the opcode for the DNS packet.
  53.      */
  54.     var $opcode;
  55.     /**
  56.      * The AA (authoritative answer) bit in a DNS packet header
  57.      *
  58.      * The AA bit as described in RFC1035.  AA is set to  1 if the answer
  59.      * is authoritative.  It has no meaning if QR is set to 0.
  60.      */
  61.     var $aa;
  62.     /**
  63.      * The TC (truncated) bit in a DNS packet header
  64.      *
  65.      * This flag is set to 1 if the response was truncated.  This flag has
  66.      * no meaning in a query packet.
  67.      */
  68.     var $tc;
  69.     /**
  70.      * The RD (recursion desired) bit in a DNS packet header
  71.      *
  72.      * This bit should be set to 1 in a query if recursion  is desired by
  73.      * the DNS server.
  74.      */
  75.     var $rd;
  76.     /**
  77.      * The RA (recursion available) bit in a DNS packet header
  78.      *
  79.      * This bit is set to 1 by the DNS server if the server is willing to
  80.      * perform recursion.
  81.      */
  82.     var $ra;
  83.     /**
  84.      * The RCODE name for this packet.
  85.      *
  86.      * The string value (name) of the rcode for the DNS packet.
  87.      */
  88.     var $rcode;
  89.     /**
  90.      * Number of questions contained within the packet
  91.      *
  92.      * 16bit integer representing the number of questions in the question
  93.      * section of the DNS packet.
  94.      *
  95.      * @var integer $qdcount 
  96.      * @see     Net_DNS_Question class
  97.      */
  98.     var $qdcount;
  99.     /**
  100.      * Number of answer RRs contained within the packet
  101.      *
  102.      * 16bit integer representing the number of answer resource records
  103.      * contained in the answer section of the DNS packet.
  104.      *
  105.      * @var integer $ancount 
  106.      * @see     Net_DNS_RR class
  107.      */
  108.     var $ancount;
  109.     /**
  110.      * Number of authority RRs within the packet
  111.      *
  112.      * 16bit integer representing the number of authority (NS) resource
  113.      * records  contained in the authority section of the DNS packet.
  114.      *
  115.      * @var integer $nscount 
  116.      * @see     Net_DNS_RR class
  117.      */
  118.     var $nscount;
  119.     /**
  120.      * Number of additional RRs within the packet
  121.      *
  122.      * 16bit integer representing the number of additional resource records
  123.      * contained in the additional section of the DNS packet.
  124.      *
  125.      * @var integer $arcount 
  126.      * @see     Net_DNS_RR class
  127.      */
  128.     var $arcount;
  129.  
  130.     /* }}} */
  131.     /* class constructor - Net_DNS_Header($data = "") {{{ */
  132.     /**
  133.      * Initializes the default values for the Header object.
  134.      * 
  135.      * Builds a header object from either default values, or from a DNS
  136.      * packet passed into the constructor as $data
  137.      *
  138.      * @param string $data  A DNS packet of which the header will be parsed.
  139.      * @return  object  Net_DNS_Header 
  140.      * @access public
  141.      */
  142.     function Net_DNS_Header($data '')
  143.     {
  144.         if ($data != ''{
  145.             /*
  146.              * The header MUST be at least 12 bytes.
  147.              * Passing the full datagram to this constructor
  148.              * will examine only the header section of the DNS packet
  149.              */
  150.             if (strlen($data< 12)
  151.                 return false;
  152.  
  153.             $a unpack('nid/C2flags/n4counts'$data);
  154.             $this->id      = $a['id'];
  155.             $this->qr      = ($a['flags1'>> 70x1;
  156.             $this->opcode  = ($a['flags1'>> 30xf;
  157.             $this->aa      = ($a['flags1'>> 20x1;
  158.             $this->tc      = ($a['flags1'>> 10x1;
  159.             $this->rd      = $a['flags1'0x1;
  160.             $this->ra      = ($a['flags2'>> 70x1;
  161.             $this->rcode   = $a['flags2'0xf;
  162.             $this->qdcount = $a['counts1'];
  163.             $this->ancount = $a['counts2'];
  164.             $this->nscount = $a['counts3'];
  165.             $this->arcount = $a['counts4'];
  166.         }
  167.         else {
  168.             $this->id      = Net_DNS_Resolver::nextid();
  169.             $this->qr      = 0;
  170.             $this->opcode  = 0;
  171.             $this->aa      = 0;
  172.             $this->tc      = 0;
  173.             $this->rd      = 1;
  174.             $this->ra      = 0;
  175.             $this->rcode   = 0;
  176.             $this->qdcount = 1;
  177.             $this->ancount = 0;
  178.             $this->nscount = 0;
  179.             $this->arcount = 0;
  180.         }
  181.  
  182.         if (Net_DNS::opcodesbyval($this->opcode)) {
  183.             $this->opcode = Net_DNS::opcodesbyval($this->opcode);
  184.         }
  185.         if (Net_DNS::rcodesbyval($this->rcode)) {
  186.             $this->rcode = Net_DNS::rcodesbyval($this->rcode);
  187.         }
  188.     }
  189.  
  190.     /* }}} */
  191.     /* Net_DNS_Header::display() {{{ */
  192.     /**
  193.      * Displays the properties of the header.
  194.      *
  195.      * Displays the properties of the header.
  196.      *
  197.      * @access public
  198.      */
  199.     function display()
  200.     {
  201.         echo $this->string();
  202.     }
  203.  
  204.     /* }}} */
  205.     /* Net_DNS_Header::string() {{{ */
  206.     /**
  207.      * Returns a formatted string containing the properties of the header.
  208.      *
  209.      * @return string   a formatted string containing the properties of the header.
  210.      * @access public
  211.      */
  212.     function string()
  213.     {
  214.         $retval ';; id = ' $this->id . "\n";
  215.         if ($this->opcode == 'UPDATE'{
  216.             $retval .= ';; qr = ' $this->qr . '    ' .
  217.                 'opcode = ' $this->opcode . '    '   .
  218.                 'rcode = ' $this->rcode . "\n";
  219.             $retval .= ';; zocount = ' $this->qdcount . '  ' .  
  220.                 'prcount = ' $this->ancount . '  '           .
  221.                 'upcount = ' $this->nscount . '  '           .
  222.                 'adcount = ' $this->arcount . "\n";
  223.         else {
  224.             $retval .= ';; qr = ' $this->qr . '    ' .
  225.                 'opcode = ' $this->opcode . '    '   .
  226.                 'aa = ' $this->aa . '    '           .
  227.                 'tc = ' $this->tc . '    '           .
  228.                 'rd = ' $this->rd . "\n";
  229.  
  230.             $retval .= ';; ra = ' $this->ra . '    ' .
  231.                 'rcode  = ' $this->rcode . "\n";
  232.  
  233.             $retval .= ';; qdcount = ' $this->qdcount . '  ' .
  234.                 'ancount = ' $this->ancount . '  '    .
  235.                 'nscount = ' $this->nscount . '  '    .
  236.                 'arcount = ' $this->arcount . "\n";
  237.         }
  238.         return $retval;
  239.     }
  240.  
  241.     /* }}} */
  242.     /* Net_DNS_Header::data() {{{ */
  243.     /**
  244.      * Returns the binary data containing the properties of the header
  245.      *
  246.      * Packs the properties of the Header object into a binary string
  247.      * suitable for using as the Header section of a DNS packet.
  248.      *
  249.      * @return string   binary representation of the header object
  250.      * @access public
  251.      */
  252.     function data()
  253.     {
  254.         $opcode Net_DNS::opcodesbyname($this->opcode);
  255.         $rcode  Net_DNS::rcodesbyname($this->rcode);
  256.  
  257.         $byte2 ($this->qr << 7)
  258.             | ($opcode << 3)
  259.             | ($this->aa << 2)
  260.             | ($this->tc << 1)
  261.             | ($this->rd);
  262.  
  263.         $byte3 ($this->ra << 7$rcode;
  264.  
  265.         return pack('nC2n4'$this->id,
  266.                 $byte2,
  267.                 $byte3,
  268.                 $this->qdcount,
  269.                 $this->ancount,
  270.                 $this->nscount,
  271.                 $this->arcount);
  272.     }
  273.  
  274.     /* }}} */
  275. }
  276. /* }}} */
  277. /* VIM settings {{{
  278.  * Local variables:
  279.  * tab-width: 4
  280.  * c-basic-offset: 4
  281.  * soft-stop-width: 4
  282.  * c indent on
  283.  * expandtab on
  284.  * End:
  285.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  286.  * vim<600: sw=4 ts=4
  287.  * }}} */
  288. ?>

Documentation generated on Mon, 11 Mar 2019 14:35:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.