1Net::DNS::Nameserver(3)User Contributed Perl DocumentatioNnet::DNS::Nameserver(3)
2
3
4

NAME

6       Net::DNS::Nameserver - DNS server class
7

SYNOPSIS

9       "use Net::DNS::Nameserver;"
10

DESCRIPTION

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

METHODS

16       new
17
18        my $ns = Net::DNS::Nameserver->new(
19               LocalAddr        => "10.1.2.3",
20               LocalPort        => "5353",
21               ReplyHandler => \&reply_handler,
22               Verbose          => 1
23        );
24
25        my $ns = Net::DNS::Nameserver->new(
26               LocalAddr        => ['::1' , '127.0.0.1' ],
27               LocalPort        => "5353",
28               ReplyHandler => \&reply_handler,
29               Verbose          => 1
30        );
31
32       Creates a nameserver object.  Attributes are:
33
34         LocalAddr             IP address on which to listen.  Defaults to INADDR_ANY.
35         LocalPort             Port on which to listen.        Defaults to 53.
36         ReplyHandler          Reference to reply-handling
37                               subroutine                      Required.
38         Verbose               Print info about received
39                               queries.                        Defaults to 0 (off).
40
41       The LocalAddr attribute may alternatively be specified as a list of IP
42       addresses to listen to.
43
44       If IO::Socket::INET6 and Socket6 are available on the system you can
45       also list IPv6 addresses and the default is '0' (listen on all inter‐
46       faces on IPv6 and IPv4);
47
48       The ReplyHandler subroutine is passed the query name, query class,
49       query type and optionally an argument containing header bit settings
50       (see below).  It must return the response code and references to the
51       answer, authority, and additional sections of the response.  Common
52       response codes are:
53
54         NOERROR       No error
55         FORMERR       Format error
56         SERVFAIL      Server failure
57         NXDOMAIN      Non-existent domain (name doesn't exist)
58         NOTIMP        Not implemented
59         REFUSED       Query refused
60
61       For advanced usage there is an optional argument containing an hashref
62       with the settings for the "aa", "ra", and "ad" header bits. The argu‐
63       ment is of the form "{ ad => 1, aa => 0, ra => 1 }".
64
65       See RFC 1035 and the IANA dns-parameters file for more information:
66
67         ftp://ftp.rfc-editor.org/in-notes/rfc1035.txt
68         http://www.isi.edu/in-notes/iana/assignments/dns-parameters
69
70       The nameserver will listen for both UDP and TCP connections.  On Unix-
71       like systems, the program will probably have to run as root to listen
72       on the default port, 53.  A non-privileged user should be able to lis‐
73       ten on ports 1024 and higher.
74
75       Returns a Net::DNS::Nameserver object, or undef if the object couldn't
76       be created.
77
78       See "EXAMPLE" for an example.
79
80       main_loop
81
82               $ns->main_loop;
83
84       Start accepting queries. Calling main_loop never returns.
85
86       get_open_tcp
87
88       In scalar context returns the number of TCP connections for which state
89       is maintained. In array context it returns IO::Socket objects, these
90       could be useful for troubleshooting but be careful using them.
91

EXAMPLE

93       The following example will listen on port 5353 and respond to all
94       queries for A records with the IP address 10.1.2.3.   All other queries
95       will be answered with NXDOMAIN.   Authority and additional sections are
96       left empty.  The $peerhost variable catches the IP address of the peer
97       host, so that additional filtering on its basis may be applied.
98
99        #!/usr/bin/perl
100
101        use Net::DNS::Nameserver;
102        use strict;
103        use warnings;
104
105        sub reply_handler {
106                my ($qname, $qclass, $qtype, $peerhost) = @_;
107                my ($rcode, @ans, @auth, @add);
108
109                if ($qtype eq "A" && qname eq "foo.example.com" ) {
110                        my ($ttl, $rdata) = (3600, "10.1.2.3");
111                        push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
112                        $rcode = "NOERROR";
113                }elsif( qname eq "foo.example.com" ) {
114                        $rcode = "NOERROR";
115
116                }else{
117                         $rcode = "NXDOMAIN";
118                }
119
120                # mark the answer as authoritive (by setting the 'aa' flag
121                return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
122        }
123
124        my $ns = Net::DNS::Nameserver->new(
125            LocalPort    => 5353,
126            ReplyHandler => \&reply_handler,
127            Verbose      => 1,
128        ) ⎪⎪ die "couldn't create nameserver object\n";
129
130        $ns->main_loop;
131

BUGS

133       Limitations in perl 5.8.6 makes it impossible to guarantee that replies
134       to UDP queries from Net::DNS::Nameserver are sent from the IP-address
135       they were received on. This is a problem for machines with multiple IP-
136       addresses and causes violation of RFC2181 section 4.  Thus a UDP socket
137       created listening to INADDR_ANY (all available IP-addresses) will reply
138       not necessarily with the source address being the one to which the
139       request was sent, but rather with the address that the operating system
140       choses. This is also often called "the closest address". This should
141       really only be a problem on a server which has more than one IP-address
142       (besides localhost - any experience with IPv6 complications here, would
143       be nice). If this is a problem for you, a work-around would be to not
144       listen to INADDR_ANY but to specify each address that you want this
145       module to listen on. A seperate set of sockets will then be created for
146       each IP-address.
147
149       Copyright (c) 1997-2002 Michael Fuhr.
150
151       Portions Copyright (c) 2002-2004 Chris Reinhardt.
152
153       Portions Copyright (c) 2005 O.M, Kolkman, RIPE NCC.
154
155       Portions Copyright (c) 2005 Robert Martin-Legene.
156
157       All rights reserved.  This program is free software; you may redis‐
158       tribute it and/or modify it under the same terms as Perl itself.
159

SEE ALSO

161       perl(1), Net::DNS, Net::DNS::Resolver, Net::DNS::Packet,
162       Net::DNS::Update, Net::DNS::Header, Net::DNS::Question, Net::DNS::RR,
163       RFC 1035
164
165
166
167perl v5.8.8                       2007-08-01           Net::DNS::Nameserver(3)
Impressum