| 1 | package DJabberd::DNS; | ||||||||||||||||||||
| 2 | 20 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 20 | ▶ | use base 'Danga::Socket'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 20 | ▶ | use fields ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 20 | 'hostname', | |||||||||||||||||||
| 6 | 'callback', | ||||||||||||||||||||
| 7 | 'srv', | ||||||||||||||||||||
| 8 | 'port', | ||||||||||||||||||||
| 9 | 'recurse_count', | ||||||||||||||||||||
| 10 | 'became_readable', # bool | ||||||||||||||||||||
| 11 | 'timed_out', # bool | ||||||||||||||||||||
| 12 | 20 | ); | |||||||||||||||||||
| 13 | 20 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 20 | ▶ | use DJabberd::Log; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 20 | our $logger = DJabberd::Log->get_logger(); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 20 | ▶ | use Net::DNS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 20 | my $resolver = Net::DNS::Resolver->new; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub srv { | ||||||||||||||||||
|
Subroutine
srv: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||
| 22 | 0 | my ($class, %opts) = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | foreach (qw(callback domain service)) { | |||||||||||||||||||
| 25 | 0 | ▶ | croak("No '$_' field") unless $opts{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | my $hostname = delete $opts{'domain'}; | |||||||||||||||||||
| 29 | 0 | my $callback = delete $opts{'callback'}; | |||||||||||||||||||
| 30 | 0 | my $service = delete $opts{'service'}; | |||||||||||||||||||
| 31 | 0 | my $port = delete $opts{'port'}; | |||||||||||||||||||
| 32 | 0 | ▶ | my $recurse_count = delete($opts{'recurse_count'}) || 0; | ||||||||||||||||||
|
Condition0%0/2
delete $opts{'recurse_count'} || 0
MC/DC0%0/1
delete $opts{'recurse_count'} || 0
delete $opts{'recurse_count'}
Truth table0%0/2
A
A= delete $opts{'recurse_count'}
| |||||||||||||||||||||
| 33 | 0 | ▶ | croak "unknown opts" if %opts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # default port for s2s | ||||||||||||||||||||
| 36 | 0 | ▶ | $port ||= 5269 if $service eq "_xmpp-server._tcp"; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$port ||= 5269
MC/DC0%0/1
$port ||= 5269
$port
Truth table0%0/2
A
A= $port
| |||||||||||||||||||||
| 37 | 0 | ▶ | croak("No specified 'port'") unless $port; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # testing support | ||||||||||||||||||||
| 40 | 0 | ▶ | if ($service eq "_xmpp-server._tcp") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | my $endpt = DJabberd->fake_s2s_peer($hostname); | |||||||||||||||||||
| 42 | 0 | ▶ | if ($endpt) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | $callback->($endpt); | |||||||||||||||||||
| 44 | 0 | return; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | my $pkt = Net::DNS::Packet->new("$service.$hostname", "SRV", "IN"); | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | $logger->debug("pkt = $pkt"); | |||||||||||||||||||
| 51 | 0 | my $sock = $resolver->bgsend($pkt); | |||||||||||||||||||
| 52 | 0 | $logger->debug("sock = $sock"); | |||||||||||||||||||
| 53 | 0 | my $self = $class->SUPER::new($sock); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | $self->{hostname} = $hostname; | |||||||||||||||||||
| 56 | 0 | $self->{callback} = $callback; | |||||||||||||||||||
| 57 | 0 | $self->{srv} = $service; | |||||||||||||||||||
| 58 | 0 | $self->{port} = $port; | |||||||||||||||||||
| 59 | 0 | $self->{recurse_count} = $recurse_count; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | $self->{became_readable} = 0; | |||||||||||||||||||
| 62 | 0 | $self->{timed_out} = 0; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # TODO: make DNS timeout configurable | ||||||||||||||||||||
| 65 | 0 | ▶ | Danga::Socket->AddTimer(5.0, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 66 | 0 | ▶ | return if $self->{became_readable}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | $self->{timed_out} = 1; | |||||||||||||||||||
| 68 | 0 | $logger->debug("DNS 'SRV' lookup for '$hostname' timed out"); | |||||||||||||||||||
| 69 | 0 | $callback->(); | |||||||||||||||||||
| 70 | 0 | $self->close; | |||||||||||||||||||
| 71 | 0 | }); | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | $self->watch_read(1); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 77 | 0 | my ($class, %opts) = @_; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | foreach (qw(hostname callback port)) { | |||||||||||||||||||
| 80 | 0 | ▶ | croak("No '$_' field") unless $opts{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | my $hostname = delete $opts{'hostname'}; | |||||||||||||||||||
| 84 | 0 | my $callback = delete $opts{'callback'}; | |||||||||||||||||||
| 85 | 0 | my $port = delete $opts{'port'}; | |||||||||||||||||||
| 86 | 0 | ▶ | my $recurse_count = delete($opts{'recurse_count'}) || 0; | ||||||||||||||||||
|
Condition0%0/2
delete $opts{'recurse_count'} || 0
MC/DC0%0/1
delete $opts{'recurse_count'} || 0
delete $opts{'recurse_count'}
Truth table0%0/2
A
A= delete $opts{'recurse_count'}
| |||||||||||||||||||||
| 87 | 0 | ▶ | croak "unknown opts" if %opts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | if ($hostname =~/^\d+\.\d+\.\d+\.\d+$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | # we already have the IP, lets not looking it up | ||||||||||||||||||||
| 91 | 0 | $logger->debug("Skipping lookup for '$hostname', it is already the IP"); | |||||||||||||||||||
| 92 | 0 | $callback->(DJabberd::IPEndPoint->new($hostname, $port)); | |||||||||||||||||||
| 93 | 0 | return; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $sock = $resolver->bgsend($hostname); | |||||||||||||||||||
| 98 | 0 | my $self = $class->SUPER::new($sock); | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | $self->{hostname} = $hostname; | |||||||||||||||||||
| 101 | 0 | $self->{callback} = $callback; | |||||||||||||||||||
| 102 | 0 | $self->{port} = $port; | |||||||||||||||||||
| 103 | 0 | $self->{recurse_count} = $recurse_count; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | $self->{became_readable} = 0; | |||||||||||||||||||
| 106 | 0 | $self->{timed_out} = 0; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # TODO: make DNS timeout configurable, remove duplicate code | ||||||||||||||||||||
| 109 | 0 | ▶ | Danga::Socket->AddTimer(5.0, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 110 | 0 | ▶ | return if $self->{became_readable}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | $self->{timed_out} = 1; | |||||||||||||||||||
| 112 | 0 | $logger->debug("DNS 'A' lookup for '$hostname' timed out"); | |||||||||||||||||||
| 113 | 0 | $callback->(); | |||||||||||||||||||
| 114 | 0 | $self->close; | |||||||||||||||||||
| 115 | 0 | }); | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | $self->watch_read(1); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # TODO: verify response is for correct thing? or maybe Net::DNS does that? | ||||||||||||||||||||
| 121 | # TODO: lots of other stuff. | ||||||||||||||||||||
| 122 | 0 | ▶ | sub event_read { | ||||||||||||||||||
|
Subroutine
event_read: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | if ($self->{timed_out}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | $self->close; | |||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | 0 | $self->{became_readable} = 1; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | if ($self->{srv}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | $logger->debug("DNS socket $self->{sock} became readable for 'srv'"); | |||||||||||||||||||
| 133 | 0 | return $self->event_read_srv; | |||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||
| 135 | 0 | $logger->debug("DNS socket $self->{sock} became readable for 'a'"); | |||||||||||||||||||
| 136 | 0 | return $self->event_read_a; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | sub event_read_a { | ||||||||||||||||||
|
Subroutine
event_read_a: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | my $sock = $self->{sock}; | |||||||||||||||||||
| 144 | 0 | my $cb = $self->{callback}; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 0 | my $packet = $resolver->bgread($sock); | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | my @ans = $packet->answer; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | for my $ans (@ans) { | |||||||||||||||||||
| 151 | 0 | my $rv = eval { | |||||||||||||||||||
| 152 | 0 | ▶ | if ($ans->isa('Net::DNS::RR::CNAME')) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 153 | 0 | ▶ | if ($self->{recurse_count} < 5) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | $self->close; | |||||||||||||||||||
| 155 | DJabberd::DNS->new(hostname => $ans->cname, | ||||||||||||||||||||
| 156 | port => $self->{port}, | ||||||||||||||||||||
| 157 | callback => $cb, | ||||||||||||||||||||
| 158 | 0 | recurse_count => $self->{recurse_count}+1); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||
| 161 | # Too much recursion | ||||||||||||||||||||
| 162 | 0 | $logger->warn("Too much CNAME recursion while resolving ".$self->{hostname}); | |||||||||||||||||||
| 163 | 0 | $self->close; | |||||||||||||||||||
| 164 | 0 | $cb->(); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | } elsif ($ans->isa("Net::DNS::RR::PTR")) { | ||||||||||||||||||||
| 167 | 0 | $logger->debug("Ignoring RR response for $self->{hostname}"); | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||
| 170 | 0 | $cb->(DJabberd::IPEndPoint->new($ans->address, $self->{port})); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | 0 | $self->close; | |||||||||||||||||||
| 173 | 0 | 1; | |||||||||||||||||||
| 174 | }; | ||||||||||||||||||||
| 175 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | 0 | $self->close; | |||||||||||||||||||
| 177 | 0 | die "ERROR in DNS world: [$@]\n"; | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | 0 | ▶ | return if $rv; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | # no result | ||||||||||||||||||||
| 183 | 0 | $self->close; | |||||||||||||||||||
| 184 | 0 | $cb->(); | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 0 | ▶ | sub event_read_srv { | ||||||||||||||||||
|
Subroutine
event_read_srv: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | my $sock = $self->{sock}; | |||||||||||||||||||
| 191 | 0 | my $cb = $self->{callback}; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 0 | my $packet = $resolver->bgread($sock); | |||||||||||||||||||
| 194 | 0 | my @ans = $packet->answer; | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # FIXME: is this right? right order and direction? | ||||||||||||||||||||
| 197 | my @targets = sort { | ||||||||||||||||||||
| 198 | 0 | ▶ | $a->priority <=> $b->priority || | ||||||||||||||||||
|
Condition0%0/3
$a->priority <=> $b->priority || $a->weight <=> $b->weight
MC/DC0%0/2
$a->priority <=> $b->priority || $a->weight <=> $b->weight
$a->priority <=> $b->priority
$a->weight <=> $b->weight
Truth table0%0/3
A || B
A= $a->priority <=> $b->priorityB= $a->weight <=> $b->weight
| |||||||||||||||||||||
| 199 | $a->weight <=> $b->weight | ||||||||||||||||||||
| 200 | 0 | ▶ | } grep { ref $_ eq "Net::DNS::RR::SRV" && $_->port } @ans; | ||||||||||||||||||
|
Condition0%0/3
ref $_ eq 'Net::DNS::RR::SRV' && $_->port
MC/DC0%0/2
ref $_ eq 'Net::DNS::RR::SRV' && $_->port
ref $_ eq 'Net::DNS::RR::SRV'
$_->port
Truth table0%0/3
A && B
A= ref $_ eq 'Net::DNS::RR::SRV'B= $_->port
| |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 0 | ▶ | unless (@targets) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | # no result, fallback to an A lookup | ||||||||||||||||||||
| 204 | 0 | $self->close; | |||||||||||||||||||
| 205 | 0 | $logger->debug("DNS socket $sock for 'srv' had nothing, falling back to 'a' lookup"); | |||||||||||||||||||
| 206 | DJabberd::DNS->new(hostname => $self->{hostname}, | ||||||||||||||||||||
| 207 | port => $self->{port}, | ||||||||||||||||||||
| 208 | 0 | callback => $cb); | |||||||||||||||||||
| 209 | 0 | return; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | # FIXME: we only do the first target now. should do a chain. | ||||||||||||||||||||
| 213 | 0 | $logger->debug("DNS socket $sock for 'srv' found stuff, now doing hostname lookup on " . $targets[0]->target); | |||||||||||||||||||
| 214 | 0 | DJabberd::DNS->new(hostname => $targets[0]->target, | |||||||||||||||||||
| 215 | port => $targets[0]->port, | ||||||||||||||||||||
| 216 | callback => $cb); | ||||||||||||||||||||
| 217 | 0 | $self->close; | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | package DJabberd::IPEndPoint; | ||||||||||||||||||||
| 221 | 23 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 222 | 23 | my ($class, $addr, $port) = @_; | |||||||||||||||||||
| 223 | 23 | return bless { addr => $addr, port => $port }; | |||||||||||||||||||
| 224 | } | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 0 | ▶ | sub addr { $_[0]{addr} } | ||||||||||||||||||
|
Subroutine
addr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 227 | 0 | ▶ | sub port { $_[0]{port} } | ||||||||||||||||||
|
Subroutine
port: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 20 | 1; | |||||||||||||||||||