home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Resolver.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-24  |  18.6 KB  |  696 lines

  1. package Net::DNS::Resolver;
  2. #
  3. # $Id: Resolver.pm,v 2.101 2004/02/17 05:21:03 ctriv Exp $
  4. #
  5.  
  6. use strict;
  7. use vars qw($VERSION @ISA);
  8.  
  9. $VERSION = (qw$Revision: 2.101 $)[1];
  10.  
  11. BEGIN {
  12.     if ($^O eq 'MSWin32') {
  13.         require Net::DNS::Resolver::Win32;
  14.         @ISA = qw(Net::DNS::Resolver::Win32);
  15.     } elsif ($^O eq 'cygwin') {
  16.          require Net::DNS::Resolver::Cygwin;
  17.          @ISA = qw(Net::DNS::Resolver::Cygwin);
  18.     } else {   
  19.         require Net::DNS::Resolver::UNIX;
  20.         @ISA = qw(Net::DNS::Resolver::UNIX);
  21.     }
  22. }
  23.  
  24. __PACKAGE__->init();
  25.  
  26. 1;
  27.  
  28. __END__
  29.  
  30. =head1 NAME
  31.  
  32. Net::DNS::Resolver - DNS resolver class
  33.  
  34. =head1 SYNOPSIS
  35.  
  36.   use Net::DNS;
  37.   
  38.   my $res = Net::DNS::Resolver->new;
  39.   
  40.   # Perform a lookup, using the searchlist if appropriate.
  41.   my $answer = $res->search('example.com');
  42.   
  43.   # Perform a lookup, without the searchlist
  44.   my $answer = $res->query('example.com', 'MX');
  45.   
  46.   # Perform a lookup, without pre or post-processing
  47.   my $answer = $res->send('example.com', 'MX', 'CH');
  48.   
  49.   # Send a prebuilt packet
  50.   my $packet = Net::DNS::Packet->new(...);
  51.   my $answer = $res->send($packet);
  52.   
  53. =head1 DESCRIPTION
  54.  
  55. Instances of the C<Net::DNS::Resolver> class represent resolver objects.
  56. A program can have multiple resolver objects, each maintaining its
  57. own state information such as the nameservers to be queried, whether
  58. recursion is desired, etc.
  59.  
  60. =head1 METHODS
  61.  
  62. =head2 new
  63.  
  64.   # Use the system defaults
  65.   my $res = Net::DNS::Resolver->new;
  66.   
  67.   # Use my own configuration file
  68.   my $res = Net::DNS::Resolver->new(config_file => '/my/dns.conf');
  69.   
  70.   # Set options in the constructor
  71.   my $res = Net::DNS::Resolver->new(
  72.       nameservers => [qw(10.1.1.128 10.1.2.128)],
  73.       recurse     => 0,
  74.       debug       => 1,
  75.   );
  76.  
  77. Returns a resolver object.  If given no arguments, C<new()> returns an
  78. object configured to your system's defaults.  On UNIX systems the 
  79. defaults are read from the following files, in the order indicated:
  80.  
  81.     /etc/resolv.conf
  82.     $HOME/.resolv.conf
  83.     ./.resolv.conf
  84.  
  85. The following keywords are recognized in resolver configuration files:
  86.  
  87. =over 4
  88.  
  89. =item domain
  90.  
  91. The default domain.
  92.  
  93. =item search
  94.  
  95. A space-separated list of domains to put in the search list.
  96.  
  97. =item nameserver
  98.  
  99. A space-separated list of nameservers to query.
  100.  
  101. =back
  102.  
  103. Files except for F</etc/resolv.conf> must be owned by the effective
  104. userid running the program or they won't be read.  In addition, several
  105. environment variables can also contain configuration information; see
  106. L</ENVIRONMENT>.
  107.  
  108. On Windows systems, an attempt is made to determine the system defaults
  109. using the registry.  This is still a work in progress; systems with many
  110. dynamically configured network interfaces may confuse Net::DNS.
  111.  
  112. You can include a configuration file of your own when creating a
  113. resolver object:
  114.  
  115.  # Use my own configuration file 
  116.  my $res = Net::DNS::Resolver->new(config_file => '/my/dns.conf');
  117.  
  118. This is supported on both UNIX and Windows.  Values pulled from a custom
  119. configuration file override the the system's defaults, but can still be
  120. overridden by the other arguments to new().
  121.  
  122. Explicit arguments to new override both the system's defaults and the
  123. values of the custom configuration file, if any.  The following
  124. arguments to new() are supported:
  125.  
  126. =over 4
  127.  
  128. =item nameservers
  129.  
  130. An array reference of nameservers to query.  
  131.  
  132. =item searchlist
  133.  
  134. An array reference of domains.
  135.  
  136. =item recurse
  137.  
  138. =item debug
  139.  
  140. =item domain
  141.  
  142. =item port
  143.  
  144. =item srcaddr
  145.  
  146. =item srcport
  147.  
  148. =item tcp_timeout
  149.  
  150. =item udp_timeout
  151.  
  152. =item retrans
  153.  
  154. =item retry
  155.  
  156. =item usecv
  157.  
  158. =item stayopen
  159.  
  160. =item igntc
  161.  
  162. =item defnames
  163.  
  164. =item dnsrch
  165.  
  166. =item persistent_tcp
  167.  
  168. =item persistent_udp
  169.  
  170. =item dnssec
  171.  
  172. =back
  173.  
  174. For more information on any of these options, please consult the method
  175. of the same name.
  176.  
  177. =head2 print
  178.  
  179.     $res->print;
  180.  
  181. Prints the resolver state on the standard output.
  182.  
  183. =head2 string
  184.  
  185.     print $res->string;
  186.  
  187. Returns a string representation of the resolver state.
  188.  
  189. =head2 searchlist
  190.  
  191.     @searchlist = $res->searchlist;
  192.     $res->searchlist('example.com', 'a.example.com', 'b.example.com');
  193.  
  194. Gets or sets the resolver search list.
  195.  
  196. =head2 nameservers
  197.  
  198.     @nameservers = $res->nameservers;
  199.     $res->nameservers('192.168.1.1', '192.168.2.2', '192.168.3.3');
  200.  
  201. Gets or sets the nameservers to be queried.
  202.  
  203. =head2 port
  204.  
  205.     print 'sending queries to port ', $res->port, "\n";
  206.     $res->port(9732);
  207.  
  208. Gets or sets the port to which we send queries.  This can be useful
  209. for testing a nameserver running on a non-standard port.  The
  210. default is port 53.
  211.  
  212. =head2 srcport
  213.  
  214.     print 'sending queries from port ', $res->srcport, "\n";
  215.     $res->srcport(5353);
  216.  
  217. Gets or sets the port from which we send queries.  The default is 0,
  218. meaning any port.
  219.  
  220. =head2 srcaddr
  221.  
  222.     print 'sending queries from address ', $res->srcaddr, "\n";
  223.     $res->srcaddr('192.168.1.1');
  224.  
  225. Gets or sets the source address from which we send queries.  Convenient
  226. for forcing queries out a specific interfaces on a multi-homed host.
  227. The default is 0.0.0.0, meaning any local address.
  228.  
  229. =head2 search
  230.  
  231.     $packet = $res->search('mailhost');
  232.     $packet = $res->search('mailhost.example.com');
  233.     $packet = $res->search('192.168.1.1');
  234.     $packet = $res->search('example.com', 'MX');
  235.     $packet = $res->search('user.passwd.example.com', 'TXT', 'HS');
  236.  
  237. Performs a DNS query for the given name, applying the searchlist
  238. if appropriate.  The search algorithm is as follows:
  239.  
  240. =over 4
  241.  
  242. =item 1.
  243.  
  244. If the name contains at least one dot, try it as is.
  245.  
  246. =item 2.
  247.  
  248. If the name doesn't end in a dot then append each item in
  249. the search list to the name.  This is only done if B<dnsrch>
  250. is true.
  251.  
  252. =item 3.
  253.  
  254. If the name doesn't contain any dots, try it as is.
  255.  
  256. =back
  257.  
  258. The record type and class can be omitted; they default to A and
  259. IN.  If the name looks like an IP address (4 dot-separated numbers),
  260. then an appropriate PTR query will be performed.
  261.  
  262. Returns a C<Net::DNS::Packet> object, or C<undef> if no answers
  263. were found.
  264.  
  265. =head2 query
  266.  
  267.     $packet = $res->query('mailhost');
  268.     $packet = $res->query('mailhost.example.com');
  269.     $packet = $res->query('192.168.1.1');
  270.     $packet = $res->query('example.com', 'MX');
  271.     $packet = $res->query('user.passwd.example.com', 'TXT', 'HS');
  272.  
  273. Performs a DNS query for the given name; the search list is not
  274. applied.  If the name doesn't contain any dots and B<defnames>
  275. is true then the default domain will be appended.
  276.  
  277. The record type and class can be omitted; they default to A and
  278. IN.  If the name looks like an IP address (4 dot-separated numbers),
  279. then an appropriate PTR query will be performed.
  280.  
  281. Returns a C<Net::DNS::Packet> object, or C<undef> if no answers
  282. were found.
  283.  
  284. =head2 send
  285.  
  286.     $packet = $res->send($packet_object);
  287.     $packet = $res->send('mailhost.example.com');
  288.     $packet = $res->send('example.com', 'MX');
  289.     $packet = $res->send('user.passwd.example.com', 'TXT', 'HS');
  290.  
  291. Performs a DNS query for the given name.  Neither the searchlist
  292. nor the default domain will be appended.
  293.  
  294. The argument list can be either a C<Net::DNS::Packet> object or a list
  295. of strings.  The record type and class can be omitted; they default to
  296. A and IN.  If the name looks like an IP address (4 dot-separated numbers),
  297. then an appropriate PTR query will be performed.
  298.  
  299. Returns a C<Net::DNS::Packet> object whether there were any answers or not.
  300. Use C<< $packet->header->ancount >> or C<< $packet->answer >> to find out
  301. if there were any records in the answer section.  Returns C<undef> if there
  302. was an error.
  303.  
  304. =head2 bgsend
  305.  
  306.     $socket = $res->bgsend($packet_object);
  307.     $socket = $res->bgsend('mailhost.example.com');
  308.     $socket = $res->bgsend('example.com', 'MX');
  309.     $socket = $res->bgsend('user.passwd.example.com', 'TXT', 'HS');
  310.  
  311. Performs a background DNS query for the given name, i.e., sends a
  312. query packet to the first nameserver listed in C<< $res->nameservers >>
  313. and returns immediately without waiting for a response.  The program
  314. can then perform other tasks while waiting for a response from the
  315. nameserver.
  316.  
  317. The argument list can be either a C<Net::DNS::Packet> object or a list
  318. of strings.  The record type and class can be omitted; they default to
  319. A and IN.  If the name looks like an IP address (4 dot-separated numbers),
  320. then an appropriate PTR query will be performed.
  321.  
  322. Returns an C<IO::Socket::INET> object.  The program must determine when
  323. the socket is ready for reading and call C<< $res->bgread >> to get
  324. the response packet.  You can use C<< $res->bgisready >> or C<IO::Select>
  325. to find out if the socket is ready before reading it.
  326.  
  327. =head2 bgread
  328.  
  329.     $packet = $res->bgread($socket);
  330.     undef $socket;
  331.  
  332. Reads the answer from a background query (see L</bgsend>).  The argument
  333. is an C<IO::Socket> object returned by C<bgsend>.
  334.  
  335. Returns a C<Net::DNS::Packet> object or C<undef> on error.
  336.  
  337. The programmer should close or destroy the socket object after reading it.
  338.  
  339. =head2 bgisready
  340.  
  341.     $socket = $res->bgsend('foo.example.com');
  342.     until ($res->bgisready($socket)) {
  343.         # do some other processing
  344.     }
  345.     $packet = $res->bgread($socket);
  346.     $socket = undef;
  347.  
  348. Determines whether a socket is ready for reading.  The argument is
  349. an C<IO::Socket> object returned by C<< $res->bgsend >>.
  350.  
  351. Returns true if the socket is ready, false if not.
  352.  
  353. =head2 axfr
  354.  
  355.     @zone = $res->axfr;
  356.     @zone = $res->axfr('example.com');
  357.     @zone = $res->axfr('passwd.example.com', 'HS');
  358.  
  359. Performs a zone transfer from the first nameserver listed in C<nameservers>.
  360. If the zone is omitted, it defaults to the first zone listed in the resolver's
  361. search list.  If the class is omitted, it defaults to IN.
  362.  
  363. Returns a list of C<Net::DNS::RR> objects, or C<undef> if the zone
  364. transfer failed.
  365.  
  366. The redundant SOA record that terminates the zone transfer is not
  367. returned to the caller.
  368.  
  369. See also L</axfr_start> and L</axfr_next>.
  370.  
  371. Here's an example that uses a timeout:
  372.  
  373.     $res->tcp_timeout(10);
  374.     my @zone = $res->axfr('example.com');
  375.  
  376.     if (@zone) {
  377.         foreach my $rr (@zone) {
  378.             $rr->print;
  379.         }
  380.     } else {
  381.         print 'Zone transfer failed: ', $res->errorstring, "\n";
  382.     }
  383.  
  384. =head2 axfr_start
  385.  
  386.     $res->axfr_start;
  387.     $res->axfr_start('example.com');
  388.     $res->axfr_start('example.com', 'HS');
  389.  
  390. Starts a zone transfer from the first nameserver listed in C<nameservers>.
  391. If the zone is omitted, it defaults to the first zone listed in the resolver's
  392. search list.  If the class is omitted, it defaults to IN.
  393.  
  394. B<IMPORTANT>:
  395.  
  396. This method currently returns the C<IO::Socket::INET> object that will
  397. be used for reading, or C<undef> on error.  DO NOT DEPEND ON C<axfr_next()>
  398. returning a socket object.  THIS WILL CHANGE in future releases.
  399.  
  400. Use C<axfr_next> to read the zone records one at a time.
  401.  
  402. =head2 axfr_next
  403.  
  404.     $res->axfr_start('example.com');
  405.  
  406.     while (my $rr = $res->axfr_next) {
  407.         $rr->print;
  408.     }
  409.  
  410. Reads records from a zone transfer one at a time.
  411.  
  412. Returns C<undef> at the end of the zone transfer.  The redundant
  413. SOA record that terminates the zone transfer is not returned.
  414.  
  415. See also L</axfr>.
  416.  
  417. =head2 tsig
  418.  
  419.     my $tsig = $res->tsig;
  420.  
  421.     $res->tsig(Net::DNS::RR->new("$key_name TSIG $key"));
  422.  
  423.     $tsig = Net::DNS::RR->new("$key_name TSIG $key");
  424.     $tsig->fudge(60);
  425.     $res->tsig($tsig);
  426.  
  427.     $res->tsig($key_name, $key);
  428.  
  429.     $res->tsig(0);
  430.  
  431. Get or set the TSIG record used to automatically sign outgoing
  432. queries and updates.  Call with an argument of 0 or '' to turn off
  433. automatic signing.
  434.  
  435. The default resolver behavior is not to sign any packets.  You must
  436. call this method to set the key if you'd like the resolver to sign
  437. packets automatically.
  438.  
  439. You can also sign packets manually -- see the C<Net::DNS::Packet>
  440. and C<Net::DNS::Update> manual pages for examples.  TSIG records
  441. in manually-signed packets take precedence over those that the
  442. resolver would add automatically.
  443.  
  444. =head2 retrans
  445.  
  446.     print 'retrans interval: ', $res->retrans, "\n";
  447.     $res->retrans(3);
  448.  
  449. Get or set the retransmission interval.  The default is 5.
  450.  
  451. =head2 retry
  452.  
  453.     print 'number of tries: ', $res->retry, "\n";
  454.     $res->retry(2);
  455.  
  456. Get or set the number of times to try the query.  The default is 4.
  457.  
  458. =head2 recurse
  459.  
  460.     print 'recursion flag: ', $res->recurse, "\n";
  461.     $res->recurse(0);
  462.  
  463. Get or set the recursion flag.  If this is true, nameservers will
  464. be requested to perform a recursive query.  The default is true.
  465.  
  466. =head2 defnames
  467.  
  468.     print 'defnames flag: ', $res->defnames, "\n";
  469.     $res->defnames(0);
  470.  
  471. Get or set the defnames flag.  If this is true, calls to B<query> will
  472. append the default domain to names that contain no dots.  The default
  473. is true.
  474.  
  475. =head2 dnsrch
  476.  
  477.     print 'dnsrch flag: ', $res->dnsrch, "\n";
  478.     $res->dnsrch(0);
  479.  
  480. Get or set the dnsrch flag.  If this is true, calls to B<search> will
  481. apply the search list.  The default is true.
  482.  
  483. =head2 debug
  484.  
  485.     print 'debug flag: ', $res->debug, "\n";
  486.     $res->debug(1);
  487.  
  488. Get or set the debug flag.  If set, calls to B<search>, B<query>,
  489. and B<send> will print debugging information on the standard output.
  490. The default is false.
  491.  
  492. =head2 usevc
  493.  
  494.     print 'usevc flag: ', $res->usevc, "\n";
  495.     $res->usevc(1);
  496.  
  497. Get or set the usevc flag.  If true, then queries will be performed
  498. using virtual circuits (TCP) instead of datagrams (UDP).  The default
  499. is false.
  500.  
  501. =head2 tcp_timeout
  502.  
  503.     print 'TCP timeout: ', $res->tcp_timeout, "\n";
  504.     $res->tcp_timeout(10);
  505.  
  506. Get or set the TCP timeout in seconds.  A timeout of C<undef> means
  507. indefinite.  The default is 120 seconds (2 minutes).
  508.  
  509. =head2 udp_timeout
  510.  
  511.     print 'UDP timeout: ', $res->udp_timeout, "\n";
  512.     $res->udp_timeout(10);
  513.  
  514. Get or set the UDP timeout in seconds.  A timeout of C<undef> means
  515. the retry and retrans settings will be just utilized to perform the
  516. retries until they are exhausted.  The default is C<undef>.
  517.  
  518. =head2 persistent_tcp
  519.  
  520.     print 'Persistent TCP flag: ', $res->persistent_tcp, "\n";
  521.     $res->persistent_tcp(1);
  522.  
  523. Get or set the persistent TCP setting.  If set to true, Net::DNS
  524. will keep a TCP socket open for each host:port to which it connects.
  525. This is useful if you're using TCP and need to make a lot of queries
  526. or updates to the same nameserver.
  527.  
  528. This option defaults to false unless you're running under a
  529. SOCKSified Perl, in which case it defaults to true.
  530.  
  531. =head2 persistent_udp
  532.  
  533.     print 'Persistent UDP flag: ', $res->persistent_udp, "\n";
  534.     $res->persistent_udp(1);
  535.  
  536. Get or set the persistent UDP setting.  If set to true, Net::DNS
  537. will keep a single UDP socket open for all queries.
  538. This is useful if you're using UDP and need to make a lot of queries
  539. or updates.
  540.  
  541. =head2 igntc
  542.  
  543.     print 'igntc flag: ', $res->igntc, "\n";
  544.     $res->igntc(1);
  545.  
  546. Get or set the igntc flag.  If true, truncated packets will be
  547. ignored.  If false, truncated packets will cause the query to
  548. be retried using TCP.  The default is false.
  549.  
  550. =head2 errorstring
  551.  
  552.     print 'query status: ', $res->errorstring, "\n";
  553.  
  554. Returns a string containing the status of the most recent query.
  555.  
  556. =head2 answerfrom
  557.  
  558.     print 'last answer was from: ', $res->answerfrom, "\n";
  559.  
  560. Returns the IP address from which we received the last answer in
  561. response to a query.
  562.  
  563. =head2 answersize
  564.  
  565.     print 'size of last answer: ', $res->answersize, "\n";
  566.  
  567. Returns the size in bytes of the last answer we received in
  568. response to a query.
  569.  
  570.  
  571. =head2 dnssec
  572.  
  573.     print "dnssec flag: ", $res->dnssec, "\n";
  574.     $res->dnssec(0);
  575.  
  576. Enabled DNSSEC this will set the checking disabled flag in the query header
  577. and add EDNS0 data as in RFC2671 and RFC3225
  578.  
  579. When set to true the answer and additional section of queries from
  580. secured zones will contain KEY, NXT and SIG records.
  581.  
  582.  
  583. =head2 cdflag
  584.  
  585.     print "checking disabled flag: ", $res->dnssec, "\n";
  586.     $res->dnssec(1);
  587.     $res->cdflag(1);
  588.  
  589. Sets or gets the CD bit for a dnssec query.  This bit is always zero
  590. for non dnssec queries. When the dnssec is enabled the flag can be set
  591. to 1.
  592.  
  593. =head2 udppacketsize
  594.  
  595.     print "udppacketsize: ", $res->udppacketsize, "\n";
  596.     $res->udppacketsize(2048);
  597.  
  598. udppacketsize will set or get the packet size. If set to a value greater than 
  599. &Net::DNS::PACKETSZ an EDNS extension will be added indicating suppport for MTU path recovery.
  600.  
  601. Default udppacketsize is &Net::DNS::PACKETSZ (512)
  602.  
  603. =head1 CUSTOMIZING
  604.  
  605. Net::DNS::Resolver is actually an empty subclass.  At compile time a
  606. super class is chosen based on the current platform.  A side benefit of
  607. this allows for easy modification of the methods in Net::DNS::Resolver. 
  608. You simply add a method to the namespace!  
  609.  
  610. For example, if we wanted to cache lookups:
  611.  
  612.  package Net::DNS::Resolver;
  613.  
  614.  my %cache;
  615.  
  616.  sub search {
  617.     my ($self, @args) = @_;
  618.     
  619.     return $cache{@args} ||= $self->SUPER::search(@args);
  620.  } 
  621.  
  622.  
  623. =head1 ENVIRONMENT
  624.  
  625. The following environment variables can also be used to configure
  626. the resolver:
  627.  
  628. =head2 RES_NAMESERVERS
  629.  
  630.     # Bourne Shell
  631.     RES_NAMESERVERS="192.168.1.1 192.168.2.2 192.168.3.3"
  632.     export RES_NAMESERVERS
  633.  
  634.     # C Shell
  635.     setenv RES_NAMESERVERS "192.168.1.1 192.168.2.2 192.168.3.3"
  636.  
  637. A space-separated list of nameservers to query.
  638.  
  639. =head2 RES_SEARCHLIST
  640.  
  641.     # Bourne Shell
  642.     RES_SEARCHLIST="example.com sub1.example.com sub2.example.com"
  643.     export RES_SEARCHLIST
  644.  
  645.     # C Shell
  646.     setenv RES_SEARCHLIST "example.com sub1.example.com sub2.example.com"
  647.  
  648. A space-separated list of domains to put in the search list.
  649.  
  650. =head2 LOCALDOMAIN
  651.  
  652.     # Bourne Shell
  653.     LOCALDOMAIN=example.com
  654.     export LOCALDOMAIN
  655.  
  656.     # C Shell
  657.     setenv LOCALDOMAIN example.com
  658.  
  659. The default domain.
  660.  
  661. =head2 RES_OPTIONS
  662.  
  663.     # Bourne Shell
  664.     RES_OPTIONS="retrans:3 retry:2 debug"
  665.     export RES_OPTIONS
  666.  
  667.     # C Shell
  668.     setenv RES_OPTIONS "retrans:3 retry:2 debug"
  669.  
  670. A space-separated list of resolver options to set.  Options that
  671. take values are specified as I<option>:I<value>.
  672.  
  673. =head1 BUGS
  674.  
  675. Error reporting and handling needs to be improved.
  676.  
  677. The current implementation supports TSIG only on outgoing packets.
  678. No validation of server replies is performed.
  679.  
  680. =head1 COPYRIGHT
  681.  
  682. Copyright (c) 1997-2002 Michael Fuhr. 
  683.  
  684. Portions Copyright (c) 2002-2003 Chris Reinhardt.
  685.  
  686. All rights reserved.  This program is free software; you may redistribute
  687. it and/or modify it under the same terms as Perl itself.
  688.  
  689. =head1 SEE ALSO
  690.  
  691. L<perl(1)>, L<Net::DNS>, L<Net::DNS::Packet>, L<Net::DNS::Update>,
  692. L<Net::DNS::Header>, L<Net::DNS::Question>, L<Net::DNS::RR>,
  693. L<resolver(5)>, RFC 1035, RFC 1034 Section 4.3.5
  694.  
  695. =cut
  696.