Rob Brown > Net-DNS-0.19 > Net::DNS

Download:
Net-DNS-0.19.tar.gz

Dependencies

Annotate this POD

Related Modules

Net::SMTP
Net::SNMP
Data::Dumper
LWP::Simple
CGI::Carp
Net::Ping
Mail::Sendmail
Net::Whois
Thread::Queue
Net::hostent
more...
By perlmonks.org

CPAN RT

New  3
Open  7
Stalled  6
View/Report Bugs
Module Version: 0.19   Source   Latest Release: Net-DNS-0.78

NAME ^

Net::DNS - Perl interface to the DNS resolver

SYNOPSIS ^

use Net::DNS;

DESCRIPTION ^

Net::DNS is a collection of Perl modules that acts as a Domain Name System (DNS) resolver. It allows the programmer to perform DNS queries that are beyond the capabilities of gethostbyname and gethostbyaddr.

The programmer should be familiar with the format of a DNS packet and its various sections. See RFC 1035 or DNS and BIND (Albitz & Liu) for details.

Programmers interested in using Net::DNS to perform dynamic updates should be familiar with RFC 2136. Those interested in performing signed (TSIG) transactions should be familiar with RFC 2845.

Resolver Objects

A resolver object is an instance of the Net::DNS::Resolver class. A program can have multiple resolver objects, each maintaining its own state information such as the nameservers to be queried, whether recursion is desired, etc.

Packet Objects

Net::DNS::Resolver queries return Net::DNS::Packet objects. Packet objects have five sections:

The Net::DNS::Update package is a front-end to Net::DNS::Packet for creating packet objects to be used in dynamic updates.

Header Objects

Net::DNS::Header objects represent the header section of a DNS packet.

Question Objects

Net::DNS::Question objects represent the question section of a DNS packet.

RR Objects

Net::DNS::RR is the base class for DNS resource record (RR) objects in the answer, authority, and additional sections of a DNS packet.

Don't assume that RR objects will be of the type you requested -- always check an RR object's type before calling any of its methods.

METHODS ^

See the manual pages listed above for other class-specific methods.

version

    print Net::DNS->version, "\n";

Returns the version of Net::DNS.

mx

    # Use a default resolver -- can't get an error string this way.
    use Net::DNS;
    @mx = mx("example.com");

    # Use your own resolver object.
    use Net::DNS;
    $res = Net::DNS::Resolver->new;
    @mx = mx($res, "example.com");

Returns a list of Net::DNS::RR::MX objects representing the MX records for the specified name; the list will be sorted by preference. Returns an empty list if the query failed or no MX records were found.

This method does not look up A records -- it only performs MX queries.

See "EXAMPLES" for a more complete example.

yxrrset

Use this method to add an "RRset exists" prerequisite to a dynamic update packet. There are two forms, value-independent and value-dependent:

    # RRset exists (value-independent)
    $packet->push("pre", yxrrset("foo.example.com A"));

Meaning: At least one RR with the specified name and type must exist.

    # RRset exists (value-dependent)
    $packet->push("pre", yxrrset("foo.example.com A 10.1.2.3"));

Meaning: At least one RR with the specified name and type must exist and must have matching data.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

nxrrset

Use this method to add an "RRset does not exist" prerequisite to a dynamic update packet.

    $packet->push("pre", nxrrset("foo.example.com A"));

Meaning: No RRs with the specified name and type can exist.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

yxdomain

Use this method to add a "name is in use" prerequisite to a dynamic update packet.

    $packet->push("pre", yxdomain("foo.example.com"));

Meaning: At least one RR with the specified name must exist.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

nxdomain

Use this method to add a "name is not in use" prerequisite to a dynamic update packet.

    $packet->push("pre", nxdomain("foo.example.com"));

Meaning: No RR with the specified name can exist.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

rr_add

Use this method to add RRs to a zone.

    $packet->push("update", rr_add("foo.example.com A 10.1.2.3"));

Meaning: Add this RR to the zone.

RR objects created by this method should be added to the "update" section of a dynamic update packet. The TTL defaults to 86400 seconds (24 hours) if not specified.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

rr_del

Use this method to delete RRs from a zone. There are three forms: delete an RRset, delete all RRsets, and delete an RR.

    # Delete an RRset.
    $packet->push("update", rr_del("foo.example.com A"));

Meaning: Delete all RRs having the specified name and type.

    # Delete all RRsets.
    $packet->push("update", rr_del("foo.example.com"));

Meaning: Delete all RRs having the specified name.

    # Delete an RR.
    $packet->push("update", rr_del("foo.example.com A 10.1.2.3"));

