1NetPacket::UDP(3) User Contributed Perl Documentation NetPacket::UDP(3)
2
3
4
6 NetPacket::UDP - Assemble and disassemble UDP (User Datagram Protocol)
7 packets.
8
10 version 1.7.2
11
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
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
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 IP data
65 The IP data for the $l3_obj object consists of the following fields.
66 Additional items may be supplied as well as passing the whole object
67 returned by NetPacket::IP->decode but are unnecessary.
68
69 src_ip
70 The source IP for the datagram
71
72 dest_ip
73 The destination IP for the datagram
74
75 Exports
76 default
77 none
78
79 exportable
80 udp_strip
81
82 tags
83 The following tags group together related exportable items.
84
85 ":strip"
86 Import the strip function "udp_strip".
87
88 ":ALL"
89 All the above exportable items.
90
92 The following example prints the source IP address and port, the
93 destination IP address and port, and the UDP packet length:
94
95 #!/usr/bin/perl -w
96
97 use strict;
98 use Net::PcapUtils;
99 use NetPacket::Ethernet qw(:strip);
100 use NetPacket::IP;
101 use NetPacket::UDP;
102
103 sub process_pkt {
104 my($arg, $hdr, $pkt) = @_;
105
106 my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
107 my $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
108
109 print("$ip_obj->{src_ip}:$udp_obj->{src_port} -> ",
110 "$ip_obj->{dest_ip}:$udp_obj->{dest_port} ",
111 "$udp_obj->{len}\n");
112 }
113
114 Net::PcapUtils::loop(\&process_pkt, FILTER => 'udp');
115
116 The following is an example use in combination with Net::Divert to
117 alter the payload of packets that pass through. All occurrences of foo
118 will be replaced with bar. This example is easy to test with netcat,
119 but otherwise makes little sense. :) Adapt to your needs:
120
121 use Net::Divert;
122 use NetPacket::IP qw(IP_PROTO_UDP);
123 use NetPacket::UDP;
124
125 $divobj = Net::Divert->new('yourhost',9999);
126
127 $divobj->getPackets(\&alterPacket);
128
129 sub alterPacket
130 {
131 my ($data, $fwtag) = @_;
132
133 $ip_obj = NetPacket::IP->decode($data);
134
135 if($ip_obj->{proto} == IP_PROTO_UDP) {
136
137 # decode the UDP header
138 $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
139
140 # replace foo in the payload with bar
141 $udp_obj->{data} =~ s/foo/bar/g;
142
143 # re-encode the packet
144 $ip_obj->{data} = $udp_obj->encode($udp_obj, $ip_obj);
145 $data = $ip_obj->encode;
146
147 }
148
149 $divobj->putPacket($data,$fwtag);
150 }
151
153 Copyright (c) 2001 Tim Potter.
154
155 Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of the
156 participants in the CRC for Advanced Computational Systems ('ACSys').
157
158 This module is free software. You can redistribute it and/or modify it
159 under the terms of the Artistic License 2.0.
160
161 This program is distributed in the hope that it will be useful, but
162 without any warranty; without even the implied warranty of
163 merchantability or fitness for a particular purpose.
164
166 Tim Potter <tpot@samba.org>
167
168 Stephanie Wehner <atrak@itsx.com>
169
170 Yanick Champoux <yanick@cpan.org>
171
172
173
174perl v5.34.0 2022-01-21 NetPacket::UDP(3)