Packet/IPv6.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 103 CC 3 SCAR 12.8
CC
3
Cov
0%
_computePayloadLength
12.0
BEGIN
2.0
new
2.0
CRAP 3.6
1 #
2 # $Id: IPv6.pm,v 1.3.2.4 2006/06/11 09:46:30 gomor Exp $
3 #
4 package Net::Packet::IPv6;
5 use strict;
6 use warnings;
7  
8 require Net::Packet::Layer3;
9 our @ISA = qw(Net::Packet::Layer3);
10  
11 use Net::Packet::Env qw($Env);
12 use Net::Packet::Utils qw(unpackIntFromNet packIntToNet
13 inet6Aton inet6Ntoa);
14 use Net::Packet::Consts qw(:ipv6 :layer);
15  
16 BEGIN {
17 *protocol = \&nextHeader;
18 }
19  
20 our @AS = qw(
21 version
22 trafficClass
23 flowLabel
24 nextHeader
25 payloadLength
26 hopLimit
27 src
28 dst
29 );
30 __PACKAGE__->cgBuildIndices;
31 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
32  
33 no strict 'vars';
34  
35 sub new {
36 my $self = shift->SUPER::new(
37 version => 6,
38 trafficClass => 0,
39 flowLabel => 0,
40 nextHeader => NP_IPv6_PROTOCOL_TCP,
41 hopLimit => 0xff,
42 src => $Env->ip6,
43 dst => '::1',
44 @_,
45 );
46  
47 $self;
48 }
49  
50 sub getLength { NP_IPv6_HDR_LEN }
51 sub getPayloadLength { shift->[$__payloadLength] }
52  
53 sub _computePayloadLength {
54 my $self = shift;
55 my ($frame) = @_;
56  
57 my $len = 0;
58 $len += $frame->l4->getLength if $frame->l4;
59 $len += $frame->l7->getLength if $frame->l7;
60 $self->[$__payloadLength] = $len;
61 }
62  
63 sub computeLengths {
64 my $self = shift;
65 my ($frame) = @_;
66  
67 $frame->l4->computeLengths($frame) or return undef;
68 $self->_computePayloadLength($frame);
69 1;
70 }
71  
72 sub pack {
73 my $self = shift;
74  
75 my $vtf1 = packIntToNet($self->[$__version], 'C', 0, 4);
76 my $vtf2 = packIntToNet($self->[$__trafficClass], 'C', 4, 8);
77 my $vtf3 = packIntToNet($self->[$__flowLabel], 'N', 12, 20);
78  
79 $self->[$__raw] = $self->SUPER::pack('B32nCCa*a*',
80 $vtf1.$vtf2.$vtf3,
81 $self->[$__payloadLength],
82 $self->[$__nextHeader],
83 $self->[$__hopLimit],
84 inet6Aton($self->[$__src]),
85 inet6Aton($self->[$__dst]),
86 ) or return undef;
87  
88 1;
89 }
90  
91 sub unpack {
92 my $self = shift;
93  
94 my ($vtf, $pl, $nh, $hl, $sa, $da, $payload) =
95 $self->SUPER::unpack('B32nCCa16a16 a*', $self->[$__raw])
96 or return undef;
97  
98 $self->[$__version] = unpackIntFromNet($vtf, 'C', 0, 4, 4);
99 $self->[$__trafficClass] = unpackIntFromNet($vtf, 'C', 4, 0, 8);
100 $self->[$__flowLabel] = unpackIntFromNet($vtf, 'N', 12, 12, 20);
101 $self->[$__payloadLength] = $pl;
102 $self->[$__nextHeader] = $nh;
103 $self->[$__hopLimit] = $hl;
104 $self->[$__src] = inet6Ntoa($sa);
105 $self->[$__dst] = inet6Ntoa($da);
106  
107 $self->[$__payload] = $payload;
108  
109 1;
110 }
111  
112 sub encapsulate {
113 my $types = {
114 NP_IPv6_PROTOCOL_TCP() => NP_LAYER_TCP(),
115 NP_IPv6_PROTOCOL_UDP() => NP_LAYER_UDP(),
116 #NP_IPv4_PROTOCOL_ICMPv6() => NP_LAYER_ICMPv6(),
117 };
118  
119 $types->{shift->[$__nextHeader]} || NP_LAYER_UNKNOWN();
120 }
121  
122 sub print {
123 my $self = shift;
124
125 my $i = $self->is;
126 my $l = $self->layer;
127 sprintf
128 "$l:+$i: version:%d trafficClass:%.2d flowLabel:%.5d nextHeader:%.2d\n".
129 "$l: $i: [%s => %s]\n".
130 "$l: $i: length:%d",
131 $self->version,
132 $self->trafficClass,
133 $self->flowLabel,
134 $self->nextHeader,
135 $self->src,
136 $self->dst,
137 $self->getLength,
138 ;
139 }
140  
141 1;
142  
143 =head1 NAME
144  
145 Net::Packet::IPv6 - Internet Protocol v6 layer 3 object
146  
147 =head1 SYNOPSIS
148  
149 use Net::Packet::IPv6;
150  
151 # Build layer to inject to network
152 my $ip6 = Net::Packet::IPv6->new(
153 dst => $hostname6,
154 );
155  
156 # Decode from network to create the object
157 # Usually, you do not use this, it is used by Net::Packet::Frame
158 my $ip6Decoded = Net::Packet::IPv6->new(raw = $rawFromNetwork);
159  
160 print $ip6->print, "\n";
161  
162 =head1 DESCRIPTION
163  
164 This modules implements the encoding and decoding of the IPv6 layer.
165  
166 RFC: ftp://ftp.rfc-editor.org/in-notes/rfc2460.txt
167  
168 See also B<Net::Packet::Layer> and B<Net::Packet::Layer3> for other attributes and methods.
169  
170 =head1 ATTRIBUTES
171  
172 =over 4
173  
174 =item B<version>
175  
176 Version of Internet Protocol header.
177  
178 =item B<trafficClass>
179  
180 Traffic class field. Was Type of Service in IPv4.
181  
182 =item B<flowLabel>
183  
184 Flow label class field. Was IP ID in IPv4.
185  
186 =item B<nextHeader>
187  
188 The type of next header. Was protocol in IPv4.
189  
190 =item B<payloadLength>
191  
192 Length in bytes of encapsulated layers (that is, layer 4 + layer 7).
193  
194 =item B<hopLimit>
195  
196 Was TTL field in IPv4.
197  
198 =item B<src>
199  
200 =item B<dst>
201  
202 Source and destination addresses.
203  
204 =back
205  
206 =head1 METHODS
207  
208 =over 4
209  
210 =item B<new>
211  
212 Object constructor. You can pass attributes that will overwrite default ones. Default values:
213  
214 version: 6
215  
216 trafficClass: 0
217  
218 flowLabel: 0
219  
220 nextHeader: NP_IPv6_PROTOCOL_TCP
221  
222 hopLimit: 0xff
223  
224 src: $Env->ip6
225  
226 dst: '::1'
227  
228 =item B<getPayloadLength>
229  
230 Returns the length in bytes of encapsulated layers (that is layer 4 + layer 7).
231  
232 =item B<pack>
233  
234 Packs all attributes into a raw format, in order to inject to network. Returns 1 on success, undef otherwise.
235  
236 =item B<unpack>
237  
238 Unpacks raw data from network and stores attributes into the object. Returns 1 on success, undef otherwise.
239  
240 =back
241  
242 =head1 CONSTANTS
243  
244 Load them: use Net::Packet::Consts qw(:ipv6);
245  
246 =over 4
247  
248 =item B<NP_IPv6_PROTOCOL_TCP>
249  
250 =item B<NP_IPv6_PROTOCOL_UDP>
251  
252 Constants for B<nextHeader> attribute.
253  
254 =back
255  
256 =head1 AUTHOR
257
258 Patrice E<lt>GomoRE<gt> Auffret
259
260 =head1 COPYRIGHT AND LICENSE
261
262 Copyright (c) 2004-2006, Patrice E<lt>GomoRE<gt> Auffret
263
264 You may distribute this module under the terms of the Artistic license.
265 See LICENSE.Artistic file in the source distribution archive.
266
267 =head1 RELATED MODULES
268
269 L<NetPacket>, L<Net::RawIP>, L<Net::RawSock>
270
271 =cut