line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# NetPacket::UDP - Decode and encode UDP (User Datagram Protocol) |
3
|
|
|
|
|
|
|
# packets. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package NetPacket::UDP; |
6
|
|
|
|
|
|
|
BEGIN { |
7
|
4
|
|
|
4
|
|
56552
|
$NetPacket::UDP::AUTHORITY = 'cpan:YANICK'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
# ABSTRACT: Assemble and disassemble UDP (User Datagram Protocol) packets. |
10
|
|
|
|
|
|
|
$NetPacket::UDP::VERSION = '1.5.0'; |
11
|
4
|
|
|
4
|
|
31
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
141
|
|
12
|
4
|
|
|
4
|
|
18
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
240
|
|
13
|
4
|
|
|
4
|
|
529
|
use NetPacket; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
152
|
|
14
|
4
|
|
|
4
|
|
1118
|
use NetPacket::IP; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
357
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
BEGIN { |
17
|
4
|
|
|
4
|
|
64
|
@ISA = qw(Exporter NetPacket); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Items to export into callers namespace by default |
20
|
|
|
|
|
|
|
# (move infrequently used names to @EXPORT_OK below) |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
8
|
@EXPORT = qw( |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Other items we are prepared to export if requested |
26
|
|
|
|
|
|
|
|
27
|
4
|
|
|
|
|
8
|
@EXPORT_OK = qw(udp_strip |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Tags: |
31
|
|
|
|
|
|
|
|
32
|
4
|
|
|
|
|
1416
|
%EXPORT_TAGS = ( |
33
|
|
|
|
|
|
|
ALL => [@EXPORT, @EXPORT_OK], |
34
|
|
|
|
|
|
|
strip => [qw(udp_strip)], |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# Decode the packet |
41
|
|
|
|
|
|
|
# |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub decode { |
44
|
3
|
|
|
3
|
1
|
29
|
my $class = shift; |
45
|
3
|
|
|
|
|
9
|
my($pkt, $parent) = @_; |
46
|
3
|
|
|
|
|
5
|
my $self = {}; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Class fields |
49
|
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
8
|
$self->{_parent} = $parent; |
51
|
3
|
|
|
|
|
7
|
$self->{_frame} = $pkt; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Decode UDP packet |
54
|
|
|
|
|
|
|
|
55
|
3
|
50
|
|
|
|
10
|
if (defined($pkt)) { |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
22
|
($self->{src_port}, $self->{dest_port}, $self->{len}, $self->{cksum}, |
58
|
|
|
|
|
|
|
$self->{data}) = unpack("nnnna*", $pkt); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Return a blessed object |
62
|
|
|
|
|
|
|
|
63
|
3
|
|
|
|
|
10
|
bless($self, $class); |
64
|
3
|
|
|
|
|
13
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# |
68
|
|
|
|
|
|
|
# Strip header from packet and return the data contained in it |
69
|
|
|
|
|
|
|
# |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
undef &udp_strip; |
72
|
|
|
|
|
|
|
*udp_strip = \&strip; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub strip { |
75
|
1
|
|
|
1
|
1
|
4
|
return decode(__PACKAGE__,shift)->{data}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# Encode a packet |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub encode { |
83
|
1
|
|
|
1
|
0
|
4154
|
my ($self, $ip) = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Adjust the length accordingly |
86
|
1
|
|
|
|
|
5
|
$self->{len} = 8 + length($self->{data}); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# First of all, fix the checksum |
89
|
1
|
|
|
|
|
5
|
$self->checksum($ip); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Put the packet together |
92
|
1
|
|
|
|
|
8
|
return pack("nnnna*", $self->{src_port},$self->{dest_port}, |
93
|
|
|
|
|
|
|
$self->{len}, $self->{cksum}, $self->{data}); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
# UDP Checksum |
99
|
|
|
|
|
|
|
# |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub checksum { |
102
|
|
|
|
|
|
|
|
103
|
3
|
|
|
3
|
0
|
1983
|
my( $self, $ip ) = @_; |
104
|
|
|
|
|
|
|
|
105
|
3
|
|
|
|
|
8
|
my $proto = NetPacket::IP::IP_PROTO_UDP; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Pack pseudo-header for udp checksum |
108
|
|
|
|
|
|
|
|
109
|
3
|
|
|
|
|
399
|
my $src_ip = gethostbyname($ip->{src_ip}); |
110
|
3
|
|
|
|
|
149
|
my $dest_ip = gethostbyname($ip->{dest_ip}); |
111
|
|
|
|
|
|
|
|
112
|
4
|
|
|
4
|
|
29
|
no warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
541
|
|
113
|
|
|
|
|
|
|
|
114
|
3
|
|
|
|
|
38
|
my $packet = pack 'a4a4CCnnnnna*' => |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# fake ip header part |
117
|
|
|
|
|
|
|
$src_ip, $dest_ip, 0, $proto, $self->{len}, |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# proper UDP part |
120
|
|
|
|
|
|
|
$self->{src_port}, $self->{dest_port}, $self->{len}, 0, $self->{data}; |
121
|
|
|
|
|
|
|
|
122
|
3
|
100
|
|
|
|
23
|
$packet .= "\x00" if length($packet) % 2; |
123
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
18
|
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet)); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__END__ |