DRI/Data/Hosts.pm

filter: Statementstmt 77.6% 101 / 130 Branchbran 61.7% 42 / 68 Conditioncond 40.7% 22 / 54 MC/DCmcdc 18.1% 6 / 33 Subroutinesub 76.4% 13 / 17 Podpod 90.0% 9 / 10 totaltotal 61.8% 193 / 312 CC 60 SCAR 25.5
CC
60
Cov
65%
as_string
30.0
_push
20.5
get_details
15.8
CRAP 12.8
« DRI/Protocol/DAS/Connection.pm ↑ Summary DRI/DRD/CIRA.pm »
1 ## Domain Registry Interface, Implements a list of host (names+ip) with order preserved
2 ##
3 ## Copyright (c) 2005-2009,2013-2014 Patrick Mevzek <netdri@dotandco.com>. All rights reserved.
4 ##
5 ## This file is part of Net::DRI
6 ##
7 ## Net::DRI is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## See the LICENSE file that comes with this distribution for more details.
13 ####################################################################################################
14  
15 package Net::DRI::Data::Hosts;
16  
17 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 82 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 82 ▶use base qw(Class::Accessor::Chained::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 82 __PACKAGE__->mk_accessors(qw(name loid));
23  
24 82 ▶use Net::DRI::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =pod
27  
28 =head1 NAME
29  
30 Net::DRI::Data::Hosts - Handle ordered list of nameservers (name, IPv4 addresses, IPv6 addresses) for Net::DRI
31  
32 =head1 SYNOPSIS
33  
34 use Net::DRI::Data::Hosts;
35  
36 my $dh=Net::DRI::Data::Hosts->new();
37 $dh->add('ns.example.foo',['1.2.3.4','1.2.3.5']);
38 $dh->add('ns2.example.foo',['10.1.1.1']); ## Third element can be an array ref of IPv6 addresses
39 ## ->add() returns the object itself, and thus can be chained
40  
41 ## Number of nameservers
42 print $dh->count(); ## Gives 2
43  
44 ## List of names, either all without arguments, or the amount given by the argument
45 my @a=$dh->get_names(2); ## Gives ('ns.example.foo','ns2.example.foo')
46  
47 ## Details for the nth nameserver (the list starts at 1 !)
48 my @d=$dh->get_details(2); ## Gives ('ns2.example.foo',['10.1.1.1'])
49  
50 ## Details by name is possible also
51 my @d=$dh->get_details('ns2.example.foo');
52  
53 =head1 DESCRIPTION
54  
55 Order of nameservers is preserved. Order of IP addresses is preserved, but no duplicate IP is allowed.
56  
57 If you try to add a nameserver that is already in the list, the IP
58 addresses provided will be added to the existing IP addresses (without duplicates)
59  
60 Hostnames are verified before being used with Net::DRI::Util::is_hostname().
61  
62 IP addresses are verified with Net::DRI::Util::is_ipv4() and Net::DRI::Util::is_ipv6().
63  
64 =head1 METHODS
65  
66 =head2 new(...)
67  
68 creates a new instance ; if parameters are given, add() is called with them all at once
69  
70 =head2 new_set(...)
71  
72 creates a new instance ; if parameters are given, add() is called once for each parameter
73  
74 =head2 clear()
75  
76 clears the current list of nameservers
77  
78 =head2 set(...)
79  
80 clears the current list of nameservers, and call add() once for each parameter passed
81  
82 =head2 add(name,[ipv4],[ipv6])
83  
84 adds a new nameserver with the given name and lists of IPv4 and IPv6 addresses
85  
86 =head2 name()
87  
88 name of this object (for example for registries having the notion of host groups) ;
89 this has nothing to do with the name(s) of the nameservers inside this object
90  
91 =head2 loid()
92  
93 local id of this object
94  
95 =head2 get_names(limit)
96  
97 returns a list of nameservers' names included in this object ; if limit is provided
98 we return only the number of names asked
99  
100 =head2 count()
101  
102 returns the number of nameservers currently stored in this object
103  
104 =head2 is_empty()
105  
106 returns 0 if this object has nameservers, 1 otherwise
107  
108 =head2 get_details(pos_or_name)
109  
110 given an integer (position in the list, we start to count at 1) or a name,
111 we return all details as a 3 element array in list context or only the first
112 element (the name) in scalar context for the nameserver stored at
113 the given position or with the given name ; returns undef if nothing found
114 at the position/with the name given.
115  
116 =head1 SUPPORT
117  
118 For now, support questions should be sent to:
119  
120 E<lt>netdri@dotandco.comE<gt>
121  
122 Please also see the SUPPORT file in the distribution.
123  
124 =head1 SEE ALSO
125  
126 http://www.dotandco.com/services/software/Net-DRI/
127  
128 =head1 AUTHOR
129  
130 Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>
131  
132 =head1 COPYRIGHT
133  
134 Copyright (c) 2005-2009,2013-2014 Patrick Mevzek <netdri@dotandco.com>.
135 All rights reserved.
136  
137 This program is free software; you can redistribute it and/or modify
138 it under the terms of the GNU General Public License as published by
139 the Free Software Foundation; either version 2 of the License, or
140 (at your option) any later version.
141  
142 See the LICENSE file that comes with this distribution for more details.
143  
144 =cut
145  
146 ####################################################################################################
147  
148 sub new
149 {
150 3 ▶ my ($class,@args)=@_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
151 3 my $self={ list => [] }; ## list=>[['',[ipv4],[ipv6],{}]+],options=>{}
152 3 bless $self,$class;
153 3 ▶ $self->add(@args) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 1 3
154 3 return $self;
155 }
156  
157 sub new_set
158 {
159 0 ▶ my ($class,@args)=@_;
Subroutine new_set: not called CC=2 SCAR=17.9
Pod documented
160 0 my $s=$class->new();
161 0 foreach (@args) { $s->add($_); }
162 0 return $s;
163 }
164  
165 sub clear
166 {
167 0 ▶ my $s=shift;
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
168 0 $s->{list}=[];
169 0 return;
170 }
171  
172 sub set
173 {
174 1 ▶ my ($s,@args)=@_;
Subroutine set: called CC=2 SCAR=6.9
Pod documented
175 1 $s->{list}=[];
176 1 foreach (@args) { $s->add($_); }
177 1 return $s;
178 }
179  
180 sub add
181 {
182 10 ▶ my ($self,$in,$e1,$e2,$ipall,$rextra)=@_;
Subroutine add: called CC=10 SCAR=25.1
Pod documented
183 10 ▶ ($ipall,$rextra)=(undef,$ipall) if (defined($ipall) && ref($ipall));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipall and ref $ipall 2 8 10
Condition66%2/3
defined $ipall and ref $ipall
!ll&&!rl&&r
802
MC/DC50%1/2
defined $ipall and ref $ipall
defined $ipall ref $ipall
Truth table66%2/3
A and B
A= defined $ipallB= ref $ipall
ABresult
0X0
100
111
184 10 ▶ return unless (defined($in) && $in);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in and $in 0 10 10
Condition33%1/3
defined $in and $in
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $in and $in
defined $in $in
Truth table33%1/3
A and B
A= defined $inB= $in
ABresult
0X0
100
111
185  
186 10 ▶ if (ref $in eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in eq "ARRAY") 1 9 10
187 {
188 1 return $self->add(@$in);
189 }
190  
191 9 ▶ if (defined $e2 && $e2)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e2 and $e2) 3 6 9
Condition66%2/3
defined $e2 and $e2
!ll&&!rl&&r
603
MC/DC50%1/2
defined $e2 and $e2
defined $e2 $e2
Truth table66%2/3
A and B
A= defined $e2B= $e2
ABresult
0X0
100
111
192 {
193 3 $self->_push($in,$e1,$e2,$ipall,$rextra);
194 3 return $self;
195 }
196  
197 6 ▶ if (defined $e1 && $e1)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e1 and $e1) 6 0 6
Condition33%1/3
defined $e1 and $e1
!ll&&!rl&&r
006
MC/DC0%0/2
defined $e1 and $e1
defined $e1 $e1
Truth table33%1/3
A and B
A= defined $e1B= $e1
ABresult
0X0
100
111
198 {
199 6 $self->_push($in,_separate_ips($e1,$ipall),$ipall,$rextra);
200 6 return $self;
201 }
202  
203 0 $self->_push($in,[],[],1,$rextra);
204 0 return $self;
205 }
206  
207 sub _separate_ips
208 {
209 6 ▶ my (@args)=@_;
Subroutine _separate_ips: called CC=6 SCAR=18.6
210 6 my (@ip4,@ip6);
211 6 my $ipall=pop @args;
212 6 ▶ $ipall=0 unless defined $ipall;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipall 6 0 6
213 6 ▶ foreach my $ip (map {ref($_)? @{$_} : $_} @args)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 6 0 6
214 {
215 ## We keep only the public ips
216 9 ▶ push @ip4,$ip if Net::DRI::Util::is_ipv4($ip,1-$ipall);
Branch: 1 branch
BranchTrueFalseTotal
if Net::DRI::Util::is_ipv4($ip, 1 - $ipall) 9 0 9
217 9 ▶ push @ip6,$ip if Net::DRI::Util::is_ipv6($ip,1-$ipall);
Branch: 1 branch
BranchTrueFalseTotal
if Net::DRI::Util::is_ipv6($ip, 1 - $ipall) 0 9 9
218 }
219 6 return (\@ip4,\@ip6);
220 }
221  
222 sub _push
223 {
224 9 ▶ my ($self,$name,$ipv4,$ipv6,$ipall,$rextra)=@_;
Subroutine _push: called CC=17 SCAR=30.2
225 9 ▶ $ipall=0 unless defined $ipall;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipall 9 0 9
226 9 ▶ chop($name) if (defined $name && $name && $name=~m/\.$/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $name and $name =~ /\.$/ 0 9 9
Condition33%2/6
defined $name and $name and $name =~ /\.$/
!ll&&!rl&&r
090
defined $name and $name
!ll&&!rl&&r
009
MC/DC0%0/3
defined $name and $name and $name =~ /\.$/
defined $name $name $name =~ /\.$/
Truth table25%1/4
A and B and C
A= defined $nameB= $nameC= $name =~ /\.$/
ABCresult
0XX0
10X0
1100
1111
227 9 ▶ return unless Net::DRI::Util::is_hostname($name);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::DRI::Util::is_hostname($name) 0 9 9
228 9 $name=lc($name); ## by default, hostnames are case insensitive
229  
230 ## We keep only the public ips
231 9 ▶ my @ipv4=grep { Net::DRI::Util::is_ipv4($_,1-$ipall) } ref $ipv4 ? @$ipv4 : ($ipv4);
Branch: 1 branch
BranchTrueFalseTotal
ref $ipv4 ? : 9 0 9
232 9 ▶ my @ipv6=grep { Net::DRI::Util::is_ipv6($_,1-$ipall) } ref $ipv6 ? @$ipv6 : ($ipv6);
Branch: 1 branch
BranchTrueFalseTotal
ref $ipv6 ? : 9 0 9
233  
234 9 ▶ if ($self->count() && defined $self->get_details($name)) ## name already here, we append IP
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count and defined $self->get_details($name)) { } 4 5 9
Condition100%3/3
$self->count and defined $self->get_details($name)
!ll&&!rl&&r
324
MC/DC100%2/2
$self->count and defined $self->get_details($name)
$self->count defined $self->get_details($name)
Truth table100%3/3
A and B
A= $self->countB= defined $self->get_details($name)
ABresult
0X0
100
111
235 {
236 4 foreach my $el (@{$self->{list}})
237 {
238 5 ▶ next unless ($el->[0] eq $name);
Branch: 1 branch
BranchTrueFalseTotal
unless $el->[0] eq $name 1 4 5
239 4 unshift @ipv4,@{$el->[1]};
240 4 unshift @ipv6,@{$el->[2]};
241 4 $el->[1]=_remove_dups_ip(\@ipv4);
242 4 $el->[2]=_remove_dups_ip(\@ipv6);
243 4 ▶ if (defined $el->[3] || defined $rextra)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $el->[3] or defined $rextra) 1 3 4
Condition66%2/3
defined $el->[3] or defined $rextra
l!l&&r!l&&!r
103
MC/DC50%1/2
defined $el->[3] or defined $rextra
defined $el->[3] defined $rextra
Truth table66%2/3
A or B
A= defined $el->[3]B= defined $rextra
ABresult
1X1
011
000
244 {
245 1 ▶ $el->[3]={ defined $el->[3] ? %{$el->[3]} : (), (defined $rextra && ref $rextra eq 'HASH')? %$rextra : () };
Branch: 2 branches
BranchTrueFalseTotal
defined $el->[3] ? : 1 0 1
defined $rextra && ref $rextra eq 'HASH' ? : 1 0 1
Condition33%1/3
defined $rextra and ref $rextra eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $rextra and ref $rextra eq "HASH"
defined $rextra ref $rextra eq "HASH"
Truth table33%1/3
A and B
A= defined $rextraB= ref $rextra eq "HASH"
ABresult
0X0
100
111
246 }
247 4 last;
248 }
249 } else
250 {
251 5 push @{$self->{list}},[$name,_remove_dups_ip(\@ipv4),_remove_dups_ip(\@ipv6),$rextra];
252 }
253 9 return;
254 }
255  
256 sub _remove_dups_ip
257 {
258 18 ▶ my $ip=shift;
Subroutine _remove_dups_ip: called CC=3 SCAR=11.3
259 18 my @r;
260 my %tmp;
261 18 ▶ @r=ref($ip)? grep { ! $tmp{$_}++ } @$ip : ($ip) if defined $ip;
Branch: 2 branches
BranchTrueFalseTotal
if defined $ip 18 0 18
ref $ip ? : 18 0 18
262 18 return \@r;
263 }
264  
265 ## Give back an array of all hostnames, or up to a limit if provided
266 sub get_names
267 {
268 4 ▶ my ($self,$limit)=@_;
Subroutine get_names: called CC=6 SCAR=18.6
Pod documented
269 4 ▶ return unless (defined $self && ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and ref $self 0 4 4
Condition33%1/3
defined $self and ref $self
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table33%1/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
270 4 my $c=$self->count();
271 4 ▶ $c=$limit if ($limit && ($limit <= $c));
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $limit <= $c 3 1 4
Condition66%2/3
$limit and $limit <= $c
!ll&&!rl&&r
103
MC/DC50%1/2
$limit and $limit <= $c
$limit $limit <= $c
Truth table66%2/3
A and B
A= $limitB= $limit <= $c
ABresult
0X0
100
111
272 4 my @r;
273 4 foreach (0..($c-1))
274 {
275 6 push @r,$self->{list}->[$_]->[0];
276 }
277 4 return @r;
278 }
279  
280 sub count
281 {
282 30 ▶ my $self=shift;
Subroutine count: called CC=3 SCAR=12.0
Pod documented
283 30 ▶ return unless (defined $self && ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and ref $self 0 30 30
Condition33%1/3
defined $self and ref $self
!ll&&!rl&&r
0030
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table33%1/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
284 30 return scalar(@{$self->{list}});
285 }
286  
287 sub is_empty
288 {
289 0 ▶ my $self=shift;
Subroutine is_empty: not called CC=3 SCAR=24.8
Pod documented
290 0 my $c=$self->count();
291 0 ▶ return (defined $c && ($c > 0))? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $c && $c > 0 ? : 0 0 0
Condition0%0/3
defined $c and $c > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c and $c > 0
defined $c $c > 0
Truth table0%0/3
A and B
A= defined $cB= $c > 0
ABresult
0X0
100
111
292 }
293  
294 sub get_details
295 {
296 15 ▶ my ($self,$pos)=@_;
Subroutine get_details: called CC=12 SCAR=27.6
Pod documented
297 15 ▶ return unless (defined $self && ref $self && defined $pos && $pos);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and ref $self and defined $pos and $pos 0 15 15
Condition33%3/9
defined $self and ref $self and defined $pos and $pos
!ll&&!rl&&r
0015
defined $self and ref $self and defined $pos
!ll&&!rl&&r
0015
defined $self and ref $self
!ll&&!rl&&r
0015
MC/DC0%0/4
defined $self and ref $self and defined $pos and $pos
defined $self ref $self defined $pos $pos
Truth table20%1/5
A and B and C and D
A= defined $selfB= ref $selfC= defined $posD= $pos
ABCDresult
0XXX0
10XX0
110X0
11100
11111
298 15 my $c=$self->count();
299  
300 15 ▶ if ($pos=~m/^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($pos =~ /^\d+$/) { } 5 10 15
301 {
302 5 ▶ return unless ($c && ($pos <= $c));
Branch: 1 branch
BranchTrueFalseTotal
unless $c and $pos <= $c 0 5 5
Condition33%1/3
$c and $pos <= $c
!ll&&!rl&&r
005
MC/DC0%0/2
$c and $pos <= $c
$c $pos <= $c
Truth table33%1/3
A and B
A= $cB= $pos <= $c
ABresult
0X0
100
111
303 5 my $el=$self->{list}->[$pos-1];
304 5 ▶ return wantarray()? @$el : $el->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
305 } else
306 {
307 10 $pos=lc($pos);
308 10 foreach my $el (@{$self->{list}})
309 {
310 11 ▶ next unless ($el->[0] eq $pos);
Branch: 1 branch
BranchTrueFalseTotal
unless $el->[0] eq $pos 3 8 11
311 8 ▶ return wantarray()? @$el : $el->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
312 }
313 2 return;
314 }
315 }
316  
317 # Do not use this method for anything else than debugging. The output format is not guaranteed to remain stable
318 sub as_string
319 {
320 0 ▶ my ($self)=shift;
Subroutine as_string: not called CC=5 SCAR=34.0
Pod undocumented
321 0 my @s;
322 0 foreach my $el (@{$self->{list}})
323 {
324 0 my $s=$el->[0];
325 0 my $ips=join(',',@{$el->[1]},@{$el->[2]});
326 0 ▶ $s.=' ['.$ips.']' if $ips;
Branch: 1 branch
BranchTrueFalseTotal
if $ips 0 0 0
327 0 ▶ $s.=' {'.join(' ',map { $_.'='.$el->[3]->{$_} } sort { $a cmp $b } keys %{$el->[3]}).'}' if (defined $el->[3] && %{$el->[3]});
Branch: 1 branch
BranchTrueFalseTotal
if defined $el->[3] and %{$el->[3];} 0 0 0
Condition0%0/3
defined $el->[3] and %{$el->[3];}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $el->[3] and %{$el->[3];}
defined $el->[3] %{$el->[3];}
Truth table0%0/3
A and B
A= defined $el->[3]B= %{$el->[3];}
ABresult
0X0
100
111
328 0 push @s,$s;
329 }
330 0 return join(' ',@s);
331 }
332  
333 ####################################################################################################
334 82 1;
« DRI/Protocol/DAS/Connection.pm ↑ Summary DRI/DRD/CIRA.pm »