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

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