1NetPacket::UDP(3)     User Contributed Perl Documentation    NetPacket::UDP(3)
2
3
4

NAME

6       NetPacket::UDP - Assemble and disassemble UDP (User Datagram Protocol)
7       packets.
8

VERSION

10       version 1.2.0
11

SYNOPSIS

13         use NetPacket::UDP;
14
15         $udp_obj = NetPacket::UDP->decode($raw_pkt);
16         $udp_pkt = NetPacket::UDP->encode($ip_obj);
17         $udp_data = NetPacket::UDP::strip($raw_pkt);
18

DESCRIPTION

20       "NetPacket::UDP" provides a set of routines for assembling and
21       disassembling packets using UDP (User Datagram Protocol).
22
23   Methods
24       "NetPacket::UDP->decode([RAW PACKET])"
25           Decode the raw packet data given and return an object containing
26           instance data.  This method will quite happily decode garbage
27           input.  It is the responsibility of the programmer to ensure valid
28           packet data is passed to this method.
29
30       "NetPacket::UDP->encode($ip_obj)"
31           Return a UDP packet encoded with the instance data specified. Needs
32           parts of the IP header contained in $ip_obj, the IP object, in
33           order to calculate the UDP checksum. The length field will also be
34           set automatically.
35
36   Functions
37       "NetPacket::UDP::strip([RAW PACKET])"
38           Return the encapsulated data (or payload) contained in the UDP
39           packet.  This data is suitable to be used as input for other
40           "NetPacket::*" modules.
41
42           This function is equivalent to creating an object using the
43           "decode()" constructor and returning the "data" field of that
44           object.
45
46   Instance data
47       The instance data for the "NetPacket::UDP" object consists of the
48       following fields.
49
50       src_port
51           The source UDP port for the datagram.
52
53       dest_port
54           The destination UDP port for the datagram.
55
56       len The length (including length of header) in bytes for this packet.
57
58       cksum
59           The checksum value for this packet.
60
61       data
62           The encapsulated data (payload) for this packet.
63
64   Exports
65       default
66           none
67
68       exportable
69           udp_strip
70
71       tags
72           The following tags group together related exportable items.
73
74           ":strip"
75               Import the strip function "udp_strip".
76
77           ":ALL"
78               All the above exportable items.
79

EXAMPLE

81       The following example prints the source IP address and port, the
82       destination IP address and port, and the UDP packet length:
83
84         #!/usr/bin/perl -w
85
86         use strict;
87         use Net::PcapUtils;
88         use NetPacket::Ethernet qw(:strip);
89         use NetPacket::IP;
90         use NetPacket::UDP;
91
92         sub process_pkt {
93             my($arg, $hdr, $pkt) = @_;
94
95             my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
96             my $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
97
98             print("$ip_obj->{src_ip}:$udp_obj->{src_port} -> ",
99                   "$ip_obj->{dest_ip}:$udp_obj->{dest_port} ",
100                   "$udp_obj->{len}\n");
101         }
102
103         Net::PcapUtils::loop(\&process_pkt, FILTER => 'udp');
104
105       The following is an example use in combination with Net::Divert to
106       alter the payload of packets that pass through. All occurences of foo
107       will be replaced with bar. This example is easy to test with netcat,
108       but otherwise makes little sense. :) Adapt to your needs:
109
110           use Net::Divert;
111           use NetPacket::IP qw(IP_PROTO_UDP);
112           use NetPacket::UDP;
113
114           $divobj = Net::Divert->new('yourhost',9999);
115
116           $divobj->getPackets(\&alterPacket);
117
118           sub alterPacket
119           {
120               my ($data, $fwtag) = @_;
121
122               $ip_obj = NetPacket::IP->decode($data);
123
124               if($ip_obj->{proto} == IP_PROTO_UDP) {
125
126                   # decode the UDP header
127                   $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
128
129                   # replace foo in the payload with bar
130                   $udp_obj->{data} =~ s/foo/bar/g;
131
132                   # reencode the packet
133                   $ip_obj->{data} = $udp_obj->encode($ip_obj);
134                   $data = $ip_obj->encode;
135
136               }
137
138               $divobj->putPacket($data,$fwtag);
139           }
140
142       Copyright (c) 2001 Tim Potter.
143
144       Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of the
145       participants in the CRC for Advanced Computational Systems ('ACSys').
146
147       This module is free software.  You can redistribute it and/or modify it
148       under the terms of the Artistic License 2.0.
149
150       This program is distributed in the hope that it will be useful, but
151       without any warranty; without even the implied warranty of
152       merchantability or fitness for a particular purpose.
153

AUTHOR

155       Tim Potter <tpot@samba.org>
156
157       Stephanie Wehner <atrak@itsx.com>
158
159       Yanick Champoux <yanick@cpan.org>
160
161
162
163perl v5.12.3                      2011-07-30                 NetPacket::UDP(3)
Impressum