Frame/Utils.pm

filter: Statementstmt 18.9% 15 / 79 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.3% 5 / 19 Podpod n/a totaltotal 16.3% 20 / 122 CC 22 SCAR 29.7
CC
22
Cov
15%
explodeIps
56.0
getHostIpv4Addr
20.0
getHostIpv4Addrs
20.0
CRAP 19.5
↑ Summary Frame/RAW.pm »
1 #
2 # $Id: Utils.pm,v 1.5 2006/12/03 16:07:35 gomor Exp $
3 #
4 package Net::Frame::Utils;
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 require Exporter;
9 our @ISA = qw(Exporter);
10  
11 our @EXPORT_OK = qw(
12 getHostIpv4Addr
13 getHostIpv4Addrs
14 getHostIpv6Addr
15 getRandomHighPort
16 getRandom32bitsInt
17 getRandom16bitsInt
18 convertMac
19 inetChecksum
20 inetAton
21 inetNtoa
22 inet6Aton
23 inet6Ntoa
24 explodeIps
25 explodePorts
26 );
27  
28 our %EXPORT_TAGS = (
29 all => [ @EXPORT_OK ],
30 );
31  
32 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use Socket6 qw(NI_NUMERICHOST NI_NUMERICSERV inet_pton inet_ntop);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 require Net::Libdnet;
36 require Net::IPv6Addr;
37  
38 sub getHostIpv4Addr {
39 0 ▶ my $name = shift;
Subroutine getHostIpv4Addr: not called CC=4 SCAR=30.0
40  
41 0 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
42 0 ▶ return $name 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
43  
44 0 my @addrs = (gethostbyname($name))[4];
45 0 ▶ @addrs ? return join('.', unpack('C4', $addrs[0]))
Branch: 1 branch
BranchTrueFalseTotal
@addrs ? : 0 0 0
46 0 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
47 0 return undef;
48 }
49  
50 sub getHostIpv4Addrs {
51 0 ▶ my $name = shift;
Subroutine getHostIpv4Addrs: not called CC=4 SCAR=30.0
52  
53 0 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
54 0 ▶ return $name 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
55  
56 0 my @addrs = (gethostbyname($name))[4];
57 0 ▶ @addrs ? return @addrs
Branch: 1 branch
BranchTrueFalseTotal
@addrs ? : 0 0 0
58 0 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
59 0 return ();
60 }
61  
62 sub getHostIpv6Addr {
63 0 ▶ my $name = shift;
Subroutine getHostIpv6Addr: not called CC=4 SCAR=30.0
64  
65 0 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
66 0 ▶ return $name if Net::IPv6Addr::is_ipv6($name);
Branch: 1 branch
BranchTrueFalseTotal
if Net::IPv6Addr::is_ipv6($name) 0 0 0
67  
68 0 my @res = getaddrinfo($name, 'ssh', AF_INET6, SOCK_STREAM);
69 0 ▶ if (@res >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res >= 5) { } 0 0 0
70 0 my ($ipv6) = getnameinfo($res[3], NI_NUMERICHOST | NI_NUMERICSERV);
71 0 $ipv6 =~ s/%.*$//;
72 0 return $ipv6;
73 }
74 else {
75 0 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
76 }
77 0 return undef;
78 }
79  
80 0 ▶sub inetAton { inet_aton(shift()) }
Subroutine inetAton: not called CC=1 SCAR=6.9
81 0 ▶sub inetNtoa { inet_ntoa(shift()) }
Subroutine inetNtoa: not called CC=1 SCAR=6.9
82 0 ▶sub inet6Aton { inet_pton(AF_INET6, shift()) }
Subroutine inet6Aton: not called CC=1 SCAR=6.9
83 0 ▶sub inet6Ntoa { inet_ntop(AF_INET6, shift()) }
Subroutine inet6Ntoa: not called CC=1 SCAR=6.9
84  
85 sub getRandomHighPort {
86 0 ▶ my $highPort = int rand 0xffff;
Subroutine getRandomHighPort: not called CC=2 SCAR=17.9
87 0 ▶ $highPort += 1024 if $highPort < 1025;
Branch: 1 branch
BranchTrueFalseTotal
if $highPort < 1025 0 0 0
88 0 return $highPort;
89 }
90  
91 0 ▶sub getRandom32bitsInt { int rand 0xffffffff }
Subroutine getRandom32bitsInt: not called CC=1 SCAR=6.9
92 0 ▶sub getRandom16bitsInt { int rand 0xffff }
Subroutine getRandom16bitsInt: not called CC=1 SCAR=6.9
93  
94 sub convertMac {
95 0 ▶ my $mac = shift;
Subroutine convertMac: not called CC=1 SCAR=6.9
96 0 $mac =~ s/(..)/$1:/g;
97 0 $mac =~ s/:$//;
98 0 return lc $mac;
99 }
100  
101 sub inetChecksum {
102 0 ▶ my $phpkt = shift;
Subroutine inetChecksum: not called CC=4 SCAR=30.0
103  
104 0 ▶ $phpkt .= "\x00" if length($phpkt) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if length($phpkt) % 2 0 0 0
105 0 my $len = length $phpkt;
106 0 my $nshort = $len / 2;
107 0 my $checksum = 0;
108 0 $checksum += $_ for unpack("S$nshort", $phpkt);
109 0 ▶ $checksum += unpack('C', substr($phpkt, $len - 1, 1)) if $len % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len % 2 0 0 0
110 0 $checksum = ($checksum >> 16) + ($checksum & 0xffff);
111  
112 0 unpack('n', pack('S', ~(($checksum >> 16) + $checksum) & 0xffff));
113 }
114  
115 sub explodePorts {
116 0 ▶ my @ports;
Subroutine explodePorts: not called CC=3 SCAR=24.8
117 0 do { s/-/../g; push @ports, $_ for eval } for split /,/, shift();
118 0 @ports;
119 }
120  
121 sub explodeIps {
122 0 ▶ my @ips;
Subroutine explodeIps: not called CC=7 SCAR=40.3
123 0 for (split(/,/, shift())) {
124 0 my @bytes;
125 0 do { s/-/../g; push @bytes, $_ } for split(/\./);
126 0 for my $b1 (eval($bytes[0])) {
127 0 for my $b2 (eval($bytes[1])) {
128 0 for my $b3 (eval($bytes[2])) {
129 0 for my $b4 (eval($bytes[3])) {
130 0 push @ips, "$b1.$b2.$b3.$b4";
131 }
132 }
133 }
134 }
135 }
136 0 @ips;
137 }
138  
139 1;
140  
141 __END__
↑ Summary Frame/RAW.pm »