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

Source for file RR.php

Documentation is available at RR.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. /* Include files {{{ */
  24. require_once("$phpdns_basedir/DNS/RR/A.php");
  25. require_once("$phpdns_basedir/DNS/RR/AAAA.php");
  26. require_once("$phpdns_basedir/DNS/RR/NS.php");
  27. require_once("$phpdns_basedir/DNS/RR/CNAME.php");
  28. require_once("$phpdns_basedir/DNS/RR/PTR.php");
  29. require_once("$phpdns_basedir/DNS/RR/SOA.php");
  30. require_once("$phpdns_basedir/DNS/RR/MX.php");
  31. require_once("$phpdns_basedir/DNS/RR/TSIG.php");
  32. require_once("$phpdns_basedir/DNS/RR/TXT.php");
  33. require_once("$phpdns_basedir/DNS/RR/HINFO.php");
  34. require_once("$phpdns_basedir/DNS/RR/SRV.php");
  35. require_once("$phpdns_basedir/DNS/RR/NAPTR.php");
  36. /* }}} */
  37. /* Net_DNS_RR object definition {{{ */
  38. /**
  39.  * Resource Record object definition
  40.  *
  41.  * Builds or parses resource record sections of the DNS  packet including
  42.  * the answer, authority, and additional  sections of the packet.
  43.  *
  44.  * @package Net_DNS
  45.  */
  46. class Net_DNS_RR
  47. {
  48.     /* class variable definitions {{{ */
  49.     var $name;
  50.     var $type;
  51.     var $class;
  52.     var $ttl;
  53.     var $rdlength;
  54.     var $rdata;
  55.     /* }}} */
  56.  
  57.     /*
  58.      * Use Net_DNS_RR::factory() instead
  59.      *
  60.      * @access private
  61.      */
  62.     /* class constructor - Net_DNS_RR($rrdata) {{{ */
  63.     function Net_DNS_RR($rrdata)
  64.     {
  65.         if ($rrdata != 'getRR'//BC check/warning remove later
  66.             trigger_error("Please use Net_DNS_RR::factory() instead");
  67.         }
  68.     }
  69.  
  70.     /*
  71.      * Returns an RR object, use this instead of constructor
  72.      *
  73.      * @param mixed $rr_rdata Options as string, array or data
  74.      * @return object Net_DNS_RR or Net_DNS_RR_<type>
  75.      * @access public
  76.      * @see Net_DNS_RR::new_from_array Net_DNS_RR::new_from_data Net_DNS_RR::new_from_string
  77.      */
  78.     function &factory($rrdata)
  79.     {
  80.         if (is_string($rrdata)) {
  81.             $rr &Net_DNS_RR::new_from_string($rrdata);
  82.         elseif (count($rrdata== 7{
  83.             list($name$rrtype$rrclass$ttl$rdlength$data$offset$rrdata;
  84.             $rr &Net_DNS_RR::new_from_data($name$rrtype$rrclass$ttl$rdlength$data$offset);
  85.         else {
  86.             $rr &Net_DNS_RR::new_from_array($rrdata);
  87.         }
  88.         return $rr;
  89.     }
  90.  
  91.     /* }}} */
  92.     /* Net_DNS_RR::new_from_data($name, $ttl, $rrtype, $rrclass, $rdlength, $data, $offset) {{{ */
  93.     function &new_from_data($name$rrtype$rrclass$ttl$rdlength$data$offset)
  94.     {
  95.         $rr &new Net_DNS_RR('getRR');
  96.         $rr->name = $name;
  97.         $rr->type = $rrtype;
  98.         $rr->class = $rrclass;
  99.         $rr->ttl = $ttl;
  100.         $rr->rdlength = $rdlength;
  101.         $rr->rdata = substr($data$offset$rdlength);
  102.         if (class_exists('Net_DNS_RR_' $rrtype)) {
  103.             $scn 'Net_DNS_RR_' $rrtype;
  104.             $rr = new $scn($rr$data$offset);
  105.         }
  106.         return $rr;
  107.     }
  108.  
  109.     /* }}} */
  110.     /* Net_DNS_RR::new_from_string($rrstring, $update_type = '') {{{ */
  111.     function &new_from_string($rrstring$update_type '')
  112.     {
  113.         $rr &new Net_DNS_RR('getRR');
  114.         $ttl = 0;
  115.         $parts preg_split('/[\s]+/'$rrstring);
  116.         while ($s array_shift($parts)) {
  117.             if (!isset($name)) {
  118.                 $name ereg_replace('\.+$'''$s);
  119.             else if (preg_match('/^\d+$/'$s)) {
  120.                 $ttl $s;
  121.             else if (!isset($rrclass&& is_null(Net_DNS::classesbyname(strtoupper($s)))) {
  122.                 $rrclass strtoupper($s);
  123.                 $rdata join(' '$parts);
  124.             else if (is_null(Net_DNS::typesbyname(strtoupper($s)))) {
  125.                 $rrtype strtoupper($s);
  126.                 $rdata join(' '$parts);
  127.                 break;
  128.             else {
  129.                 break;
  130.             }
  131.         }
  132.  
  133.         /*
  134.          *  Do we need to do this?
  135.          */
  136.         $rdata trim(chop($rdata));
  137.  
  138.         if (strlen($rrtype&& strlen($rrclass&& $rrclass == 'ANY'{
  139.             $rrtype $rrclass;
  140.             $rrclass 'IN';
  141.         else if (isset($rrclass)) {
  142.             $rrclass 'IN';
  143.         }
  144.  
  145.         if (strlen($rrtype)) {
  146.             $rrtype 'ANY';
  147.         }
  148.  
  149.         if (strlen($update_type)) {
  150.             $update_type strtolower($update_type);
  151.             if ($update_type == 'yxrrset'{
  152.                 $ttl = 0;
  153.                 if (strlen($rdata)) {
  154.                     $rrclass 'ANY';
  155.                 }
  156.             else if ($update_type == 'nxrrset'{
  157.                 $ttl = 0;
  158.                 $rrclass 'NONE';
  159.                 $rdata '';
  160.             else if ($update_type == 'yxdomain'{
  161.                 $ttl = 0;
  162.                 $rrclass 'ANY';
  163.                 $rrtype 'ANY';
  164.                 $rdata '';
  165.             else if ($update_type == 'nxdomain'{
  166.                 $ttl = 0;
  167.                 $rrclass 'NONE';
  168.                 $rrtype 'ANY';
  169.                 $rdata '';
  170.             else if (preg_match('/^(rr_)?add$/'$update_type)) {
  171.                 $update_type 'add';
  172.                 if ($ttl{
  173.                     $ttl = 86400;
  174.                 }
  175.             else if (preg_match('/^(rr_)?del(ete)?$/'$update_type)) {
  176.                 $update_type 'del';
  177.                 $ttl = 0;
  178.                 $rrclass $rdata 'NONE' 'ANY';
  179.             }
  180.         }
  181.  
  182.         if (strlen($rrtype)) {
  183.             $rr->name = $name;
  184.             $rr->type = $rrtype;
  185.             $rr->class = $rrclass;
  186.             $rr->ttl = $ttl;
  187.             $rr->rdlength = 0;
  188.             $rr->rdata = '';
  189.  
  190.             if (class_exists('Net_DNS_RR_' $rrtype)) {
  191.                 $scn 'Net_DNS_RR_' $rrtype;
  192.                 return new $scn($rr$rdata);
  193.             else {
  194.                 return $rr;
  195.             }
  196.         else {
  197.             return null;
  198.         }
  199.     }
  200.  
  201.     /* }}} */
  202.     /* Net_DNS_RR::new_from_array($rrarray) {{{ */
  203.     function &new_from_array($rrarray)
  204.     {
  205.         $rr &new Net_DNS_RR('getRR');
  206.         foreach ($rrarray as $k => $v{
  207.             $rr->{strtolower($k)$v;
  208.         }
  209.  
  210.         if (strlen($rr->name)) {
  211.             return null;
  212.         }
  213.         if (strlen($rr->type)){
  214.             return null;
  215.         }
  216.         if ($rr->ttl{
  217.             $rr->ttl = 0;
  218.         }
  219.         if (strlen($rr->class)) {
  220.             $rr->class = 'IN';
  221.         }
  222.         if (strlen($rr->rdata)) {
  223.             $rr->rdlength = strlen($rdata);
  224.         }
  225.         if (class_exists('Net_DNS_RR_' $rrtype)) {
  226.             $scn 'Net_DNS_RR_' $rrtype;
  227.             return new $scn($rr$rdata);
  228.         else {
  229.             return $rr;
  230.         }
  231.     }
  232.  
  233.     /* }}} */
  234.     /* Net_DNS_RR::display() {{{ */
  235.     function display()
  236.     {
  237.         echo $this->string("\n";
  238.     }
  239.  
  240.     /* }}} */
  241.     /* Net_DNS_RR::string() {{{ */
  242.     function string()
  243.     {
  244.         return $this->name . ".\t" (strlen($this->name< 16 ? "\t" ''.
  245.                 $this->ttl  . "\t"  .
  246.                 $this->class"\t"  .
  247.                 $this->type . "\t"  .
  248.                 $this->rdatastr();
  249.  
  250.     }
  251.  
  252.     /* }}} */
  253.     /* Net_DNS_RR::rdatastr() {{{ */
  254.     function rdatastr()
  255.     {
  256.         if ($this->rdlength{
  257.             return '; rdlength = ' $this->rdlength;
  258.         }
  259.         return '; no data';
  260.     }
  261.  
  262.     /* }}} */
  263.     /* Net_DNS_RR::rdata() {{{ */
  264.     function rdata(&$packetORrdata$offset '')
  265.     {
  266.         if ($offset{
  267.             return $this->rr_rdata($packetORrdata$offset);
  268.         else if (strlen($this->rdata)) {
  269.             return $this->rdata;
  270.         else {
  271.             return null;
  272.         }
  273.     }
  274.  
  275.     /* }}} */
  276.     /* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
  277.     function rr_rdata(&$packet$offset)
  278.     {
  279.         return (strlen($this->rdata$this->rdata : '');
  280.     }
  281.     /* }}} */
  282.     /* Net_DNS_RR::data() {{{ */
  283.     function data(&$packet$offset)
  284.     {
  285.         $data $packet->dn_comp($this->name$offset);
  286.         $data .= pack('n'Net_DNS::typesbyname(strtoupper($this->type)));
  287.         $data .= pack('n'Net_DNS::classesbyname(strtoupper($this->class)));
  288.         $data .= pack('N'$this->ttl);
  289.  
  290.         $offset += strlen($data+ 2;  // The 2 extra bytes are for rdlength
  291.  
  292.         $rdata $this->rdata($packet$offset);
  293.         $data .= pack('n'strlen($rdata));
  294.         $data .= $rdata;
  295.  
  296.         return $data;
  297.     }
  298.     /* }}} */
  299. }
  300. /* }}} */
  301. /* VIM settings {{{
  302.  * Local variables:
  303.  * tab-width: 4
  304.  * c-basic-offset: 4
  305.  * soft-stop-width: 4
  306.  * c indent on
  307.  * End:
  308.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  309.  * vim<600: sw=4 ts=4
  310.  * }}} */
  311. ?>

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