Meaning: Delete all RRs having the specified name, type, and data.

RR objects created by this method should be added to the "update" section of a dynamic update packet.

Returns a Net::DNS::RR object or undef if the object couldn't be created.

EXAMPLES ^

The following examples show how to use the Net::DNS modules. See the other manual pages and the demo scripts included with the source code for additional examples.

Many of the examples are intentionally simple and don't do things like follow CNAME records. Such code is left as an exercise for the reader.

See the Net::DNS::Update manual page for examples of performing dynamic updates. RFC 2136 describes dynamic updates.

See the Net::DNS::Packet and Net::DNS::Update manual pages for examples of performing signed queries and updates. RFC 2845 describes signing DNS transactions with TSIG records.

Look up a host's address from its name.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $query = $res->search("foo.example.com");
  if ($query) {
      foreach $rr ($query->answer) {
          next unless $rr->type eq "A";
          print $rr->address, "\n";
      }
  }
  else {
      print "query failed: ", $res->errorstring, "\n";
  }

Look up a host's name from its address.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $query = $res->query("10.1.2.3");
  if ($query) {
      foreach $rr ($query->answer) {
          next unless $rr->type eq "PTR";
          print $rr->ptrdname, "\n";
      }
  }
  else {
      print "query failed: ", $res->errorstring, "\n";
  }

Find the nameservers for a domain.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $query = $res->query("example.com", "NS");
  if ($query) {
      foreach $rr ($query->answer) {
          next unless $rr->type eq "NS";
          print $rr->nsdname, "\n";
      }
  }
  else {
      print "query failed: ", $res->errorstring, "\n";
  }

Find the MX records for a domain.

  #!/usr/bin/perl -w
  use Net::DNS;
  $name = "example.com";
  $res = Net::DNS::Resolver->new;
  @mx = mx($res, $name);
  if (@mx) {
      foreach $rr (@mx) {
          print $rr->preference, " ", $rr->exchange, "\n";
      }
  }
  else {
      print "can't find MX records for $name: ", $res->errorstring, "\n";
  }

Print a domain's SOA record in zone file format.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $query = $res->query("example.com", "SOA");
  if ($query) {
      ($query->answer)[0]->print;
  }
  else {
      print "query failed: ", $res->errorstring, "\n";
  }

Perform a zone transfer and print all the records.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $res->nameservers("ns.example.com");
  @zone = $res->axfr("example.com");
  foreach $rr (@zone) {
      $rr->print;
  }

Perform a background query and do some other work while waiting for the answer.

  #!/usr/bin/perl -w
  use Net::DNS;
  $res = Net::DNS::Resolver->new;
  $socket = $res->bgsend("foo.example.com");
  until ($res->bgisready($socket)) {
      # do some work here while waiting for the answer
      # ...and some more here
  }
  $packet = $res->bgread($socket);
  $packet->print;

Send a background query and use select to determine when the answer has arrived.

  #!/usr/bin/perl -w
  use Net::DNS;
  use IO::Select;
  $timeout = 5;
  $res = Net::DNS::Resolver->new;
  $bgsock = $res->bgsend("foo.example.com");
  $sel = IO::Select($bgsock)->new;
  # Add more sockets to $sel if desired.
  @ready = $sel->can_read($timeout);
  if (@ready) {
      foreach $sock (@ready) {
          if ($sock == $bgsock) {
              $packet = $res->bgread($bgsock);
              $packet->print;
              $bgsock = undef;
          }
          # Check for the other sockets.
          $sel->remove($sock);
          $sock = undef;
      }
  }
  else {
      print "timed out after $timeout seconds\n";
  }

BUGS ^

Net::DNS is slow. Real slow.

Net::DNS is crippled on Microsoft systems because Win32 Perl has only a limited socket implementation. UDP queries seem to be problematic so TCP is the defualt. TCP timeouts may not work.

On some systems, TCP queries result in the error "Resource temporarily unavailable." Setting $res->tcp_timeout(undef) seems to work around this problem.

For other known issues, please see the TODO file included with the source distribution.

COPYRIGHT ^

Copyright (c) 1997-2000 Michael Fuhr. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR INFORMATION ^

Michael Fuhr <mike@fuhr.org> http://www.fuhr.org/~mfuhr/perldns/

SEE ALSO ^

perl(1), Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Header, Net::DNS::Question, Net::DNS::RR, RFC 1035, RFC 2136, RFC 2845, DNS and BIND by Paul Albitz & Cricket Liu

syntax highlighting: