2
# NetPacket::UDP - Decode and encode UDP (User Datagram Protocol)
5
package NetPacket::UDP;
7
$NetPacket::UDP::AUTHORITY = 'cpan:YANICK';
10
$NetPacket::UDP::VERSION = '1.4.2';
12
# ABSTRACT: Assemble and disassemble UDP (User Datagram Protocol) packets.
15
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
20
@ISA = qw(Exporter NetPacket);
22
# Items to export into callers namespace by default
23
# (move infrequently used names to @EXPORT_OK below)
28
# Other items we are prepared to export if requested
30
@EXPORT_OK = qw(udp_strip
36
ALL => [@EXPORT, @EXPORT_OK],
37
strip => [qw(udp_strip)],
48
my($pkt, $parent) = @_;
53
$self->{_parent} = $parent;
54
$self->{_frame} = $pkt;
60
($self->{src_port}, $self->{dest_port}, $self->{len}, $self->{cksum},
61
$self->{data}) = unpack("nnnna*", $pkt);
64
# Return a blessed object
71
# Strip header from packet and return the data contained in it
78
return decode(__PACKAGE__,shift)->{data};
88
# Adjust the length accordingly
89
$self->{len} = 8 + length($self->{data});
91
# First of all, fix the checksum
94
# Put the packet together
95
return pack("nnnna*", $self->{src_port},$self->{dest_port},
96
$self->{len}, $self->{cksum}, $self->{data});
106
my( $self, $ip ) = @_;
108
my $proto = NetPacket::IP::IP_PROTO_UDP;
110
# Pack pseudo-header for udp checksum
112
my $src_ip = gethostbyname($ip->{src_ip});
113
my $dest_ip = gethostbyname($ip->{dest_ip});
117
my $packet = pack 'a4a4CCnnnnna*' =>
119
# fake ip header part
120
$src_ip, $dest_ip, 0, $proto, $self->{len},
123
$self->{src_port}, $self->{dest_port}, $self->{len}, 0, $self->{data};
125
$packet .= "\x00" if length($packet) % 2;
127
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet));
139
NetPacket::UDP - Assemble and disassemble UDP (User Datagram Protocol) packets.
149
$udp_obj = NetPacket::UDP->decode($raw_pkt);
150
$udp_pkt = $udp_obj->encode($l3_obj);
151
$udp_data = NetPacket::UDP::strip($raw_pkt);
155
C<NetPacket::UDP> provides a set of routines for assembling and
156
disassembling packets using UDP (User Datagram Protocol).
162
=item C<NetPacket::UDP-E<gt>decode([RAW PACKET])>
164
Decode the raw packet data given and return an object containing
165
instance data. This method will quite happily decode garbage input.
166
It is the responsibility of the programmer to ensure valid packet data
167
is passed to this method.
169
=item C<$udp_packet-<gt>encode($l3_obj)>
171
Return the encoded version of the UDP packet object. Needs
172
part of the IP header contained (src_ip and dest_ip specifically) in $l3_obj,
173
in order to calculate the UDP checksum. The length field will also be set
174
automatically based on values provided.
182
=item C<NetPacket::UDP::strip([RAW PACKET])>
184
Return the encapsulated data (or payload) contained in the UDP
185
packet. This data is suitable to be used as input for other
186
C<NetPacket::*> modules.
188
This function is equivalent to creating an object using the
189
C<decode()> constructor and returning the C<data> field of that
196
The instance data for the C<NetPacket::UDP> object consists of
197
the following fields.
203
The source UDP port for the datagram.
207
The destination UDP port for the datagram.
211
The length (including length of header) in bytes for this packet.
215
The checksum value for this packet.
219
The encapsulated data (payload) for this packet.
225
The IP data for the $l3_obj object consists of the following fields.
226
Additional items may be supplied as well as passing the whole
227
object returned by NetPacket::IP->decode but are unnecessary.
233
The source IP for the datagram
237
The destination IP for the datagram
255
The following tags group together related exportable items.
261
Import the strip function C<udp_strip>.
265
All the above exportable items.
273
The following example prints the source IP address and port, the
274
destination IP address and port, and the UDP packet length:
280
use NetPacket::Ethernet qw(:strip);
285
my($arg, $hdr, $pkt) = @_;
287
my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
288
my $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
290
print("$ip_obj->{src_ip}:$udp_obj->{src_port} -> ",
291
"$ip_obj->{dest_ip}:$udp_obj->{dest_port} ",
292
"$udp_obj->{len}\n");
295
Net::PcapUtils::loop(\&process_pkt, FILTER => 'udp');
297
The following is an example use in combination with Net::Divert
298
to alter the payload of packets that pass through. All occurences
299
of foo will be replaced with bar. This example is easy to test with
300
netcat, but otherwise makes little sense. :) Adapt to your needs:
303
use NetPacket::IP qw(IP_PROTO_UDP);
306
$divobj = Net::Divert->new('yourhost',9999);
308
$divobj->getPackets(\&alterPacket);
312
my ($data, $fwtag) = @_;
314
$ip_obj = NetPacket::IP->decode($data);
316
if($ip_obj->{proto} == IP_PROTO_UDP) {
318
# decode the UDP header
319
$udp_obj = NetPacket::UDP->decode($ip_obj->{data});
321
# replace foo in the payload with bar
322
$udp_obj->{data} =~ s/foo/bar/g;
324
# reencode the packet
325
$ip_obj->{data} = $udp_obj->encode($udp_obj, $ip_obj);
326
$data = $ip_obj->encode;
330
$divobj->putPacket($data,$fwtag);
335
Copyright (c) 2001 Tim Potter.
337
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
338
the participants in the CRC for Advanced Computational Systems
341
This module is free software. You can redistribute it and/or
342
modify it under the terms of the Artistic License 2.0.
344
This program is distributed in the hope that it will be useful,
345
but without any warranty; without even the implied warranty of
346
merchantability or fitness for a particular purpose.
350
Tim Potter E<lt>tpot@samba.orgE<gt>
352
Stephanie Wehner E<lt>atrak@itsx.comE<gt>
354
Yanick Champoux <yanick@cpan.org>