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.7.2
11

SYNOPSIS

13         use NetPacket::UDP;
14
15         $udp_obj = NetPacket::UDP->decode($raw_pkt);
16         $udp_pkt = $udp_obj->encode($l3_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       "$udp_packet-<gt"encode($l3_obj)>
31           Return the encoded version of the UDP packet object. Needs part of
32           the IP header contained (src_ip and dest_ip specifically) in
33           $l3_obj, in order to calculate the UDP checksum. The length field
34           will also be set automatically based on values provided.
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 object.
44
45   Instance data
46       The instance data for the "NetPacket::UDP" object consists of the
47       following fields.
48
49       src_port
50           The source UDP port for the datagram.
51
52       dest_port
53           The destination UDP port for the datagram.
54
55       len The length (including length of header) in bytes for this packet.
56
57       cksum
58           The checksum value for this packet.
59
60       data
61           The encapsulated data (payload) for this packet.
62
63   IP data
64       The IP data for the $l3_obj object consists of the following fields.
65       Additional items may be supplied as well as passing the whole object
66       returned by NetPacket::IP->decode but are unnecessary.
67
68       src_ip
69           The source IP for the datagram
70
71       dest_ip
72           The destination IP for the datagram
73
74   Exports
75       default
76           none
77
78       exportable
79           udp_strip
80
81       tags
82           The following tags group together related exportable items.
83
84           ":strip"
85               Import the strip function "udp_strip".
86
87           ":ALL"
88               All the above exportable items.
89

EXAMPLE

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

AUTHOR

165       Tim Potter <tpot@samba.org>
166
167       Stephanie Wehner <atrak@itsx.com>
168
169       Yanick Champoux <yanick@cpan.org>
170
171
172
173perl v5.36.0                      2023-01-20                 NetPacket::UDP(3)
Impressum