line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NetPacket::UDP; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Assemble and disassemble UDP (User Datagram Protocol) packets. |
4
|
|
|
|
|
|
|
$NetPacket::UDP::VERSION = '1.7.2'; |
5
|
4
|
|
|
4
|
|
76304
|
use strict; |
|
4
|
|
|
|
|
19
|
|
|
4
|
|
|
|
|
138
|
|
6
|
4
|
|
|
4
|
|
22
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
124
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
477
|
use parent 'NetPacket'; |
|
4
|
|
|
|
|
373
|
|
|
4
|
|
|
|
|
19
|
|
9
|
4
|
|
|
4
|
|
1132
|
use NetPacket::IP; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
1430
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(udp_strip); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
14
|
|
|
|
|
|
|
ALL => [@EXPORT_OK], |
15
|
|
|
|
|
|
|
strip => [qw(udp_strip)], |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# Decode the packet |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub decode { |
23
|
3
|
|
|
3
|
1
|
38
|
my $class = shift; |
24
|
3
|
|
|
|
|
9
|
my($pkt, $parent) = @_; |
25
|
3
|
|
|
|
|
8
|
my $self = {}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Class fields |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
8
|
$self->{_parent} = $parent; |
30
|
3
|
|
|
|
|
8
|
$self->{_frame} = $pkt; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Decode UDP packet |
33
|
|
|
|
|
|
|
|
34
|
3
|
50
|
|
|
|
10
|
if (defined($pkt)) { |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
($self->{src_port}, $self->{dest_port}, $self->{len}, $self->{cksum}, |
37
|
3
|
|
|
|
|
17
|
$self->{data}) = unpack("nnnna*", $pkt); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Return a blessed object |
41
|
|
|
|
|
|
|
|
42
|
3
|
|
|
|
|
7
|
bless($self, $class); |
43
|
3
|
|
|
|
|
26
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# |
47
|
|
|
|
|
|
|
# Strip header from packet and return the data contained in it |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub udp_strip { |
51
|
1
|
|
|
1
|
0
|
6
|
goto \&strip; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub strip { |
55
|
1
|
|
|
1
|
1
|
57
|
return decode(__PACKAGE__,shift)->{data}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
# Encode a packet |
60
|
|
|
|
|
|
|
# |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub encode { |
63
|
1
|
|
|
1
|
0
|
4015
|
my ($self, $ip) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Adjust the length accordingly |
66
|
1
|
|
|
|
|
4
|
$self->{len} = 8 + length($self->{data}); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# First of all, fix the checksum |
69
|
1
|
|
|
|
|
5
|
$self->checksum($ip); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Put the packet together |
72
|
|
|
|
|
|
|
return pack("nnnna*", $self->{src_port},$self->{dest_port}, |
73
|
1
|
|
|
|
|
7
|
$self->{len}, $self->{cksum}, $self->{data}); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# |
78
|
|
|
|
|
|
|
# UDP Checksum |
79
|
|
|
|
|
|
|
# |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub checksum { |
82
|
|
|
|
|
|
|
|
83
|
3
|
|
|
3
|
0
|
1324
|
my( $self, $ip ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
3
|
|
|
|
|
6
|
my $proto = NetPacket::IP::IP_PROTO_UDP; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Pack pseudo-header for udp checksum |
88
|
|
|
|
|
|
|
|
89
|
4
|
|
|
4
|
|
34
|
no warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
895
|
|
90
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
7
|
my $packet; |
92
|
3
|
50
|
|
|
|
31
|
if ($ip->isa('NetPacket::IPv6')) { |
93
|
0
|
|
|
|
|
0
|
$packet = $ip->pseudo_header($self->{len}, $proto); |
94
|
|
|
|
|
|
|
} else { |
95
|
3
|
|
|
|
|
169
|
my $src_ip = gethostbyname($ip->{src_ip}); |
96
|
3
|
|
|
|
|
96
|
my $dest_ip = gethostbyname($ip->{dest_ip}); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$packet = pack 'a4a4CCn' => |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# fake ip header part |
101
|
3
|
|
|
|
|
37
|
$src_ip, $dest_ip, 0, $proto, $self->{len}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$packet .= pack 'nnnna*' => |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# proper UDP part |
107
|
3
|
|
|
|
|
18
|
$self->{src_port}, $self->{dest_port}, $self->{len}, 0, $self->{data}; |
108
|
|
|
|
|
|
|
|
109
|
3
|
100
|
|
|
|
17
|
$packet .= "\x00" if length($packet) % 2; |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
16
|
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet)); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |