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. the length field 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 other NetPacket::* 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 datagram
  • dest_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 function udp_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 (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