The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::DNS::Nameserver - DNS server class

SYNOPSIS

use Net::DNS::Nameserver;

DESCRIPTION

Instances of the Net::DNS::Nameserver class represent simple DNS server objects. See "EXAMPLE" for an example.

METHODS

new

    my $ns = Net::DNS::Nameserver->new(
        LocalAddr    => "10.1.2.3",
        LocalPort    => "5353",
        ReplyHandler => \&reply_handler,
        Verbose      => 1
    );

Creates a nameserver object. Attributes are:

  LocalAddr     IP address on which to listen.  Defaults to INADDR_ANY.
  LocalPort     Port on which to listen.  Defaults to 53.
  ReplyHandler  Reference to reply-handling subroutine.  Required.
  Verbose       Print info about received queries.  Defaults to 0 (off).

The ReplyHandler subroutine is passed the query name, query class, and query type. It must return the response code and references to the answer, authority, and additional sections of the response. Common response codes are:

  NOERROR   No error
  FORMERR   Format error
  SERVFAIL  Server failure
  NXDOMAIN  Non-existent domain (name doesn't exist)
  NOTIMP    Not implemented
  REFUSED   Query refused

See RFC 1035 and the IANA dns-parameters file for more information:

  ftp://ftp.rfc-editor.org/in-notes/rfc1035.txt
  http://www.isi.edu/in-notes/iana/assignments/dns-parameters

The nameserver will listen for both UDP and TCP connections. On Unix-like systems, the program will probably have to run as root to listen on the default port, 53. A non-privileged user should be able to listen on ports 1024 and higher.

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

See "EXAMPLE" for an example.

main_loop

    $ns->main_loop;

Start accepting queries.

EXAMPLE

The following example will listen on port 5353 and respond to all queries for A records with the IP address 10.1.2.3. All other queries will be answered with NXDOMAIN. Authority and additional sections are left empty.

  #!/usr/bin/perl -Tw
  
  use Net::DNS;
  use strict;
  
  sub reply_handler {
      my ($qname, $qclass, $qtype) = @_;
      my ($rcode, @ans, @auth, @add);
  
      if ($qtype eq "A") {
        my ($ttl, $rdata) = (3600, "10.1.2.3");
        push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
        $rcode = "NOERROR";
      }
      else {
        $rcode = "NXDOMAIN";
      }
        
      return ($rcode, \@ans, \@auth, \@add);
  }

  my $ns = Net::DNS::Nameserver->new(
      LocalPort    => 5353,
      ReplyHandler => \&reply_handler,
      Verbose      => 1
  );
  
  if ($ns) {
      $ns->main_loop;
  }
  else {
      die "couldn't create nameserver object\n";
  }

BUGS

Net::DNS::Nameserver objects can handle only one query at a time.

COPYRIGHT

Copyright (c) 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.

SEE ALSO

perl(1), Net::DNS, Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Header, Net::DNS::Question, Net::DNS::RR, RFC 1035