rtnl. listener

Represents a netlink listener resource.

Example
const nlListener = listener((msg) => {
    print('Received netlink message:', msg, '\n');
}, [RTM_NEWROUTE, RTM_DELROUTE]);

nlListener.set_commands([RTM_GETLINK, RTM_SETLINK]);

nlListener.close();

Methods

close() → {boolean}

Close a netlink listener.

Closes the netlink listener and stops receiving messages.

Returns: boolean
  • true if successful, false on error
Example
// Close the listener
listener.close();

set_commands(commands) → {boolean}

Set the commands for a netlink listener.

Updates the set of netlink commands that the listener will receive.

Parameters:
NameTypeDescription
commandsstring[]

Array of netlink commands to listen for

Returns: boolean
  • true if successful, false on error
Example
// Update listener to only receive route messages
listener.set_commands([RTM_NEWROUTE, RTM_DELROUTE]);

set_overrun_handler(handlernullable) → {boolean}nullable

Set the overrun handler of a netlink listener.

The kernel drops event messages when the receive buffer of the event socket is full. The module then enlarges the buffer and calls the overrun handler of each listener without arguments.

Events of every joined group can be lost, including deletions. The handler must dump each table that the script keeps state for. It must treat every entry of the dump as new or changed, and every cached entry that is missing from the dump as deleted.

Parameters:
NameTypeDescription
handlerfunction(nullable)

The function to call after an overrun, or null to remove it

Returns: boolean
  • true if successful, null if the listener is closed
Example
// Rebuild the link cache after lost events
listener.set_overrun_handler(() => {
    let dump = request(RTM_GETLINK, NLM_F_DUMP, {});
    let seen = {};

    if (dump === false)
        return;

    for (let link in dump ?? []) {
        seen[link.ifname] = true;
        links[link.ifname] = link;
    }

    for (let name in keys(links))
        if (!seen[name])
            delete links[name];
});