| 1 | # | ||
| 2 | # $Id: IPv4.pm,v 1.3.2.4 2006/06/04 13:36:56 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::IPv4; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | require Net::Packet::Layer3; | ||
| 9 | our @ISA = qw(Net::Packet::Layer3); | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Net::Packet::Env qw($Env); | ||
| 13 | use Net::Packet::Utils qw(getRandom16bitsInt inetAton inetNtoa inetChecksum); | ||
| 14 | use Net::Packet::Consts qw(:ipv4 :layer); | ||
| 15 | |||
| 16 | our @AS = qw( | ||
| 17 | id | ||
| 18 | ttl | ||
| 19 | src | ||
| 20 | dst | ||
| 21 | protocol | ||
| 22 | checksum | ||
| 23 | flags | ||
| 24 | offset | ||
| 25 | version | ||
| 26 | tos | ||
| 27 | length | ||
| 28 | hlen | ||
| 29 | options | ||
| 30 | noFixLen | ||
| 31 | doChecksum | ||
| 32 | ); | ||
| 33 | __PACKAGE__->cgBuildIndices; | ||
| 34 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 35 | |||
| 36 | no strict 'vars'; | ||
| 37 | |||
| 38 | BEGIN { | ||
| 39 | my $osname = { | ||
| 40 | freebsd => [ \&_fixLenBsd, undef, ], | ||
| 41 | netbsd => [ \&_fixLenBsd, undef, ], | ||
| 42 | cygwin => [ undef, \&_newWin32, ], | ||
| 43 | MSWin32 => [ undef, \&_newWin32, ], | ||
| 44 | }; | ||
| 45 | |||
| 46 | *_fixLen = $osname->{$^O}->[0] || \&_fixLenOther; | ||
| 47 | *new = $osname->{$^O}->[1] || \&_newOther; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _fixLenBsd { pack('v', shift) } | ||
| 51 | sub _fixLenOther { pack('n', shift) } | ||
| 52 | |||
| 53 | sub _newWin32 { shift->_newCommon(doChecksum => 1, @_) } | ||
| 54 | sub _newOther { shift->_newCommon(@_) } | ||
| 55 | |||
| 56 | sub _newCommon { | ||
| 57 | my $self = shift->SUPER::new( | ||
| 58 | version => 4, | ||
| 59 | tos => 0, | ||
| 60 | id => getRandom16bitsInt(), | ||
| 61 | length => 0, | ||
| 62 | hlen => 0, | ||
| 63 | flags => 0, | ||
| 64 | offset => 0, | ||
| 65 | ttl => 128, | ||
| 66 | protocol => NP_IPv4_PROTOCOL_TCP, | ||
| 67 | checksum => 0, | ||
| 68 | src => $Env->ip, | ||
| 69 | dst => '127.0.0.1', | ||
| 70 | options => '', | ||
| 71 | noFixLen => 0, | ||
| 72 | doChecksum => 0, | ||
| 73 | @_, | ||
| 74 | ); | ||
| 75 | |||
| 76 | unless ($self->[$__raw]) { | ||
| 77 | # Autocompute header length if not user specified | ||
| 78 | unless ($self->[$__hlen]) { | ||
| 79 | my $hLen = NP_IPv4_HDR_LEN; | ||
| 80 | $hLen += length($self->[$__options]) if $self->[$__options]; | ||
| 81 | $self->[$__hlen] = $hLen / 4; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub pack { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | # Thank you Stephanie Wehner | ||
| 92 | my $hlenVer = ($self->[$__hlen] & 0x0f)|(($self->[$__version] << 4) & 0xf0); | ||
| 93 | my $flags = $self->[$__flags]; | ||
| 94 | my $offset = $self->[$__offset]; | ||
| 95 | |||
| 96 | my $len = ($self->[$__noFixLen] ? _fixLenOther($self->[$__length]) | ||
| 97 | : _fixLen($self->[$__length])); | ||
| 98 | |||
| 99 | $self->[$__raw] = $self->SUPER::pack('CCa*nnCCna4a4', | ||
| 100 | $hlenVer, | ||
| 101 | $self->[$__tos], | ||
| 102 | $len, | ||
| 103 | $self->[$__id], | ||
| 104 | $flags << 13 | $offset, | ||
| 105 | $self->[$__ttl], | ||
| 106 | $self->[$__protocol], | ||
| 107 | $self->[$__checksum], | ||
| 108 | inetAton($self->[$__src]), | ||
| 109 | inetAton($self->[$__dst]), | ||
| 110 | ) or return undef; | ||
| 111 | |||
| 112 | my $opt; | ||
| 113 | if ($self->[$__options]) { | ||
| 114 | $opt = $self->SUPER::pack('a*', $self->[$__options]) | ||
| 115 | or return undef; | ||
| 116 | $self->[$__raw] = $self->[$__raw].$opt; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub unpack { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | my ($verHlen, $tos, $len, $id, $flags, $ttl, $proto, $cksum, $src, $dst, | ||
| 126 | $payload) = $self->SUPER::unpack('CCnnnCCna4a4 a*', $self->[$__raw]) | ||
| 127 | or return undef; | ||
| 128 | |||
| 129 | $self->[$__version] = ($verHlen & 0xf0) >> 4; | ||
| 130 | $self->[$__hlen] = $verHlen & 0x0f; | ||
| 131 | $self->[$__tos] = $tos; | ||
| 132 | $self->[$__length] = $len; | ||
| 133 | $self->[$__id] = $id; | ||
| 134 | $self->[$__flags] = $flags >> 13; | ||
| 135 | $self->[$__offset] = $flags & 0x1FFF; | ||
| 136 | $self->[$__ttl] = $ttl; | ||
| 137 | $self->[$__protocol] = $proto; | ||
| 138 | $self->[$__checksum] = $cksum; | ||
| 139 | $self->[$__src] = inetNtoa($src); | ||
| 140 | $self->[$__dst] = inetNtoa($dst); | ||
| 141 | $self->[$__payload] = $payload; | ||
| 142 | |||
| 143 | my ($options, $payload2) = $self->SUPER::unpack( | ||
| 144 | 'a'. $self->getOptionsLength. 'a*', $self->[$__payload] | ||
| 145 | ) or return undef; | ||
| 146 | |||
| 147 | $self->[$__options] = $options; | ||
| 148 | $self->[$__payload] = $payload2; | ||
| 149 | |||
| 150 | 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub getLength { | ||
| 154 | my $self = shift; | ||
| 155 | $self->[$__hlen] > 0 ? $self->[$__hlen] * 4 : 0; | ||
| 156 | } | ||
| 157 | sub getHeaderLength { NP_IPv4_HDR_LEN } | ||
| 158 | sub getPayloadLength { | ||
| 159 | my $self = shift; | ||
| 160 | my $gLen = $self->getLength; | ||
| 161 | $self->[$__length] > $gLen ? $self->[$__length] - $gLen : 0; | ||
| 162 | } | ||
| 163 | sub getOptionsLength { | ||
| 164 | my $self = shift; | ||
| 165 | my $gLen = $self->getLength; | ||
| 166 | my $hLen = $self->getHeaderLength; | ||
| 167 | $gLen > $hLen ? $gLen - $hLen : 0; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub _computeTotalLength { | ||
| 171 | my $self = shift; | ||
| 172 | my $frame = shift; | ||
| 173 | |||
| 174 | # Do not compute if user specified | ||
| 175 | return if $self->[$__length]; | ||
| 176 | |||
| 177 | my $total = $self->getLength; | ||
| 178 | $total += $frame->l4->getLength; | ||
| 179 | $total += $frame->l7->getLength if $frame->l7; | ||
| 180 | $self->[$__length] = $total; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub computeLengths { | ||
| 184 | my $self = shift; | ||
| 185 | my $frame = shift; | ||
| 186 | |||
| 187 | $frame->l4->computeLengths($frame) or return undef; | ||
| 188 | $self->_computeTotalLength($frame); | ||
| 189 | 1; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub computeChecksums { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | return 1 unless $self->[$__doChecksum]; | ||
| 196 | |||
| 197 | $self->pack; | ||
| 198 | $self->[$__checksum] = inetChecksum($self->[$__raw]); | ||
| 199 | |||
| 200 | 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub encapsulate { | ||
| 204 | my $types = { | ||
| 205 | NP_IPv4_PROTOCOL_TCP() => NP_LAYER_TCP(), | ||
| 206 | NP_IPv4_PROTOCOL_UDP() => NP_LAYER_UDP(), | ||
| 207 | NP_IPv4_PROTOCOL_ICMPv4() => NP_LAYER_ICMPv4(), | ||
| 208 | }; | ||
| 209 | |||
| 210 | $types->{shift->protocol} || NP_LAYER_UNKNOWN(); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub getKey { | ||
| 214 | my $self = shift; | ||
| 215 | $self->is.':'.$self->[$__src].'-'.$self->[$__dst]; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub getKeyReverse { | ||
| 219 | my $self = shift; | ||
| 220 | $self->is.':'.$self->[$__dst].'-'.$self->[$__src]; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub print { | ||
| 224 | my $self = shift; | ||
| 225 | |||
| 226 | my $i = $self->is; | ||
| 227 | my $l = $self->layer; | ||
| 228 | sprintf | ||
| 229 | "$l:+$i: version:%d id:%.4d ttl:%d [%s => %s]\n". | ||
| 230 | "$l: $i: tos:0x%.2x flags:0x%.2x offset:%d\n". | ||
| 231 | "$l: $i: checksum:0x%.4x protocol:0x%.2x\n". | ||
| 232 | "$l: $i: size:%d length:%d optionsLength:%d payload:%d", | ||
| 233 | $self->[$__version], | ||
| 234 | $self->[$__id], | ||
| 235 | $self->[$__ttl], | ||
| 236 | $self->[$__src], | ||
| 237 | $self->[$__dst], | ||
| 238 | $self->[$__tos], | ||
| 239 | $self->[$__flags], | ||
| 240 | $self->[$__offset], | ||
| 241 | $self->[$__checksum], | ||
| 242 | $self->[$__protocol], | ||
| 243 | $self->[$__length], | ||
| 244 | $self->getLength, | ||
| 245 | $self->getOptionsLength, | ||
| 246 | $self->getPayloadLength, | ||
| 247 | ; | ||
| 248 | } | ||
| 249 | |||
| 250 | # | ||
| 251 | # Helpers | ||
| 252 | # | ||
| 253 | |||
| 254 | sub _haveFlag { (shift->flags & shift()) ? 1 : 0 } | ||
| 255 | sub haveFlagDf { shift->_haveFlag(NP_IPv4_DONT_FRAGMENT) } | ||
| 256 | sub haveFlagMf { shift->_haveFlag(NP_IPv4_MORE_FRAGMENT) } | ||
| 257 | sub haveFlagRf { shift->_haveFlag(NP_IPv4_RESERVED_FRAGMENT) } | ||
| 258 | |||
| 259 | sub _isProtocol { shift->protocol == shift() } | ||
| 260 | sub isProtocolTcp { shift->_isProtocol(NP_IPv4_PROTOCOL_TCP) } | ||
| 261 | sub isProtocolUdp { shift->_isProtocol(NP_IPv4_PROTOCOL_UDP) } | ||
| 262 | sub isProtocolIcmpv4 { shift->_isProtocol(NP_IPv4_PROTOCOL_ICMPv4) } | ||
| 263 | |||
| 264 | 1; | ||
| 265 | |||
| 266 | __END__ |