blib/lib/AnyEvent/DNS/Nameserver.pm

filter: Statementstmt 13.4% 14 / 104 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 4 / 8 Podpod 0.0% 0 / 3 totaltotal 9.0% 18 / 200 CC 40 SCAR 57.4
CC
40
Cov
8%
make_reply
552.0
__ANON__
110.0
new
90.0
CRAP 310.9
↑ Summary
1 package AnyEvent::DNS::Nameserver;
2 1 our $VERSION = "1.2";
3 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AnyEvent::Handle::UDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Socket qw(sockaddr_in sockaddr_in6 inet_ntop sockaddr_family AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod undocumented
9 0 my $class = shift;
10 0 my %p = @_;
11 0 my $self = {};
12 0 ▶ $self->{LocalAddr} = $p{LocalAddr} || '0.0.0.0';
Condition0%0/2
$p{'LocalAddr'} || '0.0.0.0'
l!l
00
MC/DC0%0/1
$p{'LocalAddr'} || '0.0.0.0'
$p{'LocalAddr'}
Truth table0%0/2
A
A= $p{'LocalAddr'}
Aresult
11
00
13 0 ▶ $self->{LocalPort} = $p{LocalPort} || 53;
Condition0%0/2
$p{'LocalPort'} || 53
l!l
00
MC/DC0%0/1
$p{'LocalPort'} || 53
$p{'LocalPort'}
Truth table0%0/2
A
A= $p{'LocalPort'}
Aresult
11
00
14 0 ▶ $self->{ReplyHandler} = $p{ReplyHandler} or die "ReplyHandler invalid\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ReplyHandler"} = $p{"ReplyHandler"} or die "ReplyHandler invalid\n" 0 0 0
15 0 ▶ $self->{Verbose} = $p{Verbose} || 0;
Condition0%0/2
$p{'Verbose'} || 0
l!l
00
MC/DC0%0/1
$p{'Verbose'} || 0
$p{'Verbose'}
Truth table0%0/2
A
A= $p{'Verbose'}
Aresult
11
00
16 0 ▶ $self->{Truncate} = $p{Truncate} || 1;
Condition0%0/2
$p{'Truncate'} || 1
l!l
00
MC/DC0%0/1
$p{'Truncate'} || 1
$p{'Truncate'}
Truth table0%0/2
A
A= $p{'Truncate'}
Aresult
11
00
17 0 ▶ $self->{IdleTimeout} = $p{IdleTimeout} || 120;
Condition0%0/2
$p{'IdleTimeout'} || 120
l!l
00
MC/DC0%0/1
$p{'IdleTimeout'} || 120
$p{'IdleTimeout'}
Truth table0%0/2
A
A= $p{'IdleTimeout'}
Aresult
11
00
18 0 $self->{NotifyHandler} = $p{NotifyHandler};
19  
20 0 $self->{watchers} = [];
21  
22 0 ▶ my @LocalAddr =ref $self->{LocalAddr} eq 'ARRAY'?@{$self->{LocalAddr}}:($self->{LocalAddr});
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'LocalAddr'} eq 'ARRAY' ? : 0 0 0
23 0 for my $la (@LocalAddr){
24 0 my $hdl;$hdl = AnyEvent::Handle::UDP->new(
25 bind => [$la,$self->{LocalPort}],
26 0 ▶ on_recv => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
27 0 my ($data, $ae_handle, $client_addr) = @_;
28 0 my $family = sockaddr_family($client_addr);
29 0 ▶ my ($peerport, $peerhost) = ( $family == AF_INET6 ) ? sockaddr_in6($client_addr) : sockaddr_in($client_addr);
Branch: 1 branch
BranchTrueFalseTotal
$family == 10 ? : 0 0 0
30 0 $peerhost = inet_ntop($family, $peerhost);
31 0 my $query = new Net::DNS::Packet( \$data );
32 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
33 0 ▶ print "Error decoding query packet: $err\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
34 0 undef $query;
35 }
36 my $conn = {
37 sockhost=>$la,
38 sockport=>$self->{LocalPort},
39 0 peerhost=>$peerhost,
40 peerport=>$peerport,
41 };
42 0 ▶ print "UDP connection from $peerhost:$peerport to $conn->{sockhost}:$conn->{sockport}\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
43 0 ▶ my $reply = make_reply($self,$query,$peerhost,$conn) || return;
Condition0%0/2
make_reply($self, $query, $peerhost, $conn) || (return)
l!l
00
MC/DC0%0/1
make_reply($self, $query, $peerhost, $conn) || (return)
make_reply($self, $query, $peerhost, $conn)
Truth table0%0/2
A
A= make_reply($self, $query, $peerhost, $conn)
Aresult
11
00
44 0 ▶ my $max_len = ( $query && $self->{Truncate} ) ? $query->edns->size : undef;
Branch: 1 branch
BranchTrueFalseTotal
$query && $self->{'Truncate'} ? : 0 0 0
Condition0%0/3
$query and $self->{"Truncate"}
!ll&&!rl&&r
000
MC/DC0%0/2
$query and $self->{"Truncate"}
$query $self->{"Truncate"}
Truth table0%0/3
A and B
A= $queryB= $self->{"Truncate"}
ABresult
0X0
100
111
45 0 ▶ if ( $self->{Verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Verbose"}) 0 0 0
46 0 local $| = 1;
47 0 ▶ print "Maximum UDP size advertised by $peerhost:$peerport: $max_len bytes\n" if $max_len;
Branch: 1 branch
BranchTrueFalseTotal
if $max_len 0 0 0
48 0 print "Sending response to $peerhost:$peerport\n";
49 0 $reply->print ;
50 }
51 0 $ae_handle->push_send($reply->data($max_len), $client_addr);
52 },
53 0 );
54 0 push @{$self->{watchers}},$hdl;
55 }
56 0 return bless $self,$class;
57 }
58  
59 #copy from Net::DNS::Nameserver
60 0 ▶sub make_reply {
Subroutine make_reply: not called CC=23 SCAR=63.1
Pod undocumented
61 0 my ( $self, $query, $peerhost, $conn ) = @_;
62
63 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
64 0 ▶ print "ERROR: invalid packet\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
65 0 my $empty = new Net::DNS::Packet(); # create empty reply packet
66 0 my $reply = $empty->reply();
67 0 $reply->header->rcode("FORMERR");
68 0 return $reply;
69 }
70
71 0 ▶ if ( $query->header->qr() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->header->qr) 0 0 0
72 0 ▶ print "ERROR: invalid packet (qr set), dropping\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
73 0 return;
74 }
75
76 0 my $reply = $query->reply();
77 0 my $header = $reply->header;
78 0 my $headermask;
79
80 0 my $opcode = $query->header->opcode;
81 0 my $qdcount = $query->header->qdcount;
82
83 0 ▶ unless ($qdcount) {
Branch: 2 branches
BranchTrueFalseTotal
if ($qdcount) { } 0 0 0
if (do { $qdcount > 1 }) { } 0 0 0
84 0 $header->rcode("NOERROR");
85
86 } elsif ( $qdcount > 1 ) {
87 0 ▶ print "ERROR: qdcount $qdcount unsupported\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
88 0 $header->rcode("FORMERR");
89
90 } else {
91 0 my ($qr) = $query->question;
92 0 my $qname = $qr->qname;
93 0 my $qtype = $qr->qtype;
94 0 my $qclass = $qr->qclass;
95
96 0 my $id = $query->header->id;
97 0 ▶ $query->print if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
98
99 0 my ( $rcode, $ans, $auth, $add );
100 0 my @arglist = ( $qname, $qclass, $qtype, $peerhost, $query, $conn );
101
102 0 ▶ if ( $opcode eq "QUERY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opcode eq 'QUERY') { } 0 0 0
elsif ($opcode eq 'NOTIFY') { } 0 0 0
103 ( $rcode, $ans, $auth, $add, $headermask ) =
104 0 &{$self->{ReplyHandler}}(@arglist);
105
106 } elsif ( $opcode eq "NOTIFY" ) { #RFC1996
107 0 ▶ if ( ref $self->{NotifyHandler} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'NotifyHandler'} eq 'CODE') { } 0 0 0
108 ( $rcode, $ans, $auth, $add, $headermask ) =
109 0 &{$self->{NotifyHandler}}(@arglist);
110 } else {
111 0 $rcode = "NOTIMP";
112 }
113
114 } else {
115 0 ▶ print "ERROR: opcode $opcode unsupported\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
116 0 $rcode = "FORMERR";
117 }
118
119 0 ▶ if ( !defined($rcode) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rcode) 0 0 0
120 0 ▶ print "remaining silent\n" if $self->{Verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Verbose"} 0 0 0
121 0 return undef;
122 }
123
124 0 $header->rcode($rcode);
125
126 0 ▶ $reply->{answer} = [@$ans] if $ans;
Branch: 1 branch
BranchTrueFalseTotal
if $ans 0 0 0
127 0 ▶ $reply->{authority} = [@$auth] if $auth;
Branch: 1 branch
BranchTrueFalseTotal
if $auth 0 0 0
128 0 ▶ $reply->{additional} = [@$add] if $add;
Branch: 1 branch
BranchTrueFalseTotal
if $add 0 0 0
129 }
130
131 0 ▶ if ( !defined($headermask) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $headermask) { } 0 0 0
132 0 $header->ra(1);
133 0 $header->ad(0);
134 } else {
135 0 ▶ $header->opcode( $headermask->{opcode} ) if $headermask->{opcode};
Branch: 1 branch
BranchTrueFalseTotal
if $headermask->{"opcode"} 0 0 0
136
137 0 ▶ $header->aa(1) if $headermask->{aa};
Branch: 1 branch
BranchTrueFalseTotal
if $headermask->{"aa"} 0 0 0
138 0 ▶ $header->ra(1) if $headermask->{ra};
Branch: 1 branch
BranchTrueFalseTotal
if $headermask->{"ra"} 0 0 0
139 0 ▶ $header->ad(1) if $headermask->{ad};
Branch: 1 branch
BranchTrueFalseTotal
if $headermask->{"ad"} 0 0 0
140 }
141 0 return $reply;
142 }
143  
144 0 ▶sub main_loop{
Subroutine main_loop: not called CC=1 SCAR=6.9
Pod undocumented
145 0 my $self = shift;
146 0 AE::cv->recv;
147 }
148 1 1;
149 =pod
150  
151 =head1 NAME
152  
153 AnyEvent::DNS::Nameserver - DNS server class using AnyEvent
154  
155 =head1 SYNOPSIS
156  
157 use AnyEvent::DNS::Nameserver;
158 my $nameserver = new Net::DNS::Nameserver(
159 LocalAddr => ['192.168.1.1' , '127.0.0.1' ],
160 LocalPort => "53",
161 ReplyHandler => \&reply_handler,
162 Verbose => 1,
163 Truncate => 0
164 );
165 $nameserver->main_loop;
166  
167 =head1 DESCRIPTION
168  
169 Net::DNS::Nameserver doesn't work with AnyEvent so I wrote this module in honor of Net::DNS::Nameserver
170  
171 AnyEvent::DNS::Nameserver try to be compatible with all the methods and features of Net::DNS::Nameserver
172  
173 You can find more information and usage from Net::DNS::Nameserver
174  
175 AnyEvent::DNS::Nameserver only support udp query and answer by now
176  
177 =head1 SEE ALSO
178  
179 L<https://github.com/sjdy521/AnyEvent-DNS-Nameserver>
180  
181 L<Net::DNS::Nameserver>
182  
183 =head1 AUTHOR
184  
185 sjdy521, E<lt>sjdy521@163.comE<gt>
186  
187 =head1 COPYRIGHT AND LICENSE
188  
189 Copyright (C) 2014 by Perfi
190  
191 This library is free software; you can redistribute it and/or modify
192 it under the same terms as Perl itself, either Perl version 5.8.8 or,
193 at your option, any later version of Perl 5 you may have available.
194  
195  
196 =cut
↑ Summary