.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Net::DNS::Nameserver 3pm" .TH Net::DNS::Nameserver 3pm "2011-11-15" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Net::DNS::Nameserver \- DNS server class .SH "SYNOPSIS" .IX Header "SYNOPSIS" \&\f(CW\*(C`use Net::DNS::Nameserver;\*(C'\fR .SH "DESCRIPTION" .IX Header "DESCRIPTION" Instances of the \f(CW\*(C`Net::DNS::Nameserver\*(C'\fR class represent \s-1DNS\s0 server objects. See \*(L"\s-1EXAMPLE\s0\*(R" for an example. .SH "METHODS" .IX Header "METHODS" .SS "new" .IX Subsection "new" .Vb 6 \& my $ns = Net::DNS::Nameserver\->new( \& LocalAddr => "10.1.2.3", \& LocalPort => "5353", \& ReplyHandler => \e&reply_handler, \& Verbose => 1 \& ); \& \& \& \& my $ns = Net::DNS::Nameserver\->new( \& LocalAddr => [\*(Aq::1\*(Aq , \*(Aq127.0.0.1\*(Aq ], \& LocalPort => "5353", \& ReplyHandler => \e&reply_handler, \& Verbose => 1, \& Truncate => 0, \& ); .Ve .PP Creates a nameserver object. Attributes are: .PP .Vb 11 \& 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. \& NotifyHandler Reference to reply\-handling \& subroutine for queries with \& opdcode NS_NOTIFY (RFC1996) \& Verbose Print info about received \& queries. Defaults to 0 (off). \& Truncate Truncates UDP packets that \& are to big for the reply Defaults to 1 (on) .Ve .PP The LocalAddr attribute may alternatively be specified as a list of \s-1IP\s0 addresses to listen to. .PP If IO::Socket::INET6 and Socket6 are available on the system you can also list IPv6 addresses and the default is '0' (listen on all interfaces on IPv6 and IPv4); .PP The ReplyHandler subroutine is passed the query name, query class, query type and optionally an argument containing the peerhost, the incoming query, and the name of the incoming socket (sockethost). It must return the response code and references to the answer, authority, and additional sections of the response. Common response codes are: .PP .Vb 6 \& NOERROR No error \& FORMERR Format error \& SERVFAIL Server failure \& NXDOMAIN Non\-existent domain (name doesn\*(Aqt exist) \& NOTIMP Not implemented \& REFUSED Query refused .Ve .PP For advanced usage it may also contain a headermaks containing an hashref with the settings for the \f(CW\*(C`aa\*(C'\fR, \f(CW\*(C`ra\*(C'\fR, and \f(CW\*(C`ad\*(C'\fR header bits. The argument is of the form \&\f(CW\*(C`{ ad => 1, aa => 0, ra => 1 }\*(C'\fR. .PP See \s-1RFC\s0 1035 and the \s-1IANA\s0 dns-parameters file for more information: .PP .Vb 2 \& ftp://ftp.rfc\-editor.org/in\-notes/rfc1035.txt \& http://www.isi.edu/in\-notes/iana/assignments/dns\-parameters .Ve .PP The nameserver will listen for both \s-1UDP\s0 and \s-1TCP\s0 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. .PP Packet Truncation is new functionality for \&\f(CW$Net::DNS::Nameserver::VERSION\fR>830 and uses the Net::DNS::Packet::truncate method with a size determinde by the advertised \s-1EDNS0\s0 size in the query, or 512 if \s-1EDNS0\s0 is not advertised in the query. Only \s-1UDP\s0 replies are truncated. If you want to do packet runcation yourself you should set Truncate to 0 and use the truncate method on the reply packet in the code you use for the ReplyHandler. .PP Returns a Net::DNS::Nameserver object, or undef if the object couldn't be created. .PP See \*(L"\s-1EXAMPLE\s0\*(R" for an example. .SS "main_loop" .IX Subsection "main_loop" .Vb 1 \& $ns\->main_loop; .Ve .PP Start accepting queries. Calling main_loop never returns. .SS "loop_once" .IX Subsection "loop_once" .Vb 1 \& $ns\->loop_once( [TIMEOUT_IN_SECONDS] ); .Ve .PP Start accepting queries, but returns. If called without a parameter, the call will not return until a request has been received (and replied to). If called with a number, that number specifies how many seconds (even fractional) to maximum wait before returning. If called with 0 it will return immediately unless there's something to do. .PP Handling a request and replying obviously depends on the speed of ReplyHandler. Assuming ReplyHandler is super fast, loop_once should spend just a fraction of a second, if called with a timeout value of 0 seconds. One exception is when an \s-1AXFR\s0 has requested a huge amount of data that the \s-1OS\s0 is not ready to receive in full. In that case, it will keep running through a loop (while servicing new requests) until the reply has been sent. .PP In case loop_once accepted a \s-1TCP\s0 connection it will immediatly check if there is data to be read from the socket. If not it will return and you will have to call \fIloop_once()\fR again to check if there is any data waiting on the socket to be processed. In most cases you will have to count on calling \*(L"loop_once\*(R" twice. .PP A code fragment like: \f(CW$ns\fR\->loop_once(10); while( \f(CW$ns\fR\->\fIget_open_tcp()\fR ){ \f(CW$ns\fR\->\fIloop_once\fR\|(0); } .PP Would wait for 10 seconds for the initial connection and would then process all \s-1TCP\s0 sockets until none is left. .SS "get_open_tcp" .IX Subsection "get_open_tcp" In scalar context returns the number of \s-1TCP\s0 connections for which state is maintained. In array context it returns IO::Socket objects, these could be useful for troubleshooting but be careful using them. .SH "EXAMPLE" .IX Header "EXAMPLE" The following example will listen on port 5353 and respond to all queries for A records with the \s-1IP\s0 address 10.1.2.3. All other queries will be answered with \s-1NXDOMAIN\s0. Authority and additional sections are left empty. The \f(CW$peerhost\fR variable catches the \s-1IP\s0 address of the peer host, so that additional filtering on its basis may be applied. .PP .Vb 1 \& #!/usr/bin/perl \& \& use Net::DNS::Nameserver; \& use strict; \& use warnings; \& \& sub reply_handler { \& my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; \& my ($rcode, @ans, @auth, @add); \& \& print "Received query from $peerhost to ". $conn\->{"sockhost"}. "\en"; \& $query\->print; \& \& \& if ($qtype eq "A" && $qname eq "foo.example.com" ) { \& my ($ttl, $rdata) = (3600, "10.1.2.3"); \& push @ans, Net::DNS::RR\->new("$qname $ttl $qclass $qtype $rdata"); \& $rcode = "NOERROR"; \& }elsif( $qname eq "foo.example.com" ) { \& $rcode = "NOERROR"; \& \& }else{ \& $rcode = "NXDOMAIN"; \& } \& \& \& # mark the answer as authoritive (by setting the \*(Aqaa\*(Aq flag \& return ($rcode, \e@ans, \e@auth, \e@add, { aa => 1 }); \& } \& \& my $ns = Net::DNS::Nameserver\->new( \& LocalPort => 5353, \& ReplyHandler => \e&reply_handler, \& Verbose => 1, \& ) || die "couldn\*(Aqt create nameserver object\en"; \& \& $ns\->main_loop; .Ve .SH "BUGS" .IX Header "BUGS" Limitations in perl 5.8.6 makes it impossible to guarantee that replies to \s-1UDP\s0 queries from Net::DNS::Nameserver are sent from the IP-address they were received on. This is a problem for machines with multiple IP-addresses and causes violation of \s-1RFC2181\s0 section 4. Thus a \s-1UDP\s0 socket created listening to \s-1INADDR_ANY\s0 (all available IP-addresses) will reply not necessarily with the source address being the one to which the request was sent, but rather with the address that the operating system choses. This is also often called \*(L"the closest address\*(R". This should really only be a problem on a server which has more than one IP-address (besides localhost \- any experience with IPv6 complications here, would be nice). If this is a problem for you, a work-around would be to not listen to \s-1INADDR_ANY\s0 but to specify each address that you want this module to listen on. A separate set of sockets will then be created for each IP-address. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 1997\-2002 Michael Fuhr. .PP Portions Copyright (c) 2002\-2004 Chris Reinhardt. .PP Portions Copyright (c) 2005\-2009 O.M, Kolkman, \s-1RIPE\s0 \s-1NCC\s0. .PP Portions Copyright (c) 2005 Robert Martin-Legene. .PP All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIperl\fR\|(1), Net::DNS, Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Update, Net::DNS::Header, Net::DNS::Question, Net::DNS::RR, \s-1RFC\s0 1035