| 1 | # | ||
| 2 | # $Id: UDP.pm 2002 2015-02-15 16:50:35Z gomor $ | ||
| 3 | # | ||
| 4 | package Net::Packet::UDP; | ||
| 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::Layer4; | ||
| 9 | our @ISA = qw(Net::Packet::Layer4); | ||
| 10 | |||
| 11 | 0 | ▶ | use Net::Packet::Utils qw(inetChecksum getRandomHighPort inetAton inet6Aton); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Net::Packet::Consts qw(:udp :layer); | ||
| 13 | |||
| 14 | our @AS = qw( | ||
| 15 | src | ||
| 16 | dst | ||
| 17 | length | ||
| 18 | checksum | ||
| 19 | ); | ||
| 20 | __PACKAGE__->cgBuildIndices; | ||
| 21 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 22 | |||
| 23 | no strict 'vars'; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | shift->SUPER::new( | ||
| 27 | src => getRandomHighPort(), | ||
| 28 | dst => 0, | ||
| 29 | length => 0, | ||
| 30 | checksum => 0, | ||
| 31 | @_, | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub recv { | ||
| 36 | my $self = shift; | ||
| 37 | my ($frame) = @_; | ||
| 38 | |||
| 39 | my $env = $frame->env; | ||
| 40 | |||
| 41 | for ($env->dump->framesFor($frame)) { | ||
| 42 | return $_ if $_->timestamp ge $frame->timestamp; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $l2Key = ($frame->l2 && $frame->l2->getKeyReverse($frame)) || 'all'; | ||
| 46 | my $l3Key = ($frame->l3 && $frame->l3->is.':'.$frame->l3->src) || 'all'; | ||
| 47 | my $l4Key = ($frame->l4 && 'ICMP') || 'all'; | ||
| 48 | |||
| 49 | my $href = $env->dump->framesSorted; | ||
| 50 | for (@{$href->{$l2Key}{$l3Key}{$l4Key}}) { | ||
| 51 | if (($_->timestamp ge $frame->timestamp) | ||
| 52 | && $_->l4->error | ||
| 53 | && ($_->l4->error->l4->src == $self->src) | ||
| 54 | && ($_->l4->error->l4->dst == $self->dst)) { | ||
| 55 | return $_; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub pack { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | $self->[$__raw] = $self->SUPER::pack('nnnn', | ||
| 66 | $self->[$__src], | ||
| 67 | $self->[$__dst], | ||
| 68 | $self->[$__length], | ||
| 69 | $self->[$__checksum], | ||
| 70 | ) or return undef; | ||
| 71 | |||
| 72 | 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub unpack { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | my ($src, $dst, $len, $checksum, $payload) = | ||
| 79 | $self->SUPER::unpack('nnnn a*', $self->[$__raw]) | ||
| 80 | or return undef; | ||
| 81 | |||
| 82 | $self->[$__src] = $src; | ||
| 83 | $self->[$__dst] = $dst; | ||
| 84 | $self->[$__length] = $len; | ||
| 85 | $self->[$__checksum] = $checksum; | ||
| 86 | $self->[$__payload] = $payload; | ||
| 87 | |||
| 88 | 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub getLength { NP_UDP_HDR_LEN } | ||
| 92 | |||
| 93 | sub getPayloadLength { | ||
| 94 | my $self = shift; | ||
| 95 | my $len = $self->[$__length]; | ||
| 96 | my $gLen = $self->getLength; | ||
| 97 | ($len > $gLen) ? do { $len - $gLen } : 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _computeTotalLength { | ||
| 101 | my $self = shift; | ||
| 102 | my ($l7) = @_; | ||
| 103 | |||
| 104 | my $totalLength = $self->getLength; | ||
| 105 | $totalLength += $l7->getLength if $l7; | ||
| 106 | $self->[$__length] = $totalLength; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub computeLengths { | ||
| 110 | my $self = shift; | ||
| 111 | my ($env, $l2, $l3, $l4, $l7) = @_; | ||
| 112 | |||
| 113 | $self->_computeTotalLength($l7); | ||
| 114 | 1; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub computeChecksums { | ||
| 118 | my $self = shift; | ||
| 119 | my ($env, $l2, $l3, $l4, $l7) = @_; | ||
| 120 | |||
| 121 | my $phpkt; | ||
| 122 | if ($l3) { | ||
| 123 | if ($l3->isIpv4) { | ||
| 124 | $phpkt = $self->SUPER::pack('a4a4CCn', | ||
| 125 | inetAton($l3->src), | ||
| 126 | inetAton($l3->dst), | ||
| 127 | 0, | ||
| 128 | $l3->protocol, | ||
| 129 | $self->[$__length], | ||
| 130 | ) or return undef; | ||
| 131 | } | ||
| 132 | elsif ($l3->isIpv6) { | ||
| 133 | $phpkt = $self->SUPER::pack('a*a*NnCC', | ||
| 134 | inet6Aton($l3->src), | ||
| 135 | inet6Aton($l3->dst), | ||
| 136 | $l3->payloadLength, | ||
| 137 | 0, | ||
| 138 | 0, | ||
| 139 | $l3->nextHeader, | ||
| 140 | ) or return undef; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | my $totalLength = $self->getLength; | ||
| 145 | $totalLength += $l7->getLength if $l7; | ||
| 146 | |||
| 147 | if ($env->desc->isFamilyIpv4) { | ||
| 148 | $phpkt = $self->SUPER::pack('a4a4CCn', | ||
| 149 | inetAton($env->ip), | ||
| 150 | inetAton($env->desc->target), | ||
| 151 | 0, | ||
| 152 | $env->desc->protocol, | ||
| 153 | $totalLength, | ||
| 154 | ) or return undef; | ||
| 155 | } | ||
| 156 | elsif ($env->desc->isFamilyIpv6) { | ||
| 157 | $phpkt = $self->SUPER::pack('a*a*NnCC', | ||
| 158 | inet6Aton($env->ip6), | ||
| 159 | inet6Aton($env->desc->target), | ||
| 160 | $totalLength, | ||
| 161 | 0, | ||
| 162 | 0, | ||
| 163 | $env->desc->protocol, | ||
| 164 | ) or return undef; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | # Reset the checksum if already filled by a previous pack | ||
| 169 | $self->[$__checksum] = 0; | ||
| 170 | |||
| 171 | $phpkt .= $self->SUPER::pack('nnnn', | ||
| 172 | $self->[$__src], | ||
| 173 | $self->[$__dst], | ||
| 174 | $self->[$__length], | ||
| 175 | $self->[$__checksum], | ||
| 176 | ) or return undef; | ||
| 177 | |||
| 178 | if ($l7 && $l7->data) { | ||
| 179 | $phpkt .= $self->SUPER::pack('a*', $l7->data) | ||
| 180 | or return undef; | ||
| 181 | } | ||
| 182 | |||
| 183 | $self->[$__checksum] = inetChecksum($phpkt); | ||
| 184 | |||
| 185 | 1; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub encapsulate { shift->[$__payload] ? NP_LAYER_7 : NP_LAYER_NONE } | ||
| 189 | |||
| 190 | sub getKey { | ||
| 191 | my $self = shift; | ||
| 192 | $self->is.':'.$self->[$__src].'-'.$self->[$__dst]; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub getKeyReverse { | ||
| 196 | my $self = shift; | ||
| 197 | $self->is.':'.$self->[$__dst].'-'.$self->[$__src]; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub print { | ||
| 201 | my $self = shift; | ||
| 202 | |||
| 203 | my $i = $self->is; | ||
| 204 | my $l = $self->layer; | ||
| 205 | sprintf | ||
| 206 | "$l:+$i: src:%d dst:%d length:%d checksum:0x%02x", | ||
| 207 | $self->[$__src], $self->[$__dst], $self->[$__length], | ||
| 208 | $self->[$__checksum]; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ | ||