NetPacket::UDP(3)
β¨ NAME β¨
NetPacket::UDP β bestie, this is the manual page for it (section 3 of the User Contributed Perl Documentation, if you're keeping receipts) π and it does exactly two things and slays at both: it assembles and disassembles UDP (User Datagram Protocol) packets.
π VERSION π
version 1.8.0 β yes she's got a version number and yes she's proud of it π«Ά
πΈ SYNOPSIS πΈ
okay so here's the whole vibe in four lines, no notes:
use NetPacket::UDP;
$udp_obj = NetPacket::UDP->decode($raw_pkt);
$udp_pkt = $udp_obj->encode($l3_obj);
$udp_data = NetPacket::UDP::strip($raw_pkt);
π¦ DESCRIPTION π¦
NetPacket::UDP hands you a little set of routines for assembling and disassembling packets using UDP (User Datagram Protocol). that's it, that's the whole job, and she does it flawlessly β¨
π Methods π
NetPacket::UDP->decode([RAW PACKET])β decode the raw packet data you hand over and return an object stuffed with instance data. and here's the tea: this method will quite happily decode garbage input π it does not check, it does not care, it is not your mother. it is the programmer's responsibility to make sure valid packet data is passed to this method. fr fr, check your data first.$udp_packet->encode($l3_obj)β returns the encoded version of the UDP packet object. needs part of the IP header contained in$l3_objβ src_ip and dest_ip specifically β because that's how it calculates the UDP checksum, iykyk. thelengthfield also gets set automatically based on the values you provided. no manual labour required π
π Functions π
NetPacket::UDP::strip([RAW PACKET])β returns the encapsulated data (aka the payload) that's chillin' inside the UDP packet. that data is suitable to be used as input for otherNetPacket::*modules, so it's a whole pipeline moment π₯
this function is equivalent to creating an object using the decode() constructor and returning the data field of that object. same energy, shorter syntax.
π Instance data π
the instance data for the NetPacket::UDP object consists of the following fields, and you're gonna be obsessed with all of them:
src_portβ the source UDP port for the datagram.dest_portβ the destination UDP port for the datagram.lenβ the length (including length of header) in bytes for this packet.cksumβ the checksum value for this packet.dataβ the encapsulated data (payload) for this packet.
π§ IP data π§
the IP data for the $l3_obj object consists of the following fields. additional items may be supplied as well as passing the whole object returned by NetPacket::IP->decode but those are unnecessary β lowkey optional, highkey fine:
src_ipβ the source IP for the datagramdest_ipβ the destination IP for the datagram
π Exports π
- default β none. as in, zero. nothing. she gives you nothing by default π
- exportable β
udp_strip - tags β the following tags group together related exportable items:
:stripβ import the strip functionudp_strip.:ALLβ all the above exportable items. the everything bagel π₯―
β EXAMPLE β
the following example prints the source IP address and port, the destination IP address and port, and the UDP packet length. look at her go:
#!/usr/bin/perl -w
use strict;
use Net::PcapUtils;
use NetPacket::Ethernet qw(:strip);
use NetPacket::IP;
use NetPacket::UDP;
sub process_pkt {
my($arg, $hdr, $pkt) = @_;
my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
my $udp_obj = NetPacket::UDP->decode($ip_obj->{data});
print("$ip_obj->{src_ip}:$udp_obj->{src_port} -> ",
"$ip_obj->{dest_ip}:$udp_obj->{dest_port} ",
"$udp_obj->{len}\n");
}
Net::PcapUtils::loop(\&process_pkt, FILTER => 'udp');
the following is an example use in combination with Net::Divert to alter the payload of packets that pass through. all occurrences of foo will be replaced with bar. this example is easy to test with netcat, but otherwise makes little sense. :) adapt to your needs:
use Net::Divert;
use NetPacket::IP qw(IP_PROTO_UDP);
use NetPacket::UDP;
$divobj = Net::Divert->new('yourhost',9999);
$divobj->getPackets(\&alterPacket);
sub alterPacket
{
my ($data, $fwtag) = @_;
$ip_obj = NetPacket::IP->decode($data);
if($ip_obj->{proto} == IP_PROTO_UDP) {
# decode the UDP header
$udp_obj = NetPacket::UDP->decode($ip_obj->{data});
# replace foo in the payload with bar
$udp_obj->{data} =~ s/foo/bar/g;
# re-encode the packet
$ip_obj->{data} = $udp_obj->encode($udp_obj, $ip_obj);
$data = $ip_obj->encode;
}
$divobj->putPacket($data,$fwtag);
}
π COPYRIGHT π
Copyright (c) 2001 Tim Potter.
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of the participants in the CRC for Advanced Computational Systems ('ACSys').
this module is free software. you can redistribute it and/or modify it under the terms of the Artistic License 2.0. π
this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. read that twice bestie, no warranty, periodt.
π«Ά AUTHOR π«Ά
- Tim Potter
<tpot@samba.org> - Stephanie Wehner
<atrak@itsx.com> - Yanick Champoux
<yanick@cpan.org>
okay that's the whole page, mwah π this was perl v5.42.0, dated 2026-02-05, and it's the NetPacket::UDP(3) manpage β go decode something cute, and pls validate your input first ππ
π¦ where this page comes from
- fedora 43: perl-NetPacket 1.8.0-1.fc43 Β· Artistic-2.0 AND (GPL-1.0-or-later OR Artistic-1.0-Perl)
- fedora 44: perl-NetPacket 1.8.0-1.fc44 Β· Artistic-2.0 AND (GPL-1.0-or-later OR Artistic-1.0-Perl)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms