| 1 | # | ||||||||||
| 2 | # $Id: Simple.pm,v 1.8 2006/12/06 21:28:05 gomor Exp $ | ||||||||||
| 3 | # | ||||||||||
| 4 | package Net::Frame::Simple; | ||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | our $VERSION = '1.00_03'; | ||||||||||
| 9 | |||||||||||
| 10 | require Class::Gomor::Array; | ||||||||||
| 11 | our @ISA = qw(Class::Gomor::Array); | ||||||||||
| 12 | |||||||||||
| 13 | our @AS = qw( | ||||||||||
| 14 | raw | ||||||||||
| 15 | reply | ||||||||||
| 16 | timestamp | ||||||||||
| 17 | firstLayer | ||||||||||
| 18 | padding | ||||||||||
| 19 | ref | ||||||||||
| 20 | _canMatchLayer | ||||||||||
| 21 | _getKey | ||||||||||
| 22 | _getKeyReverse | ||||||||||
| 23 | ); | ||||||||||
| 24 | our @AA = qw( | ||||||||||
| 25 | layers | ||||||||||
| 26 | ); | ||||||||||
| 27 | __PACKAGE__->cgBuildIndices; | ||||||||||
| 28 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||||||||||
| 29 | __PACKAGE__->cgBuildAccessorsArray (\@AA); | ||||||||||
| 30 | |||||||||||
| 31 | 2 | ▶ | no strict 'vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use Time::HiRes qw(gettimeofday); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | ▶ | use Net::Frame::Layer qw(:consts); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | sub _gettimeofday { | ||||||||||
| 38 | 0 | ▶ | my ($sec, $usec) = gettimeofday(); | ||||||||
|
Subroutine
_gettimeofday: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | sprintf("%d.%06d", $sec, $usec); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | sub new { | ||||||||||
| 43 | 0 | ▶ | my $self = shift->SUPER::new( | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||
| 44 | timestamp => _gettimeofday(), | ||||||||||
| 45 | firstLayer => NP_LAYER_UNKNOWN, | ||||||||||
| 46 | layers => [], | ||||||||||
| 47 | @_, | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | $self->[$__raw] ? $self->unpack : $self->pack; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $self; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub newFromDump { | ||||||||||
| 55 | my $self = shift; | ||||||||||
| 56 | my ($h) = @_; | ||||||||||
| 57 | $self->new( | ||||||||||
| 58 | timestamp => $h->{timestamp}, | ||||||||||
| 59 | firstLayer => $h->{firstLayer}, | ||||||||||
| 60 | raw => $h->{raw}, | ||||||||||
| 61 | ); | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # If there are multiple layers of the same type, the upper will be kept | ||||||||||
| 65 | sub _setRef { | ||||||||||
| 66 | my $self = shift; | ||||||||||
| 67 | my ($l) = @_; | ||||||||||
| 68 | $self->[$__ref]->{$l->layer} = $l; | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub unpack { | ||||||||||
| 72 | my $self = shift; | ||||||||||
| 73 | |||||||||||
| 74 | my $encapsulate = $self->[$__firstLayer]; | ||||||||||
| 75 | |||||||||||
| 76 | if ($encapsulate eq NP_LAYER_UNKNOWN) { | ||||||||||
| 77 | print("Unable to unpack frame from this layer type.\n"); | ||||||||||
| 78 | return undef; | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | my @layers; | ||||||||||
| 82 | my $n = 0; | ||||||||||
| 83 | my $raw = $self->[$__raw]; | ||||||||||
| 84 | my $rawLength = length($raw); | ||||||||||
| 85 | my $oRaw = $raw; | ||||||||||
| 86 | # No more than a thousand nested layers, maybe should be a parameter | ||||||||||
| 87 | for (1..1000) { | ||||||||||
| 88 | last unless $raw; | ||||||||||
| 89 | |||||||||||
| 90 | my $layer = 'Net::Frame::'.$encapsulate; | ||||||||||
| 91 | (my $module = $layer) =~ s/::/\//g; | ||||||||||
| 92 | eval { require "$module.pm" }; | ||||||||||
| 93 | if ($@) { | ||||||||||
| 94 | print("*** Net::Frame::$encapsulate module not found.\n". | ||||||||||
| 95 | "*** Either install it (if avail), or implement it.\n". | ||||||||||
| 96 | "*** You can also send the pcap file to perl\@gomor.org.\n"); | ||||||||||
| 97 | last; | ||||||||||
| 98 | } | ||||||||||
| 99 | my $l = $layer->new(raw => $raw)->unpack; | ||||||||||
| 100 | |||||||||||
| 101 | $encapsulate = $l->encapsulate; | ||||||||||
| 102 | $raw = $l->payload; | ||||||||||
| 103 | |||||||||||
| 104 | push @layers, $l; | ||||||||||
| 105 | # If there are multiple layers of the same type, the upper will be kept | ||||||||||
| 106 | $self->_setRef($l); | ||||||||||
| 107 | |||||||||||
| 108 | last unless $encapsulate; | ||||||||||
| 109 | |||||||||||
| 110 | if ($encapsulate eq NP_LAYER_UNKNOWN) { | ||||||||||
| 111 | print("Unable to unpack next layer, not yet implemented in layer: ". | ||||||||||
| 112 | "$n:@{[$l->layer]}\n"); | ||||||||||
| 113 | last; | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | $oRaw = $raw; | ||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | if (@layers > 0) { | ||||||||||
| 120 | $self->[$__layers] = \@layers; | ||||||||||
| 121 | $self->_getPadding($rawLength); | ||||||||||
| 122 | $self->_searchCanGetKeyLayer; | ||||||||||
| 123 | $self->_searchCanGetKeyReverseLayer; | ||||||||||
| 124 | $self->_searchCanMatchLayer; | ||||||||||
| 125 | return $self; | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | undef; | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | sub computeLengths { | ||||||||||
| 132 | my $self = shift; | ||||||||||
| 133 | |||||||||||
| 134 | if (exists $self->[$__ref]->{IPv4}) { | ||||||||||
| 135 | my $ip4 = $self->[$__ref]->{IPv4}; | ||||||||||
| 136 | if (exists $self->[$__ref]->{TCP}) { | ||||||||||
| 137 | my $tcp = $self->[$__ref]->{TCP}; | ||||||||||
| 138 | $tcp->computeLengths; | ||||||||||
| 139 | $ip4->computeLengths({ | ||||||||||
| 140 | payloadLength => $tcp->getLength + $tcp->getPayloadLength, | ||||||||||
| 141 | }); | ||||||||||
| 142 | } | ||||||||||
| 143 | elsif (exists $self->[$__ref]->{UDP}) { | ||||||||||
| 144 | my $udp = $self->[$__ref]->{UDP}; | ||||||||||
| 145 | $udp->computeLengths; | ||||||||||
| 146 | $ip4->computeLengths({ | ||||||||||
| 147 | payloadLength => $udp->getLength + $udp->getPayloadLength, | ||||||||||
| 148 | }); | ||||||||||
| 149 | } | ||||||||||
| 150 | elsif (exists $self->[$__ref]->{ICMPv4}) { | ||||||||||
| 151 | my $icmp4 = $self->[$__ref]->{ICMPv4}; | ||||||||||
| 152 | $ip4->computeLengths({ | ||||||||||
| 153 | payloadLength => $icmp4->getLength, | ||||||||||
| 154 | }); | ||||||||||
| 155 | } | ||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 1; | ||||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | sub computeChecksums { | ||||||||||
| 162 | my $self = shift; | ||||||||||
| 163 | |||||||||||
| 164 | if (exists $self->[$__ref]->{IPv4}) { | ||||||||||
| 165 | my $ip4 = $self->[$__ref]->{IPv4}; | ||||||||||
| 166 | if (exists $self->[$__ref]->{ETH}) { | ||||||||||
| 167 | $ip4->computeChecksums; | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | if (exists $self->[$__ref]->{TCP}) { | ||||||||||
| 171 | $self->[$__ref]->{TCP}->computeChecksums({ | ||||||||||
| 172 | type => 'IPv4', | ||||||||||
| 173 | src => $ip4->src, | ||||||||||
| 174 | dst => $ip4->dst, | ||||||||||
| 175 | }); | ||||||||||
| 176 | } | ||||||||||
| 177 | elsif (exists $self->[$__ref]->{UDP}) { | ||||||||||
| 178 | $self->[$__ref]->{UDP}->computeChecksums({ | ||||||||||
| 179 | type => 'IPv4', | ||||||||||
| 180 | src => $ip4->src, | ||||||||||
| 181 | dst => $ip4->dst, | ||||||||||
| 182 | }); | ||||||||||
| 183 | } | ||||||||||
| 184 | elsif (exists $self->[$__ref]->{ICMPv4}) { | ||||||||||
| 185 | $self->[$__ref]->{ICMPv4}->computeChecksums; | ||||||||||
| 186 | } | ||||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | 1; | ||||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | sub pack { | ||||||||||
| 193 | my $self = shift; | ||||||||||
| 194 | |||||||||||
| 195 | # If there are multiple layers of the same type, | ||||||||||
| 196 | # the upper will be kept for the reference | ||||||||||
| 197 | $self->_setRef($_) for @{$self->[$__layers]}; | ||||||||||
| 198 | |||||||||||
| 199 | $self->computeLengths; | ||||||||||
| 200 | $self->computeChecksums; | ||||||||||
| 201 | |||||||||||
| 202 | my $raw = ''; | ||||||||||
| 203 | $raw .= $_->pack for @{$self->[$__layers]}; | ||||||||||
| 204 | |||||||||||
| 205 | $raw .= $self->[$__padding] if $self->[$__padding]; | ||||||||||
| 206 | |||||||||||
| 207 | $self->_searchCanGetKeyLayer; | ||||||||||
| 208 | $self->_searchCanGetKeyReverseLayer; | ||||||||||
| 209 | $self->_searchCanMatchLayer; | ||||||||||
| 210 | |||||||||||
| 211 | $self->[$__raw] = $raw; | ||||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | sub _getPadding { | ||||||||||
| 215 | my $self = shift; | ||||||||||
| 216 | my ($rawLength) = @_; | ||||||||||
| 217 | |||||||||||
| 218 | my $last = ${$self->[$__layers]}[-1]; | ||||||||||
| 219 | |||||||||||
| 220 | # Last layer has no payload, so no padding | ||||||||||
| 221 | return unless $last->payload; | ||||||||||
| 222 | |||||||||||
| 223 | my $tLen = 0; | ||||||||||
| 224 | for my $l (@{$self->[$__layers]}) { | ||||||||||
| 225 | if ($l->layer eq 'IPv4') { | ||||||||||
| 226 | $tLen += $l->length; | ||||||||||
| 227 | last; | ||||||||||
| 228 | } | ||||||||||
| 229 | elsif ($l->layer eq 'IPv6') { | ||||||||||
| 230 | $tLen += $l->getPayloadLength; | ||||||||||
| 231 | last; | ||||||||||
| 232 | } | ||||||||||
| 233 | $tLen += $l->getLength; | ||||||||||
| 234 | } | ||||||||||
| 235 | |||||||||||
| 236 | # No padding | ||||||||||
| 237 | return if $rawLength == $tLen; | ||||||||||
| 238 | |||||||||||
| 239 | my $pLen = ($rawLength > $tLen) ? ($rawLength - $tLen) : 0; | ||||||||||
| 240 | my $padding = substr($self->[$__raw], $tLen, $pLen); | ||||||||||
| 241 | |||||||||||
| 242 | $self->[$__padding] = $padding; | ||||||||||
| 243 | |||||||||||
| 244 | # Now, split padding between true padding and true payload | ||||||||||
| 245 | my $payloadLength = length($last->payload); | ||||||||||
| 246 | if ($payloadLength > $pLen) { | ||||||||||
| 247 | my $payload = substr($last->payload, 0, ($payloadLength - $pLen)); | ||||||||||
| 248 | $last->payload($payload); | ||||||||||
| 249 | } | ||||||||||
| 250 | else { | ||||||||||
| 251 | $last->payload(undef); | ||||||||||
| 252 | } | ||||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | sub send { | ||||||||||
| 256 | my $self = shift; | ||||||||||
| 257 | my ($oWrite) = @_; | ||||||||||
| 258 | $oWrite->send($self->[$__raw]); | ||||||||||
| 259 | } | ||||||||||
| 260 | |||||||||||
| 261 | sub reSend { my $self = shift; $self->send unless $self->[$__reply] } | ||||||||||
| 262 | |||||||||||
| 263 | sub _searchCanMatchLayer { | ||||||||||
| 264 | my $self = shift; | ||||||||||
| 265 | for my $l (reverse @{$self->[$__layers]}) { | ||||||||||
| 266 | if ($l->can('match')) { | ||||||||||
| 267 | $self->[$___canMatchLayer] = $l; | ||||||||||
| 268 | last; | ||||||||||
| 269 | } | ||||||||||
| 270 | } | ||||||||||
| 271 | undef; | ||||||||||
| 272 | } | ||||||||||
| 273 | |||||||||||
| 274 | sub _searchCanGetKeyLayer { | ||||||||||
| 275 | my $self = shift; | ||||||||||
| 276 | for my $l (reverse @{$self->[$__layers]}) { | ||||||||||
| 277 | if ($l->can('getKey')) { | ||||||||||
| 278 | $self->[$___getKey] = $l->getKey; | ||||||||||
| 279 | last; | ||||||||||
| 280 | } | ||||||||||
| 281 | } | ||||||||||
| 282 | } | ||||||||||
| 283 | |||||||||||
| 284 | sub _searchCanGetKeyReverseLayer { | ||||||||||
| 285 | my $self = shift; | ||||||||||
| 286 | for my $l (reverse @{$self->[$__layers]}) { | ||||||||||
| 287 | if ($l->can('getKeyReverse')) { | ||||||||||
| 288 | $self->[$___getKeyReverse] = $l->getKeyReverse; | ||||||||||
| 289 | last; | ||||||||||
| 290 | } | ||||||||||
| 291 | } | ||||||||||
| 292 | } | ||||||||||
| 293 | |||||||||||
| 294 | sub _recv { | ||||||||||
| 295 | my $self = shift; | ||||||||||
| 296 | my ($oDump) = @_; | ||||||||||
| 297 | |||||||||||
| 298 | my $layer = $self->[$___canMatchLayer]; | ||||||||||
| 299 | |||||||||||
| 300 | for my $this ($oDump->getFramesFor($self)) { | ||||||||||
| 301 | next unless $this->[$__timestamp] ge $self->[$__timestamp]; | ||||||||||
| 302 | |||||||||||
| 303 | # We must put ICMPv4 before, because the other will | ||||||||||
| 304 | # always match for UDP. | ||||||||||
| 305 | if (exists $this->[$__ref]->{ICMPv4} | ||||||||||
| 306 | && (exists $this->[$__ref]->{UDP} || exists $this->[$__ref]->{TCP})) { | ||||||||||
| 307 | if (exists $this->[$__ref]->{$layer->layer}) { | ||||||||||
| 308 | return $this | ||||||||||
| 309 | if $this->[$__ref]->{$layer->layer}->getKey eq $layer->getKey; | ||||||||||
| 310 | } | ||||||||||
| 311 | } | ||||||||||
| 312 | elsif (exists $this->[$__ref]->{$layer->layer}) { | ||||||||||
| 313 | return $this if $layer->match($this->[$__ref]->{$layer->layer}); | ||||||||||
| 314 | } | ||||||||||
| 315 | } | ||||||||||
| 316 | |||||||||||
| 317 | undef; | ||||||||||
| 318 | } | ||||||||||
| 319 | |||||||||||
| 320 | sub recv { | ||||||||||
| 321 | my $self = shift; | ||||||||||
| 322 | my ($oDump) = @_; | ||||||||||
| 323 | |||||||||||
| 324 | # We already have the reply | ||||||||||
| 325 | $self->[$__reply] and return $self->[$__reply]; | ||||||||||
| 326 | |||||||||||
| 327 | # Is there anything waiting ? | ||||||||||
| 328 | my $h = $oDump->next or return undef; | ||||||||||
| 329 | |||||||||||
| 330 | my $oSimple = Net::Frame::Simple->newFromDump($h); | ||||||||||
| 331 | $oDump->store($oSimple); | ||||||||||
| 332 | |||||||||||
| 333 | if (my $reply = $self->_recv($oDump)) { | ||||||||||
| 334 | $self->cgDebugPrint(1, "Reply received"); | ||||||||||
| 335 | return $self->[$__reply] = $reply; | ||||||||||
| 336 | } | ||||||||||
| 337 | |||||||||||
| 338 | undef; | ||||||||||
| 339 | } | ||||||||||
| 340 | |||||||||||
| 341 | # Needed by Net::Frame::Dump | ||||||||||
| 342 | sub getKey { shift->[$___getKey] || 'all' } | ||||||||||
| 343 | sub getKeyReverse { shift->[$___getKeyReverse] || 'all' } | ||||||||||
| 344 | |||||||||||
| 345 | sub print { | ||||||||||
| 346 | my $self = shift; | ||||||||||
| 347 | |||||||||||
| 348 | my $str = ''; | ||||||||||
| 349 | my $last; | ||||||||||
| 350 | for my $l (@{$self->[$__layers]}) { | ||||||||||
| 351 | $str .= $l->print."\n"; | ||||||||||
| 352 | $last = $l; | ||||||||||
| 353 | } | ||||||||||
| 354 | $str =~ s/\n$//s; | ||||||||||
| 355 | |||||||||||
| 356 | # Print remaining to be decoded, if any | ||||||||||
| 357 | if ($last && $last->payload) { | ||||||||||
| 358 | $str .= "\n".$last->layer.': payload:'.CORE::unpack('H*', $last->payload); | ||||||||||
| 359 | } | ||||||||||
| 360 | |||||||||||
| 361 | # Print the padding, if any | ||||||||||
| 362 | if ($self->[$__padding]) { | ||||||||||
| 363 | $str .= "\n".'Padding: '.CORE::unpack('H*', $self->[$__padding]); | ||||||||||
| 364 | } | ||||||||||
| 365 | |||||||||||
| 366 | $str; | ||||||||||
| 367 | } | ||||||||||
| 368 | |||||||||||
| 369 | sub dump { | ||||||||||
| 370 | my $self = shift; | ||||||||||
| 371 | |||||||||||
| 372 | my $str = ''; | ||||||||||
| 373 | for (@{$self->[$__layers]}) { | ||||||||||
| 374 | $str .= $_->dump."\n"; | ||||||||||
| 375 | } | ||||||||||
| 376 | |||||||||||
| 377 | if ($self->[$__padding]) { | ||||||||||
| 378 | $str .= 'Padding: '.CORE::unpack('H*', $self->[$__padding])."\n"; | ||||||||||
| 379 | } | ||||||||||
| 380 | |||||||||||
| 381 | $str; | ||||||||||
| 382 | } | ||||||||||
| 383 | |||||||||||
| 384 | 1; | ||||||||||
| 385 | |||||||||||
| 386 | __END__ | ||||||||||