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.             return new $scn($rr$data$offset);
  105.         else {
  106.             return $rr;
  107.         }
  108.     }
  109.  
  110.     /* }}} */
  111.     /* Net_DNS_RR::new_from_string($rrstring, $update_type = '') {{{ */
  112.     function &new_from_string($rrstring$update_type '')
  113.     {
  114.         $rr &new Net_DNS_RR('getRR');
  115.         $ttl = 0;
  116.         $parts preg_split('/[\s]+/'$rrstring);
  117.         while ($s array_shift($parts)) {
  118.             if (!isset($name)) {
  119.                 $name ereg_replace('\.+$'''$s);
  120.             else if (preg_match('/^\d+$/'$s)) {
  121.                 $ttl $s;
  122.             else if (!isset($rrclass&& is_null(Net_DNS::classesbyname(strtoupper($s)))) {
  123.                 $rrclass strtoupper($s);
  124.                 $rdata join(' '$parts);
  125.             else if (is_null(Net_DNS::typesbyname(strtoupper($s)))) {
  126.                 $rrtype strtoupper($s);
  127.                 $rdata join(' '$parts);
  128.                 break;
  129.             else {
  130.                 break;
  131.             }
  132.         }
  133.  
  134.         /*
  135.          *  Do we need to do this?
  136.          */
  137.         $rdata trim(chop($rdata));
  138.  
  139.         if (strlen($rrtype&& strlen($rrclass&& $rrclass == 'ANY'{
  140.             $rrtype $rrclass;
  141.             $rrclass 'IN';
  142.         else if (isset($rrclass)) {
  143.             $rrclass 'IN';
  144.         }
  145.  
  146.         if (strlen($rrtype)) {
  147.             $rrtype 'ANY';
  148.         }
  149.  
  150.         if (strlen($update_type)) {
  151.             $update_type strtolower($update_type);
  152.             if ($update_type == 'yxrrset'{
  153.                 $ttl = 0;
  154.                 if (strlen($rdata)) {
  155.                     $rrclass 'ANY';
  156.                 }
  157.             else if ($update_type == 'nxrrset'{
  158.                 $ttl = 0;
  159.                 $rrclass 'NONE';
  160.                 $rdata '';
  161.             else if ($update_type == 'yxdomain'{
  162.                 $ttl = 0;
  163.                 $rrclass 'ANY';
  164.                 $rrtype 'ANY';
  165.                 $rdata '';
  166.             else if ($update_type == 'nxdomain'{
  167.                 $ttl = 0;
  168.                 $rrclass 'NONE';
  169.                 $rrtype 'ANY';
  170.                 $rdata '';
  171.             else if (preg_match('/^(rr_)?add$/'$update_type)) {
  172.                 $update_type 'add';
  173.                 if ($ttl{
  174.                     $ttl = 86400;
  175.                 }
  176.             else if (preg_match('/^(rr_)?del(ete)?$/'$update_type)) {
  177.                 $update_type 'del';
  178.                 $ttl = 0;
  179.                 $rrclass $rdata 'NONE' 'ANY';
  180.             }
  181.         }
  182.  
  183.         if (strlen($rrtype)) {
  184.             $rr->name = $name;
  185.             $rr->type = $rrtype;
  186.             $rr->class = $rrclass;
  187.             $rr->ttl = $ttl;
  188.             $rr->rdlength = 0;
  189.             $rr->rdata = '';
  190.  
  191.             if (class_exists('Net_DNS_RR_' $rrtype)) {
  192.                 $scn 'Net_DNS_RR_' $rrtype;
  193.                 return new $scn($rr$rdata);
  194.             else {
  195.                 return $rr;
  196.             }
  197.         else {
  198.             return NULL;
  199.         }
  200.     }
  201.  
  202.     /* }}} */
  203.     /* Net_DNS_RR::new_from_array($rrarray) {{{ */
  204.     function &new_from_array($rrarray)
  205.     {
  206.         $rr &new Net_DNS_RR('getRR');
  207.         foreach ($rrarray as $k => $v{
  208.             $rr->{strtolower($k)$v;
  209.         }
  210.  
  211.         if (strlen($rr->name)) {
  212.             return NULL;
  213.         }
  214.         if (strlen($rr->type)){
  215.             return NULL;
  216.         }
  217.         if ($rr->ttl{
  218.             $rr->ttl = 0;
  219.         }
  220.         if (strlen($rr->class)) {
  221.             $rr->class = 'IN';
  222.         }
  223.         if (strlen($rr->rdata)) {
  224.             $rr->rdlength = strlen($rdata);
  225.         }
  226.         if (class_exists('Net_DNS_RR_' $rrtype)) {
  227.             $scn 'Net_DNS_RR_' $rrtype;
  228.             return new $scn($rr$rdata);
  229.         else {
  230.             return $rr;
  231.         }
  232.     }
  233.  
  234.     /* }}} */
  235.     /* Net_DNS_RR::display() {{{ */
  236.     function display()
  237.     {
  238.         echo $this->string("\n";
  239.     }
  240.  
  241.     /* }}} */
  242.     /* Net_DNS_RR::string() {{{ */
  243.     function string()
  244.     {
  245.         return($this->name . ".\t" (strlen($this->name< 16 ? "\t" ''.
  246.                 $this->ttl  . "\t"  .
  247.                 $this->class"\t"  .
  248.                 $this->type . "\t"  .
  249.                 $this->rdatastr());
  250.  
  251.     }
  252.  
  253.     /* }}} */
  254.     /* Net_DNS_RR::rdatastr() {{{ */
  255.     function rdatastr()
  256.     {
  257.         if ($this->rdlength{
  258.             return('; rdlength = ' $this->rdlength);
  259.         }
  260.         return('; no data');
  261.     }
  262.  
  263.     /* }}} */
  264.     /* Net_DNS_RR::rdata() {{{ */
  265.     function rdata(&$packetORrdata$offset '')
  266.     {
  267.         if ($offset{
  268.             return($this->rr_rdata($packetORrdata$offset));
  269.         else if (strlen($this->rdata)) {
  270.             return($this->rdata);
  271.         else {
  272.             return(NULL);
  273.         }
  274.     }
  275.  
  276.     /* }}} */
  277.     /* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
  278.     function rr_rdata(&$packet$offset)
  279.     {
  280.         return((strlen($this->rdata$this->rdata : ''));
  281.     }
  282.     /* }}} */
  283.     /* Net_DNS_RR::data() {{{ */
  284.     function data(&$packet$offset)
  285.     {
  286.         $data $packet->dn_comp($this->name$offset);
  287.         $data .= pack('n'Net_DNS::typesbyname(strtoupper($this->type)));
  288.         $data .= pack('n'Net_DNS::classesbyname(strtoupper($this->class)));
  289.         $data .= pack('N'$this->ttl);
  290.  
  291.         $offset += strlen($data+ 2;  // The 2 extra bytes are for rdlength
  292.  
  293.         $rdata $this->rdata($packet$offset);
  294.         $data .= pack('n'strlen($rdata));
  295.         $data .= $rdata;
  296.  
  297.         return($data);
  298.     }
  299.     /* }}} */
  300. }
  301. /* }}} */
  302. /* VIM settings {{{
  303.  * Local variables:
  304.  * tab-width: 4
  305.  * c-basic-offset: 4
  306.  * soft-stop-width: 4
  307.  * c indent on
  308.  * End:
  309.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  310.  * vim<600: sw=4 ts=4
  311.  * }}} */
  312. ?>

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