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

Source for file DNS.php

Documentation is available at DNS.php

  1. <?php
  2. /*
  3.  *  Module written/ported by Eric Kilfoil <eric@ypass.net>
  4.  *
  5.  *  This is the copyright notice from the PERL Net::DNS module:
  6.  *
  7.  *  Copyright (c) 1997-2000 Michael Fuhr.  All rights reserved.  This
  8.  *  program is free software; you can redistribute it and/or modify it
  9.  *  under the same terms as Perl itself.
  10.  *
  11.  *  The majority of this is _NOT_ my code.  I simply ported it from the
  12.  *  PERL Net::DNS module.
  13.  *
  14.  *  The author of the Net::DNS module is Michael Fuhr <mike@fuhr.org>
  15.  *  http://www.fuhr.org/~mfuhr/perldns/
  16.  *
  17.  *  I _DO_ maintain this code, and Miachael Fuhr has nothing to with the
  18.  *  porting of this code to PHP.  Any questions directly related to this
  19.  *  class library should be directed to me.
  20.  *
  21.  *  I'll be setting up a CVS repository for this class soon.  The more
  22.  *  emails i get concerning this, the more apt i am to do it.
  23.  *
  24.  *  License Information:
  25.  *
  26.  *    Net_DNS:  A resolver library for PHP
  27.  *    Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  28.  *
  29.  *    This library is free software; you can redistribute it and/or
  30.  *    modify it under the terms of the GNU Lesser General Public
  31.  *    License as published by the Free Software Foundation; either
  32.  *    version 2.1 of the License, or (at your option) any later version.
  33.  *
  34.  *    This library is distributed in the hope that it will be useful,
  35.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  37.  *    Lesser General Public License for more details.
  38.  *
  39.  *    You should have received a copy of the GNU Lesser General Public
  40.  *    License along with this library; if not, write to the Free Software
  41.  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  42.  */
  43.  
  44. /* Include information {{{ */
  45.  
  46.     $phpdns_basedir 'Net';
  47.     require_once("$phpdns_basedir/DNS/Header.php");
  48.     require_once("$phpdns_basedir/DNS/Question.php");
  49.     require_once("$phpdns_basedir/DNS/Packet.php");
  50.     require_once("$phpdns_basedir/DNS/Resolver.php");
  51.     require_once("$phpdns_basedir/DNS/RR.php");
  52.  
  53. /* }}} */
  54. /* GLOBAL VARIABLE definitions {{{ */
  55.  
  56. // Used by the Net_DNS_Resolver object to generate an ID
  57.  
  58. $GLOBALS['_Net_DNS_packet_id'mt_rand(065535);
  59.  
  60. /* }}} */
  61. /* Net_DNS object definition (incomplete) {{{ */
  62. /**
  63.  * Initializes a resolver object
  64.  *
  65.  * Net_DNS allows you to query a nameserver for DNS  lookups.  It bypasses the
  66.  * system resolver library  entirely, which allows you to query any nameserver,
  67.  * set your own values for retries, timeouts, recursion,  etc.
  68.  *
  69.  * @author Eric Kilfoil <eric@ypass.net>
  70.  * @package Net_DNS
  71.  * @version 0.01alpha
  72.  */
  73. class Net_DNS
  74. {
  75.     /* class variable definitions {{{ */
  76.     /**
  77.      * A default resolver object created on instantiation
  78.      *
  79.      * @var object Net_DNS_Resolver 
  80.      */
  81.     var $resolver;
  82.     var $VERSION = '1.00b2'// This should probably be a define :(
  83.     var $PACKETSZ = 512;
  84.     var $HFIXEDSZ = 12;
  85.     var $QFIXEDSZ = 4;
  86.     var $RRFIXEDSZ = 10;
  87.     var $INT32SZ = 4;
  88.     var $INT16SZ = 2;
  89.     /* }}} */
  90.     /* class constructor - Net_DNS() {{{ */
  91.     /**
  92.      * Initializes a resolver object
  93.      *
  94.      * @see Net_DNS_Resolver
  95.      */
  96.     function Net_DNS()
  97.     {
  98.         $this->resolver = new Net_DNS_Resolver();
  99.     }
  100.     /* }}} */
  101.     /* Net_DNS::opcodesbyname() {{{ */
  102.     /**
  103.      * Translates opcode names to integers
  104.      *
  105.      * Translates the name of a DNS OPCODE into it's assigned  number
  106.      * listed in RFC1035, RFC1996, or RFC2136. Valid  OPCODES are:
  107.      * <ul>
  108.      *   <li>QUERY
  109.      *   <li>IQUERY
  110.      *   <li>STATUS
  111.      *   <li>NS_NOTIFY_OP
  112.      *   <li>UPDATE
  113.      * <ul>
  114.      *
  115.      * @param   string  $opcode A DNS Packet OPCODE name
  116.      * @return  integer The integer value of an OPCODE
  117.      * @see     Net_DNS::opcodesbyval()
  118.      */
  119.     function opcodesbyname($opcode)
  120.     {
  121.         $op = array(
  122.                 'QUERY'        => 0,   // RFC 1035
  123.                 'IQUERY'       => 1,   // RFC 1035
  124.                 'STATUS'       => 2,   // RFC 1035
  125.                 'NS_NOTIFY_OP' => 4,   // RFC 1996
  126.                 'UPDATE'       => 5,   // RFC 2136
  127.                 );
  128.         if (strlen($op[$opcode])) {
  129.             $op[$opcode= NULL;
  130.         }
  131.         return($op[$opcode]);
  132.     }
  133.  
  134.     /* }}} */
  135.     /* Net_DNS::opcodesbyval() {{{*/
  136.     /**
  137.      * Translates opcode integers into names
  138.      *
  139.      * Translates the integer value of an opcode into it's name
  140.      *
  141.      * @param   integer $opcodeval  A DNS packet opcode integer
  142.      * @return  string  The name of the OPCODE
  143.      * @see     Net_DNS::opcodesbyname()
  144.      */
  145.     function opcodesbyval($opcodeval)
  146.     {
  147.         $opval = array(
  148.                 0 => 'QUERY',
  149.                 1 => 'IQUERY',
  150.                 2 => 'STATUS',
  151.                 4 => 'NS_NOTIFY_OP',
  152.                 5 => 'UPDATE',
  153.                 );
  154.         if (strlen($opval[$opcodeval])) {
  155.             $opval[$opcodeval= NULL;
  156.         }
  157.         return($opval[$opcodeval]);
  158.     }
  159.  
  160.     /*}}}*/
  161.     /* Net_DNS::rcodesbyname() {{{*/
  162.     /**
  163.      * Translates rcode names to integers
  164.      *
  165.      * Translates the name of a DNS RCODE (result code) into it's assigned number.
  166.      * <ul>
  167.      *   <li>NOERROR
  168.      *   <li>FORMERR
  169.      *   <li>SERVFAIL
  170.      *   <li>NXDOMAIN
  171.      *   <li>NOTIMP
  172.      *   <li>REFUSED
  173.      *   <li>YXDOMAIN
  174.      *   <li>YXRRSET
  175.      *   <li>NXRRSET
  176.      *   <li>NOTAUTH
  177.      *   <li>NOTZONE
  178.      * <ul>
  179.      *
  180.      * @param   string  $rcode  A DNS Packet RCODE name
  181.      * @return  integer The integer value of an RCODE
  182.      * @see     Net_DNS::rcodesbyval()
  183.      */
  184.     function rcodesbyname($rcode)
  185.     {
  186.         $rc = array(
  187.                 'NOERROR'   => 0,   // RFC 1035
  188.                 'FORMERR'   => 1,   // RFC 1035
  189.                 'SERVFAIL'  => 2,   // RFC 1035
  190.                 'NXDOMAIN'  => 3,   // RFC 1035
  191.                 'NOTIMP'    => 4,   // RFC 1035
  192.                 'REFUSED'   => 5,   // RFC 1035
  193.                 'YXDOMAIN'  => 6,   // RFC 2136
  194.                 'YXRRSET'   => 7,   // RFC 2136
  195.                 'NXRRSET'   => 8,   // RFC 2136
  196.                 'NOTAUTH'   => 9,   // RFC 2136
  197.                 'NOTZONE'   => 10,    // RFC 2136
  198.                 );
  199.         if (strlen($rc[$rcode])) {
  200.             $rc[$rcode= NULL;
  201.         }
  202.         return($rc[$rcode]);
  203.     }
  204.  
  205.     /*}}}*/
  206.     /* Net_DNS::rcodesbyval() {{{*/
  207.     /**
  208.      * Translates rcode integers into names
  209.      *
  210.      * Translates the integer value of an rcode into it's name
  211.      *
  212.      * @param   integer $rcodeval   A DNS packet rcode integer
  213.      * @return  string  The name of the RCODE
  214.      * @see     Net_DNS::rcodesbyname()
  215.      */
  216.     function rcodesbyval($rcodeval)
  217.     {
  218.         $rc = array(
  219.                 0 => 'NOERROR',
  220.                 1 => 'FORMERR',
  221.                 2 => 'SERVFAIL',
  222.                 3 => 'NXDOMAIN',
  223.                 4 => 'NOTIMP',
  224.                 5 => 'REFUSED',
  225.                 6 => 'YXDOMAIN',
  226.                 7 => 'YXRRSET',
  227.                 8 => 'NXRRSET',
  228.                 9 => 'NOTAUTH',
  229.                 10 => 'NOTZONE',
  230.                 );
  231.         if (strlen($rc[$rcodeval])) {
  232.             $rc[$rcodeval= NULL;
  233.         }
  234.         return($rc[$rcodeval]);
  235.     }
  236.  
  237.     /*}}}*/
  238.     /* Net_DNS::typesbyname() {{{*/
  239.     /**
  240.      * Translates RR type names into integers
  241.      *
  242.      * Translates a Resource Record from it's name to it's  integer value.
  243.      * Valid resource record types are:
  244.      *
  245.      * <ul>
  246.      *   <li>A
  247.      *   <li>NS
  248.      *   <li>MD
  249.      *   <li>MF
  250.      *   <li>CNAME
  251.      *   <li>SOA
  252.      *   <li>MB
  253.      *   <li>MG
  254.      *   <li>MR
  255.      *   <li>NULL
  256.      *   <li>WKS
  257.      *   <li>PTR
  258.      *   <li>HINFO
  259.      *   <li>MINFO
  260.      *   <li>MX
  261.      *   <li>TXT
  262.      *   <li>RP
  263.      *   <li>AFSDB
  264.      *   <li>X25
  265.      *   <li>ISDN
  266.      *   <li>RT
  267.      *   <li>NSAP
  268.      *   <li>NSAP_PTR
  269.      *   <li>SIG
  270.      *   <li>KEY
  271.      *   <li>PX
  272.      *   <li>GPOS
  273.      *   <li>AAAA
  274.      *   <li>LOC
  275.      *   <li>NXT
  276.      *   <li>EID
  277.      *   <li>NIMLOC
  278.      *   <li>SRV
  279.      *   <li>ATMA
  280.      *   <li>NAPTR
  281.      *   <li>TSIG
  282.      *   <li>UINFO
  283.      *   <li>UID
  284.      *   <li>GID
  285.      *   <li>UNSPEC
  286.      *   <li>IXFR
  287.      *   <li>AXFR
  288.      *   <li>MAILB
  289.      *   <li>MAILA
  290.      *   <li>ANY
  291.      * <ul>
  292.      *
  293.      * @param   string  $rrtype A DNS packet RR type name
  294.      * @return  integer The integer value of an RR type
  295.      * @see     Net_DNS::typesbyval()
  296.      */
  297.     function typesbyname($rrtype)
  298.     {
  299.         $rc = array(
  300.                 'A'             => 1,
  301.                 'NS'            => 2,
  302.                 'MD'            => 3,
  303.                 'MF'            => 4,
  304.                 'CNAME'         => 5,
  305.                 'SOA'           => 6,
  306.                 'MB'            => 7,
  307.                 'MG'            => 8,
  308.                 'MR'            => 9,
  309.                 'NULL'          => 10,
  310.                 'WKS'           => 11,
  311.                 'PTR'           => 12,
  312.                 'HINFO'         => 13,
  313.                 'MINFO'         => 14,
  314.                 'MX'            => 15,
  315.                 'TXT'           => 16,
  316.                 'RP'            => 17,
  317.                 'AFSDB'         => 18,
  318.                 'X25'           => 19,
  319.                 'ISDN'          => 20,
  320.                 'RT'            => 21,
  321.                 'NSAP'          => 22,
  322.                 'NSAP_PTR'      => 23,
  323.                 'SIG'           => 24,
  324.                 'KEY'           => 25,
  325.                 'PX'            => 26,
  326.                 'GPOS'          => 27,
  327.                 'AAAA'          => 28,
  328.                 'LOC'           => 29,
  329.                 'NXT'           => 30,
  330.                 'EID'           => 31,
  331.                 'NIMLOC'        => 32,
  332.                 'SRV'           => 33,
  333.                 'ATMA'          => 34,
  334.                 'NAPTR'         => 35,
  335.                 'UINFO'         => 100,
  336.                 'UID'           => 101,
  337.                 'GID'           => 102,
  338.                 'UNSPEC'        => 103,
  339.                 'TSIG'          => 250,
  340.                 'IXFR'          => 251,
  341.                 'AXFR'          => 252,
  342.                 'MAILB'         => 253,
  343.                 'MAILA'         => 254,
  344.                 'ANY'           => 255,
  345.                 );
  346.         if (empty($rc[$rrtype])) {
  347.             $rc[$rrtype= NULL;
  348.         }
  349.         return($rc[$rrtype]);
  350.     }
  351.  
  352.     /*}}}*/
  353.     /* Net_DNS::typesbyval() {{{*/
  354.     /**
  355.      * Translates RR type integers into names
  356.      *
  357.      * Translates the integer value of an RR type into it's name
  358.      *
  359.      * @param   integer $rrtypeval  A DNS packet RR type integer
  360.      * @return  string  The name of the RR type
  361.      * @see     Net_DNS::typesbyname()
  362.      */
  363.     function typesbyval($rrtypeval)
  364.     {
  365.         $rc = array(
  366.                 1 => 'A',
  367.                 2 => 'NS',
  368.                 3 => 'MD',
  369.                 4 => 'MF',
  370.                 5 => 'CNAME',
  371.                 6 => 'SOA',
  372.                 7 => 'MB',
  373.                 8 => 'MG',
  374.                 9 => 'MR',
  375.                 10 => 'NULL',
  376.                 11 => 'WKS',
  377.                 12 => 'PTR',
  378.                 13 => 'HINFO',
  379.                 14 => 'MINFO',
  380.                 15 => 'MX',
  381.                 16 => 'TXT',
  382.                 17 => 'RP',
  383.                 18 => 'AFSDB',
  384.                 19 => 'X25',
  385.                 20 => 'ISDN',
  386.                 21 => 'RT',
  387.                 22 => 'NSAP',
  388.                 23 => 'NSAP_PTR',
  389.                 24 => 'SIG',
  390.                 25 => 'KEY',
  391.                 26 => 'PX',
  392.                 27 => 'GPOS',
  393.                 28 => 'AAAA',
  394.                 29 => 'LOC',
  395.                 30 => 'NXT',
  396.                 31 => 'EID',
  397.                 32 => 'NIMLOC',
  398.                 33 => 'SRV',
  399.                 34 => 'ATMA',
  400.                 35 => 'NAPTR',
  401.                 100 => 'UINFO',
  402.                 101 => 'UID',
  403.                 102 => 'GID',
  404.                 103 => 'UNSPEC',
  405.                 250 => 'TSIG',
  406.                 251 => 'IXFR',
  407.                 252 => 'AXFR',
  408.                 253 => 'MAILB',
  409.                 254 => 'MAILA',
  410.                 255 => 'ANY',
  411.                 );
  412.         $rrtypeval preg_replace(array('/\s*/',' /^0*/')''$rrtypeval);
  413.         if (empty($rc[$rrtypeval])) {
  414.             $rc[$rrtypeval= NULL;
  415.         }
  416.         return($rc[$rrtypeval]);
  417.     }
  418.  
  419.     /*}}}*/
  420.     /* Net_DNS::classesbyname() {{{*/
  421.     /**
  422.      * translates a DNS class from it's name to it's  integer value. Valid
  423.      * class names are:
  424.      * <ul>
  425.      *   <li>IN
  426.      *   <li>CH
  427.      *   <li>HS
  428.      *   <li>NONE
  429.      *   <li>ANY
  430.      * </ul>
  431.      *
  432.      * @param   string  $class  A DNS packet class type
  433.      * @return  integer The integer value of an class type
  434.      * @see     Net_DNS::classesbyval()
  435.      */
  436.     function classesbyname($class)
  437.     {
  438.         $rc = array(
  439.                 'IN'   => 1,   // RFC 1035
  440.                 'CH'   => 3,   // RFC 1035
  441.                 'HS'   => 4,   // RFC 1035
  442.                 'NONE' => 254// RFC 2136
  443.                 'ANY'  => 255  // RFC 1035
  444.                 );
  445.         if (!isset($rc[$class])) {
  446.             $rc[$class= NULL;
  447.         }
  448.         return $rc[$class];
  449.     }
  450.  
  451.     /*}}}*/
  452.     /* Net_DNS::classesbyval() {{{*/
  453.     /**
  454.      * Translates RR class integers into names
  455.      *
  456.      * Translates the integer value of an RR class into it's name
  457.      *
  458.      * @param   integer $classval   A DNS packet RR class integer
  459.      * @return  string  The name of the RR class
  460.      * @see     Net_DNS::classesbyname()
  461.      */
  462.     function classesbyval($classval)
  463.     {
  464.         $rc = array(
  465.                 1 => 'IN',
  466.                 3 => 'CH',
  467.                 4 => 'HS',
  468.                 254 => 'NONE',
  469.                 255 => 'ANY'
  470.                 );
  471.         $classval preg_replace(array('/\s*/',' /^0*/')''$classval);
  472.         if (empty($rc[$classval])) {
  473.             $rc[$classval= NULL;
  474.         }
  475.         return($rc[$classval]);
  476.     }
  477.  
  478.     /*}}}*/
  479.     /* not completed - Net_DNS::mx() {{{*/
  480.     /*}}}*/
  481.     /* not completed - Net_DNS::yxrrset() {{{*/
  482.     /*}}}*/
  483.     /* not completed - Net_DNS::nxrrset() {{{*/
  484.     /*}}}*/
  485.     /* not completed - Net_DNS::yxdomain() {{{*/
  486.     /*}}}*/
  487.     /* not completed - Net_DNS::nxdomain() {{{*/
  488.     /*}}}*/
  489.     /* not completed - Net_DNS::rr_add() {{{*/
  490.     /*}}}*/
  491.     /* not completed - Net_DNS::rr_del() {{{*/
  492.     /*}}}*/
  493. }
  494. /* }}} */
  495. /* VIM Settings {{{
  496.  * Local variables:
  497.  * tab-width: 4
  498.  * c-basic-offset: 4
  499.  * soft-stop-width: 4
  500.  * c indent on
  501.  * End:
  502.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  503.  * vim<600: sw=4 ts=4
  504.  * }}} */
  505. ?>

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