Frame/Layer.pm

filter: Statementstmt 58.7% 121 / 206 Branchbran 35.7% 20 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 28 / 42 Podpod 100% 23 / 23 totaltotal 58.7% 192 / 327 CC 30 SCAR 17.1
CC
30
Cov
54%
getHostIpv4Addrs
20.0
inetChecksum
12.0
_setGetAddressSub
11.9
CRAP 5.5
« Frame/Layer/ARP.pm ↑ Summary Frame/Layer/IPv4.pm »
1 #
2 # $Id: Layer.pm,v ce68fbcc7f6d 2019/05/23 05:58:40 gomor $
3 #
4 package Net::Frame::Layer;
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 require Class::Gomor::Array;
9 12 require Exporter;
10 12 our @ISA = qw(Class::Gomor::Array Exporter);
11  
12 12 our %EXPORT_TAGS = (
13 consts => [qw(
14 NF_LAYER_NONE
15 NF_LAYER_UNKNOWN
16 NF_LAYER_NOT_AVAILABLE
17 )],
18 subs => [qw(
19 getHostIpv4Addr
20 getHostIpv4Addrs
21 getHostIpv6Addr
22 inetAton
23 inetNtoa
24 inet6Aton
25 inet6Ntoa
26 getRandomHighPort
27 getRandom32bitsInt
28 getRandom16bitsInt
29 convertMac
30 inetChecksum
31 )],
32 );
33 our @EXPORT_OK = (
34 12 @{$EXPORT_TAGS{consts}},
35 12 @{$EXPORT_TAGS{subs}},
36 );
37  
38 12 ▶use constant NF_LAYER_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 12 ▶use constant NF_LAYER_UNKNOWN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 12 ▶use constant NF_LAYER_NOT_AVAILABLE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 12 our @AS = qw(
43 raw
44 payload
45 nextLayer
46 );
47 12 __PACKAGE__->cgBuildIndices;
48 12 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
49  
50 12 ▶no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 9 ▶sub new { shift->SUPER::new(nextLayer => NF_LAYER_NONE, @_) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
55  
56 9 ▶sub layer {
Subroutine layer: called CC=1 SCAR=0.0
Pod documented
57 9 my $layer = ref(shift);
58 9 $layer =~ s/^Net::Frame::Layer:://;
59 9 $layer;
60 }
61  
62 # XXX: may use some optimizations
63 8 ▶sub pack {
Subroutine pack: called CC=2 SCAR=7.8
Pod documented
64 8 my $self = shift;
65 8 my ($fmt, @args) = @_;
66 8 my $res;
67 8 eval { $res = CORE::pack($fmt, @args) };
68 0 ▶ $@ ? do { carp("@{[ref($self)]}: unable to pack structure\n"); undef }
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 8 8
69 : $res;
70 }
71  
72 11 ▶sub unpack {
Subroutine unpack: called CC=2 SCAR=7.8
Pod documented
73 11 my $self = shift;
74 11 my ($fmt, $arg) = @_;
75 11 my @res;
76 11 eval { @res = CORE::unpack($fmt, $arg) };
77 0 ▶ $@ ? do { carp("@{[ref($self)]}: unable to unpack structure\n"); () }
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 11 11
78 : @res;
79 }
80  
81 0 ▶sub getPayloadLength {
Subroutine getPayloadLength: not called CC=2 SCAR=17.9
Pod documented
82 0 my $self = shift;
83 0 ▶ $self->payload ? length($self->payload) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->payload ? : 0 0 0
84 }
85  
86 0 ▶sub encapsulate { shift->nextLayer }
Subroutine encapsulate: not called CC=1 SCAR=6.9
Pod documented
87 0 ▶sub computeLengths { 1 }
Subroutine computeLengths: not called CC=1 SCAR=6.9
Pod documented
88 0 ▶sub computeChecksums { 1 }
Subroutine computeChecksums: not called CC=1 SCAR=6.9
Pod documented
89 0 ▶sub print { $self->layer.': to implement' }
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
90 0 ▶sub getLength { 0 }
Subroutine getLength: not called CC=1 SCAR=6.9
Pod documented
91  
92 0 ▶sub dump { CORE::unpack('H*', shift->raw) }
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
93  
94 #
95 # Useful subroutines
96 #
97  
98 # Load AF_INET and default imports from Socket. Safe back to at least 5.8.8.
99 12 ▶use Socket qw(:DEFAULT AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 12 ▶sub _setInet6Sub {
Subroutine _setInet6Sub: called CC=6 SCAR=23.2
102 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 12 my $inetp_found = 0;
105  
106 # Check Socket against some IPv6 functions and constants.
107 12 eval {
108 12 require Socket;
109 12 Socket->import(qw(AF_INET6 inet_pton inet_ntop));
110 };
111 12 ▶ if (! $@) { # Socket has support for required functions and constants.
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 12 0 12
112 12 *{__PACKAGE__.'::_inet_pton'} = \&Socket::inet_pton;
113 12 *{__PACKAGE__.'::_inet_ntop'} = \&Socket::inet_ntop;
114  
115 12 $inetp_found = 1;
116 }
117  
118 # Fallback to Socket6
119 12 ▶ if (! $inetp_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inetp_found) 0 12 12
120 0 eval {
121 0 require Socket6;
122 0 Socket6->import(qw(AF_INET6 inet_pton inet_ntop));
123 };
124 0 ▶ if (! $@) { # Socket6 has support for required functions and constants.
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
125 0 *{__PACKAGE__.'::_inet_pton'} = \&Socket6::inet_pton;
126 0 *{__PACKAGE__.'::_inet_ntop'} = \&Socket6::inet_ntop;
127 }
128 }
129  
130 # Unfortunately, we have to test if inet_ntop()/inet_pton() works (i.e., are implemented)
131 # If no support for inet_ntop/inet_pton, we branch to fake functions.
132 12 ▶ if ($inetp_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inetp_found) 12 0 12
133 12 eval {
134 # inet_pton() may exist, but die with:
135 # inet_pton not implemented on this architecture
136 12 _inet_pton(AF_INET6(), "::1");
137 };
138 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 12 12
139 0 print "[!] inet_pton support: $@\n";
140 0 ▶ *{__PACKAGE__.'::_inet_pton'} = sub { return 0; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 ▶ *{__PACKAGE__.'::_inet_ntop'} = sub { return 0; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 }
143 else {
144 12 return 1; # OK
145 }
146 }
147  
148 0 die("[-] Net::Frame: inet_pton/inet_ntop: not supported by Socket nor Socket6: ".
149 "try upgrading your Perl version or Socket/Socket6 modules.\n");
150 }
151  
152 12 ▶sub _setGetAddressSub {
Subroutine _setGetAddressSub: called CC=6 SCAR=24.8
153 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 12 my $getaddr_found = 0;
156  
157 # Check Socket against some IPv6 functions and constants.
158 12 eval {
159 12 require Socket;
160 12 Socket->import(qw(getaddrinfo getnameinfo AF_INET6));
161 };
162 12 ▶ if (! $@) { # Socket has support for required functions and constants.
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 12 0 12
163 12 ▶ *{__PACKAGE__.'::_getAddress'} = sub {
Subroutine __ANON__: called CC=4 SCAR=17.5
164 13 my ($name) = @_;
165  
166 #print STDERR "*** Socket supports IPv6 OK\n";
167  
168 13 my %hints = (
169 family => Socket::AF_INET6(),
170 );
171 13 my ($err, @res) = Socket::getaddrinfo($name, '', \%hints);
172 13 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 13 13
173 0 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getaddrinfo: $err\n");
174 0 return;
175 }
176 13 ▶ if (@res > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res > 0) { } 13 0 13
177 13 my $h = $res[0];
178 my ($err, $ipv6) = Socket::getnameinfo(
179 13 $h->{addr}, Socket::NI_NUMERICHOST() | Socket::NI_NUMERICSERV()
180 );
181 13 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 13 13
182 0 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getnameinfo: $err\n");
183 0 return;
184 }
185  
186 13 return $ipv6;
187 }
188 else {
189 0 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname: getaddrinfo: $!\n");
190 0 return;
191 }
192 12 };
193  
194 12 $getaddr_found = 1;
195 }
196  
197 # Fallback to Socket6
198 12 ▶ if (! $getaddr_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($getaddr_found) 0 12 12
199 0 eval {
200 0 require Socket6;
201 0 Socket6->import(qw(getaddrinfo getnameinfo AF_INET6));
202 };
203 0 ▶ if (! $@) { # Socket6 has support for required functions and constants.
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
204 0 ▶ *{__PACKAGE__.'::_getAddress'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 my ($name) = @_;
206  
207 #print STDERR "*** Fallback to Socket6 support\n";
208  
209 0 my @res = Socket6::getaddrinfo($name, '', Socket6::AF_INET6(), SOCK_STREAM);
210 0 ▶ if (@res >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res >= 5) 0 0 0
211 0 my ($ipv6) = Socket6::getnameinfo(
212 $res[3], Socket6::NI_NUMERICHOST() | Socket6::NI_NUMERICSERV()
213 );
214  
215 0 return $ipv6;
216 }
217 0 };
218 }
219  
220 0 $getaddr_found = 1;
221 }
222  
223 # Unfortunately, we have to test if INET6 family is supported
224 # If no support, we branch to fake functions.
225 12 ▶ if ($getaddr_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($getaddr_found) 12 0 12
226 12 eval {
227 # getaddrinfo() may exist, but die with:
228 # getaddrinfo: ai_family not supported
229 12 _getAddress("::1");
230 };
231 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 12 12
232 0 print "[!] getaddrinfo support: $@\n";
233 0 ▶ *{__PACKAGE__.'::_getAddress'} = sub { return 0; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 ▶ *{__PACKAGE__.'::_getAddress'} = sub { return 0; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 }
236 else {
237 12 return 1; # OK
238 }
239 }
240  
241 0 die("[-] Net::Frame: getaddrinfo/getnameinfo: not supported by Socket nor Socket6: ".
242 "try upgrading your Perl version or Socket/Socket6 modules.\n");
243 }
244  
245 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
246 12 _setInet6Sub();
247 12 _setGetAddressSub();
248 }
249  
250 12 require Net::IPv6Addr;
251  
252 1 ▶sub getHostIpv4Addr {
Subroutine getHostIpv4Addr: called CC=4 SCAR=17.3
Pod documented
253 1 my ($name) = @_;
254  
255 # No address given
256 1 ▶ if (! defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 1 1
257 0 return;
258 }
259  
260 # Already an IPv4 address
261 1 ▶ if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 1 1
262 0 return $name;
263 }
264  
265 1 my @addrs = (gethostbyname($name))[4];
266 1 ▶ @addrs ? return join('.', CORE::unpack('C4', $addrs[0]))
Branch: 1 branch
BranchTrueFalseTotal
@addrs ? : 1 0 1
267 0 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
268  
269 # Error
270 0 return;
271 }
272  
273 0 ▶sub getHostIpv4Addrs {
Subroutine getHostIpv4Addrs: not called CC=4 SCAR=30.0
Pod documented
274 0 my ($name) = @_;
275  
276 # No address given
277 0 ▶ if (! defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
278 0 return;
279 }
280  
281 # Already an IPv4 address
282 0 ▶ if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 0 0
283 0 return $name;
284 }
285  
286 0 my @addrs = (gethostbyname($name))[4];
287 0 ▶ @addrs ? return @addrs
Branch: 1 branch
BranchTrueFalseTotal
@addrs ? : 0 0 0
288 0 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
289  
290 # Error
291 0 return ();
292 }
293  
294 1 ▶sub getHostIpv6Addr {
Subroutine getHostIpv6Addr: called CC=4 SCAR=16.9
Pod documented
295 1 my ($name) = @_;
296  
297 # No address given
298 1 ▶ if (! defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 1 1
299 0 return;
300 }
301  
302 # Already an IPv6 address
303 1 ▶ if (Net::IPv6Addr::is_ipv6($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::IPv6Addr::is_ipv6($name)) 0 1 1
304 0 return $name;
305 }
306  
307 1 my $ipv6 = _getAddress($name);
308 1 ▶ if (! defined($ipv6)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ipv6) 0 1 1
309 0 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
310 0 return;
311 }
312  
313 1 $ipv6 =~ s/%.*$//;
314  
315 1 return $ipv6;
316 }
317  
318 5 ▶sub inetAton { Socket::inet_aton(shift()) }
Subroutine inetAton: called CC=1 SCAR=0.0
Pod documented
319 5 ▶sub inetNtoa { Socket::inet_ntoa(shift()) }
Subroutine inetNtoa: called CC=1 SCAR=0.0
Pod documented
320 1 ▶sub inet6Aton { _inet_pton(AF_INET6, shift()) }
Subroutine inet6Aton: called CC=1 SCAR=0.0
Pod documented
321 1 ▶sub inet6Ntoa { _inet_ntop(AF_INET6, shift()) }
Subroutine inet6Ntoa: called CC=1 SCAR=0.0
Pod documented
322  
323 2 ▶sub getRandomHighPort {
Subroutine getRandomHighPort: called CC=2 SCAR=7.1
Pod documented
324 2 my $highPort = int rand 0xffff;
325 2 ▶ $highPort += 1024 if $highPort < 1025;
Branch: 1 branch
BranchTrueFalseTotal
if $highPort < 1025 0 2 2
326 2 $highPort;
327 }
328  
329 1 ▶sub getRandom32bitsInt { int rand 0xffffffff }
Subroutine getRandom32bitsInt: called CC=1 SCAR=0.0
Pod documented
330 1 ▶sub getRandom16bitsInt { int rand 0xffff }
Subroutine getRandom16bitsInt: called CC=1 SCAR=0.0
Pod documented
331  
332 4 ▶sub convertMac {
Subroutine convertMac: called CC=1 SCAR=0.0
Pod documented
333 4 return lc(join(':', $_[0] =~ /../g));
334 }
335  
336 0 ▶sub inetChecksum {
Subroutine inetChecksum: not called CC=3 SCAR=24.8
Pod documented
337 0 my ($phpkt) = @_;
338  
339 0 ▶ $phpkt .= "\x00" if length($phpkt) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if length($phpkt) % 2 0 0 0
340 0 my $len = length $phpkt;
341 0 my $nshort = $len / 2;
342 0 my $checksum = 0;
343 0 $checksum += $_ for CORE::unpack("S$nshort", $phpkt);
344 # XXX: This line never does anything as the lenth was made even above. Currently testing it breaks nothing.
345 #$checksum += CORE::unpack('C', substr($phpkt, $len - 1, 1)) if $len % 2;
346 0 $checksum = ($checksum >> 16) + ($checksum & 0xffff);
347  
348 0 CORE::unpack('n',
349 CORE::pack('S', ~(($checksum >> 16) + $checksum) & 0xffff),
350 );
351 }
352  
353 12 1;
354  
355 __END__
« Frame/Layer/ARP.pm ↑ Summary Frame/Layer/IPv4.pm »