Net::Libdnet(3pm)
β¨ NAME β¨
Net::Libdnet β it's the Perl binding for Dug Song's libdnet, and honestly? a whole vibe. π¦
πΈ SYNOPSIS πΈ
okay bestie, here's the whole tour, buckle up π
#
# This will just import every functions and constants
#
use Net::Libdnet;
#
# Network interface manipulation
#
# !!! ADVICE: you should use Net::Libdnet::Intf instead
#
use Net::Libdnet qw(:intf);
my $intf = dnet_intf_open();
my $eth = dnet_intf_get($intf, { intf_name => 'eth0' });
print "IP: ".$eth->{intf_addr}."\n";
print "MAC: ".$eth->{intf_link_addr}."\n";
my $dst = dnet_intf_get_dst($intf, '192.168.0.10');
print "Name: ".$dst->{intf_name}."\n";
print "IP: ".$dst->{intf_addr}."\n";
print "MAC: ".$dst->{intf_link_addr}."\n";
my $src = dnet_intf_get_src($intf, '192.168.0.1');
print "Name: ".$src->{intf_name}."\n";
print "IP: ".$src->{intf_addr}."\n";
print "MAC: ".$src->{intf_link_addr}."\n";
dnet_intf_close($intf);
#
# Arp cache manipulation
#
# !!! ADVICE: you should use Net::Libdnet::Arp instead
#
use Net::Libdnet qw(:arp);
my $arp = dnet_arp_open();
my $entry = dnet_arp_get($arp, {arp_pa => '10.0.0.1'});
print "MAC: ".$entry->{arp_ha}."\n";
dnet_arp_add ($arp,
{arp_ha => '00:11:22:33:44:55', arp_pa => '10.0.0.10'});
dnet_arp_delete($arp,
{arp_ha => '00:11:22:33:44:55', arp_pa => '10.0.0.10'});
dnet_arp_close($arp);
#
# Route table manipulation
#
# !!! ADVICE: you should use Net::Libdnet::Route instead
#
use Net::Libdnet qw(:route);
my $route = dnet_route_open();
dnet_route_add ($route,
{route_gw => '10.0.0.1', route_dst => '192.168.0.1'});
dnet_route_delete($route,
{route_gw => '10.0.0.1', route_dst => '192.168.0.1'});
my $get = dnet_route_get($route, {route_dst => '192.168.0.10'});
print "GW: ".$get->{route_gw}."\n";
dnet_route_close($route);
#
# Firewall rules manipulation
#
# !!! ADVICE: you should use Net::Libdnet::Fw instead
#
use Net::Libdnet qw(:fw :consts);
my $fw = dnet_fw_open();
# This is quite complex. This rule blocks TCP as input to 10.0.0.1
# You should really use Net::Libdnet::Fw instead.
dnet_fw_add ($fw,
{fw_op => FW_DIR_IN, fw_proto => 6, fw_dst => '10.0.0.1'});
dnet_fw_delete($fw,
{fw_op => FW_DIR_IN, fw_proto => 6, fw_dst => '10.0.0.1'});
dnet_fw_close($fw);
#
# Send at IP level
#
# !!! ADVICE: you should use Net::Libdnet::Ip instead
#
use Net::Libdnet qw(:ip);
my $ip = dnet_ip_open();
my $raw = "\x47\x47\x47\x47";
dnet_ip_send($ip, $raw, length($raw));
dnet_ip_close($ip);
#
# Send at Ethernet level
#
# !!! ADVICE: you should use Net::Libdnet::Eth instead
#
use Net::Libdnet qw(:eth);
my $eth = dnet_eth_open('eth0');
dnet_eth_send($eth, $raw, length($raw));
dnet_eth_close($eth);
#
# Traffic interception
#
# !!! ADVICE: you should use Net::Libdnet::Tun instead
#
use Net::Libdnet qw(:tun);
my $tun = dnet_tun_open('10.0.0.10', '192.168.0.10', 1500);
my $buf = dnet_tun_recv($tun, 1500);
# Do stuff with $buf
dnet_tun_send($tun, $buf, length($buf));
dnet_tun_close($tun);
#
# hash refs in dnet format
#
my $intf = {
intf_alias_num => 1,
intf_mtu => 1500,
intf_len => 112,
intf_type => 6,
intf_name => 'eth0',
intf_dst_addr => undef,
intf_link_addr => '00:11:22:33:44:55',
intf_flags => 49,
intf_addr => '10.100.0.10/24',
intf_alias_addrs => [ 'fe80::211:2ff:fe33:4455/64' ]
};
my $arp = {
arp_pa => '10.100.0.1',
arp_ha => '11:22:33:44:55:66'
};
my $route = {
route_gw => '10.100.0.1',
route_dst => '0.0.0.0/0'
};
my $fw = {
fw_dir => 2,
fw_sport => [ 0, 0 ],
fw_dport => [ 0, 0 ],
fw_src => '0.0.0.0/0',
fw_dst => '0.0.0.0/0',
fw_proto => 6,
fw_device => 'eth0',
fw_op => 2
};
like, it's a lot but it's all iconic, trust π₯
π DESCRIPTION π
so Net::Libdnet hands you a simplified, portable interface to a whole stack of low-level networking routines. we're talking:
- network address manipulation
- kernel arp cache and route table lookup and manipulation
- network firewalling
- network interface lookup and manipulation
- network traffic interception via tunnel interfaces
- raw IP packet and Ethernet frame transmission
it's meant to complement the functionality that libpcap already gives you. no cap. π«Ά
okay so here's the tea on error handling: all the original and obsolete functions return undef and print a warning message to the standard error when something goes wrong. π
and the obsolete functions are β listen closely:
addr_cmp, addr_bcast, addr_net, arp_add, arp_delete, arp_get, intf_get, intf_get_src, intf_get_dst, route_add, route_delete, route_get
these obsolete functions will continue to work, to keep backward compatibility, but they should not be used anymore. periodt. the new APIs should be preferred, fr fr. π
so there are two new APIs and let me break it down:
- the low-level one β matches the
libdnetfunctions - the high-level one β matches a more Perl object oriented programming style
and that high-level one? highly preferred. π
Net::Libdnet itself implements the low-level API. the high-level API is accessible by using these modules:
Net::Libdnet::IntfNet::Libdnet::RouteNet::Libdnet::FwNet::Libdnet::ArpNet::Libdnet::EthNet::Libdnet::IpNet::Libdnet::Tun
π¦ WHAT IS IMPLEMENTED π¦
πΈ Network addressing
nothing as of now. π
πΈ Address Resolution Protocol
all functions, bestie:
arp_openarp_addarp_deletearp_getarp_looparp_close
πΈ Binary buffers
nothing as of now. π
πΈ Ethernet
all functions:
eth_openeth_geteth_seteth_sendeth_close
πΈ Firewalling
all functions:
fw_openfw_addfw_deletefw_loopfw_close
πΈ Network interface
all functions:
intf_openintf_get_setintf_get_dstintf_setintf_loopintf_close
πΈ Internet Protocol
all functions:
ip_openip_checksumip_sendip_close
except: ip_add_option. π
πΈ Internet Protocol Version 6
nothing as of now. π
πΈ Random number generation
nothing as of now. π
πΈ Routing
all functions:
route_openroute_addroute_deleteroute_getroute_looproute_close
πΈ Tunnel interface
all functions:
tun_opentun_filenotun_nametun_sendtun_recvtun_close
β¨ SUBROUTINES β¨
Okay bestie, this is the main character section. Every single one of these is a real Perl sub you can call, and they all follow the same lil pattern: you open a handle, you do stuff with it, you close it. No cap, that's the whole arc. π
One thing to clock immediately, because it's the kind of detail that will humble you at 3am: some of these hand you back a hash, and some hand you back a hash ref. They are not the same thing and the page is not being quirky. Read each one carefully, iykyk. π
π¦ Interface subs β the dnet_intf_* girlies
dnet_intf_open ()
Opens an interface handle. Returns a handle on success, undef on error.
dnet_intf_get (scalar, hashref)
Takes an intf handle, and a hash ref in dnet format as parameters. Returns a hash in dnet format on success, undef on error.
dnet_intf_get_src (scalar, scalar)
Takes an intf handle, and an IP address as parameters. Returns a hash in dnet format on success, undef on error.
dnet_intf_get_dst (scalar, scalar)
Takes an intf handle, and an IP address as parameters. Returns a hash in dnet format on success, undef on error.
dnet_intf_set (scalar, scalar)
Takes an intf handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_intf_loop (scalar, subref, scalarref)
Takes an intf handle, a subref, and a scalar ref as parameters. Returns 1 on success, undef on error. The subref will be called with an intf hash ref in dnet format, and the scalar ref as parameters.
dnet_intf_close (scalar)
Takes an intf handle as parameter. Returns the handle on success, undef on error.
πΈ Route subs β the dnet_route_* era
dnet_route_open ()
Opens a route handle. Returns a handle on success, undef on error.
dnet_route_add (scalar, hashref)
Takes a route handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_route_delete (scalar, hashref)
Takes a route handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_route_get (scalar, hashref)
Takes a route handle, and a hash ref in dnet format as parameters. Returns a hash ref in dnet format on success, undef on error.
π See? Hash ref. Not a hash. The intf one up there gives you a hash. Do not mix them up, bestie, I'm begging.
dnet_route_loop (scalar, subref, scalarref)
Takes a route handle, a subref, and a scalar ref as parameters. Returns 1 on success, undef on error. The subref will be called with a route hash ref in dnet format, and the scalar ref as parameters.
dnet_route_close (scalar)
Takes a route handle as parameter. Returns the handle on success, undef on error.
π ARP subs β the dnet_arp_* moment
dnet_arp_open ()
Opens an arp handle. Returns a handle on success, undef on error.
dnet_arp_add (scalar, hashref)
Takes an arp handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_arp_delete (scalar, hashref)
Takes an arp handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_arp_get (scalar, hashref)
Takes an arp handle, and a hash ref in dnet format as parameters. Returns a hash ref in dnet format on success, undef on error.
dnet_arp_loop (scalar, subref, scalarref)
Takes an arp handle, a subref, and a scalar ref as parameters. Returns 1 on success, undef on error. The subref will be called with an arp hash ref in dnet format, and the scalar ref as parameters.
dnet_arp_close (scalar)
Takes an arp handle as parameter. Returns the handle on success, undef on error.
π₯ Firewall subs β the dnet_fw_* serve
dnet_fw_open ()
Opens a fw handle. Returns a handle on success, undef on error.
dnet_fw_add (scalar, hashref)
Takes a fw handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_fw_delete (scalar, hashref)
Takes a fw handle, and a hash ref in dnet format as parameters. Returns 1 on success, undef on error.
dnet_fw_loop (scalar, subref, scalarref)
Takes a fw handle, a subref, and a scalar ref as parameters. Returns 1 on success, undef on error. The subref will be called with a fw hash ref in dnet format, and the scalar ref as parameters.
dnet_fw_close (scalar)
Takes a fw handle as parameter. Returns the handle on success, undef on error.
π«Ά Note for the completionists: the fw family hasopen,add,delete,loopandclose. There is nodnet_fw_geton this list. I'm not gatekeeping, that's just what's here.
π§ Tunnel subs β the dnet_tun_* portal
dnet_tun_open (scalar, scalar, scalar)
Creates a tunnel between src and dst IP addresses. Captured packets will have specified size. First argument is the source IP address, second the destination IP address, and the third is the capture size. Returns a handle on success, undef on error.
dnet_tun_fileno (scalar)
Takes a tun handle as parameter. Returns the file number on success, undef on error.
dnet_tun_name (scalar)
Takes a tun handle as parameter. Returns the interface name on success, undef on error.
dnet_tun_send (scalar, scalar, scalar)
Takes a tun handle, the raw data to send, and its size as parameters. Returns the number of bytes sent on success, undef on error.
dnet_tun_recv (scalar, scalar)
Takes a tun handle, and the maximum size to read as parameters. Returns the read buffer on success, undef on error.
dnet_tun_close (scalar)
Takes a tun handle as parameter. Returns the handle on success, undef on error.
π Ethernet subs β the dnet_eth_* lewk
dnet_eth_open (scalar)
Opens an eth handle. Takes an interface name as parameter. Returns a handle on success, undef on error.
dnet_eth_get (scalar)
Takes an eth handle as parameter. Returns the hardware address of currently opened eth handle on success, undef on error.
dnet_eth_set (scalar, scalar)
Takes an eth handle, and the hardware address to set as parameters. Returns 1 on success, undef on error.
dnet_eth_send (scalar, scalar, scalar)
Takes an eth handle, the raw data to send, and its size as parameters. Returns the number of bytes sent on success, undef on error.
dnet_eth_close (scalar)
Takes an eth handle as parameter. Returns the handle on success, undef on error.
β IP subs β the dnet_ip_* finale
dnet_ip_open ()
Opens an ip handle. Returns a handle on success, undef on error.
dnet_ip_checksum (scalar, scalar)
Takes a raw IPv4 frame as the first parameter, and its size as a second parameter. It then updates the frame with the good checksum. Returns nothing.
dnet_ip_send (scalar, scalar, scalar)
Takes an ip handle, the raw data to send, and its size as parameters. Returns the number of bytes sent on success, undef on error.
dnet_ip_close (scalar)
Takes an ip handle as parameter. Returns the handle on success, undef on error.
π OBSOLETE FUNCTIONS π
They should not be used anymore. You have been warned.
Likeβ¦ genuinely. This isn't a soft launch. These are the exes you don't text back. π
addr_bcastaddr_cmpaddr_netarp_addarp_deletearp_getintf_getintf_get_dstintf_get_srcroute_addroute_deleteroute_get
π CONSTANTS π
The glow-up squad of named values you can sprinkle into your hashes and comparisons. Zero guessing at magic numbers, we love that for us. β¨
DNET_ADDR_TYPE_NONEDNET_ADDR_TYPE_ETHDNET_ADDR_TYPE_IPDNET_ADDR_TYPE_IP6DNET_FW_OP_ALLOWDNET_FW_OP_BLOCKDNET_FW_DIR_INDNET_FW_DIR_OUTDNET_INTF_TYPE_OTHERDNET_INTF_TYPE_ETHDNET_INTF_TYPE_LOOPBACKDNET_INTF_TYPE_TUNDNET_INTF_FLAG_UPDNET_INTF_FLAG_LOOPBACKDNET_INTF_FLAG_POINTOPOINTDNET_INTF_FLAG_NOARPDNET_INTF_FLAG_BROADCASTDNET_INTF_FLAG_MULTICAST
π COPYRIGHT AND LICENSE π
You may distribute this module under the terms of the BSD license. See LICENSE file in the source distribution archive.
Copyright (c) 2004, Vlad Manilici
Copyright (c) 2008-2024, Patrice <GomoR> Auffret
π«Ά SEE ALSO π«Ά
And that's the whole page, bestie β this was Net::Libdnet(3pm), generated with perl v5.40.1 and dated 2025-03-01. Now go open a handle and slay. Periodt. πβ¨π
π¦ where this page comes from
- debian trixie: libnet-libdnet-perl 0.990-1 Β· BSD-2-Clause
- ubuntu resolute: libnet-libdnet-perl 0.990-1 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms