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. *  Maintainers:
  8. *  Marco Kaiser <bate@php.net>
  9. *  Florian Anderiasch <fa@php.net>
  10. *
  11. * PHP versions 4 and 5
  12. *
  13. * LICENSE: This source file is subject to version 3.01 of the PHP license
  14. * that is available through the world-wide-web at the following URI:
  15. * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  16. * the PHP License and are unable to obtain it through the web, please
  17. * send a note to license@php.net so we can mail you a copy immediately.
  18. */
  19.  
  20. /* Include files {{{ */
  21. require_once("Net/DNS/RR/A.php");
  22. require_once("Net/DNS/RR/AAAA.php");
  23. require_once("Net/DNS/RR/NS.php");
  24. require_once("Net/DNS/RR/CNAME.php");
  25. require_once("Net/DNS/RR/PTR.php");
  26. require_once("Net/DNS/RR/SOA.php");
  27. require_once("Net/DNS/RR/MX.php");
  28. require_once("Net/DNS/RR/TSIG.php");
  29. require_once("Net/DNS/RR/TXT.php");
  30. require_once("Net/DNS/RR/HINFO.php");
  31. require_once("Net/DNS/RR/SRV.php");
  32. require_once("Net/DNS/RR/NAPTR.php");
  33. require_once("Net/DNS/RR/RP.php");
  34. require_once("Net/DNS/RR/SPF.php");
  35. /* }}} */
  36. /* Net_DNS_RR object definition {{{ */
  37. /**
  38.  * Resource Record object definition
  39.  *
  40.  * Builds or parses resource record sections of the DNS  packet including
  41.  * the answer, authority, and additional  sections of the packet.
  42.  *
  43.  * @package Net_DNS
  44.  */
  45. class Net_DNS_RR
  46. {
  47.     /* class variable definitions {{{ */
  48.     var $name;
  49.     var $type;
  50.     var $class;
  51.     var $ttl;
  52.     var $rdlength;
  53.     var $rdata;
  54.     /* }}} */
  55.  
  56.     /*
  57.      * Use Net_DNS_RR::factory() instead
  58.      *
  59.      * @access private
  60.      */
  61.     /* class constructor - Net_DNS_RR($rrdata) {{{ */
  62.     function Net_DNS_RR($rrdata)
  63.     {
  64.         if ($rrdata != 'getRR'//BC check/warning remove later
  65.             trigger_error("Please use Net_DNS_RR::factory() instead");
  66.         }
  67.     }
  68.  
  69.     /*
  70.      * Returns an RR object, use this instead of constructor
  71.      *
  72.      * @param mixed $rr_rdata Options as string, array or data
  73.      * @return object Net_DNS_RR or Net_DNS_RR_<type>
  74.      * @access public
  75.      * @see Net_DNS_RR::new_from_array Net_DNS_RR::new_from_data Net_DNS_RR::new_from_string
  76.      */
  77.     function &factory($rrdata$update_type '')
  78.     {
  79.         if (is_string($rrdata)) {
  80.             $rr &Net_DNS_RR::new_from_string($rrdata$update_type);
  81.         elseif (count($rrdata== 7{
  82.             list($name$rrtype$rrclass$ttl$rdlength$data$offset$rrdata;
  83.             $rr &Net_DNS_RR::new_from_data($name$rrtype$rrclass$ttl$rdlength$data$offset);
  84.         else {
  85.             $rr &Net_DNS_RR::new_from_array($rrdata);
  86.         }
  87.         return $rr;
  88.     }
  89.  
  90.     /* }}} */
  91.     /* Net_DNS_RR::new_from_data($name, $ttl, $rrtype, $rrclass, $rdlength, $data, $offset) {{{ */
  92.     function &new_from_data($name$rrtype$rrclass$ttl$rdlength$data$offset)
  93.     {
  94.         $rr = new Net_DNS_RR('getRR');
  95.         $rr->name = $name;
  96.         $rr->type = $rrtype;
  97.         $rr->class = $rrclass;
  98.         $rr->ttl = $ttl;
  99.         $rr->rdlength = $rdlength;
  100.         $rr->rdata = substr($data$offset$rdlength);
  101.         if (class_exists('Net_DNS_RR_' $rrtype)) {
  102.             $scn 'Net_DNS_RR_' $rrtype;
  103.             $rr = new $scn($rr$data$offset);
  104.         }
  105.         return $rr;
  106.     }
  107.  
  108.     /* }}} */
  109.     /* Net_DNS_RR::new_from_string($rrstring, $update_type = '') {{{ */
  110.     function &new_from_string($rrstring$update_type '')
  111.     {
  112.         $rr = new Net_DNS_RR('getRR');
  113.         $ttl = 0;
  114.         $parts preg_split('/[\s]+/'$rrstring);
  115.         while (count($parts> 0{
  116.             $s array_shift($parts);
  117.             if (!isset($name)) {
  118.                 $name preg_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.  
  193.                 $obj = new $scn($rr$rdata);
  194.                 return $obj;
  195.             }
  196.  
  197.             return $rr;
  198.  
  199.         }
  200.  
  201.         return null;
  202.     }
  203.  
  204.     /* }}} */
  205.     /* Net_DNS_RR::new_from_array($rrarray) {{{ */
  206.     function &new_from_array($rrarray)
  207.     {
  208.         $rr = new Net_DNS_RR('getRR');
  209.         foreach ($rrarray as $k => $v{
  210.             $rr->{strtolower($k)$v;
  211.         }
  212.  
  213.         if (strlen($rr->name)) {
  214.             return null;
  215.         }
  216.         if (strlen($rr->type)){
  217.             return null;
  218.         }
  219.         if ($rr->ttl{
  220.             $rr->ttl = 0;
  221.         }
  222.         if (strlen($rr->class)) {
  223.             $rr->class = 'IN';
  224.         }
  225.         if (strlen($rr->rdata)) {
  226.             $rr->rdlength = strlen($rr->rdata);
  227.         }
  228.         if (class_exists('Net_DNS_RR_' $rr->type)) {
  229.             $scn 'Net_DNS_RR_' $rr->type;
  230.  
  231.             $obj = new $scn($rr!empty($rr->rdata$rr->rdata : $rrarray);
  232.             return $obj;
  233.         }
  234.  
  235.         return $rr;
  236.     }
  237.  
  238.     /* }}} */
  239.     /* Net_DNS_RR::display() {{{ */
  240.     function display()
  241.     {
  242.         echo $this->string("\n";
  243.     }
  244.  
  245.     /* }}} */
  246.     /* Net_DNS_RR::string() {{{ */
  247.     function string()
  248.     {
  249.         return $this->name . ".\t" (strlen($this->name< 16 ? "\t" ''.
  250.                 $this->ttl  . "\t"  .
  251.                 $this->class"\t"  .
  252.                 $this->type . "\t"  .
  253.                 $this->rdatastr();
  254.  
  255.     }
  256.  
  257.     /* }}} */
  258.     /* Net_DNS_RR::rdatastr() {{{ */
  259.     function rdatastr()
  260.     {
  261.         if ($this->rdlength{
  262.             return '; rdlength = ' $this->rdlength;
  263.         }
  264.         return '; no data';
  265.     }
  266.  
  267.     /* }}} */
  268.     /* Net_DNS_RR::rdata() {{{ */
  269.     function rdata(&$packetORrdata$offset '')
  270.     {
  271.         if ($offset{
  272.             return $this->rr_rdata($packetORrdata$offset);
  273.         else if (strlen($this->rdata)) {
  274.             return $this->rdata;
  275.         else {
  276.             return null;
  277.         }
  278.     }
  279.  
  280.     /* }}} */
  281.     /* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
  282.     function rr_rdata(&$packet$offset)
  283.     {
  284.         return (strlen($this->rdata$this->rdata : '');
  285.     }
  286.     /* }}} */
  287.     /* Net_DNS_RR::data() {{{ */
  288.     function data(&$packet$offset)
  289.     {
  290.         $data $packet->dn_comp($this->name$offset);
  291.         $data .= pack('n'Net_DNS::typesbyname(strtoupper($this->type)));
  292.         $data .= pack('n'Net_DNS::classesbyname(strtoupper($this->class)));
  293.         $data .= pack('N'$this->ttl);
  294.  
  295.         $offset += strlen($data+ 2;  // The 2 extra bytes are for rdlength
  296.  
  297.         $rdata $this->rdata($packet$offset);
  298.         $data .= pack('n'strlen($rdata));
  299.         $data .= $rdata;
  300.  
  301.         return $data;
  302.     }
  303.     /* }}} */
  304. }
  305. /* }}} */
  306. /* VIM settings {{{
  307.  * Local variables:
  308.  * tab-width: 4
  309.  * c-basic-offset: 4
  310.  * soft-stop-width: 4
  311.  * c indent on
  312.  * End:
  313.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  314.  * vim<600: sw=4 ts=4
  315.  * }}} */
  316. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.