blib/lib/CCCP/LiveMX.pm

filter: Statementstmt 17.9% 24 / 134 Branchbran 1.6% 1 / 60 Conditioncond 7.6% 1 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 40.0% 6 / 15 Podpod 100% 6 / 6 totaltotal 16.1% 38 / 236 CC 39 SCAR 52.9
CC
39
Cov
13%
check_host
441.3
_mx_servers
20.0
_mx_ip
20.0
CRAP 197.6
↑ Summary
1 package CCCP::LiveMX;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.01';
7  
8 # from Mail::CheckUser, I use only $Mail::CheckUser::NXDOMAIN
9 1 ▶use Mail::CheckUser qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::Ping 2.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 CCCP::LiveMX
16  
17 =head1 DESCRIPTION
18  
19 Getting a ip-list of living MX-records for hostname
20  
21 =head1 SYNOPSIS
22  
23 use CCCP::LiveMX;
24
25 my $lmx = CCCP::LiveMX->check_host('example.org');
26 if ($lmx->success) {
27 my @live_ip = $lmx->live_ip;
28 } else {
29 print $lmx->error,"\n";
30 my @not_ping_ip = $lmx->not_ping;
31 my @not_ask_ip = $lmx->not_ask;
32 }
33
34 =head1 PACKAGE VARIABLES
35  
36 =head2 $CCCP::LiveMX::timeout
37  
38 Timeout for ping, resolve and another.
39 By default 5 sec.
40  
41 =head1 METHODS
42  
43 =cut
44  
45 1 $CCCP::LiveMX::timeout = 5;
46  
47 1 my $resolver;
48 my $ping;
49  
50 =head2 check_host($host_name)
51  
52 Checking MX records for C<$host_name> and return instance.
53  
54 =cut
55 1 ▶sub check_host {
Subroutine check_host: called CC=23 SCAR=60.9
Pod documented
56 1 my ($class, $host) = @_;
57
58 1 my $self = bless {
59 error => undef,
60 mx => {}
61 }, $class;
62
63 # Net::DNS::Resolver as a singletone
64 1 ▶ $resolver ||= Net::DNS::Resolver->new();
Condition33%1/3
$resolver ||= "Net::DNS::Resolver"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$resolver ||= "Net::DNS::Resolver"->new
$resolver "Net::DNS::Resolver"->new
Truth table33%1/3
A ||= B
A= $resolverB= "Net::DNS::Resolver"->new
ABresult
1X1
011
000
65
66 # Net::Ping as a singletone
67 1 ▶ unless ($ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ping) 1 0 1
68 1 $ping = Net::Ping->new("syn", $CCCP::LiveMX::timeout);
69 0 $ping->{port_num} = getservbyname("smtp", "tcp");
70 0 $ping->service_check(1);
71 };
72
73 # getting mx-records
74 0 $resolver->udp_timeout($CCCP::LiveMX::timeout);
75 0 my @mx = mx($resolver, $host);
76 0 ▶ unless (@mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mx) { } 0 0 0
77 # if the mx record is not found, try to check the hostname as a mail-server
78 0 $resolver->udp_timeout($CCCP::LiveMX::timeout);
79 0 my $res = $resolver->search($host, 'A');
80 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 0 0
81 0 my $ip;
82 0 foreach my $rr ($res->answer) {
83 0 ▶ if ($rr->type eq "A") {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->type eq 'A') { } 0 0 0
elsif ($rr->type eq 'CNAME') { } 0 0 0
84 0 $ip = $rr->address;
85 0 last;
86 } elsif ($rr->type eq "CNAME") {
87 0 $ip = $rr->cname;
88 } else {
89 # should never happen!
90 0 $ip = "";
91 }
92 }
93 0 ▶ if ($Mail::CheckUser::NXDOMAIN->{lc $ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$Mail::CheckUser::NXDOMAIN{lc $ip}) 0 0 0
94 0 ▶ $self->error('Wildcard gTLD: '.$host.' ('.(lc $ip || '').')');
Condition0%0/2
lc $ip || ''
l!l
00
MC/DC0%0/1
lc $ip || ''
lc $ip
Truth table0%0/2
A
A= lc $ip
Aresult
11
00
95 0 return $self;
96 }
97 0 $self->_mx_servers($host,0);
98 } else {
99 0 $self->error('DNS failure: '.$host.' ('.$resolver->errorstring.')');
100 0 return $self;
101 }
102 } else {
103 # if there is a mx-records, they have always been ordered by "preference"
104 0 $self->_mx_servers(map {$_->exchange,$_->preference} @mx);
105 };
106
107 0 foreach my $mserver ($self->_mx_servers) {
108 0 ▶ if ($Mail::CheckUser::NXDOMAIN->{lc $mserver}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$Mail::CheckUser::NXDOMAIN{lc $mserver}) 0 0 0
109 0 ▶ $self->error('Wildcard gTLD: '.$host.' ('.(lc $mserver || '').')');
Condition0%0/2
lc $mserver || ''
l!l
00
MC/DC0%0/1
lc $mserver || ''
lc $mserver
Truth table0%0/2
A
A= lc $mserver
Aresult
11
00
110 0 return $self;
111 };
112 # getting ip from server-name
113 0 my $ip;
114 0 ▶ if ($mserver !~ /^\d+\.\d+\.\d+\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $mserver =~ /^\d+\.\d+\.\d+\.\d+$/) { } 0 0 0
115 # resolve server-name if we do not have ip
116 0 $resolver->udp_timeout($CCCP::LiveMX::timeout);
117 0 ▶ if (my $ans = $resolver->query($mserver)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ans = $resolver->query($mserver)) 0 0 0
118 0 foreach my $rr_a ($ans->answer) {
119 0 ▶ if ($rr_a->type eq "A") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr_a->type eq "A") 0 0 0
120 0 $ip = $rr_a->address;
121 0 $self->_mx_ip($mserver,$ip);
122 0 last;
123 }
124 }
125 }
126 } else {
127 0 $ip = $mserver;
128 0 $self->_mx_ip($mserver,$ip);
129 }
130 0 ▶ next unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 0 0
131
132 # try ping
133 0 ▶ my $succ_ping = $ping->ping($ip) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ping->ping($ip) ? : 0 0 0
134 0 $self->_mx_ping($mserver, $succ_ping);
135 0 ▶ next unless $succ_ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $succ_ping 0 0 0
136
137 # try to get an answer
138 0 ▶ my $succ_ask = $ping->ack($ip) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ping->ack($ip) ? : 0 0 0
139 0 $self->_mx_ask($mserver,$succ_ask);
140 0 ▶ next unless $succ_ask;
Branch: 1 branch
BranchTrueFalseTotal
unless $succ_ask 0 0 0
141 }
142
143 0 ▶ unless (@{$self->live_ip}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->live_ip;}) 0 0 0
144 0 $self->error('Not found live ip for: '.$host);
145 }
146
147 0 return $self;
148 }
149  
150 =head2 success()
151  
152 Return status of check (bool)
153  
154 =cut
155 0 ▶sub success {
Subroutine success: not called CC=2 SCAR=17.9
Pod documented
156 0 my $self = shift;
157
158 0 ▶ return $self->error ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 0 0
159 }
160  
161 =head2 live_ip()
162  
163 Return list avaliable ip for host, sorted by "preference" mx-records
164  
165 =cut
166 0 ▶sub live_ip {
Subroutine live_ip: not called CC=3 SCAR=24.8
Pod documented
167 0 my $self = shift;
168 0 ▶ if ($self->{live_ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'live_ip'}) { } 0 0 0
169 0 return wantarray ? @{$self->{live_ip}} : $self->{live_ip}
170 0 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
171 0 $self->{live_ip} = [$self->_mx_ask];
172 0 return $self->live_ip;
173 };
174 }
175  
176 =head2 not_ping()
177  
178 Return list ip for host, that not ping
179  
180 =cut
181 0 ▶sub not_ping {
Subroutine not_ping: not called CC=2 SCAR=17.9
Pod documented
182 0 my $self = shift;
183 0 my @ret = map {$self->{mx}->{$_}->{ip}} grep {!$self->{mx}->{$_}->{ping}} keys %{$self->{mx}};
184 0 ▶ return wantarray ? @ret : [@ret];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
185 }
186  
187 =head2 not_ask()
188  
189 Return list ip for host, that ping but not ask
190  
191 =cut
192 0 ▶sub not_ask {
Subroutine not_ask: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 my @ret = map {$self->{mx}->{$_}->{ip}} grep {!$self->{mx}->{$_}->{ask}} keys %{$self->{mx}};
195 0 ▶ return wantarray ? @ret : [@ret];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
196 }
197  
198 =head2 error()
199  
200 Return error(string) or undef
201  
202 =cut
203 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
204 0 my $self = shift;
205 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
206 0 $self->{error} = shift;
207 0 return;
208 } else {
209 0 return $self->{error};
210 };
211 }
212  
213 # добавляем сервак с весом в список известных нам
214 0 ▶sub _mx_servers {
Subroutine _mx_servers: not called CC=4 SCAR=30.0
215 0 my $self = shift;
216 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
217 0 my %hash = @_;
218 0 ▶ while (my ($mx_name, $order) = each %hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash) 0 0 0
219 0 ▶ $self->{mx}->{$mx_name}->{order} = $order unless exists $self->{mx}->{$mx_name}->{order};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"mx"}{$mx_name}{"order"} 0 0 0
220 }
221 } else {
222 0 return keys %{$self->{mx}};
223 };
224 }
225  
226 # добавляем ip для сервака
227 0 ▶sub _mx_ip {
Subroutine _mx_ip: not called CC=4 SCAR=30.0
228 0 my $self = shift;
229 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
230 0 my %hash = @_;
231 0 ▶ while (my ($mx_name, $ip) = each %hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash) 0 0 0
232 0 ▶ $self->{mx}->{$mx_name}->{ip} = $ip unless exists $self->{mx}->{$mx_name}->{ip};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"mx"}{$mx_name}{"ip"} 0 0 0
233 }
234 0 return;
235 } else {
236 0 return map {$self->{mx}->{$_}->{ip}} grep {$self->{mx}->{$_}->{ip}} keys %{$self->{mx}};
237 };
238 }
239  
240 # добавляем стутус пинга
241 0 ▶sub _mx_ping {
Subroutine _mx_ping: not called CC=3 SCAR=24.8
242 0 my $self = shift;
243 0 my $mx_name = shift;
244 0 ▶ if ($mx_name and @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mx_name and @_) { } 0 0 0
Condition0%0/3
$mx_name and @_
!ll&&!rl&&r
000
MC/DC0%0/2
$mx_name and @_
$mx_name @_
Truth table0%0/3
A and B
A= $mx_nameB= @_
ABresult
0X0
100
111
245 0 $self->{mx}->{$mx_name}->{ping} = $_[0];
246 0 return;
247 } else {
248 0 return map {$self->{mx}->{$_}->{ip}} grep {$self->{mx}->{$_}->{ping}} keys %{$self->{mx}};
249 };
250 }
251  
252 # респонсим
253 0 ▶sub _mx_ask {
Subroutine _mx_ask: not called CC=3 SCAR=24.8
254 0 my $self = shift;
255 0 my $mx_name = shift;
256 0 ▶ if ($mx_name and @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mx_name and @_) { } 0 0 0
Condition0%0/3
$mx_name and @_
!ll&&!rl&&r
000
MC/DC0%0/2
$mx_name and @_
$mx_name @_
Truth table0%0/3
A and B
A= $mx_nameB= @_
ABresult
0X0
100
111
257 0 $self->{mx}->{$mx_name}->{ask} = $_[0];
258 0 return;
259 } else {
260 0 return map {$self->{mx}->{$_}->{ip}} sort {$self->{mx}->{$a}->{order} <=> $self->{mx}->{$b}->{order}} grep {$self->{mx}->{$_}->{ask}} keys %{$self->{mx}};
261 };
262 }
263  
264 =head1 DEPENDS ON
265  
266 =over 4
267  
268 =item *
269  
270 L<Mail::CheckUser> (used only package variables)
271  
272 =item *
273  
274 L<Net::DNS>
275  
276 =item *
277  
278 L<Net::Ping>
279  
280 =back
281  
282 =head1 AUTHOR
283  
284 mr.Rico <catamoose at yandex.ru>
285  
286 =cut
287  
288 1 1;
289 __END__
↑ Summary