Packet/Utils.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 154 CC 13 SCAR 22.3
CC
13
Cov
0%
getHostIpv4Addr
20.0
getHostIpv4Addrs
20.0
getHostIpv6Addr
20.0
CRAP 9.3
1 #
2 # $Id: Utils.pm,v 1.2.2.2 2006/05/31 16:43:41 gomor Exp $
3 #
4 package Net::Packet::Utils;
5 use strict;
6 use warnings;
7 use Carp;
8  
9 require Exporter;
10 our @ISA = qw(Exporter);
11  
12 our @EXPORT_OK = qw(
13 getHostIpv4Addr
14 getHostIpv4Addrs
15 getHostIpv6Addr
16 getRandomHighPort
17 getRandom32bitsInt
18 getRandom16bitsInt
19 convertMac
20 unpackIntFromNet
21 packIntToNet
22 inetChecksum
23 inetAton
24 inetNtoa
25 inet6Aton
26 inet6Ntoa
27 explodeIps
28 explodePorts
29 );
30  
31 our %EXPORT_TAGS = (
32 all => [ @EXPORT_OK ],
33 );
34  
35 use Socket;
36 use Socket6;
37 require Net::IPv6Addr;
38  
39 sub getHostIpv4Addr {
40 my $name = shift;
41  
42 return undef unless $name;
43 return $name if $name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
44  
45 my @addrs = (gethostbyname($name))[4];
46 @addrs ? return join('.', unpack('C4', $addrs[0]))
47 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
48 return undef;
49 }
50  
51 sub getHostIpv4Addrs {
52 my $name = shift;
53  
54 return undef unless $name;
55 return $name if $name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
56  
57 my @addrs = (gethostbyname($name))[4];
58 @addrs ? return @addrs
59 : carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
60 return ();
61 }
62  
63 sub getHostIpv6Addr {
64 my $name = shift;
65  
66 return undef unless $name;
67 return $name if Net::IPv6Addr::is_ipv6($name);
68  
69 my @res = getaddrinfo($name, 'ssh', AF_INET6, SOCK_STREAM);
70 if (@res >= 5) {
71 my ($ipv6) = getnameinfo($res[3], NI_NUMERICHOST | NI_NUMERICSERV);
72 $ipv6 =~ s/%.*$//;
73 return $ipv6;
74 }
75 else {
76 carp("@{[(caller(0))[3]]}: unable to resolv `$name' hostname\n");
77 }
78 return undef;
79 }
80  
81 sub inetAton { inet_aton(shift()) }
82 sub inetNtoa { inet_ntoa(shift()) }
83 sub inet6Aton { inet_pton(AF_INET6, shift()) }
84 sub inet6Ntoa { inet_ntop(AF_INET6, shift()) }
85  
86 sub getRandomHighPort {
87 my $highPort = int rand 0xffff;
88 $highPort += 1024 if $highPort < 1025;
89 return $highPort;
90 }
91  
92 sub getRandom32bitsInt { int rand 0xffffffff }
93 sub getRandom16bitsInt { int rand 0xffff }
94  
95 sub convertMac {
96 my $mac = shift;
97 $mac =~ s/(..)/$1:/g;
98 $mac =~ s/:$//;
99 return lc $mac;
100 }
101  
102 sub unpackIntFromNet {
103 my ($net, $format, $offset, $pad, $bit) = @_;
104 unpack($format, pack('B*', 0 x $pad . substr($net, $offset, $bit)));
105 }
106  
107 sub packIntToNet {
108 my ($int, $format, $offset, $bit) = @_;
109 substr(unpack('B*', pack($format, $int << $bit)), $offset, $bit);
110 }
111  
112 sub inetChecksum {
113 my $phpkt = shift;
114  
115 $phpkt .= "\x00" if length($phpkt) % 2;
116 my $len = length $phpkt;
117 my $nshort = $len / 2;
118 my $checksum = 0;
119 $checksum += $_ for unpack("S$nshort", $phpkt);
120 $checksum += unpack('C', substr($phpkt, $len - 1, 1)) if $len % 2;
121 $checksum = ($checksum >> 16) + ($checksum & 0xffff);
122  
123 unpack('n', pack('S', ~(($checksum >> 16) + $checksum) & 0xffff));
124 }
125  
126 sub explodePorts {
127 my @ports;
128 do { s/-/../g; push @ports, $_ for eval } for split /,/, shift();
129 @ports;
130 }
131  
132 sub explodeIps {
133 my @ips;
134 for (split(/,/, shift())) {
135 my @bytes;
136 do { s/-/../g; push @bytes, $_ } for split(/\./);
137 for my $b1 (eval($bytes[0])) {
138 for my $b2 (eval($bytes[1])) {
139 for my $b3 (eval($bytes[2])) {
140 for my $b4 (eval($bytes[3])) {
141 push @ips, "$b1.$b2.$b3.$b4";
142 }
143 }
144 }
145 }
146 }
147 @ips;
148 }
149  
150 1;
151  
152 =head1 NAME
153  
154 Net::Packet::Utils - useful subroutines used in Net::Packet
155  
156 =head1 SYNOPSIS
157  
158 # Load all subroutines
159 use Net::Packet::Utils qw(:all);
160  
161 # Load only specific subroutines
162 use Net::Packet::Utils qw(explodeIps explodePorts);
163  
164 my @ips = explodeIps('192.168.0.1-254,192.168.1.1');
165 my @ports = explodePorts('1-1024,6000');
166  
167 print "@ips\n";
168 print "@ports\n";
169  
170 =head1 DESCRIPTION
171  
172 This module is not object oriented, it just implements some utilities used accros Net::Packet framework. They may be useful in other modules too, so here lies their descriptions.
173  
174 =head1 SUBROUTINES
175  
176 =over 4
177  
178 =item B<getHostIpv4Addr> (scalar)
179  
180 Tries to resolve hostname passed as an argument. Returns its IP address.
181  
182 =item B<getHostIpv4Addrs> (scalar)
183  
184 Tries to resolve hostname passed as an argument. Returns an array of IP addresses.
185  
186 =item B<getHostIpv6Addr> (scalar)
187  
188 Tries to resolve hostname passed as an argument. Returns its IPv6 address.
189  
190 =item B<inetAton> (scalar)
191  
192 Returns numeric value of IP address passed as an argument.
193  
194 =item B<inetNtoa> (scalar)
195  
196 Returns IP address of numeric value passed as an argument.
197  
198 =item B<inet6Aton> (scalar)
199  
200 Returns numeric value of IPv6 address passed as an argument.
201  
202 =item B<inet6Ntoa> (scalar)
203  
204 Returns IPv6 address of numeric value passed as an argument.
205  
206 =item B<getRandomHighPort>
207  
208 Returns a port number for direct use as source in a TCP or UDP header (that is a port between 1025 and 65535).
209  
210 =item B<getRandom32bitsInt>
211  
212 Returns a random integer of 32 bits in length.
213  
214 =item B<getRandom16bitsInt>
215  
216 Returns a random integer of 16 bits in length.
217  
218 =item B<convertMac> (scalar)
219  
220 Converts a MAC address from network format to human format.
221  
222 =item B<unpackIntFromNet> (scalar, scalar, scalar, scalar, scalar)
223  
224 Almost used internally, to convert network bits to integers. First argument is what to convert, second is an unpack format, third the offset of first argument where bits to get begins, the fourth are padding bits to achieve the length we need, and the last is the number of bits to get from offset argument.
225  
226 =item B<packIntToNet> (scalar, scalar, scalar, scalar)
227  
228 Almost used internally, to convert integers to network bits. First argument is what to convert, second is a pack format, third the offset where to store the first argument, and the last the number of bits the integer will be once packed.
229  
230 =item B<inetChecksum> (scalar)
231  
232 Compute the INET checksum used in various layers.
233  
234 =item B<explodePorts>
235  
236 =item B<explodeIps>
237  
238 See B<SYNOPSIS>.
239  
240 =back
241  
242 =head1 AUTHOR
243  
244 Patrice E<lt>GomoRE<gt> Auffret
245  
246 =head1 COPYRIGHT AND LICENSE
247  
248 Copyright (c) 2004-2006, Patrice E<lt>GomoRE<gt> Auffret
249
250 You may distribute this module under the terms of the Artistic license.
251 See LICENSE.Artistic file in the source distribution archive.
252
253 =head1 RELATED MODULES
254  
255 L<NetPacket>, L<Net::RawIP>, L<Net::RawSock>
256
257 =cut