Socket::Netlink(3pm)
β¨ NAME β¨
Socket::Netlink β bestie, it's the interface to Linux's PF_NETLINK socket family π
π SYNOPSIS π
use Socket;
use Socket::Netlink qw( :DEFAULT pack_nlmsghdr unpack_nlmsghdr );
socket( my $sock, PF_NETLINK, SOCK_RAW, 0 ) or die "socket: $!";
send( $sock, pack_nlmsghdr( 18, NLM_F_REQUEST|NLM_F_DUMP, 0, 0,
"\0\0\0\0\0\0\0\0" ), 0 )
or die "send: $!";
recv( $sock, my $buffer, 65536, 0 ) or die "recv: $!";
printf "Received type=%d flags=%x:\n%v02x\n",
( unpack_nlmsghdr( $buffer ) )[ 0, 1, 4 ];
π¦ DESCRIPTION π¦
Okay so this module is where all the low-level constants and the structure-handling functions live β literally everything you need to actually use Linux's PF_NETLINK socket family. π
But ngl, bestie? The docs suggest you go use the high-level object interface instead, because it's so much nicer to your brain. See IO::Socket::Netlink. π
π CONSTANTS π
The following constants are exported, no cap:
PF_NETLINKβ the packet family, aka the one you hand to yoursocket()calls πΈAF_NETLINKβ the address family β¨
π ADDRESS FUNCTIONS π
The following pair of functions operate on AF_NETLINK address structures. The meanings of the parameters are:
pidβ the unique endpoint number for this netlink socket. If you pass it as0to thebind()syscall, the kernel will allocate an endpoint number of the process's PID. iconic behaviour tbh π«Άgroupsβ a 32-bit bitmask of the multicast groups to join π―
π pack_sockaddr_nl π
$addr = pack_sockaddr_nl( $pid, $groups )
Returns a sockaddr_nl structure with the fields packed into it. Slay.
π unpack_sockaddr_nl π
( $pid, $groups ) = unpack_sockaddr_nl( $addr )
Takes a sockaddr_nl structure and returns the unpacked fields from it. Reverse uno card. π
π₯ STRUCTURE FUNCTIONS π₯
The following function pairs operate on structure types used by netlink.
π« pack_nlmsghdr π«
$buffer = pack_nlmsghdr( $type, $flags, $seq, $pid, $body )
π« unpack_nlmsghdr π«
( $type, $flags, $seq, $pid, $body, $morebuffer ) = unpack_nlmsghdr( $buffer )
Pack or unpack a struct nlmsghdr and its payload body. Periodt. π
Because a single netlink message can contain more than one payload body, the unpack_nlmsghdr function will return the remaining buffer after unpacking the first message, in case there are others. And if there are no more? Then the $morebuffer list element just... won't be returned. It ghosts you. π
while( defined $buffer ) {
( my ( $type, $flags, $seq, $pid, $body ), $buffer ) = unpack_nlmsghdr( $buffer );
...
}
There is no similar functionality for pack_nlmsghdr β bestie, you just concatenate multiple results together if you wanna send more than one message. π§
π« pack_nlmsgerr π«
$buffer = pack_nlmsgerr( $error, $msg )
π« unpack_nlmsgerr π«
( $error, $msg ) = unpack_nlmsgerr( $buffer )
Pack or unpack a struct nlmsgerr. Here's the tea: the kernel expects or reports negative integers in its structures, so these functions take or return normal positive error values that are suitable for use with $!. π
π« pack_nlattrs π«
$buffer = pack_nlattrs( %attrs )
π« unpack_nlattrs π«
%attrs = unpack_nlattrs( $buffer )
Pack or unpack a list of netlink attributes. π
These functions take or return even-sized lists of $type, $value pairs. The type will be the number in the netlink attribute message, and the value will be a plain packed string buffer. It is the caller's responsibility to further pack/unpack this buffer as appropriate for the specific type. You got this, bestie. π«‘
And because these functions take/return even-sized lists, they may be passed or returned into hashes. β¨
π SEE ALSO π
netlink(7)β netlink β Communication between kernel and userspace (AF_NETLINK)IO::Socket::Netlinkβ Object interface toAF_NETLINKdomain sockets
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> β legend behaviour, honestly π
And that's the whole vibe check, bestie β this was Socket::Netlink(3pm) from the User Contributed Perl Documentation, sitting pretty at perl v5.42.0, dated 2025-07-25. Go forth and netlink. Mwah. π
π
π¦ where this page comes from
- fedora 43: perl-Socket-Netlink 0.05-32.fc43 Β· 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