blib/lib/IPTables/Rule.pm

filter: Statementstmt 88.8% 359 / 404 Branchbran 62.5% 175 / 280 Conditioncond 59.5% 25 / 42 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 100% 42 / 42 Podpod 87.5% 21 / 24 totaltotal 77.0% 630 / 818 CC 159 SCAR 45.5
CC
159
Cov
77%
generate
364.5
__is_valid_inet_port
23.9
table
8.6
CRAP 94.4
↑ Summary
1 package IPTables::Rule;
2  
3 1 ▶use 5.000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.03';
8  
9 ###############################################################################
10 ### PRECOMPILED REGEX
11 1 my $qr_fqdn = qr/(([A-Z0-9]|[A-Z0-9][A-Z0-9\-]*[A-Z0-9])\.)*([A-Z]|[A-Z][A-Z0-9\-]*[A-Z0-9])/io;
12 1 my $qr_mac_addr = qr/(([A-F0-9]{2}[:.-]?){6})/io;
13  
14 1 my $qr_ip4_addr = qr/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/o;
15 1 my $qr_ip6_addr;
16 {
17 # This block courtesy of Regexp::IPv6 0.03 by Salvador FandiƱo
18 # http://search.cpan.org/~salva/Regexp-IPv6/
19 # http://cpansearch.perl.org/src/SALVA/Regexp-IPv6-0.03/lib/Regexp/IPv6.pm
20 1 my $IPv4 = "((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))";
21 1 my $G = "[0-9a-fA-F]{1,4}";
22 1 my @tail = ( ":",
23 "(:($G)?|$IPv4)",
24 ":($IPv4|$G(:$G)?|)",
25 "(:$IPv4|:$G(:$IPv4|(:$G){0,2})|:)",
26 "((:$G){0,2}(:$IPv4|(:$G){1,2})|:)",
27 "((:$G){0,3}(:$IPv4|(:$G){1,2})|:)",
28 "((:$G){0,4}(:$IPv4|(:$G){1,2})|:)" );
29 1 my $IPv6_re = $G;
30 1 $IPv6_re = "$G:($IPv6_re|$_)" for @tail;
31 1 $IPv6_re = qq/:(:$G){0,5}((:$G){1,2}|:$IPv4)|$IPv6_re/;
32 1 $IPv6_re =~ s/\(/(?:/g;
33 1 $qr_ip6_addr = qr/$IPv6_re/;
34 }
35 # and the CIDR versions of the above
36 1 my $qr_ip4_cidr = qr/$qr_ip4_addr\/[0-9]{1,2}/o;
37 1 my $qr_ip6_cidr = qr/$qr_ip6_addr\/[0-9]{1,3}/io;
38  
39 ###############################################################################
40 ### METHODS
41  
42 27 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
43 27 my $self = {
44 ip4binary => 'iptables',
45 ip6binary => 'ip6tables',
46 iptaction => '-A',
47 ipver => '4', # IPv4 by default
48 table => undef,
49 chain => undef,
50 target => undef,
51 in => undef,
52 out => undef,
53 src => undef,
54 dst => undef,
55 proto => undef,
56 dpt => undef,
57 spt => undef,
58 mac => undef,
59 state => undef,
60 comment => undef,
61 logprefix => undef,
62 icmp_type => undef,
63 };
64  
65 27 bless $self;
66 }
67  
68 1 ▶sub dump {
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
69 1 my $self = shift;
70 1 my %dump_hash;
71  
72 1 foreach my $key ( keys %$self ) {
73 19 ▶ $dump_hash{$key} = $self->{$key} if ( defined($self->{$key}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} 7 12 19
74 }
75  
76 1 return \%dump_hash;
77 }
78  
79 1 ▶sub errstr {
Subroutine errstr: called CC=1 SCAR=0.0
Pod undocumented
80 1 my $self = shift;
81 1 return $self->{errstr};
82 }
83  
84 3 ▶sub ip4binary {
Subroutine ip4binary: called CC=3 SCAR=11.0
Pod documented
85 3 my $self = shift;
86 3 my ($arg) = @_;
87  
88 3 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 3 0 3
89 3 ▶ unless ( $arg =~ m|\A/.+\z| ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ m[\A/.+\z]) 1 2 3
90 1 __errstr($self, 'invalid path: '.$arg);
91 1 return;
92 }
93 2 $self->{ip4binary} = $arg;
94 }
95  
96 2 return $self->{ip4binary};
97 }
98  
99 3 ▶sub ip6binary {
Subroutine ip6binary: called CC=3 SCAR=11.0
Pod documented
100 3 my $self = shift;
101 3 my ($arg) = @_;
102  
103 3 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 3 0 3
104 3 ▶ unless ( $arg =~ m|\A/.+\z| ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ m[\A/.+\z]) 1 2 3
105 1 __errstr($self, 'invalid path: '.$arg);
106 1 return;
107 }
108 2 $self->{ip6binary} = $arg;
109 }
110  
111 2 return $self->{ip6binary};
112 }
113  
114 14 ▶sub iptaction {
Subroutine iptaction: called CC=3 SCAR=11.0
Pod documented
115 14 my $self = shift;
116 14 my ($arg) = @_;
117  
118 14 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 14 0 14
119 14 ▶ unless ( $arg =~ m/\A-[ADIRLSFZNXPE]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /\A-[ADIRLSFZNXPE]\z/) 2 12 14
120 2 __errstr($self, 'invalid action: '.$arg);
121 2 return;
122 }
123 12 $self->{iptaction} = $arg;
124 }
125  
126 12 return $self->{iptaction};
127 }
128  
129 12 ▶sub ipversion {
Subroutine ipversion: called CC=3 SCAR=11.0
Pod documented
130 12 my $self = shift;
131 12 my ($arg) = @_;
132  
133 12 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 12 0 12
134 # Valid arguments are 4 and 6
135 12 ▶ unless ( $arg =~ m/\A[46]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /\A[46]\z/) 4 8 12
136 4 __errstr($self, 'invalid ip version: '.$arg);
137 4 return;
138 }
139  
140 8 $self->{ipver} = $arg;
141 }
142  
143 8 return $self->{ipver};
144 }
145  
146 8 ▶sub table {
Subroutine table: called CC=7 SCAR=21.5
Pod documented
147 8 my $self = shift;
148 8 my ($arg) = @_;
149  
150 8 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 8 0 8
151 8 my $need_to_barf;
152 8 ▶ $need_to_barf = 1 if ( $self->{ipver} eq '4' and $arg !~ m/\A(filter|nat|mangle|raw)\z/i );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ipver"} eq 4 and not $arg =~ /\A(filter|nat|mangle|raw)\z/i 0 8 8
Condition66%2/3
$self->{"ipver"} eq 4 and not $arg =~ /\A(filter|nat|mangle|raw)\z/i
!ll&&!rl&&r
350
MC/DC0%0/2
$self->{"ipver"} eq 4 and not $arg =~ /\A(filter|nat|mangle|raw)\z/i
$self->{"ipver"} eq 4 not $arg =~ /\A(filter|nat|mangle|raw)\z/i
Truth table66%2/3
A and not(B)
A= $self->{"ipver"} eq 4B= not $arg =~ /\A(filter|nat|mangle|raw)\z/i
ABresult
0X0
100
111
153 8 ▶ $need_to_barf = 1 if ( $self->{ipver} eq '6' and $arg !~ m/\A(filter|mangle|raw)\z/i );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ipver"} eq 6 and not $arg =~ /\A(filter|mangle|raw)\z/i 0 8 8
Condition66%2/3
$self->{"ipver"} eq 6 and not $arg =~ /\A(filter|mangle|raw)\z/i
!ll&&!rl&&r
530
MC/DC0%0/2
$self->{"ipver"} eq 6 and not $arg =~ /\A(filter|mangle|raw)\z/i
$self->{"ipver"} eq 6 not $arg =~ /\A(filter|mangle|raw)\z/i
Truth table66%2/3
A and not(B)
A= $self->{"ipver"} eq 6B= not $arg =~ /\A(filter|mangle|raw)\z/i
ABresult
0X0
100
111
154 8 ▶ if ( $need_to_barf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_to_barf) 0 8 8
155 0 __errstr($self, sprintf('invalid table "%s" for ip version: %s', $arg, $self->{ipver}));
156 0 return;
157 }
158  
159 8 $self->{table} = $arg;
160 }
161  
162 8 return $self->{table};
163 }
164  
165 8 ▶sub chain {
Subroutine chain: called CC=2 SCAR=7.0
Pod documented
166 8 my $self = shift;
167 8 my ($arg) = @_;
168  
169 8 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 8 0 8
170 8 $self->{chain} = $arg;
171 }
172  
173 8 return $self->{chain};
174 }
175  
176 10 ▶sub target {
Subroutine target: called CC=2 SCAR=7.0
Pod documented
177 10 my $self = shift;
178 10 my ($arg) = @_;
179  
180 10 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 10 0 10
181 10 $self->{target} = $arg;
182 }
183  
184 10 return $self->{target};
185 }
186  
187 1 *protocol = \&proto;
188 11 ▶sub proto {
Subroutine proto: called CC=7 SCAR=19.7
Pod documented
189 11 my $self = shift;
190 11 my ($arg) = @_;
191  
192 11 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 11 0 11
193 11 ▶ unless ( $arg =~ m/\A[a-z0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /\A[a-z0-9]+\z/) 0 11 11
194 0 __errstr($self, 'invalid protocol: '.$arg);
195 0 return;
196 }
197 11 ▶ if ( $self->{ipver} eq '6' and $arg eq 'icmp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ipver"} eq 6 and $arg eq "icmp") 1 10 11
Condition100%3/3
$self->{"ipver"} eq 6 and $arg eq "icmp"
!ll&&!rl&&r
911
MC/DC100%2/2
$self->{"ipver"} eq 6 and $arg eq "icmp"
$self->{"ipver"} eq 6 $arg eq "icmp"
Truth table100%3/3
A and B
A= $self->{"ipver"} eq 6B= $arg eq "icmp"
ABresult
0X0
100
111
198 1 __errstr($self, 'icmp not valid protocol for IPv6. Perhaps you meant "icmpv6"?');
199 1 return;
200 }
201 10 ▶ if ( $self->{ipver} eq '4' and $arg eq 'icmpv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ipver"} eq 4 and $arg eq "icmpv6") 1 9 10
Condition100%3/3
$self->{"ipver"} eq 4 and $arg eq "icmpv6"
!ll&&!rl&&r
181
MC/DC100%2/2
$self->{"ipver"} eq 4 and $arg eq "icmpv6"
$self->{"ipver"} eq 4 $arg eq "icmpv6"
Truth table100%3/3
A and B
A= $self->{"ipver"} eq 4B= $arg eq "icmpv6"
ABresult
0X0
100
111
202 1 __errstr($self, 'icmpv6 not valid protocol for IPv4. Perhaps you meant "icmp"?');
203 1 return;
204 }
205  
206 9 $self->{proto} = $arg;
207 }
208  
209 9 return $self->{proto};
210 }
211  
212 9 ▶sub in {
Subroutine in: called CC=2 SCAR=7.0
Pod documented
213 9 my $self = shift;
214 9 my ($arg) = @_;
215  
216 9 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 9 0 9
217 9 $self->{in} = $arg;
218 }
219  
220 9 return $self->{in};
221 }
222  
223 8 ▶sub out {
Subroutine out: called CC=2 SCAR=7.0
Pod documented
224 8 my $self = shift;
225 8 my ($arg) = @_;
226  
227 8 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 8 0 8
228 8 $self->{out} = $arg;
229 }
230  
231 8 return $self->{out};
232 }
233  
234 1 *source = \&src;
235 17 ▶sub src {
Subroutine src: called CC=5 SCAR=16.2
Pod undocumented
236 17 my $self = shift;
237 17 my ($arg) = @_;
238  
239 17 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 17 0 17
240 17 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)) 7 10 17
Condition83%5/6
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)
l!l&&r!l&&!r
1007
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg)
l!l&&r!l&&!r
647
MC/DC66%2/3
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)
__is_valid_inet_host($arg) __is_valid_inet_cidr($arg) __is_valid_inet_range($arg)
Truth table75%3/4
A or B or C
A= __is_valid_inet_host($arg)B= __is_valid_inet_cidr($arg)C= __is_valid_inet_range($arg)
ABCresult
1XX1
01X1
0011
0000
241 __is_valid_inet_host($arg) or
242 __is_valid_inet_cidr($arg) or
243 __is_valid_inet_range($arg)
244 ) {
245 7 __errstr($self, 'invalid source address: '.$arg);
246 7 return;
247 }
248  
249 10 $self->{src} = $arg;
250 }
251  
252 10 return $self->{src};
253 }
254  
255 1 *dest = \&dst;
256 1 *destination = \&dst;
257 16 ▶sub dst {
Subroutine dst: called CC=5 SCAR=16.2
Pod undocumented
258 16 my $self = shift;
259 16 my ($arg) = @_;
260  
261 16 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 16 0 16
262 16 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)) 7 9 16
Condition83%5/6
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)
l!l&&r!l&&!r
907
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg)
l!l&&r!l&&!r
637
MC/DC66%2/3
__is_valid_inet_host($arg) or __is_valid_inet_cidr($arg) or __is_valid_inet_range($arg)
__is_valid_inet_host($arg) __is_valid_inet_cidr($arg) __is_valid_inet_range($arg)
Truth table75%3/4
A or B or C
A= __is_valid_inet_host($arg)B= __is_valid_inet_cidr($arg)C= __is_valid_inet_range($arg)
ABCresult
1XX1
01X1
0011
0000
263 __is_valid_inet_host($arg) or
264 __is_valid_inet_cidr($arg) or
265 __is_valid_inet_range($arg)
266 ) {
267 7 __errstr($self, 'invalid destination address: '.$arg);
268 7 return;
269 }
270  
271 9 $self->{dst} = $arg;
272 }
273  
274 9 return $self->{dst};
275 }
276  
277 1 *port = \&dpt;
278 1 *dport = \&dpt;
279 14 ▶sub dpt {
Subroutine dpt: called CC=3 SCAR=11.0
Pod documented
280 14 my $self = shift;
281 14 my ($arg) = @_;
282  
283 14 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 14 0 14
284 14 ▶ unless ( __is_valid_inet_port($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet_port($arg)) 5 9 14
285 5 __errstr($self, 'invalid destination port: '.$arg);
286 5 return;
287 }
288  
289 9 $self->{dpt} = $arg;
290 }
291  
292 9 return $self->{dpt};
293 }
294  
295 1 *sport = \&spt;
296 11 ▶sub spt {
Subroutine spt: called CC=3 SCAR=11.0
Pod documented
297 11 my $self = shift;
298 11 my ($arg) = @_;
299  
300 11 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 11 0 11
301 11 ▶ unless ( __is_valid_inet_port($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet_port($arg)) 5 6 11
302 5 __errstr($self, 'invalid source port: '.$arg);
303 5 return;
304 }
305  
306 6 $self->{spt} = $arg;
307 }
308  
309 6 return $self->{spt};
310 }
311  
312 4 ▶sub mac {
Subroutine mac: called CC=3 SCAR=11.0
Pod documented
313 4 my $self = shift;
314 4 my ($arg) = @_;
315  
316 4 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 4 0 4
317 4 ▶ unless ( __is_valid_mac_address($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_mac_address($arg)) 3 1 4
318 3 __errstr($self, 'invalid mac address: '.$arg);
319 3 return;
320 }
321  
322 1 $self->{mac} = $arg;
323 }
324  
325 1 return $self->{mac};
326 }
327  
328 11 ▶sub state {
Subroutine state: called CC=4 SCAR=13.9
Pod documented
329 11 my $self = shift;
330 11 my ($arg) = @_;
331  
332 11 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 11 0 11
333 11 my @states = split(",",$arg);
334 11 for (@states) {
335 14 ▶ unless ( $_ =~ m/\A(NEW|ESTABLISHED|RELATED|INVALID|UNTRACKED)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /\A(NEW|ESTABLISHED|RELATED|INVALID|UNTRACKED)\z/i) 3 11 14
336 3 __errstr($self, 'invalid connection tracking state: '.$_);
337 3 return;
338 }
339 }
340 8 $self->{state} = $arg;
341 }
342  
343 8 return $self->{state};
344 }
345  
346 1 *rate_limit = \&limit;
347 17 ▶sub limit {
Subroutine limit: called CC=3 SCAR=11.0
Pod documented
348 17 my $self = shift;
349 17 my ($arg) = @_;
350  
351 17 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 17 0 17
352 # --limit rate[/second|/minute|/hour|/day]
353 17 ▶ unless ( $arg =~ m/\A\d+\/(s(ec(ond)?)?|m(in(ute)?)?|h(our)?|d(ay)?)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ m[\A\d+/(s(ec(ond)?)?|m(in(ute)?)?|h(our)?|d(ay)?)\z]i) 6 11 17
354 6 __errstr($self, 'invalid rate limit: '.$arg);
355 6 return;
356 }
357 11 $self->{limit} = $arg;
358 }
359  
360 11 return $self->{limit};
361 }
362  
363 10 ▶sub icmp_type {
Subroutine icmp_type: called CC=3 SCAR=11.0
Pod documented
364 10 my $self = shift;
365 10 my ($arg) = @_;
366  
367 10 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 10 0 10
368 10 ▶ unless ( $arg =~ m|\A[a-z0-9\-]+(/[a-z0-9\-]+)?\z|i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ m[\A[a-z0-9\-]+(/[a-z0-9\-]+)?\z]i) 2 8 10
369 2 __errstr($self, 'invalid icmp type: '.$arg);
370 2 return;
371 }
372  
373 8 $self->{icmp_type} = $arg;
374 }
375  
376 8 return $self->{icmp_type};
377 }
378  
379 6 ▶sub logprefix {
Subroutine logprefix: called CC=4 SCAR=14.3
Pod documented
380 6 my $self = shift;
381 6 my ($arg) = @_;
382  
383 6 my $max_length = 29;
384  
385 6 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 6 0 6
386 6 ▶ if ( length($arg) > $max_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg > $max_length) 3 3 6
387 3 __errstr($self, 'log prefix too long (>'.$max_length.'): '.$arg);
388 3 return;
389 }
390 3 ▶ if ( $arg =~ m/[\"\']/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /[\"\']/) 0 3 3
391 0 __errstr($self, 'quotes not permitted: '.$arg);
392 0 return;
393 }
394  
395 3 $self->{logprefix} = $arg;
396 }
397  
398 3 return $self->{logprefix};
399 }
400  
401 7 ▶sub comment {
Subroutine comment: called CC=4 SCAR=13.9
Pod documented
402 7 my $self = shift;
403 7 my ($arg) = @_;
404  
405 7 my $max_length = 256;
406  
407 7 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 7 0 7
408 7 ▶ if ( length($arg) > $max_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg > $max_length) 1 6 7
409 1 __errstr($self, 'comment too long (>'.$max_length.'): '.$arg);
410 1 return;
411 }
412 6 ▶ if ( $arg =~ m/[\"\']/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /[\"\']/) 1 5 6
413 1 __errstr($self, 'quotes not permitted: '.$arg);
414 1 return;
415 }
416  
417 5 $self->{comment} = $arg;
418 }
419  
420 5 return $self->{comment};
421 }
422  
423 1 *compile = \&generate;
424 6 ▶sub generate {
Subroutine generate: called CC=49 SCAR=59.0
Pod documented
425 6 my $self = shift;
426  
427 # what is required?
428 6 ▶ unless ( $self->{chain} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"chain"}) 0 6 6
429 0 __errstr($self, 'Chain must be specified');
430 0 return;
431 }
432 # ports are only valid with protocol tcp and udp
433 6 ▶ if ( defined($self->{spt}) and $self->{proto} !~ m/\A(tcp|udp)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"spt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i) 0 6 6
Condition33%1/3
defined $self->{"spt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i
!ll&&!rl&&r
600
MC/DC0%0/2
defined $self->{"spt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i
defined $self->{"spt"} not $self->{"proto"} =~ /\A(tcp|udp)\z/i
Truth table33%1/3
A and not(B)
A= defined $self->{"spt"}B= not $self->{"proto"} =~ /\A(tcp|udp)\z/i
ABresult
0X0
100
111
434 0 __errstr($self, 'Protocol must be TCP or UDP when specifying source port');
435 0 return;
436 }
437 6 ▶ if ( defined($self->{dpt}) and $self->{proto} !~ m/\A(tcp|udp)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dpt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i) 0 6 6
Condition66%2/3
defined $self->{"dpt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i
!ll&&!rl&&r
510
MC/DC0%0/2
defined $self->{"dpt"} and not $self->{"proto"} =~ /\A(tcp|udp)\z/i
defined $self->{"dpt"} not $self->{"proto"} =~ /\A(tcp|udp)\z/i
Truth table66%2/3
A and not(B)
A= defined $self->{"dpt"}B= not $self->{"proto"} =~ /\A(tcp|udp)\z/i
ABresult
0X0
100
111
438 0 __errstr($self, 'Protocol must be TCP or UDP when specifying destinatipn port');
439 0 return;
440 }
441 # cant use 'logprefix' unless the target is 'log'
442 6 ▶ if ( defined($self->{logprefix}) and $self->{target} !~ m/\Alog\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logprefix"} and not $self->{"target"} =~ /\Alog\z/i) 0 6 6
Condition33%1/3
defined $self->{"logprefix"} and not $self->{"target"} =~ /\Alog\z/i
!ll&&!rl&&r
600
MC/DC0%0/2
defined $self->{"logprefix"} and not $self->{"target"} =~ /\Alog\z/i
defined $self->{"logprefix"} not $self->{"target"} =~ /\Alog\z/i
Truth table33%1/3
A and not(B)
A= defined $self->{"logprefix"}B= not $self->{"target"} =~ /\Alog\z/i
ABresult
0X0
100
111
443 0 __errstr($self, 'Target must be LOG when specifying log prefix');
444 0 return;
445 }
446 # ipversion matches the source/dest addresses?
447 6 ▶ if ( $self->{ipver} eq '4' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ipver'} eq '4') { } 5 1 6
elsif ($self->{'ipver'} eq '6') { } 1 0 1
448 5 ▶ if ( $self->{src} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"src"}) 1 4 5
449 # make sure it's ipv4
450 1 ▶ unless ( __is_valid_inet4($self->{src}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet4($self->{"src"})) 1 0 1
451 1 __errstr($self, 'IP Version is 4 but source is not valid IPv4');
452 1 return;
453 }
454 }
455 4 ▶ if ( $self->{dst} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dst"}) 0 4 4
456 # make sure it's ipv4
457 0 ▶ unless ( __is_valid_inet4($self->{dst}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet4($self->{"dst"})) 0 0 0
458 0 __errstr($self, 'IP Version is 4 but destination is not valid IPv4');
459 0 return;
460 }
461 }
462 } elsif ( $self->{ipver} eq '6' ) {
463 1 ▶ if ( $self->{src} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"src"}) 1 0 1
464 # make sure it's ipv6
465 1 ▶ unless ( __is_valid_inet6($self->{src}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet6($self->{"src"})) 1 0 1
466 1 __errstr($self, 'IP Version is 6 but source is not valid IPv6');
467 1 return;
468 }
469 }
470 0 ▶ if ( $self->{dst} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dst"}) 0 0 0
471 # make sure it's ipv6
472 0 ▶ unless ( __is_valid_inet6($self->{dst}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__is_valid_inet6($self->{"dst"})) 0 0 0
473 0 __errstr($self, 'IP Version is 6 but destination is not valid IPv6');
474 0 return;
475 }
476 }
477 } else {
478 # should never happen; the ipversion sub validates user input
479 0 __errstr($self, 'Code bug 0x01; Please report to developer.');
480 0 return;
481 }
482 # if icmp_type is set, protocol must be icmp or icmpv6
483 4 ▶ if ( defined($self->{icmp_type}) and $self->{proto} !~ m/\Aicmp(v6)?\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"icmp_type"} and not $self->{"proto"} =~ /\Aicmp(v6)?\z/i) 0 4 4
Condition33%1/3
defined $self->{"icmp_type"} and not $self->{"proto"} =~ /\Aicmp(v6)?\z/i
!ll&&!rl&&r
400
MC/DC0%0/2
defined $self->{"icmp_type"} and not $self->{"proto"} =~ /\Aicmp(v6)?\z/i
defined $self->{"icmp_type"} not $self->{"proto"} =~ /\Aicmp(v6)?\z/i
Truth table33%1/3
A and not(B)
A= defined $self->{"icmp_type"}B= not $self->{"proto"} =~ /\Aicmp(v6)?\z/i
ABresult
0X0
100
111
484 0 __errstr($self, 'icmp_type is set, but protocol is: '.$self->{proto});
485 0 return;
486 }
487  
488 4 my $rule_prefix;
489 my $rule_criteria;
490  
491 4 ▶ $rule_prefix = $self->{ip4binary} if $self->{ipver} eq '4';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ipver"} eq 4 4 0 4
492 4 ▶ $rule_prefix = $self->{ip6binary} if $self->{ipver} eq '6';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ipver"} eq 6 0 4 4
493 4 ▶ $rule_prefix .= ' -t '.$self->{table} if ( defined($self->{'table'}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"table"} 2 2 4
494 4 $rule_prefix .= ' '.$self->{iptaction};
495 4 $rule_prefix .= ' '.$self->{chain};
496
497 # Source and Destination Addresses
498 4 ▶ if ( defined($self->{src}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"src"}) 0 4 4
499 0 ▶ if ( __is_valid_inet_host($self->{src}) or __is_valid_inet_cidr($self->{src}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__is_valid_inet_host($self->{"src"}) or __is_valid_inet_cidr($self->{"src"})) 0 0 0
Condition0%0/3
__is_valid_inet_host($self->{"src"}) or __is_valid_inet_cidr($self->{"src"})
l!l&&r!l&&!r
000
MC/DC0%0/2
__is_valid_inet_host($self->{"src"}) or __is_valid_inet_cidr($self->{"src"})
__is_valid_inet_host($self->{"src"}) __is_valid_inet_cidr($self->{"src"})
Truth table0%0/3
A or B
A= __is_valid_inet_host($self->{"src"})B= __is_valid_inet_cidr($self->{"src"})
ABresult
1X1
011
000
500 0 $rule_criteria .= sprintf(' -s %s', $self->{src});
501 }
502 0 ▶ if ( __is_valid_inet_range($self->{src}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__is_valid_inet_range($self->{"src"})) 0 0 0
503 0 $rule_criteria .= sprintf(' -m iprange --src-range %s', $self->{'src'});
504 }
505 }
506 4 ▶ if ( defined($self->{dst}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dst"}) 0 4 4
507 0 ▶ if ( __is_valid_inet_host($self->{dst}) or __is_valid_inet_cidr($self->{dst}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__is_valid_inet_host($self->{"dst"}) or __is_valid_inet_cidr($self->{"dst"})) 0 0 0
Condition0%0/3
__is_valid_inet_host($self->{"dst"}) or __is_valid_inet_cidr($self->{"dst"})
l!l&&r!l&&!r
000
MC/DC0%0/2
__is_valid_inet_host($self->{"dst"}) or __is_valid_inet_cidr($self->{"dst"})
__is_valid_inet_host($self->{"dst"}) __is_valid_inet_cidr($self->{"dst"})
Truth table0%0/3
A or B
A= __is_valid_inet_host($self->{"dst"})B= __is_valid_inet_cidr($self->{"dst"})
ABresult
1X1
011
000
508 0 $rule_criteria .= sprintf(' -d %s', $self->{dst});
509 }
510 0 ▶ if ( __is_valid_inet_range($self->{dst}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__is_valid_inet_range($self->{"dst"})) 0 0 0
511 0 $rule_criteria .= sprintf(' -m iprange --dst-range %s', $self->{'dst'});
512 }
513 }
514  
515 # this needs to be written out before we output the src/dst port (if they are present)
516 # otherwise iptables/ip6tables complains at the command.
517 4 ▶ $rule_criteria .= sprintf(' -p %s', $self->{proto}) if ( defined($self->{proto}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"proto"} 1 3 4
518
519 # Source and Destination Ports
520 4 ▶ if ( defined($self->{spt}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"spt"}) 0 4 4
521 0 ▶ if ( $self->{spt} =~ m/\A\w+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spt"} =~ /\A\w+\z/) 0 0 0
522 # just a single port
523 0 $rule_criteria .= sprintf(' --sport %s', $self->{'spt'});
524 }
525 0 ▶ if ( $self->{spt} =~ m/\A\w+(:\w+)+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spt"} =~ /\A\w+(:\w+)+\z/) 0 0 0
526 # port range
527 0 $rule_criteria .= sprintf(' --sport %s', $self->{'spt'});
528 }
529 0 ▶ if ( $self->{spt} =~ m/\A\w+(:\w+)+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spt"} =~ /\A\w+(:\w+)+\z/) 0 0 0
530 # multiport
531 0 $rule_criteria .= sprintf(' -m multiport --sports %s', $self->{'spt'});
532 }
533 }
534 4 ▶ if ( defined($self->{dpt}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dpt"}) 1 3 4
535 1 ▶ if ( $self->{dpt} =~ m/\A\w+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dpt"} =~ /\A\w+\z/) 1 0 1
536 # just a single port
537 1 $rule_criteria .= sprintf(' --dport %s', $self->{'dpt'});
538 }
539 1 ▶ if ( $self->{dpt} =~ m/\A\w+(:\w+)+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dpt"} =~ /\A\w+(:\w+)+\z/) 0 1 1
540 # port range
541 0 $rule_criteria .= sprintf(' --dport %s', $self->{'dpt'});
542 }
543 1 ▶ if ( $self->{dpt} =~ m/\A\w+(:\w+)+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dpt"} =~ /\A\w+(:\w+)+\z/) 0 1 1
544 # multiport
545 0 $rule_criteria .= sprintf(' -m multiport --dports %s', $self->{'dpt'});
546 }
547 }
548  
549 4 ▶ $rule_criteria .= sprintf(' -i %s', $self->{in}) if ( defined($self->{in}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"in"} 3 1 4
550 4 ▶ $rule_criteria .= sprintf(' -o %s', $self->{out}) if ( defined($self->{out}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"out"} 1 3 4
551 4 ▶ $rule_criteria .= sprintf(' -m mac --mac-source %s', $self->{mac}) if ( defined($self->{mac}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mac"} 0 4 4
552 4 ▶ $rule_criteria .= sprintf(' -m conntrack --ctstate %s', $self->{state}) if ( defined($self->{state}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"state"} 1 3 4
553 4 ▶ $rule_criteria .= sprintf(' --icmp-type %s', $self->{icmp_type}) if ( defined($self->{icmp_type}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"icmp_type"} 0 4 4
554 4 ▶ $rule_criteria .= sprintf(' -m comment --comment "%s"', $self->{comment}) if ( defined($self->{comment}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"comment"} 3 1 4
555 4 ▶ $rule_criteria .= sprintf(' -m limit --limit %s', $self->{limit}) if ( defined($self->{limit}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"limit"} 0 4 4
556 4 ▶ $rule_criteria .= sprintf(' -j %s', $self->{'target'}) if ( defined($self->{'target'}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"target"} 4 0 4
557 4 ▶ $rule_criteria .= sprintf(' --log-prefix "[%s] "', $self->{logprefix}) if ( defined($self->{logprefix}) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"logprefix"} 0 4 4
558  
559 # $ipt_rule .= sprintf(' -m statistic %s', $criteria{'statistic'}) if (defined($criteria{'statistic'}));
560 # $ipt_rule .= sprintf(' -m time %s', $criteria{'time'}) if (defined($criteria{'time'}));
561  
562 4 my $full_cmd = $rule_prefix.$rule_criteria;
563 4 return $full_cmd;
564 }
565  
566 ###############################################################################
567 ### INTERNAL HELPERS
568 # These are subs that are NOT expected to be used outside this module itself.
569 # They are for internal code reuse only.
570 # All sub named should be prefixed with double underslash (__) to indicate they
571 # are internal use only.
572  
573 4 ▶sub __is_valid_mac_address {
Subroutine __is_valid_mac_address: called CC=3 SCAR=11.0
574 4 my ( $arg ) = @_;
575 4 chomp($arg);
576  
577 4 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 4 4
578  
579 4 ▶ if ( $arg =~ m/\A$qr_mac_addr\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A$qr_mac_addr\z/) 1 3 4
580 1 return 1;
581 }
582  
583 # fail by default
584 3 return;
585 }
586  
587 1 ▶sub __is_valid_inet4 {
Subroutine __is_valid_inet4: called CC=6 SCAR=19.1
588 1 my ( $arg ) = @_;
589 1 chomp($arg);
590  
591 1 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 1 1
592  
593 # ipv4 address?
594 1 ▶ return 1 if ( __is_inet4_host($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_host($arg) 0 1 1
595  
596 # ipv4 cidr?
597 1 ▶ return 1 if ( __is_inet4_cidr($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_cidr($arg) 0 1 1
598  
599 # ipv4 range?
600 1 ▶ return 1 if ( __is_inet4_range($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_range($arg) 0 1 1
601  
602 # fqdn?
603 1 ▶ return 1 if ( $arg =~ m/\A$qr_fqdn\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_fqdn\z/ 0 1 1
604  
605 # fail by default
606 1 return;
607 }
608  
609 1 ▶sub __is_valid_inet6 {
Subroutine __is_valid_inet6: called CC=6 SCAR=19.1
610 1 my ( $arg ) = @_;
611 1 chomp($arg);
612  
613 1 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 1 1
614  
615 # ipv6 address?
616 1 ▶ return 1 if ( __is_inet6_host($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_host($arg) 0 1 1
617  
618 # ipv4 cidr?
619 1 ▶ return 1 if ( __is_inet6_cidr($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_cidr($arg) 0 1 1
620  
621 # ipv4 range?
622 1 ▶ return 1 if ( __is_inet6_range($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_range($arg) 0 1 1
623  
624 # fqdn?
625 1 ▶ return 1 if ( $arg =~ m/\A$qr_fqdn\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_fqdn\z/ 0 1 1
626  
627 # fail by default
628 1 return;
629 }
630  
631 33 ▶sub __is_valid_inet_host {
Subroutine __is_valid_inet_host: called CC=5 SCAR=16.1
632 33 my ( $arg ) = @_;
633 33 chomp($arg);
634  
635 33 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 33 33
636  
637 # ipv4 address?
638 33 ▶ return 1 if ( __is_inet4_host($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_host($arg) 3 30 33
639  
640 # ipv6 address?
641 30 ▶ return 1 if ( __is_inet6_host($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_host($arg) 4 26 30
642  
643 # fqdn?
644 26 ▶ return 1 if ( $arg =~ m/\A$qr_fqdn\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_fqdn\z/ 5 21 26
645  
646 # fail by default
647 21 return;
648 }
649  
650 34 ▶sub __is_inet4_host {
Subroutine __is_inet4_host: called CC=3 SCAR=11.0
651 34 my ( $arg ) = @_;
652 34 chomp($arg);
653  
654 34 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 34 34
655  
656 # ipv4 address?
657 34 ▶ return 1 if ( $arg =~ m/\A$qr_ip4_addr\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_ip4_addr\z/ 3 31 34
658  
659 # fail by default
660 31 return;
661 }
662  
663 31 ▶sub __is_inet6_host {
Subroutine __is_inet6_host: called CC=3 SCAR=11.0
664 31 my ( $arg ) = @_;
665 31 chomp($arg);
666  
667 31 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 31 31
668  
669 # ipv6 address?
670 31 ▶ return 1 if ( $arg =~ m/\A$qr_ip6_addr\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_ip6_addr\z/ 4 27 31
671  
672 # fail by default
673 27 return;
674 }
675  
676 21 ▶sub __is_valid_inet_cidr {
Subroutine __is_valid_inet_cidr: called CC=4 SCAR=13.9
677 21 my ( $arg ) = @_;
678 21 chomp($arg);
679  
680 21 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 21 21
681  
682 # ipv4 cidr?
683 21 ▶ return 1 if ( __is_inet4_cidr($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_cidr($arg) 2 19 21
684  
685 # ipv6 cidr?
686 19 ▶ return 1 if ( __is_inet6_cidr($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_cidr($arg) 5 14 19
687  
688 # fail by default
689 14 return;
690 }
691  
692 22 ▶sub __is_inet4_cidr {
Subroutine __is_inet4_cidr: called CC=5 SCAR=16.2
693 22 my ( $arg ) = @_;
694 22 chomp($arg);
695  
696 22 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 22 22
697  
698 # ipv4 cidr?
699 22 ▶ if ( $arg =~ m/\A$qr_ip4_cidr\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A$qr_ip4_cidr\z/) 4 18 22
700 # validate the cidr
701 4 my ($host, $cidr) = split(/\//, $arg);
702 4 ▶ return if ( $cidr < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cidr < 0 0 4 4
703 4 ▶ return if ( $cidr > 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $cidr > 32 2 2 4
704  
705 2 return 1;
706 }
707  
708 # fail by default
709 18 return;
710 }
711  
712 20 ▶sub __is_inet6_cidr {
Subroutine __is_inet6_cidr: called CC=5 SCAR=16.2
713 20 my ( $arg ) = @_;
714 20 chomp($arg);
715  
716 20 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 20 20
717  
718 # ipv6 cidr?
719 20 ▶ if ( $arg =~ m/\A$qr_ip6_cidr\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A$qr_ip6_cidr\z/) 9 11 20
720 # validate the cidr
721 9 my ($host, $cidr) = split(/\//, $arg);
722 9 ▶ return if ( $cidr < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cidr < 0 0 9 9
723 9 ▶ return if ( $cidr > 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $cidr > 128 4 5 9
724  
725 5 return 1;
726 }
727  
728 # fail by default
729 11 return;
730 }
731  
732 14 ▶sub __is_valid_inet_range {
Subroutine __is_valid_inet_range: called CC=4 SCAR=14.5
733 14 my ( $arg ) = @_;
734 14 chomp($arg);
735  
736 14 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 14 14
737  
738 # ipv4 address range?
739 14 ▶ return 1 if ( __is_inet4_range($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet4_range($arg) 0 14 14
740  
741 # ipv6 address range?
742 14 ▶ return 1 if ( __is_inet6_range($arg) );
Branch: 1 branch
BranchTrueFalseTotal
if __is_inet6_range($arg) 0 14 14
743  
744 # fail by default
745 14 return;
746 }
747  
748 15 ▶sub __is_inet4_range {
Subroutine __is_inet4_range: called CC=3 SCAR=11.3
749 15 my ( $arg ) = @_;
750 15 chomp($arg);
751  
752 15 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 15 15
753  
754 # ipv4 address range?
755 15 ▶ return 1 if (
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_ip4_addr\-$qr_ip4_addr\z/ 0 15 15
756 $arg =~ m/\A$qr_ip4_addr\-$qr_ip4_addr\z/
757 );
758  
759 # fail by default
760 15 return;
761 }
762  
763 15 ▶sub __is_inet6_range {
Subroutine __is_inet6_range: called CC=3 SCAR=11.3
764 15 my ( $arg ) = @_;
765 15 chomp($arg);
766  
767 15 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 15 15
768  
769 # ipv6 address range?
770 15 ▶ return 1 if (
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A$qr_ip6_addr\-$qr_ip6_addr\z/ 0 15 15
771 $arg =~ m/\A$qr_ip6_addr\-$qr_ip6_addr\z/
772 );
773  
774 # fail by default
775 15 return;
776 }
777  
778 41 ▶sub __is_valid_inet_port {
Subroutine __is_valid_inet_port: called CC=19 SCAR=31.7
779 41 my ( $arg ) = @_;
780 41 chomp($arg);
781  
782 41 ▶ return unless ( $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 41 41
783  
784 # just a numeric port?
785 41 ▶ if ( __is_a_number($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__is_a_number($arg)) 21 20 41
786 21 ▶ return if ( $arg < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $arg < 0 0 21 21
787 21 ▶ return if ( $arg > 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if $arg > 65535 2 19 21
788  
789 19 return 1;
790 }
791  
792 # just a named port?
793 20 ▶ if ( $arg =~ m/\A[a-z]+\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A[a-z]+\z/i) 6 14 20
794 6 return 1;
795 }
796  
797 # numeric port range?
798 14 ▶ if ( $arg =~ /\A\d+:\d+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A\d+:\d+\z/) 4 10 14
799 4 my ( $lower, $upper) = split(/:/, $arg, 2);
800  
801 # recursive call to this sub to validate individal ports in multiport
802 4 ▶ return unless ( __is_valid_inet_port($lower) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($lower) 0 4 4
803 4 ▶ return unless ( __is_valid_inet_port($upper) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($upper) 0 4 4
804  
805 # lower is higher than upper?
806 4 ▶ return if ( $upper < $lower );
Branch: 1 branch
BranchTrueFalseTotal
if $upper < $lower 2 2 4
807  
808 2 return 1;
809 }
810  
811 # named port range?
812 10 ▶ if ( $arg =~ /\A[a-z]+:[a-z]+\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A[a-z]+:[a-z]+\z/i) 0 10 10
813 0 my ( $lower, $upper) = split(/:/, $arg, 2);
814  
815 # recursive call to this sub to validate individal ports in multiport
816 0 ▶ return unless ( __is_valid_inet_port($lower) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($lower) 0 0 0
817 0 ▶ return unless ( __is_valid_inet_port($upper) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($upper) 0 0 0
818  
819 0 return 1;
820 }
821  
822 # numeric multiport?
823 10 ▶ if ( $arg =~ /\A\d+(,\d+)+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A\d+(,\d+)+\z/) 2 8 10
824 2 my @ports = split(/,/, $arg);
825  
826 2 foreach my $port ( @ports ) {
827 # recursive call to this sub to validate individal ports in multiport
828 4 ▶ return unless ( __is_valid_inet_port($port) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($port) 0 4 4
829 }
830  
831 2 return 1;
832 }
833  
834 # named multiport?
835 8 ▶ if ( $arg =~ /\A[a-z]+(,[a-z]+)+\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /\A[a-z]+(,[a-z]+)+\z/i) 2 6 8
836 2 my @ports = split(/,/, $arg);
837  
838 2 foreach my $port ( @ports ) {
839 # recursive call to this sub to validate individal ports in multiport
840 4 ▶ return unless ( __is_valid_inet_port($port) );
Branch: 1 branch
BranchTrueFalseTotal
unless __is_valid_inet_port($port) 0 4 4
841 }
842  
843 2 return 1;
844 }
845  
846 # fail by default
847 6 return;
848 }
849  
850 41 ▶sub __is_a_number {
Subroutine __is_a_number: called CC=2 SCAR=6.9
851 41 my ( $arg) = @_;
852 41 ▶ return 1 if ( $arg =~ /\A-?\d+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\A-?\d+\z/ 21 20 41
853 20 return;
854 }
855  
856 55 ▶sub __errstr {
Subroutine __errstr: called CC=1 SCAR=0.0
857 55 my $self = shift;
858 55 my $errstr = shift;
859 55 $self->{errstr} = $errstr;
860 55 return 1;
861 }
862  
863 1 1;
864 __END__
↑ Summary