ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/DNS.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/DNS.pm (file contents):
Revision 1.39 by root, Thu May 29 03:45:37 2008 UTC vs.
Revision 1.46 by root, Thu May 29 06:53:00 2008 UTC

14=head1 DESCRIPTION 14=head1 DESCRIPTION
15 15
16This module offers both a number of DNS convenience functions as well 16This module offers both a number of DNS convenience functions as well
17as a fully asynchronous and high-performance pure-perl stub resolver. 17as a fully asynchronous and high-performance pure-perl stub resolver.
18 18
19The stub resolver supports DNS over UDP, optional EDNS0 support for up to 19The stub resolver supports DNS over IPv4 and IPv6, UDP and TCP, optional
204kiB datagrams and automatically falls back to virtual circuit mode for 20EDNS0 support for up to 4kiB datagrams and automatically falls back to
21large responses. 21virtual circuit mode for large responses.
22 22
23=head2 CONVENIENCE FUNCTIONS 23=head2 CONVENIENCE FUNCTIONS
24 24
25=over 4 25=over 4
26 26
187 187
188=item $AnyEvent::DNS::EDNS0 188=item $AnyEvent::DNS::EDNS0
189 189
190This variable decides whether dns_pack automatically enables EDNS0 190This variable decides whether dns_pack automatically enables EDNS0
191support. By default, this is disabled (C<0>), unless overridden by 191support. By default, this is disabled (C<0>), unless overridden by
192C<$ENV{PERL_ANYEVENT_EDNS0>), but when set to C<1>, AnyEvent::DNS will use 192C<$ENV{PERL_ANYEVENT_EDNS0}>, but when set to C<1>, AnyEvent::DNS will use
193EDNS0 in all requests. 193EDNS0 in all requests.
194 194
195=cut 195=cut
196 196
197our $EDNS0 = $ENV{PERL_ANYEVENT_EDNS0} * 1; # set to 1 to enable (partial) edns0 197our $EDNS0 = $ENV{PERL_ANYEVENT_EDNS0} * 1; # set to 1 to enable (partial) edns0
270 "*" => 255, 270 "*" => 255,
271); 271);
272 272
273our %class_str = reverse %class_id; 273our %class_str = reverse %class_id;
274 274
275# names MUST have a trailing dot
276sub _enc_name($) { 275sub _enc_name($) {
277 pack "(C/a*)*", (split /\./, shift), "" 276 pack "(C/a*)*", (split /\./, shift), ""
278} 277}
279 278
280sub _enc_qd() { 279sub _enc_qd() {
340 + $rcode_id{$req->{rc}} * 0x0001, 339 + $rcode_id{$req->{rc}} * 0x0001,
341 340
342 scalar @{ $req->{qd} || [] }, 341 scalar @{ $req->{qd} || [] },
343 scalar @{ $req->{an} || [] }, 342 scalar @{ $req->{an} || [] },
344 scalar @{ $req->{ns} || [] }, 343 scalar @{ $req->{ns} || [] },
345 $EDNS0 + scalar @{ $req->{ar} || [] }, # include EDNS0 option here 344 $EDNS0 + scalar @{ $req->{ar} || [] }, # EDNS0 option included here
346 345
347 (join "", map _enc_qd, @{ $req->{qd} || [] }), 346 (join "", map _enc_qd, @{ $req->{qd} || [] }),
348 (join "", map _enc_rr, @{ $req->{an} || [] }), 347 (join "", map _enc_rr, @{ $req->{an} || [] }),
349 (join "", map _enc_rr, @{ $req->{ns} || [] }), 348 (join "", map _enc_rr, @{ $req->{ns} || [] }),
350 (join "", map _enc_rr, @{ $req->{ar} || [] }), 349 (join "", map _enc_rr, @{ $req->{ar} || [] }),
351 350
352 ($EDNS0 ? pack "C nnNn", 0, 41, MAX_PKT, 0, 0 : "") # EDNS0, 4kiB udp payload size 351 ($EDNS0 ? pack "C nnNn", 0, 41, MAX_PKT, 0, 0 : "") # EDNS0 option
353} 352}
354 353
355our $ofs; 354our $ofs;
356our $pkt; 355our $pkt;
357 356
560 559
561=over 4 560=over 4
562 561
563=item server => [...] 562=item server => [...]
564 563
565A list of server addresses (default: C<v127.0.0.1>) in network format (4 564A list of server addresses (default: C<v127.0.0.1>) in network format
566octets for IPv4, 16 octets for IPv6 - not yet supported). 565(i.e. as returned by C<AnyEvent::Socket::parse_address> - both IPv4 and
566IPv6 are supported).
567 567
568=item timeout => [...] 568=item timeout => [...]
569 569
570A list of timeouts to use (also determines the number of retries). To make 570A list of timeouts to use (also determines the number of retries). To make
571three retries with individual time-outs of 2, 5 and 5 seconds, use C<[2, 571three retries with individual time-outs of 2, 5 and 5 seconds, use C<[2,
580The number of dots (default: C<1>) that a name must have so that the resolver 580The number of dots (default: C<1>) that a name must have so that the resolver
581tries to resolve the name without any suffixes first. 581tries to resolve the name without any suffixes first.
582 582
583=item max_outstanding => $integer 583=item max_outstanding => $integer
584 584
585Most name servers do not handle many parallel requests very well. This option 585Most name servers do not handle many parallel requests very well. This
586limits the number of outstanding requests to C<$n> (default: C<10>), that means 586option limits the number of outstanding requests to C<$integer>
587if you request more than this many requests, then the additional requests will be queued 587(default: C<10>), that means if you request more than this many requests,
588until some other requests have been resolved. 588then the additional requests will be queued until some other requests have
589been resolved.
589 590
590=item reuse => $seconds 591=item reuse => $seconds
591 592
592The number of seconds (default: C<300>) that a query id cannot be re-used 593The number of seconds (default: C<300>) that a query id cannot be re-used
593after a timeout. If there as no time-out then query id's can be reused 594after a timeout. If there as no time-out then query id's can be reused
957 $self->_scheduler; 958 $self->_scheduler;
958} 959}
959 960
960=item $resolver->resolve ($qname, $qtype, %options, $cb->($rcode, @rr)) 961=item $resolver->resolve ($qname, $qtype, %options, $cb->($rcode, @rr))
961 962
962Queries the DNS for the given domain name C<$qname> of type C<$qtype> (a 963Queries the DNS for the given domain name C<$qname> of type C<$qtype>.
963qtype of "*" is supported and means "any"). 964
965A C<$qtype> is either a numerical query type (e.g. C<1> for A recods) or
966a lowercase name (you have to look at the source to see which aliases are
967supported, but all types from RFC 1034, C<aaaa>, C<srv>, C<spf> and a few
968more are known to this module). A qtype of "*" is supported and means
969"any" record type.
964 970
965The callback will be invoked with a list of matching result records or 971The callback will be invoked with a list of matching result records or
966none on any error or if the name could not be found. 972none on any error or if the name could not be found.
967 973
968CNAME chains (although illegal) are followed up to a length of 8. 974CNAME chains (although illegal) are followed up to a length of 8.
975
976The callback will be invoked with an result code in string form (noerror,
977formerr, servfail, nxdomain, notimp, refused and so on), or numerical
978form if the result code is not supported. The remaining arguments are
979arraryefs of the form C<[$name, $type, $class, @data>], where C<$name> is
980the domain name, C<$type> a type string or number, C<$class> a class name
981and @data is resource-record-dependent data. For C<a> records, this will
982be the textual IPv4 addresses, for C<ns> or C<cname> records this will be
983a domain name, for C<txt> records these are all the strings and so on.
984
985All types mentioned in RFC 1034, C<aaaa>, C<srv> and C<spf> are
986decoded. All resource records not known to this module will just return
987the raw C<rdata> field as fourth entry.
969 988
970Note that this resolver is just a stub resolver: it requires a name server 989Note that this resolver is just a stub resolver: it requires a name server
971supporting recursive queries, will not do any recursive queries itself and 990supporting recursive queries, will not do any recursive queries itself and
972is not secure when used against an untrusted name server. 991is not secure when used against an untrusted name server.
973 992
984 1003
985=item accept => [$type...] 1004=item accept => [$type...]
986 1005
987Lists the acceptable result types: only result types in this set will be 1006Lists the acceptable result types: only result types in this set will be
988accepted and returned. The default includes the C<$qtype> and nothing 1007accepted and returned. The default includes the C<$qtype> and nothing
989else. 1008else. If this list includes C<cname>, then CNAME-chains will not be
1009followed (because you asked for the CNAME record).
990 1010
991=item class => "class" 1011=item class => "class"
992 1012
993Specify the query class ("in" for internet, "ch" for chaosnet and "hs" for 1013Specify the query class ("in" for internet, "ch" for chaosnet and "hs" for
994hesiod are the only ones making sense). The default is "in", of course. 1014hesiod are the only ones making sense). The default is "in", of course.
995 1015
996=back 1016=back
997 1017
998Examples: 1018Examples:
999 1019
1000 $res->resolve ("ruth.plan9.de", "a", sub { 1020 # full example, you can paste this into perl:
1001 warn Dumper [@_]; 1021 use Data::Dumper;
1002 }); 1022 use AnyEvent::DNS;
1023 AnyEvent::DNS::resolver->resolve (
1024 "google.com", "*", my $cv = AnyEvent->condvar);
1025 warn Dumper [$cv->recv];
1003 1026
1027 # shortened result:
1004 [ 1028 # [
1029 # [ 'google.com', 'soa', 'in', 'ns1.google.com', 'dns-admin.google.com',
1030 # 2008052701, 7200, 1800, 1209600, 300 ],
1005 [ 1031 # [
1006 'ruth.schmorp.de', 1032 # 'google.com', 'txt', 'in',
1007 'a', 1033 # 'v=spf1 include:_netblocks.google.com ~all'
1008 'in', 1034 # ],
1009 '129.13.162.95' 1035 # [ 'google.com', 'a', 'in', '64.233.187.99' ],
1036 # [ 'google.com', 'mx', 'in', 10, 'smtp2.google.com' ],
1037 # [ 'google.com', 'ns', 'in', 'ns2.google.com' ],
1010 ] 1038 # ]
1039
1040 # resolve a records:
1041 $res->resolve ("ruth.plan9.de", "a", sub { warn Dumper [@_] });
1042
1043 # result:
1044 # [
1045 # [ 'ruth.schmorp.de', 'a', 'in', '129.13.162.95' ]
1011 ] 1046 # ]
1012 1047
1048 # resolve any records, but return only a and aaaa records:
1013 $res->resolve ("test1.laendle", "*", 1049 $res->resolve ("test1.laendle", "*",
1014 accept => ["a", "aaaa"], 1050 accept => ["a", "aaaa"],
1015 sub { 1051 sub {
1016 warn Dumper [@_]; 1052 warn Dumper [@_];
1017 } 1053 }
1018 ); 1054 );
1019 1055
1020 [ 1056 # result:
1021 [ 1057 # [
1022 'test1.laendle', 1058 # [ 'test1.laendle', 'a', 'in', '10.0.0.255' ],
1023 'a', 1059 # [ 'test1.laendle', 'aaaa', 'in', '3ffe:1900:4545:0002:0240:0000:0000:f7e1' ]
1024 'in',
1025 '10.0.0.255'
1026 ],
1027 [
1028 'test1.laendle',
1029 'aaaa',
1030 'in',
1031 '3ffe:1900:4545:0002:0240:0000:0000:f7e1'
1032 ] 1060 # ]
1033 ]
1034 1061
1035=cut 1062=cut
1036 1063
1037sub resolve($%) { 1064sub resolve($%) {
1038 my $cb = pop; 1065 my $cb = pop;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines