Frame/Layer/IPv4.pm

filter: Statementstmt 85.1% 172 / 202 Branchbran 28.1% 9 / 32 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.0% 46 / 50 Podpod 100% 10 / 10 totaltotal 79.6% 239 / 300 CC 20 SCAR 17.1
CC
20
Cov
77%
computeLengths
42.0
getPayloadLength
6.0
computeChecksums
6.0
CRAP 5.5
« Frame/Layer.pm ↑ Summary Frame/Layer/ETH.pm »
1 #
2 # $Id: IPv4.pm,v ce68fbcc7f6d 2019/05/23 05:58:40 gomor $
3 #
4 package Net::Frame::Layer::IPv4;
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Net::Frame::Layer qw(:consts :subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 require Exporter;
10 3 our @ISA = qw(Net::Frame::Layer Exporter);
11  
12 3 our %EXPORT_TAGS = (
13 consts => [qw(
14 NF_IPv4_HDR_LEN
15 NF_IPv4_PROTOCOL_ICMPv4
16 NF_IPv4_PROTOCOL_IGMP
17 NF_IPv4_PROTOCOL_IPIP
18 NF_IPv4_PROTOCOL_TCP
19 NF_IPv4_PROTOCOL_EGP
20 NF_IPv4_PROTOCOL_IGRP
21 NF_IPv4_PROTOCOL_CHAOS
22 NF_IPv4_PROTOCOL_UDP
23 NF_IPv4_PROTOCOL_IDP
24 NF_IPv4_PROTOCOL_DCCP
25 NF_IPv4_PROTOCOL_IPv6
26 NF_IPv4_PROTOCOL_IPv6ROUTING
27 NF_IPv4_PROTOCOL_IPv6FRAGMENT
28 NF_IPv4_PROTOCOL_IDRP
29 NF_IPv4_PROTOCOL_RSVP
30 NF_IPv4_PROTOCOL_GRE
31 NF_IPv4_PROTOCOL_ESP
32 NF_IPv4_PROTOCOL_AH
33 NF_IPv4_PROTOCOL_ICMPv6
34 NF_IPv4_PROTOCOL_EIGRP
35 NF_IPv4_PROTOCOL_OSPF
36 NF_IPv4_PROTOCOL_ETHERIP
37 NF_IPv4_PROTOCOL_PIM
38 NF_IPv4_PROTOCOL_VRRP
39 NF_IPv4_PROTOCOL_STP
40 NF_IPv4_PROTOCOL_SCTP
41 NF_IPv4_PROTOCOL_UDPLITE
42 NF_IPv4_MORE_FRAGMENT
43 NF_IPv4_DONT_FRAGMENT
44 NF_IPv4_RESERVED_FRAGMENT
45 )],
46 );
47 our @EXPORT_OK = (
48 3 @{$EXPORT_TAGS{consts}},
49 );
50  
51 3 ▶use constant NF_IPv4_HDR_LEN => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use constant NF_IPv4_PROTOCOL_ICMPv4 => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶use constant NF_IPv4_PROTOCOL_IGMP => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use constant NF_IPv4_PROTOCOL_IPIP => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use constant NF_IPv4_PROTOCOL_TCP => 0x06;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use constant NF_IPv4_PROTOCOL_EGP => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use constant NF_IPv4_PROTOCOL_IGRP => 0x09;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use constant NF_IPv4_PROTOCOL_CHAOS => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use constant NF_IPv4_PROTOCOL_UDP => 0x11;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use constant NF_IPv4_PROTOCOL_IDP => 0x16;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use constant NF_IPv4_PROTOCOL_DCCP => 0x21;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶use constant NF_IPv4_PROTOCOL_IPv6 => 0x29;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use constant NF_IPv4_PROTOCOL_IPv6ROUTING => 0x2b;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use constant NF_IPv4_PROTOCOL_IPv6FRAGMENT => 0x2c;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 ▶use constant NF_IPv4_PROTOCOL_IDRP => 0x2d;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use constant NF_IPv4_PROTOCOL_RSVP => 0x2e;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use constant NF_IPv4_PROTOCOL_GRE => 0x2f;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 3 ▶use constant NF_IPv4_PROTOCOL_ESP => 0x32;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 3 ▶use constant NF_IPv4_PROTOCOL_AH => 0x33;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use constant NF_IPv4_PROTOCOL_ICMPv6 => 0x3a;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶use constant NF_IPv4_PROTOCOL_EIGRP => 0x58;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 3 ▶use constant NF_IPv4_PROTOCOL_OSPF => 0x59;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 3 ▶use constant NF_IPv4_PROTOCOL_ETHERIP => 0x61;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 ▶use constant NF_IPv4_PROTOCOL_PIM => 0x67;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use constant NF_IPv4_PROTOCOL_VRRP => 0x70;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶use constant NF_IPv4_PROTOCOL_STP => 0x76;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use constant NF_IPv4_PROTOCOL_SCTP => 0x84;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 3 ▶use constant NF_IPv4_PROTOCOL_UDPLITE => 0x88;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 3 ▶use constant NF_IPv4_MORE_FRAGMENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 3 ▶use constant NF_IPv4_DONT_FRAGMENT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 3 ▶use constant NF_IPv4_RESERVED_FRAGMENT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 3 our @AS = qw(
84 id
85 ttl
86 src
87 dst
88 protocol
89 checksum
90 flags
91 offset
92 version
93 tos
94 length
95 hlen
96 options
97 noFixLen
98 );
99 3 __PACKAGE__->cgBuildIndices;
100 3 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
101  
102 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
103 3 my $osname = {
104 freebsd => [ \&_fixLenBsd, ],
105 netbsd => [ \&_fixLenBsd, ],
106 openbsd => [ \&_fixLenBsd, ],
107 darwin => [ \&_fixLenBsd, ],
108 };
109  
110 3 ▶ *_fixLen = $osname->{$^O}->[0] || \&_fixLenOther;
Condition50%1/2
$osname->{$^O}[0] || \&Net::Frame::Layer::IPv4::_fixLenOther
l!l
03
MC/DC0%0/1
$osname->{$^O}[0] || \&Net::Frame::Layer::IPv4::_fixLenOther
$osname->{$^O}[0]
Truth table50%1/2
A
A= $osname->{$^O}[0]
Aresult
11
00
111 }
112  
113 3 ▶no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 3 ▶use Bit::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 0 ▶sub _fixLenBsd { pack('v', shift) }
Subroutine _fixLenBsd: not called CC=1 SCAR=6.9
119 1 ▶sub _fixLenOther { pack('n', shift) }
Subroutine _fixLenOther: called CC=1 SCAR=0.0
120  
121 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
122 shift->SUPER::new(
123 1 version => 4,
124 tos => 0,
125 id => getRandom16bitsInt(),
126 length => NF_IPv4_HDR_LEN,
127 hlen => 5,
128 flags => 0,
129 offset => 0,
130 ttl => 128,
131 protocol => NF_IPv4_PROTOCOL_TCP,
132 checksum => 0,
133 src => '127.0.0.1',
134 dst => '127.0.0.1',
135 options => '',
136 noFixLen => 0,
137 @_,
138 );
139 }
140  
141 1 ▶sub pack {
Subroutine pack: called CC=5 SCAR=17.6
Pod documented
142 1 my $self = shift;
143  
144 # Here, we pack in this order: version, hlen (4 bits each)
145 1 my $version = Bit::Vector->new_Dec(4, $self->[$__version]);
146 1 my $hlen = Bit::Vector->new_Dec(4, $self->[$__hlen]);
147 1 my $v8 = $version->Concat_List($hlen);
148  
149 # Here, we pack in this order: flags (3 bits), offset (13 bits)
150 1 my $flags = Bit::Vector->new_Dec(3, $self->[$__flags]);
151 1 my $offset = Bit::Vector->new_Dec(13, $self->[$__offset]);
152 1 my $v16 = $flags->Concat_List($offset);
153  
154 1 ▶ my $len = ($self->[$__noFixLen] ? _fixLenOther($self->[$__length])
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__noFixLen] ? : 0 1 1
155 : _fixLen($self->[$__length]));
156  
157 1 ▶ $self->[$__raw] = $self->SUPER::pack('CCa*nnCCna4a4',
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__raw] = $self->SUPER::pack("CCa*nnCCna4a4", $v8->to_Dec, $self->[$__tos], $len, $self->[$__id], $v16->to_Dec, $self->[$__ttl], $self->[$__protocol], $self->[$__checksum], inetAton($self->[$__src]), inetAton($self->[$__dst])) or return undef 1 0 1
158 $v8->to_Dec,
159 $self->[$__tos],
160 $len,
161 $self->[$__id],
162 $v16->to_Dec,
163 $self->[$__ttl],
164 $self->[$__protocol],
165 $self->[$__checksum],
166 inetAton($self->[$__src]),
167 inetAton($self->[$__dst]),
168 ) or return undef;
169  
170 1 my $opt;
171 1 ▶ if ($self->[$__options]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$__options]) 0 1 1
172 0 ▶ $opt = $self->SUPER::pack('a*', $self->[$__options])
Branch: 1 branch
BranchTrueFalseTotal
$opt = $self->SUPER::pack("a*", $self->[$__options]) or return undef 0 0 0
173 or return undef;
174 0 $self->[$__raw] = $self->[$__raw].$opt;
175 }
176  
177 1 $self->[$__raw];
178 }
179  
180 1 ▶sub unpack {
Subroutine unpack: called CC=3 SCAR=11.0
Pod documented
181 1 my $self = shift;
182  
183 1 ▶ my ($verHlen, $tos, $len, $id, $flagsOffset, $ttl, $proto, $cksum, $src,
Branch: 1 branch
BranchTrueFalseTotal
() = $self->SUPER::unpack("CCnnnCCna4a4 a*", $self->[$__raw]) or return undef 1 0 1
184 $dst, $payload) = $self->SUPER::unpack('CCnnnCCna4a4 a*', $self->[$__raw])
185 or return undef;
186  
187 1 my $v8 = Bit::Vector->new_Dec(8, $verHlen);
188 1 my $v16 = Bit::Vector->new_Dec(16, $flagsOffset);
189  
190 # Here, we unpack in this order: hlen, version (4 bits each)
191 1 $self->[$__hlen] = $v8->Chunk_Read(4, 0);
192 1 $self->[$__version] = $v8->Chunk_Read(4, 4);
193 1 $self->[$__tos] = $tos;
194 1 $self->[$__length] = $len;
195 1 $self->[$__id] = $id;
196 # Here, we unpack in this order: offset (13 bits), flags (3 bits)
197 1 $self->[$__offset] = $v16->Chunk_Read(13, 0);
198 1 $self->[$__flags] = $v16->Chunk_Read( 3, 13);
199 1 $self->[$__ttl] = $ttl;
200 1 $self->[$__protocol] = $proto;
201 1 $self->[$__checksum] = $cksum;
202 1 $self->[$__src] = inetNtoa($src);
203 1 $self->[$__dst] = inetNtoa($dst);
204 1 $self->[$__payload] = $payload;
205  
206 1 ▶ my ($options, $payload2) = $self->SUPER::unpack(
Branch: 1 branch
BranchTrueFalseTotal
() = $self->SUPER::unpack('a' . $self->getOptionsLength . 'a*', $self->[$__payload]) or return undef 1 0 1
207 'a'. $self->getOptionsLength. 'a*', $self->[$__payload]
208 ) or return undef;
209  
210 1 $self->[$__options] = $options;
211 1 $self->[$__payload] = $payload2;
212  
213 1 $self;
214 }
215  
216 1 ▶sub getLength {
Subroutine getLength: called CC=2 SCAR=7.2
Pod documented
217 1 my $self = shift;
218 1 ▶ $self->[$__hlen] > 0 ? $self->[$__hlen] * 4 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__hlen] > 0 ? : 1 0 1
219 }
220  
221 0 ▶sub getPayloadLength {
Subroutine getPayloadLength: not called CC=2 SCAR=17.9
Pod documented
222 0 my $self = shift;
223 0 my $gLen = $self->getLength;
224 0 ▶ $self->[$__length] > $gLen ? $self->[$__length] - $gLen : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->[$__length] > $gLen ? : 0 0 0
225 }
226  
227 1 ▶sub getOptionsLength {
Subroutine getOptionsLength: called CC=2 SCAR=7.0
Pod documented
228 1 my $self = shift;
229 1 my $gLen = $self->getLength;
230 1 my $hLen = NF_IPv4_HDR_LEN;
231 1 ▶ $gLen > $hLen ? $gLen - $hLen : 0;
Branch: 1 branch
BranchTrueFalseTotal
$gLen > $hLen ? : 0 1 1
232 }
233  
234 0 ▶sub computeLengths {
Subroutine computeLengths: not called CC=6 SCAR=37.4
Pod documented
235 0 my $self = shift;
236 0 my ($layers) = @_;
237  
238 0 my $hLen = NF_IPv4_HDR_LEN;
239 0 ▶ $hLen += length($self->[$__options]) if $self->[$__options];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$__options] 0 0 0
240 0 $self->[$__hlen] = $hLen / 4;
241  
242 0 my $len = $hLen;
243 0 my $last;
244 my $start;
245 0 for my $l (@$layers) {
246 0 ▶ if (! $start) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start) 0 0 0
247 0 ▶ $start++ if $l->layer eq 'IPv4';
Branch: 1 branch
BranchTrueFalseTotal
if $l->layer eq "IPv4" 0 0 0
248 0 next;
249 }
250 0 $len += $l->getLength;
251 0 $last = $l;
252 }
253 0 ▶ if (defined($last->payload)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last->payload) 0 0 0
254 0 $len += length($last->payload);
255 }
256  
257 0 $self->length($len);
258  
259 0 return 1;
260 }
261  
262 0 ▶sub computeChecksums {
Subroutine computeChecksums: not called CC=2 SCAR=17.9
Pod documented
263 0 my $self = shift;
264 0 my ($layers) = @_;
265  
266 # Reset the checksum if already filled by a previous pack
267 0 ▶ if ($self->[$__checksum]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$__checksum]) 0 0 0
268 0 $self->[$__checksum] = 0;
269 }
270  
271 0 $self->[$__checksum] = inetChecksum($self->pack);
272  
273 0 return 1;
274 }
275  
276 3 our $Next = {
277 NF_IPv4_PROTOCOL_ICMPv4() => 'ICMPv4',
278 NF_IPv4_PROTOCOL_IGMP() => 'IGMP',
279 NF_IPv4_PROTOCOL_IPIP() => 'IPv4',
280 NF_IPv4_PROTOCOL_TCP() => 'TCP',
281 NF_IPv4_PROTOCOL_EGP() => 'EGP',
282 NF_IPv4_PROTOCOL_IGRP() => 'IGRP',
283 NF_IPv4_PROTOCOL_CHAOS() => 'CHAOS',
284 NF_IPv4_PROTOCOL_UDP() => 'UDP',
285 NF_IPv4_PROTOCOL_IDP() => 'IDP',
286 NF_IPv4_PROTOCOL_DCCP() => 'DCCP',
287 NF_IPv4_PROTOCOL_IPv6() => 'IPv6',
288 NF_IPv4_PROTOCOL_IPv6ROUTING() => 'IPv6Routing',
289 NF_IPv4_PROTOCOL_IPv6FRAGMENT() => 'IPv6Fragment',
290 NF_IPv4_PROTOCOL_IDRP() => 'IDRP',
291 NF_IPv4_PROTOCOL_RSVP() => 'RSVP',
292 NF_IPv4_PROTOCOL_GRE() => 'GRE',
293 NF_IPv4_PROTOCOL_ESP() => 'ESP',
294 NF_IPv4_PROTOCOL_AH() => 'AH',
295 NF_IPv4_PROTOCOL_ICMPv6() => 'ICMPv6',
296 NF_IPv4_PROTOCOL_EIGRP() => 'EIGRP',
297 NF_IPv4_PROTOCOL_OSPF() => 'OSPF',
298 NF_IPv4_PROTOCOL_ETHERIP() => 'ETHERIP',
299 NF_IPv4_PROTOCOL_PIM() => 'PIM',
300 NF_IPv4_PROTOCOL_VRRP() => 'VRRP',
301 NF_IPv4_PROTOCOL_STP() => 'STP',
302 NF_IPv4_PROTOCOL_SCTP() => 'SCTP',
303 NF_IPv4_PROTOCOL_UDPLITE() => 'UDPLite',
304 };
305  
306 1 ▶sub encapsulate {
Subroutine encapsulate: called CC=3 SCAR=11.7
Pod documented
307 1 my $self = shift;
308  
309 1 ▶ return $self->[$__nextLayer] if $self->[$__nextLayer];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$__nextLayer] 0 1 1
310  
311 1 ▶ return $Next->{$self->[$__protocol]} || NF_LAYER_UNKNOWN;
Condition50%1/2
$$Next{$self->[$__protocol]} || 1
l!l
10
MC/DC0%0/1
$$Next{$self->[$__protocol]} || 1
$$Next{$self->[$__protocol]}
Truth table50%1/2
A
A= $$Next{$self->[$__protocol]}
Aresult
11
00
312 }
313  
314 1 ▶sub print {
Subroutine print: called CC=2 SCAR=7.2
Pod documented
315 1 my $self = shift;
316  
317 1 my $l = $self->layer;
318 1 my $buf = sprintf
319 "$l: version:%d hlen:%d tos:0x%02x length:%d id:%d\n".
320 "$l: flags:0x%02x offset:%d ttl:%d protocol:0x%02x checksum:0x%04x\n".
321 "$l: src:%s dst:%s",
322 $self->[$__version], $self->[$__hlen], $self->[$__tos],
323 $self->[$__length], $self->[$__id], $self->[$__flags],
324 $self->[$__offset], $self->[$__ttl], $self->[$__protocol],
325 $self->[$__checksum], $self->[$__src], $self->[$__dst];
326  
327 1 ▶ if ($self->[$__options]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$__options]) 0 1 1
328 0 $buf .= sprintf "\n$l: optionsLength:%d options:%s",
329 $self->getOptionsLength,
330 CORE::unpack('H*', $self->[$__options]);
331 }
332  
333 1 $buf;
334 }
335  
336 3 1;
337  
338 __END__
« Frame/Layer.pm ↑ Summary Frame/Layer/ETH.pm »