| 1 | # | ||
| 2 | # $Id: IPv6.pm 2002 2015-02-15 16:50:35Z gomor $ | ||
| 3 | # | ||
| 4 | package Net::Packet::IPv6; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | require Net::Packet::Layer3; | ||
| 9 | our @ISA = qw(Net::Packet::Layer3); | ||
| 10 | |||
| 11 | 0 | ▶ | use Net::Packet::Env qw($Env); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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 | require Bit::Vector; | ||
| 36 | |||
| 37 | sub new { | ||
| 38 | shift->SUPER::new( | ||
| 39 | version => 6, | ||
| 40 | trafficClass => 0, | ||
| 41 | flowLabel => 0, | ||
| 42 | nextHeader => NP_IPv6_PROTOCOL_TCP, | ||
| 43 | hopLimit => 0xff, | ||
| 44 | src => $Env->ip6, | ||
| 45 | dst => '::1', | ||
| 46 | payloadLength => 0, | ||
| 47 | @_, | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub getLength { NP_IPv6_HDR_LEN } | ||
| 52 | sub getPayloadLength { shift->[$__payloadLength] } | ||
| 53 | |||
| 54 | sub _computePayloadLength { | ||
| 55 | my $self = shift; | ||
| 56 | my ($l4, $l7) = @_; | ||
| 57 | |||
| 58 | my $len = 0; | ||
| 59 | $len += $l4->getLength if $l4; | ||
| 60 | $len += $l7->getLength if $l7; | ||
| 61 | $self->[$__payloadLength] = $len; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub computeLengths { | ||
| 65 | my $self = shift; | ||
| 66 | my ($env, $l2, $l3, $l4, $l7) = @_; | ||
| 67 | |||
| 68 | $l4 && ($l4->computeLengths($env, $l2, $l3, $l4, $l7) or return undef); | ||
| 69 | $self->_computePayloadLength($l4, $l7); | ||
| 70 | 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub pack { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | my $version = Bit::Vector->new_Dec(4, $self->[$__version]); | ||
| 77 | my $trafficClass = Bit::Vector->new_Dec(8, $self->[$__trafficClass]); | ||
| 78 | my $flowLabel = Bit::Vector->new_Dec(20, $self->[$__flowLabel]); | ||
| 79 | my $v32 = $version->Concat_List($trafficClass, $flowLabel); | ||
| 80 | |||
| 81 | $self->[$__raw] = $self->SUPER::pack('NnCCa*a*', | ||
| 82 | $v32->to_Dec, | ||
| 83 | $self->[$__payloadLength], | ||
| 84 | $self->[$__nextHeader], | ||
| 85 | $self->[$__hopLimit], | ||
| 86 | inet6Aton($self->[$__src]), | ||
| 87 | inet6Aton($self->[$__dst]), | ||
| 88 | ) or return undef; | ||
| 89 | |||
| 90 | 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub unpack { | ||
| 94 | my $self = shift; | ||
| 95 | |||
| 96 | my ($vTcFl, $pl, $nh, $hl, $sa, $da, $payload) = | ||
| 97 | $self->SUPER::unpack('NnCCa16a16 a*', $self->[$__raw]) | ||
| 98 | or return undef; | ||
| 99 | |||
| 100 | my $v32 = Bit::Vector->new_Dec(32, $vTcFl); | ||
| 101 | |||
| 102 | $self->[$__flowLabel] = $v32->Chunk_Read(20, 0); | ||
| 103 | $self->[$__trafficClass] = $v32->Chunk_Read( 8, 20); | ||
| 104 | $self->[$__version] = $v32->Chunk_Read( 4, 28); | ||
| 105 | $self->[$__payloadLength] = $pl; | ||
| 106 | $self->[$__nextHeader] = $nh; | ||
| 107 | $self->[$__hopLimit] = $hl; | ||
| 108 | $self->[$__src] = inet6Ntoa($sa); | ||
| 109 | $self->[$__dst] = inet6Ntoa($da); | ||
| 110 | |||
| 111 | $self->[$__payload] = $payload; | ||
| 112 | |||
| 113 | 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub encapsulate { | ||
| 117 | my $types = { | ||
| 118 | NP_IPv6_PROTOCOL_TCP() => NP_LAYER_TCP(), | ||
| 119 | NP_IPv6_PROTOCOL_UDP() => NP_LAYER_UDP(), | ||
| 120 | #NP_IPv4_PROTOCOL_ICMPv6() => NP_LAYER_ICMPv6(), | ||
| 121 | }; | ||
| 122 | |||
| 123 | $types->{shift->[$__nextHeader]} || NP_LAYER_UNKNOWN(); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub print { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | my $i = $self->is; | ||
| 130 | my $l = $self->layer; | ||
| 131 | sprintf | ||
| 132 | "$l:+$i: version:%d trafficClass:0x%02x flowLabel:0x%05x ". | ||
| 133 | "nextHeader:0x%02x\n". | ||
| 134 | "$l: $i: payloadLength:%d hopLimit:%d\n". | ||
| 135 | "$l: $i: src:%s dst:%s", | ||
| 136 | $self->[$__version], $self->[$__trafficClass], $self->[$__flowLabel], | ||
| 137 | $self->[$__nextHeader], $self->[$__payloadLength], | ||
| 138 | $self->[$__hopLimit], $self->[$__src], $self->[$__dst]; | ||
| 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::Consts qw(:ipv6); | ||
| 150 | require Net::Packet::IPv6; | ||
| 151 | |||
| 152 | # Build a layer | ||
| 153 | my $layer = Net::Packet::IPv6->new( | ||
| 154 | dst => $hostname6, | ||
| 155 | ); | ||
| 156 | $layer->pack; | ||
| 157 | |||
| 158 | print 'RAW: '.unpack('H*', $layer->raw)."\n"; | ||
| 159 | |||
| 160 | # Read a raw layer | ||
| 161 | my $layer = Net::Packet::IPv6->new(raw = $raw); | ||
| 162 | |||
| 163 | print $layer->print."\n"; | ||
| 164 | print 'PAYLOAD: '.unpack('H*', $layer->payload)."\n" | ||
| 165 | if $layer->payload; | ||
| 166 | |||
| 167 | =head1 DESCRIPTION | ||
| 168 | |||
| 169 | This modules implements the encoding and decoding of the IPv6 layer. | ||
| 170 | |||
| 171 | RFC: ftp://ftp.rfc-editor.org/in-notes/rfc2460.txt | ||
| 172 | |||
| 173 | See also B<Net::Packet::Layer> and B<Net::Packet::Layer3> for other attributes and methods. | ||
| 174 | |||
| 175 | =head1 ATTRIBUTES | ||
| 176 | |||
| 177 | =over 4 | ||
| 178 | |||
| 179 | =item B<version> | ||
| 180 | |||
| 181 | Version of Internet Protocol header. | ||
| 182 | |||
| 183 | =item B<trafficClass> | ||
| 184 | |||
| 185 | Traffic class field. Was Type of Service in IPv4. | ||
| 186 | |||
| 187 | =item B<flowLabel> | ||
| 188 | |||
| 189 | Flow label class field. Was IP ID in IPv4. | ||
| 190 | |||
| 191 | =item B<nextHeader> | ||
| 192 | |||
| 193 | The type of next header. Was protocol in IPv4. | ||
| 194 | |||
| 195 | =item B<protocol> | ||
| 196 | |||
| 197 | Is an alias for B<nextHeader> | ||
| 198 | |||
| 199 | =item B<payloadLength> | ||
| 200 | |||
| 201 | Length in bytes of encapsulated layers (that is, layer 4 + layer 7). | ||
| 202 | |||
| 203 | =item B<hopLimit> | ||
| 204 | |||
| 205 | Was TTL field in IPv4. | ||
| 206 | |||
| 207 | =item B<src> | ||
| 208 | |||
| 209 | =item B<dst> | ||
| 210 | |||
| 211 | Source and destination addresses. | ||
| 212 | |||
| 213 | =back | ||
| 214 | |||
| 215 | =head1 METHODS | ||
| 216 | |||
| 217 | =over 4 | ||
| 218 | |||
| 219 | =item B<new> | ||
| 220 | |||
| 221 | Object constructor. You can pass attributes that will overwrite default ones. Default values: | ||
| 222 | |||
| 223 | version: 6 | ||
| 224 | |||
| 225 | trafficClass: 0 | ||
| 226 | |||
| 227 | flowLabel: 0 | ||
| 228 | |||
| 229 | nextHeader: NP_IPv6_PROTOCOL_TCP | ||
| 230 | |||
| 231 | hopLimit: 0xff | ||
| 232 | |||
| 233 | src: $Env->ip6 | ||
| 234 | |||
| 235 | dst: '::1' | ||
| 236 | |||
| 237 | =item B<getPayloadLength> | ||
| 238 | |||
| 239 | Returns the length in bytes of encapsulated layers (that is layer 4 + layer 7). | ||
| 240 | |||
| 241 | =item B<pack> | ||
| 242 | |||
| 243 | Packs all attributes into a raw format, in order to inject to network. Returns 1 on success, undef otherwise. | ||
| 244 | |||
| 245 | =item B<unpack> | ||
| 246 | |||
| 247 | Unpacks raw data from network and stores attributes into the object. Returns 1 on success, undef otherwise. | ||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | =head1 CONSTANTS | ||
| 252 | |||
| 253 | Load them: use Net::Packet::Consts qw(:ipv6); | ||
| 254 | |||
| 255 | =over 4 | ||
| 256 | |||
| 257 | =item B<NP_IPv6_PROTOCOL_TCP> | ||
| 258 | |||
| 259 | =item B<NP_IPv6_PROTOCOL_UDP> | ||
| 260 | |||
| 261 | Constants for B<nextHeader> attribute. | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | =head1 AUTHOR | ||
| 266 | |||
| 267 | Patrice E<lt>GomoRE<gt> Auffret | ||
| 268 | |||
| 269 | =head1 COPYRIGHT AND LICENSE | ||
| 270 | |||
| 271 | Copyright (c) 2004-2015, Patrice E<lt>GomoRE<gt> Auffret | ||
| 272 | |||
| 273 | You may distribute this module under the terms of the Artistic license. | ||
| 274 | See LICENSE.Artistic file in the source distribution archive. | ||
| 275 | |||
| 276 | =head1 RELATED MODULES | ||
| 277 | |||
| 278 | L<NetPacket>, L<Net::RawIP>, L<Net::RawSock> | ||
| 279 | |||
| 280 | =cut | ||