Module: netlink
Provides a basic implementation of the netlink protocol.
class NetlinkMessage
A simple class that holds a netlink message.
class NetlinkSocket
A socket that implements the netlink protocol.
async with connect(family: int) -> NetlinkSocket
Creates an AF_NETLINK socket for the given family.
Netlink Families
NETLINK_ROUTE = 0
NETLINK_UNUSED = 1
NETLINK_USERSOCK = 2
NETLINK_FIREWALL = 3
NETLINK_SOCK_DIAG = 4
NETLINK_NFLOG = 5
NETLINK_XFRM = 6
NETLINK_SELINUX = 7
NETLINK_ISCSI = 8
NETLINK_AUDIT = 9
NETLINK_FIB_LOOKUP = 10
NETLINK_CONNECTOR = 11
NETLINK_NETFILTER = 12
NETLINK_IP6_FW = 13
NETLINK_DNRTMSG = 14
NETLINK_KOBJECT_UEVENT = 15
NETLINK_GENERIC = 16
NETLINK_SCSITRANSPORT = 18
NETLINK_ECRYPTFS = 19
NETLINK_RDMMA = 20
NETLINK_CRYPTO = 21
NETLINK_SMC = 22
NETLINK_INET_DIAG = NETLINK_SOCK_DIAG
Netlink Flags
NLM_F_REQUEST = 1
NLM_F_MULTI = 2
NLM_F_ACK = 4
NLM_F_ECHO = 8
NLM_F_DUMP_INTR = 16
NLM_F_DUMP_FILTERED = 32
NLM_F_ROOT = 0x100
NLM_F_MATCH = 0x200
NLM_F_ATOMIC = 0x400
NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH
NLM_F_REPLACE = 0x100
NLM_F_EXCL = 0x200
NLM_F_CREATE = 0x400
NLM_F_APPEND = 0x800
NLM_F_NONREC = 0x100
NLM_F_CAPPED = 0x100
NLM_F_ACK_TLVS = 0x200
Message Types
NLMSG_NOOP = 1
NLMSG_ERROR = 2
NLMSG_DONE = 3
NLMSG_OVERRUN = 4
NLMSG_MIN_TYPE = 16
NetlinkMessage
type: int
flags: int
payload: bytes
NetlinkSocket
def add_membership(id: int) -> None
Adds the netlink socket to a multicast group.
async def request(type: int, payload: bytes = b"", flags: int = 0) -> list[NetlinkMessage]
Sends a netlink request to the kernel and waits for an acknowledgement. The flags argument can be used to specify additional flags (e.g. NLM_F_DUMP). The flags NLM_F_REQUEST and NLM_F_ACK are always added to the request automatically. Returns the messages that were received from the kernel with a matching sequence id. Raises OSError if the kernel returns an error code.
async def receive() -> NetlinkMessage
Receives a netlink message from the kernel with sequence id 0.
async def noop() -> None
Sends NLMSG_NOOP to the kernel and waits for acknowledgement. Basically, this method does nothing.